Skip to content

Commit b0bea2d

Browse files
committed
- Added request property
1 parent 56f9289 commit b0bea2d

File tree

12 files changed

+53
-104
lines changed

12 files changed

+53
-104
lines changed

bin/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ 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] or path to custom request file', 'fetch')
15+
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node]', 'fetch')
1616
.option('--useOptions', 'Use options instead of arguments')
1717
.option('--useUnionTypes', 'Use union types instead of enums')
1818
.option('--exportCore <value>', 'Write core files to disk', true)
1919
.option('--exportServices <value>', 'Write services to disk', true)
2020
.option('--exportModels <value>', 'Write models to disk', true)
2121
.option('--exportSchemas <value>', 'Write schemas to disk', false)
22+
.option('--request <value>', 'Path to custom request file')
2223
.parse(process.argv);
2324

2425
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
@@ -34,6 +35,7 @@ if (OpenAPI) {
3435
exportServices: JSON.parse(program.exportServices) === true,
3536
exportModels: JSON.parse(program.exportModels) === true,
3637
exportSchemas: JSON.parse(program.exportSchemas) === true,
38+
request: program.request,
3739
})
3840
.then(() => {
3941
process.exit(0);

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
@@ -91,7 +91,6 @@
9191
"express": "4.17.1",
9292
"form-data": "3.0.0",
9393
"glob": "7.1.6",
94-
"httpntlm": "1.7.6",
9594
"jest": "26.6.3",
9695
"jest-cli": "26.6.3",
9796
"node-fetch": "2.6.1",

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ export { HttpClient } from './HttpClient';
1313
export type Options = {
1414
input: string | Record<string, any>;
1515
output: string;
16-
httpClient?: string | HttpClient;
16+
httpClient?: HttpClient;
1717
useOptions?: boolean;
1818
useUnionTypes?: boolean;
1919
exportCore?: boolean;
2020
exportServices?: boolean;
2121
exportModels?: boolean;
2222
exportSchemas?: boolean;
23+
request?: string;
2324
write?: boolean;
2425
};
2526

@@ -36,6 +37,7 @@ export type Options = {
3637
* @param exportServices: Generate services
3738
* @param exportModels: Generate models
3839
* @param exportSchemas: Generate schemas
40+
* @param request: Path to custom request file
3941
* @param write Write the files to disk (true or false)
4042
*/
4143
export async function generate({
@@ -48,6 +50,7 @@ export async function generate({
4850
exportServices = true,
4951
exportModels = true,
5052
exportSchemas = false,
53+
request,
5154
write = true,
5255
}: Options): Promise<void> {
5356
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
@@ -59,15 +62,15 @@ export async function generate({
5962
const client = parseV2(openApi);
6063
const clientFinal = postProcessClient(client);
6164
if (!write) break;
62-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
65+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
6366
break;
6467
}
6568

6669
case OpenApiVersion.V3: {
6770
const client = parseV3(openApi);
6871
const clientFinal = postProcessClient(client);
6972
if (!write) break;
70-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
73+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
7174
break;
7275
}
7376
}

src/utils/writeClient.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ import { writeClientServices } from './writeClientServices';
2323
* @param exportServices: Generate services
2424
* @param exportModels: Generate models
2525
* @param exportSchemas: Generate schemas
26+
* @param request: Path to custom request file
2627
*/
2728
export async function writeClient(
2829
client: Client,
2930
templates: Templates,
3031
output: string,
31-
httpClient: string | HttpClient,
32+
httpClient: HttpClient,
3233
useOptions: boolean,
3334
useUnionTypes: boolean,
3435
exportCore: boolean,
3536
exportServices: boolean,
3637
exportModels: boolean,
37-
exportSchemas: boolean
38+
exportSchemas: boolean,
39+
request?: string
3840
): Promise<void> {
3941
const outputPath = resolve(process.cwd(), output);
4042
const outputPathCore = resolve(outputPath, 'core');
@@ -49,7 +51,7 @@ export async function writeClient(
4951
if (exportCore) {
5052
await rmdir(outputPathCore);
5153
await mkdir(outputPathCore);
52-
await writeClientCore(client, templates, outputPathCore, httpClient);
54+
await writeClientCore(client, templates, outputPathCore, httpClient, request);
5355
}
5456

5557
if (exportServices) {

src/utils/writeClientCore.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { Templates } from './registerHandlebarTemplates';
1111
* @param templates The loaded handlebar templates
1212
* @param outputPath Directory to write the generated files to
1313
* @param httpClient The selected httpClient (fetch, xhr or node)
14+
* @param request: Path to custom request file
1415
*/
15-
export async function writeClientCore(client: Client, templates: Templates, outputPath: string, httpClient: string | HttpClient): Promise<void> {
16+
export async function writeClientCore(client: Client, templates: Templates, outputPath: string, httpClient: HttpClient, request?: string): Promise<void> {
1617
const context = {
1718
httpClient,
1819
server: client.server,
@@ -23,20 +24,14 @@ export async function writeClientCore(client: Client, templates: Templates, outp
2324
await writeFile(resolve(outputPath, 'ApiError.ts'), templates.core.apiError({}));
2425
await writeFile(resolve(outputPath, 'ApiRequestOptions.ts'), templates.core.apiRequestOptions({}));
2526
await writeFile(resolve(outputPath, 'ApiResult.ts'), templates.core.apiResult({}));
27+
await writeFile(resolve(outputPath, 'request.ts'), templates.core.request(context));
2628

27-
switch (httpClient) {
28-
case HttpClient.FETCH:
29-
case HttpClient.XHR:
30-
case HttpClient.NODE:
31-
await writeFile(resolve(outputPath, 'request.ts'), templates.core.request(context));
32-
break;
33-
default:
34-
const customRequestFile = resolve(process.cwd(), httpClient);
35-
const customRequestFileExists = await exists(customRequestFile);
36-
if (!customRequestFileExists) {
37-
throw new Error(`Custom request file "${customRequestFile}" does not exists`);
38-
}
39-
await copyFile(customRequestFile, resolve(outputPath, 'request.ts'));
40-
break;
29+
if (request) {
30+
const requestFile = resolve(process.cwd(), request);
31+
const requestFileExists = await exists(requestFile);
32+
if (!requestFileExists) {
33+
throw new Error(`Custom request file "${requestFile}" does not exists`);
34+
}
35+
await copyFile(requestFile, resolve(outputPath, 'request.ts'));
4136
}
4237
}

src/utils/writeClientModels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Templates } from './registerHandlebarTemplates';
1414
* @param httpClient The selected httpClient (fetch, xhr or node)
1515
* @param useUnionTypes Use union types instead of enums
1616
*/
17-
export async function writeClientModels(models: Model[], templates: Templates, outputPath: string, httpClient: string | HttpClient, useUnionTypes: boolean): Promise<void> {
17+
export async function writeClientModels(models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean): Promise<void> {
1818
for (const model of models) {
1919
const file = resolve(outputPath, `${model.name}.ts`);
2020
const templateResult = templates.exports.model({

src/utils/writeClientSchemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Templates } from './registerHandlebarTemplates';
1414
* @param httpClient The selected httpClient (fetch, xhr or node)
1515
* @param useUnionTypes Use union types instead of enums
1616
*/
17-
export async function writeClientSchemas(models: Model[], templates: Templates, outputPath: string, httpClient: string | HttpClient, useUnionTypes: boolean): Promise<void> {
17+
export async function writeClientSchemas(models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean): Promise<void> {
1818
for (const model of models) {
1919
const file = resolve(outputPath, `$${model.name}.ts`);
2020
const templateResult = templates.exports.schema({

src/utils/writeClientServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION';
1717
* @param useUnionTypes Use union types instead of enums
1818
* @param useOptions Use options or arguments functions
1919
*/
20-
export async function writeClientServices(services: Service[], templates: Templates, outputPath: string, httpClient: string | HttpClient, useUnionTypes: boolean, useOptions: boolean): Promise<void> {
20+
export async function writeClientServices(services: Service[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean): Promise<void> {
2121
for (const service of services) {
2222
const file = resolve(outputPath, `${service.name}.ts`);
2323
const useVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING));

test/custom/request.ts

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,23 @@
1-
// @ts-ignore
2-
import httpntlm from 'httpntlm';
3-
import { promisify } from 'util';
4-
import { stringify } from 'qs';
1+
/* istanbul ignore file */
2+
/* tslint:disable */
3+
/* eslint-disable */
4+
import type { ApiRequestOptions } from './ApiRequestOptions';
5+
import type { ApiResult } from './ApiResult';
6+
import { OpenAPI } from './OpenAPI';
57

6-
type TRequestOptions = {
7-
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
8-
readonly path: string;
9-
readonly cookies?: Record<string, any>;
10-
readonly headers?: Record<string, any>;
11-
readonly query?: Record<string, any>;
12-
readonly formData?: Record<string, any>;
13-
readonly body?: any;
14-
readonly responseHeader?: string;
15-
readonly errors?: Record<number, string>;
16-
}
17-
18-
type TResult = {
19-
readonly url: string;
20-
readonly ok: boolean;
21-
readonly status: number;
22-
readonly statusText: string;
23-
readonly body: any;
24-
}
25-
26-
export async function request(options: TRequestOptions): Promise<TResult> {
8+
export async function request(options: ApiRequestOptions): Promise<ApiResult> {
279

28-
const path = options.path.replace(/[:]/g, '_');
29-
const query = stringify(options.query);
30-
const host = 'http://localhost:8080';
31-
const url = `${host}${path}${query}`;
10+
const url = `${OpenAPI.BASE}${options.path}`;
3211

33-
const body = options.body && JSON.stringify(options.body);
34-
const headers = {
35-
...options.headers,
36-
'Accept': 'application/json',
37-
'Content-Type': 'application/json; charset=utf-8',
38-
'Accept-Encoding': 'identity',
39-
}
40-
41-
const method = options.method.toLowerCase();
42-
const fetch = promisify(httpntlm[method]);
43-
44-
const response = await fetch({
45-
url,
46-
___domain: '___domain',
47-
username: 'username',
48-
password: 'password',
49-
headers,
50-
body,
51-
});
12+
// Do your request...
5213

5314
return {
5415
url,
55-
ok: response.ok,
56-
status: response.statusCode,
57-
statusText: response.statusText,
58-
body: JSON.parse(response.body),
16+
ok: true,
17+
status: 200,
18+
statusText: 'dummy',
19+
body: {
20+
...options
21+
},
5922
};
6023
}

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ async function generateV2() {
1313
exportSchemas: true,
1414
exportModels: true,
1515
exportServices: true,
16+
request: './test/custom/request.ts',
1617
});
1718
}
1819

@@ -27,6 +28,7 @@ async function generateV3() {
2728
exportSchemas: true,
2829
exportModels: true,
2930
exportServices: true,
31+
request: './test/custom/request.ts',
3032
});
3133
}
3234

0 commit comments

Comments
 (0)