Skip to content

Commit

Permalink
Merge pull request #35 from nicolas-chaulet/feat/response-headers-3
Browse files Browse the repository at this point in the history
fix(request): support returning raw result object
  • Loading branch information
mrlubos authored Mar 4, 2024
2 parents 9bf43e2 + 1322362 commit 5e37208
Show file tree
Hide file tree
Showing 20 changed files with 547 additions and 57 deletions.
37 changes: 27 additions & 10 deletions src/templates/core/OpenAPI.hbs
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
{{>header}}

import type { ApiRequestOptions } from './ApiRequestOptions';
import type { TConfig, TResult } from './types';

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

export type OpenAPIConfig = {
BASE: string;
VERSION: string;
WITH_CREDENTIALS: boolean;
CREDENTIALS: 'include' | 'omit' | 'same-origin';
ENCODE_PATH?: ((path: string) => string) | undefined;
HEADERS?: Headers | Resolver<Headers> | undefined;
PASSWORD?: string | Resolver<string> | undefined;
RESULT?: TResult;
TOKEN?: string | Resolver<string> | undefined;
USERNAME?: string | Resolver<string> | undefined;
PASSWORD?: string | Resolver<string> | undefined;
HEADERS?: Headers | Resolver<Headers> | undefined;
ENCODE_PATH?: ((path: string) => string) | undefined;
VERSION: string;
WITH_CREDENTIALS: boolean;
};

export const OpenAPI: OpenAPIConfig = {
BASE: '{{{server}}}',
VERSION: '{{{version}}}',
WITH_CREDENTIALS: false,
CREDENTIALS: 'include',
ENCODE_PATH: undefined,
HEADERS: undefined,
PASSWORD: undefined,
RESULT: 'body',
TOKEN: undefined,
USERNAME: undefined,
PASSWORD: undefined,
HEADERS: undefined,
ENCODE_PATH: undefined,
VERSION: '{{{version}}}',
WITH_CREDENTIALS: false,
};

export const mergeOpenApiConfig = <T extends TResult>(config: OpenAPIConfig, overrides: TConfig<T>) => {
const merged = { ...config };
Object.entries(overrides)
.filter(([key]) => key.startsWith('_'))
.forEach(([key, value]) => {
const k = key.slice(1).toLocaleUpperCase() as keyof typeof merged;
if (merged.hasOwnProperty(k)) {
// @ts-ignore
merged[k] = value;
}
});
return merged;
};
2 changes: 1 addition & 1 deletion src/templates/core/axios/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, ax

catchErrorCodes(options, result);

resolve(result.body);
resolve(config.RESULT === 'raw' ? result : result.body);
}
} catch (error) {
reject(error);
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/fetch/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C

catchErrorCodes(options, result);

resolve(result.body);
resolve(config.RESULT === 'raw' ? result : result.body);
}
} catch (error) {
reject(error);
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/node/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C

catchErrorCodes(options, result);

resolve(result.body);
resolve(config.RESULT === 'raw' ? result : result.body);
}
} catch (error) {
reject(error);
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/xhr/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C

catchErrorCodes(options, result);

resolve(result.body);
resolve(config.RESULT === 'raw' ? result : result.body);
}
} catch (error) {
reject(error);
Expand Down
11 changes: 7 additions & 4 deletions src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ import { BaseHttpRequest } from '../core/BaseHttpRequest';
import type { BaseHttpRequest } from '../core/BaseHttpRequest';
{{/equals}}
{{else}}
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
{{#if @root.useOptions}}
import { mergeOpenApiConfig, OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
import type { TApiResponse, TConfig, TResult } from '../core/types';
{{else}}
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
{{/if}}
{{/if}}

Expand Down Expand Up @@ -99,11 +102,11 @@ export class {{{name}}}{{{@root.postfix}}} {
{{#equals @root.httpClient 'angular'}}
public {{{name}}}{{>operationTypes}}({{>operationParameters}}): Observable<{{>operationResult}}> {
{{>destructureData}}
return __request(OpenAPI, this.http, {
return __request({{>requestConfig}}, this.http, {
{{else}}
public static {{{name}}}{{>operationTypes}}({{>operationParameters}}): CancelablePromise<{{>operationResult}}> {
{{>destructureData}}
return __request(OpenAPI, {
return __request({{>requestConfig}}, {
{{/equals}}
{{/if}}
method: '{{{method}}}',
Expand Down
3 changes: 1 addition & 2 deletions src/templates/partials/destructureData.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{{#if parameters}}
{{#if @root.useOptions~}}
const {
{{#each parameters}}
{{{name}}}{{#if default}} = {{{default}}}{{/if}},
{{/each}}
...overrides
} = data;
{{/if}}
{{/if}}
5 changes: 5 additions & 0 deletions src/templates/partials/requestConfig.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{~#if @root.useOptions~}}
mergeOpenApiConfig(OpenAPI, overrides)
{{~else~}}
OpenAPI
{{~/if~}}
2 changes: 2 additions & 0 deletions src/utils/registerHandlebarTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import partialIsRequired from '../templates/partials/isRequired.hbs';
import partialOperationParameters from '../templates/partials/operationParameters.hbs';
import partialOperationResult from '../templates/partials/operationResult.hbs';
import partialOperationTypes from '../templates/partials/operationTypes.hbs';
import partialRequestConfig from '../templates/partials/requestConfig.hbs';
import partialSchema from '../templates/partials/schema.hbs';
import partialSchemaArray from '../templates/partials/schemaArray.hbs';
import partialSchemaComposition from '../templates/partials/schemaComposition.hbs';
Expand Down Expand Up @@ -153,6 +154,7 @@ export const registerHandlebarTemplates = (
Handlebars.registerPartial('operationParameters', Handlebars.template(partialOperationParameters));
Handlebars.registerPartial('operationResult', Handlebars.template(partialOperationResult));
Handlebars.registerPartial('operationTypes', Handlebars.template(partialOperationTypes));
Handlebars.registerPartial('requestConfig', Handlebars.template(partialRequestConfig));
Handlebars.registerPartial('schema', Handlebars.template(partialSchema));
Handlebars.registerPartial('schemaArray', Handlebars.template(partialSchemaArray));
Handlebars.registerPartial('schemaComposition', Handlebars.template(partialSchemaComposition));
Expand Down
Loading

0 comments on commit 5e37208

Please sign in to comment.