Skip to content

Commit d7479ae

Browse files
committed
- Working on updating client export PR
1 parent 15f7955 commit d7479ae

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ $ openapi --help
4141
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
4242
-o, --output <value> Output directory (required)
4343
-c, --client <value> HTTP client to generate [fetch, xhr, axios, node] (default: "fetch")
44+
--name <value> Custom client class name
4445
--useOptions Use options instead of arguments
4546
--useUnionTypes Use union types instead of enums
4647
--exportCore <value> Write core files to disk (default: true)
@@ -96,6 +97,33 @@ OpenAPI.generate({
9697

9798
## Features
9899

100+
101+
### Generate client instance with `--name` option
102+
The OpenAPI generator allows creation of client instances to support the multiple backend services use case.
103+
The generated client uses an instance of the server configuration and not the global `OpenAPI` constant.
104+
To generate a client instance, set a custom name to the client class, use `--name` option.
105+
106+
```
107+
openapi --input ./spec.json --output ./dist ---name AppClient
108+
```
109+
110+
The generated client will be exported from the `index` file and can be used as shown below:
111+
112+
```typescript
113+
// Create the client instance with server and authentication details
114+
const appClient = new AppClient({
115+
BASE: 'http://server-host.com',
116+
TOKEN: '1234'
117+
});
118+
119+
// Use the client instance to make the API call
120+
const res = await appClient.organizations.createOrganization({
121+
name: 'OrgName',
122+
description: 'OrgDescription',
123+
});
124+
```
125+
126+
99127
### Argument style vs. Object style `--useOptions`
100128
There's no [named parameter](https://en.wikipedia.org/wiki/Named_parameter) in JavaScript or TypeScript, because of
101129
that, we offer the flag `--useOptions` to generate code in two different styles.

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const params = program
1313
.requiredOption('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
1414
.requiredOption('-o, --output <value>', 'Output directory (required)')
1515
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, axios]', 'fetch')
16+
.option('--name <value>', 'Custom client class name')
1617
.option('--useOptions', 'Use options instead of arguments')
1718
.option('--useUnionTypes', 'Use union types instead of enums')
1819
.option('--exportCore <value>', 'Write core files to disk', true)
@@ -32,6 +33,7 @@ if (OpenAPI) {
3233
input: params.input,
3334
output: params.output,
3435
httpClient: params.client,
36+
clientName: params.name,
3537
useOptions: params.useOptions,
3638
useUnionTypes: params.useUnionTypes,
3739
exportCore: JSON.parse(params.exportCore) === true,

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type Options = {
1616
input: string | Record<string, any>;
1717
output: string;
1818
httpClient?: HttpClient;
19+
clientName?: string;
1920
useOptions?: boolean;
2021
useUnionTypes?: boolean;
2122
exportCore?: boolean;

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const generate = async (input, output) => {
1414
exportSchemas: true,
1515
exportModels: true,
1616
exportServices: true,
17+
clientName: 'AppClient',
1718
// indent: OpenAPI.Indent.SPACE_2,
1819
// postfix: 'Api',
1920
// request: './test/custom/request.ts',

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type Options = {
1515
input: string | Record<string, any>;
1616
output: string;
1717
httpClient?: HttpClient | 'fetch' | 'xhr' | 'node' | 'axios';
18+
clientName?: string;
1819
useOptions?: boolean;
1920
useUnionTypes?: boolean;
2021
exportCore?: boolean;
@@ -31,5 +32,6 @@ export declare function generate(options: Options): Promise<void>;
3132

3233
export default {
3334
HttpClient,
35+
Indent,
3436
generate,
3537
};

0 commit comments

Comments
 (0)