diff --git a/bin/index.js b/bin/index.js index aa2cfbed..19abc93a 100755 --- a/bin/index.js +++ b/bin/index.js @@ -12,7 +12,7 @@ program .option('-o, --output ', 'Output directory', './generated') .option('-c, --client ', 'HTTP client to generate [fetch, xhr]', 'fetch') .option('--useOptions', 'Use options vs arguments style functions') - .option('--useUnionTypes', 'Use inclusive union types') + .option('--useUnionTypes', 'Use union types instead of enums') .option('--exportCore ', 'Generate core', true) .option('--exportServices ', 'Generate services', true) .option('--exportModels ', 'Generate models', true) diff --git a/src/index.spec.ts b/src/index.spec.ts index 03d2e49b..612f2173 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -6,7 +6,6 @@ describe('index', () => { input: './test/mock/v2/spec.json', output: './test/result/v2/', useOptions: true, - useUnionTypes: true, write: false, }); }); @@ -16,7 +15,6 @@ describe('index', () => { input: './test/mock/v3/spec.json', output: './test/result/v3/', useOptions: true, - useUnionTypes: true, write: false, }); }); diff --git a/src/index.ts b/src/index.ts index 19118359..4f2749b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,18 +61,18 @@ export async function generate({ switch (openApiVersion) { case OpenApiVersion.V2: { const client = parseV2(openApi); - const clientFinal = postProcessClient(client, useUnionTypes); + const clientFinal = postProcessClient(client); if (write) { - await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas); + await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas); } break; } case OpenApiVersion.V3: { const client = parseV3(openApi); - const clientFinal = postProcessClient(client, useUnionTypes); + const clientFinal = postProcessClient(client); if (write) { - await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas); + await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas); } break; } diff --git a/src/templates/core/request.ts b/src/templates/core/request.ts index d6c9932b..1d17662b 100644 --- a/src/templates/core/request.ts +++ b/src/templates/core/request.ts @@ -2,13 +2,13 @@ /* tslint:disable */ /* eslint-disable */ -import {getFormData} from './getFormData'; -import {getQueryString} from './getQueryString'; -import {OpenAPI} from './OpenAPI'; -import {RequestOptions} from './RequestOptions'; -import {requestUsingFetch} from './requestUsingFetch'; -import {requestUsingXHR} from './requestUsingXHR'; -import {Result} from './Result'; +import { getFormData } from './getFormData'; +import { getQueryString } from './getQueryString'; +import { OpenAPI } from './OpenAPI'; +import { RequestOptions } from './RequestOptions'; +import { requestUsingFetch } from './requestUsingFetch'; +import { requestUsingXHR } from './requestUsingXHR'; +import { Result } from './Result'; /** * Create the request. diff --git a/src/utils/getExtendedByList.ts b/src/utils/getExtendedByList.ts index 9b0d025d..9b0192be 100644 --- a/src/utils/getExtendedByList.ts +++ b/src/utils/getExtendedByList.ts @@ -5,7 +5,6 @@ import { unique } from './unique'; /** * Get the full list of models that are extended by the given model. - * This list is used when we have the flag "useUnionTypes" enabled. * @param model * @param client */ diff --git a/src/utils/getExtendedFromList.ts b/src/utils/getExtendedFromList.ts index 4880094c..b2271890 100644 --- a/src/utils/getExtendedFromList.ts +++ b/src/utils/getExtendedFromList.ts @@ -5,7 +5,6 @@ import { unique } from './unique'; /** * Get the full list of models that are extended from the given model. - * This list is used when we have the flag "useUnionTypes" enabled. * @param model * @param client */ diff --git a/src/utils/postProcessClient.ts b/src/utils/postProcessClient.ts index 2ee9303c..d3cec023 100644 --- a/src/utils/postProcessClient.ts +++ b/src/utils/postProcessClient.ts @@ -5,12 +5,11 @@ import { postProcessService } from './postProcessService'; /** * Post process client * @param client Client object with all the models, services, etc. - * @param useUnionTypes Use inclusive union types. */ -export function postProcessClient(client: Client, useUnionTypes: boolean): Client { +export function postProcessClient(client: Client): Client { return { ...client, - models: client.models.map(model => postProcessModel(model, client, useUnionTypes)), - services: client.services.map(service => postProcessService(service, client, useUnionTypes)), + models: client.models.map(model => postProcessModel(model)), + services: client.services.map(service => postProcessService(service)), }; } diff --git a/src/utils/postProcessModel.ts b/src/utils/postProcessModel.ts index ae002d37..08adea20 100644 --- a/src/utils/postProcessModel.ts +++ b/src/utils/postProcessModel.ts @@ -1,24 +1,18 @@ -import { Client } from '../client/interfaces/Client'; import { Model } from '../client/interfaces/Model'; import { postProcessModelEnum } from './postProcessModelEnum'; import { postProcessModelEnums } from './postProcessModelEnums'; import { postProcessModelImports } from './postProcessModelImports'; -import { postProcessUnionTypes } from './postProcessUnionTypes'; /** - * Post process the model. If needed this will convert types to union types, - * see the "useUnionTypes" flag in the documentation. Plus this will cleanup - * any double imports or enum values. + * Post process the model. + * This will cleanup any double imports or enum values. * @param model - * @param client - * @param useUnionTypes */ -export function postProcessModel(model: Model, client: Client, useUnionTypes: boolean): Model { - const clone = postProcessUnionTypes(model, client, useUnionTypes); +export function postProcessModel(model: Model): Model { return { - ...clone, - imports: postProcessModelImports(clone), - enums: postProcessModelEnums(clone), - enum: postProcessModelEnum(clone), + ...model, + imports: postProcessModelImports(model), + enums: postProcessModelEnums(model), + enum: postProcessModelEnum(model), }; } diff --git a/src/utils/postProcessService.ts b/src/utils/postProcessService.ts index b114a7de..51e8e432 100644 --- a/src/utils/postProcessService.ts +++ b/src/utils/postProcessService.ts @@ -1,11 +1,10 @@ -import { Client } from '../client/interfaces/Client'; import { Service } from '../client/interfaces/Service'; import { postProcessServiceImports } from './postProcessServiceImports'; import { postProcessServiceOperations } from './postProcessServiceOperations'; -export function postProcessService(service: Service, client: Client, useUnionTypes: boolean): Service { +export function postProcessService(service: Service): Service { const clone = { ...service }; - clone.operations = postProcessServiceOperations(clone, client, useUnionTypes); + clone.operations = postProcessServiceOperations(clone); clone.operations.forEach(operation => { clone.imports.push(...operation.imports); }); diff --git a/src/utils/postProcessServiceOperations.ts b/src/utils/postProcessServiceOperations.ts index 1fec8f0b..33105243 100644 --- a/src/utils/postProcessServiceOperations.ts +++ b/src/utils/postProcessServiceOperations.ts @@ -1,10 +1,8 @@ -import { Client } from '../client/interfaces/Client'; import { Operation } from '../client/interfaces/Operation'; import { Service } from '../client/interfaces/Service'; import { flatMap } from './flatMap'; -import { postProcessUnionTypes } from './postProcessUnionTypes'; -export function postProcessServiceOperations(service: Service, client: Client, useUnionTypes: boolean = false): Operation[] { +export function postProcessServiceOperations(service: Service): Operation[] { const names = new Map(); return service.operations.map(operation => { @@ -12,8 +10,6 @@ export function postProcessServiceOperations(service: Service, client: Client, u // Parse the service parameters and results, very similar to how we parse // properties of models. These methods will extend the type if needed. - clone.parameters = clone.parameters.map(parameter => postProcessUnionTypes(parameter, client, useUnionTypes)); - clone.results = clone.results.map(result => postProcessUnionTypes(result, client, useUnionTypes)); clone.imports.push(...flatMap(clone.parameters, parameter => parameter.imports)); clone.imports.push(...flatMap(clone.results, result => result.imports)); diff --git a/src/utils/postProcessUnionTypes.ts b/src/utils/postProcessUnionTypes.ts deleted file mode 100644 index f4c3b9ea..00000000 --- a/src/utils/postProcessUnionTypes.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { Client } from '../client/interfaces/Client'; -import { Model } from '../client/interfaces/Model'; -import { getExtendedByList } from './getExtendedByList'; - -/** - * This post processor will convert types to union types. For more information - * please check the documentation. In a nutshell: By setting the "useUnionTypes" - * flag we will convert base types to a union of types that are extended from - * the base type. - * @param model - * @param client - * @param useUnionTypes - */ -export function postProcessUnionTypes(model: T, client: Client, useUnionTypes: boolean): T { - const clone = { ...model }; - - if (useUnionTypes) { - // If this is not a root definition, then new need to check the base type - if (!clone.isDefinition) { - const extendedBy = getExtendedByList(clone, client); - const extendedByNames = extendedBy.map(m => m.name); - clone.base = [clone.base, ...extendedByNames].sort().join(' | '); - clone.imports = clone.imports.concat(...extendedByNames); - } - - // In any case we need to check the properties of a model. - // When the types get extended, we also need to make sure we update the imports. - clone.properties = clone.properties.map(property => postProcessUnionTypes(property, client, useUnionTypes)); - clone.properties.forEach(property => { - clone.imports.push(...property.imports); - }); - - // When the model has a link (in case of an Array or Dictionary), - // then we also process this linked model and again update the imports. - clone.link = clone.link ? postProcessUnionTypes(clone.link, client, useUnionTypes) : null; - if (clone.link) { - clone.imports.push(...clone.link.imports); - } - } - return clone; -} diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index fc607e4a..3df807a4 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -21,6 +21,7 @@ async function copySupportFile(filePath: string, outputPath: string): Promise { + + const result = await __request({ + method: 'post', + path: \`/api/v\${OpenAPI.VERSION}/requestBody/\`, + body: requestBody, + }); + + catchGenericError(result); + + return result.body; + } + +}" +`; + exports[`v3 should generate: ./test/result/v3/services/ResponseService.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ diff --git a/test/index.js b/test/index.js index 7203be83..12df508d 100644 --- a/test/index.js +++ b/test/index.js @@ -28,7 +28,6 @@ async function run() { output: './test/result/v2/', httpClient: OpenAPI.HttpClient.FETCH, useOptions: false, - useUnionTypes: false, exportCore: true, exportSchemas: true, exportModels: true, @@ -40,7 +39,6 @@ async function run() { output: './test/result/v3/', httpClient: OpenAPI.HttpClient.FETCH, useOptions: false, - useUnionTypes: false, exportCore: true, exportSchemas: true, exportModels: true, diff --git a/test/index.spec.js b/test/index.spec.js index d4acc508..3b671daf 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -11,7 +11,6 @@ describe('v2', () => { output: './test/result/v2/', httpClient: OpenAPI.HttpClient.FETCH, useOptions: false, - useUnionTypes: false, exportCore: true, exportSchemas: true, exportModels: true, @@ -34,7 +33,6 @@ describe('v3', () => { output: './test/result/v3/', httpClient: OpenAPI.HttpClient.FETCH, useOptions: false, - useUnionTypes: false, exportCore: true, exportSchemas: true, exportModels: true, diff --git a/test/mock/v3/spec.json b/test/mock/v3/spec.json index 24d346ac..9e7c7102 100644 --- a/test/mock/v3/spec.json +++ b/test/mock/v3/spec.json @@ -298,6 +298,16 @@ } } }, + "/api/v{api-version}/requestBody/": { + "post": { + "tags": [ + "RequestBody" + ], + "requestBody": { + "$ref": "#/components/requestBodies/SimpleRequestBody" + } + } + }, "/api/v{api-version}/defaults": { "get": { "tags": [ @@ -1115,6 +1125,20 @@ } }, "components": { + "requestBodies": { + "SimpleRequestBody": { + "description": "A reusable request body", + "required": false, + "content": { + "application/json": { + "description": "Message for default response", + "schema": { + "$ref": "#/components/schemas/ModelWithString" + } + } + } + } + }, "schemas": { "MultilineComment": { "description": "Testing multiline comments.\nThis must go to the next line.\n\nThis will contain a break.",