Skip to content

Commit 2e1aac4

Browse files
Запасной Александр СергеевичЗапасной Александр Сергеевич
authored andcommitted
Added custom httpClient generation
1 parent 1ee13ff commit 2e1aac4

File tree

13 files changed

+691
-31
lines changed

13 files changed

+691
-31
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- Supports JSON and YAML files for input
2121
- Supports generation through CLI, Node.js and NPX
2222
- Supports tsc and @babel/plugin-transform-typescript
23+
- Supports custom httpClients with methods GET, POST, PUT, DELETE
2324

2425

2526
## Install
@@ -37,21 +38,23 @@ $ openapi --help
3738
Usage: openapi [options]
3839
3940
Options:
40-
-V, --version output the version number
41-
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
42-
-o, --output <value> Output directory (required)
43-
-c, --client <value> HTTP client to generate [fetch, xhr, node] (default: "fetch")
44-
--useOptions Use options instead of arguments
45-
--useUnionTypes Use union types instead of enums
46-
--exportCore <value> Write core files to disk (default: true)
47-
--exportServices <value> Write services to disk (default: true)
48-
--exportModels <value> Write models to disk (default: true)
49-
--exportSchemas <value> Write schemas to disk (default: false)
41+
-V, --version output the version number
42+
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
43+
-o, --output <value> Output directory (required)
44+
-c, --client <value> HTTP client to generate [fetch, xhr, node, httpClient] (default: "fetch")
45+
--httpClientLibrary <value> Library for httpClient with methods GET, POST, PUT, DELETE
46+
--useOptions Use options instead of arguments
47+
--useUnionTypes Use union types instead of enums
48+
--exportCore <value> Write core files to disk (default: true)
49+
--exportServices <value> Write services to disk (default: true)
50+
--exportModels <value> Write models to disk (default: true)
51+
--exportSchemas <value> Write schemas to disk (default: false)
5052
5153
Examples
5254
$ openapi --input ./spec.json
5355
$ openapi --input ./spec.json --output ./dist
5456
$ openapi --input ./spec.json --output ./dist --client xhr
57+
$ openapi --input ./spec.json --output ./dist --client httpClient --httpClientLibrary @myapp/XHR/HttpClient
5558
```
5659

5760

bin/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ program
1212
.version(pkg.version)
1313
.requiredOption('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
1414
.requiredOption('-o, --output <value>', 'Output directory (required)')
15-
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node]', 'fetch')
15+
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, httpClient]', 'fetch')
16+
.option('--httpClientLibrary <value>', 'Library for httpClient with methods GET, POST, PUT, DELETE', '')
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)
@@ -28,6 +29,7 @@ if (OpenAPI) {
2829
input: program.input,
2930
output: program.output,
3031
httpClient: program.client,
32+
httpClientLibrary: program.httpClientLibrary,
3133
useOptions: program.useOptions,
3234
useUnionTypes: program.useUnionTypes,
3335
exportCore: JSON.parse(program.exportCore) === true,

package.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"name": "openapi-typescript-codegen",
3-
"version": "0.7.0",
4-
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
5-
"author": "Ferdi Koomen",
6-
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
2+
"name": "openapi-typescript-codegen-with-custom-client",
3+
"version": "0.0.1",
4+
"description": "Fork of library that generates Typescript clients based on the OpenAPI specification.",
5+
"author": "Sundved",
6+
"homepage": "https://github.com/Sundved/openapi-typescript-codegen",
77
"repository": {
88
"type": "git",
9-
"url": "git+https://github.com/ferdikoomen/openapi-typescript-codegen.git"
9+
"url": "git+https://github.com/Sundved/openapi-typescript-codegen.git"
1010
},
1111
"bugs": {
12-
"url": "https://github.com/ferdikoomen/openapi-typescript-codegen/issues"
12+
"url": "https://github.com/Sundved/openapi-typescript-codegen/issues"
1313
},
1414
"license": "MIT",
1515
"keywords": [
@@ -27,8 +27,8 @@
2727
],
2828
"maintainers": [
2929
{
30-
"name": "Ferdi Koomen",
31-
"email": "info@madebyferdi.com"
30+
"name": "Sundved",
31+
"email": "sundved@gmail.com"
3232
}
3333
],
3434
"main": "dist/index.js",
@@ -57,8 +57,7 @@
5757
"eslint:fix": "eslint \"./src/**/*.ts\" \"./bin/index.js\" \"./types/index.d.ts\" --fix",
5858
"prettier": "prettier \"./src/**/*.ts\" \"./bin/index.js\" \"./types/index.d.ts\" --check",
5959
"prettier:fix": "prettier \"./src/**/*.ts\" \"./bin/index.js\" \"./types/index.d.ts\" --write",
60-
"prepublish": "yarn run clean && yarn run release",
61-
"codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b"
60+
"prepublish": "yarn run clean && yarn run release"
6261
},
6362
"dependencies": {
6463
"camelcase": "^6.2.0",
@@ -82,7 +81,6 @@
8281
"@types/node-fetch": "2.5.7",
8382
"@typescript-eslint/eslint-plugin": "4.8.1",
8483
"@typescript-eslint/parser": "4.8.2",
85-
"codecov": "3.8.1",
8684
"eslint": "7.14.0",
8785
"eslint-config-prettier": "6.15.0",
8886
"eslint-plugin-prettier": "3.1.4",

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export enum HttpClient {
1111
FETCH = 'fetch',
1212
XHR = 'xhr',
1313
NODE = 'node',
14+
HTTP_CLIENT = 'httpClient',
1415
}
1516

1617
export type Options = {
@@ -23,6 +24,7 @@ export type Options = {
2324
exportServices?: boolean;
2425
exportModels?: boolean;
2526
exportSchemas?: boolean;
27+
httpClientLibrary?: string;
2628
write?: boolean;
2729
};
2830

@@ -39,6 +41,7 @@ export type Options = {
3941
* @param exportServices: Generate services
4042
* @param exportModels: Generate models
4143
* @param exportSchemas: Generate schemas
44+
* @param httpClientLibrary Library for httpClient
4245
* @param write Write the files to disk (true or false)
4346
*/
4447
export async function generate({
@@ -51,6 +54,7 @@ export async function generate({
5154
exportServices = true,
5255
exportModels = true,
5356
exportSchemas = false,
57+
httpClientLibrary = '',
5458
write = true,
5559
}: Options): Promise<void> {
5660
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
@@ -62,15 +66,15 @@ export async function generate({
6266
const client = parseV2(openApi);
6367
const clientFinal = postProcessClient(client);
6468
if (!write) break;
65-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
69+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, httpClientLibrary);
6670
break;
6771
}
6872

6973
case OpenApiVersion.V3: {
7074
const client = parseV3(openApi);
7175
const clientFinal = postProcessClient(client);
7276
if (!write) break;
73-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
77+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, httpClientLibrary);
7478
break;
7579
}
7680
}

src/templates/core/functions/getUrl.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function getUrl(options: ApiRequestOptions): string {
33
const url = `${OpenAPI.BASE}${path}`;
44

55
if (options.query) {
6-
return `${url}${getQueryString(options.query)}`;
6+
return `${url}${getQueryString({...options.query, ts: new Date().getTime()})}`;
77
}
88
return url;
99
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{>header}}
2+
import type { ApiRequestOptions } from './ApiRequestOptions';
3+
import { OpenAPI } from './OpenAPI';
4+
5+
{{>functions/isDefined}}
6+
7+
8+
{{>functions/getQueryString}}
9+
10+
11+
{{>functions/getUrl}}
12+
13+
14+
{{>httpClient/sendRequest}}
15+
16+
17+
/**
18+
* Request using XHR client
19+
* @param options The request options from the the service
20+
* @result ApiResult
21+
*/
22+
export function request(options: ApiRequestOptions): Promise<any> {
23+
const url = getUrl(options);
24+
25+
return sendRequest(options, url);
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {DELETE, GET, POST, PUT} from '{{>httpClientLibrary}}';
2+
3+
async function sendRequest(options: ApiRequestOptions, url: string): Promise<any> {
4+
5+
switch (options.method) {
6+
case 'GET':
7+
return GET(url);
8+
case 'POST':
9+
return POST(url, getRequestBody(options));
10+
case 'PUT':
11+
return PUT(url, getRequestBody(options));
12+
case 'DELETE':
13+
return DELETE(url, getRequestBody(options));
14+
}
15+
}

src/templates/core/request.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{{#equals @root.httpClient 'fetch'}}{{>fetch/request}}{{/equals}}
22
{{#equals @root.httpClient 'xhr'}}{{>xhr/request}}{{/equals}}
33
{{#equals @root.httpClient 'node'}}{{>node/request}}{{/equals}}
4+
{{#equals @root.httpClient 'httpClient'}}{{>httpClient/request}}{{/equals}}

src/utils/registerHandlebarTemplates.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import functionIsString from '../templates/core/functions/isString.hbs';
2020
import functionIsStringWithValue from '../templates/core/functions/isStringWithValue.hbs';
2121
import functionIsSuccess from '../templates/core/functions/isSuccess.hbs';
2222
import functionResolve from '../templates/core/functions/resolve.hbs';
23+
import httpClientRequest from '../templates/core/httpClient/request.hbs';
24+
import httpClientSendRequest from '../templates/core/httpClient/sendRequest.hbs';
2325
import nodeGetHeaders from '../templates/core/node/getHeaders.hbs';
2426
import nodeGetRequestBody from '../templates/core/node/getRequestBody.hbs';
2527
import nodeGetResponseBody from '../templates/core/node/getResponseBody.hbs';
@@ -165,6 +167,10 @@ export function registerHandlebarTemplates(): Templates {
165167
Handlebars.registerPartial('xhr/sendRequest', Handlebars.template(xhrSendRequest));
166168
Handlebars.registerPartial('xhr/request', Handlebars.template(xhrRequest));
167169

170+
// Specific files for the httpClient client implementation
171+
Handlebars.registerPartial('httpClient/sendRequest', Handlebars.template(httpClientSendRequest));
172+
Handlebars.registerPartial('httpClient/request', Handlebars.template(httpClientRequest));
173+
168174
// Specific files for the node client implementation
169175
Handlebars.registerPartial('node/getHeaders', Handlebars.template(nodeGetHeaders));
170176
Handlebars.registerPartial('node/getRequestBody', Handlebars.template(nodeGetRequestBody));

src/utils/writeClient.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { writeClientServices } from './writeClientServices';
2323
* @param exportServices: Generate services
2424
* @param exportModels: Generate models
2525
* @param exportSchemas: Generate schemas
26+
* @param httpClientLibrary: Library for httpClient
2627
*/
2728
export async function writeClient(
2829
client: Client,
@@ -34,7 +35,8 @@ export async function writeClient(
3435
exportCore: boolean,
3536
exportServices: boolean,
3637
exportModels: boolean,
37-
exportSchemas: boolean
38+
exportSchemas: boolean,
39+
httpClientLibrary: string
3840
): Promise<void> {
3941
const outputPath = path.resolve(process.cwd(), output);
4042
const outputPathCore = path.resolve(outputPath, 'core');
@@ -55,7 +57,7 @@ export async function writeClient(
5557
if (exportServices) {
5658
await rmdir(outputPathServices);
5759
await mkdir(outputPathServices);
58-
await writeClientServices(client.services, templates, outputPathServices, httpClient, useUnionTypes, useOptions);
60+
await writeClientServices(client.services, templates, outputPathServices, httpClient, useUnionTypes, useOptions, httpClientLibrary);
5961
}
6062

6163
if (exportSchemas) {

0 commit comments

Comments
 (0)