Skip to content

Commit 4637325

Browse files
committed
- Shorter writing style for props passed in constructors
- Simplified header creation - Made helper scripts for test more reasable
1 parent a6fdd0a commit 4637325

36 files changed

+195
-151
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Before working on a Pull Request, create an issue explaining what you want to co
88
This ensures that your pull request won't go unnoticed, and that you are not contributing
99
something that is not suitable for the project.
1010

11-
If you are unfamiliar with Github Pull Requests, please read the following documentation:
11+
If you are unfamiliar with GitHub Pull Requests, please read the following documentation:
1212
https://help.github.com/articles/using-pull-requests
1313

1414
**Your Pull Request must:**

jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const config: Config.InitialOptions = {
3131
'<rootDir>/test/e2e/client.axios.spec.ts',
3232
'<rootDir>/test/e2e/client.babel.spec.ts',
3333
],
34+
testPathIgnorePatterns: ['<rootDir>/test/e2e/generated'],
3435
},
3536
],
3637
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!<rootDir>/src/**/*.d.ts', '!<rootDir>/bin', '!<rootDir>/dist'],

src/templates/core/BaseHttpRequest.hbs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import type { OpenAPIConfig } from './OpenAPI';
66

77
export class BaseHttpRequest {
88

9-
protected readonly config: OpenAPIConfig;
10-
11-
constructor(config: OpenAPIConfig) {
12-
this.config = config;
13-
}
9+
constructor(protected readonly config: OpenAPIConfig) {}
1410

1511
public request<T>(options: ApiRequestOptions): CancelablePromise<T> {
1612
throw new Error('Not Implemented');

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 2 deletions
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

@@ -95,4 +95,3 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
9595
}
9696
});
9797
};
98-

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/getHeaders.hbs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
44
const password = await resolve(options, config.PASSWORD);
55
const additionalHeaders = await resolve(options, config.HEADERS);
66

7-
const defaultHeaders = Object.entries({
7+
const headers = Object.entries({
88
Accept: 'application/json',
99
...additionalHeaders,
1010
...options.headers,
@@ -15,28 +15,26 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
1515
[key]: String(value),
1616
}), {} as Record<string, string>);
1717

18-
const headers = new Headers(defaultHeaders);
19-
2018
if (isStringWithValue(token)) {
21-
headers.append('Authorization', `Bearer ${token}`);
19+
headers['Authorization'] = `Bearer ${token}`;
2220
}
2321

2422
if (isStringWithValue(username) && isStringWithValue(password)) {
2523
const credentials = base64(`${username}:${password}`);
26-
headers.append('Authorization', `Basic ${credentials}`);
24+
headers['Authorization'] = `Basic ${credentials}`;
2725
}
2826

2927
if (options.body) {
3028
if (options.mediaType) {
31-
headers.append('Content-Type', options.mediaType);
29+
headers['Content-Type'] = options.mediaType;
3230
} else if (isBlob(options.body)) {
33-
headers.append('Content-Type', options.body.type || 'application/octet-stream');
31+
headers['Content-Type'] = options.body.type || 'application/octet-stream';
3432
} else if (isString(options.body)) {
35-
headers.append('Content-Type', 'text/plain');
33+
headers['Content-Type'] = 'text/plain';
3634
} else if (!isFormData(options.body)) {
37-
headers.append('Content-Type', 'application/json');
35+
headers['Content-Type'] = 'application/json';
3836
}
3937
}
4038

41-
return headers;
39+
return new Headers(headers);
4240
};

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> => {
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
2-
const encoder = config.ENCODE_PATH || encodeURI;
2+
const encoder = config.ENCODE_PATH || encodeURI;
33

4-
const path = options.url
5-
.replace('{api-version}', config.VERSION)
6-
.replace(/{(.*?)}/g, (substring: string, group: string) => {
7-
if (options.path?.hasOwnProperty(group)) {
8-
return encoder(String(options.path[group]));
9-
}
10-
return substring;
11-
});
4+
const path = options.url
5+
.replace('{api-version}', config.VERSION)
6+
.replace(/{(.*?)}/g, (substring: string, group: string) => {
7+
if (options.path?.hasOwnProperty(group)) {
8+
return encoder(String(options.path[group]));
9+
}
10+
return substring;
11+
});
1212

13-
const url = `${config.BASE}${path}`;
14-
if (options.query) {
15-
return `${url}${getQueryString(options.query)}`;
16-
}
17-
return url;
13+
const url = `${config.BASE}${path}`;
14+
if (options.query) {
15+
return `${url}${getQueryString(options.query)}`;
16+
}
17+
return url;
1818
};

0 commit comments

Comments
 (0)