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 0b4f9ebb..ac9bac5a 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -33,6 +33,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/angular/getHeaders.hbs b/src/templates/core/angular/getHeaders.hbs index f9f6b8a2..b0e395c7 100644 --- a/src/templates/core/angular/getHeaders.hbs +++ b/src/templates/core/angular/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 HttpHeaders(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 HttpHeaders(headers); }; diff --git a/src/templates/core/fetch/getHeaders.hbs b/src/templates/core/fetch/getHeaders.hbs index 009b3a84..bad34b1b 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,28 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); + const headers = 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/node/getHeaders.hbs b/src/templates/core/node/getHeaders.hbs index 10f61278..7d744575 100644 --- a/src/templates/core/node/getHeaders.hbs +++ b/src/templates/core/node/getHeaders.hbs @@ -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/xhr/getHeaders.hbs b/src/templates/core/xhr/getHeaders.hbs index 16a611bc..0ac8b443 100644 --- a/src/templates/core/xhr/getHeaders.hbs +++ b/src/templates/core/xhr/getHeaders.hbs @@ -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/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index 22ac736b..0556152f 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,30 +376,30 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); + const headers = 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): any => { @@ -3268,7 +3268,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, @@ -3279,30 +3279,30 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr [key]: String(value), }), {} as Record); - const headers = new Headers(defaultHeaders); + const headers = 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): any => { diff --git a/test/e2e/assets/index.html b/test/e2e/assets/index.html index fbd6675e..ff6dd555 100644 --- a/test/e2e/assets/index.html +++ b/test/e2e/assets/index.html @@ -1,10 +1,10 @@ - - - - - - - - + + + + + + + + diff --git a/test/e2e/assets/main-angular.ts b/test/e2e/assets/main-angular.ts index d9d32fac..fff6ad67 100644 --- a/test/e2e/assets/main-angular.ts +++ b/test/e2e/assets/main-angular.ts @@ -3,6 +3,7 @@ import { Component, NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; +import { OpenAPI } from './core/OpenAPI'; import { CollectionFormatService } from './services/CollectionFormatService'; import { ComplexService } from './services/ComplexService'; import { DefaultService } from './services/DefaultService'; @@ -21,42 +22,43 @@ import { TypesService } from './services/TypesService'; @Component({ selector: 'app-root', - template: `
Angular
`, + template: `
Angular is ready
`, }) export class AppComponent { constructor( private readonly collectionFormatService: CollectionFormatService, - private readonly complexServiceService: ComplexService, - private readonly defaultServiceService: DefaultService, - private readonly defaultsServiceService: DefaultsService, - private readonly duplicateServiceService: DuplicateService, - private readonly errorServiceService: ErrorService, - private readonly headerServiceService: HeaderService, - private readonly multipleTags1ServiceService: MultipleTags1Service, - private readonly multipleTags2ServiceService: MultipleTags2Service, - private readonly multipleTags3ServiceService: MultipleTags3Service, - private readonly noContentServiceService: NoContentService, - private readonly parametersServiceService: ParametersService, - private readonly responseServiceService: ResponseService, - private readonly simpleServiceService: SimpleService, - private readonly typesServiceService: TypesService + private readonly complexService: ComplexService, + private readonly defaultService: DefaultService, + private readonly defaultsService: DefaultsService, + private readonly duplicateService: DuplicateService, + private readonly errorService: ErrorService, + private readonly headerService: HeaderService, + private readonly multipleTags1Service: MultipleTags1Service, + private readonly multipleTags2Service: MultipleTags2Service, + private readonly multipleTags3Service: MultipleTags3Service, + private readonly noContentService: NoContentService, + private readonly parametersService: ParametersService, + private readonly responseService: ResponseService, + private readonly simpleService: SimpleService, + private readonly typesService: TypesService ) { (window as any).api = { - collectionFormatService, - complexServiceService, - defaultServiceService, - defaultsServiceService, - duplicateServiceService, - errorServiceService, - headerServiceService, - multipleTags1ServiceService, - multipleTags2ServiceService, - multipleTags3ServiceService, - noContentServiceService, - parametersServiceService, - responseServiceService, - simpleServiceService, - typesServiceService, + OpenAPI, + CollectionFormatService: collectionFormatService, + ComplexService: complexService, + DefaultService: defaultService, + DefaultsService: defaultsService, + DuplicateService: duplicateService, + ErrorService: errorService, + HeaderService: headerService, + MultipleTags1Service: multipleTags1Service, + MultipleTags2Service: multipleTags2Service, + MultipleTags3Service: multipleTags3Service, + NoContentService: noContentService, + ParametersService: parametersService, + ResponseService: responseService, + SimpleService: simpleService, + TypesService: typesService, }; } } diff --git a/test/e2e/v2.angular.spec.ts b/test/e2e/v2.angular.spec.ts index 7aa342d0..2f4d396b 100644 --- a/test/e2e/v2.angular.spec.ts +++ b/test/e2e/v2.angular.spec.ts @@ -25,6 +25,30 @@ describe('v2.angular', () => { }); it('requests token', async () => { - expect(true).toBe(true); + await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN')); + const result = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { OpenAPI, SimpleService } = (window as any).api; + OpenAPI.TOKEN = (window as any).tokenRequest; + SimpleService.getCallWithoutParametersAndResponse().subscribe(resolve); + }); + }); + expect(result.headers.authorization).toBe('Bearer MY_TOKEN'); + }); + + it('supports complex params', async () => { + const result = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { ComplexService } = (window as any).api; + ComplexService.complexTypes({ + first: { + second: { + third: 'Hello World!', + }, + }, + }).subscribe(resolve); + }); + }); + expect(result).toBeDefined(); }); }); diff --git a/test/e2e/v3.angular.spec.ts b/test/e2e/v3.angular.spec.ts index afd76fd1..0951720a 100644 --- a/test/e2e/v3.angular.spec.ts +++ b/test/e2e/v3.angular.spec.ts @@ -23,6 +23,64 @@ describe('v3.angular', () => { }); it('requests token', async () => { - expect(true).toBe(true); + await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN')); + const result = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { OpenAPI, SimpleService } = (window as any).api; + OpenAPI.TOKEN = (window as any).tokenRequest; + OpenAPI.USERNAME = undefined; + OpenAPI.PASSWORD = undefined; + SimpleService.getCallWithoutParametersAndResponse().subscribe(resolve); + }); + }); + expect(result.headers.authorization).toBe('Bearer MY_TOKEN'); + }); + + it('uses credentials', async () => { + const result = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { OpenAPI, SimpleService } = (window as any).api; + OpenAPI.TOKEN = undefined; + OpenAPI.USERNAME = 'username'; + OpenAPI.PASSWORD = 'password'; + SimpleService.getCallWithoutParametersAndResponse().subscribe(resolve); + }); + }); + expect(result.headers.authorization).toBe('Basic dXNlcm5hbWU6cGFzc3dvcmQ='); + }); + + it('supports complex params', async () => { + const result = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { ComplexService } = (window as any).api; + ComplexService.complexTypes({ + first: { + second: { + third: 'Hello World!', + }, + }, + }).subscribe(resolve); + }); + }); + expect(result).toBeDefined(); + }); + + it('support form data', async () => { + const result = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { ParametersService } = (window as any).api; + ParametersService.callWithParameters( + 'valueHeader', + 'valueQuery', + 'valueForm', + 'valueCookie', + 'valuePath', + { + prop: 'valueBody', + } + ).subscribe(resolve); + }); + }); + expect(result).toBeDefined(); }); });