mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Working on updating client export PR
This commit is contained in:
parent
15f7955ce4
commit
d7479aef16
28
README.md
28
README.md
@ -41,6 +41,7 @@ $ openapi --help
|
||||
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
|
||||
-o, --output <value> Output directory (required)
|
||||
-c, --client <value> HTTP client to generate [fetch, xhr, axios, node] (default: "fetch")
|
||||
--name <value> Custom client class name
|
||||
--useOptions Use options instead of arguments
|
||||
--useUnionTypes Use union types instead of enums
|
||||
--exportCore <value> Write core files to disk (default: true)
|
||||
@ -96,6 +97,33 @@ OpenAPI.generate({
|
||||
|
||||
## Features
|
||||
|
||||
|
||||
### Generate client instance with `--name` option
|
||||
The OpenAPI generator allows creation of client instances to support the multiple backend services use case.
|
||||
The generated client uses an instance of the server configuration and not the global `OpenAPI` constant.
|
||||
To generate a client instance, set a custom name to the client class, use `--name` option.
|
||||
|
||||
```
|
||||
openapi --input ./spec.json --output ./dist ---name AppClient
|
||||
```
|
||||
|
||||
The generated client will be exported from the `index` file and can be used as shown below:
|
||||
|
||||
```typescript
|
||||
// Create the client instance with server and authentication details
|
||||
const appClient = new AppClient({
|
||||
BASE: 'http://server-host.com',
|
||||
TOKEN: '1234'
|
||||
});
|
||||
|
||||
// Use the client instance to make the API call
|
||||
const res = await appClient.organizations.createOrganization({
|
||||
name: 'OrgName',
|
||||
description: 'OrgDescription',
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
### Argument style vs. Object style `--useOptions`
|
||||
There's no [named parameter](https://en.wikipedia.org/wiki/Named_parameter) in JavaScript or TypeScript, because of
|
||||
that, we offer the flag `--useOptions` to generate code in two different styles.
|
||||
|
||||
@ -13,6 +13,7 @@ const params = program
|
||||
.requiredOption('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
|
||||
.requiredOption('-o, --output <value>', 'Output directory (required)')
|
||||
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, axios]', 'fetch')
|
||||
.option('--name <value>', 'Custom client class name')
|
||||
.option('--useOptions', 'Use options instead of arguments')
|
||||
.option('--useUnionTypes', 'Use union types instead of enums')
|
||||
.option('--exportCore <value>', 'Write core files to disk', true)
|
||||
@ -32,6 +33,7 @@ if (OpenAPI) {
|
||||
input: params.input,
|
||||
output: params.output,
|
||||
httpClient: params.client,
|
||||
clientName: params.name,
|
||||
useOptions: params.useOptions,
|
||||
useUnionTypes: params.useUnionTypes,
|
||||
exportCore: JSON.parse(params.exportCore) === true,
|
||||
|
||||
@ -16,6 +16,7 @@ export type Options = {
|
||||
input: string | Record<string, any>;
|
||||
output: string;
|
||||
httpClient?: HttpClient;
|
||||
clientName?: string;
|
||||
useOptions?: boolean;
|
||||
useUnionTypes?: boolean;
|
||||
exportCore?: boolean;
|
||||
|
||||
@ -14,6 +14,7 @@ const generate = async (input, output) => {
|
||||
exportSchemas: true,
|
||||
exportModels: true,
|
||||
exportServices: true,
|
||||
clientName: 'AppClient',
|
||||
// indent: OpenAPI.Indent.SPACE_2,
|
||||
// postfix: 'Api',
|
||||
// request: './test/custom/request.ts',
|
||||
|
||||
2
types/index.d.ts
vendored
2
types/index.d.ts
vendored
@ -15,6 +15,7 @@ export type Options = {
|
||||
input: string | Record<string, any>;
|
||||
output: string;
|
||||
httpClient?: HttpClient | 'fetch' | 'xhr' | 'node' | 'axios';
|
||||
clientName?: string;
|
||||
useOptions?: boolean;
|
||||
useUnionTypes?: boolean;
|
||||
exportCore?: boolean;
|
||||
@ -31,5 +32,6 @@ export declare function generate(options: Options): Promise<void>;
|
||||
|
||||
export default {
|
||||
HttpClient,
|
||||
Indent,
|
||||
generate,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user