From eca24b0c35bd6b6ea39906172ff0ee4ffd8a6106 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Wed, 11 Dec 2019 23:06:37 +0100 Subject: [PATCH] - Added getSchema --- src/templates/javascript/index.hbs | 21 +- src/templates/javascript/model.hbs | 6 - src/templates/javascript/models/Dictionary.js | 12 ++ .../javascript/partials/schemaEnum.hbs | 5 + src/templates/typescript/core/Schema.ts | 31 +++ src/templates/typescript/index.hbs | 25 ++- src/templates/typescript/model.hbs | 3 +- src/templates/typescript/models/Dictionary.ts | 10 + .../typescript/partials/exportEnum.hbs | 2 +- .../typescript/partials/exportGeneric.hbs | 2 +- .../typescript/partials/exportInterface.hbs | 2 +- .../typescript/partials/schemaEnum.hbs | 5 + test/__snapshots__/index.spec.js.snap | 180 ++++++++++++++++++ test/index.js | 4 +- 14 files changed, 294 insertions(+), 14 deletions(-) create mode 100644 src/templates/javascript/models/Dictionary.js create mode 100644 src/templates/typescript/core/Schema.ts diff --git a/src/templates/javascript/index.hbs b/src/templates/javascript/index.hbs index 8fff728b..1342610e 100644 --- a/src/templates/javascript/index.hbs +++ b/src/templates/javascript/index.hbs @@ -8,7 +8,11 @@ export { OpenAPI } from './core/OpenAPI'; {{#if models}} {{#each models}} -export { {{{this}}} } from './models/{{{this}}}'; +import { {{{this}}} } from './models/{{{this}}}'; +{{/each}} + +{{#each models}} +export { {{{this}}} }; {{/each}} {{/if}} {{#if services}} @@ -17,3 +21,18 @@ export { {{{this}}} } from './models/{{{this}}}'; export { {{{this}}} } from './services/{{{this}}}'; {{/each}} {{/if}} +{{#if models}} + +const schemas = { +{{#each models}} + '{{{this}}}': {{{this}}}.schema, +{{/each}} +}; + +export function getSchema(schema) { + if (schemas.hasOwnProperty(schema)) { + return schemas[schema]; + } + return null; +} +{{/if}} diff --git a/src/templates/javascript/model.hbs b/src/templates/javascript/model.hbs index 529403db..530737cf 100644 --- a/src/templates/javascript/model.hbs +++ b/src/templates/javascript/model.hbs @@ -2,12 +2,6 @@ /* eslint-disable */ /* prettier-ignore */ -{{#if imports}} -{{#each imports}} -import { {{{this}}} } from '../models/{{{this}}}'; -{{/each}} -{{/if}} - {{#equals export 'interface'}} {{>exportInterface}} {{else equals export 'enum'}} diff --git a/src/templates/javascript/models/Dictionary.js b/src/templates/javascript/models/Dictionary.js new file mode 100644 index 00000000..d27ef928 --- /dev/null +++ b/src/templates/javascript/models/Dictionary.js @@ -0,0 +1,12 @@ +/* istanbul ignore file */ +/* eslint-disable */ +/* prettier-ignore */ + +export let Dictionary; +(function (Dictionary) { + + Dictionary.schema = { + type: 'Dictionary' + }; + +})(Dictionary || (Dictionary = {})); diff --git a/src/templates/javascript/partials/schemaEnum.hbs b/src/templates/javascript/partials/schemaEnum.hbs index 9a4e7ac3..b10ddfc2 100644 --- a/src/templates/javascript/partials/schemaEnum.hbs +++ b/src/templates/javascript/partials/schemaEnum.hbs @@ -1,5 +1,10 @@ { type: 'Enum', + enums: [ + {{#each enum}} + {{{value}}}, + {{/each}} + ], {{#if isReadOnly~}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/src/templates/typescript/core/Schema.ts b/src/templates/typescript/core/Schema.ts new file mode 100644 index 00000000..06eaf035 --- /dev/null +++ b/src/templates/typescript/core/Schema.ts @@ -0,0 +1,31 @@ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +/* prettier-ignore */ + +export type FieldSchema = { + readonly type?: string; + readonly isReadOnly?: boolean; + readonly isRequired?: boolean; + readonly isNullable?: boolean; + readonly format?: 'int32' | 'int64' | 'float' | 'double' | 'string' | 'boolean' | 'byte' | 'binary' | 'date' | 'date-time' | 'password'; + readonly maximum?: number; + readonly exclusiveMaximum?: boolean; + readonly minimum?: number; + readonly exclusiveMinimum?: boolean; + readonly multipleOf?: number; + readonly maxLength?: number; + readonly minLength?: number; + readonly pattern?: string; + readonly maxItems?: number; + readonly minItems?: number; + readonly uniqueItems?: boolean; + readonly maxProperties?: number; + readonly minProperties?: number; +} + +export type Schema = FieldSchema & { + readonly item?: string | Schema | FieldSchema; +} & { + readonly [K in keyof T]: Schema | FieldSchema; +} diff --git a/src/templates/typescript/index.hbs b/src/templates/typescript/index.hbs index 637cf9f4..23144713 100644 --- a/src/templates/typescript/index.hbs +++ b/src/templates/typescript/index.hbs @@ -6,10 +6,17 @@ export { ApiError } from './core/ApiError'; export { isSuccess } from './core/isSuccess'; export { OpenAPI } from './core/OpenAPI'; +import { FieldSchema, Schema } from './core/Schema'; {{#if models}} {{#each models}} -export { {{{this}}} } from './models/{{{this}}}'; +import { {{{this}}} } from './models/{{{this}}}'; +{{/each}} +{{/if}} +{{#if models}} + +{{#each models}} +export { {{{this}}} }; {{/each}} {{/if}} {{#if services}} @@ -18,3 +25,19 @@ export { {{{this}}} } from './models/{{{this}}}'; export { {{{this}}} } from './services/{{{this}}}'; {{/each}} {{/if}} +{{#if models}} + +const schemas = { +{{#each models}} + '{{{this}}}': {{{this}}}.schema, +{{/each}} +}; + +export function getSchema(schema: string): Schema | FieldSchema | null { + if (schemas.hasOwnProperty(schema)) { + return schemas[schema]; + } + return null; +} + +{{/if}} diff --git a/src/templates/typescript/model.hbs b/src/templates/typescript/model.hbs index 159d5bfc..0471144a 100644 --- a/src/templates/typescript/model.hbs +++ b/src/templates/typescript/model.hbs @@ -2,8 +2,9 @@ /* tslint:disable */ /* eslint-disable */ /* prettier-ignore */ -{{#if imports}} +import { Schema } from '../core/Schema'; +{{#if imports}} {{#each imports}} import { {{{this}}} } from '../models/{{{this}}}'; {{/each}} diff --git a/src/templates/typescript/models/Dictionary.ts b/src/templates/typescript/models/Dictionary.ts index d780f127..ab5aa54f 100644 --- a/src/templates/typescript/models/Dictionary.ts +++ b/src/templates/typescript/models/Dictionary.ts @@ -3,6 +3,16 @@ /* eslint-disable */ /* prettier-ignore */ +import { Schema } from '../core/Schema'; + export interface Dictionary { [key: string]: T; } + +export namespace Dictionary { + + export const schema: Schema = { + type: 'Dictionary' + }; + +} diff --git a/src/templates/typescript/partials/exportEnum.hbs b/src/templates/typescript/partials/exportEnum.hbs index eafe26d2..f586888d 100644 --- a/src/templates/typescript/partials/exportEnum.hbs +++ b/src/templates/typescript/partials/exportEnum.hbs @@ -11,6 +11,6 @@ export enum {{{name}}} { export namespace {{{name}}} { - export const schema = {{>schema}}; + export const schema: Schema<{{{name}}}> = {{>schema}}; } diff --git a/src/templates/typescript/partials/exportGeneric.hbs b/src/templates/typescript/partials/exportGeneric.hbs index 006dbb85..ce4c70d9 100644 --- a/src/templates/typescript/partials/exportGeneric.hbs +++ b/src/templates/typescript/partials/exportGeneric.hbs @@ -7,6 +7,6 @@ export type {{{name}}} = {{>type}}; export namespace {{{name}}} { - export const schema = {{>schema}}; + export const schema: Schema<{{{name}}}> = {{>schema}}; } diff --git a/src/templates/typescript/partials/exportInterface.hbs b/src/templates/typescript/partials/exportInterface.hbs index dd49884f..5269a046 100644 --- a/src/templates/typescript/partials/exportInterface.hbs +++ b/src/templates/typescript/partials/exportInterface.hbs @@ -31,6 +31,6 @@ export namespace {{{name}}} { {{/each}} {{/if}} - export const schema = {{>schema}}; + export const schema: Schema<{{{name}}}> = {{>schema}}; } diff --git a/src/templates/typescript/partials/schemaEnum.hbs b/src/templates/typescript/partials/schemaEnum.hbs index 9a4e7ac3..b10ddfc2 100644 --- a/src/templates/typescript/partials/schemaEnum.hbs +++ b/src/templates/typescript/partials/schemaEnum.hbs @@ -1,5 +1,10 @@ { type: 'Enum', + enums: [ + {{#each enum}} + {{{value}}}, + {{/each}} + ], {{#if isReadOnly~}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index b8d8eb60..39083e67 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -687,6 +687,11 @@ export let EnumFromDescription; EnumFromDescription.schema = { type: 'Enum', + enums: [ + 3, + 1, + 2, + ], }; })(EnumFromDescription || (EnumFromDescription = {}));" @@ -710,6 +715,11 @@ export let EnumWithNumbers; EnumWithNumbers.schema = { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }; })(EnumWithNumbers || (EnumWithNumbers = {}));" @@ -733,6 +743,11 @@ export let EnumWithStrings; EnumWithStrings.schema = { type: 'Enum', + enums: [ + 'Error', + 'Success', + 'Warning', + ], }; })(EnumWithStrings || (EnumWithStrings = {}));" @@ -986,6 +1001,11 @@ export let ModelWithEnum; ModelWithEnum.schema = { test: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }; @@ -1016,6 +1036,11 @@ export let ModelWithEnumFromDescription; ModelWithEnumFromDescription.schema = { test: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }; @@ -1084,24 +1109,44 @@ export let ModelWithNestedEnums; type: 'Array', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, arrayWithEnum: { type: 'Array', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnum: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnumFromDescription: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, }; @@ -2496,6 +2541,11 @@ export namespace EnumFromDescription { export const schema = { type: 'Enum', + enums: [ + 3, + 1, + 2, + ], }; }" @@ -2520,6 +2570,11 @@ export namespace EnumWithNumbers { export const schema = { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }; }" @@ -2544,6 +2599,11 @@ export namespace EnumWithStrings { export const schema = { type: 'Enum', + enums: [ + 'Error', + 'Success', + 'Warning', + ], }; }" @@ -2845,6 +2905,11 @@ export namespace ModelWithEnum { export const schema = { test: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }; @@ -2881,6 +2946,11 @@ export namespace ModelWithEnumFromDescription { export const schema = { test: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }; @@ -2966,24 +3036,44 @@ export namespace ModelWithNestedEnums { type: 'Array', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, arrayWithEnum: { type: 'Array', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnum: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnumFromDescription: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, }; @@ -4339,6 +4429,11 @@ export let EnumFromDescription; EnumFromDescription.schema = { type: 'Enum', + enums: [ + 3, + 1, + 2, + ], }; })(EnumFromDescription || (EnumFromDescription = {}));" @@ -4362,6 +4457,11 @@ export let EnumWithNumbers; EnumWithNumbers.schema = { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }; })(EnumWithNumbers || (EnumWithNumbers = {}));" @@ -4385,6 +4485,11 @@ export let EnumWithStrings; EnumWithStrings.schema = { type: 'Enum', + enums: [ + 'Error', + 'Success', + 'Warning', + ], }; })(EnumWithStrings || (EnumWithStrings = {}));" @@ -4638,6 +4743,11 @@ export let ModelWithEnum; ModelWithEnum.schema = { Test: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }; @@ -4668,6 +4778,11 @@ export let ModelWithEnumFromDescription; ModelWithEnumFromDescription.schema = { Test: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }; @@ -4736,24 +4851,44 @@ export let ModelWithNestedEnums; type: 'Array', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, arrayWithEnum: { type: 'Array', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnum: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnumFromDescription: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, }; @@ -6161,6 +6296,11 @@ export namespace EnumFromDescription { export const schema = { type: 'Enum', + enums: [ + 3, + 1, + 2, + ], }; }" @@ -6185,6 +6325,11 @@ export namespace EnumWithNumbers { export const schema = { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }; }" @@ -6209,6 +6354,11 @@ export namespace EnumWithStrings { export const schema = { type: 'Enum', + enums: [ + 'Error', + 'Success', + 'Warning', + ], }; }" @@ -6510,6 +6660,11 @@ export namespace ModelWithEnum { export const schema = { Test: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }; @@ -6546,6 +6701,11 @@ export namespace ModelWithEnumFromDescription { export const schema = { Test: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }; @@ -6631,24 +6791,44 @@ export namespace ModelWithNestedEnums { type: 'Array', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, arrayWithEnum: { type: 'Array', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnum: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 'Success', + 'Warning', + 'Error', + ], }, }, dictionaryWithEnumFromDescription: { type: 'Dictionary', item: { type: 'Enum', + enums: [ + 1, + 2, + 3, + ], }, }, }; diff --git a/test/index.js b/test/index.js index e88c3309..b87a616a 100644 --- a/test/index.js +++ b/test/index.js @@ -28,5 +28,5 @@ OpenAPI.generate( OpenAPI.HttpClient.XHR, ); -// OpenAPI.compile('./test/result/v2/typescript/'); -// OpenAPI.compile('./test/result/v3/typescript/'); +OpenAPI.compile('./test/result/v2/typescript/'); +OpenAPI.compile('./test/result/v3/typescript/');