diff --git a/src/templates/core/CancelablePromise.hbs b/src/templates/core/CancelablePromise.hbs index bd24d59e..ff02b01c 100644 --- a/src/templates/core/CancelablePromise.hbs +++ b/src/templates/core/CancelablePromise.hbs @@ -1,35 +1,42 @@ +{{>header}} + export class CancelablePromise implements Promise { readonly [Symbol.toStringTag]: string; - private _isPending: boolean = true; - private _isCanceled: boolean = false; - private _promise: Promise; - private _resolve?: (value: T | PromiseLike) => void; - private _reject?: (reason?: unknown) => void; - private _cancelHandler?: () => void; + #isPending: boolean; + #isCanceled: boolean; + readonly #cancelHandlers: (() => void)[]; + readonly #promise: Promise; + #resolve?: (value: T | PromiseLike) => void; + #reject?: (reason?: any) => void; - constructor(executor: (resolve: (value: T | PromiseLike) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) { - this._promise = new Promise((resolve, reject) => { - this._resolve = resolve; - this._reject = reject; + constructor(executor: ( + resolve: (value: T | PromiseLike) => void, + reject: (reason?: any) => void, + onCancel: (cancelHandler: () => void) => void) => void, + ) { + this.#isPending = true; + this.#isCanceled = false; + this.#cancelHandlers = []; + this.#promise = new Promise((resolve, reject) => { + this.#resolve = resolve; + this.#reject = reject; const onResolve = (value: T | PromiseLike): void => { - if (!this._isCanceled && this._resolve) { - this._isPending = false; - this._resolve(value); + if (!this.#isCanceled) { + this.#isPending = false; + this.#resolve?.(value); } }; - const onReject = (reason?: unknown): void => { - if (this._reject) { - this._isPending = false; - this._reject(reason); - } + const onReject = (reason?: any): void => { + this.#isPending = false; + this.#reject?.(reason); }; const onCancel = (cancelHandler: () => void): void => { - if (this._isPending) { - this._cancelHandler = cancelHandler; + if (this.#isPending) { + this.#cancelHandlers.push(cancelHandler); } }; @@ -41,33 +48,35 @@ export class CancelablePromise implements Promise { onFulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onRejected?: ((reason: any) => TResult2 | PromiseLike) | null ): Promise { - return this._promise.then(onFulfilled, onRejected); + return this.#promise.then(onFulfilled, onRejected); } public catch(onRejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise { - return this._promise.catch(onRejected); + return this.#promise.catch(onRejected); } public finally(onFinally?: (() => void) | undefined | null): Promise { - return this._promise.finally(onFinally); + return this.#promise.finally(onFinally); } - public cancel() { - if (!this._isPending || this._isCanceled) { + public cancel(): void { + if (!this.#isPending || this.#isCanceled) { return; } - this._isCanceled = true; - if (this._cancelHandler && this._reject) { + this.#isCanceled = true; + if (this.#cancelHandlers.length) { try { - this._cancelHandler(); + for (const cancelHandler of this.#cancelHandlers) { + cancelHandler(); + } } catch (error) { - this._reject(error); + this.#reject?.(error); return; } } } public get isCanceled(): boolean { - return this._isCanceled; + return this.#isCanceled; } } diff --git a/src/templates/core/axios/request.hbs b/src/templates/core/axios/request.hbs index c4ad13d6..73b355a6 100644 --- a/src/templates/core/axios/request.hbs +++ b/src/templates/core/axios/request.hbs @@ -1,6 +1,6 @@ {{>header}} -import { AbortController, AbortSignal } from 'abort-controller'; +import { AbortController } from 'abort-controller'; import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; import FormData from 'form-data'; diff --git a/src/templates/core/axios/sendRequest.hbs b/src/templates/core/axios/sendRequest.hbs index 0736cb7b..3f47a12d 100644 --- a/src/templates/core/axios/sendRequest.hbs +++ b/src/templates/core/axios/sendRequest.hbs @@ -2,6 +2,7 @@ async function sendRequest(options: ApiRequestOptions, url: string, onCancel: (c const controller = new AbortController(); const formData = options.formData && getFormData(options.formData); const data = formData || options.body; + const config: AxiosRequestConfig = { url, data, diff --git a/src/templates/core/node/request.hbs b/src/templates/core/node/request.hbs index 76c83cbf..3270a53a 100644 --- a/src/templates/core/node/request.hbs +++ b/src/templates/core/node/request.hbs @@ -1,6 +1,6 @@ {{>header}} -import { AbortController, AbortSignal } from 'abort-controller'; +import { AbortController } from 'abort-controller'; import FormData from 'form-data'; import fetch, { BodyInit, Headers, RequestInit, Response } from 'node-fetch'; import { types } from 'util'; diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index c11acf52..8c3b9065 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -55,38 +55,46 @@ export type ApiResult = { `; exports[`v2 should generate: ./test/generated/v2/core/CancelablePromise.ts 1`] = ` -"export class CancelablePromise implements Promise { +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export class CancelablePromise implements Promise { readonly [Symbol.toStringTag]: string; - private _isPending: boolean = true; - private _isCanceled: boolean = false; - private _promise: Promise; - private _resolve?: (value: T | PromiseLike) => void; - private _reject?: (reason?: unknown) => void; - private _cancelHandler?: () => void; + #isPending: boolean; + #isCanceled: boolean; + readonly #cancelHandlers: (() => void)[]; + readonly #promise: Promise; + #resolve?: (value: T | PromiseLike) => void; + #reject?: (reason?: any) => void; - constructor(executor: (resolve: (value: T | PromiseLike) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) { - this._promise = new Promise((resolve, reject) => { - this._resolve = resolve; - this._reject = reject; + constructor(executor: ( + resolve: (value: T | PromiseLike) => void, + reject: (reason?: any) => void, + onCancel: (cancelHandler: () => void) => void) => void, + ) { + this.#isPending = true; + this.#isCanceled = false; + this.#cancelHandlers = []; + this.#promise = new Promise((resolve, reject) => { + this.#resolve = resolve; + this.#reject = reject; const onResolve = (value: T | PromiseLike): void => { - if (!this._isCanceled && this._resolve) { - this._isPending = false; - this._resolve(value); + if (!this.#isCanceled) { + this.#isPending = false; + this.#resolve?.(value); } }; - const onReject = (reason?: unknown): void => { - if (this._reject) { - this._isPending = false; - this._reject(reason); - } + const onReject = (reason?: any): void => { + this.#isPending = false; + this.#reject?.(reason); }; const onCancel = (cancelHandler: () => void): void => { - if (this._isPending) { - this._cancelHandler = cancelHandler; + if (this.#isPending) { + this.#cancelHandlers.push(cancelHandler); } }; @@ -98,34 +106,36 @@ exports[`v2 should generate: ./test/generated/v2/core/CancelablePromise.ts 1`] = onFulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onRejected?: ((reason: any) => TResult2 | PromiseLike) | null ): Promise { - return this._promise.then(onFulfilled, onRejected); + return this.#promise.then(onFulfilled, onRejected); } public catch(onRejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise { - return this._promise.catch(onRejected); + return this.#promise.catch(onRejected); } public finally(onFinally?: (() => void) | undefined | null): Promise { - return this._promise.finally(onFinally); + return this.#promise.finally(onFinally); } - public cancel() { - if (!this._isPending || this._isCanceled) { + public cancel(): void { + if (!this.#isPending || this.#isCanceled) { return; } - this._isCanceled = true; - if (this._cancelHandler && this._reject) { + this.#isCanceled = true; + if (this.#cancelHandlers.length) { try { - this._cancelHandler(); + for (const cancelHandler of this.#cancelHandlers) { + cancelHandler(); + } } catch (error) { - this._reject(error); + this.#reject?.(error); return; } } } public get isCanceled(): boolean { - return this._isCanceled; + return this.#isCanceled; } }" `; @@ -2514,38 +2524,46 @@ export type ApiResult = { `; exports[`v3 should generate: ./test/generated/v3/core/CancelablePromise.ts 1`] = ` -"export class CancelablePromise implements Promise { +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export class CancelablePromise implements Promise { readonly [Symbol.toStringTag]: string; - private _isPending: boolean = true; - private _isCanceled: boolean = false; - private _promise: Promise; - private _resolve?: (value: T | PromiseLike) => void; - private _reject?: (reason?: unknown) => void; - private _cancelHandler?: () => void; + #isPending: boolean; + #isCanceled: boolean; + readonly #cancelHandlers: (() => void)[]; + readonly #promise: Promise; + #resolve?: (value: T | PromiseLike) => void; + #reject?: (reason?: any) => void; - constructor(executor: (resolve: (value: T | PromiseLike) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) { - this._promise = new Promise((resolve, reject) => { - this._resolve = resolve; - this._reject = reject; + constructor(executor: ( + resolve: (value: T | PromiseLike) => void, + reject: (reason?: any) => void, + onCancel: (cancelHandler: () => void) => void) => void, + ) { + this.#isPending = true; + this.#isCanceled = false; + this.#cancelHandlers = []; + this.#promise = new Promise((resolve, reject) => { + this.#resolve = resolve; + this.#reject = reject; const onResolve = (value: T | PromiseLike): void => { - if (!this._isCanceled && this._resolve) { - this._isPending = false; - this._resolve(value); + if (!this.#isCanceled) { + this.#isPending = false; + this.#resolve?.(value); } }; - const onReject = (reason?: unknown): void => { - if (this._reject) { - this._isPending = false; - this._reject(reason); - } + const onReject = (reason?: any): void => { + this.#isPending = false; + this.#reject?.(reason); }; const onCancel = (cancelHandler: () => void): void => { - if (this._isPending) { - this._cancelHandler = cancelHandler; + if (this.#isPending) { + this.#cancelHandlers.push(cancelHandler); } }; @@ -2557,34 +2575,36 @@ exports[`v3 should generate: ./test/generated/v3/core/CancelablePromise.ts 1`] = onFulfilled?: ((value: T) => TResult1 | PromiseLike) | null, onRejected?: ((reason: any) => TResult2 | PromiseLike) | null ): Promise { - return this._promise.then(onFulfilled, onRejected); + return this.#promise.then(onFulfilled, onRejected); } public catch(onRejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise { - return this._promise.catch(onRejected); + return this.#promise.catch(onRejected); } public finally(onFinally?: (() => void) | undefined | null): Promise { - return this._promise.finally(onFinally); + return this.#promise.finally(onFinally); } - public cancel() { - if (!this._isPending || this._isCanceled) { + public cancel(): void { + if (!this.#isPending || this.#isCanceled) { return; } - this._isCanceled = true; - if (this._cancelHandler && this._reject) { + this.#isCanceled = true; + if (this.#cancelHandlers.length) { try { - this._cancelHandler(); + for (const cancelHandler of this.#cancelHandlers) { + cancelHandler(); + } } catch (error) { - this._reject(error); + this.#reject?.(error); return; } } } public get isCanceled(): boolean { - return this._isCanceled; + return this.#isCanceled; } }" `;