From 923c141fdba787cf8e9326c8e871994b7de2e651 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Wed, 30 Sep 2020 23:35:02 +0200 Subject: [PATCH] - Cleanup of templates - Fixed File type for Node.js and Browser clients --- package.json | 4 +-- src/openApi/v2/parser/constants.ts | 4 +-- src/openApi/v2/parser/getMappedType.spec.ts | 2 +- src/openApi/v3/parser/constants.ts | 2 +- src/openApi/v3/parser/getMappedType.spec.ts | 2 +- src/templates/core/request.hbs | 12 ++----- src/templates/exportSchema.hbs | 2 +- src/templates/exportService.hbs | 18 +++++------ src/templates/partials/base.hbs | 7 ++++ src/templates/partials/isRequired.hbs | 10 +++--- src/templates/partials/parameters.hbs | 2 +- src/templates/partials/schemaArray.hbs | 6 ++-- src/templates/partials/schemaDictionary.hbs | 6 ++-- src/templates/partials/schemaEnum.hbs | 6 ++-- src/templates/partials/schemaGeneric.hbs | 36 ++++++++++----------- src/templates/partials/schemaInterface.hbs | 6 ++-- src/templates/partials/typeArray.hbs | 2 +- src/templates/partials/typeDictionary.hbs | 2 +- src/templates/partials/typeEnum.hbs | 10 ++---- src/templates/partials/typeGeneric.hbs | 2 +- src/templates/partials/typeReference.hbs | 2 +- src/utils/registerHandlebarHelpers.ts | 4 +-- src/utils/registerHandlebarTemplates.ts | 2 ++ src/utils/writeClient.ts | 8 ++--- src/utils/writeClientModels.ts | 5 ++- src/utils/writeClientSchemas.ts | 5 ++- src/utils/writeClientServices.ts | 5 ++- test/__snapshots__/index.spec.js.snap | 34 ++++++++++--------- test/e2e/scripts/compile.js | 4 +-- test/e2e/v2.fetch.spec.js | 2 +- test/e2e/v2.xhr.spec.js | 2 +- test/e2e/v3.fetch.spec.js | 2 +- test/e2e/v3.xhr.spec.js | 2 +- tsconfig.json | 2 +- 34 files changed, 114 insertions(+), 106 deletions(-) create mode 100644 src/templates/partials/base.hbs diff --git a/package.json b/package.json index 128572aa..604ccb55 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi-typescript-codegen", - "version": "0.5.0-beta", + "version": "0.5.0-rc", "description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", @@ -46,8 +46,6 @@ "build": "rollup --config --environment NODE_ENV:development", "build:watch": "rollup --config --environment NODE_ENV:development --watch", "release": "rollup --config --environment NODE_ENV:production", - "start": "nest start --path ./test/server/tsconfig.json", - "start:watch": "nest start --path ./test/server/tsconfig.json --watch", "run": "node ./test/index.js", "test": "jest --selectProjects UNIT", "test:update": "jest --selectProjects UNIT --updateSnapshot", diff --git a/src/openApi/v2/parser/constants.ts b/src/openApi/v2/parser/constants.ts index c92bcee8..6336efda 100644 --- a/src/openApi/v2/parser/constants.ts +++ b/src/openApi/v2/parser/constants.ts @@ -1,7 +1,7 @@ export enum PrimaryType { - FILE = 'any', // File or Buffer? + FILE = 'File', OBJECT = 'any', - ARRAY = 'any', + ARRAY = 'any[]', BOOLEAN = 'boolean', NUMBER = 'number', STRING = 'string', diff --git a/src/openApi/v2/parser/getMappedType.spec.ts b/src/openApi/v2/parser/getMappedType.spec.ts index dacad502..b437f85d 100644 --- a/src/openApi/v2/parser/getMappedType.spec.ts +++ b/src/openApi/v2/parser/getMappedType.spec.ts @@ -2,7 +2,7 @@ import { getMappedType } from './getMappedType'; describe('getMappedType', () => { it('should map types to the basics', () => { - expect(getMappedType('File')).toEqual('any'); + expect(getMappedType('File')).toEqual('File'); expect(getMappedType('String')).toEqual('string'); expect(getMappedType('date')).toEqual('string'); expect(getMappedType('date-time')).toEqual('string'); diff --git a/src/openApi/v3/parser/constants.ts b/src/openApi/v3/parser/constants.ts index 70b7656a..7a8632dd 100644 --- a/src/openApi/v3/parser/constants.ts +++ b/src/openApi/v3/parser/constants.ts @@ -1,5 +1,5 @@ export enum PrimaryType { - FILE = 'any', // File or Buffer? + FILE = 'File', OBJECT = 'any', ARRAY = 'any[]', BOOLEAN = 'boolean', diff --git a/src/openApi/v3/parser/getMappedType.spec.ts b/src/openApi/v3/parser/getMappedType.spec.ts index dacad502..b437f85d 100644 --- a/src/openApi/v3/parser/getMappedType.spec.ts +++ b/src/openApi/v3/parser/getMappedType.spec.ts @@ -2,7 +2,7 @@ import { getMappedType } from './getMappedType'; describe('getMappedType', () => { it('should map types to the basics', () => { - expect(getMappedType('File')).toEqual('any'); + expect(getMappedType('File')).toEqual('File'); expect(getMappedType('String')).toEqual('string'); expect(getMappedType('date')).toEqual('string'); expect(getMappedType('date-time')).toEqual('string'); diff --git a/src/templates/core/request.hbs b/src/templates/core/request.hbs index 902d49e7..2a32097e 100644 --- a/src/templates/core/request.hbs +++ b/src/templates/core/request.hbs @@ -1,9 +1,3 @@ -{{#equals httpClient 'fetch'}} -{{>fetch/request}} -{{/equals}} -{{#equals httpClient 'xhr'}} -{{>xhr/request}} -{{/equals}} -{{#equals httpClient 'node'}} -{{>node/request}} -{{/equals}} +{{#equals @root.httpClient 'fetch'}}{{>fetch/request}}{{/equals}} +{{#equals @root.httpClient 'xhr'}}{{>xhr/request}}{{/equals}} +{{#equals @root.httpClient 'node'}}{{>node/request}}{{/equals}} diff --git a/src/templates/exportSchema.hbs b/src/templates/exportSchema.hbs index 0495c4d6..d0f33fb7 100644 --- a/src/templates/exportSchema.hbs +++ b/src/templates/exportSchema.hbs @@ -2,7 +2,7 @@ {{#if extends}} {{#each extends}} -import type { ${{{this}}} } from './${{{this}}}'; +import { ${{{this}}} } from './${{{this}}}'; {{/each}} {{/if}} diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index a1f6f00e..10cf87f7 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -37,45 +37,45 @@ export class {{{name}}} { const result = await __request({ method: '{{{method}}}', path: `{{{path}}}`, - {{#if parametersCookie~}} + {{#if parametersCookie}} cookies: { {{#each parametersCookie}} '{{{prop}}}': {{{name}}}, {{/each}} }, {{/if}} - {{#if parametersHeader~}} + {{#if parametersHeader}} headers: { {{#each parametersHeader}} '{{{prop}}}': {{{name}}}, {{/each}} }, {{/if}} - {{#if parametersQuery~}} + {{#if parametersQuery}} query: { {{#each parametersQuery}} '{{{prop}}}': {{{name}}}, {{/each}} }, {{/if}} - {{#if parametersForm~}} + {{#if parametersForm}} formData: { {{#each parametersForm}} '{{{prop}}}': {{{name}}}, {{/each}} }, {{/if}} - {{#if parametersBody~}} + {{#if parametersBody}} body: {{{parametersBody.name}}}, {{/if}} - {{#if responseHeader~}} + {{#if responseHeader}} responseHeader: '{{{responseHeader}}}', {{/if}} - {{#if errors~}} + {{#if errors}} errors: { - {{#each errors}} + {{#each errors}} {{{code}}}: `{{{description}}}`, - {{/each}} + {{/each}} }, {{/if}} }); diff --git a/src/templates/partials/base.hbs b/src/templates/partials/base.hbs new file mode 100644 index 00000000..8c6d817f --- /dev/null +++ b/src/templates/partials/base.hbs @@ -0,0 +1,7 @@ +{{~#equals this 'File'~}} +{{~#equals @root.httpClient 'fetch'}}Blob{{/equals~}} +{{~#equals @root.httpClient 'xhr'}}Blob{{/equals~}} +{{~#equals @root.httpClient 'node'}}Buffer | ArrayBuffer | ArrayBufferView{{/equals~}} +{{~else~}} +{{this}} +{{~/equals~}} diff --git a/src/templates/partials/isRequired.hbs b/src/templates/partials/isRequired.hbs index dbecae99..b829272b 100644 --- a/src/templates/partials/isRequired.hbs +++ b/src/templates/partials/isRequired.hbs @@ -1,5 +1,5 @@ -{{#if @root.useOptions}} -{{~#unless isRequired}}?{{else if default}}?{{/unless~}} -{{else}} -{{~#unless isRequired}}{{#unless default}}?{{/unless}}{{/unless~}} -{{/if}} +{{~#if @root.useOptions~}} +{{#unless isRequired}}?{{else if default}}?{{/unless}} +{{~else~}} +{{#unless isRequired}}{{#unless default}}?{{/unless}}{{/unless}} +{{~/if~}} diff --git a/src/templates/partials/parameters.hbs b/src/templates/partials/parameters.hbs index c4f5241b..4c16b47c 100644 --- a/src/templates/partials/parameters.hbs +++ b/src/templates/partials/parameters.hbs @@ -9,7 +9,7 @@ {{{name}}}{{>isRequired}}: {{>type}}, {{/each}} } -{{~else}} +{{else}} {{#each parameters}} {{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}}, diff --git a/src/templates/partials/schemaArray.hbs b/src/templates/partials/schemaArray.hbs index 31a75ae0..8d537a7f 100644 --- a/src/templates/partials/schemaArray.hbs +++ b/src/templates/partials/schemaArray.hbs @@ -1,12 +1,12 @@ { type: 'Array', -{{#if isReadOnly~}} +{{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} -{{#if isRequired~}} +{{#if isRequired}} isRequired: {{{isRequired}}}, {{/if}} -{{#if isNullable~}} +{{#if isNullable}} isNullable: {{{isNullable}}}, {{/if}} } diff --git a/src/templates/partials/schemaDictionary.hbs b/src/templates/partials/schemaDictionary.hbs index 0c159f34..c965c92c 100644 --- a/src/templates/partials/schemaDictionary.hbs +++ b/src/templates/partials/schemaDictionary.hbs @@ -1,12 +1,12 @@ { type: 'Dictionary', -{{#if isReadOnly~}} +{{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} -{{#if isRequired~}} +{{#if isRequired}} isRequired: {{{isRequired}}}, {{/if}} -{{#if isNullable~}} +{{#if isNullable}} isNullable: {{{isNullable}}}, {{/if}} } diff --git a/src/templates/partials/schemaEnum.hbs b/src/templates/partials/schemaEnum.hbs index 9a4e7ac3..308cab5b 100644 --- a/src/templates/partials/schemaEnum.hbs +++ b/src/templates/partials/schemaEnum.hbs @@ -1,12 +1,12 @@ { type: 'Enum', -{{#if isReadOnly~}} +{{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} -{{#if isRequired~}} +{{#if isRequired}} isRequired: {{{isRequired}}}, {{/if}} -{{#if isNullable~}} +{{#if isNullable}} isNullable: {{{isNullable}}}, {{/if}} } diff --git a/src/templates/partials/schemaGeneric.hbs b/src/templates/partials/schemaGeneric.hbs index dd2a60dd..7b0d96c9 100644 --- a/src/templates/partials/schemaGeneric.hbs +++ b/src/templates/partials/schemaGeneric.hbs @@ -1,56 +1,56 @@ { -{{#if type~}} +{{#if type}} type: '{{{base}}}', {{/if}} -{{#if isReadOnly~}} +{{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} -{{#if isRequired~}} +{{#if isRequired}} isRequired: {{{isRequired}}}, {{/if}} -{{#if isNullable~}} +{{#if isNullable}} isNullable: {{{isNullable}}}, {{/if}} -{{#if format~}} +{{#if format}} format: '{{{format}}}', {{/if}} -{{#if maximum~}} +{{#if maximum}} maximum: {{{maximum}}}, {{/if}} -{{#if exclusiveMaximum~}} +{{#if exclusiveMaximum}} exclusiveMaximum: {{{exclusiveMaximum}}}, {{/if}} -{{#if minimum~}} +{{#if minimum}} minimum: {{{minimum}}}, {{/if}} -{{#if exclusiveMinimum~}} +{{#if exclusiveMinimum}} exclusiveMinimum: {{{exclusiveMinimum}}}, {{/if}} -{{#if multipleOf~}} +{{#if multipleOf}} multipleOf: {{{multipleOf}}}, {{/if}} -{{#if maxLength~}} +{{#if maxLength}} maxLength: {{{maxLength}}}, {{/if}} -{{#if minLength~}} +{{#if minLength}} minLength: {{{minLength}}}, {{/if}} -{{#if pattern~}} +{{#if pattern}} pattern: '{{{pattern}}}', {{/if}} -{{#if maxItems~}} +{{#if maxItems}} maxItems: {{{maxItems}}}, {{/if}} -{{#if minItems~}} +{{#if minItems}} minItems: {{{minItems}}}, {{/if}} -{{#if uniqueItems~}} +{{#if uniqueItems}} uniqueItems: {{{uniqueItems}}}, {{/if}} -{{#if maxProperties~}} +{{#if maxProperties}} maxProperties: {{{maxProperties}}}, {{/if}} -{{#if minProperties~}} +{{#if minProperties}} minProperties: {{{minProperties}}}, {{/if}} } diff --git a/src/templates/partials/schemaInterface.hbs b/src/templates/partials/schemaInterface.hbs index 3cb486b1..037cc7f1 100644 --- a/src/templates/partials/schemaInterface.hbs +++ b/src/templates/partials/schemaInterface.hbs @@ -11,13 +11,13 @@ {{/each}} {{/if}} }, -{{#if isReadOnly~}} +{{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} -{{#if isRequired~}} +{{#if isRequired}} isRequired: {{{isRequired}}}, {{/if}} -{{#if isNullable~}} +{{#if isNullable}} isNullable: {{{isNullable}}}, {{/if}} } diff --git a/src/templates/partials/typeArray.hbs b/src/templates/partials/typeArray.hbs index e18a72da..5f2e7e2a 100644 --- a/src/templates/partials/typeArray.hbs +++ b/src/templates/partials/typeArray.hbs @@ -1,5 +1,5 @@ {{~#if link~}} Array<{{>type link}}>{{>isNullable}} {{~else~}} -Array<{{{base}}}>{{>isNullable}} +Array<{{>base base}}>{{>isNullable}} {{~/if~}} diff --git a/src/templates/partials/typeDictionary.hbs b/src/templates/partials/typeDictionary.hbs index 5d8567a1..c074544a 100644 --- a/src/templates/partials/typeDictionary.hbs +++ b/src/templates/partials/typeDictionary.hbs @@ -1,5 +1,5 @@ {{~#if link~}} Recordtype link}}>{{>isNullable}} {{~else~}} -Record{{>isNullable}} +Recordbase base}}>{{>isNullable}} {{~/if~}} diff --git a/src/templates/partials/typeEnum.hbs b/src/templates/partials/typeEnum.hbs index 125c9924..9693459b 100644 --- a/src/templates/partials/typeEnum.hbs +++ b/src/templates/partials/typeEnum.hbs @@ -1,13 +1,7 @@ {{~#if @root.useUnionTypes~}} -{{~#each enum~}} -{{{value}}}{{#unless @last}} | {{/unless}} -{{~/each~}} -{{>isNullable}} +{{#each enum}}{{{value}}}{{#unless @last}} | {{/unless}}{{/each}}{{>isNullable}} {{~else if parent~}} {{{parent}}}.{{{name}}}{{>isNullable}} {{~else~}} -{{~#each enum~}} -{{{value}}}{{#unless @last}} | {{/unless}} -{{~/each~}} -{{>isNullable}} +{{#each enum}}{{{value}}}{{#unless @last}} | {{/unless}}{{/each}}{{>isNullable}} {{~/if~}} diff --git a/src/templates/partials/typeGeneric.hbs b/src/templates/partials/typeGeneric.hbs index b98a9301..64a8b410 100644 --- a/src/templates/partials/typeGeneric.hbs +++ b/src/templates/partials/typeGeneric.hbs @@ -1 +1 @@ -{{{base}}}{{>isNullable}} +{{>base base}}{{>isNullable}} diff --git a/src/templates/partials/typeReference.hbs b/src/templates/partials/typeReference.hbs index b98a9301..64a8b410 100644 --- a/src/templates/partials/typeReference.hbs +++ b/src/templates/partials/typeReference.hbs @@ -1 +1 @@ -{{{base}}}{{>isNullable}} +{{>base base}}{{>isNullable}} diff --git a/src/utils/registerHandlebarHelpers.ts b/src/utils/registerHandlebarHelpers.ts index ebd99b69..991e4c7d 100644 --- a/src/utils/registerHandlebarHelpers.ts +++ b/src/utils/registerHandlebarHelpers.ts @@ -1,12 +1,12 @@ +// @ts-nocheck + import * as Handlebars from 'handlebars/runtime'; export function registerHandlebarHelpers(): void { Handlebars.registerHelper('equals', function (a: string, b: string, options: Handlebars.HelperOptions): string { - // @ts-ignore return a === b ? options.fn(this) : options.inverse(this); }); Handlebars.registerHelper('notEquals', function (a: string, b: string, options: Handlebars.HelperOptions): string { - // @ts-ignore return a !== b ? options.fn(this) : options.inverse(this); }); } diff --git a/src/utils/registerHandlebarTemplates.ts b/src/utils/registerHandlebarTemplates.ts index 9734e38d..dcc4be20 100644 --- a/src/utils/registerHandlebarTemplates.ts +++ b/src/utils/registerHandlebarTemplates.ts @@ -36,6 +36,7 @@ import templateExportModel from '../templates/exportModel.hbs'; import templateExportSchema from '../templates/exportSchema.hbs'; import templateExportService from '../templates/exportService.hbs'; import templateIndex from '../templates/index.hbs'; +import partialBase from '../templates/partials/base.hbs'; import partialExportEnum from '../templates/partials/exportEnum.hbs'; import partialExportInterface from '../templates/partials/exportInterface.hbs'; import partialExportType from '../templates/partials/exportType.hbs'; @@ -125,6 +126,7 @@ export function registerHandlebarTemplates(): Templates { Handlebars.registerPartial('typeGeneric', Handlebars.template(partialTypeGeneric)); Handlebars.registerPartial('typeInterface', Handlebars.template(partialTypeInterface)); Handlebars.registerPartial('typeReference', Handlebars.template(partialTypeReference)); + Handlebars.registerPartial('base', Handlebars.template(partialBase)); // Generic functions used in 'request' file @see src/templates/core/request.hbs for more info Handlebars.registerPartial('functions/catchErrors', Handlebars.template(functionCatchErrors)); diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index 556dde3b..95625b3e 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -16,7 +16,7 @@ import { writeClientServices } from './writeClientServices'; * @param client Client object with all the models, services, etc. * @param templates Templates wrapper with all loaded Handlebars templates * @param output The relative location of the output directory - * @param httpClient The selected httpClient (fetch or XHR) + * @param httpClient The selected httpClient (fetch, xhr or node) * @param useOptions Use options or arguments functions * @param useUnionTypes Use union types instead of enums * @param exportCore: Generate core client classes @@ -56,17 +56,17 @@ export async function writeClient( if (exportServices) { await mkdir(outputPathServices); - await writeClientServices(client.services, templates, outputPathServices, useUnionTypes, useOptions); + await writeClientServices(client.services, templates, outputPathServices, httpClient, useUnionTypes, useOptions); } if (exportSchemas) { await mkdir(outputPathSchemas); - await writeClientSchemas(client.models, templates, outputPathSchemas, useUnionTypes); + await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes); } if (exportModels) { await mkdir(outputPathModels); - await writeClientModels(client.models, templates, outputPathModels, useUnionTypes); + await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes); } await writeClientIndex(client, templates, outputPath, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas); diff --git a/src/utils/writeClientModels.ts b/src/utils/writeClientModels.ts index 843495cb..0558ee4e 100644 --- a/src/utils/writeClientModels.ts +++ b/src/utils/writeClientModels.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import type { Model } from '../client/interfaces/Model'; +import { HttpClient } from '../index'; import { writeFile } from './fileSystem'; import { format } from './format'; import { Templates } from './registerHandlebarTemplates'; @@ -10,13 +11,15 @@ import { Templates } from './registerHandlebarTemplates'; * @param models Array of Models to write * @param templates The loaded handlebar templates * @param outputPath Directory to write the generated files to + * @param httpClient The selected httpClient (fetch, xhr or node) * @param useUnionTypes Use union types instead of enums */ -export async function writeClientModels(models: Model[], templates: Templates, outputPath: string, useUnionTypes: boolean): Promise { +export async function writeClientModels(models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean): Promise { for (const model of models) { const file = path.resolve(outputPath, `${model.name}.ts`); const templateResult = templates.exports.model({ ...model, + httpClient, useUnionTypes, }); await writeFile(file, format(templateResult)); diff --git a/src/utils/writeClientSchemas.ts b/src/utils/writeClientSchemas.ts index 1d6d1ce1..70041aec 100644 --- a/src/utils/writeClientSchemas.ts +++ b/src/utils/writeClientSchemas.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import type { Model } from '../client/interfaces/Model'; +import { HttpClient } from '../index'; import { writeFile } from './fileSystem'; import { format } from './format'; import { Templates } from './registerHandlebarTemplates'; @@ -10,13 +11,15 @@ import { Templates } from './registerHandlebarTemplates'; * @param models Array of Models to write * @param templates The loaded handlebar templates * @param outputPath Directory to write the generated files to + * @param httpClient The selected httpClient (fetch, xhr or node) * @param useUnionTypes Use union types instead of enums */ -export async function writeClientSchemas(models: Model[], templates: Templates, outputPath: string, useUnionTypes: boolean): Promise { +export async function writeClientSchemas(models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean): Promise { for (const model of models) { const file = path.resolve(outputPath, `$${model.name}.ts`); const templateResult = templates.exports.schema({ ...model, + httpClient, useUnionTypes, }); await writeFile(file, format(templateResult)); diff --git a/src/utils/writeClientServices.ts b/src/utils/writeClientServices.ts index 8d823285..6387356d 100644 --- a/src/utils/writeClientServices.ts +++ b/src/utils/writeClientServices.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import type { Service } from '../client/interfaces/Service'; +import { HttpClient } from '../index'; import { writeFile } from './fileSystem'; import { format } from './format'; import { Templates } from './registerHandlebarTemplates'; @@ -12,15 +13,17 @@ const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION'; * @param services Array of Services to write * @param templates The loaded handlebar templates * @param outputPath Directory to write the generated files to + * @param httpClient The selected httpClient (fetch, xhr or node) * @param useUnionTypes Use union types instead of enums * @param useOptions Use options or arguments functions */ -export async function writeClientServices(services: Service[], templates: Templates, outputPath: string, useUnionTypes: boolean, useOptions: boolean): Promise { +export async function writeClientServices(services: Service[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean): Promise { for (const service of services) { const file = path.resolve(outputPath, `${service.name}.ts`); const useVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING)); const templateResult = templates.exports.service({ ...service, + httpClient, useUnionTypes, useVersion, useOptions, diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 134f844d..840010cd 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -251,7 +251,9 @@ export async function request(options: ApiRequestOptions): Promise { catchErrors(options, result); return result; -}" +} + +" `; exports[`v2 should generate: ./test/generated/v2/index.ts 1`] = ` @@ -602,7 +604,7 @@ import type { ModelWithString } from './ModelWithString'; */ export interface ModelWithArray { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; } " @@ -935,7 +937,7 @@ exports[`v2 should generate: ./test/generated/v2/models/SimpleFile.ts 1`] = ` /** * This is a simple file */ -export type SimpleFile = any;" +export type SimpleFile = Blob;" `; exports[`v2 should generate: ./test/generated/v2/models/SimpleInteger.ts 1`] = ` @@ -1122,7 +1124,7 @@ exports[`v2 should generate: ./test/generated/v2/schemas/$ModelThatExtends.ts 1` "/* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -import type { $ModelWithString } from './$ModelWithString'; +import { $ModelWithString } from './$ModelWithString'; export const $ModelThatExtends = { properties: { @@ -1141,8 +1143,8 @@ exports[`v2 should generate: ./test/generated/v2/schemas/$ModelThatExtendsExtend "/* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -import type { $ModelWithString } from './$ModelWithString'; -import type { $ModelThatExtends } from './$ModelThatExtends'; +import { $ModelWithString } from './$ModelWithString'; +import { $ModelThatExtends } from './$ModelThatExtends'; export const $ModelThatExtendsExtends = { properties: { @@ -1481,7 +1483,7 @@ exports[`v2 should generate: ./test/generated/v2/schemas/$SimpleFile.ts 1`] = ` /* tslint:disable */ /* eslint-disable */ export const $SimpleFile = { - type: 'any', + type: 'File', };" `; @@ -2284,7 +2286,9 @@ export async function request(options: ApiRequestOptions): Promise { catchErrors(options, result); return result; -}" +} + +" `; exports[`v3 should generate: ./test/generated/v3/index.ts 1`] = ` @@ -2660,7 +2664,7 @@ import type { ModelWithString } from './ModelWithString'; */ export interface ModelWithArray { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; } " @@ -3012,7 +3016,7 @@ exports[`v3 should generate: ./test/generated/v3/models/SimpleFile.ts 1`] = ` /** * This is a simple file */ -export type SimpleFile = any;" +export type SimpleFile = Blob;" `; exports[`v3 should generate: ./test/generated/v3/models/SimpleInteger.ts 1`] = ` @@ -3199,7 +3203,7 @@ exports[`v3 should generate: ./test/generated/v3/schemas/$ModelThatExtends.ts 1` "/* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -import type { $ModelWithString } from './$ModelWithString'; +import { $ModelWithString } from './$ModelWithString'; export const $ModelThatExtends = { properties: { @@ -3218,8 +3222,8 @@ exports[`v3 should generate: ./test/generated/v3/schemas/$ModelThatExtendsExtend "/* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -import type { $ModelWithString } from './$ModelWithString'; -import type { $ModelThatExtends } from './$ModelThatExtends'; +import { $ModelWithString } from './$ModelWithString'; +import { $ModelThatExtends } from './$ModelThatExtends'; export const $ModelThatExtendsExtends = { properties: { @@ -3592,7 +3596,7 @@ exports[`v3 should generate: ./test/generated/v3/schemas/$SimpleFile.ts 1`] = ` /* tslint:disable */ /* eslint-disable */ export const $SimpleFile = { - type: 'any', + type: 'File', };" `; @@ -4301,7 +4305,7 @@ export class UploadService { * @throws ApiError */ public static async uploadFile( - file: any, + file: Blob, ): Promise { const result = await __request({ method: 'POST', diff --git a/test/e2e/scripts/compile.js b/test/e2e/scripts/compile.js index fd79d9d1..9c5d7586 100644 --- a/test/e2e/scripts/compile.js +++ b/test/e2e/scripts/compile.js @@ -4,14 +4,14 @@ const ts = require('typescript'); const path = require('path'); const os = require('os'); -function compile(dir, isBrowser) { +function compile(dir) { const baseDir = `./test/e2e/generated/${dir}/`; const tsconfig = { compilerOptions: { target: 'es6', module: 'es6', moduleResolution: 'node', - lib: isBrowser ? ['es6', 'dom'] : ['es6'] + lib: ['es6', 'dom'], }, include: ['./index.ts'], }; diff --git a/test/e2e/v2.fetch.spec.js b/test/e2e/v2.fetch.spec.js index 271cf8de..eae92e03 100644 --- a/test/e2e/v2.fetch.spec.js +++ b/test/e2e/v2.fetch.spec.js @@ -11,7 +11,7 @@ describe('v2.fetch', () => { beforeAll(async () => { await generate('v2/fetch', 'v2', 'fetch'); await copy('v2/fetch'); - compile('v2/fetch', true); + compile('v2/fetch'); await server.start('v2/fetch'); await browser.start(); }, 30000); diff --git a/test/e2e/v2.xhr.spec.js b/test/e2e/v2.xhr.spec.js index 0d7afbb6..595f411b 100644 --- a/test/e2e/v2.xhr.spec.js +++ b/test/e2e/v2.xhr.spec.js @@ -11,7 +11,7 @@ describe('v2.xhr', () => { beforeAll(async () => { await generate('v2/xhr', 'v2', 'xhr'); await copy('v2/xhr'); - compile('v2/xhr', true); + compile('v2/xhr'); await server.start('v2/xhr'); await browser.start(); }, 30000); diff --git a/test/e2e/v3.fetch.spec.js b/test/e2e/v3.fetch.spec.js index 4958a3e0..b5d45ba6 100644 --- a/test/e2e/v3.fetch.spec.js +++ b/test/e2e/v3.fetch.spec.js @@ -11,7 +11,7 @@ describe('v3.fetch', () => { beforeAll(async () => { await generate('v3/fetch', 'v3', 'fetch'); await copy('v3/fetch'); - compile('v3/fetch', true); + compile('v3/fetch'); await server.start('v3/fetch'); await browser.start(); }, 30000); diff --git a/test/e2e/v3.xhr.spec.js b/test/e2e/v3.xhr.spec.js index ab68f870..1cad0068 100644 --- a/test/e2e/v3.xhr.spec.js +++ b/test/e2e/v3.xhr.spec.js @@ -11,7 +11,7 @@ describe('v3.xhr', () => { beforeAll(async () => { await generate('v3/xhr', 'v3', 'xhr'); await copy('v3/xhr'); - compile('v3/xhr', true); + compile('v3/xhr'); await server.start('v3/xhr'); await browser.start(); }, 30000); diff --git a/tsconfig.json b/tsconfig.json index 45fe430c..13479500 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "target": "es6", "module": "es6", "moduleResolution": "node", - "lib": ["es6"], + "lib": ["es6", "dom"], "types": ["jest", "node"], "declaration": false, "declarationMap": false,