Skip to content

Commit 5e37208

Browse files
authored
Merge pull request #35 from nicolas-chaulet/feat/response-headers-3
fix(request): support returning raw result object
2 parents 9bf43e2 + 1322362 commit 5e37208

20 files changed

+547
-57
lines changed

src/templates/core/OpenAPI.hbs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
{{>header}}
22

33
import type { ApiRequestOptions } from './ApiRequestOptions';
4+
import type { TConfig, TResult } from './types';
45

56
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
67
type Headers = Record<string, string>;
78

89
export type OpenAPIConfig = {
910
BASE: string;
10-
VERSION: string;
11-
WITH_CREDENTIALS: boolean;
1211
CREDENTIALS: 'include' | 'omit' | 'same-origin';
12+
ENCODE_PATH?: ((path: string) => string) | undefined;
13+
HEADERS?: Headers | Resolver<Headers> | undefined;
14+
PASSWORD?: string | Resolver<string> | undefined;
15+
RESULT?: TResult;
1316
TOKEN?: string | Resolver<string> | undefined;
1417
USERNAME?: string | Resolver<string> | undefined;
15-
PASSWORD?: string | Resolver<string> | undefined;
16-
HEADERS?: Headers | Resolver<Headers> | undefined;
17-
ENCODE_PATH?: ((path: string) => string) | undefined;
18+
VERSION: string;
19+
WITH_CREDENTIALS: boolean;
1820
};
1921

2022
export const OpenAPI: OpenAPIConfig = {
2123
BASE: '{{{server}}}',
22-
VERSION: '{{{version}}}',
23-
WITH_CREDENTIALS: false,
2424
CREDENTIALS: 'include',
25+
ENCODE_PATH: undefined,
26+
HEADERS: undefined,
27+
PASSWORD: undefined,
28+
RESULT: 'body',
2529
TOKEN: undefined,
2630
USERNAME: undefined,
27-
PASSWORD: undefined,
28-
HEADERS: undefined,
29-
ENCODE_PATH: undefined,
31+
VERSION: '{{{version}}}',
32+
WITH_CREDENTIALS: false,
33+
};
34+
35+
export const mergeOpenApiConfig = <T extends TResult>(config: OpenAPIConfig, overrides: TConfig<T>) => {
36+
const merged = { ...config };
37+
Object.entries(overrides)
38+
.filter(([key]) => key.startsWith('_'))
39+
.forEach(([key, value]) => {
40+
const k = key.slice(1).toLocaleUpperCase() as keyof typeof merged;
41+
if (merged.hasOwnProperty(k)) {
42+
// @ts-ignore
43+
merged[k] = value;
44+
}
45+
});
46+
return merged;
3047
};

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, ax
9090

9191
catchErrorCodes(options, result);
9292

93-
resolve(result.body);
93+
resolve(config.RESULT === 'raw' ? result : result.body);
9494
}
9595
} catch (error) {
9696
reject(error);

src/templates/core/fetch/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8282

8383
catchErrorCodes(options, result);
8484

85-
resolve(result.body);
85+
resolve(config.RESULT === 'raw' ? result : result.body);
8686
}
8787
} catch (error) {
8888
reject(error);

src/templates/core/node/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8787

8888
catchErrorCodes(options, result);
8989

90-
resolve(result.body);
90+
resolve(config.RESULT === 'raw' ? result : result.body);
9191
}
9292
} catch (error) {
9393
reject(error);

src/templates/core/xhr/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8585

8686
catchErrorCodes(options, result);
8787

88-
resolve(result.body);
88+
resolve(config.RESULT === 'raw' ? result : result.body);
8989
}
9090
} catch (error) {
9191
reject(error);

src/templates/exportService.hbs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ import { BaseHttpRequest } from '../core/BaseHttpRequest';
2727
import type { BaseHttpRequest } from '../core/BaseHttpRequest';
2828
{{/equals}}
2929
{{else}}
30-
import { OpenAPI } from '../core/OpenAPI';
31-
import { request as __request } from '../core/request';
3230
{{#if @root.useOptions}}
31+
import { mergeOpenApiConfig, OpenAPI } from '../core/OpenAPI';
32+
import { request as __request } from '../core/request';
3333
import type { TApiResponse, TConfig, TResult } from '../core/types';
34+
{{else}}
35+
import { OpenAPI } from '../core/OpenAPI';
36+
import { request as __request } from '../core/request';
3437
{{/if}}
3538
{{/if}}
3639

@@ -99,11 +102,11 @@ export class {{{name}}}{{{@root.postfix}}} {
99102
{{#equals @root.httpClient 'angular'}}
100103
public {{{name}}}{{>operationTypes}}({{>operationParameters}}): Observable<{{>operationResult}}> {
101104
{{>destructureData}}
102-
return __request(OpenAPI, this.http, {
105+
return __request({{>requestConfig}}, this.http, {
103106
{{else}}
104107
public static {{{name}}}{{>operationTypes}}({{>operationParameters}}): CancelablePromise<{{>operationResult}}> {
105108
{{>destructureData}}
106-
return __request(OpenAPI, {
109+
return __request({{>requestConfig}}, {
107110
{{/equals}}
108111
{{/if}}
109112
method: '{{{method}}}',
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
{{#if parameters}}
21
{{#if @root.useOptions~}}
32
const {
43
{{#each parameters}}
54
{{{name}}}{{#if default}} = {{{default}}}{{/if}},
65
{{/each}}
6+
...overrides
77
} = data;
88
{{/if}}
9-
{{/if}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{~#if @root.useOptions~}}
2+
mergeOpenApiConfig(OpenAPI, overrides)
3+
{{~else~}}
4+
OpenAPI
5+
{{~/if~}}

src/utils/registerHandlebarTemplates.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import partialIsRequired from '../templates/partials/isRequired.hbs';
6969
import partialOperationParameters from '../templates/partials/operationParameters.hbs';
7070
import partialOperationResult from '../templates/partials/operationResult.hbs';
7171
import partialOperationTypes from '../templates/partials/operationTypes.hbs';
72+
import partialRequestConfig from '../templates/partials/requestConfig.hbs';
7273
import partialSchema from '../templates/partials/schema.hbs';
7374
import partialSchemaArray from '../templates/partials/schemaArray.hbs';
7475
import partialSchemaComposition from '../templates/partials/schemaComposition.hbs';
@@ -153,6 +154,7 @@ export const registerHandlebarTemplates = (
153154
Handlebars.registerPartial('operationParameters', Handlebars.template(partialOperationParameters));
154155
Handlebars.registerPartial('operationResult', Handlebars.template(partialOperationResult));
155156
Handlebars.registerPartial('operationTypes', Handlebars.template(partialOperationTypes));
157+
Handlebars.registerPartial('requestConfig', Handlebars.template(partialRequestConfig));
156158
Handlebars.registerPartial('schema', Handlebars.template(partialSchema));
157159
Handlebars.registerPartial('schemaArray', Handlebars.template(partialSchemaArray));
158160
Handlebars.registerPartial('schemaComposition', Handlebars.template(partialSchemaComposition));

0 commit comments

Comments
 (0)