- Added support for the Angular client with --name parameter

This commit is contained in:
Ferdi Koomen 2022-02-07 18:16:11 +01:00
parent ca1c2a7353
commit 75a648cc4e
17 changed files with 437 additions and 89 deletions

View File

@ -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

View File

@ -32,6 +32,7 @@ const config: Config.InitialOptions = {
'<rootDir>/test/e2e/client.node.spec.ts',
'<rootDir>/test/e2e/client.axios.spec.ts',
'<rootDir>/test/e2e/client.babel.spec.ts',
'<rootDir>/test/e2e/client.angular.spec.ts',
],
modulePathIgnorePatterns: ['<rootDir>/test/e2e/generated'],
},

View File

@ -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<void> => {
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({

View File

@ -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}}

View File

@ -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<T>(options: ApiRequestOptions): Observable<T> {
throw new Error('Not Implemented');
}
{{else}}
public request<T>(options: ApiRequestOptions): CancelablePromise<T> {
throw new Error('Not Implemented');
}
{{/equals}}
}

View File

@ -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<T>
* @throws ApiError
*/
public override request<T>(options: ApiRequestOptions): Observable<T> {
return __request(this.config, this.http, options);
}
{{else}}
/**
* Request method
* @param options The request options from the service
* @returns CancelablePromise<T>
* @throws ApiError
*/
public request<T>(options: ApiRequestOptions): CancelablePromise<T> {
public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
return __request(this.config, options);
}
{{/equals}}
}

View File

@ -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}}> {

View File

@ -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';

View File

@ -34,6 +34,7 @@ export const writeClientClass = async (
): Promise<void> => {
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));
};

View File

@ -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: `<div>Angular is ready</div>`,
})
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));

View File

@ -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<any>(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<any>(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<any>(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<any>(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<any>(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<any>(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',
},
})
);
});
});

View File

@ -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;

View File

@ -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,
});

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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',
});
};