Skip to content

Commit 1d8dc54

Browse files
committed
- Added WIP Angular client
1 parent 9424a7b commit 1d8dc54

20 files changed

+87
-92
lines changed

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { parse as parseV2 } from './openApi/v2';
44
import { parse as parseV3 } from './openApi/v3';
55
import { getOpenApiSpec } from './utils/getOpenApiSpec';
66
import { getOpenApiVersion, OpenApiVersion } from './utils/getOpenApiVersion';
7+
import { isDefined } from './utils/isDefined';
78
import { isString } from './utils/isString';
89
import { postProcessClient } from './utils/postProcessClient';
910
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
@@ -64,6 +65,10 @@ export const generate = async ({
6465
request,
6566
write = true,
6667
}: Options): Promise<void> => {
68+
if (httpClient === HttpClient.ANGULAR && isDefined(clientName)) {
69+
throw new Error('Angular client does not support --name property');
70+
}
71+
6772
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
6873
const openApiVersion = getOpenApiVersion(openApi);
6974
const templates = registerHandlebarTemplates({

src/templates/core/angular/getHeaders.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
1+
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<HttpHeaders> => {
22
const token = await resolve(options, config.TOKEN);
33
const username = await resolve(options, config.USERNAME);
44
const password = await resolve(options, config.PASSWORD);
@@ -15,7 +15,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
1515
[key]: String(value),
1616
}), {} as Record<string, string>);
1717

18-
const headers = new Headers(defaultHeaders);
18+
const headers = new HttpHeaders(defaultHeaders);
1919

2020
if (isStringWithValue(token)) {
2121
headers.append('Authorization', `Bearer ${token}`);

src/templates/core/angular/getRequestBody.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const getRequestBody = (options: ApiRequestOptions): BodyInit | undefined => {
1+
const getRequestBody = (options: ApiRequestOptions): any => {
22
if (options.body) {
33
if (options.mediaType?.includes('/json')) {
44
return JSON.stringify(options.body)
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
const getResponseBody = async (response: Response): Promise<any> => {
2-
if (response.status !== 204) {
3-
try {
4-
const contentType = response.headers.get('Content-Type');
5-
if (contentType) {
6-
const isJSON = contentType.toLowerCase().startsWith('application/json');
7-
if (isJSON) {
8-
return await response.json();
9-
} else {
10-
return await response.text();
11-
}
12-
}
13-
} catch (error) {
14-
console.error(error);
15-
}
1+
const getResponseBody = <T>(response: HttpResponse<T>): T | undefined => {
2+
if (response.status !== 204 && response.body !== null) {
3+
return response.body;
164
}
175
return;
186
};

src/templates/core/angular/getResponseHeader.hbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
1+
const getResponseHeader = <T>(response: HttpResponse<T>, responseHeader?: string): string | undefined => {
22
if (responseHeader) {
3-
const content = response.headers.get(responseHeader);
4-
if (isString(content)) {
5-
return content;
3+
const value = response.headers.get(responseHeader);
4+
if (isString(value)) {
5+
return value;
66
}
77
}
88
return;

src/templates/core/angular/request.hbs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
{{>header}}
22

3-
import { HttpClient, HttpHeaders } from '@angular/common/http';
3+
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
44
import { Observable } from 'rxjs';
55

66
import { ApiError } from './ApiError';
77
import type { ApiRequestOptions } from './ApiRequestOptions';
88
import type { ApiResult } from './ApiResult';
9-
import { CancelablePromise } from './CancelablePromise';
10-
import type { OnCancel } from './CancelablePromise';
119
import type { OpenAPIConfig } from './OpenAPI';
1210

1311
{{>functions/isDefined}}
@@ -61,37 +59,40 @@ import type { OpenAPIConfig } from './OpenAPI';
6159
/**
6260
* Request method
6361
* @param config The OpenAPI configuration object
62+
* @param http The Angular HTTP client
6463
* @param options The request options from the service
6564
* @returns CancelablePromise<T>
6665
* @throws ApiError
6766
*/
68-
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
69-
return new CancelablePromise(async (resolve, reject, onCancel) => {
67+
export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: ApiRequestOptions): Observable<T> => {
68+
return new Observable<T>(subscriber => {
7069
try {
7170
const url = getUrl(config, options);
7271
const formData = getFormData(options);
7372
const body = getRequestBody(options);
74-
const headers = await getHeaders(config, options);
75-
76-
if (!onCancel.isCancelled) {
77-
const response = await sendRequest(config, options, url, formData, body, headers, onCancel);
78-
const responseBody = await getResponseBody(response);
79-
const responseHeader = getResponseHeader(response, options.responseHeader);
80-
81-
const result: ApiResult = {
82-
url,
83-
ok: response.ok,
84-
status: response.status,
85-
statusText: response.statusText,
86-
body: responseHeader ?? responseBody,
87-
};
88-
89-
catchErrors(options, result);
90-
91-
resolve(result.body);
92-
}
73+
getHeaders(config, options).then(headers => {
74+
75+
sendRequest<T>(config, options, http, url, formData, body, headers)
76+
.subscribe(response => {
77+
const responseBody = getResponseBody(response);
78+
const responseHeader = getResponseHeader(response, options.responseHeader);
79+
80+
const result: ApiResult = {
81+
url,
82+
ok: response.ok,
83+
status: response.status,
84+
statusText: response.statusText,
85+
body: responseHeader ?? responseBody,
86+
};
87+
88+
catchErrors(options, result);
89+
90+
subscriber.next(result.body);
91+
});
92+
});
9393
} catch (error) {
94-
reject(error);
94+
subscriber.error(error);
9595
}
9696
});
9797
};
98+
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
1-
export const sendRequest = async (
1+
export const sendRequest = <T>(
22
config: OpenAPIConfig,
33
options: ApiRequestOptions,
4+
http: HttpClient,
45
url: string,
6+
body: any,
57
formData: FormData | undefined,
6-
body: BodyInit | undefined,
7-
headers: Headers,
8-
onCancel: OnCancel
9-
): Promise<Response> => {
10-
const controller = new AbortController();
11-
12-
const request: RequestInit = {
8+
headers: HttpHeaders
9+
): Observable<HttpResponse<T>> => {
10+
return http.request<T>(url, options.method, {
1311
headers,
14-
body: body || formData,
15-
method: options.method,
16-
signal: controller.signal,
17-
};
18-
19-
if (config.WITH_CREDENTIALS) {
20-
request.credentials = config.CREDENTIALS;
21-
}
22-
23-
onCancel(() => controller.abort());
24-
25-
return await fetch(url, request);
12+
body: body ?? formData,
13+
withCredentials: config.WITH_CREDENTIALS,
14+
observe: 'response',
15+
});
2616
};

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
7474
const headers = await getHeaders(config, options, formData);
7575

7676
if (!onCancel.isCancelled) {
77-
const response = await sendRequest(config, options, url, formData, body, headers, onCancel);
77+
const response = await sendRequest<T>(config, options, url, body, formData, headers, onCancel);
7878
const responseBody = getResponseBody(response);
7979
const responseHeader = getResponseHeader(response, options.responseHeader);
8080

src/templates/core/axios/sendRequest.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const sendRequest = async (
1+
const sendRequest = async <T>(
22
config: OpenAPIConfig,
33
options: ApiRequestOptions,
44
url: string,
5-
formData: FormData | undefined,
65
body: any,
6+
formData: FormData | undefined,
77
headers: Record<string, string>,
88
onCancel: OnCancel
9-
): Promise<AxiosResponse<any>> => {
9+
): Promise<AxiosResponse<T>> => {
1010
const source = axios.CancelToken.source();
1111

1212
const requestConfig: AxiosRequestConfig = {

src/templates/core/fetch/getRequestBody.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const getRequestBody = (options: ApiRequestOptions): BodyInit | undefined => {
1+
const getRequestBody = (options: ApiRequestOptions): any => {
22
if (options.body) {
33
if (options.mediaType?.includes('/json')) {
44
return JSON.stringify(options.body)

src/templates/core/fetch/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
7171
const headers = await getHeaders(config, options);
7272

7373
if (!onCancel.isCancelled) {
74-
const response = await sendRequest(config, options, url, formData, body, headers, onCancel);
74+
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
7575
const responseBody = await getResponseBody(response);
7676
const responseHeader = getResponseHeader(response, options.responseHeader);
7777

src/templates/core/fetch/sendRequest.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export const sendRequest = async (
22
config: OpenAPIConfig,
33
options: ApiRequestOptions,
44
url: string,
5+
body: any,
56
formData: FormData | undefined,
6-
body: BodyInit | undefined,
77
headers: Headers,
88
onCancel: OnCancel
99
): Promise<Response> => {

src/templates/core/node/getRequestBody.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const getRequestBody = (options: ApiRequestOptions): BodyInit | undefined => {
1+
const getRequestBody = (options: ApiRequestOptions): any => {
22
if (options.body) {
33
if (options.mediaType?.includes('/json')) {
44
return JSON.stringify(options.body)

src/templates/core/node/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
7575
const headers = await getHeaders(config, options);
7676

7777
if (!onCancel.isCancelled) {
78-
const response = await sendRequest(options, url, formData, body, headers, onCancel);
78+
const response = await sendRequest(options, url, body, formData, headers, onCancel);
7979
const responseBody = await getResponseBody(response);
8080
const responseHeader = getResponseHeader(response, options.responseHeader);
8181

src/templates/core/node/sendRequest.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const sendRequest = async (
22
options: ApiRequestOptions,
33
url: string,
4+
body: any,
45
formData: FormData | undefined,
5-
body: BodyInit | undefined,
66
headers: Headers,
77
onCancel: OnCancel
88
): Promise<Response> => {

src/templates/core/xhr/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
7474
const headers = await getHeaders(config, options);
7575

7676
if (!onCancel.isCancelled) {
77-
const response = await sendRequest(config, options, url, formData, body, headers, onCancel);
77+
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
7878
const responseBody = getResponseBody(response);
7979
const responseHeader = getResponseHeader(response, options.responseHeader);
8080

src/templates/core/xhr/sendRequest.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export const sendRequest = async (
22
config: OpenAPIConfig,
33
options: ApiRequestOptions,
44
url: string,
5-
formData: FormData | undefined,
65
body: any,
6+
formData: FormData | undefined,
77
headers: Headers,
88
onCancel: OnCancel
99
): Promise<XMLHttpRequest> => {

src/templates/exportService.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ export class {{{name}}}{{{@root.postfix}}} {
7272
{{else}}
7373
{{#equals @root.httpClient 'angular'}}
7474
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
75+
return __request(OpenAPI, this.http, {
7576
{{else}}
7677
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
77-
{{/equals}}
7878
return __request(OpenAPI, {
79+
{{/equals}}
7980
{{/if}}
8081
method: '{{{method}}}',
8182
url: '{{{path}}}',

src/utils/registerHandlebarTemplates.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import Handlebars from 'handlebars/runtime';
22

33
import { HttpClient } from '../HttpClient';
44
import templateClient from '../templates/client.hbs';
5+
import angularGetHeaders from '../templates/core/angular/getHeaders.hbs';
6+
import angularGetRequestBody from '../templates/core/angular/getRequestBody.hbs';
7+
import angularGetResponseBody from '../templates/core/angular/getResponseBody.hbs';
8+
import angularGetResponseHeader from '../templates/core/angular/getResponseHeader.hbs';
9+
import angularRequest from '../templates/core/angular/request.hbs';
10+
import angularSendRequest from '../templates/core/angular/sendRequest.hbs';
511
import templateCoreApiError from '../templates/core/ApiError.hbs';
612
import templateCoreApiRequestOptions from '../templates/core/ApiRequestOptions.hbs';
713
import templateCoreApiResult from '../templates/core/ApiResult.hbs';
@@ -19,12 +25,6 @@ import fetchGetResponseBody from '../templates/core/fetch/getResponseBody.hbs';
1925
import fetchGetResponseHeader from '../templates/core/fetch/getResponseHeader.hbs';
2026
import fetchRequest from '../templates/core/fetch/request.hbs';
2127
import fetchSendRequest from '../templates/core/fetch/sendRequest.hbs';
22-
import angularGetHeaders from '../templates/core/angular/getHeaders.hbs';
23-
import angularGetRequestBody from '../templates/core/angular/getRequestBody.hbs';
24-
import angularGetResponseBody from '../templates/core/angular/getResponseBody.hbs';
25-
import angularGetResponseHeader from '../templates/core/angular/getResponseHeader.hbs';
26-
import angularRequest from '../templates/core/angular/request.hbs';
27-
import angularSendRequest from '../templates/core/angular/sendRequest.hbs';
2828
import functionBase64 from '../templates/core/functions/base64.hbs';
2929
import functionCatchErrors from '../templates/core/functions/catchErrors.hbs';
3030
import functionGetFormData from '../templates/core/functions/getFormData.hbs';

0 commit comments

Comments
 (0)