Skip to content

Commit 8a2b61a

Browse files
author
svolkov
committed
chore: add wrong enum subtype schema; chore(internal): change debug scripts
1 parent acc770b commit 8a2b61a

File tree

4 files changed

+138
-5
lines changed

4 files changed

+138
-5
lines changed

.vscode/launch.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@
1414
"port": 9229
1515
},
1616
{
17-
"name": "Debug CLI",
17+
"name": "Debug JSON CLI",
1818
"type": "node",
1919
"request": "launch",
2020
"cwd": "${workspaceFolder}",
2121
"runtimeExecutable": "npm",
22-
"runtimeArgs": ["run-script", "cli:debug"],
22+
"runtimeArgs": ["run-script", "cli:debug:json"],
23+
"port": 9229
24+
},
25+
{
26+
"name": "Debug YAML CLI",
27+
"type": "node",
28+
"request": "launch",
29+
"cwd": "${workspaceFolder}",
30+
"runtimeExecutable": "npm",
31+
"runtimeArgs": ["run-script", "cli:debug:yaml"],
2332
"port": 9229
2433
}
2534
]
26-
}
35+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
"version": "1.8.2",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
6-
"cli": "node index.js -r -d -p http://localhost:8080/api/v1/swagger.json -n swagger-test-cli.ts",
7-
"cli:debug": "node --nolazy --inspect-brk=9229 index.js -p ./swagger-test-cli.json -n swagger-test-cli.ts",
6+
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
7+
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
8+
"cli:debug:json": "node --nolazy --inspect-brk=9229 index.js -p ./swagger-test-cli.json -n swagger-test-cli.ts",
9+
"cli:debug:yaml": "node --nolazy --inspect-brk=9229 index.js -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
810
"cli:help": "node index.js -h",
911
"test:all": "npm-run-all generate validate test:routeTypes test:noClient test:defaultAsSuccess test:responses --continue-on-error",
1012
"generate": "node tests/generate.js",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
4+
/*
5+
* ---------------------------------------------------------------
6+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
7+
* ## ##
8+
* ## AUTHOR: acacode ##
9+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
10+
* ---------------------------------------------------------------
11+
*/
12+
13+
export type Test = XAB & { y?: string };
14+
15+
export type RequestParams = Omit<RequestInit, "body" | "method"> & {
16+
secure?: boolean;
17+
};
18+
19+
type ApiConfig<SecurityDataType> = {
20+
baseUrl?: string;
21+
baseApiParams?: RequestParams;
22+
securityWorker?: (securityData: SecurityDataType) => RequestParams;
23+
};
24+
25+
const enum BodyType {
26+
Json,
27+
}
28+
29+
class HttpClient<SecurityDataType> {
30+
public baseUrl: string = "";
31+
private securityData: SecurityDataType = null as any;
32+
private securityWorker: ApiConfig<SecurityDataType>["securityWorker"] = (() => {}) as any;
33+
34+
private baseApiParams: RequestParams = {
35+
credentials: "same-origin",
36+
headers: {
37+
"Content-Type": "application/json",
38+
},
39+
redirect: "follow",
40+
referrerPolicy: "no-referrer",
41+
};
42+
43+
constructor({ baseUrl, baseApiParams, securityWorker }: ApiConfig<SecurityDataType> = {}) {
44+
this.baseUrl = baseUrl || this.baseUrl;
45+
this.baseApiParams = baseApiParams || this.baseApiParams;
46+
this.securityWorker = securityWorker || this.securityWorker;
47+
}
48+
49+
public setSecurityData = (data: SecurityDataType) => {
50+
this.securityData = data;
51+
};
52+
53+
private bodyFormatters: Record<BodyType, (input: any) => any> = {
54+
[BodyType.Json]: JSON.stringify,
55+
};
56+
57+
private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams {
58+
return {
59+
...this.baseApiParams,
60+
...params,
61+
...(securityParams || {}),
62+
headers: {
63+
...(this.baseApiParams.headers || {}),
64+
...(params.headers || {}),
65+
...((securityParams && securityParams.headers) || {}),
66+
},
67+
};
68+
}
69+
70+
private safeParseResponse = <T = any, E = any>(response: Response): Promise<T> =>
71+
response
72+
.json()
73+
.then((data) => data)
74+
.catch((e) => response.text);
75+
76+
public request = <T = any, E = any>(
77+
path: string,
78+
method: string,
79+
{ secure, ...params }: RequestParams = {},
80+
body?: any,
81+
bodyType?: BodyType,
82+
secureByDefault?: boolean,
83+
): Promise<T> =>
84+
fetch(`${this.baseUrl}${path}`, {
85+
// @ts-ignore
86+
...this.mergeRequestOptions(params, (secureByDefault || secure) && this.securityWorker(this.securityData)),
87+
method,
88+
body: body ? this.bodyFormatters[bodyType || BodyType.Json](body) : null,
89+
}).then(async (response) => {
90+
const data = await this.safeParseResponse<T, E>(response);
91+
if (!response.ok) throw data;
92+
return data;
93+
});
94+
}
95+
96+
/**
97+
* @title Test
98+
* @version test
99+
*/
100+
export class Api<SecurityDataType = any> extends HttpClient<SecurityDataType> {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test
4+
version: test
5+
paths: {}
6+
components:
7+
schemas:
8+
Test:
9+
type: object
10+
allOf:
11+
- type: object
12+
properties:
13+
x:
14+
type: array
15+
items:
16+
type: string
17+
enum:
18+
- A-B
19+
- type: object
20+
properties:
21+
y:
22+
type: string

0 commit comments

Comments
 (0)