From e669195a8276409a3735df56298e54d7ce68cbfe Mon Sep 17 00:00:00 2001 From: Lubos Date: Wed, 20 Mar 2024 14:11:48 +0000 Subject: [PATCH] refactor(types): move types from client folder to types --- src/client/interfaces/Client.ts | 9 -- src/client/interfaces/Enum.ts | 6 - src/client/interfaces/Model.ts | 34 ---- src/client/interfaces/ModelComposition.ts | 5 - src/client/interfaces/Operation.ts | 23 --- src/client/interfaces/OperationError.ts | 4 - src/client/interfaces/OperationParameter.ts | 7 - src/client/interfaces/OperationParameters.ts | 12 -- src/client/interfaces/OperationResponse.ts | 6 - src/client/interfaces/Schema.ts | 31 ---- src/client/interfaces/Service.ts | 6 - src/client/interfaces/Type.ts | 8 - src/client/interfaces/WithEnumExtension.ts | 4 - src/index.ts | 2 +- src/openApi/v2/index.ts | 2 +- src/openApi/v2/interfaces/OpenApiItems.ts | 2 +- src/openApi/v2/interfaces/OpenApiParameter.ts | 2 +- src/openApi/v2/interfaces/OpenApiSchema.ts | 2 +- src/openApi/v2/parser/getModel.ts | 2 +- src/openApi/v2/parser/getModelComposition.ts | 3 +- src/openApi/v2/parser/getModelProperties.ts | 2 +- .../v2/parser/getModelTemplate.spec.ts | 29 ---- src/openApi/v2/parser/getModelTemplate.ts | 9 -- src/openApi/v2/parser/getModels.ts | 2 +- src/openApi/v2/parser/getOperation.ts | 12 +- src/openApi/v2/parser/getOperationErrors.ts | 3 +- .../v2/parser/getOperationParameter.ts | 2 +- .../v2/parser/getOperationParameterDefault.ts | 2 +- .../v2/parser/getOperationParameters.ts | 2 +- src/openApi/v2/parser/getOperationResponse.ts | 2 +- .../v2/parser/getOperationResponseHeader.ts | 2 +- .../v2/parser/getOperationResponses.ts | 2 +- src/openApi/v2/parser/getOperationResults.ts | 3 +- .../getRequiredPropertiesFromComposition.ts | 2 +- src/openApi/v2/parser/getServices.ts | 2 +- src/openApi/v2/parser/sortByRequired.ts | 9 -- src/openApi/v3/index.ts | 2 +- src/openApi/v3/interfaces/OpenApiSchema.ts | 2 +- .../v3/interfaces/OpenApiServerVariable.ts | 2 +- src/openApi/v3/parser/getModel.ts | 2 +- src/openApi/v3/parser/getModelComposition.ts | 3 +- src/openApi/v3/parser/getModelDefault.ts | 2 +- src/openApi/v3/parser/getModelProperties.ts | 2 +- .../v3/parser/getModelTemplate.spec.ts | 29 ---- src/openApi/v3/parser/getModelTemplate.ts | 9 -- src/openApi/v3/parser/getModels.ts | 2 +- src/openApi/v3/parser/getOperationErrors.ts | 3 +- .../v3/parser/getOperationParameter.ts | 2 +- .../v3/parser/getOperationParameters.ts | 2 +- .../v3/parser/getOperationRequestBody.ts | 2 +- src/openApi/v3/parser/getOperationResponse.ts | 2 +- .../v3/parser/getOperationResponseHeader.ts | 2 +- .../v3/parser/getOperationResponses.ts | 2 +- src/openApi/v3/parser/getOperationResults.ts | 3 +- .../getRequiredPropertiesFromComposition.ts | 2 +- src/openApi/v3/parser/getServices.ts | 3 +- src/openApi/v3/parser/operation.ts | 4 +- src/types/client.ts | 145 ++++++++++++++++++ src/utils/{ => __tests__}/getPattern.spec.ts | 2 +- .../{ => __tests__}/isSubdirectory.spec.ts | 2 +- .../{ => __tests__}/sanitizeTypeName.spec.ts | 2 +- .../{sortByName.spec.ts => sort.spec.ts} | 14 +- src/utils/{ => __tests__}/unique.spec.ts | 2 +- src/utils/discriminator.ts | 2 +- src/utils/enum.ts | 2 +- src/utils/getEnums.ts | 3 +- src/utils/getHttpRequestName.ts | 2 +- src/utils/handlebars.ts | 7 +- src/utils/operation.ts | 2 +- src/utils/postProcessClient.ts | 2 +- src/utils/postProcessModel.ts | 2 +- src/utils/postProcessModelEnum.ts | 2 +- src/utils/postProcessModelEnums.ts | 2 +- src/utils/postProcessModelImports.ts | 2 +- src/utils/postProcessService.ts | 2 +- src/utils/postProcessServiceImports.ts | 2 +- src/utils/postProcessServiceOperations.ts | 3 +- src/utils/sort.spec.ts | 12 -- src/utils/sort.ts | 8 + src/utils/sortByName.ts | 7 - src/utils/type.ts | 2 +- src/utils/write/class.ts | 6 +- src/utils/write/client.ts | 2 +- src/utils/write/core.ts | 4 +- src/utils/write/index.ts | 6 +- src/utils/write/models.ts | 4 +- src/utils/write/schemas.ts | 4 +- src/utils/write/services.ts | 2 +- tsconfig.check.json | 4 +- 89 files changed, 247 insertions(+), 353 deletions(-) delete mode 100644 src/client/interfaces/Client.ts delete mode 100644 src/client/interfaces/Enum.ts delete mode 100644 src/client/interfaces/Model.ts delete mode 100644 src/client/interfaces/ModelComposition.ts delete mode 100644 src/client/interfaces/Operation.ts delete mode 100644 src/client/interfaces/OperationError.ts delete mode 100644 src/client/interfaces/OperationParameter.ts delete mode 100644 src/client/interfaces/OperationParameters.ts delete mode 100644 src/client/interfaces/OperationResponse.ts delete mode 100644 src/client/interfaces/Schema.ts delete mode 100644 src/client/interfaces/Service.ts delete mode 100644 src/client/interfaces/Type.ts delete mode 100644 src/client/interfaces/WithEnumExtension.ts delete mode 100644 src/openApi/v2/parser/getModelTemplate.spec.ts delete mode 100644 src/openApi/v2/parser/getModelTemplate.ts delete mode 100644 src/openApi/v2/parser/sortByRequired.ts delete mode 100644 src/openApi/v3/parser/getModelTemplate.spec.ts delete mode 100644 src/openApi/v3/parser/getModelTemplate.ts create mode 100644 src/types/client.ts rename src/utils/{ => __tests__}/getPattern.spec.ts (93%) rename src/utils/{ => __tests__}/isSubdirectory.spec.ts (93%) rename src/utils/{ => __tests__}/sanitizeTypeName.spec.ts (88%) rename src/utils/__tests__/{sortByName.spec.ts => sort.spec.ts} (88%) rename src/utils/{ => __tests__}/unique.spec.ts (93%) delete mode 100644 src/utils/sort.spec.ts delete mode 100644 src/utils/sortByName.ts diff --git a/src/client/interfaces/Client.ts b/src/client/interfaces/Client.ts deleted file mode 100644 index 0bec99127..000000000 --- a/src/client/interfaces/Client.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { Model } from './Model'; -import type { Service } from './Service'; - -export interface Client { - models: Model[]; - server: string; - services: Service[]; - version: string; -} diff --git a/src/client/interfaces/Enum.ts b/src/client/interfaces/Enum.ts deleted file mode 100644 index 2c59b3659..000000000 --- a/src/client/interfaces/Enum.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface Enum { - 'x-enum-description'?: string; - 'x-enum-varname'?: string; - description?: string; - value: string | number; -} diff --git a/src/client/interfaces/Model.ts b/src/client/interfaces/Model.ts deleted file mode 100644 index e76da9159..000000000 --- a/src/client/interfaces/Model.ts +++ /dev/null @@ -1,34 +0,0 @@ -import type { Enum } from './Enum'; -import type { Schema } from './Schema'; - -export interface Model extends Schema { - /** - * **Experimental.** Contains list of original refs so they can be used - * to access the schema from anywhere instead of relying on string name. - * This allows us to do things like detect type of ref. - */ - $refs: string[]; - base: string; - default?: string; - deprecated?: boolean; - description: string | null; - enum: Enum[]; - enums: Model[]; - export: - | 'all-of' - | 'any-of' - | 'array' - | 'const' - | 'dictionary' - | 'enum' - | 'generic' - | 'interface' - | 'one-of' - | 'reference'; - imports: string[]; - link: Model | null; - name: string; - properties: Model[]; - template: string | null; - type: string; -} diff --git a/src/client/interfaces/ModelComposition.ts b/src/client/interfaces/ModelComposition.ts deleted file mode 100644 index feafcab68..000000000 --- a/src/client/interfaces/ModelComposition.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { Model } from './Model'; - -export interface ModelComposition extends Pick { - export: Extract; -} diff --git a/src/client/interfaces/Operation.ts b/src/client/interfaces/Operation.ts deleted file mode 100644 index 369d81150..000000000 --- a/src/client/interfaces/Operation.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { OperationError } from './OperationError'; -import type { OperationParameters } from './OperationParameters'; -import type { OperationResponse } from './OperationResponse'; - -export interface Operation extends OperationParameters { - deprecated: boolean; - description: string | null; - errors: OperationError[]; - method: 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT'; - /** - * Method name. Methods contain the request logic. - */ - name: string; - path: string; - responseHeader: string | null; - results: OperationResponse[]; - /** - * Service name, might be without postfix. This will be used to name the - * exported class. - */ - service: string; - summary: string | null; -} diff --git a/src/client/interfaces/OperationError.ts b/src/client/interfaces/OperationError.ts deleted file mode 100644 index bfb741996..000000000 --- a/src/client/interfaces/OperationError.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface OperationError { - code: number; - description: string; -} diff --git a/src/client/interfaces/OperationParameter.ts b/src/client/interfaces/OperationParameter.ts deleted file mode 100644 index 77c0da771..000000000 --- a/src/client/interfaces/OperationParameter.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { Model } from './Model'; - -export interface OperationParameter extends Model { - in: 'path' | 'query' | 'header' | 'formData' | 'body' | 'cookie'; - prop: string; - mediaType: string | null; -} diff --git a/src/client/interfaces/OperationParameters.ts b/src/client/interfaces/OperationParameters.ts deleted file mode 100644 index b7307b895..000000000 --- a/src/client/interfaces/OperationParameters.ts +++ /dev/null @@ -1,12 +0,0 @@ -import type { Model } from './Model'; -import type { OperationParameter } from './OperationParameter'; - -export interface OperationParameters extends Pick { - parameters: OperationParameter[]; - parametersBody: OperationParameter | null; - parametersCookie: OperationParameter[]; - parametersForm: OperationParameter[]; - parametersHeader: OperationParameter[]; - parametersPath: OperationParameter[]; - parametersQuery: OperationParameter[]; -} diff --git a/src/client/interfaces/OperationResponse.ts b/src/client/interfaces/OperationResponse.ts deleted file mode 100644 index 0eed91537..000000000 --- a/src/client/interfaces/OperationResponse.ts +++ /dev/null @@ -1,6 +0,0 @@ -import type { Model } from './Model'; - -export interface OperationResponse extends Model { - in: 'response' | 'header'; - code: number; -} diff --git a/src/client/interfaces/Schema.ts b/src/client/interfaces/Schema.ts deleted file mode 100644 index 4cf4fe19d..000000000 --- a/src/client/interfaces/Schema.ts +++ /dev/null @@ -1,31 +0,0 @@ -export interface Schema { - exclusiveMaximum?: boolean; - exclusiveMinimum?: boolean; - format?: - | 'binary' - | 'boolean' - | 'byte' - | 'date-time' - | 'date' - | 'double' - | 'float' - | 'int32' - | 'int64' - | 'password' - | 'string'; - isDefinition: boolean; - isNullable: boolean; - isReadOnly: boolean; - isRequired: boolean; - maximum?: number; - maxItems?: number; - maxLength?: number; - maxProperties?: number; - minimum?: number; - minItems?: number; - minLength?: number; - minProperties?: number; - multipleOf?: number; - pattern?: string; - uniqueItems?: boolean; -} diff --git a/src/client/interfaces/Service.ts b/src/client/interfaces/Service.ts deleted file mode 100644 index 70726d043..000000000 --- a/src/client/interfaces/Service.ts +++ /dev/null @@ -1,6 +0,0 @@ -import type { Model } from './Model'; -import type { Operation } from './Operation'; - -export interface Service extends Pick { - operations: Operation[]; -} diff --git a/src/client/interfaces/Type.ts b/src/client/interfaces/Type.ts deleted file mode 100644 index 245bc1959..000000000 --- a/src/client/interfaces/Type.ts +++ /dev/null @@ -1,8 +0,0 @@ -export interface Type { - $refs: string[]; - base: string; - imports: string[]; - isNullable: boolean; - template: string | null; - type: string; -} diff --git a/src/client/interfaces/WithEnumExtension.ts b/src/client/interfaces/WithEnumExtension.ts deleted file mode 100644 index ea15beb29..000000000 --- a/src/client/interfaces/WithEnumExtension.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface WithEnumExtension { - 'x-enum-descriptions'?: ReadonlyArray; - 'x-enum-varnames'?: ReadonlyArray; -} diff --git a/src/index.ts b/src/index.ts index d7a2d4089..f8454bf61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,9 +4,9 @@ import { pathToFileURL } from 'node:url'; import { sync } from 'cross-spawn'; -import type { Client } from './client/interfaces/Client'; import { parse as parseV2 } from './openApi/v2'; import { parse as parseV3 } from './openApi/v3'; +import type { Client } from './types/client'; import type { Config, UserConfig } from './types/config'; import { getOpenApiSpec } from './utils/getOpenApiSpec'; import { registerHandlebarTemplates } from './utils/handlebars'; diff --git a/src/openApi/v2/index.ts b/src/openApi/v2/index.ts index 3ca605386..941ed8870 100644 --- a/src/openApi/v2/index.ts +++ b/src/openApi/v2/index.ts @@ -1,4 +1,4 @@ -import type { Client } from '../../client/interfaces/Client'; +import type { Client } from '../../types/client'; import type { Config } from '../../types/config'; import type { OpenApi } from './interfaces/OpenApi'; import { getModels } from './parser/getModels'; diff --git a/src/openApi/v2/interfaces/OpenApiItems.ts b/src/openApi/v2/interfaces/OpenApiItems.ts index 56940adb8..93dc2d76f 100644 --- a/src/openApi/v2/interfaces/OpenApiItems.ts +++ b/src/openApi/v2/interfaces/OpenApiItems.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../client/interfaces/WithEnumExtension'; +import type { WithEnumExtension } from '../../../types/client'; /** * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#itemsObject diff --git a/src/openApi/v2/interfaces/OpenApiParameter.ts b/src/openApi/v2/interfaces/OpenApiParameter.ts index 167df2c10..c626fb96d 100644 --- a/src/openApi/v2/interfaces/OpenApiParameter.ts +++ b/src/openApi/v2/interfaces/OpenApiParameter.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../client/interfaces/WithEnumExtension'; +import type { WithEnumExtension } from '../../../types/client'; import type { WithNullableExtension } from './Extensions/WithNullableExtension'; import type { OpenApiItems } from './OpenApiItems'; import type { OpenApiReference } from './OpenApiReference'; diff --git a/src/openApi/v2/interfaces/OpenApiSchema.ts b/src/openApi/v2/interfaces/OpenApiSchema.ts index b334ea9f6..e2c696dce 100644 --- a/src/openApi/v2/interfaces/OpenApiSchema.ts +++ b/src/openApi/v2/interfaces/OpenApiSchema.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../client/interfaces/WithEnumExtension'; +import type { WithEnumExtension } from '../../../types/client'; import type { Dictionary } from '../../../utils/types'; import type { WithNullableExtension } from './Extensions/WithNullableExtension'; import type { OpenApiExternalDocs } from './OpenApiExternalDocs'; diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index 4a7c2d49c..c9eca4d23 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import { getEnums } from '../../../utils/getEnums'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; diff --git a/src/openApi/v2/parser/getModelComposition.ts b/src/openApi/v2/parser/getModelComposition.ts index d0c7e5cda..bd1cbbb30 100644 --- a/src/openApi/v2/parser/getModelComposition.ts +++ b/src/openApi/v2/parser/getModelComposition.ts @@ -1,5 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; -import type { ModelComposition } from '../../../client/interfaces/ModelComposition'; +import type { Model, ModelComposition } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import type { getModel } from './getModel'; diff --git a/src/openApi/v2/parser/getModelProperties.ts b/src/openApi/v2/parser/getModelProperties.ts index 0a7a53571..d4b0eeac8 100644 --- a/src/openApi/v2/parser/getModelProperties.ts +++ b/src/openApi/v2/parser/getModelProperties.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import { escapeName } from '../../../utils/escapeName'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; diff --git a/src/openApi/v2/parser/getModelTemplate.spec.ts b/src/openApi/v2/parser/getModelTemplate.spec.ts deleted file mode 100644 index 3e9cdd4a4..000000000 --- a/src/openApi/v2/parser/getModelTemplate.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { describe, expect, it } from 'vitest'; - -import { getModelTemplate } from './getModelTemplate'; - -describe('getModelTemplate', () => { - it('should return generic for template type', () => { - const template = getModelTemplate({ - $refs: [], - base: 'Link', - imports: ['Model'], - isNullable: false, - template: 'Model', - type: 'Link', - }); - expect(template).toEqual(''); - }); - - it('should return empty for primary type', () => { - const template = getModelTemplate({ - $refs: [], - base: 'string', - imports: [], - isNullable: false, - template: null, - type: 'string', - }); - expect(template).toEqual(''); - }); -}); diff --git a/src/openApi/v2/parser/getModelTemplate.ts b/src/openApi/v2/parser/getModelTemplate.ts deleted file mode 100644 index 492b3aca6..000000000 --- a/src/openApi/v2/parser/getModelTemplate.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { Type } from '../../../client/interfaces/Type'; - -/** - * If our model has a template type, then we want to generalize that! - * In that case we should return "" as our template type. - * @param modelClass The parsed model class type. - * @returns The model template type ( or empty). - */ -export const getModelTemplate = (modelClass: Type): string => (modelClass.template ? '' : ''); diff --git a/src/openApi/v2/parser/getModels.ts b/src/openApi/v2/parser/getModels.ts index 7d6613b76..07cdcf12b 100644 --- a/src/openApi/v2/parser/getModels.ts +++ b/src/openApi/v2/parser/getModels.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import { reservedWords } from '../../../utils/reservedWords'; import { getType } from '../../../utils/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v2/parser/getOperation.ts b/src/openApi/v2/parser/getOperation.ts index 93270d545..7c5b1dbad 100644 --- a/src/openApi/v2/parser/getOperation.ts +++ b/src/openApi/v2/parser/getOperation.ts @@ -1,5 +1,4 @@ -import type { Operation } from '../../../client/interfaces/Operation'; -import type { OperationParameters } from '../../../client/interfaces/OperationParameters'; +import type { Operation, OperationParameter, OperationParameters } from '../../../types/client'; import type { Config } from '../../../types/config'; import { getOperationName } from '../../../utils/operation'; import type { OpenApi } from '../interfaces/OpenApi'; @@ -10,7 +9,14 @@ import { getOperationResponseHeader } from './getOperationResponseHeader'; import { getOperationResponses } from './getOperationResponses'; import { getOperationResults } from './getOperationResults'; import { getServiceName } from './getServiceName'; -import { sortByRequired } from './sortByRequired'; + +const sortByRequired = (a: OperationParameter, b: OperationParameter): number => { + const aNeedsValue = a.isRequired && a.default === undefined; + const bNeedsValue = b.isRequired && b.default === undefined; + if (aNeedsValue && !bNeedsValue) return -1; + if (bNeedsValue && !aNeedsValue) return 1; + return 0; +}; export const getOperation = ( openApi: OpenApi, diff --git a/src/openApi/v2/parser/getOperationErrors.ts b/src/openApi/v2/parser/getOperationErrors.ts index f963cf803..1a5f6bfca 100644 --- a/src/openApi/v2/parser/getOperationErrors.ts +++ b/src/openApi/v2/parser/getOperationErrors.ts @@ -1,5 +1,4 @@ -import type { OperationError } from '../../../client/interfaces/OperationError'; -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationError, OperationResponse } from '../../../types/client'; /** * diff --git a/src/openApi/v2/parser/getOperationParameter.ts b/src/openApi/v2/parser/getOperationParameter.ts index 119b4d745..d43b17838 100644 --- a/src/openApi/v2/parser/getOperationParameter.ts +++ b/src/openApi/v2/parser/getOperationParameter.ts @@ -1,4 +1,4 @@ -import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; +import type { OperationParameter } from '../../../types/client'; import { getEnums } from '../../../utils/getEnums'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; diff --git a/src/openApi/v2/parser/getOperationParameterDefault.ts b/src/openApi/v2/parser/getOperationParameterDefault.ts index 0677ab50d..22ec9a1db 100644 --- a/src/openApi/v2/parser/getOperationParameterDefault.ts +++ b/src/openApi/v2/parser/getOperationParameterDefault.ts @@ -1,4 +1,4 @@ -import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; +import type { OperationParameter } from '../../../types/client'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; export const getOperationParameterDefault = ( diff --git a/src/openApi/v2/parser/getOperationParameters.ts b/src/openApi/v2/parser/getOperationParameters.ts index b71500292..9cc1f59ec 100644 --- a/src/openApi/v2/parser/getOperationParameters.ts +++ b/src/openApi/v2/parser/getOperationParameters.ts @@ -1,4 +1,4 @@ -import type { OperationParameters } from '../../../client/interfaces/OperationParameters'; +import type { OperationParameters } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import { getOperationParameter } from './getOperationParameter'; diff --git a/src/openApi/v2/parser/getOperationResponse.ts b/src/openApi/v2/parser/getOperationResponse.ts index d2c255373..d046b17dd 100644 --- a/src/openApi/v2/parser/getOperationResponse.ts +++ b/src/openApi/v2/parser/getOperationResponse.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationResponse } from '../../../types/client'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v2/parser/getOperationResponseHeader.ts b/src/openApi/v2/parser/getOperationResponseHeader.ts index 5424ab469..5d6a04202 100644 --- a/src/openApi/v2/parser/getOperationResponseHeader.ts +++ b/src/openApi/v2/parser/getOperationResponseHeader.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationResponse } from '../../../types/client'; export const getOperationResponseHeader = (operationResponses: OperationResponse[]): string | null => { const header = operationResponses.find(operationResponses => operationResponses.in === 'header'); diff --git a/src/openApi/v2/parser/getOperationResponses.ts b/src/openApi/v2/parser/getOperationResponses.ts index 90bfc1410..cf0cf13f9 100644 --- a/src/openApi/v2/parser/getOperationResponses.ts +++ b/src/openApi/v2/parser/getOperationResponses.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationResponse } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiResponse } from '../interfaces/OpenApiResponse'; import type { OpenApiResponses } from '../interfaces/OpenApiResponses'; diff --git a/src/openApi/v2/parser/getOperationResults.ts b/src/openApi/v2/parser/getOperationResults.ts index 00e1ccb17..5146808f4 100644 --- a/src/openApi/v2/parser/getOperationResults.ts +++ b/src/openApi/v2/parser/getOperationResults.ts @@ -1,5 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { Model, OperationResponse } from '../../../types/client'; const areEqual = (a: Model, b: Model): boolean => { const equal = a.type === b.type && a.base === b.base && a.template === b.template; diff --git a/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts b/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts index 682208b93..e8bc73935 100644 --- a/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts +++ b/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import type { getModel } from './getModel'; diff --git a/src/openApi/v2/parser/getServices.ts b/src/openApi/v2/parser/getServices.ts index 5d649b4e3..7cf1f8d10 100644 --- a/src/openApi/v2/parser/getServices.ts +++ b/src/openApi/v2/parser/getServices.ts @@ -1,4 +1,4 @@ -import type { Service } from '../../../client/interfaces/Service'; +import type { Service } from '../../../types/client'; import type { Config } from '../../../types/config'; import { unique } from '../../../utils/unique'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v2/parser/sortByRequired.ts b/src/openApi/v2/parser/sortByRequired.ts deleted file mode 100644 index 6519bb17e..000000000 --- a/src/openApi/v2/parser/sortByRequired.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; - -export const sortByRequired = (a: OperationParameter, b: OperationParameter): number => { - const aNeedsValue = a.isRequired && a.default === undefined; - const bNeedsValue = b.isRequired && b.default === undefined; - if (aNeedsValue && !bNeedsValue) return -1; - if (bNeedsValue && !aNeedsValue) return 1; - return 0; -}; diff --git a/src/openApi/v3/index.ts b/src/openApi/v3/index.ts index 1a0104d7a..f19e49e15 100644 --- a/src/openApi/v3/index.ts +++ b/src/openApi/v3/index.ts @@ -1,4 +1,4 @@ -import type { Client } from '../../client/interfaces/Client'; +import type { Client } from '../../types/client'; import type { Config } from '../../types/config'; import type { OpenApi } from './interfaces/OpenApi'; import { getModels } from './parser/getModels'; diff --git a/src/openApi/v3/interfaces/OpenApiSchema.ts b/src/openApi/v3/interfaces/OpenApiSchema.ts index 426ff6dc8..ccd90a439 100644 --- a/src/openApi/v3/interfaces/OpenApiSchema.ts +++ b/src/openApi/v3/interfaces/OpenApiSchema.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../client/interfaces/WithEnumExtension'; +import type { WithEnumExtension } from '../../../types/client'; import type { Dictionary } from '../../../utils/types'; import type { OpenApiDiscriminator } from './OpenApiDiscriminator'; import type { OpenApiExternalDocs } from './OpenApiExternalDocs'; diff --git a/src/openApi/v3/interfaces/OpenApiServerVariable.ts b/src/openApi/v3/interfaces/OpenApiServerVariable.ts index d50edf5a5..6e42c1e33 100644 --- a/src/openApi/v3/interfaces/OpenApiServerVariable.ts +++ b/src/openApi/v3/interfaces/OpenApiServerVariable.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../client/interfaces/WithEnumExtension'; +import type { WithEnumExtension } from '../../../types/client'; /** * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#serverVariableObject diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index 348eed13f..97cfd8e4f 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import { getEnums } from '../../../utils/getEnums'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; diff --git a/src/openApi/v3/parser/getModelComposition.ts b/src/openApi/v3/parser/getModelComposition.ts index 31fee8e9a..1e11bea8d 100644 --- a/src/openApi/v3/parser/getModelComposition.ts +++ b/src/openApi/v3/parser/getModelComposition.ts @@ -1,5 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; -import type { ModelComposition } from '../../../client/interfaces/ModelComposition'; +import type { Model, ModelComposition } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import type { getModel } from './getModel'; diff --git a/src/openApi/v3/parser/getModelDefault.ts b/src/openApi/v3/parser/getModelDefault.ts index b8930f3a1..a35ba89be 100644 --- a/src/openApi/v3/parser/getModelDefault.ts +++ b/src/openApi/v3/parser/getModelDefault.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; export const getModelDefault = (definition: OpenApiSchema, model?: Model): string | undefined => { diff --git a/src/openApi/v3/parser/getModelProperties.ts b/src/openApi/v3/parser/getModelProperties.ts index fa2c940ac..fb8d02c24 100644 --- a/src/openApi/v3/parser/getModelProperties.ts +++ b/src/openApi/v3/parser/getModelProperties.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import { findOneOfParentDiscriminator, mapPropertyValue } from '../../../utils/discriminator'; import { escapeName } from '../../../utils/escapeName'; import { getPattern } from '../../../utils/getPattern'; diff --git a/src/openApi/v3/parser/getModelTemplate.spec.ts b/src/openApi/v3/parser/getModelTemplate.spec.ts deleted file mode 100644 index 3e9cdd4a4..000000000 --- a/src/openApi/v3/parser/getModelTemplate.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { describe, expect, it } from 'vitest'; - -import { getModelTemplate } from './getModelTemplate'; - -describe('getModelTemplate', () => { - it('should return generic for template type', () => { - const template = getModelTemplate({ - $refs: [], - base: 'Link', - imports: ['Model'], - isNullable: false, - template: 'Model', - type: 'Link', - }); - expect(template).toEqual(''); - }); - - it('should return empty for primary type', () => { - const template = getModelTemplate({ - $refs: [], - base: 'string', - imports: [], - isNullable: false, - template: null, - type: 'string', - }); - expect(template).toEqual(''); - }); -}); diff --git a/src/openApi/v3/parser/getModelTemplate.ts b/src/openApi/v3/parser/getModelTemplate.ts deleted file mode 100644 index 492b3aca6..000000000 --- a/src/openApi/v3/parser/getModelTemplate.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { Type } from '../../../client/interfaces/Type'; - -/** - * If our model has a template type, then we want to generalize that! - * In that case we should return "" as our template type. - * @param modelClass The parsed model class type. - * @returns The model template type ( or empty). - */ -export const getModelTemplate = (modelClass: Type): string => (modelClass.template ? '' : ''); diff --git a/src/openApi/v3/parser/getModels.ts b/src/openApi/v3/parser/getModels.ts index 88596fa54..5cbf195cf 100644 --- a/src/openApi/v3/parser/getModels.ts +++ b/src/openApi/v3/parser/getModels.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import { reservedWords } from '../../../utils/reservedWords'; import { getType } from '../../../utils/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getOperationErrors.ts b/src/openApi/v3/parser/getOperationErrors.ts index 410547a89..52a4950c2 100644 --- a/src/openApi/v3/parser/getOperationErrors.ts +++ b/src/openApi/v3/parser/getOperationErrors.ts @@ -1,5 +1,4 @@ -import type { OperationError } from '../../../client/interfaces/OperationError'; -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationError, OperationResponse } from '../../../types/client'; export const getOperationErrors = (operationResponses: OperationResponse[]): OperationError[] => operationResponses diff --git a/src/openApi/v3/parser/getOperationParameter.ts b/src/openApi/v3/parser/getOperationParameter.ts index 4d68d5f57..7479595d3 100644 --- a/src/openApi/v3/parser/getOperationParameter.ts +++ b/src/openApi/v3/parser/getOperationParameter.ts @@ -1,4 +1,4 @@ -import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; +import type { OperationParameter } from '../../../types/client'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getOperationParameters.ts b/src/openApi/v3/parser/getOperationParameters.ts index b3898e279..7c650f8dd 100644 --- a/src/openApi/v3/parser/getOperationParameters.ts +++ b/src/openApi/v3/parser/getOperationParameters.ts @@ -1,4 +1,4 @@ -import type { OperationParameters } from '../../../client/interfaces/OperationParameters'; +import type { OperationParameters } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import { getOperationParameter } from './getOperationParameter'; diff --git a/src/openApi/v3/parser/getOperationRequestBody.ts b/src/openApi/v3/parser/getOperationRequestBody.ts index c392d9094..e10ef5422 100644 --- a/src/openApi/v3/parser/getOperationRequestBody.ts +++ b/src/openApi/v3/parser/getOperationRequestBody.ts @@ -1,4 +1,4 @@ -import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; +import type { OperationParameter } from '../../../types/client'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getOperationResponse.ts b/src/openApi/v3/parser/getOperationResponse.ts index 08ea848ad..ccd708ba1 100644 --- a/src/openApi/v3/parser/getOperationResponse.ts +++ b/src/openApi/v3/parser/getOperationResponse.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationResponse } from '../../../types/client'; import { getPattern } from '../../../utils/getPattern'; import { getType } from '../../../utils/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getOperationResponseHeader.ts b/src/openApi/v3/parser/getOperationResponseHeader.ts index 5424ab469..5d6a04202 100644 --- a/src/openApi/v3/parser/getOperationResponseHeader.ts +++ b/src/openApi/v3/parser/getOperationResponseHeader.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationResponse } from '../../../types/client'; export const getOperationResponseHeader = (operationResponses: OperationResponse[]): string | null => { const header = operationResponses.find(operationResponses => operationResponses.in === 'header'); diff --git a/src/openApi/v3/parser/getOperationResponses.ts b/src/openApi/v3/parser/getOperationResponses.ts index 90bfc1410..cf0cf13f9 100644 --- a/src/openApi/v3/parser/getOperationResponses.ts +++ b/src/openApi/v3/parser/getOperationResponses.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { OperationResponse } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiResponse } from '../interfaces/OpenApiResponse'; import type { OpenApiResponses } from '../interfaces/OpenApiResponses'; diff --git a/src/openApi/v3/parser/getOperationResults.ts b/src/openApi/v3/parser/getOperationResults.ts index 00e1ccb17..5146808f4 100644 --- a/src/openApi/v3/parser/getOperationResults.ts +++ b/src/openApi/v3/parser/getOperationResults.ts @@ -1,5 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; -import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; +import type { Model, OperationResponse } from '../../../types/client'; const areEqual = (a: Model, b: Model): boolean => { const equal = a.type === b.type && a.base === b.base && a.template === b.template; diff --git a/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts b/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts index 682208b93..e8bc73935 100644 --- a/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts +++ b/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../client/interfaces/Model'; +import type { Model } from '../../../types/client'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import type { getModel } from './getModel'; diff --git a/src/openApi/v3/parser/getServices.ts b/src/openApi/v3/parser/getServices.ts index ff5dd2915..2c938e6f8 100644 --- a/src/openApi/v3/parser/getServices.ts +++ b/src/openApi/v3/parser/getServices.ts @@ -1,5 +1,4 @@ -import type { Operation } from '../../../client/interfaces/Operation'; -import type { Service } from '../../../client/interfaces/Service'; +import type { Operation, Service } from '../../../types/client'; import type { Config } from '../../../types/config'; import { unique } from '../../../utils/unique'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/operation.ts b/src/openApi/v3/parser/operation.ts index 816d25d06..6cd1f99ad 100644 --- a/src/openApi/v3/parser/operation.ts +++ b/src/openApi/v3/parser/operation.ts @@ -1,6 +1,4 @@ -import type { Operation } from '../../../client/interfaces/Operation'; -import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; -import type { OperationParameters } from '../../../client/interfaces/OperationParameters'; +import type { Operation, OperationParameter, OperationParameters } from '../../../types/client'; import type { Config } from '../../../types/config'; import { getOperationName } from '../../../utils/operation'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/types/client.ts b/src/types/client.ts new file mode 100644 index 000000000..966fef062 --- /dev/null +++ b/src/types/client.ts @@ -0,0 +1,145 @@ +export interface Type { + $refs: string[]; + base: string; + imports: string[]; + isNullable: boolean; + template: string | null; + type: string; +} + +export interface ModelComposition extends Pick { + export: Extract; +} + +export interface WithEnumExtension { + 'x-enum-descriptions'?: ReadonlyArray; + 'x-enum-varnames'?: ReadonlyArray; +} + +export interface Enum { + 'x-enum-description'?: string; + 'x-enum-varname'?: string; + description?: string; + value: string | number; +} + +export interface OperationError { + code: number; + description: string; +} + +export interface OperationParameter extends Model { + in: 'path' | 'query' | 'header' | 'formData' | 'body' | 'cookie'; + prop: string; + mediaType: string | null; +} + +export interface OperationParameters extends Pick { + parameters: OperationParameter[]; + parametersBody: OperationParameter | null; + parametersCookie: OperationParameter[]; + parametersForm: OperationParameter[]; + parametersHeader: OperationParameter[]; + parametersPath: OperationParameter[]; + parametersQuery: OperationParameter[]; +} + +export interface OperationResponse extends Model { + in: 'response' | 'header'; + code: number; +} + +export interface Operation extends OperationParameters { + deprecated: boolean; + description: string | null; + errors: OperationError[]; + method: 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT'; + /** + * Method name. Methods contain the request logic. + */ + name: string; + path: string; + responseHeader: string | null; + results: OperationResponse[]; + /** + * Service name, might be without postfix. This will be used to name the + * exported class. + */ + service: string; + summary: string | null; +} + +export interface Schema { + exclusiveMaximum?: boolean; + exclusiveMinimum?: boolean; + format?: + | 'binary' + | 'boolean' + | 'byte' + | 'date-time' + | 'date' + | 'double' + | 'float' + | 'int32' + | 'int64' + | 'password' + | 'string'; + isDefinition: boolean; + isNullable: boolean; + isReadOnly: boolean; + isRequired: boolean; + maximum?: number; + maxItems?: number; + maxLength?: number; + maxProperties?: number; + minimum?: number; + minItems?: number; + minLength?: number; + minProperties?: number; + multipleOf?: number; + pattern?: string; + uniqueItems?: boolean; +} + +export interface Model extends Schema { + /** + * **Experimental.** Contains list of original refs so they can be used + * to access the schema from anywhere instead of relying on string name. + * This allows us to do things like detect type of ref. + */ + $refs: string[]; + base: string; + default?: string; + deprecated?: boolean; + description: string | null; + enum: Enum[]; + enums: Model[]; + export: + | 'all-of' + | 'any-of' + | 'array' + | 'const' + | 'dictionary' + | 'enum' + | 'generic' + | 'interface' + | 'one-of' + | 'reference'; + imports: string[]; + link: Model | null; + name: string; + properties: Model[]; + template: string | null; + type: string; +} + +export interface Service extends Pick { + operations: Operation[]; +} + +export interface Client { + models: Model[]; + server: string; + services: Service[]; + version: string; +} diff --git a/src/utils/getPattern.spec.ts b/src/utils/__tests__/getPattern.spec.ts similarity index 93% rename from src/utils/getPattern.spec.ts rename to src/utils/__tests__/getPattern.spec.ts index 873459cef..1266210b8 100644 --- a/src/utils/getPattern.spec.ts +++ b/src/utils/__tests__/getPattern.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { getPattern } from './getPattern'; +import { getPattern } from '../getPattern'; describe('getPattern', () => { it('should produce correct result', () => { diff --git a/src/utils/isSubdirectory.spec.ts b/src/utils/__tests__/isSubdirectory.spec.ts similarity index 93% rename from src/utils/isSubdirectory.spec.ts rename to src/utils/__tests__/isSubdirectory.spec.ts index 010ec02a5..e53e52098 100644 --- a/src/utils/isSubdirectory.spec.ts +++ b/src/utils/__tests__/isSubdirectory.spec.ts @@ -2,7 +2,7 @@ import path from 'node:path'; import { describe, expect, it } from 'vitest'; -import { isSubDirectory } from './isSubdirectory'; +import { isSubDirectory } from '../isSubdirectory'; describe('isSubDirectory', () => { it('should return correct result', () => { diff --git a/src/utils/sanitizeTypeName.spec.ts b/src/utils/__tests__/sanitizeTypeName.spec.ts similarity index 88% rename from src/utils/sanitizeTypeName.spec.ts rename to src/utils/__tests__/sanitizeTypeName.spec.ts index 8836cf63a..af56319a3 100644 --- a/src/utils/sanitizeTypeName.spec.ts +++ b/src/utils/__tests__/sanitizeTypeName.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import sanitizeTypeName from './sanitizeTypeName'; +import sanitizeTypeName from '../sanitizeTypeName'; describe('sanitizeTypeName', () => { it('should remove/replace illegal characters', () => { diff --git a/src/utils/__tests__/sortByName.spec.ts b/src/utils/__tests__/sort.spec.ts similarity index 88% rename from src/utils/__tests__/sortByName.spec.ts rename to src/utils/__tests__/sort.spec.ts index c86f0b8a4..751425050 100644 --- a/src/utils/__tests__/sortByName.spec.ts +++ b/src/utils/__tests__/sort.spec.ts @@ -1,8 +1,16 @@ import { describe, expect, it } from 'vitest'; -import type { Model } from '../../client/interfaces/Model'; -import type { Service } from '../../client/interfaces/Service'; -import { sortByName } from '../sortByName'; +import type { Model, Service } from '../../types/client'; +import { sort, sortByName } from '../sort'; + +describe('sort', () => { + it('should return correct index', () => { + expect(sort('a', 'b')).toEqual(-1); + expect(sort('b', 'a')).toEqual(1); + expect(sort('a', 'a')).toEqual(0); + expect(sort('', '')).toEqual(0); + }); +}); describe('sortByName', () => { it('should handle empty lists', () => { diff --git a/src/utils/unique.spec.ts b/src/utils/__tests__/unique.spec.ts similarity index 93% rename from src/utils/unique.spec.ts rename to src/utils/__tests__/unique.spec.ts index 9b8a84b11..84823055d 100644 --- a/src/utils/unique.spec.ts +++ b/src/utils/__tests__/unique.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { unique } from './unique'; +import { unique } from '../unique'; describe('unique', () => { it('should return correct index', () => { diff --git a/src/utils/discriminator.ts b/src/utils/discriminator.ts index bf1f7ecb6..ca7fb4b75 100644 --- a/src/utils/discriminator.ts +++ b/src/utils/discriminator.ts @@ -1,6 +1,6 @@ -import type { Model } from '../client/interfaces/Model'; import type { OpenApi } from '../openApi/v3/interfaces/OpenApi'; import type { OpenApiDiscriminator } from '../openApi/v3/interfaces/OpenApiDiscriminator'; +import type { Model } from '../types/client'; import { stripNamespace } from './stripNamespace'; import type { Dictionary } from './types'; diff --git a/src/utils/enum.ts b/src/utils/enum.ts index e3b40b3ec..a26f4f0e4 100644 --- a/src/utils/enum.ts +++ b/src/utils/enum.ts @@ -1,4 +1,4 @@ -import type { Enum } from '../client/interfaces/Enum'; +import type { Enum } from '../types/client'; import { unescapeName } from './escapeName'; import { unique } from './unique'; diff --git a/src/utils/getEnums.ts b/src/utils/getEnums.ts index a5077d0d5..51e2a4f5a 100644 --- a/src/utils/getEnums.ts +++ b/src/utils/getEnums.ts @@ -1,5 +1,4 @@ -import type { Enum } from '../client/interfaces/Enum'; -import type { WithEnumExtension } from '../client/interfaces/WithEnumExtension'; +import type { Enum, WithEnumExtension } from '../types/client'; import { unique } from './unique'; export const getEnums = (definition: WithEnumExtension, values?: ReadonlyArray): Enum[] => { diff --git a/src/utils/getHttpRequestName.ts b/src/utils/getHttpRequestName.ts index ed5bb7af9..5b24580a2 100644 --- a/src/utils/getHttpRequestName.ts +++ b/src/utils/getHttpRequestName.ts @@ -1,4 +1,4 @@ -import type { UserConfig } from '../node'; +import type { UserConfig } from '../types/config'; /** * Generate the HttpRequest filename based on the selected client diff --git a/src/utils/handlebars.ts b/src/utils/handlebars.ts index 5a589d6df..5b80aa00d 100644 --- a/src/utils/handlebars.ts +++ b/src/utils/handlebars.ts @@ -2,10 +2,6 @@ import camelCase from 'camelcase'; import Handlebars from 'handlebars/runtime'; import { EOL } from 'os'; -import type { Client } from '../client/interfaces/Client'; -import type { Model } from '../client/interfaces/Model'; -import type { OperationParameter } from '../client/interfaces/OperationParameter'; -import type { Service } from '../client/interfaces/Service'; import templateClient from '../templates/client.hbs'; import angularGetHeaders from '../templates/core/angular/getHeaders.hbs'; import angularGetRequestBody from '../templates/core/angular/getRequestBody.hbs'; @@ -90,10 +86,11 @@ import partialTypeInterface from '../templates/partials/typeInterface.hbs'; import partialTypeIntersection from '../templates/partials/typeIntersection.hbs'; import partialTypeReference from '../templates/partials/typeReference.hbs'; import partialTypeUnion from '../templates/partials/typeUnion.hbs'; +import type { Client, Model, OperationParameter, Service } from '../types/client'; import type { Config, UserConfig } from '../types/config'; import { enumKey, enumName, enumUnionType, enumValue } from './enum'; import { escapeName } from './escapeName'; -import { sortByName } from './sortByName'; +import { sortByName } from './sort'; import { unique } from './unique'; const escapeComment = (value: string) => diff --git a/src/utils/operation.ts b/src/utils/operation.ts index eac2f7843..b6c41290c 100644 --- a/src/utils/operation.ts +++ b/src/utils/operation.ts @@ -1,6 +1,6 @@ import camelCase from 'camelcase'; -import type { UserConfig } from '../node'; +import type { UserConfig } from '../types/config'; import sanitizeOperationName from './sanitizeOperationName'; /** diff --git a/src/utils/postProcessClient.ts b/src/utils/postProcessClient.ts index 2a5e3f5ac..02e47bcfe 100644 --- a/src/utils/postProcessClient.ts +++ b/src/utils/postProcessClient.ts @@ -1,4 +1,4 @@ -import type { Client } from '../client/interfaces/Client'; +import type { Client } from '../types/client'; import { postProcessModel } from './postProcessModel'; import { postProcessService } from './postProcessService'; diff --git a/src/utils/postProcessModel.ts b/src/utils/postProcessModel.ts index bdff88841..667ab0e4f 100644 --- a/src/utils/postProcessModel.ts +++ b/src/utils/postProcessModel.ts @@ -1,4 +1,4 @@ -import type { Model } from '../client/interfaces/Model'; +import type { Model } from '../types/client'; import { postProcessModelEnum } from './postProcessModelEnum'; import { postProcessModelEnums } from './postProcessModelEnums'; import { postProcessModelImports } from './postProcessModelImports'; diff --git a/src/utils/postProcessModelEnum.ts b/src/utils/postProcessModelEnum.ts index 6de934a7f..f48402d40 100644 --- a/src/utils/postProcessModelEnum.ts +++ b/src/utils/postProcessModelEnum.ts @@ -1,4 +1,4 @@ -import type { Model } from '../client/interfaces/Model'; +import type { Model } from '../types/client'; /** * Set unique enum values for the model diff --git a/src/utils/postProcessModelEnums.ts b/src/utils/postProcessModelEnums.ts index 9aada1ac6..201b1dcab 100644 --- a/src/utils/postProcessModelEnums.ts +++ b/src/utils/postProcessModelEnums.ts @@ -1,4 +1,4 @@ -import type { Model } from '../client/interfaces/Model'; +import type { Model } from '../types/client'; /** * Set unique enum values for the model diff --git a/src/utils/postProcessModelImports.ts b/src/utils/postProcessModelImports.ts index bd489b7be..043f35026 100644 --- a/src/utils/postProcessModelImports.ts +++ b/src/utils/postProcessModelImports.ts @@ -1,4 +1,4 @@ -import type { Model } from '../client/interfaces/Model'; +import type { Model } from '../types/client'; import { sort } from './sort'; import { unique } from './unique'; diff --git a/src/utils/postProcessService.ts b/src/utils/postProcessService.ts index 2851f4a6a..facaea2a3 100644 --- a/src/utils/postProcessService.ts +++ b/src/utils/postProcessService.ts @@ -1,4 +1,4 @@ -import type { Service } from '../client/interfaces/Service'; +import type { Service } from '../types/client'; import { postProcessServiceImports } from './postProcessServiceImports'; import { postProcessServiceOperations } from './postProcessServiceOperations'; diff --git a/src/utils/postProcessServiceImports.ts b/src/utils/postProcessServiceImports.ts index 103d04eb6..d683fc048 100644 --- a/src/utils/postProcessServiceImports.ts +++ b/src/utils/postProcessServiceImports.ts @@ -1,4 +1,4 @@ -import type { Service } from '../client/interfaces/Service'; +import type { Service } from '../types/client'; import { sort } from './sort'; import { unique } from './unique'; diff --git a/src/utils/postProcessServiceOperations.ts b/src/utils/postProcessServiceOperations.ts index fcbf524a8..597a2e73d 100644 --- a/src/utils/postProcessServiceOperations.ts +++ b/src/utils/postProcessServiceOperations.ts @@ -1,5 +1,4 @@ -import type { Operation } from '../client/interfaces/Operation'; -import type { Service } from '../client/interfaces/Service'; +import type { Operation, Service } from '../types/client'; export const postProcessServiceOperations = (service: Service): Operation[] => { const names = new Map(); diff --git a/src/utils/sort.spec.ts b/src/utils/sort.spec.ts deleted file mode 100644 index a9ce18518..000000000 --- a/src/utils/sort.spec.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { describe, expect, it } from 'vitest'; - -import { sort } from './sort'; - -describe('sort', () => { - it('should return correct index', () => { - expect(sort('a', 'b')).toEqual(-1); - expect(sort('b', 'a')).toEqual(1); - expect(sort('a', 'a')).toEqual(0); - expect(sort('', '')).toEqual(0); - }); -}); diff --git a/src/utils/sort.ts b/src/utils/sort.ts index 1d76998f4..b5d0ad531 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -3,3 +3,11 @@ export const sort = (a: string, b: string): number => { const nameB = b.toLowerCase(); return nameA.localeCompare(nameB, 'en'); }; + +export function sortByName(items: T[]): T[] { + return items.sort((a, b) => { + const nameA = a.name.toLocaleLowerCase(); + const nameB = b.name.toLocaleLowerCase(); + return nameA.localeCompare(nameB, 'en'); + }); +} diff --git a/src/utils/sortByName.ts b/src/utils/sortByName.ts deleted file mode 100644 index 2e8b050a9..000000000 --- a/src/utils/sortByName.ts +++ /dev/null @@ -1,7 +0,0 @@ -export function sortByName(items: T[]): T[] { - return items.sort((a, b) => { - const nameA = a.name.toLocaleLowerCase(); - const nameB = b.name.toLocaleLowerCase(); - return nameA.localeCompare(nameB, 'en'); - }); -} diff --git a/src/utils/type.ts b/src/utils/type.ts index d075232d5..c887f5598 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -1,4 +1,4 @@ -import type { Type } from '../client/interfaces/Type'; +import type { Type } from '../types/client'; import sanitizeTypeName from './sanitizeTypeName'; import { stripNamespace } from './stripNamespace'; diff --git a/src/utils/write/class.ts b/src/utils/write/class.ts index c50a25558..9d1127ddd 100644 --- a/src/utils/write/class.ts +++ b/src/utils/write/class.ts @@ -1,11 +1,11 @@ import { writeFileSync } from 'node:fs'; import path from 'node:path'; -import type { Client } from '../../client/interfaces/Client'; -import type { UserConfig } from '../../node'; +import type { Client } from '../../types/client'; +import type { UserConfig } from '../../types/config'; import { getHttpRequestName } from '../getHttpRequestName'; import type { Templates } from '../handlebars'; -import { sortByName } from '../sortByName'; +import { sortByName } from '../sort'; /** * Generate the OpenAPI client index file using the Handlebar template and write it to disk. diff --git a/src/utils/write/client.ts b/src/utils/write/client.ts index 7ba407b71..cbfae09fb 100644 --- a/src/utils/write/client.ts +++ b/src/utils/write/client.ts @@ -1,7 +1,7 @@ import { mkdirSync, rmSync } from 'node:fs'; import path from 'node:path'; -import type { Client } from '../../client/interfaces/Client'; +import type { Client } from '../../types/client'; import type { Config } from '../../types/config'; import type { Templates } from '../handlebars'; import { isSubDirectory } from '../isSubdirectory'; diff --git a/src/utils/write/core.ts b/src/utils/write/core.ts index 80ef60ace..2784a7e98 100644 --- a/src/utils/write/core.ts +++ b/src/utils/write/core.ts @@ -1,8 +1,8 @@ import { copyFileSync, existsSync, writeFileSync } from 'node:fs'; import path from 'node:path'; -import type { Client } from '../../client/interfaces/Client'; -import type { UserConfig } from '../../node'; +import type { Client } from '../../types/client'; +import type { UserConfig } from '../../types/config'; import { getHttpRequestName } from '../getHttpRequestName'; import type { Templates } from '../handlebars'; diff --git a/src/utils/write/index.ts b/src/utils/write/index.ts index 6a8d6b06d..39185d909 100644 --- a/src/utils/write/index.ts +++ b/src/utils/write/index.ts @@ -1,10 +1,10 @@ import { writeFileSync } from 'node:fs'; import path from 'node:path'; -import type { Client } from '../../client/interfaces/Client'; -import type { UserConfig } from '../../node'; +import type { Client } from '../../types/client'; +import type { UserConfig } from '../../types/config'; import { Templates } from '../handlebars'; -import { sortByName } from '../sortByName'; +import { sortByName } from '../sort'; /** * Generate the OpenAPI client index file using the Handlebar template and write it to disk. diff --git a/src/utils/write/models.ts b/src/utils/write/models.ts index 813dd69ca..da7fcb047 100644 --- a/src/utils/write/models.ts +++ b/src/utils/write/models.ts @@ -1,8 +1,8 @@ import { writeFileSync } from 'node:fs'; import path from 'node:path'; -import type { Client } from '../../client/interfaces/Client'; -import type { UserConfig } from '../../node'; +import type { Client } from '../../types/client'; +import type { UserConfig } from '../../types/config'; import type { Templates } from '../handlebars'; /** diff --git a/src/utils/write/schemas.ts b/src/utils/write/schemas.ts index 938323b58..4b1b9190f 100644 --- a/src/utils/write/schemas.ts +++ b/src/utils/write/schemas.ts @@ -1,8 +1,8 @@ import { writeFileSync } from 'node:fs'; import path from 'node:path'; -import type { Client } from '../../client/interfaces/Client'; -import type { UserConfig } from '../../node'; +import type { Client } from '../../types/client'; +import type { UserConfig } from '../../types/config'; import type { Templates } from '../handlebars'; /** diff --git a/src/utils/write/services.ts b/src/utils/write/services.ts index 44b1f07f0..e089aeafb 100644 --- a/src/utils/write/services.ts +++ b/src/utils/write/services.ts @@ -1,7 +1,7 @@ import { writeFileSync } from 'node:fs'; import path from 'node:path'; -import type { Client } from '../../client/interfaces/Client'; +import type { Client } from '../../types/client'; import type { Config } from '../../types/config'; import type { Templates } from '../handlebars'; diff --git a/tsconfig.check.json b/tsconfig.check.json index 2d57f3150..1ddab4038 100644 --- a/tsconfig.check.json +++ b/tsconfig.check.json @@ -1,8 +1,8 @@ { "compilerOptions": { "exactOptionalPropertyTypes": true, - "module": "node16", - "moduleResolution": "node16", + "module": "NodeNext", + "moduleResolution": "NodeNext", "noEmit": true, "strict": true, "target": "ES2020"