Skip to content

Commit 3dde57a

Browse files
committed
- Updated dependencies
- Aligned request cancelation between clients
1 parent 00cb70b commit 3dde57a

22 files changed

+429
-299
lines changed

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"dependencies": {
6161
"@types/node-fetch": "^2.5.12",
6262
"abort-controller": "^3.0.0",
63-
"axios": "^0.24.0",
63+
"axios": "^0.25.0",
6464
"camelcase": "^6.3.0",
6565
"commander": "^8.3.0",
6666
"form-data": "^4.0.0",
@@ -69,22 +69,22 @@
6969
"node-fetch": "^2.6.6"
7070
},
7171
"devDependencies": {
72-
"@babel/cli": "7.16.7",
73-
"@babel/core": "7.16.7",
74-
"@babel/preset-env": "7.16.7",
72+
"@babel/cli": "7.16.8",
73+
"@babel/core": "7.16.12",
74+
"@babel/preset-env": "7.16.11",
7575
"@babel/preset-typescript": "7.16.7",
7676
"@rollup/plugin-commonjs": "21.0.1",
7777
"@rollup/plugin-node-resolve": "13.1.3",
7878
"@rollup/plugin-typescript": "8.3.0",
7979
"@types/express": "4.17.13",
8080
"@types/glob": "7.2.0",
8181
"@types/jest": "27.4.0",
82-
"@types/node": "17.0.8",
82+
"@types/node": "17.0.10",
8383
"@types/qs": "6.9.7",
84-
"@typescript-eslint/eslint-plugin": "5.9.0",
85-
"@typescript-eslint/parser": "5.9.0",
84+
"@typescript-eslint/eslint-plugin": "5.10.0",
85+
"@typescript-eslint/parser": "5.10.0",
8686
"codecov": "3.8.3",
87-
"eslint": "8.6.0",
87+
"eslint": "8.7.0",
8888
"eslint-config-prettier": "8.3.0",
8989
"eslint-plugin-prettier": "4.0.0",
9090
"eslint-plugin-simple-import-sort": "7.0.0",
@@ -93,14 +93,14 @@
9393
"jest": "27.4.7",
9494
"jest-cli": "27.4.7",
9595
"prettier": "2.5.1",
96-
"puppeteer": "13.0.1",
97-
"qs": "6.10.2",
96+
"puppeteer": "13.1.1",
97+
"qs": "6.10.3",
9898
"rimraf": "^3.0.2",
99-
"rollup": "2.63.0",
99+
"rollup": "2.66.0",
100100
"rollup-plugin-node-externals": "3.1.2",
101101
"rollup-plugin-terser": "7.0.2",
102102
"ts-node": "10.4.0",
103103
"tslib": "2.3.1",
104-
"typescript": "4.5.4"
104+
"typescript": "4.5.5"
105105
}
106106
}

src/templates/core/ApiRequestOptions.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export type ApiRequestOptions = {
1111
readonly mediaType?: string;
1212
readonly responseHeader?: string;
1313
readonly errors?: Record<number, string>;
14-
}
14+
};

src/templates/core/ApiResult.hbs

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

src/templates/core/CancelablePromise.hbs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
export class CancelError extends Error {
44

5-
constructor(reason: string = 'Promise was canceled') {
6-
super(reason);
5+
constructor(message: string) {
6+
super(message);
77
this.name = 'CancelError';
88
}
99

@@ -13,7 +13,8 @@ export class CancelError extends Error {
1313
}
1414

1515
export interface OnCancel {
16-
readonly isPending: boolean;
16+
readonly isResolved: boolean;
17+
readonly isRejected: boolean;
1718
readonly isCancelled: boolean;
1819

1920
(cancelHandler: () => void): void;
@@ -22,7 +23,8 @@ export interface OnCancel {
2223
export class CancelablePromise<T> implements Promise<T> {
2324
readonly [Symbol.toStringTag]: string;
2425

25-
#isPending: boolean;
26+
#isResolved: boolean;
27+
#isRejected: boolean;
2628
#isCancelled: boolean;
2729
readonly #cancelHandlers: (() => void)[];
2830
readonly #promise: Promise<T>;
@@ -36,33 +38,43 @@ export class CancelablePromise<T> implements Promise<T> {
3638
onCancel: OnCancel
3739
) => void
3840
) {
39-
this.#isPending = true;
41+
this.#isResolved = false;
42+
this.#isRejected = false;
4043
this.#isCancelled = false;
4144
this.#cancelHandlers = [];
4245
this.#promise = new Promise<T>((resolve, reject) => {
4346
this.#resolve = resolve;
4447
this.#reject = reject;
4548

4649
const onResolve = (value: T | PromiseLike<T>): void => {
47-
if (!this.#isCancelled) {
48-
this.#isPending = false;
49-
this.#resolve?.(value);
50+
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
51+
return;
5052
}
53+
this.#isResolved = true;
54+
this.#resolve?.(value);
5155
};
5256

5357
const onReject = (reason?: any): void => {
54-
this.#isPending = false;
58+
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
59+
return;
60+
}
61+
this.#isRejected = true;
5562
this.#reject?.(reason);
5663
};
5764

5865
const onCancel = (cancelHandler: () => void): void => {
59-
if (this.#isPending) {
60-
this.#cancelHandlers.push(cancelHandler);
66+
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
67+
return;
6168
}
69+
this.#cancelHandlers.push(cancelHandler);
6270
};
6371

64-
Object.defineProperty(onCancel, 'isPending', {
65-
get: (): boolean => this.#isPending,
72+
Object.defineProperty(onCancel, 'isResolved', {
73+
get: (): boolean => this.#isResolved,
74+
});
75+
76+
Object.defineProperty(onCancel, 'isRejected', {
77+
get: (): boolean => this.#isRejected,
6678
});
6779

6880
Object.defineProperty(onCancel, 'isCancelled', {
@@ -91,7 +103,7 @@ export class CancelablePromise<T> implements Promise<T> {
91103
}
92104

93105
public cancel(): void {
94-
if (!this.#isPending || this.#isCancelled) {
106+
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
95107
return;
96108
}
97109
this.#isCancelled = true;
@@ -101,10 +113,12 @@ export class CancelablePromise<T> implements Promise<T> {
101113
cancelHandler();
102114
}
103115
} catch (error) {
104-
this.#reject?.(error);
116+
console.warn('Cancellation threw an error', error);
105117
return;
106118
}
107119
}
120+
this.#cancelHandlers.length = 0;
121+
this.#reject?.(new CancelError('Request aborted'));
108122
}
109123

110124
public get isCancelled(): boolean {

src/templates/core/OpenAPI.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Config = {
1515
PASSWORD?: string | Resolver<string>;
1616
HEADERS?: Headers | Resolver<Headers>;
1717
ENCODE_PATH?: (path: string) => string;
18-
}
18+
};
1919

2020
export const OpenAPI: Config = {
2121
BASE: '{{{server}}}',

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { OpenAPI } from './OpenAPI';
6060

6161
/**
6262
* Request using axios client
63-
* @param options The request options from the the service
63+
* @param options The request options from the service
6464
* @returns CancelablePromise<T>
6565
* @throws ApiError
6666
*/

src/templates/core/fetch/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import { OpenAPI } from './OpenAPI';
5757

5858
/**
5959
* Request using fetch client
60-
* @param options The request options from the the service
60+
* @param options The request options from the service
6161
* @returns CancelablePromise<T>
6262
* @throws ApiError
6363
*/

src/templates/core/node/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { OpenAPI } from './OpenAPI';
6161

6262
/**
6363
* Request using node-fetch client
64-
* @param options The request options from the the service
64+
* @param options The request options from the service
6565
* @returns CancelablePromise<T>
6666
* @throws ApiError
6767
*/

src/templates/core/xhr/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { OpenAPI } from './OpenAPI';
6060

6161
/**
6262
* Request using XHR client
63-
* @param options The request options from the the service
63+
* @param options The request options from the service
6464
* @returns CancelablePromise<T>
6565
* @throws ApiError
6666
*/

src/templates/core/xhr/sendRequest.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ async function sendRequest(
1616

1717
return new Promise<XMLHttpRequest>((resolve, reject) => {
1818
xhr.onload = () => resolve(xhr);
19-
xhr.onabort = () => reject(new Error('The user aborted a request.'));
20-
xhr.onerror = () => reject(new Error('Network error.'));
19+
xhr.onabort = () => reject(new Error('Request aborted'));
20+
xhr.onerror = () => reject(new Error('Network error'));
2121
xhr.send(body || formData);
2222

2323
onCancel(() => xhr.abort());

0 commit comments

Comments
 (0)