diff --git a/rollup.config.js b/rollup.config.js index 4e97a3de..824b5710 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -32,13 +32,13 @@ const handlebarsPlugin = () => ({ preventIndent: true, knownHelpersOnly: true, knownHelpers: { - escapeSinglequotes: true, equals: true, notEquals: true, containsSpaces: true, union: true, intersection: true, enumerator: true, + escapeQuotes: true, }, }); return `export default ${templateSpec};`; diff --git a/src/openApi/v2/parser/getOperationParameterName.ts b/src/openApi/v2/parser/getOperationParameterName.ts index e21e83c8..0954d736 100644 --- a/src/openApi/v2/parser/getOperationParameterName.ts +++ b/src/openApi/v2/parser/getOperationParameterName.ts @@ -1,6 +1,7 @@ import camelCase from 'camelcase'; -const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; +const reservedWords = + /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; /** * Replaces any invalid characters from a parameter name. diff --git a/src/openApi/v3/parser/getModelProperties.ts b/src/openApi/v3/parser/getModelProperties.ts index cad55ae9..ccafe821 100644 --- a/src/openApi/v3/parser/getModelProperties.ts +++ b/src/openApi/v3/parser/getModelProperties.ts @@ -23,7 +23,19 @@ export function getModelProperties( if (definition.properties.hasOwnProperty(propertyName)) { const property = definition.properties[propertyName]; const propertyRequired = !!definition.required?.includes(propertyName); - const propertyValues = { + const propertyValues: Omit< + Model, + | 'export' + | 'type' + | 'base' + | 'template' + | 'link' + | 'isNullable' + | 'imports' + | 'enum' + | 'enums' + | 'properties' + > = { name: escapeName(propertyName), description: getComment(property.description), isDefinition: false, diff --git a/src/openApi/v3/parser/getOperationParameterName.ts b/src/openApi/v3/parser/getOperationParameterName.ts index e21e83c8..0954d736 100644 --- a/src/openApi/v3/parser/getOperationParameterName.ts +++ b/src/openApi/v3/parser/getOperationParameterName.ts @@ -1,6 +1,7 @@ import camelCase from 'camelcase'; -const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; +const reservedWords = + /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; /** * Replaces any invalid characters from a parameter name. diff --git a/src/templates/core/node/request.hbs b/src/templates/core/node/request.hbs index 28ed21e7..67ea66be 100644 --- a/src/templates/core/node/request.hbs +++ b/src/templates/core/node/request.hbs @@ -24,6 +24,9 @@ import { OpenAPI } from './OpenAPI'; {{>functions/isBlob}} +{{>functions/isFormData}} + + {{>functions/base64}} diff --git a/src/templates/core/xhr/request.hbs b/src/templates/core/xhr/request.hbs index 274c00fb..d08a68d9 100644 --- a/src/templates/core/xhr/request.hbs +++ b/src/templates/core/xhr/request.hbs @@ -19,6 +19,9 @@ import { OpenAPI } from './OpenAPI'; {{>functions/isBlob}} +{{>functions/isFormData}} + + {{>functions/isSuccess}} diff --git a/src/templates/index.hbs b/src/templates/index.hbs index 74720e5a..97355941 100644 --- a/src/templates/index.hbs +++ b/src/templates/index.hbs @@ -2,7 +2,7 @@ {{#if @root.exportCore}} export { ApiError } from './core/ApiError'; -export { CancelablePromise } from './core/CancelablePromise'; +export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI } from './core/OpenAPI'; {{/if}} {{#if @root.exportModels}} diff --git a/src/templates/partials/schemaComposition.hbs b/src/templates/partials/schemaComposition.hbs index 1cceecf0..005ea046 100644 --- a/src/templates/partials/schemaComposition.hbs +++ b/src/templates/partials/schemaComposition.hbs @@ -1,7 +1,7 @@ { type: '{{export}}', {{#if description}} - description: '{{{escapeSinglequotes description}}}', + description: '{{{escapeQuotes description}}}', {{/if}} contains: [{{#each properties}}{{>schema}}{{#unless @last}}, {{/unless}}{{/each}}], {{#if isReadOnly}} diff --git a/src/templates/partials/schemaGeneric.hbs b/src/templates/partials/schemaGeneric.hbs index cff96b31..54f13c0e 100644 --- a/src/templates/partials/schemaGeneric.hbs +++ b/src/templates/partials/schemaGeneric.hbs @@ -3,7 +3,7 @@ type: '{{{type}}}', {{/if}} {{#if description}} - description: '{{{escapeSinglequotes description}}}', + description: '{{{escapeQuotes description}}}', {{/if}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, diff --git a/src/templates/partials/schemaInterface.hbs b/src/templates/partials/schemaInterface.hbs index 65fd3316..0488adae 100644 --- a/src/templates/partials/schemaInterface.hbs +++ b/src/templates/partials/schemaInterface.hbs @@ -1,6 +1,6 @@ { {{#if description}} - description: '{{{escapeSinglequotes description}}}', + description: '{{{escapeQuotes description}}}', {{/if}} properties: { {{#if properties}} diff --git a/src/utils/discriminator.ts b/src/utils/discriminator.ts index cf78d470..2f5e532c 100644 --- a/src/utils/discriminator.ts +++ b/src/utils/discriminator.ts @@ -1,16 +1,16 @@ -import { Model } from '../client/interfaces/Model'; -import { OpenApi } from '../openApi/v3/interfaces/OpenApi'; -import { OpenApiDiscriminator } from '../openApi/v3/interfaces/OpenApiDiscriminator'; +import type { Model } from '../client/interfaces/Model'; +import type { OpenApi } from '../openApi/v3/interfaces/OpenApi'; +import type { OpenApiDiscriminator } from '../openApi/v3/interfaces/OpenApiDiscriminator'; import { stripNamespace } from '../openApi/v3/parser/stripNamespace'; -import { Dictionary } from './types'; +import type { Dictionary } from './types'; -const inverseDictionary = (mapObj: Dictionary) => { +function inverseDictionary(map: Dictionary): Dictionary { const m2: Dictionary = {}; - for (const key in mapObj) { - m2[mapObj[key]] = key; + for (const key in map) { + m2[map[key]] = key; } return m2; -}; +} export function findOneOfParentDiscriminator(openApi: OpenApi, parent?: Model): OpenApiDiscriminator | undefined { if (openApi.components) { @@ -20,9 +20,7 @@ export function findOneOfParentDiscriminator(openApi: OpenApi, parent?: Model): if (parent && schema.oneOf?.length && schema.discriminator) { const isPartOf = schema.oneOf - .map(definition => { - return definition.$ref && stripNamespace(definition.$ref) == parent.name; - }) + .map(definition => definition.$ref && stripNamespace(definition.$ref) === parent.name) .filter(Boolean).length > 0; if (isPartOf) { return schema.discriminator; @@ -31,7 +29,7 @@ export function findOneOfParentDiscriminator(openApi: OpenApi, parent?: Model): } } } - return undefined; + return; } export function mapPropertyValue(discriminator: OpenApiDiscriminator, parent: Model): string { diff --git a/src/utils/registerHandlebarHelpers.spec.ts b/src/utils/registerHandlebarHelpers.spec.ts index 1c9ecdad..b8bd7bcb 100644 --- a/src/utils/registerHandlebarHelpers.spec.ts +++ b/src/utils/registerHandlebarHelpers.spec.ts @@ -16,5 +16,7 @@ describe('registerHandlebarHelpers', () => { expect(helpers).toContain('containsSpaces'); expect(helpers).toContain('union'); expect(helpers).toContain('intersection'); + expect(helpers).toContain('enumerator'); + expect(helpers).toContain('escapeQuotes'); }); }); diff --git a/src/utils/registerHandlebarHelpers.ts b/src/utils/registerHandlebarHelpers.ts index bea65337..a52c2a09 100644 --- a/src/utils/registerHandlebarHelpers.ts +++ b/src/utils/registerHandlebarHelpers.ts @@ -10,10 +10,6 @@ export function registerHandlebarHelpers(root: { useOptions: boolean; useUnionTypes: boolean; }): void { - Handlebars.registerHelper('escapeQuotes', function (value: string): string { - return value.replace(/(')/g, '\\$1'); - }); - Handlebars.registerHelper( 'equals', function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string { @@ -83,4 +79,8 @@ export function registerHandlebarHelpers(root: { ); } ); + + Handlebars.registerHelper('escapeQuotes', function (value: string): string { + return value.replace(/(')/g, '\\$1'); + }); } diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 5ec27b08..8dadde47 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -492,7 +492,7 @@ exports[`v2 should generate: ./test/generated/v2/index.ts 1`] = ` /* tslint:disable */ /* eslint-disable */ export { ApiError } from './core/ApiError'; -export { CancelablePromise } from './core/CancelablePromise'; +export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI } from './core/OpenAPI'; export type { ArrayWithArray } from './models/ArrayWithArray'; @@ -3196,7 +3196,7 @@ exports[`v3 should generate: ./test/generated/v3/index.ts 1`] = ` /* tslint:disable */ /* eslint-disable */ export { ApiError } from './core/ApiError'; -export { CancelablePromise } from './core/CancelablePromise'; +export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI } from './core/OpenAPI'; export type { ArrayWithArray } from './models/ArrayWithArray'; @@ -3224,6 +3224,7 @@ export type { EnumFromDescription } from './models/EnumFromDescription'; export { EnumWithExtensions } from './models/EnumWithExtensions'; export { EnumWithNumbers } from './models/EnumWithNumbers'; export { EnumWithStrings } from './models/EnumWithStrings'; +export type { File } from './models/File'; export type { ModelCircle } from './models/ModelCircle'; export type { ModelSquare } from './models/ModelSquare'; export type { ModelThatExtends } from './models/ModelThatExtends'; @@ -3278,6 +3279,7 @@ export { $EnumFromDescription } from './schemas/$EnumFromDescription'; export { $EnumWithExtensions } from './schemas/$EnumWithExtensions'; export { $EnumWithNumbers } from './schemas/$EnumWithNumbers'; export { $EnumWithStrings } from './schemas/$EnumWithStrings'; +export { $File } from './schemas/$File'; export { $ModelCircle } from './schemas/$ModelCircle'; export { $ModelSquare } from './schemas/$ModelSquare'; export { $ModelThatExtends } from './schemas/$ModelThatExtends'; @@ -3718,6 +3720,21 @@ export enum EnumWithStrings { }" `; +exports[`v3 should generate: ./test/generated/v3/models/File.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +export type File = { + readonly id?: string; + readonly updated_at?: string; + readonly created_at?: string; + mime: string; + readonly file?: string; +} +" +`; + exports[`v3 should generate: ./test/generated/v3/models/ModelCircle.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -4539,6 +4556,7 @@ exports[`v3 should generate: ./test/generated/v3/schemas/$CompositionWithOneOfDi /* eslint-disable */ export const $CompositionWithOneOfDiscriminator = { type: 'one-of', + description: 'This is a model with one property with a \\\\'one of\\\\' relationship where the options are not $ref', contains: [{ type: 'ModelCircle', }, { @@ -4657,11 +4675,48 @@ export const $EnumWithStrings = { } as const;" `; +exports[`v3 should generate: ./test/generated/v3/schemas/$File.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $File = { + properties: { + id: { + type: 'string', + isReadOnly: true, + minLength: 1, + }, + updated_at: { + type: 'string', + isReadOnly: true, + format: 'date-time', + }, + created_at: { + type: 'string', + isReadOnly: true, + format: 'date-time', + }, + mime: { + type: 'string', + isRequired: true, + maxLength: 24, + minLength: 1, + }, + file: { + type: 'string', + isReadOnly: true, + format: 'uri', + }, + }, +} as const;" +`; + exports[`v3 should generate: ./test/generated/v3/schemas/$ModelCircle.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ export const $ModelCircle = { + description: 'Circle', properties: { kind: { type: 'string', @@ -4679,6 +4734,7 @@ exports[`v3 should generate: ./test/generated/v3/schemas/$ModelSquare.ts 1`] = ` /* tslint:disable */ /* eslint-disable */ export const $ModelSquare = { + description: 'Square', properties: { kind: { type: 'string',