diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index dc7d808f..4a156ecd 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -88,6 +88,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, name: stri result.base = additionalProperties.base; result.template = additionalProperties.template; result.imports.push(...additionalProperties.imports); + result.imports.push('Dictionary'); } else { const additionalProperties = getModel(openApi, definition.additionalProperties); result.export = 'dictionary'; @@ -96,6 +97,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, name: stri result.template = additionalProperties.template; result.link = additionalProperties; result.imports.push(...additionalProperties.imports); + result.imports.push('Dictionary'); } return result; } diff --git a/src/templates/typescript/exportArray.hbs b/src/templates/typescript/exportArray.hbs index 509d7c21..d7eb200a 100644 --- a/src/templates/typescript/exportArray.hbs +++ b/src/templates/typescript/exportArray.hbs @@ -1,22 +1,20 @@ -{{#if description}} +{{~#if description}} /** * {{{description}}} */ -{{/if}} +{{~/if}} export type {{{name}}} = {{>type}}{{#if nullable}} | null{{/if}}; export namespace {{{name}}} { - {{#indent}} -export const schema = {{>validation}}; + export const schema = {{>validation}}; -export function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { - return schema.validate(value, { strict: true }); -} + export async function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { + return schema.validate(value, { strict: true }); + } -export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { - return schema.validateSync(value, { strict: true }); -} + export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { + return schema.validateSync(value, { strict: true }); + } {{/indent}} - } diff --git a/src/templates/typescript/exportDictionary.hbs b/src/templates/typescript/exportDictionary.hbs index 509d7c21..d7eb200a 100644 --- a/src/templates/typescript/exportDictionary.hbs +++ b/src/templates/typescript/exportDictionary.hbs @@ -1,22 +1,20 @@ -{{#if description}} +{{~#if description}} /** * {{{description}}} */ -{{/if}} +{{~/if}} export type {{{name}}} = {{>type}}{{#if nullable}} | null{{/if}}; export namespace {{{name}}} { - {{#indent}} -export const schema = {{>validation}}; + export const schema = {{>validation}}; -export function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { - return schema.validate(value, { strict: true }); -} + export async function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { + return schema.validate(value, { strict: true }); + } -export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { - return schema.validateSync(value, { strict: true }); -} + export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { + return schema.validateSync(value, { strict: true }); + } {{/indent}} - } diff --git a/src/templates/typescript/exportEnum.hbs b/src/templates/typescript/exportEnum.hbs index 2cef9a1f..8fea6577 100644 --- a/src/templates/typescript/exportEnum.hbs +++ b/src/templates/typescript/exportEnum.hbs @@ -1,26 +1,24 @@ -{{#if description}} +{{~#if description}} /** * {{{description}}} */ -{{/if}} +{{~/if}} export enum {{{name}}} { - {{#each enum}} + {{~#each enum}} {{{name}}} = {{{value}}}, - {{/each}} + {{~/each}} } export namespace {{{name}}} { - {{#indent}} -export const schema = {{>validation}}; + export const schema = {{>validation}}; -export function validate(value: any): Promise<{{{name}}}> { - return schema.validate(value, { strict: true }); -} + export async function validate(value: any): Promise<{{{name}}}> { + return schema.validate(value, { strict: true }); + } -export function validateSync(value: any): {{{name}}} { - return schema.validateSync(value, { strict: true }); -} + export function validateSync(value: any): {{{name}}} { + return schema.validateSync(value, { strict: true }); + } {{/indent}} - } diff --git a/src/templates/typescript/exportGeneric.hbs b/src/templates/typescript/exportGeneric.hbs index 509d7c21..d7eb200a 100644 --- a/src/templates/typescript/exportGeneric.hbs +++ b/src/templates/typescript/exportGeneric.hbs @@ -1,22 +1,20 @@ -{{#if description}} +{{~#if description}} /** * {{{description}}} */ -{{/if}} +{{~/if}} export type {{{name}}} = {{>type}}{{#if nullable}} | null{{/if}}; export namespace {{{name}}} { - {{#indent}} -export const schema = {{>validation}}; + export const schema = {{>validation}}; -export function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { - return schema.validate(value, { strict: true }); -} + export async function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { + return schema.validate(value, { strict: true }); + } -export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { - return schema.validateSync(value, { strict: true }); -} + export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { + return schema.validateSync(value, { strict: true }); + } {{/indent}} - } diff --git a/src/templates/typescript/exportInterface.hbs b/src/templates/typescript/exportInterface.hbs index e80560d3..f9143227 100644 --- a/src/templates/typescript/exportInterface.hbs +++ b/src/templates/typescript/exportInterface.hbs @@ -1,52 +1,47 @@ -{{#if description}} +{{~#if description}} /** * {{{description}}} */ -{{/if}} +{{~/if}} export interface {{{name}}}{{{template}}}{{#if extends}} extends {{#each extends}}{{{this}}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}} { - -{{#indent}} -{{#each properties}} -{{#if description}} -/** - * {{{description}}} - */ -{{/if}} -{{#if readOnly}}readonly {{/if}}{{{name}}}{{#unless required}}?{{/unless}}: {{>type}}{{#if nullable}} | null{{/if}}; -{{/each}} -{{/indent}} - +{{~#indent}} + {{~#each properties}} + {{~#if description}} + /** + * {{{description}}} + */ + {{~/if}} + {{#if readOnly}}readonly {{/if}}{{{name}}}{{#unless required}}?{{/unless}}: {{>type}}{{#if nullable}} | null{{/if}}; + {{~/each}} +{{~/indent}} } export namespace {{{name}}} { - {{#indent}} -{{#each enums}} -{{#if description}} -/** - * {{{description}}} - */ -{{/if}} -export enum {{{name}}} { - {{#each values}} - {{{name}}} = {{{value}}}, + {{~#each enums}} + {{~#if description}} + /** + * {{{description}}} + */ + {{~/if}} + export enum {{{name}}} { + {{~#each values}} + {{{name}}} = {{{value}}}, + {{~/each}} + } {{/each}} -} + export const schema: yup.ObjectSchema<{{{name}}}{{{template}}}> = ( + {{~#indent}} + {{>validation}} + {{~/indent}} + ); -{{/each}} -export const schema: yup.ObjectSchema<{{{name}}}{{{template}}}> = ( -{{#indent}} -{{>validation}} + export async function validate(value: any): Promise<{{{name}}}{{{template}}}> { + return schema.validate(value, { strict: true }); + } + + export function validateSync(value: any): {{{name}}}{{{template}}} { + return schema.validateSync(value, { strict: true }); + } {{/indent}} -/**/); - -export function validate(value: any): Promise<{{{name}}}{{{template}}}> { - return schema.validate(value, { strict: true }); -} - -export function validateSync(value: any): {{{name}}}{{{template}}} { - return schema.validateSync(value, { strict: true }); -} -{{/indent}} - } diff --git a/src/templates/typescript/exportReference.hbs b/src/templates/typescript/exportReference.hbs index 621f0ffa..d7eb200a 100644 --- a/src/templates/typescript/exportReference.hbs +++ b/src/templates/typescript/exportReference.hbs @@ -1,17 +1,20 @@ +{{~#if description}} +/** + * {{{description}}} + */ +{{~/if}} export type {{{name}}} = {{>type}}{{#if nullable}} | null{{/if}}; export namespace {{{name}}} { - {{#indent}} -export const schema = {{>validation}}; + export const schema = {{>validation}}; -export function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { - return schema.validate(value, { strict: true }); -} + export async function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> { + return schema.validate(value, { strict: true }); + } -export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { - return schema.validateSync(value, { strict: true }); -} + export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} { + return schema.validateSync(value, { strict: true }); + } {{/indent}} - } diff --git a/src/templates/typescript/index.hbs b/src/templates/typescript/index.hbs index df73b249..0d87e066 100644 --- a/src/templates/typescript/index.hbs +++ b/src/templates/typescript/index.hbs @@ -5,17 +5,14 @@ export { ApiError } from './core/ApiError'; export { isSuccess } from './core/isSuccess'; -export { Dictionary } from './core/Dictionary'; export { OpenAPI } from './core/OpenAPI'; {{#if models}} - -{{#each models}} +{{~#each models}} export { {{{this}}} } from './models/{{{this}}}'; -{{/each}} -{{/if}} +{{~/each}} +{{~/if}} {{#if services}} - -{{#each services}} +{{~#each services}} export { {{{this}}} } from './services/{{{this}}}'; -{{/each}} -{{/if}} +{{~/each}} +{{~/if}} diff --git a/src/templates/typescript/model.hbs b/src/templates/typescript/model.hbs index ce300c7b..82138964 100644 --- a/src/templates/typescript/model.hbs +++ b/src/templates/typescript/model.hbs @@ -2,25 +2,22 @@ /* tslint:disable */ /* eslint-disable */ /* prettier-ignore */ - {{#if imports}} -{{#each imports}} +{{~#each imports}} import { {{{this}}} } from '../models/{{{this}}}'; -{{/each}} -{{/if}} -import { Dictionary } from '../core/Dictionary'; +{{~/each}} +{{~/if}} import * as yup from 'yup'; - {{#eq export 'reference'}} -{{>exportReference}} -{{else eq export 'generic'}} -{{>exportGeneric}} -{{else eq export 'enum'}} -{{>exportEnum}} -{{else eq export 'array'}} -{{>exportArray}} -{{else eq export 'dictionary'}} -{{>exportDictionary}} -{{else eq export 'interface'}} -{{>exportInterface}} -{{/eq}} +{{~>exportReference}} +{{~else eq export 'generic'}} +{{~>exportGeneric}} +{{~else eq export 'enum'}} +{{~>exportEnum}} +{{~else eq export 'array'}} +{{~>exportArray}} +{{~else eq export 'dictionary'}} +{{~>exportDictionary}} +{{~else eq export 'interface'}} +{{~>exportInterface}} +{{~/eq}} diff --git a/src/templates/typescript/core/Dictionary.ts b/src/templates/typescript/models/Dictionary.ts similarity index 100% rename from src/templates/typescript/core/Dictionary.ts rename to src/templates/typescript/models/Dictionary.ts diff --git a/src/templates/typescript/service.hbs b/src/templates/typescript/service.hbs index f610b13b..eaa72ab4 100644 --- a/src/templates/typescript/service.hbs +++ b/src/templates/typescript/service.hbs @@ -2,78 +2,72 @@ /* tslint:disable */ /* eslint-disable */ /* prettier-ignore */ - {{#if imports}} -{{#each imports}} +{{~#each imports}} import { {{{this}}} } from '../models/{{{this}}}'; -{{/each}} -{{/if}} +{{~/each}} +{{~/if}} import { ApiError, catchGenericError } from '../core/ApiError'; import { request } from '../core/request'; -import { Dictionary } from '../core/Dictionary'; import { isValidRequiredParam } from '../core/isValidRequiredParam'; import { OpenAPI } from '../core/OpenAPI'; import { Result } from '../core/Result'; export class {{{name}}} { - - {{#each operations}} + {{~#each operations}} /** - {{#if deprecated}} + {{~#if deprecated}} * @deprecated - {{/if}} - {{#if summary}} + {{~/if}} + {{~#if summary}} * {{{summary}}} - {{/if}} - {{#if description}} + {{~/if}} + {{~#if description}} * {{{description}}} - {{/if}} - {{#if parameters}} - {{#each parameters}} + {{~/if}} + {{~#if parameters}} + {{~#each parameters}} * @param {{{name}}} {{{description}}} - {{/each}} - {{/if}} + {{~/each}} + {{~/if}} */ public static async {{{name}}}({{#each parameters}}{{{name}}}{{#unless required}}?{{/unless}}: {{{type}}}{{#if nullable}} | null{{/if}}{{#unless @last}}, {{/unless}}{{/each}}): Promise<{{{result}}}> { {{#if parameters}} - - {{#each parameters}} - {{#if required}} + {{~#each parameters}} + {{~#if required}} isValidRequiredParam({{{name}}}, '{{{name}}}'); + {{~/if}} + {{~/each}} {{/if}} - {{/each}} - {{/if}} - const result = await request({ method: '{{{method}}}', path: `{{{path}}}`, {{~#if parametersHeader~}} headers: { - {{#each parametersHeader}} + {{~#each parametersHeader}} '{{{prop}}}': {{{name}}}, - {{/each}} + {{~/each}} }, - {{~/if~}} - {{~#if parametersQuery~}} + {{~/if}} + {{~#if parametersQuery}} query: { - {{#each parametersQuery}} + {{~#each parametersQuery}} '{{{prop}}}': {{{name}}}, - {{/each}} + {{~/each}} }, - {{~/if~}} - {{~#if parametersForm~}} + {{~/if}} + {{~#if parametersForm}} formData: { - {{#each parametersForm}} + {{~#each parametersForm}} '{{{prop}}}': {{{name}}}, - {{/each}} + {{~/each}} }, - {{~/if~}} - {{~#if parametersBody~}} + {{~/if}} + {{~#if parametersBody}} body: {{{parametersBody.name}}}, - {{~/if~}} + {{~/if}} }); - {{#if errors}} - + {{~#if errors}} if (!result.ok) { switch (result.status) { {{#each errors}} @@ -81,12 +75,10 @@ export class {{{name}}} { {{/each}} } } - {{/if}} - + {{~/if}} catchGenericError(result); return result.body; } - - {{/each}} + {{~/each}} } diff --git a/src/templates/typescript/type.hbs b/src/templates/typescript/type.hbs index 922f6912..a6df9f21 100644 --- a/src/templates/typescript/type.hbs +++ b/src/templates/typescript/type.hbs @@ -1,13 +1,13 @@ -{{~#eq export 'reference'~}} -{{>typeForReference}} -{{~else eq export 'generic'~}} -{{>typeForGeneric}} -{{~else eq export 'enum'~}} -{{>typeForEnum}} -{{~else eq export 'array'~}} -{{>typeForArray}} -{{~else eq export 'dictionary'~}} -{{>typeForDictionary}} -{{~else eq export 'interface'~}} -{{>typeForInterface}} -{{~/eq~}} +{{~#eq export 'reference'}} +{{~>typeForReference}} +{{~else eq export 'generic'}} +{{~>typeForGeneric}} +{{~else eq export 'enum'}} +{{~>typeForEnum}} +{{~else eq export 'array'}} +{{~>typeForArray}} +{{~else eq export 'dictionary'}} +{{~>typeForDictionary}} +{{~else eq export 'interface'}} +{{~>typeForInterface}} +{{~/eq}} diff --git a/src/templates/typescript/typeForInterface.hbs b/src/templates/typescript/typeForInterface.hbs index 98034312..a9b1d150 100644 --- a/src/templates/typescript/typeForInterface.hbs +++ b/src/templates/typescript/typeForInterface.hbs @@ -1,12 +1,12 @@ { {{#indent}} -{{#each properties}} -{{#if description}} -/** - * {{{description}}} - */ -{{/if}} -{{#if readOnly}}readonly {{/if}}{{{name}}}{{#unless required}}?{{/unless}}: {{>type}}{{#if nullable}} | null{{/if}}, -{{/each}} + {{~#each properties}} + {{~#if description}} + /** + * {{{description}}} + */ + {{~/if}} + {{#if readOnly}}readonly {{/if}}{{{name}}}{{#unless required}}?{{/unless}}: {{>type}}{{#if nullable}} | null{{/if}}, + {{~/each}} {{/indent}} -/**/} +} diff --git a/src/templates/typescript/validationForDictionary.hbs b/src/templates/typescript/validationForDictionary.hbs index 69957acf..97a0d4f0 100644 --- a/src/templates/typescript/validationForDictionary.hbs +++ b/src/templates/typescript/validationForDictionary.hbs @@ -1,31 +1,31 @@ -{{~#if link~}} +{{~#if link}} yup.lazytype link}}>>(value => {{#indent}} -yup.objecttype link}}>>().shape( -{{#indent}} -Object.entries(value).reduce((obj, item) => ({ -{{#indent}} -...obj, -[item[0]]: {{>validation link}}, + yup.objecttype link}}>>().shape( + {{#indent}} + Object.entries(value).reduce((obj, item) => ({ + {{#indent}} + ...obj, + [item[0]]: {{>validation link}}, + {{/indent}} + }), {}) + {{/indent}} + ) {{/indent}} -/**/}), {}) -{{/indent}} -/**/) -{{/indent}} -/**/) -{{~else~}} +) +{{~else}} yup.lazy>(value => {{#indent}} -yup.object>().shape( -{{#indent}} -Object.entries(value).reduce((obj, item) => ({ -{{#indent}} -...obj, -[item[0]]: {{{base}}}.schema, + yup.object>().shape( + {{#indent}} + Object.entries(value).reduce((obj, item) => ({ + {{#indent}} + ...obj, + [item[0]]: {{{base}}}.schema, + {{/indent}} + }), {}) + {{/indent}} + ) {{/indent}} -/**/}), {}) -{{/indent}} -/**/) -{{/indent}} -/**/) -{{~/if~}} +) +{{~/if}} diff --git a/src/templates/typescript/validationForEnum.hbs b/src/templates/typescript/validationForEnum.hbs index a1166a74..83cf725a 100644 --- a/src/templates/typescript/validationForEnum.hbs +++ b/src/templates/typescript/validationForEnum.hbs @@ -1,9 +1,9 @@ yup.mixed{{#if name}}<{{{name}}}>{{/if}}().oneOf([ - {{#each enum}} - {{#if ../name}} + {{~#each enum}} + {{~#if ../name}} {{{../name}}}.{{{name}}}, - {{else}} + {{~else}} {{{value}}}, - {{/if}} - {{/each}} + {{~/if}} + {{~/each}} ]) diff --git a/src/templates/typescript/validationForInterface.hbs b/src/templates/typescript/validationForInterface.hbs index 3e7e7776..c01ff915 100644 --- a/src/templates/typescript/validationForInterface.hbs +++ b/src/templates/typescript/validationForInterface.hbs @@ -1,17 +1,17 @@ -{{#if extends}} -{{#each extends}} +{{~#if extends}} +{{~#each extends}} {{{this}}}.schema.concat( -{{/each}} -{{/if}} +{{~/each}} +{{~/if}} yup.object{{#if name}}<{{{name}}}>{{/if}}().shape({ {{#indent}} -{{#each properties}} -{{{name}}}: yup.lazy(() => {{>validation}}.default(undefined)){{#if required}}.required(){{/if}}{{#if nullable}}.nullable(){{/if}}, -{{/each}} + {{~#each properties}} + {{{name}}}: yup.lazy(() => {{>validation}}.default(undefined)){{#if required}}.required(){{/if}}{{#if nullable}}.nullable(){{/if}}, + {{~/each}} {{/indent}} -/**/}).noUnknown() -{{#if extends}} -{{#each extends}} +}).noUnknown() +{{~#if extends}} +{{~#each extends}} ) -{{/each}} -{{/if}} +{{~/each}} +{{~/if}} diff --git a/src/utils/readHandlebarsTemplate.ts b/src/utils/readHandlebarsTemplate.ts index 2498e9d1..1c8c6aae 100644 --- a/src/utils/readHandlebarsTemplate.ts +++ b/src/utils/readHandlebarsTemplate.ts @@ -16,6 +16,7 @@ export function readHandlebarsTemplate(filePath: string): Handlebars.TemplateDel strict: true, noEscape: true, preventIndent: true, + ignoreStandalone: true, knownHelpersOnly: true, knownHelpers: { indent: true, diff --git a/src/utils/registerHandlebarHelpers.ts b/src/utils/registerHandlebarHelpers.ts index 2291a5d8..8d18feb9 100644 --- a/src/utils/registerHandlebarHelpers.ts +++ b/src/utils/registerHandlebarHelpers.ts @@ -1,18 +1,20 @@ import * as Handlebars from 'handlebars'; -import { EOL } from 'os'; +import {EOL} from 'os'; export function registerHandlebarHelpers(): void { - Handlebars.registerHelper('indent', function(options: Handlebars.HelperOptions): string { + + Handlebars.registerHelper('indent', function (options: Handlebars.HelperOptions): string { // eslint-disable // prettier-ignore // @ts-ignore return options.fn(this) .split(EOL) - .map(line => ` ${line}`) - .join(EOL) + .map(line => line.replace(/^\s{4}/g, '')) + .map(line => `/**/${line}`) + .join(EOL); }); - Handlebars.registerHelper('eq', function(a: string, b: string, options: Handlebars.HelperOptions): string { + Handlebars.registerHelper('eq', function (a: string, b: string, options: Handlebars.HelperOptions): string { // eslint-disable // prettier-ignore // @ts-ignore diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index fa37966f..b513a69f 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -1,14 +1,13 @@ -import { writeClientModels } from './writeClientModels'; -import { writeClientServices } from './writeClientServices'; -import { Client } from '../client/interfaces/Client'; +import {writeClientModels} from './writeClientModels'; +import {writeClientServices} from './writeClientServices'; +import {Client} from '../client/interfaces/Client'; import * as path from 'path'; import * as mkdirp from 'mkdirp'; import * as rimraf from 'rimraf'; -import { Templates } from './readHandlebarsTemplates'; -import { writeClientIndex } from './writeClientIndex'; -import { Language } from '../index'; +import {Templates} from './readHandlebarsTemplates'; +import {writeClientIndex} from './writeClientIndex'; +import {Language} from '../index'; import * as fs from 'fs'; -import { getFileName } from './getFileName'; import * as glob from 'glob'; /** @@ -40,14 +39,13 @@ export function writeClient(client: Client, language: Language, templates: Templ throw new Error(`Could not create output directories`); } - // Copy all core files - const coreFiles = path.resolve(__dirname, `../../src/templates/${language}/core/`); - const coreFilesExt = getFileName('*', language); - const coreFilesList = glob.sync(coreFilesExt, { cwd: coreFiles }); - coreFilesList.forEach(file => + // Copy all support files + const supportFiles = path.resolve(__dirname, `../../src/templates/${language}/`); + const supportFilesList = glob.sync('**/*.{ts,js}', {cwd: supportFiles}); + supportFilesList.forEach(file => fs.copyFileSync( - path.resolve(coreFiles, file), // From input path - path.resolve(outputPathCore, file) // To output path + path.resolve(supportFiles, file), // From input path + path.resolve(outputPath, file) // To output path ) );