openapi-typescript-codegen
Advanced tools
Comparing version
@@ -14,2 +14,3 @@ #!/usr/bin/env node | ||
.option('--client [value]', 'HTTP client to generate [fetch, xhr]', 'fetch') | ||
.option('--useOptions', 'Use options vs arguments style functions', false) | ||
.parse(process.argv); | ||
@@ -23,4 +24,5 @@ | ||
program.output, | ||
program.client | ||
program.client, | ||
program.useOptions | ||
); | ||
} |
@@ -12,5 +12,6 @@ export declare enum HttpClient { | ||
* @param httpClient The selected httpClient (fetch or XHR). | ||
* @param useOptions Use options or arguments functions. | ||
* @param write Write the files to disk (true or false) | ||
*/ | ||
export declare function generate(input: string, output: string, httpClient?: HttpClient, write?: boolean): void; | ||
export declare function generate(input: string, output: string, httpClient?: HttpClient, useOptions?: boolean, write?: boolean): void; | ||
export declare function compile(dir: string): void; |
@@ -23,5 +23,6 @@ "use strict"; | ||
* @param httpClient The selected httpClient (fetch or XHR). | ||
* @param useOptions Use options or arguments functions. | ||
* @param write Write the files to disk (true or false) | ||
*/ | ||
function generate(input, output, httpClient = HttpClient.FETCH, write = true) { | ||
function generate(input, output, httpClient = HttpClient.FETCH, useOptions = false, write = true) { | ||
const inputPath = path.resolve(process.cwd(), input); | ||
@@ -39,3 +40,3 @@ const outputPath = path.resolve(process.cwd(), output); | ||
if (write) { | ||
writeClient_1.writeClient(clientV2, httpClient, templates, outputPath); | ||
writeClient_1.writeClient(clientV2, httpClient, templates, outputPath, useOptions); | ||
} | ||
@@ -46,3 +47,3 @@ break; | ||
if (write) { | ||
writeClient_1.writeClient(clientV3, httpClient, templates, outputPath); | ||
writeClient_1.writeClient(clientV3, httpClient, templates, outputPath, useOptions); | ||
} | ||
@@ -49,0 +50,0 @@ break; |
@@ -22,3 +22,3 @@ "use strict"; | ||
}; | ||
const partials = path.resolve(__dirname, `../../src/templates//partials`); | ||
const partials = path.resolve(__dirname, `../../src/templates/partials`); | ||
const partialsFiles = glob.sync('*.hbs', { cwd: partials }); | ||
@@ -25,0 +25,0 @@ partialsFiles.forEach(partial => { |
@@ -9,4 +9,5 @@ import { Client } from '../client/interfaces/Client'; | ||
* @param templates Templates wrapper with all loaded Handlebars templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
* @param useOptions Use options or arguments functions. | ||
*/ | ||
export declare function writeClient(client: Client, httpClient: HttpClient, templates: Templates, outputPath: string): void; | ||
export declare function writeClient(client: Client, httpClient: HttpClient, templates: Templates, outputPath: string, useOptions: boolean): void; |
@@ -18,5 +18,6 @@ "use strict"; | ||
* @param templates Templates wrapper with all loaded Handlebars templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
* @param useOptions Use options or arguments functions. | ||
*/ | ||
function writeClient(client, httpClient, templates, outputPath) { | ||
function writeClient(client, httpClient, templates, outputPath, useOptions) { | ||
const outputPathCore = path.resolve(outputPath, 'core'); | ||
@@ -55,3 +56,3 @@ const outputPathModels = path.resolve(outputPath, 'models'); | ||
writeClientSchemas_1.writeClientSchemas(client.models, templates, outputPathSchemas); | ||
writeClientServices_1.writeClientServices(client.services, templates, outputPathServices); | ||
writeClientServices_1.writeClientServices(client.services, templates, outputPathServices, useOptions); | ||
writeClientSettings_1.writeClientSettings(client, httpClient, templates, outputPathCore); | ||
@@ -58,0 +59,0 @@ writeClientIndex_1.writeClientIndex(client, templates, outputPath); |
@@ -9,4 +9,4 @@ import { Client } from '../client/interfaces/Client'; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
*/ | ||
export declare function writeClientIndex(client: Client, templates: Templates, outputPath: string): void; |
@@ -13,3 +13,3 @@ "use strict"; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
*/ | ||
@@ -16,0 +16,0 @@ function writeClientIndex(client, templates, outputPath) { |
@@ -7,4 +7,4 @@ import { Model } from '../client/interfaces/Model'; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
*/ | ||
export declare function writeClientModels(models: Model[], templates: Templates, outputPath: string): void; |
@@ -11,3 +11,3 @@ "use strict"; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
*/ | ||
@@ -14,0 +14,0 @@ function writeClientModels(models, templates, outputPath) { |
@@ -7,4 +7,4 @@ import { Model } from '../client/interfaces/Model'; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
*/ | ||
export declare function writeClientSchemas(models: Model[], templates: Templates, outputPath: string): void; |
@@ -11,3 +11,3 @@ "use strict"; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
*/ | ||
@@ -14,0 +14,0 @@ function writeClientSchemas(models, templates, outputPath) { |
@@ -7,4 +7,5 @@ import { Service } from '../client/interfaces/Service'; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
* @param useOptions Use options or arguments functions. | ||
*/ | ||
export declare function writeClientServices(services: Service[], templates: Templates, outputPath: string): void; | ||
export declare function writeClientServices(services: Service[], templates: Templates, outputPath: string, useOptions: boolean): void; |
@@ -11,9 +11,13 @@ "use strict"; | ||
* @param templates The loaded handlebar templates. | ||
* @param outputPath | ||
* @param outputPath Directory to write the generated files to. | ||
* @param useOptions Use options or arguments functions. | ||
*/ | ||
function writeClientServices(services, templates, outputPath) { | ||
function writeClientServices(services, templates, outputPath, useOptions) { | ||
services.forEach(service => { | ||
const file = path.resolve(outputPath, `${service.name}.ts`); | ||
const templateData = exportService_1.exportService(service); | ||
const templateResult = templates.service(templateData); | ||
const templateResult = templates.service({ | ||
...templateData, | ||
useOptions, | ||
}); | ||
fs.writeFileSync(file, format_1.format(templateResult)); | ||
@@ -20,0 +24,0 @@ }); |
{ | ||
"name": "openapi-typescript-codegen", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.", | ||
@@ -5,0 +5,0 @@ "author": "Ferdi Koomen", |
102
README.md
@@ -39,9 +39,8 @@ # OpenAPI Typescript Codegen | ||
"scripts": { | ||
"generate": "openapi ./api/openapi.json ./dist" | ||
"generate": "openapi --input ./api/openapi.json --output ./dist" | ||
} | ||
... | ||
} | ||
``` | ||
Command line | ||
**Command line** | ||
@@ -51,8 +50,8 @@ ``` | ||
openapi ./api/openapi.json ./dist | ||
openapi --input ./api/openapi.json --output ./dist | ||
``` | ||
NodeJS API: | ||
**NodeJS API** | ||
``` | ||
```javascript | ||
const OpenAPI = require('openapi-typescript-codegen'); | ||
@@ -65,1 +64,92 @@ | ||
``` | ||
## Features | ||
### Argument-style vs. Object-style | ||
There's no [named parameter](https://en.wikipedia.org/wiki/Named_parameter) in JS/TS, because of that, | ||
we offer an option `--useOptions` to generate code in two different styles. | ||
Argument-style: | ||
```typescript | ||
function createUser(name: string, password: string, type?: string, address?: string) { | ||
// ... | ||
} | ||
// usage | ||
createUser('Jack', '123456', undefined, 'NY US'); | ||
``` | ||
Object-style: | ||
```typescript | ||
interface CreateUserOptions { | ||
name: string, | ||
password: string, | ||
type?: string | ||
address?: string | ||
} | ||
function createUser({ name, password, type, address }: CreateUserOptions) { | ||
// ... | ||
} | ||
// usage | ||
createUser({ | ||
name: 'Jack', | ||
password: '123456', | ||
address: 'NY US' | ||
}); | ||
``` | ||
### Enum with custom names and descriptions | ||
You can use `x-enum-varnames` and `x-enum-descriptions` in your spec to generate enum with custom names and descriptions. | ||
It's not in official [spec](https://github.com/OAI/OpenAPI-Specification/issues/681) yet. But its a supported extension | ||
that can help developers use more meaningful enumerators. | ||
```json | ||
{ | ||
"EnumWithStrings": { | ||
"description": "This is a simple enum with strings", | ||
"enum": [ | ||
0, | ||
1, | ||
2 | ||
], | ||
"x-enum-varnames": [ | ||
"Success", | ||
"Warning" | ||
"Error" | ||
], | ||
"x-enum-descriptions": [ | ||
"Used when the status of something is successful", | ||
"Used when the status of something has a warning" | ||
"Used when the status of something has an error" | ||
] | ||
} | ||
} | ||
``` | ||
Generated code: | ||
```typescript | ||
enum EnumWithStrings { | ||
/* | ||
* Used when the status of something is successful | ||
*/ | ||
Success = 0, | ||
/* | ||
* Used when the status of something has a warning | ||
*/ | ||
Waring = 1, | ||
/* | ||
* Used when the status of something has an error | ||
*/ | ||
Error = 2, | ||
} | ||
``` | ||
### Authorization | ||
The OpenAPI generator supports Bearer Token authorization. In order to enable the sending | ||
of tokens in each request you can set the token using the global OpenAPI configuration: | ||
```typescript | ||
import { OpenAPI } from './' | ||
OpenAPI.TOKEN = 'some-bearer-token' | ||
``` |
Sorry, the diff of this file is not supported yet
170909
2.1%203
0.5%3666
0.3%153
142.86%