Skip to content

Commit

Permalink
refactor(types): allow passing config
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Mar 4, 2024
1 parent d5e382c commit 07e0240
Show file tree
Hide file tree
Showing 17 changed files with 898 additions and 236 deletions.
6 changes: 3 additions & 3 deletions src/templates/core/ApiResult.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{>header}}

export type ApiResult = {
readonly url: string;
export type ApiResult<TData = any> = {
readonly body: TData;
readonly ok: boolean;
readonly status: number;
readonly statusText: string;
readonly body: any;
readonly url: string;
};
14 changes: 14 additions & 0 deletions src/templates/core/types.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{>header}}

import type { ApiResult } from './ApiResult';

export type TResult = 'body' | 'raw';

export type TApiResponse<T extends TResult, TData> =
Exclude<T, 'raw'> extends never
? ApiResult<TData>
: ApiResult<TData>['body'];

export type TConfig<T extends TResult> = {
_result?: T;
};
11 changes: 7 additions & 4 deletions src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest';
{{else}}
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
{{#if @root.useOptions}}
import type { TApiResponse, TConfig, TResult } from '../core/types';
{{/if}}
{{/if}}

{{#if @root.useOptions}}
Expand Down Expand Up @@ -84,21 +87,21 @@ export class {{{name}}}{{{@root.postfix}}} {
*/
{{#if @root.exportClient}}
{{#equals @root.httpClient 'angular'}}
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
public {{{name}}}{{>operationTypes}}({{>operationParameters}}): Observable<{{>operationResult}}> {
{{>destructureData}}
return this.httpRequest.request({
{{else}}
public {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
public {{{name}}}{{>operationTypes}}({{>operationParameters}}): CancelablePromise<{{>operationResult}}> {
{{>destructureData}}
return this.httpRequest.request({
{{/equals}}
{{else}}
{{#equals @root.httpClient 'angular'}}
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
public {{{name}}}{{>operationTypes}}({{>operationParameters}}): Observable<{{>operationResult}}> {
{{>destructureData}}
return __request(OpenAPI, this.http, {
{{else}}
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
public static {{{name}}}{{>operationTypes}}({{>operationParameters}}): CancelablePromise<{{>operationResult}}> {
{{>destructureData}}
return __request(OpenAPI, {
{{/equals}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#if parameters}}
{{#if @root.useOptions~}}
data: {{{nameOperationDataType name}}}{{#ifOperationDataOptional parameters}} = {}{{/ifOperationDataOptional}}
data: {{#if parameters}}{{{nameOperationDataType name}}} & {{/if}}TConfig<T>{{#ifOperationDataOptional parameters}} = {}{{/ifOperationDataOptional}}
{{~else}}
{{#if parameters}}

{{#each parameters}}
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{{~#if @root.useOptions~}}
TApiResponse<T, {{/if~}}
{{~#if results~}}
{{#each results}}{{>type}}{{#unless @last}} | {{/unless}}{{/each}}
{{~else~}}
void
{{~/if~}}
{{~#if @root.useOptions~}}
>
{{~/if~}}
3 changes: 3 additions & 0 deletions src/templates/partials/operationTypes.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#if @root.useOptions~}}
<T extends TResult>
{{~/if}}
13 changes: 9 additions & 4 deletions src/utils/registerHandlebarTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import nodeRequest from '../templates/core/node/request.hbs';
import nodeSendRequest from '../templates/core/node/sendRequest.hbs';
import templateCoreSettings from '../templates/core/OpenAPI.hbs';
import templateCoreRequest from '../templates/core/request.hbs';
import templateCoreTypes from '../templates/core/types.hbs';
import xhrGetHeaders from '../templates/core/xhr/getHeaders.hbs';
import xhrGetRequestBody from '../templates/core/xhr/getRequestBody.hbs';
import xhrGetResponseBody from '../templates/core/xhr/getResponseBody.hbs';
Expand All @@ -65,8 +66,9 @@ import partialHeader from '../templates/partials/header.hbs';
import partialIsNullable from '../templates/partials/isNullable.hbs';
import partialIsReadOnly from '../templates/partials/isReadOnly.hbs';
import partialIsRequired from '../templates/partials/isRequired.hbs';
import partialParameters from '../templates/partials/parameters.hbs';
import partialResult from '../templates/partials/result.hbs';
import partialOperationParameters from '../templates/partials/operationParameters.hbs';
import partialOperationResult from '../templates/partials/operationResult.hbs';
import partialOperationTypes from '../templates/partials/operationTypes.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 @@ -96,6 +98,7 @@ export interface Templates {
httpRequest: Handlebars.TemplateDelegate;
request: Handlebars.TemplateDelegate;
settings: Handlebars.TemplateDelegate;
types: Handlebars.TemplateDelegate;
};
exports: {
model: Handlebars.TemplateDelegate;
Expand Down Expand Up @@ -126,6 +129,7 @@ export const registerHandlebarTemplates = (
httpRequest: Handlebars.template(templateCoreHttpRequest),
request: Handlebars.template(templateCoreRequest),
settings: Handlebars.template(templateCoreSettings),
types: Handlebars.template(templateCoreTypes),
},
exports: {
model: Handlebars.template(templateExportModel),
Expand All @@ -146,8 +150,9 @@ export const registerHandlebarTemplates = (
Handlebars.registerPartial('isNullable', Handlebars.template(partialIsNullable));
Handlebars.registerPartial('isReadOnly', Handlebars.template(partialIsReadOnly));
Handlebars.registerPartial('isRequired', Handlebars.template(partialIsRequired));
Handlebars.registerPartial('parameters', Handlebars.template(partialParameters));
Handlebars.registerPartial('result', Handlebars.template(partialResult));
Handlebars.registerPartial('operationParameters', Handlebars.template(partialOperationParameters));
Handlebars.registerPartial('operationResult', Handlebars.template(partialOperationResult));
Handlebars.registerPartial('operationTypes', Handlebars.template(partialOperationTypes));
Handlebars.registerPartial('schema', Handlebars.template(partialSchema));
Handlebars.registerPartial('schemaArray', Handlebars.template(partialSchemaArray));
Handlebars.registerPartial('schemaComposition', Handlebars.template(partialSchemaComposition));
Expand Down
19 changes: 10 additions & 9 deletions src/utils/writeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ describe('writeClient', () => {
};

const templates: Templates = {
index: () => 'index',
client: () => 'client',
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
core: {
settings: () => 'settings',
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
cancelablePromise: () => 'cancelablePromise',
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClient(client, templates, {
Expand Down
19 changes: 10 additions & 9 deletions src/utils/writeClientClass.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ describe('writeClientClass', () => {
};

const templates: Templates = {
index: () => 'index',
client: () => 'client',
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
core: {
settings: () => 'settings',
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
cancelablePromise: () => 'cancelablePromise',
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientClass(client, templates, './dist', HttpClient.FETCH, 'AppClient', Indent.SPACE_4, '');
Expand Down
20 changes: 11 additions & 9 deletions src/utils/writeClientCore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ describe('writeClientCore', () => {
};

const templates: Templates = {
index: () => 'index',
client: () => 'client',
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
core: {
settings: () => 'settings',
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
cancelablePromise: () => 'cancelablePromise',
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientCore(client, templates, '/', HttpClient.FETCH, Indent.SPACE_4);
Expand All @@ -47,5 +48,6 @@ describe('writeClientCore', () => {
expect(writeFile).toBeCalledWith(resolve('/', '/ApiResult.ts'), `apiResult${EOL}`);
expect(writeFile).toBeCalledWith(resolve('/', '/CancelablePromise.ts'), `cancelablePromise${EOL}`);
expect(writeFile).toBeCalledWith(resolve('/', '/request.ts'), `request${EOL}`);
expect(writeFile).toBeCalledWith(resolve('/', '/types.ts'), `types${EOL}`);
});
});
1 change: 1 addition & 0 deletions src/utils/writeClientCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const writeClientCore = async (
i(templates.core.cancelablePromise(context), indent)
);
await writeFile(Path.resolve(outputPath, 'request.ts'), i(templates.core.request(context), indent));
await writeFile(Path.resolve(outputPath, 'types.ts'), i(templates.core.types(context), indent));

if (Boolean(clientName)) {
await writeFile(
Expand Down
19 changes: 10 additions & 9 deletions src/utils/writeClientIndex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ describe('writeClientIndex', () => {
};

const templates: Templates = {
index: () => 'index',
client: () => 'client',
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
core: {
settings: () => 'settings',
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
cancelablePromise: () => 'cancelablePromise',
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', '');
Expand Down
19 changes: 10 additions & 9 deletions src/utils/writeClientModels.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ describe('writeClientModels', () => {
];

const templates: Templates = {
index: () => 'index',
client: () => 'client',
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
core: {
settings: () => 'settings',
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
cancelablePromise: () => 'cancelablePromise',
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientModels(models, templates, '/', {
Expand Down
19 changes: 10 additions & 9 deletions src/utils/writeClientSchemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ describe('writeClientSchemas', () => {
];

const templates: Templates = {
index: () => 'index',
client: () => 'client',
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
core: {
settings: () => 'settings',
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
cancelablePromise: () => 'cancelablePromise',
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientSchemas(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4);
Expand Down
19 changes: 10 additions & 9 deletions src/utils/writeClientServices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ describe('writeClientServices', () => {
];

const templates: Templates = {
index: () => 'index',
client: () => 'client',
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
core: {
settings: () => 'settings',
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
cancelablePromise: () => 'cancelablePromise',
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientServices(services, templates, '/', {
Expand Down
Loading

0 comments on commit 07e0240

Please sign in to comment.