Skip to content

Commit 8e05e64

Browse files
committed
- Fixed Angular client
1 parent dc5e69a commit 8e05e64

File tree

13 files changed

+230
-85
lines changed

13 files changed

+230
-85
lines changed

jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const config: Config.InitialOptions = {
3333
'<rootDir>/test/e2e/client.axios.spec.ts',
3434
'<rootDir>/test/e2e/client.babel.spec.ts',
3535
],
36-
modulePathIgnorePatterns: ['<rootDir>/test/e2e/generated'],
36+
modulePathIgnorePatterns: ['<rootDir>/test/e2e/generated'],
3737
},
3838
],
3939
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!<rootDir>/src/**/*.d.ts', '!<rootDir>/bin', '!<rootDir>/dist'],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"test:update": "jest --selectProjects UNIT --updateSnapshot",
5252
"test:watch": "jest --selectProjects UNIT --watch",
5353
"test:coverage": "jest --selectProjects UNIT --coverage",
54-
"test:e2e": "jest --selectProjects E2E --runInBand",
54+
"test:e2e": "jest --selectProjects E2E --runInBand --verbose",
5555
"eslint": "eslint .",
5656
"eslint:fix": "eslint . --fix",
5757
"prepublishOnly": "yarn run clean && yarn run release",
Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
1-
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<HttpHeaders> => {
2-
const token = await resolve(options, config.TOKEN);
3-
const username = await resolve(options, config.USERNAME);
4-
const password = await resolve(options, config.PASSWORD);
5-
const additionalHeaders = await resolve(options, config.HEADERS);
6-
7-
const headers = Object.entries({
8-
Accept: 'application/json',
9-
...additionalHeaders,
10-
...options.headers,
1+
const getHeaders = (config: OpenAPIConfig, options: ApiRequestOptions): Observable<HttpHeaders> => {
2+
return forkJoin({
3+
token: resolve(options, config.TOKEN),
4+
username: resolve(options, config.USERNAME),
5+
password: resolve(options, config.PASSWORD),
6+
additionalHeaders: resolve(options, config.HEADERS),
117
})
12-
.filter(([_, value]) => isDefined(value))
13-
.reduce((headers, [key, value]) => ({
14-
...headers,
15-
[key]: String(value),
16-
}), {} as Record<string, string>);
8+
.pipe(
9+
map(({ token, username, password, additionalHeaders }) => {
10+
const headers = Object.entries({
11+
Accept: 'application/json',
12+
...additionalHeaders,
13+
...options.headers,
14+
})
15+
.filter(([_, value]) => isDefined(value))
16+
.reduce((headers, [key, value]) => ({
17+
...headers,
18+
[key]: String(value),
19+
}), {} as Record<string, string>);
1720

18-
if (isStringWithValue(token)) {
19-
headers['Authorization'] = `Bearer ${token}`;
20-
}
21+
if (isStringWithValue(token)) {
22+
headers['Authorization'] = `Bearer ${token}`;
23+
}
2124

22-
if (isStringWithValue(username) && isStringWithValue(password)) {
23-
const credentials = base64(`${username}:${password}`);
24-
headers['Authorization'] = `Basic ${credentials}`;
25-
}
25+
if (isStringWithValue(username) && isStringWithValue(password)) {
26+
const credentials = base64(`${username}:${password}`);
27+
headers['Authorization'] = `Basic ${credentials}`;
28+
}
2629

27-
if (options.body) {
28-
if (options.mediaType) {
29-
headers['Content-Type'] = options.mediaType;
30-
} else if (isBlob(options.body)) {
31-
headers['Content-Type'] = options.body.type || 'application/octet-stream';
32-
} else if (isString(options.body)) {
33-
headers['Content-Type'] = 'text/plain';
34-
} else if (!isFormData(options.body)) {
35-
headers['Content-Type'] = 'application/json';
36-
}
37-
}
30+
if (options.body) {
31+
if (options.mediaType) {
32+
headers['Content-Type'] = options.mediaType;
33+
} else if (isBlob(options.body)) {
34+
headers['Content-Type'] = options.body.type || 'application/octet-stream';
35+
} else if (isString(options.body)) {
36+
headers['Content-Type'] = 'text/plain';
37+
} else if (!isFormData(options.body)) {
38+
headers['Content-Type'] = 'application/json';
39+
}
40+
}
3841

39-
return new HttpHeaders(headers);
42+
return new HttpHeaders(headers);
43+
})
44+
);
4045
};

src/templates/core/angular/request.hbs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{{>header}}
22

33
import { HttpClient, HttpHeaders } from '@angular/common/http';
4-
import type { HttpResponse } from '@angular/common/http';
5-
import { Observable } from 'rxjs';
4+
import type { HttpResponse, HttpErrorResponse } from '@angular/common/http';
5+
import { catchError, forkJoin, map, mergeMap, of, throwError } from 'rxjs';
6+
import type { Observable } from 'rxjs';
67

78
import { ApiError } from './ApiError';
89
import type { ApiRequestOptions } from './ApiRequestOptions';
@@ -54,46 +55,55 @@ import type { OpenAPIConfig } from './OpenAPI';
5455
{{>angular/getResponseBody}}
5556

5657

57-
{{>functions/catchErrors}}
58+
{{>functions/catchErrorCodes}}
5859

5960

6061
/**
6162
* Request method
6263
* @param config The OpenAPI configuration object
6364
* @param http The Angular HTTP client
6465
* @param options The request options from the service
65-
* @returns CancelablePromise<T>
66+
* @returns Observable<T>
6667
* @throws ApiError
6768
*/
6869
export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: ApiRequestOptions): Observable<T> => {
69-
return new Observable<T>(subscriber => {
70-
try {
71-
const url = getUrl(config, options);
72-
const formData = getFormData(options);
73-
const body = getRequestBody(options);
74-
getHeaders(config, options).then(headers => {
75-
76-
sendRequest<T>(config, options, http, url, formData, body, headers)
77-
.subscribe(response => {
78-
const responseBody = 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-
subscriber.next(result.body);
92-
});
93-
});
94-
} catch (error) {
95-
subscriber.error(error);
96-
}
97-
});
70+
const url = getUrl(config, options);
71+
const formData = getFormData(options);
72+
const body = getRequestBody(options);
73+
74+
return getHeaders(config, options).pipe(
75+
mergeMap( headers => {
76+
return sendRequest<T>(config, options, http, url, formData, body, headers);
77+
}),
78+
map(response => {
79+
const responseBody = getResponseBody(response);
80+
const responseHeader = getResponseHeader(response, options.responseHeader);
81+
return {
82+
url,
83+
ok: response.ok,
84+
status: response.status,
85+
statusText: response.statusText,
86+
body: responseHeader ?? responseBody,
87+
} as ApiResult;
88+
}),
89+
catchError((error: HttpErrorResponse) => {
90+
if (!error.status) {
91+
return throwError(() => error);
92+
}
93+
return of({
94+
url,
95+
ok: error.ok,
96+
status: error.status,
97+
statusText: error.statusText,
98+
body: error.statusText,
99+
} as ApiResult);
100+
}),
101+
map(result => {
102+
catchErrorCodes(options, result);
103+
return result.body as T;
104+
}),
105+
catchError((error: ApiError) => {
106+
return throwError(() => error);
107+
}),
108+
);
98109
};
99-

src/templates/core/axios/request.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import type { OpenAPIConfig } from './OpenAPI';
5555
{{>axios/getResponseBody}}
5656

5757

58-
{{>functions/catchErrors}}
58+
{{>functions/catchErrorCodes}}
5959

6060

6161
/**
@@ -86,7 +86,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8686
body: responseHeader ?? responseBody,
8787
};
8888

89-
catchErrors(options, result);
89+
catchErrorCodes(options, result);
9090

9191
resolve(result.body);
9292
}

src/templates/core/fetch/request.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import type { OpenAPIConfig } from './OpenAPI';
5252
{{>fetch/getResponseBody}}
5353

5454

55-
{{>functions/catchErrors}}
55+
{{>functions/catchErrorCodes}}
5656

5757

5858
/**
@@ -83,7 +83,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8383
body: responseHeader ?? responseBody,
8484
};
8585

86-
catchErrors(options, result);
86+
catchErrorCodes(options, result);
8787

8888
resolve(result.body);
8989
}

src/templates/core/functions/catchErrors.hbs renamed to src/templates/core/functions/catchErrorCodes.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const catchErrors = (options: ApiRequestOptions, result: ApiResult): void => {
1+
const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
22
const errors: Record<number, string> = {
33
400: 'Bad Request',
44
401: 'Unauthorized',

src/templates/core/node/request.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import type { OpenAPIConfig } from './OpenAPI';
5656
{{>node/getResponseBody}}
5757

5858

59-
{{>functions/catchErrors}}
59+
{{>functions/catchErrorCodes}}
6060

6161

6262
/**
@@ -87,7 +87,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8787
body: responseHeader ?? responseBody,
8888
};
8989

90-
catchErrors(options, result);
90+
catchErrorCodes(options, result);
9191

9292
resolve(result.body);
9393
}

src/templates/core/xhr/request.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import type { OpenAPIConfig } from './OpenAPI';
5555
{{>xhr/getResponseBody}}
5656

5757

58-
{{>functions/catchErrors}}
58+
{{>functions/catchErrorCodes}}
5959

6060

6161
/**
@@ -86,7 +86,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8686
body: responseHeader ?? responseBody,
8787
};
8888

89-
catchErrors(options, result);
89+
catchErrorCodes(options, result);
9090

9191
resolve(result.body);
9292
}

src/utils/registerHandlebarTemplates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import fetchGetResponseHeader from '../templates/core/fetch/getResponseHeader.hb
2626
import fetchRequest from '../templates/core/fetch/request.hbs';
2727
import fetchSendRequest from '../templates/core/fetch/sendRequest.hbs';
2828
import functionBase64 from '../templates/core/functions/base64.hbs';
29-
import functionCatchErrors from '../templates/core/functions/catchErrors.hbs';
29+
import functionCatchErrorCodes from '../templates/core/functions/catchErrorCodes.hbs';
3030
import functionGetFormData from '../templates/core/functions/getFormData.hbs';
3131
import functionGetQueryString from '../templates/core/functions/getQueryString.hbs';
3232
import functionGetUrl from '../templates/core/functions/getUrl.hbs';
@@ -167,7 +167,7 @@ export const registerHandlebarTemplates = (root: {
167167
Handlebars.registerPartial('base', Handlebars.template(partialBase));
168168

169169
// Generic functions used in 'request' file @see src/templates/core/request.hbs for more info
170-
Handlebars.registerPartial('functions/catchErrors', Handlebars.template(functionCatchErrors));
170+
Handlebars.registerPartial('functions/catchErrorCodes', Handlebars.template(functionCatchErrorCodes));
171171
Handlebars.registerPartial('functions/getFormData', Handlebars.template(functionGetFormData));
172172
Handlebars.registerPartial('functions/getQueryString', Handlebars.template(functionGetQueryString));
173173
Handlebars.registerPartial('functions/getUrl', Handlebars.template(functionGetUrl));

0 commit comments

Comments
 (0)