From 75a648cc4e2e90cf78b26fcfe8b641797d82ad86 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Mon, 7 Feb 2022 18:16:11 +0100 Subject: [PATCH] - Added support for the Angular client with --name parameter --- CHANGELOG.md | 5 + jest.config.ts | 1 + src/index.ts | 5 - src/templates/client.hbs | 40 +++++++ src/templates/core/BaseHttpRequest.hbs | 21 ++++ src/templates/core/HttpRequest.hbs | 39 ++++++- src/templates/exportService.hbs | 21 +++- src/templates/index.hbs | 3 +- src/utils/writeClientClass.ts | 3 +- test/e2e/assets/main-angular-module.ts | 76 ++++++++++++ test/e2e/client.angular.spec.ts | 156 +++++++++++++++++++++++++ test/e2e/client.axios.spec.ts | 30 ++--- test/e2e/client.babel.spec.ts | 30 ++--- test/e2e/client.fetch.spec.ts | 30 ++--- test/e2e/client.node.spec.ts | 30 ++--- test/e2e/client.xhr.spec.ts | 30 ++--- test/index.js | 6 +- 17 files changed, 437 insertions(+), 89 deletions(-) create mode 100644 test/e2e/assets/main-angular-module.ts create mode 100644 test/e2e/client.angular.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a89e3a60..b2d1fe98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog All notable changes to this project will be documented in this file. +## [0.19.0] - 2022-02-02 +### Added +- Support for Angular client with `--name` option +- Added test cases for Angular client + ## [0.18.2] - 2022-02-02 ### Fixed - Updated dependencies diff --git a/jest.config.ts b/jest.config.ts index 55be5866..af04ee15 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -32,6 +32,7 @@ const config: Config.InitialOptions = { '/test/e2e/client.node.spec.ts', '/test/e2e/client.axios.spec.ts', '/test/e2e/client.babel.spec.ts', + '/test/e2e/client.angular.spec.ts', ], modulePathIgnorePatterns: ['/test/e2e/generated'], }, diff --git a/src/index.ts b/src/index.ts index 78bf0546..ef7a8b1b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import { parse as parseV2 } from './openApi/v2'; import { parse as parseV3 } from './openApi/v3'; import { getOpenApiSpec } from './utils/getOpenApiSpec'; import { getOpenApiVersion, OpenApiVersion } from './utils/getOpenApiVersion'; -import { isDefined } from './utils/isDefined'; import { isString } from './utils/isString'; import { postProcessClient } from './utils/postProcessClient'; import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates'; @@ -65,10 +64,6 @@ export const generate = async ({ request, write = true, }: Options): Promise => { - if (httpClient === HttpClient.ANGULAR && isDefined(clientName)) { - throw new Error('Angular client does not support --name property'); - } - const openApi = isString(input) ? await getOpenApiSpec(input) : input; const openApiVersion = getOpenApiVersion(openApi); const templates = registerHandlebarTemplates({ diff --git a/src/templates/client.hbs b/src/templates/client.hbs index 0408c593..601f27d8 100644 --- a/src/templates/client.hbs +++ b/src/templates/client.hbs @@ -1,8 +1,18 @@ {{>header}} +{{#equals @root.httpClient 'angular'}} +import { NgModule} from '@angular/core'; +import { HttpClientModule } from '@angular/common/http'; + +import { AngularHttpRequest } from './core/AngularHttpRequest'; +import { BaseHttpRequest } from './core/BaseHttpRequest'; +import type { OpenAPIConfig } from './core/OpenAPI'; +import { OpenAPI } from './core/OpenAPI'; +{{else}} import type { BaseHttpRequest } from './core/BaseHttpRequest'; import type { OpenAPIConfig } from './core/OpenAPI'; import { {{{httpRequest}}} } from './core/{{{httpRequest}}}'; +{{/equals}} {{#if services}} {{#each services}} @@ -10,6 +20,35 @@ import { {{{name}}}{{{@root.postfix}}} } from './services/{{{name}}}{{{@root.pos {{/each}} {{/if}} +{{#equals @root.httpClient 'angular'}} +@NgModule({ + imports: [HttpClientModule], + providers: [ + { + provide: OpenAPI, + useValue: { + BASE: OpenAPI?.BASE ?? '{{{server}}}', + VERSION: OpenAPI?.VERSION ?? '{{{version}}}', + WITH_CREDENTIALS: OpenAPI?.WITH_CREDENTIALS ?? false, + CREDENTIALS: OpenAPI?.CREDENTIALS ?? 'include', + TOKEN: OpenAPI?.TOKEN, + USERNAME: OpenAPI?.USERNAME, + PASSWORD: OpenAPI?.PASSWORD, + HEADERS: OpenAPI?.HEADERS, + ENCODE_PATH: OpenAPI?.ENCODE_PATH, + } as OpenAPIConfig, + }, + { + provide: BaseHttpRequest, + useClass: AngularHttpRequest, + }, + {{#each services}} + {{{name}}}{{{@root.postfix}}}, + {{/each}} + ] +}) +export class {{{clientName}}} {} +{{else}} type HttpRequestConstructor = new (config: OpenAPIConfig) => BaseHttpRequest; export class {{{clientName}}} { @@ -38,3 +77,4 @@ export class {{{clientName}}} { {{/each}} } } +{{/equals}} diff --git a/src/templates/core/BaseHttpRequest.hbs b/src/templates/core/BaseHttpRequest.hbs index 07ad17fe..032b9fff 100644 --- a/src/templates/core/BaseHttpRequest.hbs +++ b/src/templates/core/BaseHttpRequest.hbs @@ -1,14 +1,35 @@ {{>header}} +{{#equals @root.httpClient 'angular'}} +import type { HttpClient } from '@angular/common/http'; +import type { Observable } from 'rxjs'; + +import type { ApiRequestOptions } from './ApiRequestOptions'; +import type { OpenAPIConfig } from './OpenAPI'; +{{else}} import type { ApiRequestOptions } from './ApiRequestOptions'; import type { CancelablePromise } from './CancelablePromise'; import type { OpenAPIConfig } from './OpenAPI'; +{{/equals}} export class BaseHttpRequest { + {{#equals @root.httpClient 'angular'}} + constructor( + public readonly config: OpenAPIConfig, + public readonly http: HttpClient, + ) {} + {{else}} constructor(public readonly config: OpenAPIConfig) {} + {{/equals}} + {{#equals @root.httpClient 'angular'}} + public request(options: ApiRequestOptions): Observable { + throw new Error('Not Implemented'); + } + {{else}} public request(options: ApiRequestOptions): CancelablePromise { throw new Error('Not Implemented'); } + {{/equals}} } diff --git a/src/templates/core/HttpRequest.hbs b/src/templates/core/HttpRequest.hbs index 3a3a3308..157333eb 100644 --- a/src/templates/core/HttpRequest.hbs +++ b/src/templates/core/HttpRequest.hbs @@ -1,24 +1,61 @@ {{>header}} +{{#equals @root.httpClient 'angular'}} +import { HttpClient } from '@angular/common/http'; +import { Inject, Injectable } from '@angular/core'; +import type { Observable } from 'rxjs'; + +import type { ApiRequestOptions } from './ApiRequestOptions'; +import { BaseHttpRequest } from './BaseHttpRequest'; +import type { OpenAPIConfig } from './OpenAPI'; +import { OpenAPI } from './OpenAPI'; +import { request as __request } from './request'; +{{else}} import type { ApiRequestOptions } from './ApiRequestOptions'; import { BaseHttpRequest } from './BaseHttpRequest'; import type { CancelablePromise } from './CancelablePromise'; import type { OpenAPIConfig } from './OpenAPI'; import { request as __request } from './request'; +{{/equals}} +{{#equals @root.httpClient 'angular'}} +@Injectable() +{{/equals}} export class {{httpRequest}} extends BaseHttpRequest { + {{#equals @root.httpClient 'angular'}} + constructor( + @Inject(OpenAPI) + config: OpenAPIConfig, + http: HttpClient, + ) { + super(config, http); + } + {{else}} constructor(config: OpenAPIConfig) { super(config); } + {{/equals}} + {{#equals @root.httpClient 'angular'}} + /** + * Request method + * @param options The request options from the service + * @returns Observable + * @throws ApiError + */ + public override request(options: ApiRequestOptions): Observable { + return __request(this.config, this.http, options); + } + {{else}} /** * Request method * @param options The request options from the service * @returns CancelablePromise * @throws ApiError */ - public request(options: ApiRequestOptions): CancelablePromise { + public override request(options: ApiRequestOptions): CancelablePromise { return __request(this.config, options); } + {{/equals}} } diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index 7cbaa75d..2fdd9af5 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -1,9 +1,14 @@ {{>header}} {{#equals @root.httpClient 'angular'}} +{{#if @root.exportClient}} +import { Injectable } from '@angular/core'; +import type { Observable } from 'rxjs'; +{{else}} import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import type { Observable } from 'rxjs'; +{{/if}} {{/equals}} {{#if imports}} @@ -16,7 +21,11 @@ import type { {{{this}}} } from '../models/{{{this}}}'; import type { CancelablePromise } from '../core/CancelablePromise'; {{/notEquals}} {{#if @root.exportClient}} +{{#equals @root.httpClient 'angular'}} +import { BaseHttpRequest } from '../core/BaseHttpRequest'; +{{else}} import type { BaseHttpRequest } from '../core/BaseHttpRequest'; +{{/equals}} {{else}} import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; @@ -28,12 +37,13 @@ import { request as __request } from '../core/request'; export class {{{name}}}{{{@root.postfix}}} { {{#if @root.exportClient}} - constructor(private readonly httpRequest: BaseHttpRequest) {} - {{/if}} + constructor(public readonly httpRequest: BaseHttpRequest) {} + {{else}} {{#equals @root.httpClient 'angular'}} - constructor(private readonly http: HttpClient) {} + constructor(public readonly http: HttpClient) {} {{/equals}} + {{/if}} {{#each operations}} /** @@ -59,8 +69,13 @@ export class {{{name}}}{{{@root.postfix}}} { * @throws ApiError */ {{#if @root.exportClient}} + {{#equals @root.httpClient 'angular'}} + public {{{name}}}({{>parameters}}): Observable<{{>result}}> { + return this.httpRequest.request({ + {{else}} public {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> { return this.httpRequest.request({ + {{/equals}} {{else}} {{#equals @root.httpClient 'angular'}} public {{{name}}}({{>parameters}}): Observable<{{>result}}> { diff --git a/src/templates/index.hbs b/src/templates/index.hbs index 855d0c63..72544d7d 100644 --- a/src/templates/index.hbs +++ b/src/templates/index.hbs @@ -1,7 +1,8 @@ {{>header}} {{#if @root.exportClient}} -export { {{{clientName}}} } from './client'; +export { {{{clientName}}} } from './{{{clientName}}}'; + {{/if}} {{#if @root.exportCore}} export { ApiError } from './core/ApiError'; diff --git a/src/utils/writeClientClass.ts b/src/utils/writeClientClass.ts index 549a5c5b..558150bb 100644 --- a/src/utils/writeClientClass.ts +++ b/src/utils/writeClientClass.ts @@ -34,6 +34,7 @@ export const writeClientClass = async ( ): Promise => { const templateResult = templates.client({ clientName, + httpClient, postfix, server: client.server, version: client.version, @@ -42,5 +43,5 @@ export const writeClientClass = async ( httpRequest: getHttpRequestName(httpClient), }); - await writeFile(resolve(outputPath, 'client.ts'), i(f(templateResult), indent)); + await writeFile(resolve(outputPath, `${clientName}.ts`), i(f(templateResult), indent)); }; diff --git a/test/e2e/assets/main-angular-module.ts b/test/e2e/assets/main-angular-module.ts new file mode 100644 index 00000000..f6529550 --- /dev/null +++ b/test/e2e/assets/main-angular-module.ts @@ -0,0 +1,76 @@ +import { HttpClientModule } from '@angular/common/http'; +import { Component, NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { ApiModule } from './ApiModule'; +import { OpenAPI } from './core/OpenAPI'; +import { CollectionFormatService } from './services/CollectionFormatService'; +import { ComplexService } from './services/ComplexService'; +import { DefaultService } from './services/DefaultService'; +import { DefaultsService } from './services/DefaultsService'; +import { DuplicateService } from './services/DuplicateService'; +import { ErrorService } from './services/ErrorService'; +import { HeaderService } from './services/HeaderService'; +import { MultipleTags1Service } from './services/MultipleTags1Service'; +import { MultipleTags2Service } from './services/MultipleTags2Service'; +import { MultipleTags3Service } from './services/MultipleTags3Service'; +import { NoContentService } from './services/NoContentService'; +import { ParametersService } from './services/ParametersService'; +import { ResponseService } from './services/ResponseService'; +import { SimpleService } from './services/SimpleService'; +import { TypesService } from './services/TypesService'; + +@Component({ + selector: 'app-root', + template: `
Angular is ready
`, +}) +export class AppComponent { + constructor( + private readonly collectionFormatService: CollectionFormatService, + 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 = { + OpenAPI, + ApiModule, + CollectionFormatService: this.collectionFormatService, + ComplexService: this.complexService, + DefaultService: this.defaultService, + DefaultsService: this.defaultsService, + DuplicateService: this.duplicateService, + ErrorService: this.errorService, + HeaderService: this.headerService, + MultipleTags1Service: this.multipleTags1Service, + MultipleTags2Service: this.multipleTags2Service, + MultipleTags3Service: this.multipleTags3Service, + NoContentService: this.noContentService, + ParametersService: this.parametersService, + ResponseService: this.responseService, + SimpleService: this.simpleService, + TypesService: this.typesService, + }; + } +} + +@NgModule({ + imports: [BrowserModule, HttpClientModule, ApiModule], + bootstrap: [AppComponent], +}) +export class AppModule {} + +platformBrowserDynamic() + .bootstrapModule(AppModule) + .catch(err => console.error(err)); diff --git a/test/e2e/client.angular.spec.ts b/test/e2e/client.angular.spec.ts new file mode 100644 index 00000000..ef8deee8 --- /dev/null +++ b/test/e2e/client.angular.spec.ts @@ -0,0 +1,156 @@ +import browser from './scripts/browser'; +import { buildAngularProject } from './scripts/buildAngularProject'; +import { cleanup } from './scripts/cleanup'; +import { copyAsset } from './scripts/copyAsset'; +import { createAngularProject } from './scripts/createAngularProject'; +import { generateClient } from './scripts/generateClient'; +import server from './scripts/server'; + +describe('client.angular', () => { + beforeAll(async () => { + cleanup('client/angular'); + createAngularProject('client/angular', 'app'); + await generateClient('client/angular/app/src', 'v3', 'angular', false, false, 'ApiModule'); + copyAsset('main-angular-module.ts', 'client/angular/app/src/main.ts'); + buildAngularProject('client/angular', 'app', 'dist'); + await server.start('client/angular/app/dist'); + await browser.start(); + }, 30000); + + afterAll(async () => { + await browser.stop(); + await server.stop(); + }); + + it('requests token', async () => { + await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN')); + const result = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { SimpleService } = (window as any).api; + SimpleService.httpRequest.config.TOKEN = (window as any).tokenRequest; + SimpleService.httpRequest.config.USERNAME = undefined; + SimpleService.httpRequest.config.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 { SimpleService } = (window as any).api; + SimpleService.httpRequest.config.TOKEN = undefined; + SimpleService.httpRequest.config.USERNAME = 'username'; + SimpleService.httpRequest.config.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(); + }); + + it('should throw known error (500)', async () => { + const error = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { ErrorService } = (window as any).api; + ErrorService.testErrorCode(500).subscribe({ + error: (e: any) => { + resolve( + JSON.stringify({ + name: e.name, + message: e.message, + url: e.url, + status: e.status, + statusText: e.statusText, + body: e.body, + }) + ); + }, + }); + }); + }); + expect(error).toBe( + JSON.stringify({ + name: 'ApiError', + message: 'Custom message: Internal Server Error', + url: 'http://localhost:3000/base/api/v1.0/error?status=500', + status: 500, + statusText: 'Internal Server Error', + body: { + status: 500, + message: 'hello world', + }, + }) + ); + }); + + it('should throw unknown error (409)', async () => { + const error = await browser.evaluate(async () => { + return await new Promise(resolve => { + const { ErrorService } = (window as any).api; + ErrorService.testErrorCode(409).subscribe({ + error: (e: any) => { + resolve( + JSON.stringify({ + name: e.name, + message: e.message, + url: e.url, + status: e.status, + statusText: e.statusText, + body: e.body, + }) + ); + }, + }); + }); + }); + expect(error).toBe( + JSON.stringify({ + name: 'ApiError', + message: 'Generic Error', + url: 'http://localhost:3000/base/api/v1.0/error?status=409', + status: 409, + statusText: 'Conflict', + body: { + status: 409, + message: 'hello world', + }, + }) + ); + }); +}); diff --git a/test/e2e/client.axios.spec.ts b/test/e2e/client.axios.spec.ts index 0cf3fab4..ed8952b6 100644 --- a/test/e2e/client.axios.spec.ts +++ b/test/e2e/client.axios.spec.ts @@ -6,7 +6,7 @@ import server from './scripts/server'; describe('client.axios', () => { beforeAll(async () => { cleanup('client/axios'); - await generateClient('client/axios', 'v3', 'axios', false, false, 'AppClient'); + await generateClient('client/axios', 'v3', 'axios', false, false, 'ApiClient'); compileWithTypescript('client/axios'); await server.start('client/axios'); }, 30000); @@ -16,9 +16,9 @@ describe('client.axios', () => { }); it('requests token', async () => { - const { AppClient } = require('./generated/client/axios/index.js'); + const { ApiClient } = require('./generated/client/axios/index.js'); const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN'); - const client = new AppClient({ + const client = new ApiClient({ TOKEN: tokenRequest, USERNAME: undefined, PASSWORD: undefined, @@ -29,8 +29,8 @@ describe('client.axios', () => { }); it('uses credentials', async () => { - const { AppClient } = require('./generated/client/axios/index.js'); - const client = new AppClient({ + const { ApiClient } = require('./generated/client/axios/index.js'); + const client = new ApiClient({ TOKEN: undefined, USERNAME: 'username', PASSWORD: 'password', @@ -40,8 +40,8 @@ describe('client.axios', () => { }); it('supports complex params', async () => { - const { AppClient } = require('./generated/client/axios/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/axios/index.js'); + const client = new ApiClient(); const result = await client.complex.complexTypes({ first: { second: { @@ -53,8 +53,8 @@ describe('client.axios', () => { }); it('supports form data', async () => { - const { AppClient } = require('./generated/client/axios/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/axios/index.js'); + const client = new ApiClient(); const result = await client.parameters.callWithParameters( 'valueHeader', 'valueQuery', @@ -71,8 +71,8 @@ describe('client.axios', () => { it('can abort the request', async () => { let error; try { - const { AppClient } = require('./generated/client/axios/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/axios/index.js'); + const client = new ApiClient(); const promise = client.simple.getCallWithoutParametersAndResponse(); setTimeout(() => { promise.cancel(); @@ -87,8 +87,8 @@ describe('client.axios', () => { it('should throw known error (500)', async () => { let error; try { - const { AppClient } = require('./generated/client/axios/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/axios/index.js'); + const client = new ApiClient(); await client.error.testErrorCode(500); } catch (e) { const err = e as any; @@ -119,8 +119,8 @@ describe('client.axios', () => { it('should throw unknown error (409)', async () => { let error; try { - const { AppClient } = require('./generated/client/axios/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/axios/index.js'); + const client = new ApiClient(); await client.error.testErrorCode(409); } catch (e) { const err = e as any; diff --git a/test/e2e/client.babel.spec.ts b/test/e2e/client.babel.spec.ts index 38f912cd..7fe79d0b 100644 --- a/test/e2e/client.babel.spec.ts +++ b/test/e2e/client.babel.spec.ts @@ -8,7 +8,7 @@ import server from './scripts/server'; describe('client.babel', () => { beforeAll(async () => { cleanup('client/babel'); - await generateClient('client/babel', 'v3', 'fetch', true, true, 'AppClient'); + await generateClient('client/babel', 'v3', 'fetch', true, true, 'ApiClient'); copyAsset('index.html', 'client/babel/index.html'); copyAsset('main.ts', 'client/babel/main.ts'); compileWithBabel('client/babel'); @@ -24,8 +24,8 @@ describe('client.babel', () => { it('requests token', async () => { await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN')); const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient({ + const { ApiClient } = (window as any).api; + const client = new ApiClient({ TOKEN: (window as any).tokenRequest, USERNAME: undefined, PASSWORD: undefined, @@ -37,8 +37,8 @@ describe('client.babel', () => { it('uses credentials', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient({ + const { ApiClient } = (window as any).api; + const client = new ApiClient({ TOKEN: undefined, USERNAME: 'username', PASSWORD: 'password', @@ -50,8 +50,8 @@ describe('client.babel', () => { it('supports complex params', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); return await client.complex.complexTypes({ parameterObject: { first: { @@ -67,8 +67,8 @@ describe('client.babel', () => { it('support form data', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); return await client.parameters.callWithParameters({ parameterHeader: 'valueHeader', parameterQuery: 'valueQuery', @@ -87,8 +87,8 @@ describe('client.babel', () => { let error; try { await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); const promise = client.simple.getCallWithoutParametersAndResponse(); setTimeout(() => { promise.cancel(); @@ -104,8 +104,8 @@ describe('client.babel', () => { it('should throw known error (500)', async () => { const error = await browser.evaluate(async () => { try { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); await client.error.testErrorCode({ status: 500, }); @@ -140,8 +140,8 @@ describe('client.babel', () => { it('should throw unknown error (409)', async () => { const error = await browser.evaluate(async () => { try { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); await client.error.testErrorCode({ status: 409, }); diff --git a/test/e2e/client.fetch.spec.ts b/test/e2e/client.fetch.spec.ts index 7794de79..3a184438 100644 --- a/test/e2e/client.fetch.spec.ts +++ b/test/e2e/client.fetch.spec.ts @@ -8,7 +8,7 @@ import server from './scripts/server'; describe('client.fetch', () => { beforeAll(async () => { cleanup('client/fetch'); - await generateClient('client/fetch', 'v3', 'fetch', false, false, 'AppClient'); + await generateClient('client/fetch', 'v3', 'fetch', false, false, 'ApiClient'); copyAsset('index.html', 'client/fetch/index.html'); copyAsset('main.ts', 'client/fetch/main.ts'); compileWithTypescript('client/fetch'); @@ -24,8 +24,8 @@ describe('client.fetch', () => { it('requests token', async () => { await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN')); const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient({ + const { ApiClient } = (window as any).api; + const client = new ApiClient({ TOKEN: (window as any).tokenRequest, USERNAME: undefined, PASSWORD: undefined, @@ -37,8 +37,8 @@ describe('client.fetch', () => { it('uses credentials', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient({ + const { ApiClient } = (window as any).api; + const client = new ApiClient({ TOKEN: undefined, USERNAME: 'username', PASSWORD: 'password', @@ -50,8 +50,8 @@ describe('client.fetch', () => { it('supports complex params', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); return await client.complex.complexTypes({ first: { second: { @@ -65,8 +65,8 @@ describe('client.fetch', () => { it('support form data', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); return await client.parameters.callWithParameters( 'valueHeader', 'valueQuery', @@ -85,8 +85,8 @@ describe('client.fetch', () => { let error; try { await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); const promise = client.simple.getCallWithoutParametersAndResponse(); setTimeout(() => { promise.cancel(); @@ -102,8 +102,8 @@ describe('client.fetch', () => { it('should throw known error (500)', async () => { const error = await browser.evaluate(async () => { try { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); await client.error.testErrorCode(500); } catch (e) { const error = e as any; @@ -137,8 +137,8 @@ describe('client.fetch', () => { it('should throw unknown error (409)', async () => { const error = await browser.evaluate(async () => { try { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); await client.error.testErrorCode(409); } catch (e) { const error = e as any; diff --git a/test/e2e/client.node.spec.ts b/test/e2e/client.node.spec.ts index 3e01a532..9967cf01 100644 --- a/test/e2e/client.node.spec.ts +++ b/test/e2e/client.node.spec.ts @@ -6,7 +6,7 @@ import server from './scripts/server'; describe('client.node', () => { beforeAll(async () => { cleanup('client/node'); - await generateClient('client/node', 'v3', 'node', false, false, 'AppClient'); + await generateClient('client/node', 'v3', 'node', false, false, 'ApiClient'); compileWithTypescript('client/node'); await server.start('client/node'); }, 30000); @@ -16,9 +16,9 @@ describe('client.node', () => { }); it('requests token', async () => { - const { AppClient } = require('./generated/client/node/index.js'); + const { ApiClient } = require('./generated/client/node/index.js'); const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN'); - const client = new AppClient({ + const client = new ApiClient({ TOKEN: tokenRequest, USERNAME: undefined, PASSWORD: undefined, @@ -29,8 +29,8 @@ describe('client.node', () => { }); it('uses credentials', async () => { - const { AppClient } = require('./generated/client/node/index.js'); - const client = new AppClient({ + const { ApiClient } = require('./generated/client/node/index.js'); + const client = new ApiClient({ TOKEN: undefined, USERNAME: 'username', PASSWORD: 'password', @@ -40,8 +40,8 @@ describe('client.node', () => { }); it('supports complex params', async () => { - const { AppClient } = require('./generated/client/node/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/node/index.js'); + const client = new ApiClient(); const result = await client.complex.complexTypes({ first: { second: { @@ -53,8 +53,8 @@ describe('client.node', () => { }); it('support form data', async () => { - const { AppClient } = require('./generated/client/node/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/node/index.js'); + const client = new ApiClient(); const result = await client.parameters.callWithParameters( 'valueHeader', 'valueQuery', @@ -71,8 +71,8 @@ describe('client.node', () => { it('can abort the request', async () => { let error; try { - const { AppClient } = require('./generated/client/node/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/node/index.js'); + const client = new ApiClient(); const promise = client.simple.getCallWithoutParametersAndResponse(); setTimeout(() => { promise.cancel(); @@ -87,8 +87,8 @@ describe('client.node', () => { it('should throw known error (500)', async () => { let error; try { - const { AppClient } = require('./generated/client/node/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/node/index.js'); + const client = new ApiClient(); await client.error.testErrorCode(500); } catch (e) { const err = e as any; @@ -119,8 +119,8 @@ describe('client.node', () => { it('should throw unknown error (409)', async () => { let error; try { - const { AppClient } = require('./generated/client/node/index.js'); - const client = new AppClient(); + const { ApiClient } = require('./generated/client/node/index.js'); + const client = new ApiClient(); await client.error.testErrorCode(409); } catch (e) { const err = e as any; diff --git a/test/e2e/client.xhr.spec.ts b/test/e2e/client.xhr.spec.ts index 49d61668..487fb014 100644 --- a/test/e2e/client.xhr.spec.ts +++ b/test/e2e/client.xhr.spec.ts @@ -8,7 +8,7 @@ import server from './scripts/server'; describe('client.xhr', () => { beforeAll(async () => { cleanup('client/xhr'); - await generateClient('client/xhr', 'v3', 'xhr', false, false, 'AppClient'); + await generateClient('client/xhr', 'v3', 'xhr', false, false, 'ApiClient'); copyAsset('index.html', 'client/xhr/index.html'); copyAsset('main.ts', 'client/xhr/main.ts'); compileWithTypescript('client/xhr'); @@ -24,8 +24,8 @@ describe('client.xhr', () => { it('requests token', async () => { await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN')); const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient({ + const { ApiClient } = (window as any).api; + const client = new ApiClient({ TOKEN: (window as any).tokenRequest, USERNAME: undefined, PASSWORD: undefined, @@ -37,8 +37,8 @@ describe('client.xhr', () => { it('uses credentials', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient({ + const { ApiClient } = (window as any).api; + const client = new ApiClient({ TOKEN: undefined, USERNAME: 'username', PASSWORD: 'password', @@ -50,8 +50,8 @@ describe('client.xhr', () => { it('supports complex params', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); return await client.complex.complexTypes({ first: { second: { @@ -65,8 +65,8 @@ describe('client.xhr', () => { it('support form data', async () => { const result = await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); return await client.parameters.callWithParameters( 'valueHeader', 'valueQuery', @@ -85,8 +85,8 @@ describe('client.xhr', () => { let error; try { await browser.evaluate(async () => { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); const promise = client.simple.getCallWithoutParametersAndResponse(); setTimeout(() => { promise.cancel(); @@ -102,8 +102,8 @@ describe('client.xhr', () => { it('should throw known error (500)', async () => { const error = await browser.evaluate(async () => { try { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); await client.error.testErrorCode(500); } catch (e) { const error = e as any; @@ -136,8 +136,8 @@ describe('client.xhr', () => { it('should throw unknown error (409)', async () => { const error = await browser.evaluate(async () => { try { - const { AppClient } = (window as any).api; - const client = new AppClient(); + const { ApiClient } = (window as any).api; + const client = new ApiClient(); await client.error.testErrorCode(409); } catch (e) { const error = e as any; diff --git a/test/index.js b/test/index.js index 2d4bc491..6d276c41 100644 --- a/test/index.js +++ b/test/index.js @@ -8,15 +8,15 @@ const generate = async (input, output) => { input, output, httpClient: OpenAPI.HttpClient.FETCH, - useOptions: false, + useOptions: true, useUnionTypes: false, exportCore: true, exportSchemas: true, exportModels: true, exportServices: true, - // clientName: 'DemoAppClient', + // clientName: 'Demo', // indent: OpenAPI.Indent.SPACE_2, - // postfix: 'Api', + // postfix: 'Service', // request: './test/custom/request.ts', }); };