2020-04-05 00:57:34 +09:00
# OpenAPI codegen for TypeScript
2020-04-09 20:21:58 +09:00
## What is this?
2020-04-15 07:45:51 +09:00
This is a TypeScript code generator which generates TypeScript types and interfaces base on your [OpenAPI document ](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md ), including
2020-04-09 20:21:58 +09:00
- `schemas`
2020-04-15 07:45:51 +09:00
- `IHandler` , types and interfaces for both server and client api
- `apiRouter` , server api partial implementation using [koa router ](https://github.com/koajs/router )
2020-04-09 20:21:58 +09:00
- `ClientAPI` , client api implementation using [axios ](https://github.com/axios/axios )
2020-04-10 21:17:34 +09:00
This tool assumes you use **koa router** for server and **axios** for client.
2020-04-09 20:21:58 +09:00
2020-04-11 02:22:47 +09:00
## How to use this tool?
### 0. Install it
2020-04-09 20:21:58 +09:00
```
2020-04-10 21:17:34 +09:00
yarn add -D @supmiku39/api -ts-gen
2020-04-09 20:21:58 +09:00
```
Also, install the dependencies that generated code will use.
```
yarn add koa @koa/router koa-body
yarn add -D @types/koa @types/koa__router axios
```
### 1. Write your OpenAPI document
You can use [Swagger Editor ](https://swagger.io/tools/swagger-editor/ ) to edit your api document. Make sure to follow the [OpenAPI Specification ](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md ).
2020-04-11 02:22:47 +09:00
This tool generates code from `paths` and `components/schemas` .
2020-04-09 20:21:58 +09:00
:warning: only `get` , `post` , `update` , `delete` , `patch` are supported in `paths`
### 2. Set path alias
Determine the **output directory for generated code** and the **server api implementation location** , and edit your `tsconfig.json` :
```
{
// ...
"compilerOptions": {
// ...
"baseUrl": ".", // or your project root
"paths": {
// ...
"#api/*": ["path/to/your/output/directory/for/generated/code/*"],
"#ServerAPI": ["path/to/your/server/api/implementation"]
}
}
}
```
If you use webpack, you should also edit your `webpack.config.js` :
```
module.exports = {
// ...
resolve: {
// ...
alias: {
'#api': 'path/to/your/output/directory/for/generated/code',
// #ServerAPI is not required
},
},
};
```
### 3. Run this tool
```
2020-04-15 07:45:51 +09:00
yarn run api-codegen < your-openapi-document > [-o < output-dir > ] [-s < ctx.state-interface-path > ]
2020-04-09 20:21:58 +09:00
```
The default output directory is `api/generated` .
2020-04-10 21:17:34 +09:00
For example, if you put your api document at `api.yml` , and want the generated code put in `generated/` directory, you can execute
2020-04-09 20:21:58 +09:00
```
# example
2020-04-10 21:17:34 +09:00
yarn run api-codegen api.yml -o generated
2020-04-09 20:21:58 +09:00
```
### 4. Implement server api
```
2020-04-15 07:45:51 +09:00
import {IServerAPI} from '#api/IServerAPI';
2020-04-09 20:21:58 +09:00
export default {
2020-04-15 07:45:51 +09:00
operationId: async (req, state, ctx) => {
2020-04-09 20:21:58 +09:00
// ...
2020-04-15 17:34:34 +09:00
return [status, body];
2020-04-09 20:21:58 +09:00
},
// ...
2020-04-15 07:45:51 +09:00
} as IServerAPI;
2020-04-09 20:21:58 +09:00
```
The function name is the `operationId` defined in the api document.
2020-04-15 07:45:51 +09:00
There are 3 arguments passed to the function.
2020-04-09 20:21:58 +09:00
#### req
The request parameters and body, defined in `parameters` and `requestBody` .
Any parameter will be put in `req.{in}.{name}` , where `{in}` is one of `path` , `query` , `header` , `cookie` .
`requestBody` will be put in `req.body` .
#### state
Alias to `ctx.state`
2020-04-15 17:34:34 +09:00
You can specify the type of `ctx.state` by setting the export default type to `IServerAPI<YourStateType>` .
```
// example
import {IServerAPI} from '#api/IHandler';
interface IState {
user: {
id: number;
}
}
export default {
// ...
operationId: async (req, state, ctx) => {
// state has IState type here
state.user.id // number
// ...
},
} as IServerAPI< IState > // specify ctx.state type to IState
```
2020-04-09 20:21:58 +09:00
#### ctx
The `ctx` object from koa router. **Avoid to use it** unless required.
```
2020-04-15 17:34:34 +09:00
// Don't do this
2020-04-09 20:21:58 +09:00
ctx.body = responseBody;
ctx.status = statusCode;
// Do this
2020-04-15 17:34:34 +09:00
return [statusCode, responseBody];
2020-04-09 20:21:58 +09:00
```
2020-04-15 07:45:51 +09:00
#### return value
`[status, body]`
If the responseBody is not required, you can omit it or pass anything to it.
```
// example
return [200, {...}];
return [404, 'some response string'];
// if responseBody is not required
return [statusCode]; // OK
return [statusCode, 'message string']; // OK
return [statusCode, {some: 'object'}]; // OK
```
2020-04-09 20:21:58 +09:00
### 5. Mount the api router to server
```
import * as Koa from 'koa';
import apiRouter from '#api/apiRouter';
const app = new Koa();
// some other entry
2020-04-10 21:17:34 +09:00
app.use(apiRouter.prefix('/your/api/prefix').routes());
2020-04-09 20:21:58 +09:00
// or simply app.use(apiRouter.routes());
app.listen(yourAppListenPort);
```
### 6. Use api in client
2020-04-10 21:17:34 +09:00
Simply import and use it! Like the server api, the function name will be the `operationId` defined in the api document, and the parameters will be the `parameters` and `requestBody` if defined.
2020-04-09 20:21:58 +09:00
```
api.{operationId}([path], [query], [header], [cookie], [body])
```
where the `path` , `query` , `header` , `cookie` is a object whose key is the `name` defined in `parameters` in the api document, and `body` is the `requestBody` .
:bulb: Note that if the method is `patch` , `req.body` will be `Partial` , which means that any property can be omitted.
```
import api from '#api/ClientAPI';
2020-04-10 21:17:34 +09:00
// import {FullDate} from '@supmiku39/api-ts-gen/utils';
2020-04-09 20:21:58 +09:00
// import {SchemaA} from '#api/schemas';
// ...
api.operationWithPath({pathName: pathValue});
api.operationWithQueryAndBody({queryName: queryValue}, body);
api.operationWithPathAndQueryAndBody({
pathName1: pathvalue1,
pathName2: pathvalue2,
pathName3: pathvalue3,
}, {
queryName1: queryvalue1,
queryName2: queryvalue2,
}, body);
```
2020-04-10 21:17:34 +09:00
If you set the prefix of the api, you have to set the `$baseURL` of client api.
2020-04-09 20:21:58 +09:00
```
2020-04-10 21:17:34 +09:00
api.$baseURL = '/same/as/the/prefix/in/server';
2020-04-09 20:21:58 +09:00
```
#### FullDate
If the format is `string` `date` , you should use `FullDate` instead of `Date` .
`FullDate` is a wrapper of `Date` , which implements `.toString()` , `.toJSON()` and `.valueOf()` to make it more convenience to convert it to String or JSON.
2020-04-10 21:17:34 +09:00
Import `FullDate` class from `@supmiku39/api-ts-gen/utils` .
2020-04-09 20:21:58 +09:00
```
2020-04-10 21:17:34 +09:00
import {FullDate} from '@supmiku39/api-ts-gen/utils';
2020-04-09 20:21:58 +09:00
// initialization
new FullDate(new Date()); // from a Date instance
new FullDate(); // today
new FullDate('2012-03-31'); // 2012-03-31
2020-04-10 21:17:34 +09:00
new FullDate(2015, 5); // 2015-05-01
2020-04-09 20:21:58 +09:00
new FullDate(2015, 5, 4); // 2015-05-04
new FullDate(1449446400000); // 2015-12-07
// getter
const d = new FullDate();
d.date // a Date instance clone
d.year // date.getUTCFullYear()
d.month // date.getUTCMonth()+1
d.day // date.getUTCDate()
d.dayOfWeek // date.getUTCDay()
// setter
d.year = 2018; // date.setUTCFullYear(2018)
d.month = 3; // date.setUTCMonth(3-1)
d.day = 5; // date.setUTCDate(5)
// method
/*
.advance(period: number): FullDate
return a new FullDate that advanced by $peroid days
*/
const d0 = new FullDate('2015-05-04');
d0.advance(217) // 2015-12-07
d0.advance(-1129) // 2012-03-31
/*
.distanceFrom(d0: FullDate): number
return the distance(unit: day) from d0
*/
d0.distanceFrom(new FullDate(2018, 3, 5)); // -1036
d0.distanceFrom(new FullDate(2007, 8, 31)); // 2803
```
:warning: `FullDate` use `month` from 1 to 12, which differs from `Date` . Also, `FullDate` use `day` and `dayOfWeek` instead of `date` and `day` .
#### Schema
2020-04-15 07:45:51 +09:00
It is not necessary to use `SomeSchema.from(...)` of `SomeSchema.Partial(...)` to create an instance to pass to the client api. Instead, simply use an object literal.
2020-04-09 20:21:58 +09:00
2020-04-15 07:45:51 +09:00
Import the schema type interfaces from `#api/schemas` .
2020-04-09 20:21:58 +09:00
```
import {SchemaA, SchemaB} from '#api/schemas';
api.postA({id: 3}, {...}) // OK, simpler
2020-04-15 07:45:51 +09:00
api.postA({id: 3}, SchemaA.from(...)); // Well, still OK
2020-04-09 20:21:58 +09:00
api.patchB({id: 3}, {...}) // OK, simpler
api.patchB({id: 3}, SchemaB.Partial(...)); // Well, still OK
```
2020-04-15 07:45:51 +09:00
:warning: From v2.0.0, `Schemas` are no longer class. Instead, it became a `interface` and a Object including `from` , `Partial` , and `fields` properties.
2020-04-09 20:21:58 +09:00
#### Handling response
The api is a async function that returns a `APIPromise` , which is a extended Promise. The promise is resolved if the status code is `2xx` , otherwise it is rejected.
Every operation has a different `APIPromise` definition according to the possible response **status code** .
If there is only one `2xx` in the status code and error handling is not required, you can simply use `await` to get the response body.
```
const a: SchemaA = await api.getA(...);
```
2020-04-11 02:22:47 +09:00
However, if you want to handle several kinds of response, you can use `.on(statusCode, responseBody => handler)` where `XXX` is the status code.
2020-04-09 20:21:58 +09:00
```
api.getA(...)
2020-04-11 02:22:47 +09:00
.on(200, a => { // the compiler knows that type of `a` is `SchemaA`
2020-04-09 20:21:58 +09:00
console.log('Get a successfully', a);
})
2020-04-11 02:22:47 +09:00
.on(404, msg => {
2020-04-09 20:21:58 +09:00
console.log('Not found with msg', msg);
})
2020-04-11 02:22:47 +09:00
.on(403, () => {
2020-04-09 20:21:58 +09:00
console.log('Forbidden with no msg');
});
```
2020-04-15 07:45:51 +09:00
:warning: For any status, you can only call `.on(status, ...)` once.
```
// OK
api.getA(...)
.on(200, a => ...)
.on(403, () => ...)
// NG
api.getA(...)
.on(200, a => ...)
.on(200, () => ...) // Compile Error!
```
2020-04-09 20:21:58 +09:00
## Details
### Type Conversion
[OpenAPI data types ](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#dataTypes ) will be convert to TypeScript types as following:
| type | format | | TypeScript |
|----|----|----|----|
|`integer`||→|`number`|
|`number`||→|`number`|
|`string`||→|`string`|
|`boolean`||→|`boolean`|
|`string`|`date`|→|`FullDate`|
|`string`|`date-time`|→|`Date`|
In addition, assigning `nullable: true` makes it nullable.
### Strict Type Parser(STP)
To ensure type safety, `STP` provides some static functions to convert `any` type to specific type.
| type | format | valid type | note |
|----|----|----|----|
|`integer`|`int32`|`number`, `string` |integer between `-2147483648` and `2147483647` only|
|`integer`|`int64`|`number`, `string` |no check for range and floating point|
|`number`||`number`, `string` |`NaN` and `Infinity` is valid|
|`string`||`string`||
2020-04-16 02:05:04 +09:00
|`boolean`||`boolean`, `number` , `string` ||
2020-04-09 20:21:58 +09:00
|`string`|`date`|`FullDate`, `Date` , `number` , `string` ||
|`string`|`date-time`|`Date`, `number` , `string` ||
|`string`|`byte`|`string`, `Buffer` |no validation check|
|`string`|`binary`|`string`, `Buffer` |no validation check|
In addition, assigning `nullable: true` makes it nullable.
```
// integer.int32
39 // OK
'39' // OK
'1e3' // NG, no scientific notation
3.14 // NG, not a integer
2147483648 // NG, overflow
'1e100' // NG, overflow
NaN // NG, not a integer
Infinity // NG, not a integer
'pi' // NG, not a number
// integer.int64, number
39 // OK
'39' // OK
'1e3' // OK
3.14 // OK, although it is convertible to int64
2147483648 // OK
'1e100' // OK, although too big for int64
NaN // OK, although it is convertible to int64
Infinity // OK, although it is convertible to int64
'pi' // NG, not a number
// boolean
true, false // OK
'true', 'false' // OK
2020-04-16 02:05:04 +09:00
0, 1, -1, 3.14, Infinity, NaN // OK (Only 0 is converted to false)
2020-04-09 20:21:58 +09:00
'T', 'F' // NG, only 'true' and 'false' are valid
2020-04-16 02:05:04 +09:00
null, {} // NG
2020-04-09 20:21:58 +09:00
// FullDate
new FullDate() // OK
new Date() // OK
'2017-05-28' // OK
1449446400000 // OK, 2015-12-07
'today' // NG, not a valid Date
// Date
new Date() // OK
'2017-05-28' // OK
1449446400000 // OK, 2015-12-07T00:00:00.000Z
new FullDate() // NG
'today' // NG, not a valid Date
// string.byte
'5aeJ5qeY44GL44KP44GE44GE' // OK
'********' // Well, OK, although invalid for a base64 string
Buffer.from('nya') // OK, a Buffer(node.js)
0x52391207 // NG, number is invalid
new Blob([]) // NG, Blob is not supported
// string.
'e5a789e6a798e3818be3828fe38184e38184' // OK
'********' // Well, OK, although invalid for a hex string
Buffer.from('nya') // OK, a Buffer(node.js)
0x52391207 // NG, number is invalid
new Blob([]) // NG, Blob is not supported
```
### Schema
2020-04-15 07:45:51 +09:00
Base on `#/components/schemas` , it generates interface definitions and constructor functions in `schemas.ts` .
#### Interface Definition
2020-04-09 20:21:58 +09:00
For example,
```
Post:
type: "object"
properties:
id:
type: "integer"
format: "int32"
ts:
type: "string"
format: "date-time"
authorID
type: "integer"
format: "int32"
content:
type: "string"
nullable: true
images:
type: "array"
items:
type: "string"
format: "binary"
pinned:
type: "boolean"
```
will become
```
2020-04-15 07:45:51 +09:00
interface Post {
2020-04-09 20:21:58 +09:00
id: number;
ts: string;
authorID: number;
content: string | null;
images: string[];
pinned: boolean;
}
```
2020-04-15 07:45:51 +09:00
#### Constructor Function
It also generates constructor function `Schema.from` with **strict type checking** .
The constructor function takes exactly one argument with literal object type.
2020-04-09 20:21:58 +09:00
If any property of the argument is not convertible to expected type, it throws an `BadValueError` .
For example,
```
NamedCircle:
type: "object"
properties:
name:
type: "string"
radius:
type: "number"
format: "double"
color:
type: "string"
nullable: true
```
will become
```
2020-04-15 07:45:51 +09:00
interface NamedCircle {
2020-04-09 20:21:58 +09:00
name: string;
radius: number;
color: string | null;
}
```
Here are some examples for strict type checking:
```
2020-04-15 07:45:51 +09:00
NamedCircle.from({
2020-04-09 20:21:58 +09:00
name: 'red circle',
radius: 39,
color: 'red',
}); // OK
2020-04-15 07:45:51 +09:00
NamedCircle.from({
2020-04-09 20:21:58 +09:00
name: 'circle with null color',
radius: 0,
color: null,
}); // OK, color is nullable
2020-04-15 07:45:51 +09:00
NamedCircle.from({
2020-04-09 20:21:58 +09:00
name: 'circle with null color',
radius: 0,
color: undefined,
}); // Error! color should be a number or null
2020-04-15 07:45:51 +09:00
NamedCircle.from({
2020-04-09 20:21:58 +09:00
name: 'circle without given color',
radius: 0,
}); // Error! color should be given
2020-04-15 07:45:51 +09:00
NamedCircle.from({
2020-04-09 20:21:58 +09:00
name: 'circle with invalid radius',
radius: 'miku',
color: 'cyan',
}); // Error! radius should be a number
```
#### Partial Function
2020-04-15 07:45:51 +09:00
It also generates a function called `Partial` for initializing Partial type.
2020-04-09 20:21:58 +09:00
The function also takes exactly one argument.
If any property of the argument is absent or is undefined, the function will skip setting the property.
However, if the property is given but not convertible to expected type, it throws a `BadValueError` .
Here are some examples.
```
NamedCircle.Partial({
name: 'circle with radius 39',
radius: 39,
color: 'cyan',
}); // OK
NamedCircle.Partial({
name: 'circle without radius and color',
}); // OK
NamedCircle.Partial({
radius: 1207,
}); // OK
NamedCircle.Partial({}); // OK
NamedCircle.Partial({
radius: 1207,
color: null,
}); // OK, color can be null
NamedCircle.Partial({
name: undefined,
radius: 1207,
color: null,
}); // OK, just skip setting name
NamedCircle.Partial({
name: null,
radius: 'miku',
color: 'cyan',
}); // Error! name is not nullable
NamedCircle.Partial({
radius: 'miku',
color: 'cyan',
}); // Error! radius should be a number
```
2020-04-15 07:45:51 +09:00
:warning: Use **object literal** if possible. The `from` and `Partial` function are mainly for internal use, avoid to use them unless you want to safe and strict convert a any variable to a schema type.
### fields
`Schema` exposes its fields to `fields` constant.
Its type is `Array<keyof Schema>` .
```
NamedCircle.fields // ['name', 'radius', 'color']: Array< keyof NamedCircle >
```
2020-04-09 20:21:58 +09:00
## Limitations
### application/json only
This tool only supports `application/json` type for request and response body. Any other type like `multipart/form` or `image/*` are **not supported** and will be ignored.
### schema $ref only
Other $ref like requestBody, responseBody are not supported currently.
## Versions
2020-04-16 02:05:04 +09:00
#### 2.0.2
- make number convertible to boolean
2020-04-15 17:34:34 +09:00
#### 2.0.1
- use IState as a generic type and remove it from api-codegen.
2020-04-15 07:45:51 +09:00
#### 2.0.0
- simplify generated code
- merge all APIPromise class
- remove IServerAPI and IClientAPI
- remove res Object, return [status, body] in ServerAPI instead
- remove schema classes, use interface instead
- `-s` flag for `ctx.state` interface path
2020-04-15 07:15:54 +09:00
#### 1.1.3
- expose fields of schemas to XXX.fields(static variable)
2020-04-10 21:17:34 +09:00
#### 1.1.2
- publish to npmjs and change the package name in generated code
- specify constructor argument type of FullDate
2020-04-09 20:21:58 +09:00
#### 1.1.1
- implement FullDate#distanceFrom(d0)
- fix FullDate timezone bug, use UTC instead
#### 1.1.0
- fix Partial constructor, enhance error msg
- add int32 STP
#### 1.0.1
- implement FullDate getter, setter, function(advance)
#### 1.0.0
- ClientAPI, ServerAPI interface, Schema TS codegen implemented
- application/json only
- get, post, put, delete, patch only