Skip to content

Commit d002818

Browse files
committed
upgrade openapi-typescript-codegen and re-generate code
introduces more controllable endpoints (by using CancellablePromise)
1 parent 93f86bc commit d002818

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1281
-847
lines changed

package-lock.json

Lines changed: 182 additions & 266 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"author": "",
1414
"license": "ISC",
1515
"devDependencies": {
16-
"@types/node-fetch": "^2.5.8",
16+
"@types/node-fetch": "^2.6.2",
1717
"form-data": "^4.0.0",
1818
"node-fetch": "^2.6.7",
19-
"openapi-typescript-codegen": "^0.8.1",
19+
"openapi-typescript-codegen": "^0.23.0",
2020
"ts-loader": "^9.3.1",
2121
"ts-node": "^9.1.1",
2222
"typescript": "^4.2.3",

src/api/core/ApiError.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
/* istanbul ignore file */
22
/* tslint:disable */
33
/* eslint-disable */
4+
import type { ApiRequestOptions } from './ApiRequestOptions';
45
import type { ApiResult } from './ApiResult';
56

67
export class ApiError extends Error {
78
public readonly url: string;
89
public readonly status: number;
910
public readonly statusText: string;
1011
public readonly body: any;
12+
public readonly request: ApiRequestOptions;
1113

12-
constructor(response: ApiResult, message: string) {
14+
constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
1315
super(message);
1416

17+
this.name = 'ApiError';
1518
this.url = response.url;
1619
this.status = response.status;
1720
this.statusText = response.statusText;
1821
this.body = response.body;
22+
this.request = request;
1923
}
20-
}
24+
}

src/api/core/ApiRequestOptions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
/* eslint-disable */
44
export type ApiRequestOptions = {
55
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
6-
readonly path: string;
6+
readonly url: string;
7+
readonly path?: Record<string, any>;
78
readonly cookies?: Record<string, any>;
89
readonly headers?: Record<string, any>;
910
readonly query?: Record<string, any>;
1011
readonly formData?: Record<string, any>;
1112
readonly body?: any;
13+
readonly mediaType?: string;
1214
readonly responseHeader?: string;
1315
readonly errors?: Record<number, string>;
14-
}
16+
};

src/api/core/ApiResult.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export type ApiResult = {
77
readonly status: number;
88
readonly statusText: string;
99
readonly body: any;
10-
}
10+
};

src/api/core/CancelablePromise.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* istanbul ignore file */
2+
/* tslint:disable */
3+
/* eslint-disable */
4+
export class CancelError extends Error {
5+
6+
constructor(message: string) {
7+
super(message);
8+
this.name = 'CancelError';
9+
}
10+
11+
public get isCancelled(): boolean {
12+
return true;
13+
}
14+
}
15+
16+
export interface OnCancel {
17+
readonly isResolved: boolean;
18+
readonly isRejected: boolean;
19+
readonly isCancelled: boolean;
20+
21+
(cancelHandler: () => void): void;
22+
}
23+
24+
export class CancelablePromise<T> implements Promise<T> {
25+
readonly [Symbol.toStringTag]!: string;
26+
27+
private _isResolved: boolean;
28+
private _isRejected: boolean;
29+
private _isCancelled: boolean;
30+
private readonly _cancelHandlers: (() => void)[];
31+
private readonly _promise: Promise<T>;
32+
private _resolve?: (value: T | PromiseLike<T>) => void;
33+
private _reject?: (reason?: any) => void;
34+
35+
constructor(
36+
executor: (
37+
resolve: (value: T | PromiseLike<T>) => void,
38+
reject: (reason?: any) => void,
39+
onCancel: OnCancel
40+
) => void
41+
) {
42+
this._isResolved = false;
43+
this._isRejected = false;
44+
this._isCancelled = false;
45+
this._cancelHandlers = [];
46+
this._promise = new Promise<T>((resolve, reject) => {
47+
this._resolve = resolve;
48+
this._reject = reject;
49+
50+
const onResolve = (value: T | PromiseLike<T>): void => {
51+
if (this._isResolved || this._isRejected || this._isCancelled) {
52+
return;
53+
}
54+
this._isResolved = true;
55+
this._resolve?.(value);
56+
};
57+
58+
const onReject = (reason?: any): void => {
59+
if (this._isResolved || this._isRejected || this._isCancelled) {
60+
return;
61+
}
62+
this._isRejected = true;
63+
this._reject?.(reason);
64+
};
65+
66+
const onCancel = (cancelHandler: () => void): void => {
67+
if (this._isResolved || this._isRejected || this._isCancelled) {
68+
return;
69+
}
70+
this._cancelHandlers.push(cancelHandler);
71+
};
72+
73+
Object.defineProperty(onCancel, 'isResolved', {
74+
get: (): boolean => this._isResolved,
75+
});
76+
77+
Object.defineProperty(onCancel, 'isRejected', {
78+
get: (): boolean => this._isRejected,
79+
});
80+
81+
Object.defineProperty(onCancel, 'isCancelled', {
82+
get: (): boolean => this._isCancelled,
83+
});
84+
85+
return executor(onResolve, onReject, onCancel as OnCancel);
86+
});
87+
}
88+
89+
public then<TResult1 = T, TResult2 = never>(
90+
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
91+
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
92+
): Promise<TResult1 | TResult2> {
93+
return this._promise.then(onFulfilled, onRejected);
94+
}
95+
96+
public catch<TResult = never>(
97+
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
98+
): Promise<T | TResult> {
99+
return this._promise.catch(onRejected);
100+
}
101+
102+
public finally(onFinally?: (() => void) | null): Promise<T> {
103+
return this._promise.finally(onFinally);
104+
}
105+
106+
public cancel(): void {
107+
if (this._isResolved || this._isRejected || this._isCancelled) {
108+
return;
109+
}
110+
this._isCancelled = true;
111+
if (this._cancelHandlers.length) {
112+
try {
113+
for (const cancelHandler of this._cancelHandlers) {
114+
cancelHandler();
115+
}
116+
} catch (error) {
117+
console.warn('Cancellation threw an error', error);
118+
return;
119+
}
120+
}
121+
this._cancelHandlers.length = 0;
122+
this._reject?.(new CancelError('Request aborted'));
123+
}
124+
125+
public get isCancelled(): boolean {
126+
return this._isCancelled;
127+
}
128+
}

src/api/core/OpenAPI.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
/* istanbul ignore file */
22
/* tslint:disable */
33
/* eslint-disable */
4-
type Resolver<T> = () => Promise<T>;
4+
import type { ApiRequestOptions } from './ApiRequestOptions';
5+
6+
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
57
type Headers = Record<string, string>;
68

7-
type Config = {
9+
export type OpenAPIConfig = {
810
BASE: string;
911
VERSION: string;
1012
WITH_CREDENTIALS: boolean;
13+
CREDENTIALS: 'include' | 'omit' | 'same-origin';
1114
TOKEN?: string | Resolver<string>;
1215
USERNAME?: string | Resolver<string>;
1316
PASSWORD?: string | Resolver<string>;
1417
HEADERS?: Headers | Resolver<Headers>;
15-
}
18+
ENCODE_PATH?: (path: string) => string;
19+
};
1620

17-
export const OpenAPI: Config = {
21+
export const OpenAPI: OpenAPIConfig = {
1822
BASE: '',
1923
VERSION: '2.0',
2024
WITH_CREDENTIALS: false,
25+
CREDENTIALS: 'include',
2126
TOKEN: undefined,
2227
USERNAME: undefined,
2328
PASSWORD: undefined,
2429
HEADERS: undefined,
25-
};
30+
ENCODE_PATH: undefined,
31+
};

0 commit comments

Comments
 (0)