diff --git a/README.md b/README.md index 985dc7c5..03d076f7 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ $ openapi --help -i, --input OpenAPI specification, can be a path, url or string content (required) -o, --output Output directory (required) -c, --client HTTP client to generate [fetch, xhr, axios, node] (default: "fetch") + --name Custom client class name --useOptions Use options instead of arguments --useUnionTypes Use union types instead of enums --exportCore 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. diff --git a/bin/index.js b/bin/index.js index b1952439..77cf737b 100755 --- a/bin/index.js +++ b/bin/index.js @@ -13,6 +13,7 @@ const params = program .requiredOption('-i, --input ', 'OpenAPI specification, can be a path, url or string content (required)') .requiredOption('-o, --output ', 'Output directory (required)') .option('-c, --client ', 'HTTP client to generate [fetch, xhr, node, axios]', 'fetch') + .option('--name ', 'Custom client class name') .option('--useOptions', 'Use options instead of arguments') .option('--useUnionTypes', 'Use union types instead of enums') .option('--exportCore ', '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, diff --git a/src/index.ts b/src/index.ts index 2edaa308..2949881a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ export type Options = { input: string | Record; output: string; httpClient?: HttpClient; + clientName?: string; useOptions?: boolean; useUnionTypes?: boolean; exportCore?: boolean; diff --git a/test/index.js b/test/index.js index db0a8d4b..d945a191 100644 --- a/test/index.js +++ b/test/index.js @@ -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', diff --git a/types/index.d.ts b/types/index.d.ts index 9104791f..39329e37 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,6 +15,7 @@ export type Options = { input: string | Record; 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; export default { HttpClient, + Indent, generate, };