From 4637325b0a8fd3f951568725e0198ec6da619c13 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Wed, 26 Jan 2022 20:15:50 +0100 Subject: [PATCH 1/2] - Shorter writing style for props passed in constructors - Simplified header creation - Made helper scripts for test more reasable --- CONTRIBUTING.md | 2 +- jest.config.ts | 1 + src/templates/core/BaseHttpRequest.hbs | 6 +-- src/templates/core/axios/request.hbs | 3 +- src/templates/core/axios/sendRequest.hbs | 6 +-- src/templates/core/fetch/getHeaders.hbs | 18 +++---- src/templates/core/fetch/getRequestBody.hbs | 2 +- src/templates/core/fetch/request.hbs | 2 +- src/templates/core/fetch/sendRequest.hbs | 2 +- src/templates/core/functions/getUrl.hbs | 28 +++++----- src/templates/core/node/getHeaders.hbs | 19 ++++--- src/templates/core/node/getRequestBody.hbs | 2 +- src/templates/core/node/request.hbs | 2 +- src/templates/core/node/sendRequest.hbs | 2 +- src/templates/core/xhr/getHeaders.hbs | 19 ++++--- src/templates/core/xhr/request.hbs | 2 +- src/templates/core/xhr/sendRequest.hbs | 2 +- src/templates/exportService.hbs | 10 ++-- test/__snapshots__/index.spec.ts.snap | 58 ++++++++++++--------- test/e2e/client.axios.spec.ts | 6 ++- test/e2e/client.babel.spec.ts | 11 ++-- test/e2e/client.fetch.spec.ts | 11 ++-- test/e2e/client.node.spec.ts | 6 ++- test/e2e/client.xhr.spec.ts | 11 ++-- test/e2e/scripts/compileWithTypescript.ts | 7 +-- test/e2e/scripts/server.ts | 18 +++++-- test/e2e/v2.axios.spec.ts | 6 ++- test/e2e/v2.babel.spec.ts | 11 ++-- test/e2e/v2.fetch.spec.ts | 11 ++-- test/e2e/v2.node.spec.ts | 6 ++- test/e2e/v2.xhr.spec.ts | 11 ++-- test/e2e/v3.axios.spec.ts | 6 ++- test/e2e/v3.babel.spec.ts | 11 ++-- test/e2e/v3.fetch.spec.ts | 11 ++-- test/e2e/v3.node.spec.ts | 6 ++- test/e2e/v3.xhr.spec.ts | 11 ++-- 36 files changed, 195 insertions(+), 151 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5c36c30a..e2427d27 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,7 +8,7 @@ Before working on a Pull Request, create an issue explaining what you want to co This ensures that your pull request won't go unnoticed, and that you are not contributing something that is not suitable for the project. -If you are unfamiliar with Github Pull Requests, please read the following documentation: +If you are unfamiliar with GitHub Pull Requests, please read the following documentation: https://help.github.com/articles/using-pull-requests **Your Pull Request must:** diff --git a/jest.config.ts b/jest.config.ts index 9c39db93..e1b5e68a 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -31,6 +31,7 @@ const config: Config.InitialOptions = { '/test/e2e/client.axios.spec.ts', '/test/e2e/client.babel.spec.ts', ], + testPathIgnorePatterns: ['/test/e2e/generated'], }, ], collectCoverageFrom: ['/src/**/*.ts', '!/src/**/*.d.ts', '!/bin', '!/dist'], diff --git a/src/templates/core/BaseHttpRequest.hbs b/src/templates/core/BaseHttpRequest.hbs index 2b7ca480..fc7f0df5 100644 --- a/src/templates/core/BaseHttpRequest.hbs +++ b/src/templates/core/BaseHttpRequest.hbs @@ -6,11 +6,7 @@ import type { OpenAPIConfig } from './OpenAPI'; export class BaseHttpRequest { - protected readonly config: OpenAPIConfig; - - constructor(config: OpenAPIConfig) { - this.config = config; - } + constructor(protected readonly config: OpenAPIConfig) {} public request(options: ApiRequestOptions): CancelablePromise { throw new Error('Not Implemented'); diff --git a/src/templates/core/axios/request.hbs b/src/templates/core/axios/request.hbs index 6f7765a0..1ec09e8a 100644 --- a/src/templates/core/axios/request.hbs +++ b/src/templates/core/axios/request.hbs @@ -74,7 +74,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C const headers = await getHeaders(config, options, formData); if (!onCancel.isCancelled) { - const response = await sendRequest(config, options, url, formData, body, headers, onCancel); + const response = await sendRequest(config, options, url, body, formData, headers, onCancel); const responseBody = getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); @@ -95,4 +95,3 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C } }); }; - diff --git a/src/templates/core/axios/sendRequest.hbs b/src/templates/core/axios/sendRequest.hbs index 41af3b46..3f0d6e02 100644 --- a/src/templates/core/axios/sendRequest.hbs +++ b/src/templates/core/axios/sendRequest.hbs @@ -1,12 +1,12 @@ -const sendRequest = async ( +const sendRequest = async ( config: OpenAPIConfig, options: ApiRequestOptions, url: string, - formData: FormData | undefined, body: any, + formData: FormData | undefined, headers: Record, onCancel: OnCancel -): Promise> => { +): Promise> => { const source = axios.CancelToken.source(); const requestConfig: AxiosRequestConfig = { diff --git a/src/templates/core/fetch/getHeaders.hbs b/src/templates/core/fetch/getHeaders.hbs index 009b3a84..41a65005 100644 --- a/src/templates/core/fetch/getHeaders.hbs +++ b/src/templates/core/fetch/getHeaders.hbs @@ -4,7 +4,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr const password = await resolve(options, config.PASSWORD); const additionalHeaders = await resolve(options, config.HEADERS); - const defaultHeaders = Object.entries({ + const headers = Object.entries({ Accept: 'application/json', ...additionalHeaders, ...options.headers, @@ -15,28 +15,26 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); - if (isStringWithValue(token)) { - headers.append('Authorization', `Bearer ${token}`); + headers['Authorization'] = `Bearer ${token}`; } if (isStringWithValue(username) && isStringWithValue(password)) { const credentials = base64(`${username}:${password}`); - headers.append('Authorization', `Basic ${credentials}`); + headers['Authorization'] = `Basic ${credentials}`; } if (options.body) { if (options.mediaType) { - headers.append('Content-Type', options.mediaType); + headers['Content-Type'] = options.mediaType; } else if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); + headers['Content-Type'] = options.body.type || 'application/octet-stream'; } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); + headers['Content-Type'] = 'text/plain'; } else if (!isFormData(options.body)) { - headers.append('Content-Type', 'application/json'); + headers['Content-Type'] = 'application/json'; } } - return headers; + return new Headers(headers); }; diff --git a/src/templates/core/fetch/getRequestBody.hbs b/src/templates/core/fetch/getRequestBody.hbs index 4d4780c4..9c404966 100644 --- a/src/templates/core/fetch/getRequestBody.hbs +++ b/src/templates/core/fetch/getRequestBody.hbs @@ -1,4 +1,4 @@ -const getRequestBody = (options: ApiRequestOptions): BodyInit | undefined => { +const getRequestBody = (options: ApiRequestOptions): any => { if (options.body) { if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) diff --git a/src/templates/core/fetch/request.hbs b/src/templates/core/fetch/request.hbs index 5815f6e4..13bf6a3e 100644 --- a/src/templates/core/fetch/request.hbs +++ b/src/templates/core/fetch/request.hbs @@ -71,7 +71,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C const headers = await getHeaders(config, options); if (!onCancel.isCancelled) { - const response = await sendRequest(config, options, url, formData, body, headers, onCancel); + const response = await sendRequest(config, options, url, body, formData, headers, onCancel); const responseBody = await getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); diff --git a/src/templates/core/fetch/sendRequest.hbs b/src/templates/core/fetch/sendRequest.hbs index d0a0c7c7..73f71f42 100644 --- a/src/templates/core/fetch/sendRequest.hbs +++ b/src/templates/core/fetch/sendRequest.hbs @@ -2,8 +2,8 @@ export const sendRequest = async ( config: OpenAPIConfig, options: ApiRequestOptions, url: string, + body: any, formData: FormData | undefined, - body: BodyInit | undefined, headers: Headers, onCancel: OnCancel ): Promise => { diff --git a/src/templates/core/functions/getUrl.hbs b/src/templates/core/functions/getUrl.hbs index 4624533d..fe181ab2 100644 --- a/src/templates/core/functions/getUrl.hbs +++ b/src/templates/core/functions/getUrl.hbs @@ -1,18 +1,18 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => { - const encoder = config.ENCODE_PATH || encodeURI; + const encoder = config.ENCODE_PATH || encodeURI; - const path = options.url - .replace('{api-version}', config.VERSION) - .replace(/{(.*?)}/g, (substring: string, group: string) => { - if (options.path?.hasOwnProperty(group)) { - return encoder(String(options.path[group])); - } - return substring; - }); + const path = options.url + .replace('{api-version}', config.VERSION) + .replace(/{(.*?)}/g, (substring: string, group: string) => { + if (options.path?.hasOwnProperty(group)) { + return encoder(String(options.path[group])); + } + return substring; + }); - const url = `${config.BASE}${path}`; - if (options.query) { - return `${url}${getQueryString(options.query)}`; - } - return url; + const url = `${config.BASE}${path}`; + if (options.query) { + return `${url}${getQueryString(options.query)}`; + } + return url; }; diff --git a/src/templates/core/node/getHeaders.hbs b/src/templates/core/node/getHeaders.hbs index 10f61278..80f2cbcc 100644 --- a/src/templates/core/node/getHeaders.hbs +++ b/src/templates/core/node/getHeaders.hbs @@ -4,7 +4,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr const password = await resolve(options, config.PASSWORD); const additionalHeaders = await resolve(options, config.HEADERS); - const defaultHeaders = Object.entries({ + const headers = Object.entries({ Accept: 'application/json', ...additionalHeaders, ...options.headers, @@ -15,27 +15,26 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); - if (isStringWithValue(token)) { - headers.append('Authorization', `Bearer ${token}`); + headers['Authorization'] = `Bearer ${token}`; } if (isStringWithValue(username) && isStringWithValue(password)) { const credentials = base64(`${username}:${password}`); - headers.append('Authorization', `Basic ${credentials}`); + headers['Authorization'] = `Basic ${credentials}`; } if (options.body) { if (options.mediaType) { - headers.append('Content-Type', options.mediaType); + headers['Content-Type'] = options.mediaType; } else if (isBlob(options.body)) { - headers.append('Content-Type', 'application/octet-stream'); + headers['Content-Type'] = 'application/octet-stream'; } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); + headers['Content-Type'] = 'text/plain'; } else if (!isFormData(options.body)) { - headers.append('Content-Type', 'application/json'); + headers['Content-Type'] = 'application/json'; } } - return headers; + + return new Headers(headers); }; diff --git a/src/templates/core/node/getRequestBody.hbs b/src/templates/core/node/getRequestBody.hbs index 7cc68fb8..e0b420c2 100644 --- a/src/templates/core/node/getRequestBody.hbs +++ b/src/templates/core/node/getRequestBody.hbs @@ -1,4 +1,4 @@ -const getRequestBody = (options: ApiRequestOptions): BodyInit | undefined => { +const getRequestBody = (options: ApiRequestOptions): any => { if (options.body) { if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) diff --git a/src/templates/core/node/request.hbs b/src/templates/core/node/request.hbs index fdd33a26..ba82a166 100644 --- a/src/templates/core/node/request.hbs +++ b/src/templates/core/node/request.hbs @@ -75,7 +75,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C const headers = await getHeaders(config, options); if (!onCancel.isCancelled) { - const response = await sendRequest(options, url, formData, body, headers, onCancel); + const response = await sendRequest(options, url, body, formData, headers, onCancel); const responseBody = await getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); diff --git a/src/templates/core/node/sendRequest.hbs b/src/templates/core/node/sendRequest.hbs index dd6ac3df..b7b160f4 100644 --- a/src/templates/core/node/sendRequest.hbs +++ b/src/templates/core/node/sendRequest.hbs @@ -1,8 +1,8 @@ export const sendRequest = async ( options: ApiRequestOptions, url: string, + body: any, formData: FormData | undefined, - body: BodyInit | undefined, headers: Headers, onCancel: OnCancel ): Promise => { diff --git a/src/templates/core/xhr/getHeaders.hbs b/src/templates/core/xhr/getHeaders.hbs index 16a611bc..41a65005 100644 --- a/src/templates/core/xhr/getHeaders.hbs +++ b/src/templates/core/xhr/getHeaders.hbs @@ -4,7 +4,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr const password = await resolve(options, config.PASSWORD); const additionalHeaders = await resolve(options, config.HEADERS); - const defaultHeaders = Object.entries({ + const headers = Object.entries({ Accept: 'application/json', ...additionalHeaders, ...options.headers, @@ -15,27 +15,26 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); - if (isStringWithValue(token)) { - headers.append('Authorization', `Bearer ${token}`); + headers['Authorization'] = `Bearer ${token}`; } if (isStringWithValue(username) && isStringWithValue(password)) { const credentials = base64(`${username}:${password}`); - headers.append('Authorization', `Basic ${credentials}`); + headers['Authorization'] = `Basic ${credentials}`; } if (options.body) { if (options.mediaType) { - headers.append('Content-Type', options.mediaType); + headers['Content-Type'] = options.mediaType; } else if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); + headers['Content-Type'] = options.body.type || 'application/octet-stream'; } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); + headers['Content-Type'] = 'text/plain'; } else if (!isFormData(options.body)) { - headers.append('Content-Type', 'application/json'); + headers['Content-Type'] = 'application/json'; } } - return headers; + + return new Headers(headers); }; diff --git a/src/templates/core/xhr/request.hbs b/src/templates/core/xhr/request.hbs index b5f91d4b..7732cd6e 100644 --- a/src/templates/core/xhr/request.hbs +++ b/src/templates/core/xhr/request.hbs @@ -74,7 +74,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C const headers = await getHeaders(config, options); if (!onCancel.isCancelled) { - const response = await sendRequest(config, options, url, formData, body, headers, onCancel); + const response = await sendRequest(config, options, url, body, formData, headers, onCancel); const responseBody = getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); diff --git a/src/templates/core/xhr/sendRequest.hbs b/src/templates/core/xhr/sendRequest.hbs index ea2f8da5..0badf8da 100644 --- a/src/templates/core/xhr/sendRequest.hbs +++ b/src/templates/core/xhr/sendRequest.hbs @@ -2,8 +2,8 @@ export const sendRequest = async ( config: OpenAPIConfig, options: ApiRequestOptions, url: string, - formData: FormData | undefined, body: any, + formData: FormData | undefined, headers: Headers, onCancel: OnCancel ): Promise => { diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index d0f8aa5c..1d481dd5 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -4,6 +4,7 @@ {{#each imports}} import type { {{{this}}} } from '../models/{{{this}}}'; {{/each}} + {{/if}} import type { CancelablePromise } from '../core/CancelablePromise'; {{#if @root.exportClient}} @@ -16,9 +17,7 @@ import { request as __request } from '../core/request'; export class {{{name}}}{{{@root.postfix}}} { {{#if @root.exportClient}} - private readonly httpRequest: BaseHttpRequest; - - constructor(httpRequest: BaseHttpRequest) { + constructor(private readonly httpRequest: BaseHttpRequest) { this.httpRequest = httpRequest; } {{/if}} @@ -49,11 +48,10 @@ export class {{{name}}}{{{@root.postfix}}} { {{#if @root.exportClient}} public {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> { return this.httpRequest.request({ - {{/if}} - {{#unless @root.exportClient}} + {{else}} public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> { return __request(OpenAPI, { - {{/unless}} + {{/if}} method: '{{{method}}}', url: '{{{path}}}', {{#if parametersPath}} diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index 062e39f2..2b48d1d1 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -365,7 +365,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr const password = await resolve(options, config.PASSWORD); const additionalHeaders = await resolve(options, config.HEADERS); - const defaultHeaders = Object.entries({ + const headers = Object.entries({ Accept: 'application/json', ...additionalHeaders, ...options.headers, @@ -376,33 +376,31 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); - if (isStringWithValue(token)) { - headers.append('Authorization', \`Bearer \${token}\`); + headers['Authorization'] = \`Bearer \${token}\`; } if (isStringWithValue(username) && isStringWithValue(password)) { const credentials = base64(\`\${username}:\${password}\`); - headers.append('Authorization', \`Basic \${credentials}\`); + headers['Authorization'] = \`Basic \${credentials}\`; } if (options.body) { if (options.mediaType) { - headers.append('Content-Type', options.mediaType); + headers['Content-Type'] = options.mediaType; } else if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); + headers['Content-Type'] = options.body.type || 'application/octet-stream'; } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); + headers['Content-Type'] = 'text/plain'; } else if (!isFormData(options.body)) { - headers.append('Content-Type', 'application/json'); + headers['Content-Type'] = 'application/json'; } } - return headers; + return new Headers(headers); }; -const getRequestBody = (options: ApiRequestOptions): BodyInit | undefined => { +const getRequestBody = (options: ApiRequestOptions): any => { if (options.body) { if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) @@ -419,8 +417,8 @@ export const sendRequest = async ( config: OpenAPIConfig, options: ApiRequestOptions, url: string, + body: any, formData: FormData | undefined, - body: BodyInit | undefined, headers: Headers, onCancel: OnCancel ): Promise => { @@ -509,7 +507,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C const headers = await getHeaders(config, options); if (!onCancel.isCancelled) { - const response = await sendRequest(config, options, url, formData, body, headers, onCancel); + const response = await sendRequest(config, options, url, body, formData, headers, onCancel); const responseBody = await getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); @@ -2196,6 +2194,7 @@ exports[`v2 should generate: ./test/generated/v2/services/ComplexService.ts 1`] /* tslint:disable */ /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -2263,6 +2262,7 @@ exports[`v2 should generate: ./test/generated/v2/services/DefaultsService.ts 1`] /* tslint:disable */ /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -2702,6 +2702,7 @@ exports[`v2 should generate: ./test/generated/v2/services/ResponseService.ts 1`] import type { ModelThatExtends } from '../models/ModelThatExtends'; import type { ModelThatExtendsExtends } from '../models/ModelThatExtendsExtends'; import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -3265,7 +3266,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr const password = await resolve(options, config.PASSWORD); const additionalHeaders = await resolve(options, config.HEADERS); - const defaultHeaders = Object.entries({ + const headers = Object.entries({ Accept: 'application/json', ...additionalHeaders, ...options.headers, @@ -3276,33 +3277,31 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); - if (isStringWithValue(token)) { - headers.append('Authorization', \`Bearer \${token}\`); + headers['Authorization'] = \`Bearer \${token}\`; } if (isStringWithValue(username) && isStringWithValue(password)) { const credentials = base64(\`\${username}:\${password}\`); - headers.append('Authorization', \`Basic \${credentials}\`); + headers['Authorization'] = \`Basic \${credentials}\`; } if (options.body) { if (options.mediaType) { - headers.append('Content-Type', options.mediaType); + headers['Content-Type'] = options.mediaType; } else if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); + headers['Content-Type'] = options.body.type || 'application/octet-stream'; } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); + headers['Content-Type'] = 'text/plain'; } else if (!isFormData(options.body)) { - headers.append('Content-Type', 'application/json'); + headers['Content-Type'] = 'application/json'; } } - return headers; + return new Headers(headers); }; -const getRequestBody = (options: ApiRequestOptions): BodyInit | undefined => { +const getRequestBody = (options: ApiRequestOptions): any => { if (options.body) { if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) @@ -3319,8 +3318,8 @@ export const sendRequest = async ( config: OpenAPIConfig, options: ApiRequestOptions, url: string, + body: any, formData: FormData | undefined, - body: BodyInit | undefined, headers: Headers, onCancel: OnCancel ): Promise => { @@ -3409,7 +3408,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C const headers = await getHeaders(config, options); if (!onCancel.isCancelled) { - const response = await sendRequest(config, options, url, formData, body, headers, onCancel); + const response = await sendRequest(config, options, url, body, formData, headers, onCancel); const responseBody = await getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); @@ -5676,6 +5675,7 @@ import type { ModelWithArray } from '../models/ModelWithArray'; import type { ModelWithDictionary } from '../models/ModelWithDictionary'; import type { ModelWithEnum } from '../models/ModelWithEnum'; import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -5776,6 +5776,7 @@ exports[`v3 should generate: ./test/generated/v3/services/DefaultsService.ts 1`] /* tslint:disable */ /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -5976,6 +5977,7 @@ exports[`v3 should generate: ./test/generated/v3/services/FormDataService.ts 1`] /* tslint:disable */ /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -6039,6 +6041,7 @@ exports[`v3 should generate: ./test/generated/v3/services/MultipartService.ts 1` /* tslint:disable */ /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -6206,6 +6209,7 @@ exports[`v3 should generate: ./test/generated/v3/services/ParametersService.ts 1 /* tslint:disable */ /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -6349,6 +6353,7 @@ exports[`v3 should generate: ./test/generated/v3/services/RequestBodyService.ts /* tslint:disable */ /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -6385,6 +6390,7 @@ exports[`v3 should generate: ./test/generated/v3/services/ResponseService.ts 1`] import type { ModelThatExtends } from '../models/ModelThatExtends'; import type { ModelThatExtendsExtends } from '../models/ModelThatExtendsExtends'; import type { ModelWithString } from '../models/ModelWithString'; + import type { CancelablePromise } from '../core/CancelablePromise'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; diff --git a/test/e2e/client.axios.spec.ts b/test/e2e/client.axios.spec.ts index 3a96ac85..80bde560 100644 --- a/test/e2e/client.axios.spec.ts +++ b/test/e2e/client.axios.spec.ts @@ -1,10 +1,12 @@ +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { generate } from './scripts/generate'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.node', () => { beforeAll(async () => { - await generate('client/axios', 'v3', 'axios', false, false, 'AppClient'); + cleanup('client/axios'); + await generateClient('client/axios', 'v3', 'axios', false, false, 'AppClient'); compileWithTypescript('client/axios'); await server.start('client/axios'); }, 30000); diff --git a/test/e2e/client.babel.spec.ts b/test/e2e/client.babel.spec.ts index 5354f5ec..ec47ee14 100644 --- a/test/e2e/client.babel.spec.ts +++ b/test/e2e/client.babel.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithBabel } from './scripts/compileWithBabel'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.babel', () => { beforeAll(async () => { - await generate('client/babel', 'v3', 'fetch', true, true, 'AppClient'); - await copy('client/babel'); + cleanup('client/babel'); + await generateClient('client/babel', 'v3', 'fetch', true, true, 'AppClient'); + copyAsset('index.html', 'client/babel/index.html'); + copyAsset('main.ts', 'client/babel/main.ts'); compileWithBabel('client/babel'); await server.start('client/babel'); await browser.start(); diff --git a/test/e2e/client.fetch.spec.ts b/test/e2e/client.fetch.spec.ts index 126030a1..58b0db9e 100644 --- a/test/e2e/client.fetch.spec.ts +++ b/test/e2e/client.fetch.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.fetch', () => { beforeAll(async () => { - await generate('client/fetch', 'v3', 'fetch', false, false, 'AppClient'); - await copy('client/fetch'); + cleanup('client/fetch'); + await generateClient('client/fetch', 'v3', 'fetch', false, false, 'AppClient'); + copyAsset('index.html', 'client/fetch/index.html'); + copyAsset('main.ts', 'client/fetch/main.ts'); compileWithTypescript('client/fetch'); await server.start('client/fetch'); await browser.start(); diff --git a/test/e2e/client.node.spec.ts b/test/e2e/client.node.spec.ts index 8ed61d96..c4a0e509 100644 --- a/test/e2e/client.node.spec.ts +++ b/test/e2e/client.node.spec.ts @@ -1,10 +1,12 @@ +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { generate } from './scripts/generate'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.node', () => { beforeAll(async () => { - await generate('client/node', 'v3', 'node', false, false, 'AppClient'); + cleanup('client/node'); + await generateClient('client/node', 'v3', 'node', false, false, 'AppClient'); compileWithTypescript('client/node'); await server.start('client/node'); }, 30000); diff --git a/test/e2e/client.xhr.spec.ts b/test/e2e/client.xhr.spec.ts index 67446c42..9887f619 100644 --- a/test/e2e/client.xhr.spec.ts +++ b/test/e2e/client.xhr.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.xhr', () => { beforeAll(async () => { - await generate('client/xhr', 'v3', 'xhr', false, false, 'AppClient'); - await copy('client/xhr'); + cleanup('client/xhr'); + await generateClient('client/xhr', 'v3', 'xhr', false, false, 'AppClient'); + copyAsset('index.html', 'client/xhr/index.html'); + copyAsset('main.ts', 'client/xhr/main.ts'); compileWithTypescript('client/xhr'); await server.start('client/xhr'); await browser.start(); diff --git a/test/e2e/scripts/compileWithTypescript.ts b/test/e2e/scripts/compileWithTypescript.ts index 919ca28b..4a8ab1de 100644 --- a/test/e2e/scripts/compileWithTypescript.ts +++ b/test/e2e/scripts/compileWithTypescript.ts @@ -11,7 +11,7 @@ import { } from 'typescript'; export const compileWithTypescript = (dir: string) => { - const baseDir = `./test/e2e/generated/${dir}/`; + const cwd = `./test/e2e/generated/${dir}/`; const tsconfig = { compilerOptions: { target: 'es2020', @@ -27,8 +27,9 @@ export const compileWithTypescript = (dir: string) => { strict: true, skipLibCheck: true, allowSyntheticDefaultImports: true, + experimentalDecorators: true, }, - include: ['./index.ts'], + include: ['**/*.ts'], }; // Compile files to JavaScript (ES6 modules) @@ -36,7 +37,7 @@ export const compileWithTypescript = (dir: string) => { const configFileResult = parseJsonConfigFileContent( configFile.config, sys, - resolve(process.cwd(), baseDir), + resolve(process.cwd(), cwd), undefined, 'tsconfig.json' ); diff --git a/test/e2e/scripts/server.ts b/test/e2e/scripts/server.ts index e7a6730f..6097ff97 100644 --- a/test/e2e/scripts/server.ts +++ b/test/e2e/scripts/server.ts @@ -1,5 +1,6 @@ import express, { Express } from 'express'; import { Server } from 'http'; +import { resolve as resolvePath } from 'path'; let _app: Express; let _server: Server; @@ -19,11 +20,16 @@ const start = async (dir: string) => { }) ); - // When we request the index then we can just return the script loader. - // This file is copied from test/e2e/assets/script.js to the output directory - // of the specific version and client. + _app.use( + '/js', + express.static(`./test/e2e/generated/${dir}/`, { + extensions: ['', 'js'], + index: 'index.js', + }) + ); + _app.get('/', (req, res) => { - res.send(''); + res.sendFile(resolvePath(`./test/e2e/generated/${dir}/index.html`)); }); // Register an 'echo' server for testing error codes. This will just grab the @@ -51,7 +57,9 @@ const start = async (dir: string) => { }); }, 100); }); - _server = _app.listen(3000, resolve); + _server = _app.listen(3000, () => { + resolve(); + }); }); }; diff --git a/test/e2e/v2.axios.spec.ts b/test/e2e/v2.axios.spec.ts index 5ef89239..9b4a912a 100644 --- a/test/e2e/v2.axios.spec.ts +++ b/test/e2e/v2.axios.spec.ts @@ -1,10 +1,12 @@ +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { generate } from './scripts/generate'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v2.node', () => { beforeAll(async () => { - await generate('v2/axios', 'v2', 'axios'); + cleanup('v2/axios'); + await generateClient('v2/axios', 'v2', 'axios'); compileWithTypescript('v2/axios'); await server.start('v2/axios'); }, 30000); diff --git a/test/e2e/v2.babel.spec.ts b/test/e2e/v2.babel.spec.ts index 96f14ee4..574558f6 100644 --- a/test/e2e/v2.babel.spec.ts +++ b/test/e2e/v2.babel.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithBabel } from './scripts/compileWithBabel'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v2.babel', () => { beforeAll(async () => { - await generate('v2/babel', 'v2', 'fetch', true, true); - await copy('v2/babel'); + cleanup('v2/babel'); + await generateClient('v2/babel', 'v2', 'fetch', true, true); + copyAsset('index.html', 'v2/babel/index.html'); + copyAsset('main.ts', 'v2/babel/main.ts'); compileWithBabel('v2/babel'); await server.start('v2/babel'); await browser.start(); diff --git a/test/e2e/v2.fetch.spec.ts b/test/e2e/v2.fetch.spec.ts index a3b831f1..9d4beba8 100644 --- a/test/e2e/v2.fetch.spec.ts +++ b/test/e2e/v2.fetch.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v2.fetch', () => { beforeAll(async () => { - await generate('v2/fetch', 'v2', 'fetch'); - await copy('v2/fetch'); + cleanup('v2/fetch'); + await generateClient('v2/fetch', 'v2', 'fetch'); + copyAsset('index.html', 'v2/fetch/index.html'); + copyAsset('main.ts', 'v2/fetch/main.ts'); compileWithTypescript('v2/fetch'); await server.start('v2/fetch'); await browser.start(); diff --git a/test/e2e/v2.node.spec.ts b/test/e2e/v2.node.spec.ts index 19a779ee..1e943c56 100644 --- a/test/e2e/v2.node.spec.ts +++ b/test/e2e/v2.node.spec.ts @@ -1,10 +1,12 @@ +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { generate } from './scripts/generate'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v2.node', () => { beforeAll(async () => { - await generate('v2/node', 'v2', 'node'); + cleanup('v2/node'); + await generateClient('v2/node', 'v2', 'node'); compileWithTypescript('v2/node'); await server.start('v2/node'); }, 30000); diff --git a/test/e2e/v2.xhr.spec.ts b/test/e2e/v2.xhr.spec.ts index 16b0c1e2..d8c7b0ea 100644 --- a/test/e2e/v2.xhr.spec.ts +++ b/test/e2e/v2.xhr.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v2.xhr', () => { beforeAll(async () => { - await generate('v2/xhr', 'v2', 'xhr'); - await copy('v2/xhr'); + cleanup('v2/xhr'); + await generateClient('v2/xhr', 'v2', 'xhr'); + copyAsset('index.html', 'v2/xhr/index.html'); + copyAsset('main.ts', 'v2/xhr/main.ts'); compileWithTypescript('v2/xhr'); await server.start('v2/xhr'); await browser.start(); diff --git a/test/e2e/v3.axios.spec.ts b/test/e2e/v3.axios.spec.ts index bd4f0745..535274d0 100644 --- a/test/e2e/v3.axios.spec.ts +++ b/test/e2e/v3.axios.spec.ts @@ -1,10 +1,12 @@ +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { generate } from './scripts/generate'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.node', () => { beforeAll(async () => { - await generate('v3/axios', 'v3', 'axios'); + cleanup('v3/axios'); + await generateClient('v3/axios', 'v3', 'axios'); compileWithTypescript('v3/axios'); await server.start('v3/axios'); }, 30000); diff --git a/test/e2e/v3.babel.spec.ts b/test/e2e/v3.babel.spec.ts index e6cf6aef..8ad86724 100644 --- a/test/e2e/v3.babel.spec.ts +++ b/test/e2e/v3.babel.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithBabel } from './scripts/compileWithBabel'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.babel', () => { beforeAll(async () => { - await generate('v3/babel', 'v3', 'fetch', true, true); - await copy('v3/babel'); + cleanup('v3/babel'); + await generateClient('v3/babel', 'v3', 'fetch', true, true); + copyAsset('index.html', 'v3/babel/index.html'); + copyAsset('main.ts', 'v3/babel/main.ts'); compileWithBabel('v3/babel'); await server.start('v3/babel'); await browser.start(); diff --git a/test/e2e/v3.fetch.spec.ts b/test/e2e/v3.fetch.spec.ts index fca7080a..2fd949e1 100644 --- a/test/e2e/v3.fetch.spec.ts +++ b/test/e2e/v3.fetch.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.fetch', () => { beforeAll(async () => { - await generate('v3/fetch', 'v3', 'fetch'); - await copy('v3/fetch'); + cleanup('v3/fetch'); + await generateClient('v3/fetch', 'v3', 'fetch'); + copyAsset('index.html', 'v3/fetch/index.html'); + copyAsset('main.ts', 'v3/fetch/main.ts'); compileWithTypescript('v3/fetch'); await server.start('v3/fetch'); await browser.start(); diff --git a/test/e2e/v3.node.spec.ts b/test/e2e/v3.node.spec.ts index 09672953..df1ce1fe 100644 --- a/test/e2e/v3.node.spec.ts +++ b/test/e2e/v3.node.spec.ts @@ -1,10 +1,12 @@ +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { generate } from './scripts/generate'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.node', () => { beforeAll(async () => { - await generate('v3/node', 'v3', 'node'); + cleanup('v3/node'); + await generateClient('v3/node', 'v3', 'node'); compileWithTypescript('v3/node'); await server.start('v3/node'); }, 30000); diff --git a/test/e2e/v3.xhr.spec.ts b/test/e2e/v3.xhr.spec.ts index 1062fd05..f06bac34 100644 --- a/test/e2e/v3.xhr.spec.ts +++ b/test/e2e/v3.xhr.spec.ts @@ -1,13 +1,16 @@ import browser from './scripts/browser'; +import { cleanup } from './scripts/cleanup'; import { compileWithTypescript } from './scripts/compileWithTypescript'; -import { copy } from './scripts/copy'; -import { generate } from './scripts/generate'; +import { copyAsset } from './scripts/copyAsset'; +import { generateClient } from './scripts/generateClient'; import server from './scripts/server'; describe('v3.xhr', () => { beforeAll(async () => { - await generate('v3/xhr', 'v3', 'xhr'); - await copy('v3/xhr'); + cleanup('v3/xhr'); + await generateClient('v3/xhr', 'v3', 'xhr'); + copyAsset('index.html', 'v3/xhr/index.html'); + copyAsset('main.ts', 'v3/xhr/main.ts'); compileWithTypescript('v3/xhr'); await server.start('v3/xhr'); await browser.start(); From 0f11ea84a9ca9867bb72700f468fa21d8085ce86 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Wed, 26 Jan 2022 20:16:04 +0100 Subject: [PATCH 2/2] - Added renamed files --- test/e2e/assets/index.html | 10 ++++++++++ test/e2e/assets/main.ts | 3 +++ test/e2e/assets/script.js | 3 --- test/e2e/scripts/cleanup.ts | 8 ++++++++ test/e2e/scripts/copy.ts | 5 ----- test/e2e/scripts/copyAsset.ts | 5 +++++ test/e2e/scripts/{generate.ts => generateClient.ts} | 4 ++-- 7 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 test/e2e/assets/index.html create mode 100644 test/e2e/assets/main.ts delete mode 100644 test/e2e/assets/script.js create mode 100644 test/e2e/scripts/cleanup.ts delete mode 100644 test/e2e/scripts/copy.ts create mode 100644 test/e2e/scripts/copyAsset.ts rename test/e2e/scripts/{generate.ts => generateClient.ts} (80%) diff --git a/test/e2e/assets/index.html b/test/e2e/assets/index.html new file mode 100644 index 00000000..ff6dd555 --- /dev/null +++ b/test/e2e/assets/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/e2e/assets/main.ts b/test/e2e/assets/main.ts new file mode 100644 index 00000000..4bbf7cd3 --- /dev/null +++ b/test/e2e/assets/main.ts @@ -0,0 +1,3 @@ +import * as api from './index'; + +(window as any).api = api; diff --git a/test/e2e/assets/script.js b/test/e2e/assets/script.js deleted file mode 100644 index 743714a1..00000000 --- a/test/e2e/assets/script.js +++ /dev/null @@ -1,3 +0,0 @@ -import('./index.js').then(module => { - window.api = module; -}); diff --git a/test/e2e/scripts/cleanup.ts b/test/e2e/scripts/cleanup.ts new file mode 100644 index 00000000..d3d9d896 --- /dev/null +++ b/test/e2e/scripts/cleanup.ts @@ -0,0 +1,8 @@ +import { rmSync } from 'fs'; + +export const cleanup = (dir: string) => { + rmSync(`./test/e2e/generated/${dir}/`, { + force: true, + recursive: true, + }); +}; diff --git a/test/e2e/scripts/copy.ts b/test/e2e/scripts/copy.ts deleted file mode 100644 index f1b73f2e..00000000 --- a/test/e2e/scripts/copy.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { copyFileSync } from 'fs'; - -export const copy = (dir: string) => { - copyFileSync('./test/e2e/assets/script.js', `./test/e2e/generated/${dir}/script.js`); -}; diff --git a/test/e2e/scripts/copyAsset.ts b/test/e2e/scripts/copyAsset.ts new file mode 100644 index 00000000..166aecc6 --- /dev/null +++ b/test/e2e/scripts/copyAsset.ts @@ -0,0 +1,5 @@ +import { copyFileSync } from 'fs'; + +export const copyAsset = (fileNameIn: string, fileNameOut: string) => { + copyFileSync(`./test/e2e/assets/${fileNameIn}`, `./test/e2e/generated/${fileNameOut}`); +}; diff --git a/test/e2e/scripts/generate.ts b/test/e2e/scripts/generateClient.ts similarity index 80% rename from test/e2e/scripts/generate.ts rename to test/e2e/scripts/generateClient.ts index 3e2794fd..9c040252 100644 --- a/test/e2e/scripts/generate.ts +++ b/test/e2e/scripts/generateClient.ts @@ -1,9 +1,9 @@ import { generate as __generate } from '../../../'; -export const generate = async ( +export const generateClient = async ( dir: string, version: string, - client: 'fetch' | 'xhr' | 'node' | 'axios', + client: 'fetch' | 'xhr' | 'node' | 'axios' | 'angular', useOptions: boolean = false, useUnionTypes: boolean = false, clientName?: string