publish to npmjs; specify FullDate constructor type
change the package name in generated code
This commit is contained in:
parent
3b1562315d
commit
65482a6890
6 changed files with 39 additions and 17 deletions
26
README.md
26
README.md
|
@ -9,12 +9,12 @@ It is a TypeScript code generator which generates TypeScript classes and interfa
|
||||||
- `apiRouter`, server api prototype using [koa router](https://github.com/koajs/router)
|
- `apiRouter`, server api prototype using [koa router](https://github.com/koajs/router)
|
||||||
- `ClientAPI`, client api implementation using [axios](https://github.com/axios/axios)
|
- `ClientAPI`, client api implementation using [axios](https://github.com/axios/axios)
|
||||||
|
|
||||||
This tool assume you use **koa router** for server and **axios** for client.
|
This tool assumes you use **koa router** for server and **axios** for client.
|
||||||
|
|
||||||
## How to use it?
|
## How to use it?
|
||||||
### 0. Install this tool
|
### 0. Install this tool
|
||||||
```
|
```
|
||||||
yarn add -D https://github.com/supmiku39/api-codegen-ts
|
yarn add -D @supmiku39/api-ts-gen
|
||||||
```
|
```
|
||||||
Also, install the dependencies that generated code will use.
|
Also, install the dependencies that generated code will use.
|
||||||
```
|
```
|
||||||
|
@ -62,10 +62,10 @@ module.exports = {
|
||||||
yarn run api-codegen <your-openapi-document> [-o <output-dir>]
|
yarn run api-codegen <your-openapi-document> [-o <output-dir>]
|
||||||
```
|
```
|
||||||
The default output directory is `api/generated`.
|
The default output directory is `api/generated`.
|
||||||
For example, if you put your api document at `api.yml`, and want the generated code put in `api-generated/` directory, you can execute
|
For example, if you put your api document at `api.yml`, and want the generated code put in `generated/` directory, you can execute
|
||||||
```
|
```
|
||||||
# example
|
# example
|
||||||
yarn run api-codegen api.yml -o api-generated
|
yarn run api-codegen api.yml -o generated
|
||||||
```
|
```
|
||||||
### 4. Implement server api
|
### 4. Implement server api
|
||||||
```
|
```
|
||||||
|
@ -117,14 +117,14 @@ import apiRouter from '#api/apiRouter';
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
// some other entry
|
// some other entry
|
||||||
|
|
||||||
app.use(apiRouter.prefix('your/api/prefix').routes());
|
app.use(apiRouter.prefix('/your/api/prefix').routes());
|
||||||
// or simply app.use(apiRouter.routes());
|
// or simply app.use(apiRouter.routes());
|
||||||
|
|
||||||
app.listen(yourAppListenPort);
|
app.listen(yourAppListenPort);
|
||||||
```
|
```
|
||||||
|
|
||||||
### 6. Use api in client
|
### 6. Use api in client
|
||||||
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.
|
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.
|
||||||
|
|
||||||
```
|
```
|
||||||
api.{operationId}([path], [query], [header], [cookie], [body])
|
api.{operationId}([path], [query], [header], [cookie], [body])
|
||||||
|
@ -135,7 +135,7 @@ where the `path`, `query`, `header`, `cookie` is a object whose key is the `name
|
||||||
|
|
||||||
```
|
```
|
||||||
import api from '#api/ClientAPI';
|
import api from '#api/ClientAPI';
|
||||||
// import {FullDate} from 'api-codegen-ts/utils';
|
// import {FullDate} from '@supmiku39/api-ts-gen/utils';
|
||||||
// import {SchemaA} from '#api/schemas';
|
// import {SchemaA} from '#api/schemas';
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
@ -150,23 +150,24 @@ api.operationWithPathAndQueryAndBody({
|
||||||
queryName2: queryvalue2,
|
queryName2: queryvalue2,
|
||||||
}, body);
|
}, body);
|
||||||
```
|
```
|
||||||
If you set the prefix of the api, you have to set the `$baseURL` of api.
|
If you set the prefix of the api, you have to set the `$baseURL` of client api.
|
||||||
```
|
```
|
||||||
api.$baseURL = 'same/as/the/prefix/in/server';
|
api.$baseURL = '/same/as/the/prefix/in/server';
|
||||||
```
|
```
|
||||||
|
|
||||||
#### FullDate
|
#### FullDate
|
||||||
If the format is `string` `date`, you should use `FullDate` instead of `Date`.
|
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.
|
`FullDate` is a wrapper of `Date`, which implements `.toString()`, `.toJSON()` and `.valueOf()` to make it more convenience to convert it to String or JSON.
|
||||||
|
|
||||||
Import `FullDate` class from `api-codegen-ts/utils`.
|
Import `FullDate` class from `@supmiku39/api-ts-gen/utils`.
|
||||||
```
|
```
|
||||||
import {FullDate} from 'api-codegen-ts/utils';
|
import {FullDate} from '@supmiku39/api-ts-gen/utils';
|
||||||
|
|
||||||
// initialization
|
// initialization
|
||||||
new FullDate(new Date()); // from a Date instance
|
new FullDate(new Date()); // from a Date instance
|
||||||
new FullDate(); // today
|
new FullDate(); // today
|
||||||
new FullDate('2012-03-31'); // 2012-03-31
|
new FullDate('2012-03-31'); // 2012-03-31
|
||||||
|
new FullDate(2015, 5); // 2015-05-01
|
||||||
new FullDate(2015, 5, 4); // 2015-05-04
|
new FullDate(2015, 5, 4); // 2015-05-04
|
||||||
new FullDate(1449446400000); // 2015-12-07
|
new FullDate(1449446400000); // 2015-12-07
|
||||||
|
|
||||||
|
@ -476,6 +477,9 @@ This tool only supports `application/json` type for request and response body. A
|
||||||
Other $ref like requestBody, responseBody are not supported currently.
|
Other $ref like requestBody, responseBody are not supported currently.
|
||||||
|
|
||||||
## Versions
|
## Versions
|
||||||
|
#### 1.1.2
|
||||||
|
- publish to npmjs and change the package name in generated code
|
||||||
|
- specify constructor argument type of FullDate
|
||||||
#### 1.1.1
|
#### 1.1.1
|
||||||
- implement FullDate#distanceFrom(d0)
|
- implement FullDate#distanceFrom(d0)
|
||||||
- fix FullDate timezone bug, use UTC instead
|
- fix FullDate timezone bug, use UTC instead
|
||||||
|
|
2
dist/Config.js
vendored
2
dist/Config.js
vendored
|
@ -15,7 +15,7 @@ exports.configDefault = {
|
||||||
// TS path
|
// TS path
|
||||||
apiDirTSPath: '#api',
|
apiDirTSPath: '#api',
|
||||||
ServerAPITSPath: '#ServerAPI',
|
ServerAPITSPath: '#ServerAPI',
|
||||||
utilsTSPath: 'api-codegen-ts/utils',
|
utilsTSPath: '@supmiku39/api-ts-gen/utils',
|
||||||
stateTSPath: null,
|
stateTSPath: null,
|
||||||
// other
|
// other
|
||||||
outputDir: 'api/generated',
|
outputDir: 'api/generated',
|
||||||
|
|
7
dist/utils/FullDate.d.ts
vendored
7
dist/utils/FullDate.d.ts
vendored
|
@ -1,6 +1,11 @@
|
||||||
export declare class FullDate {
|
export declare class FullDate {
|
||||||
private _date;
|
private _date;
|
||||||
constructor(...argv: any);
|
constructor();
|
||||||
|
constructor(copyFrom: FullDate);
|
||||||
|
constructor(date: Date);
|
||||||
|
constructor(s: string);
|
||||||
|
constructor(n: number);
|
||||||
|
constructor(y: number, m: number, d?: number);
|
||||||
toString(): string;
|
toString(): string;
|
||||||
toJSON(): string;
|
toJSON(): string;
|
||||||
valueOf(): number;
|
valueOf(): number;
|
||||||
|
|
|
@ -38,7 +38,7 @@ export const configDefault: ConfigOptional = {
|
||||||
// TS path
|
// TS path
|
||||||
apiDirTSPath: '#api',
|
apiDirTSPath: '#api',
|
||||||
ServerAPITSPath: '#ServerAPI',
|
ServerAPITSPath: '#ServerAPI',
|
||||||
utilsTSPath: 'api-codegen-ts/utils',
|
utilsTSPath: '@supmiku39/api-ts-gen/utils',
|
||||||
stateTSPath: null,
|
stateTSPath: null,
|
||||||
// other
|
// other
|
||||||
outputDir: 'api/generated',
|
outputDir: 'api/generated',
|
||||||
|
|
|
@ -4,7 +4,14 @@ function removeTime(date: Date): Date {
|
||||||
}
|
}
|
||||||
export class FullDate {
|
export class FullDate {
|
||||||
private _date: Date;
|
private _date: Date;
|
||||||
constructor(...argv: any) {
|
|
||||||
|
constructor();
|
||||||
|
constructor(copyFrom: FullDate);
|
||||||
|
constructor(date: Date);
|
||||||
|
constructor(s: string);
|
||||||
|
constructor(n: number);
|
||||||
|
constructor(y: number, m: number, d?: number);
|
||||||
|
constructor(...argv: [any?, number?, number?]) {
|
||||||
this._date = removeTime((() => {
|
this._date = removeTime((() => {
|
||||||
if (argv.length == 0) return new Date();
|
if (argv.length == 0) return new Date();
|
||||||
if (argv.length == 1) {
|
if (argv.length == 1) {
|
||||||
|
|
10
package.json
10
package.json
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@supmiku39/openapi-codegen-ts",
|
"name": "@supmiku39/api-ts-gen",
|
||||||
"version": "1.1.1",
|
"version": "1.1.2",
|
||||||
"description": "OpenAPI code generator for TypeScript",
|
"description": "OpenAPI code generator for TypeScript",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
@ -11,6 +11,12 @@
|
||||||
},
|
},
|
||||||
"author": "supmiku39",
|
"author": "supmiku39",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"repository": {
|
||||||
|
"url": "https://github.com/supmiku39/api-ts-gen",
|
||||||
|
"type": "git"
|
||||||
|
},
|
||||||
|
"keywords": ["openapi", "swagger", "typescript", "codegen"],
|
||||||
|
"files": ["dist/*", "bin/*", "utils"],
|
||||||
"bin": {
|
"bin": {
|
||||||
"api-codegen": "bin/api-codegen.js"
|
"api-codegen": "bin/api-codegen.js"
|
||||||
},
|
},
|
||||||
|
|
Reference in a new issue