From cde9e0e9bef4f0cac6c64fd0777bba8b2bfd7bb4 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Sat, 22 Aug 2020 14:42:56 +0200 Subject: [PATCH] - Fixed export for isolatedModules --- src/templates/index.hbs | 12 ++- src/utils/getModelNames.spec.ts | 14 +-- src/utils/getServiceNames.spec.ts | 15 +-- src/utils/sortModelsByName.spec.ts | 65 ++++++++++++ src/utils/sortModelsByName.ts | 9 ++ src/utils/sortServicesByName.spec.ts | 27 +++++ src/utils/sortServicesByName.ts | 9 ++ src/utils/writeClientIndex.ts | 8 +- test/__snapshots__/index.spec.js.snap | 144 +++++++++++++------------- tsconfig.json | 3 +- 10 files changed, 212 insertions(+), 94 deletions(-) create mode 100644 src/utils/sortModelsByName.spec.ts create mode 100644 src/utils/sortModelsByName.ts create mode 100644 src/utils/sortServicesByName.spec.ts create mode 100644 src/utils/sortServicesByName.ts diff --git a/src/templates/index.hbs b/src/templates/index.hbs index c3e325f2..7633a13d 100644 --- a/src/templates/index.hbs +++ b/src/templates/index.hbs @@ -11,7 +11,13 @@ export { OpenAPI } from './core/OpenAPI'; {{#if models}} {{#each models}} -export { {{{this}}} } from './models/{{{this}}}'; +{{#if enum}} +export { {{{name}}} } from './models/{{{name}}}'; +{{else if enums}} +export { {{{name}}} } from './models/{{{name}}}'; +{{else}} +export type { {{{name}}} } from './models/{{{name}}}'; +{{/if}} {{/each}} {{/if}} {{/if}} @@ -19,7 +25,7 @@ export { {{{this}}} } from './models/{{{this}}}'; {{#if models}} {{#each models}} -export { ${{{this}}} } from './schemas/${{{this}}}'; +export { ${{{name}}} } from './schemas/${{{name}}}'; {{/each}} {{/if}} {{/if}} @@ -27,7 +33,7 @@ export { ${{{this}}} } from './schemas/${{{this}}}'; {{#if services}} {{#each services}} -export { {{{this}}} } from './services/{{{this}}}'; +export { {{{name}}} } from './services/{{{name}}}'; {{/each}} {{/if}} {{/if}} diff --git a/src/utils/getModelNames.spec.ts b/src/utils/getModelNames.spec.ts index 5769aef0..ff006a09 100644 --- a/src/utils/getModelNames.spec.ts +++ b/src/utils/getModelNames.spec.ts @@ -3,8 +3,7 @@ import { getModelNames } from './getModelNames'; describe('getModelNames', () => { it('should return sorted list', () => { - const models: Model[] = []; - models.push({ + const john: Model = { export: 'interface', name: 'John', type: 'John', @@ -21,8 +20,8 @@ describe('getModelNames', () => { enum: [], enums: [], properties: [], - }); - models.push({ + }; + const jane: Model = { export: 'interface', name: 'Jane', type: 'Jane', @@ -39,8 +38,8 @@ describe('getModelNames', () => { enum: [], enums: [], properties: [], - }); - models.push({ + }; + const doe: Model = { export: 'interface', name: 'Doe', type: 'Doe', @@ -57,7 +56,8 @@ describe('getModelNames', () => { enum: [], enums: [], properties: [], - }); + }; + const models: Model[] = [john, jane, doe]; expect(getModelNames([])).toEqual([]); expect(getModelNames(models)).toEqual(['Doe', 'Jane', 'John']); diff --git a/src/utils/getServiceNames.spec.ts b/src/utils/getServiceNames.spec.ts index 2396f4aa..a6df31d3 100644 --- a/src/utils/getServiceNames.spec.ts +++ b/src/utils/getServiceNames.spec.ts @@ -3,22 +3,23 @@ import { getServiceNames } from './getServiceNames'; describe('getServiceNames', () => { it('should return sorted list', () => { - const services: Service[] = []; - services.push({ + const john: Service = { name: 'John', operations: [], imports: [], - }); - services.push({ + }; + const jane: Service = { name: 'Jane', operations: [], imports: [], - }); - services.push({ + }; + const doe: Service = { name: 'Doe', operations: [], imports: [], - }); + }; + + const services: Service[] = [john, jane, doe]; expect(getServiceNames([])).toEqual([]); expect(getServiceNames(services)).toEqual(['Doe', 'Jane', 'John']); diff --git a/src/utils/sortModelsByName.spec.ts b/src/utils/sortModelsByName.spec.ts new file mode 100644 index 00000000..764e6aeb --- /dev/null +++ b/src/utils/sortModelsByName.spec.ts @@ -0,0 +1,65 @@ +import { Model } from '../client/interfaces/Model'; +import { sortModelsByName } from './sortModelsByName'; + +describe('sortModelsByName', () => { + it('should return sorted list', () => { + const john: Model = { + export: 'interface', + name: 'John', + type: 'John', + base: 'John', + template: null, + link: null, + description: null, + isDefinition: true, + isReadOnly: false, + isRequired: false, + isNullable: false, + imports: [], + extends: [], + enum: [], + enums: [], + properties: [], + }; + const jane: Model = { + export: 'interface', + name: 'Jane', + type: 'Jane', + base: 'Jane', + template: null, + link: null, + description: null, + isDefinition: true, + isReadOnly: false, + isRequired: false, + isNullable: false, + imports: [], + extends: [], + enum: [], + enums: [], + properties: [], + }; + const doe: Model = { + export: 'interface', + name: 'Doe', + type: 'Doe', + base: 'Doe', + template: null, + link: null, + description: null, + isDefinition: true, + isReadOnly: false, + isRequired: false, + isNullable: false, + imports: [], + extends: [], + enum: [], + enums: [], + properties: [], + }; + const models: Model[] = [john, jane, doe]; + + expect(sortModelsByName([])).toEqual([]); + expect(sortModelsByName(models)).toEqual([doe, jane, john]); + }); +}); diff --git a/src/utils/sortModelsByName.ts b/src/utils/sortModelsByName.ts new file mode 100644 index 00000000..8013cf81 --- /dev/null +++ b/src/utils/sortModelsByName.ts @@ -0,0 +1,9 @@ +import { Model } from '../client/interfaces/Model'; + +export function sortModelsByName(models: Model[]): Model[] { + return models.sort((a, b) => { + const nameA = a.name.toLowerCase(); + const nameB = b.name.toLowerCase(); + return nameA.localeCompare(nameB, 'en'); + }); +} diff --git a/src/utils/sortServicesByName.spec.ts b/src/utils/sortServicesByName.spec.ts new file mode 100644 index 00000000..3fdcc8a6 --- /dev/null +++ b/src/utils/sortServicesByName.spec.ts @@ -0,0 +1,27 @@ +import { Service } from '../client/interfaces/Service'; +import { sortServicesByName } from './sortServicesByName'; + +describe('sortServicesByName', () => { + it('should return sorted list', () => { + const john: Service = { + name: 'John', + operations: [], + imports: [], + }; + const jane: Service = { + name: 'Jane', + operations: [], + imports: [], + }; + const doe: Service = { + name: 'Doe', + operations: [], + imports: [], + }; + + const services: Service[] = [john, jane, doe]; + + expect(sortServicesByName([])).toEqual([]); + expect(sortServicesByName(services)).toEqual([doe, jane, john]); + }); +}); diff --git a/src/utils/sortServicesByName.ts b/src/utils/sortServicesByName.ts new file mode 100644 index 00000000..1dc1bfbd --- /dev/null +++ b/src/utils/sortServicesByName.ts @@ -0,0 +1,9 @@ +import { Service } from '../client/interfaces/Service'; + +export function sortServicesByName(services: Service[]): Service[] { + return services.sort((a, b) => { + const nameA = a.name.toLowerCase(); + const nameB = b.name.toLowerCase(); + return nameA.localeCompare(nameB, 'en'); + }); +} diff --git a/src/utils/writeClientIndex.ts b/src/utils/writeClientIndex.ts index c29988f7..e7a3cc44 100644 --- a/src/utils/writeClientIndex.ts +++ b/src/utils/writeClientIndex.ts @@ -2,9 +2,9 @@ import * as path from 'path'; import { Client } from '../client/interfaces/Client'; import { writeFile } from './fileSystem'; -import { getModelNames } from './getModelNames'; -import { getServiceNames } from './getServiceNames'; import { Templates } from './registerHandlebarTemplates'; +import { sortModelsByName } from './sortModelsByName'; +import { sortServicesByName } from './sortServicesByName'; /** * Generate the OpenAPI client index file using the Handlebar template and write it to disk. @@ -36,8 +36,8 @@ export async function writeClientIndex( exportSchemas, server: client.server, version: client.version, - models: getModelNames(client.models), - services: getServiceNames(client.services), + models: sortModelsByName(client.models), + services: sortServicesByName(client.services), }) ); } diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 61254674..4c212bd1 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -470,47 +470,47 @@ export { ApiError } from './core/ApiError'; export { isSuccess } from './core/isSuccess'; export { OpenAPI } from './core/OpenAPI'; -export { ArrayWithArray } from './models/ArrayWithArray'; -export { ArrayWithBooleans } from './models/ArrayWithBooleans'; -export { ArrayWithNumbers } from './models/ArrayWithNumbers'; -export { ArrayWithProperties } from './models/ArrayWithProperties'; -export { ArrayWithReferences } from './models/ArrayWithReferences'; -export { ArrayWithStrings } from './models/ArrayWithStrings'; -export { DictionaryWithArray } from './models/DictionaryWithArray'; -export { DictionaryWithDictionary } from './models/DictionaryWithDictionary'; -export { DictionaryWithProperties } from './models/DictionaryWithProperties'; -export { DictionaryWithReference } from './models/DictionaryWithReference'; -export { DictionaryWithString } from './models/DictionaryWithString'; +export type { ArrayWithArray } from './models/ArrayWithArray'; +export type { ArrayWithBooleans } from './models/ArrayWithBooleans'; +export type { ArrayWithNumbers } from './models/ArrayWithNumbers'; +export type { ArrayWithProperties } from './models/ArrayWithProperties'; +export type { ArrayWithReferences } from './models/ArrayWithReferences'; +export type { ArrayWithStrings } from './models/ArrayWithStrings'; +export type { DictionaryWithArray } from './models/DictionaryWithArray'; +export type { DictionaryWithDictionary } from './models/DictionaryWithDictionary'; +export type { DictionaryWithProperties } from './models/DictionaryWithProperties'; +export type { DictionaryWithReference } from './models/DictionaryWithReference'; +export type { DictionaryWithString } from './models/DictionaryWithString'; export { EnumFromDescription } from './models/EnumFromDescription'; export { EnumWithExtensions } from './models/EnumWithExtensions'; export { EnumWithNumbers } from './models/EnumWithNumbers'; export { EnumWithStrings } from './models/EnumWithStrings'; -export { ModelLink } from './models/ModelLink'; -export { ModelThatExtends } from './models/ModelThatExtends'; -export { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends'; -export { ModelWithArray } from './models/ModelWithArray'; -export { ModelWithBoolean } from './models/ModelWithBoolean'; -export { ModelWithCircularReference } from './models/ModelWithCircularReference'; -export { ModelWithDictionary } from './models/ModelWithDictionary'; -export { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports'; -export { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties'; +export type { ModelLink } from './models/ModelLink'; +export type { ModelThatExtends } from './models/ModelThatExtends'; +export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends'; +export type { ModelWithArray } from './models/ModelWithArray'; +export type { ModelWithBoolean } from './models/ModelWithBoolean'; +export type { ModelWithCircularReference } from './models/ModelWithCircularReference'; +export type { ModelWithDictionary } from './models/ModelWithDictionary'; +export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports'; +export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties'; export { ModelWithEnum } from './models/ModelWithEnum'; export { ModelWithEnumFromDescription } from './models/ModelWithEnumFromDescription'; -export { ModelWithInteger } from './models/ModelWithInteger'; -export { ModelWithLink } from './models/ModelWithLink'; -export { ModelWithNestedEnums } from './models/ModelWithNestedEnums'; -export { ModelWithNestedProperties } from './models/ModelWithNestedProperties'; -export { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties'; -export { ModelWithPattern } from './models/ModelWithPattern'; -export { ModelWithProperties } from './models/ModelWithProperties'; -export { ModelWithReference } from './models/ModelWithReference'; -export { ModelWithString } from './models/ModelWithString'; -export { MultilineComment } from './models/MultilineComment'; -export { SimpleBoolean } from './models/SimpleBoolean'; -export { SimpleFile } from './models/SimpleFile'; -export { SimpleInteger } from './models/SimpleInteger'; -export { SimpleReference } from './models/SimpleReference'; -export { SimpleString } from './models/SimpleString'; +export type { ModelWithInteger } from './models/ModelWithInteger'; +export type { ModelWithLink } from './models/ModelWithLink'; +export type { ModelWithNestedEnums } from './models/ModelWithNestedEnums'; +export type { ModelWithNestedProperties } from './models/ModelWithNestedProperties'; +export type { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties'; +export type { ModelWithPattern } from './models/ModelWithPattern'; +export type { ModelWithProperties } from './models/ModelWithProperties'; +export type { ModelWithReference } from './models/ModelWithReference'; +export type { ModelWithString } from './models/ModelWithString'; +export type { MultilineComment } from './models/MultilineComment'; +export type { SimpleBoolean } from './models/SimpleBoolean'; +export type { SimpleFile } from './models/SimpleFile'; +export type { SimpleInteger } from './models/SimpleInteger'; +export type { SimpleReference } from './models/SimpleReference'; +export type { SimpleString } from './models/SimpleString'; export { $ArrayWithArray } from './schemas/$ArrayWithArray'; export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans'; @@ -2957,49 +2957,49 @@ export { ApiError } from './core/ApiError'; export { isSuccess } from './core/isSuccess'; export { OpenAPI } from './core/OpenAPI'; -export { ArrayWithArray } from './models/ArrayWithArray'; -export { ArrayWithBooleans } from './models/ArrayWithBooleans'; -export { ArrayWithNumbers } from './models/ArrayWithNumbers'; -export { ArrayWithProperties } from './models/ArrayWithProperties'; -export { ArrayWithReferences } from './models/ArrayWithReferences'; -export { ArrayWithStrings } from './models/ArrayWithStrings'; -export { DictionaryWithArray } from './models/DictionaryWithArray'; -export { DictionaryWithDictionary } from './models/DictionaryWithDictionary'; -export { DictionaryWithProperties } from './models/DictionaryWithProperties'; -export { DictionaryWithReference } from './models/DictionaryWithReference'; -export { DictionaryWithString } from './models/DictionaryWithString'; +export type { ArrayWithArray } from './models/ArrayWithArray'; +export type { ArrayWithBooleans } from './models/ArrayWithBooleans'; +export type { ArrayWithNumbers } from './models/ArrayWithNumbers'; +export type { ArrayWithProperties } from './models/ArrayWithProperties'; +export type { ArrayWithReferences } from './models/ArrayWithReferences'; +export type { ArrayWithStrings } from './models/ArrayWithStrings'; +export type { DictionaryWithArray } from './models/DictionaryWithArray'; +export type { DictionaryWithDictionary } from './models/DictionaryWithDictionary'; +export type { DictionaryWithProperties } from './models/DictionaryWithProperties'; +export type { DictionaryWithReference } from './models/DictionaryWithReference'; +export type { DictionaryWithString } from './models/DictionaryWithString'; export { EnumFromDescription } from './models/EnumFromDescription'; export { EnumWithExtensions } from './models/EnumWithExtensions'; export { EnumWithNumbers } from './models/EnumWithNumbers'; export { EnumWithStrings } from './models/EnumWithStrings'; -export { ModelLink } from './models/ModelLink'; -export { ModelThatExtends } from './models/ModelThatExtends'; -export { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends'; -export { ModelWithAnyOf } from './models/ModelWithAnyOf'; -export { ModelWithArray } from './models/ModelWithArray'; -export { ModelWithBoolean } from './models/ModelWithBoolean'; -export { ModelWithCircularReference } from './models/ModelWithCircularReference'; -export { ModelWithDictionary } from './models/ModelWithDictionary'; -export { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports'; -export { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties'; +export type { ModelLink } from './models/ModelLink'; +export type { ModelThatExtends } from './models/ModelThatExtends'; +export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends'; +export type { ModelWithAnyOf } from './models/ModelWithAnyOf'; +export type { ModelWithArray } from './models/ModelWithArray'; +export type { ModelWithBoolean } from './models/ModelWithBoolean'; +export type { ModelWithCircularReference } from './models/ModelWithCircularReference'; +export type { ModelWithDictionary } from './models/ModelWithDictionary'; +export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports'; +export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties'; export { ModelWithEnum } from './models/ModelWithEnum'; export { ModelWithEnumFromDescription } from './models/ModelWithEnumFromDescription'; -export { ModelWithInteger } from './models/ModelWithInteger'; -export { ModelWithLink } from './models/ModelWithLink'; -export { ModelWithNestedEnums } from './models/ModelWithNestedEnums'; -export { ModelWithNestedProperties } from './models/ModelWithNestedProperties'; -export { ModelWithOneOf } from './models/ModelWithOneOf'; -export { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties'; -export { ModelWithPattern } from './models/ModelWithPattern'; -export { ModelWithProperties } from './models/ModelWithProperties'; -export { ModelWithReference } from './models/ModelWithReference'; -export { ModelWithString } from './models/ModelWithString'; -export { MultilineComment } from './models/MultilineComment'; -export { SimpleBoolean } from './models/SimpleBoolean'; -export { SimpleFile } from './models/SimpleFile'; -export { SimpleInteger } from './models/SimpleInteger'; -export { SimpleReference } from './models/SimpleReference'; -export { SimpleString } from './models/SimpleString'; +export type { ModelWithInteger } from './models/ModelWithInteger'; +export type { ModelWithLink } from './models/ModelWithLink'; +export type { ModelWithNestedEnums } from './models/ModelWithNestedEnums'; +export type { ModelWithNestedProperties } from './models/ModelWithNestedProperties'; +export type { ModelWithOneOf } from './models/ModelWithOneOf'; +export type { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties'; +export type { ModelWithPattern } from './models/ModelWithPattern'; +export type { ModelWithProperties } from './models/ModelWithProperties'; +export type { ModelWithReference } from './models/ModelWithReference'; +export type { ModelWithString } from './models/ModelWithString'; +export type { MultilineComment } from './models/MultilineComment'; +export type { SimpleBoolean } from './models/SimpleBoolean'; +export type { SimpleFile } from './models/SimpleFile'; +export type { SimpleInteger } from './models/SimpleInteger'; +export type { SimpleReference } from './models/SimpleReference'; +export type { SimpleString } from './models/SimpleString'; export { $ArrayWithArray } from './schemas/$ArrayWithArray'; export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans'; diff --git a/tsconfig.json b/tsconfig.json index 8c0d6f78..969ecbe0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,7 +21,8 @@ "strictFunctionTypes": true, "removeComments": true, "forceConsistentCasingInFileNames": true, - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "isolatedModules": true }, "files": [