diff --git a/bin/index.js b/bin/index.js index dcc2e23d0..a9638476a 100755 --- a/bin/index.js +++ b/bin/index.js @@ -23,6 +23,7 @@ const params = program .option('--exportSchemas ', 'Write schemas to disk', false) .option('--indent ', 'Indentation options [4, 2, tab]', '4') .option('--postfixServices ', 'Service name postfix', 'Service') + .option('--serviceResponse [value]', 'Define shape of returned value from service calls') .option('--useDateType ', 'Output Date instead of string for the format "date-time" in the models', false) .option('--useOperationId ', 'Use operation id to generate operation names', true) .option('--postfixModels ', 'Model name postfix') @@ -55,6 +56,7 @@ if (OpenAPI) { postfixModels: params.postfixModels, postfixServices: params.postfixServices, request: params.request, + serviceResponse: params.serviceResponse, useDateType: JSON.parse(params.useDateType) === true, useOperationId: JSON.parse(params.useOperationId) === true, useOptions: JSON.parse(params.useOptions) === true, diff --git a/src/client/interfaces/Options.d.ts b/src/client/interfaces/Options.d.ts index b586de4f8..1cab22a88 100644 --- a/src/client/interfaces/Options.d.ts +++ b/src/client/interfaces/Options.d.ts @@ -1,5 +1,7 @@ import { HttpClient } from '../../HttpClient'; -import { Indent } from '../interfaces/Indent'; +import { Indent } from '../../Indent'; + +export type ServiceResponse = 'body' | 'generics' | 'response'; export interface Options { autoformat?: boolean; @@ -15,6 +17,7 @@ export interface Options { postfixModels?: string; postfixServices?: string; request?: string; + serviceResponse?: ServiceResponse; useDateType?: boolean; useOperationId?: boolean; useOptions?: boolean; diff --git a/src/index.ts b/src/index.ts index 9268fb32a..6b873d36b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,23 +17,24 @@ export { Indent } from './Indent'; * Generate the OpenAPI client. This method will read the OpenAPI specification and based on the * given language it will generate the client, including the typed models, validation schemas, * service layer, etc. - * @param input The relative location of the OpenAPI spec - * @param output The relative location of the output directory - * @param httpClient The selected httpClient (fetch, xhr, node or axios) - * @param clientName Custom client class name - * @param useOptions Use options or arguments functions - * @param useUnionTypes Use union types instead of enums * @param autoformat Process generated files with autoformatter + * @param clientName Custom client class name * @param exportCore Generate core client classes - * @param exportServices Generate services * @param exportModels Generate models * @param exportSchemas Generate schemas - * @param useDateType Output Date instead of string for the format "date-time" in the models - * @param useOperationId should the operationId be used when generating operation names + * @param exportServices Generate services + * @param httpClient The selected httpClient (fetch, xhr, node or axios) * @param indent Indentation options (4, 2 or tab) - * @param postfixServices Service name postfix + * @param input The relative location of the OpenAPI spec + * @param output The relative location of the output directory * @param postfixModels Model name postfix + * @param postfixServices Service name postfix * @param request Path to custom request file + * @param serviceResponse Define shape of returned value from service calls + * @param useDateType Output Date instead of string for the format "date-time" in the models + * @param useOperationId should the operationId be used when generating operation names + * @param useOptions Use options or arguments functions + * @param useUnionTypes Use union types instead of enums * @param write Write the files to disk (true or false) */ export const generate = async (options: Options): Promise => { @@ -46,6 +47,7 @@ export const generate = async (options: Options): Promise => { indent = Indent.SPACE_4, postfixModels = '', postfixServices = 'Service', + serviceResponse = 'body', useDateType = false, useOptions = false, useUnionTypes = false, @@ -55,6 +57,7 @@ export const generate = async (options: Options): Promise => { const openApiVersion = getOpenApiVersion(openApi); const templates = registerHandlebarTemplates({ httpClient, + serviceResponse, useUnionTypes, useOptions, }); @@ -90,6 +93,7 @@ export const generate = async (options: Options): Promise => { indent, postfixModels, postfixServices, + serviceResponse, useDateType, useOptions, useUnionTypes, diff --git a/src/templates/core/axios/request.hbs b/src/templates/core/axios/request.hbs index c1e40ff4a..7787cb1f3 100644 --- a/src/templates/core/axios/request.hbs +++ b/src/templates/core/axios/request.hbs @@ -64,7 +64,7 @@ import type { OpenAPIConfig } from './OpenAPI'; * @param config The OpenAPI configuration object * @param options The request options from the service * @param axiosClient The axios client instance to use - * @returns CancelablePromise + * @returns CancelablePromise<{{#equals @root.serviceResponse 'response'}}ApiResult{{else}}T{{/equals}}> * @throws ApiError */ export const request = (config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise => { @@ -90,7 +90,11 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions, ax catchErrorCodes(options, result); + {{#equals @root.serviceResponse 'generics'}} resolve(config.RESULT === 'raw' ? result : result.body); + {{else}} + resolve({{#equals @root.serviceResponse 'body'}}result.body{{else}}result{{/equals}}); + {{/equals}} } } catch (error) { reject(error); diff --git a/src/templates/core/fetch/request.hbs b/src/templates/core/fetch/request.hbs index 84e094e85..3d70c2b3f 100644 --- a/src/templates/core/fetch/request.hbs +++ b/src/templates/core/fetch/request.hbs @@ -56,10 +56,10 @@ import type { OpenAPIConfig } from './OpenAPI'; * Request method * @param config The OpenAPI configuration object * @param options The request options from the service - * @returns CancelablePromise + * @returns CancelablePromise<{{#equals @root.serviceResponse 'response'}}ApiResult{{else}}T{{/equals}}> * @throws ApiError */ -export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise => { +export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<{{#equals @root.serviceResponse 'response'}}ApiResult{{else}}T{{/equals}}> => { return new CancelablePromise(async (resolve, reject, onCancel) => { try { const url = getUrl(config, options); @@ -82,7 +82,11 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C catchErrorCodes(options, result); + {{#equals @root.serviceResponse 'generics'}} resolve(config.RESULT === 'raw' ? result : result.body); + {{else}} + resolve({{#equals @root.serviceResponse 'body'}}result.body{{else}}result{{/equals}}); + {{/equals}} } } catch (error) { reject(error); diff --git a/src/templates/core/node/request.hbs b/src/templates/core/node/request.hbs index 5f98ff2de..8e66c345e 100644 --- a/src/templates/core/node/request.hbs +++ b/src/templates/core/node/request.hbs @@ -61,10 +61,10 @@ import type { OpenAPIConfig } from './OpenAPI'; * Request method * @param config The OpenAPI configuration object * @param options The request options from the service - * @returns CancelablePromise + * @returns CancelablePromise<{{#equals @root.serviceResponse 'response'}}ApiResult{{else}}T{{/equals}}> * @throws ApiError */ -export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise => { +export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<{{#equals @root.serviceResponse 'response'}}ApiResult{{else}}T{{/equals}}> => { return new CancelablePromise(async (resolve, reject, onCancel) => { try { const url = getUrl(config, options); @@ -87,7 +87,11 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C catchErrorCodes(options, result); + {{#equals @root.serviceResponse 'generics'}} resolve(config.RESULT === 'raw' ? result : result.body); + {{else}} + resolve({{#equals @root.serviceResponse 'body'}}result.body{{else}}result{{/equals}}); + {{/equals}} } } catch (error) { reject(error); diff --git a/src/templates/core/request.hbs b/src/templates/core/request.hbs index c4e2478dd..e562df214 100644 --- a/src/templates/core/request.hbs +++ b/src/templates/core/request.hbs @@ -1,5 +1,5 @@ -{{~#equals @root.httpClient 'fetch'}}{{>fetch/request}}{{/equals~}} -{{~#equals @root.httpClient 'xhr'}}{{>xhr/request}}{{/equals~}} -{{~#equals @root.httpClient 'axios'}}{{>axios/request}}{{/equals~}} {{~#equals @root.httpClient 'angular'}}{{>angular/request}}{{/equals~}} +{{~#equals @root.httpClient 'axios'}}{{>axios/request}}{{/equals~}} +{{~#equals @root.httpClient 'fetch'}}{{>fetch/request}}{{/equals~}} {{~#equals @root.httpClient 'node'}}{{>node/request}}{{/equals~}} +{{~#equals @root.httpClient 'xhr'}}{{>xhr/request}}{{/equals~}} diff --git a/src/templates/core/xhr/request.hbs b/src/templates/core/xhr/request.hbs index f900aaabc..5513c6c7f 100644 --- a/src/templates/core/xhr/request.hbs +++ b/src/templates/core/xhr/request.hbs @@ -59,10 +59,10 @@ import type { OpenAPIConfig } from './OpenAPI'; * Request method * @param config The OpenAPI configuration object * @param options The request options from the service - * @returns CancelablePromise + * @returns CancelablePromise<{{#equals @root.serviceResponse 'response'}}ApiResult{{else}}T{{/equals}}> * @throws ApiError */ -export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise => { +export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<{{#equals @root.serviceResponse 'response'}}ApiResult{{else}}T{{/equals}}> => { return new CancelablePromise(async (resolve, reject, onCancel) => { try { const url = getUrl(config, options); @@ -85,7 +85,11 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C catchErrorCodes(options, result); + {{#equals @root.serviceResponse 'generics'}} resolve(config.RESULT === 'raw' ? result : result.body); + {{else}} + resolve({{#equals @root.serviceResponse 'body'}}result.body{{else}}result{{/equals}}); + {{/equals}} } } catch (error) { reject(error); diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index 058328c90..52635edef 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -17,6 +17,9 @@ import type { {{{this}}} } from '../models/{{{this}}}'; {{/each}} {{/if}} +{{#equals @root.serviceResponse 'response'}} +import type { ApiResult } from '../core/ApiResult'; +{{/equals}} {{#notEquals @root.httpClient 'angular'}} import type { CancelablePromise } from '../core/CancelablePromise'; {{/notEquals}} @@ -28,19 +31,24 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest'; {{/equals}} {{else}} {{#if @root.useOptions}} +{{#equals @root.serviceResponse 'generics'}} 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'; +{{/equals}} +{{else}} +import { OpenAPI } from '../core/OpenAPI'; +import { request as __request } from '../core/request'; {{/if}} {{/if}} {{#if @root.useOptions}} {{#each operations}} {{#if parameters}} -type {{{nameOperationDataType name}}} = { +export type {{{nameOperationDataType name}}} = { {{#each parameters}} {{{name}}}{{>isRequired}}: {{>type}}, {{/each}} diff --git a/src/templates/partials/destructureData.hbs b/src/templates/partials/destructureData.hbs index c7ff08c32..488d389aa 100644 --- a/src/templates/partials/destructureData.hbs +++ b/src/templates/partials/destructureData.hbs @@ -8,13 +8,23 @@ const { } = data; {{/if}} {{/if}} -{{~else~}} +{{else~}} {{#if @root.useOptions~}} +{{~#notEquals @root.serviceResponse 'generics'~}} +{{~#if parameters~}} +const { +{{#each parameters}} +{{{name}}}{{#if default}} = {{{default}}}{{/if}}, +{{/each}} +} = data; +{{/if}} +{{else}} const { {{#each parameters}} {{{name}}}{{#if default}} = {{{default}}}{{/if}}, {{/each}} ...overrides } = data; +{{/notEquals}} +{{/if}} {{/if}} -{{~/if~}} diff --git a/src/templates/partials/operationParameters.hbs b/src/templates/partials/operationParameters.hbs index 8da5566b2..2deee04d7 100644 --- a/src/templates/partials/operationParameters.hbs +++ b/src/templates/partials/operationParameters.hbs @@ -2,7 +2,11 @@ {{~#if @root.exportClient~}} {{#if parameters}}data: {{{nameOperationDataType name}}}{{#ifOperationDataOptional parameters}} = {}{{/ifOperationDataOptional}}{{/if}} {{~else~}} +{{~#notEquals @root.serviceResponse 'generics'~}} +{{#if parameters}}data: {{{nameOperationDataType name}}}{{#ifOperationDataOptional parameters}} = {}{{/ifOperationDataOptional}}{{/if}} +{{~else~}} data: {{#if parameters}}{{{nameOperationDataType name}}} & {{/if}}TConfig{{#ifOperationDataOptional parameters}} = {}{{/ifOperationDataOptional}} +{{~/notEquals~}} {{~/if~}} {{~else}} {{#if parameters}} diff --git a/src/templates/partials/operationResult.hbs b/src/templates/partials/operationResult.hbs index 16e25bbda..a741164c1 100644 --- a/src/templates/partials/operationResult.hbs +++ b/src/templates/partials/operationResult.hbs @@ -1,10 +1,19 @@ -{{~#if @root.useOptions~}}{{~#unless @root.exportClient~}} -TApiResponsetype}}{{#unless @last}} | {{/unless}}{{/each}} {{~else~}} void {{~/if~}} -{{~#if @root.useOptions~}}{{~#unless @root.exportClient~}} -> -{{~/unless~}}{{~/if~}} \ No newline at end of file +{{~#if @root.useOptions~}} +{{~#notEquals @root.serviceResponse 'generics'~}} +{{~#equals @root.serviceResponse 'response'}}>{{/equals~}} +{{~else~}} +{{~#unless @root.exportClient}}>{{/unless~}} +{{~/notEquals~}} +{{~/if~}} \ No newline at end of file diff --git a/src/templates/partials/operationTypes.hbs b/src/templates/partials/operationTypes.hbs index b737c1f25..43813de5e 100644 --- a/src/templates/partials/operationTypes.hbs +++ b/src/templates/partials/operationTypes.hbs @@ -1,5 +1,7 @@ {{#if @root.useOptions~}} +{{#equals @root.serviceResponse 'generics'}} {{~#unless @root.exportClient~}} {{~/unless~}} +{{/equals}} {{~/if}} diff --git a/src/templates/partials/requestConfig.hbs b/src/templates/partials/requestConfig.hbs index f0eb999a5..60503cb6b 100644 --- a/src/templates/partials/requestConfig.hbs +++ b/src/templates/partials/requestConfig.hbs @@ -1,5 +1,9 @@ {{~#if @root.useOptions~}} +{{~#equals @root.serviceResponse 'generics'~}} mergeOpenApiConfig(OpenAPI, overrides) {{~else~}} OpenAPI +{{~/equals~}} +{{~else~}} +OpenAPI {{~/if~}} diff --git a/src/utils/registerHandlebarHelpers.spec.ts b/src/utils/registerHandlebarHelpers.spec.ts index 09ddae82d..cae869811 100644 --- a/src/utils/registerHandlebarHelpers.spec.ts +++ b/src/utils/registerHandlebarHelpers.spec.ts @@ -7,6 +7,7 @@ describe('registerHandlebarHelpers', () => { it('should register the helpers', () => { registerHandlebarHelpers({ httpClient: HttpClient.FETCH, + serviceResponse: 'body', useOptions: false, useUnionTypes: false, }); diff --git a/src/utils/registerHandlebarHelpers.ts b/src/utils/registerHandlebarHelpers.ts index b3e2f7df0..7f9bbe4a9 100644 --- a/src/utils/registerHandlebarHelpers.ts +++ b/src/utils/registerHandlebarHelpers.ts @@ -25,7 +25,7 @@ const escapeEnumName = (name?: string) => { }; export const registerHandlebarHelpers = ( - root: Pick, 'httpClient' | 'useOptions' | 'useUnionTypes'> + root: Pick, 'httpClient' | 'serviceResponse' | 'useOptions' | 'useUnionTypes'> ): void => { Handlebars.registerHelper('camelCase', function (value: string): string { return camelCase(value); diff --git a/src/utils/registerHandlebarTemplates.spec.ts b/src/utils/registerHandlebarTemplates.spec.ts index 5e1302384..2f69dddc2 100644 --- a/src/utils/registerHandlebarTemplates.spec.ts +++ b/src/utils/registerHandlebarTemplates.spec.ts @@ -5,6 +5,7 @@ describe('registerHandlebarTemplates', () => { it('should return correct templates', () => { const templates = registerHandlebarTemplates({ httpClient: HttpClient.FETCH, + serviceResponse: 'body', useOptions: false, useUnionTypes: false, }); diff --git a/src/utils/registerHandlebarTemplates.ts b/src/utils/registerHandlebarTemplates.ts index 501a5cda1..1a9d0e162 100644 --- a/src/utils/registerHandlebarTemplates.ts +++ b/src/utils/registerHandlebarTemplates.ts @@ -114,7 +114,7 @@ export interface Templates { * so we can easily access the templates in our generator/write functions. */ export const registerHandlebarTemplates = ( - root: Pick, 'httpClient' | 'useOptions' | 'useUnionTypes'> + root: Pick, 'httpClient' | 'serviceResponse' | 'useOptions' | 'useUnionTypes'> ): Templates => { registerHandlebarHelpers(root); diff --git a/src/utils/writeClient.spec.ts b/src/utils/writeClient.spec.ts index 77948efcf..ac1343512 100644 --- a/src/utils/writeClient.spec.ts +++ b/src/utils/writeClient.spec.ts @@ -48,6 +48,7 @@ describe('writeClient', () => { httpClient: HttpClient.FETCH, postfixModels: 'AppClient', postfixServices: 'Service', + serviceResponse: 'body', useDateType: false, useOptions: false, useUnionTypes: false, diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index 69538cc33..2d4571f41 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -34,6 +34,7 @@ export const writeClient = async ( | 'output' | 'postfixModels' | 'postfixServices' + | 'serviceResponse' | 'useDateType' | 'useOptions' | 'useUnionTypes' @@ -49,6 +50,7 @@ export const writeClient = async ( | 'output' | 'postfixModels' | 'postfixServices' + | 'serviceResponse' | 'useDateType' | 'useOptions' | 'useUnionTypes' @@ -74,15 +76,7 @@ export const writeClient = async ( const outputPathCore = Path.resolve(outputPath, 'core'); await rmdir(outputPathCore); await mkdir(outputPathCore); - await writeClientCore( - client, - templates, - outputPathCore, - options.httpClient, - options.indent, - options.clientName, - options.request - ); + await writeClientCore(client, templates, outputPathCore, options); } if (options.exportServices) { diff --git a/src/utils/writeClientCore.spec.ts b/src/utils/writeClientCore.spec.ts index e43dcad9b..ca3301e08 100644 --- a/src/utils/writeClientCore.spec.ts +++ b/src/utils/writeClientCore.spec.ts @@ -40,7 +40,13 @@ describe('writeClientCore', () => { index: () => 'index', }; - await writeClientCore(client, templates, '/', HttpClient.FETCH, Indent.SPACE_4); + await writeClientCore(client, templates, '/', { + httpClient: HttpClient.FETCH, + indent: Indent.SPACE_4, + input: '', + output: '', + serviceResponse: 'body', + }); expect(writeFile).toBeCalledWith(resolve('/', '/OpenAPI.ts'), `settings${EOL}`); expect(writeFile).toBeCalledWith(resolve('/', '/ApiError.ts'), `apiError${EOL}`); diff --git a/src/utils/writeClientCore.ts b/src/utils/writeClientCore.ts index 15de51ff3..124d44818 100644 --- a/src/utils/writeClientCore.ts +++ b/src/utils/writeClientCore.ts @@ -1,8 +1,7 @@ import Path from 'path'; import type { Client } from '../client/interfaces/Client'; -import type { HttpClient } from '../HttpClient'; -import type { Indent } from '../Indent'; +import type { Options } from '../client/interfaces/Options'; import { copyFile, exists, writeFile } from './fileSystem'; import { formatIndentation as i } from './formatIndentation'; import { getHttpRequestName } from './getHttpRequestName'; @@ -13,26 +12,23 @@ import type { Templates } from './registerHandlebarTemplates'; * @param client Client object, containing, models, schemas and services * @param templates The loaded handlebar templates * @param outputPath Directory to write the generated files to - * @param httpClient The selected httpClient (fetch, xhr, node or axios) - * @param indent Indentation options (4, 2 or tab) - * @param clientName Custom client class name - * @param request Path to custom request file + * @param options Options passed to the `generate()` function */ export const writeClientCore = async ( client: Client, templates: Templates, outputPath: string, - httpClient: HttpClient, - indent: Indent, - clientName?: string, - request?: string + options: Pick, 'httpClient' | 'indent' | 'serviceResponse'> & + Omit ): Promise => { + const { clientName, httpClient, indent, request, serviceResponse } = options; const httpRequest = getHttpRequestName(httpClient); const context = { - httpClient, clientName, + httpClient, httpRequest, server: client.server, + serviceResponse, version: client.version, }; diff --git a/src/utils/writeClientServices.spec.ts b/src/utils/writeClientServices.spec.ts index 0d1f48a1b..7c0051659 100644 --- a/src/utils/writeClientServices.spec.ts +++ b/src/utils/writeClientServices.spec.ts @@ -47,6 +47,7 @@ describe('writeClientServices', () => { input: '', output: '', postfixServices: 'Service', + serviceResponse: 'body', useOptions: false, useUnionTypes: false, }); diff --git a/src/utils/writeClientServices.ts b/src/utils/writeClientServices.ts index 7a665e186..21d9e6b4d 100644 --- a/src/utils/writeClientServices.ts +++ b/src/utils/writeClientServices.ts @@ -18,18 +18,22 @@ export const writeClientServices = async ( services: Service[], templates: Templates, outputPath: string, - options: Pick, 'httpClient' | 'indent' | 'postfixServices' | 'useOptions' | 'useUnionTypes'> & - Omit + options: Pick< + Required, + 'httpClient' | 'indent' | 'postfixServices' | 'serviceResponse' | 'useOptions' | 'useUnionTypes' + > & + Omit ): Promise => { for (const service of services) { const file = Path.resolve(outputPath, `${service.name}${options.postfixServices}.ts`); const templateResult = templates.exports.service({ ...service, + exportClient: Boolean(options.clientName), httpClient: options.httpClient, - useUnionTypes: options.useUnionTypes, - useOptions: options.useOptions, postfix: options.postfixServices, - exportClient: Boolean(options.clientName), + serviceResponse: options.serviceResponse, + useOptions: options.useOptions, + useUnionTypes: options.useUnionTypes, }); await writeFile(file, i(f(templateResult), options.indent)); } diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index 79f83f64a..edafb0dd4 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -565,7 +565,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C catchErrorCodes(options, result); - resolve(config.RESULT === 'raw' ? result : result.body); + resolve(result.body); } } catch (error) { reject(error); @@ -3872,7 +3872,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C catchErrorCodes(options, result); - resolve(config.RESULT === 'raw' ? result : result.body); + resolve(result.body); } } catch (error) { reject(error); @@ -3943,24 +3943,23 @@ exports[`v3 should generate optional argument: test/generated/v3_options/service /* eslint-disable */ import type { ModelWithString } from '../models/ModelWithString'; import type { CancelablePromise } from '../core/CancelablePromise'; -import { mergeOpenApiConfig, OpenAPI } from '../core/OpenAPI'; +import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; -import type { TApiResponse, TConfig, TResult } from '../core/types'; -type TDataCallWithDefaultParameters = { +export type TDataCallWithDefaultParameters = { parameterString?: string | null, parameterNumber?: number | null, parameterBoolean?: boolean | null, parameterEnum?: 'Success' | 'Warning' | 'Error', parameterModel?: ModelWithString | null, } -type TDataCallWithDefaultOptionalParameters = { +export type TDataCallWithDefaultOptionalParameters = { parameterString?: string, parameterNumber?: number, parameterBoolean?: boolean, parameterEnum?: 'Success' | 'Warning' | 'Error', parameterModel?: ModelWithString, } -type TDataCallToTestOrderOfParams = { +export type TDataCallToTestOrderOfParams = { parameterStringWithNoDefault: string, parameterOptionalStringWithDefault?: string, parameterOptionalStringWithEmptyDefault?: string, @@ -3974,7 +3973,7 @@ export class DefaultsService { /** * @throws ApiError */ - public static callWithDefaultParameters(data: TDataCallWithDefaultParameters & TConfig = {}): CancelablePromise> { + public static callWithDefaultParameters(data: TDataCallWithDefaultParameters = {}): CancelablePromise { const { parameterString = 'Hello World!', parameterNumber = 123, @@ -3983,9 +3982,8 @@ export class DefaultsService { parameterModel = { "prop": "Hello World!" }, - ...overrides } = data; - return __request(mergeOpenApiConfig(OpenAPI, overrides), { + return __request(OpenAPI, { method: 'GET', url: '/api/v{api-version}/defaults', query: { @@ -4000,7 +3998,7 @@ export class DefaultsService { /** * @throws ApiError */ - public static callWithDefaultOptionalParameters(data: TDataCallWithDefaultOptionalParameters & TConfig = {}): CancelablePromise> { + public static callWithDefaultOptionalParameters(data: TDataCallWithDefaultOptionalParameters = {}): CancelablePromise { const { parameterString = 'Hello World!', parameterNumber = 123, @@ -4009,9 +4007,8 @@ export class DefaultsService { parameterModel = { "prop": "Hello World!" }, - ...overrides } = data; - return __request(mergeOpenApiConfig(OpenAPI, overrides), { + return __request(OpenAPI, { method: 'POST', url: '/api/v{api-version}/defaults', query: { @@ -4026,7 +4023,7 @@ export class DefaultsService { /** * @throws ApiError */ - public static callToTestOrderOfParams(data: TDataCallToTestOrderOfParams & TConfig): CancelablePromise> { + public static callToTestOrderOfParams(data: TDataCallToTestOrderOfParams): CancelablePromise { const { parameterStringWithNoDefault, parameterOptionalStringWithDefault = 'Hello World!', @@ -4036,9 +4033,8 @@ export class DefaultsService { parameterStringWithEmptyDefault = '', parameterStringNullableWithNoDefault, parameterStringNullableWithDefault = null, - ...overrides } = data; - return __request(mergeOpenApiConfig(OpenAPI, overrides), { + return __request(OpenAPI, { method: 'PUT', url: '/api/v{api-version}/defaults', query: { @@ -4622,7 +4618,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C catchErrorCodes(options, result); - resolve(config.RESULT === 'raw' ? result : result.body); + resolve(result.body); } } catch (error) { reject(error); diff --git a/test/index.js b/test/index.js index 5b24749a5..3ddabe8cc 100644 --- a/test/index.js +++ b/test/index.js @@ -18,6 +18,7 @@ const generate = async (input, output) => { // postfixServices: 'Service', postfixModels: '', // request: './test/custom/request.ts', + serviceResponse: 'body', useDateType: false, useOptions: true, useUnionTypes: true, diff --git a/types/index.d.ts b/types/index.d.ts index ea40914c6..d15b6cfce 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,6 +12,8 @@ export declare enum Indent { TAB = 'tab', } +export type ServiceResponse = 'body' | 'generics' | 'response'; + export type Options = { autoformat?: boolean; clientName?: string; @@ -26,6 +28,7 @@ export type Options = { postfixModels?: string; postfixServices?: string; request?: string; + serviceResponse?: ServiceResponse; useDateType?: boolean; useOptions?: boolean; useUnionTypes?: boolean;