diff --git a/rollup.config.ts b/rollup.config.ts index dad7e29f9..a0e98ba7e 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -32,7 +32,6 @@ export function handlebarsPlugin(): Plugin { knownHelpers: { camelCase: true, dataParameters: true, - debugThis: true, enumKey: true, enumName: true, enumUnionType: true, diff --git a/src/openApi/common/interfaces/WithEnumExtension.ts b/src/openApi/common/interfaces/WithEnumExtension.ts new file mode 100644 index 000000000..ea15beb29 --- /dev/null +++ b/src/openApi/common/interfaces/WithEnumExtension.ts @@ -0,0 +1,4 @@ +export interface WithEnumExtension { + 'x-enum-descriptions'?: ReadonlyArray; + 'x-enum-varnames'?: ReadonlyArray; +} diff --git a/src/openApi/common/interfaces/client.ts b/src/openApi/common/interfaces/client.ts new file mode 100644 index 000000000..62512af10 --- /dev/null +++ b/src/openApi/common/interfaces/client.ts @@ -0,0 +1,124 @@ +export interface ModelComposition extends Pick { + export: Extract; +} + +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[]; +} diff --git a/src/utils/__tests__/getPattern.spec.ts b/src/openApi/common/parser/__tests__/getPattern.spec.ts similarity index 100% rename from src/utils/__tests__/getPattern.spec.ts rename to src/openApi/common/parser/__tests__/getPattern.spec.ts diff --git a/src/openApi/common/parser/__tests__/sanitize.spec.ts b/src/openApi/common/parser/__tests__/sanitize.spec.ts index 62a48015e..a653dca9a 100644 --- a/src/openApi/common/parser/__tests__/sanitize.spec.ts +++ b/src/openApi/common/parser/__tests__/sanitize.spec.ts @@ -8,47 +8,46 @@ import { } from '../sanitize'; describe('sanitizeOperationName', () => { - it('should remove/replace illegal characters', () => { - expect(sanitizeOperationName('abc')).toEqual('abc'); - expect(sanitizeOperationName('æbc')).toEqual('æbc'); - expect(sanitizeOperationName('æb.c')).toEqual('æb-c'); - expect(sanitizeOperationName('1æb.c')).toEqual('æb-c'); + it.each([ + { input: 'abc', expected: 'abc' }, + { input: 'æbc', expected: 'æbc' }, + { input: 'æb.c', expected: 'æb-c' }, + { input: '1æb.c', expected: 'æb-c' }, + ])('sanitizeOperationName($input) -> $expected', ({ input, expected }) => { + expect(sanitizeOperationName(input)).toEqual(expected); }); }); describe('sanitizeOperationParameterName', () => { - it('should remove/replace illegal characters', () => { - expect(sanitizeOperationParameterName('abc')).toEqual('abc'); - expect(sanitizeOperationParameterName('æbc')).toEqual('æbc'); - expect(sanitizeOperationParameterName('æb.c')).toEqual('æb-c'); - expect(sanitizeOperationParameterName('1æb.c')).toEqual('æb-c'); - expect(sanitizeOperationParameterName('unknown[]')).toEqual('unknownArray'); + it.each([ + { input: 'abc', expected: 'abc' }, + { input: 'æbc', expected: 'æbc' }, + { input: 'æb.c', expected: 'æb-c' }, + { input: '1æb.c', expected: 'æb-c' }, + { input: 'unknown[]', expected: 'unknownArray' }, + ])('sanitizeOperationParameterName($input) -> $expected', ({ input, expected }) => { + expect(sanitizeOperationParameterName(input)).toEqual(expected); }); }); describe('sanitizeServiceName', () => { - it('should remove/replace illegal characters', () => { - expect(sanitizeServiceName('abc')).toEqual('abc'); - expect(sanitizeServiceName('æbc')).toEqual('æbc'); - expect(sanitizeServiceName('æb.c')).toEqual('æb-c'); - expect(sanitizeServiceName('1æb.c')).toEqual('æb-c'); + it.each([ + { input: 'abc', expected: 'abc' }, + { input: 'æbc', expected: 'æbc' }, + { input: 'æb.c', expected: 'æb-c' }, + { input: '1æb.c', expected: 'æb-c' }, + ])('sanitizeServiceName($input) -> $expected', ({ input, expected }) => { + expect(sanitizeServiceName(input)).toEqual(expected); }); }); describe('sanitizeTypeName', () => { - it('should remove/replace illegal characters', () => { - expect(sanitizeTypeName('abc')).toEqual('abc'); - expect(sanitizeTypeName('æbc')).toEqual('æbc'); - expect(sanitizeTypeName('æb.c')).toEqual('æb_c'); - expect(sanitizeTypeName('1æb.c')).toEqual('æb_c'); - }); -}); - -describe('sanitizeTypeName', () => { - it('should remove/replace illegal characters', () => { - expect(sanitizeTypeName('abc')).toEqual('abc'); - expect(sanitizeTypeName('æbc')).toEqual('æbc'); - expect(sanitizeTypeName('æb.c')).toEqual('æb_c'); - expect(sanitizeTypeName('1æb.c')).toEqual('æb_c'); + it.each([ + { input: 'abc', expected: 'abc' }, + { input: 'æbc', expected: 'æbc' }, + { input: 'æb.c', expected: 'æb_c' }, + { input: '1æb.c', expected: 'æb_c' }, + ])('sanitizeTypeName($input) -> $expected', ({ input, expected }) => { + expect(sanitizeTypeName(input)).toEqual(expected); }); }); diff --git a/src/openApi/common/parser/__tests__/stripNamespace.spec.ts b/src/openApi/common/parser/__tests__/stripNamespace.spec.ts index 9b1ad1743..69e5e10a3 100644 --- a/src/openApi/common/parser/__tests__/stripNamespace.spec.ts +++ b/src/openApi/common/parser/__tests__/stripNamespace.spec.ts @@ -3,19 +3,21 @@ import { describe, expect, it } from 'vitest'; import { stripNamespace } from '../stripNamespace'; describe('stripNamespace', () => { - it('should strip namespace', () => { - expect(stripNamespace('#/definitions/Item')).toEqual('Item'); - expect(stripNamespace('#/parameters/Item')).toEqual('Item'); - expect(stripNamespace('#/responses/Item')).toEqual('Item'); - expect(stripNamespace('#/securityDefinitions/Item')).toEqual('Item'); - expect(stripNamespace('#/components/schemas/Item')).toEqual('Item'); - expect(stripNamespace('#/components/responses/Item')).toEqual('Item'); - expect(stripNamespace('#/components/parameters/Item')).toEqual('Item'); - expect(stripNamespace('#/components/examples/Item')).toEqual('Item'); - expect(stripNamespace('#/components/requestBodies/Item')).toEqual('Item'); - expect(stripNamespace('#/components/headers/Item')).toEqual('Item'); - expect(stripNamespace('#/components/securitySchemes/Item')).toEqual('Item'); - expect(stripNamespace('#/components/links/Item')).toEqual('Item'); - expect(stripNamespace('#/components/callbacks/Item')).toEqual('Item'); + it.each([ + { input: '#/definitions/Item', expected: 'Item' }, + { input: '#/parameters/Item', expected: 'Item' }, + { input: '#/responses/Item', expected: 'Item' }, + { input: '#/securityDefinitions/Item', expected: 'Item' }, + { input: '#/components/schemas/Item', expected: 'Item' }, + { input: '#/components/responses/Item', expected: 'Item' }, + { input: '#/components/parameters/Item', expected: 'Item' }, + { input: '#/components/examples/Item', expected: 'Item' }, + { input: '#/components/requestBodies/Item', expected: 'Item' }, + { input: '#/components/headers/Item', expected: 'Item' }, + { input: '#/components/securitySchemes/Item', expected: 'Item' }, + { input: '#/components/links/Item', expected: 'Item' }, + { input: '#/components/callbacks/Item', expected: 'Item' }, + ])('stripNamespace($input) -> $expected', ({ input, expected }) => { + expect(stripNamespace(input)).toEqual(expected); }); }); diff --git a/src/utils/getEnums.ts b/src/openApi/common/parser/getEnums.ts similarity index 80% rename from src/utils/getEnums.ts rename to src/openApi/common/parser/getEnums.ts index 51e2a4f5a..8786c71cd 100644 --- a/src/utils/getEnums.ts +++ b/src/openApi/common/parser/getEnums.ts @@ -1,5 +1,6 @@ -import type { Enum, WithEnumExtension } from '../types/client'; -import { unique } from './unique'; +import { unique } from '../../../utils/unique'; +import type { Enum } from '../interfaces/client'; +import type { WithEnumExtension } from '../interfaces/WithEnumExtension'; export const getEnums = (definition: WithEnumExtension, values?: ReadonlyArray): Enum[] => { if (!Array.isArray(values)) { diff --git a/src/utils/getPattern.ts b/src/openApi/common/parser/getPattern.ts similarity index 100% rename from src/utils/getPattern.ts rename to src/openApi/common/parser/getPattern.ts diff --git a/src/openApi/common/parser/operation.ts b/src/openApi/common/parser/operation.ts index 4bc8332c7..7dbfa5cb6 100644 --- a/src/openApi/common/parser/operation.ts +++ b/src/openApi/common/parser/operation.ts @@ -1,7 +1,7 @@ import camelCase from 'camelcase'; -import type { OperationError, OperationResponse } from '../../../types/client'; import type { Config } from '../../../types/config'; +import type { OperationError, OperationResponse } from '../interfaces/client'; import { reservedWords } from './reservedWords'; import { sanitizeOperationName, sanitizeOperationParameterName } from './sanitize'; diff --git a/src/openApi/index.ts b/src/openApi/index.ts index a2d955f66..7e4ac9813 100644 --- a/src/openApi/index.ts +++ b/src/openApi/index.ts @@ -4,6 +4,7 @@ import { OpenApi } from './common/interfaces/OpenApi'; import { parse as parseV2 } from './v2/index'; import { parse as parseV3 } from './v3/index'; +export { Enum, Model, Operation, OperationParameter, Service } from './common/interfaces/client'; export { OpenApi } from './common/interfaces/OpenApi'; /** diff --git a/src/openApi/v2/interfaces/OpenApiItems.ts b/src/openApi/v2/interfaces/OpenApiItems.ts index 5b861b469..cae947d5d 100644 --- a/src/openApi/v2/interfaces/OpenApiItems.ts +++ b/src/openApi/v2/interfaces/OpenApiItems.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../types/client'; +import type { WithEnumExtension } from '../../common/interfaces/WithEnumExtension'; /** * {@link} https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#items-object) diff --git a/src/openApi/v2/interfaces/OpenApiParameter.ts b/src/openApi/v2/interfaces/OpenApiParameter.ts index 8309812b8..10913d520 100644 --- a/src/openApi/v2/interfaces/OpenApiParameter.ts +++ b/src/openApi/v2/interfaces/OpenApiParameter.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../types/client'; +import type { WithEnumExtension } from '../../common/interfaces/WithEnumExtension'; 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 aaa994f8b..876986ede 100644 --- a/src/openApi/v2/interfaces/OpenApiSchema.ts +++ b/src/openApi/v2/interfaces/OpenApiSchema.ts @@ -1,5 +1,5 @@ -import type { WithEnumExtension } from '../../../types/client'; import type { Dictionary } from '../../common/interfaces/Dictionary'; +import type { WithEnumExtension } from '../../common/interfaces/WithEnumExtension'; import type { WithNullableExtension } from './Extensions/WithNullableExtension'; import type { OpenApiExternalDocs } from './OpenApiExternalDocs'; import type { OpenApiReference } from './OpenApiReference'; diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index 9179ec206..84bcd6529 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -1,6 +1,6 @@ -import type { Model } from '../../../types/client'; -import { getEnums } from '../../../utils/getEnums'; -import { getPattern } from '../../../utils/getPattern'; +import type { Model } from '../../common/interfaces/client'; +import { getEnums } from '../../common/parser/getEnums'; +import { getPattern } from '../../common/parser/getPattern'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; diff --git a/src/openApi/v2/parser/getModelComposition.ts b/src/openApi/v2/parser/getModelComposition.ts index bd1cbbb30..5df439367 100644 --- a/src/openApi/v2/parser/getModelComposition.ts +++ b/src/openApi/v2/parser/getModelComposition.ts @@ -1,4 +1,4 @@ -import type { Model, ModelComposition } from '../../../types/client'; +import type { Model, ModelComposition } from '../../common/interfaces/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 a0c5afad8..63dace623 100644 --- a/src/openApi/v2/parser/getModelProperties.ts +++ b/src/openApi/v2/parser/getModelProperties.ts @@ -1,6 +1,6 @@ -import type { Model } from '../../../types/client'; import { escapeName } from '../../../utils/escapeName'; -import { getPattern } from '../../../utils/getPattern'; +import type { Model } from '../../common/interfaces/client'; +import { getPattern } from '../../common/parser/getPattern'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; diff --git a/src/openApi/v2/parser/getModels.ts b/src/openApi/v2/parser/getModels.ts index d0018d946..61ed96120 100644 --- a/src/openApi/v2/parser/getModels.ts +++ b/src/openApi/v2/parser/getModels.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../types/client'; +import type { Model } from '../../common/interfaces/client'; import { reservedWords } from '../../common/parser/reservedWords'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v2/parser/getOperation.ts b/src/openApi/v2/parser/getOperation.ts index ccd30db1f..985c7b95b 100644 --- a/src/openApi/v2/parser/getOperation.ts +++ b/src/openApi/v2/parser/getOperation.ts @@ -1,5 +1,5 @@ -import type { Operation, OperationParameters } from '../../../types/client'; import type { Config } from '../../../types/config'; +import type { Operation, OperationParameters } from '../../common/interfaces/client'; import { getOperationErrors, getOperationName, getOperationResponseHeader } from '../../common/parser/operation'; import { toSortedByRequired } from '../../common/parser/sort'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v2/parser/getOperationParameter.ts b/src/openApi/v2/parser/getOperationParameter.ts index 79311b3d3..3e43c4ba0 100644 --- a/src/openApi/v2/parser/getOperationParameter.ts +++ b/src/openApi/v2/parser/getOperationParameter.ts @@ -1,6 +1,6 @@ -import type { OperationParameter } from '../../../types/client'; -import { getEnums } from '../../../utils/getEnums'; -import { getPattern } from '../../../utils/getPattern'; +import type { OperationParameter } from '../../common/interfaces/client'; +import { getEnums } from '../../common/parser/getEnums'; +import { getPattern } from '../../common/parser/getPattern'; import { getRef } from '../../common/parser/getRef'; import { getOperationParameterName } from '../../common/parser/operation'; import { getType } from '../../common/parser/type'; diff --git a/src/openApi/v2/parser/getOperationParameterDefault.ts b/src/openApi/v2/parser/getOperationParameterDefault.ts index 22ec9a1db..674fd72a4 100644 --- a/src/openApi/v2/parser/getOperationParameterDefault.ts +++ b/src/openApi/v2/parser/getOperationParameterDefault.ts @@ -1,4 +1,4 @@ -import type { OperationParameter } from '../../../types/client'; +import type { OperationParameter } from '../../common/interfaces/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 975781e23..9ff35bc6f 100644 --- a/src/openApi/v2/parser/getOperationParameters.ts +++ b/src/openApi/v2/parser/getOperationParameters.ts @@ -1,4 +1,4 @@ -import type { OperationParameters } from '../../../types/client'; +import type { OperationParameters } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; diff --git a/src/openApi/v2/parser/getOperationResponse.ts b/src/openApi/v2/parser/getOperationResponse.ts index ba42059c1..1b134bc7c 100644 --- a/src/openApi/v2/parser/getOperationResponse.ts +++ b/src/openApi/v2/parser/getOperationResponse.ts @@ -1,5 +1,5 @@ -import type { OperationResponse } from '../../../types/client'; -import { getPattern } from '../../../utils/getPattern'; +import type { OperationResponse } from '../../common/interfaces/client'; +import { getPattern } from '../../common/parser/getPattern'; import { getRef } from '../../common/parser/getRef'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v2/parser/getOperationResponses.ts b/src/openApi/v2/parser/getOperationResponses.ts index a50fc6767..6a6b940c4 100644 --- a/src/openApi/v2/parser/getOperationResponses.ts +++ b/src/openApi/v2/parser/getOperationResponses.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../types/client'; +import type { OperationResponse } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import { getOperationResponseCode } from '../../common/parser/operation'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v2/parser/getOperationResults.ts b/src/openApi/v2/parser/getOperationResults.ts index 5146808f4..08ecfea6c 100644 --- a/src/openApi/v2/parser/getOperationResults.ts +++ b/src/openApi/v2/parser/getOperationResults.ts @@ -1,4 +1,4 @@ -import type { Model, OperationResponse } from '../../../types/client'; +import type { Model, OperationResponse } from '../../common/interfaces/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 d8ce87b67..9be2fede5 100644 --- a/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts +++ b/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../types/client'; +import type { Model } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; diff --git a/src/openApi/v2/parser/getServices.ts b/src/openApi/v2/parser/getServices.ts index 7cf1f8d10..00609f0ef 100644 --- a/src/openApi/v2/parser/getServices.ts +++ b/src/openApi/v2/parser/getServices.ts @@ -1,6 +1,6 @@ -import type { Service } from '../../../types/client'; import type { Config } from '../../../types/config'; import { unique } from '../../../utils/unique'; +import type { Service } from '../../common/interfaces/client'; import type { OpenApi } from '../interfaces/OpenApi'; import { getOperation } from './getOperation'; import { getOperationParameters } from './getOperationParameters'; diff --git a/src/openApi/v3/interfaces/OpenApiSchema.ts b/src/openApi/v3/interfaces/OpenApiSchema.ts index 441be5cdf..d1492e955 100644 --- a/src/openApi/v3/interfaces/OpenApiSchema.ts +++ b/src/openApi/v3/interfaces/OpenApiSchema.ts @@ -1,5 +1,5 @@ -import type { WithEnumExtension } from '../../../types/client'; import type { Dictionary } from '../../common/interfaces/Dictionary'; +import type { WithEnumExtension } from '../../common/interfaces/WithEnumExtension'; import type { OpenApiDiscriminator } from './OpenApiDiscriminator'; import type { OpenApiExternalDocs } from './OpenApiExternalDocs'; import type { OpenApiReference } from './OpenApiReference'; diff --git a/src/openApi/v3/interfaces/OpenApiServerVariable.ts b/src/openApi/v3/interfaces/OpenApiServerVariable.ts index 68658fc01..80f35963b 100644 --- a/src/openApi/v3/interfaces/OpenApiServerVariable.ts +++ b/src/openApi/v3/interfaces/OpenApiServerVariable.ts @@ -1,4 +1,4 @@ -import type { WithEnumExtension } from '../../../types/client'; +import type { WithEnumExtension } from '../../common/interfaces/WithEnumExtension'; /** * {@link} https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#server-variable-object diff --git a/src/openApi/v3/parser/discriminator.ts b/src/openApi/v3/parser/discriminator.ts index cde46a1da..747c0a7cb 100644 --- a/src/openApi/v3/parser/discriminator.ts +++ b/src/openApi/v3/parser/discriminator.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../types/client'; +import type { Model } from '../../common/interfaces/client'; import type { Dictionary } from '../../common/interfaces/Dictionary'; import { stripNamespace } from '../../common/parser/stripNamespace'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index b9b347817..58bc069c9 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -1,6 +1,6 @@ -import type { Model } from '../../../types/client'; -import { getEnums } from '../../../utils/getEnums'; -import { getPattern } from '../../../utils/getPattern'; +import type { Model } from '../../common/interfaces/client'; +import { getEnums } from '../../common/parser/getEnums'; +import { getPattern } from '../../common/parser/getPattern'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; diff --git a/src/openApi/v3/parser/getModelComposition.ts b/src/openApi/v3/parser/getModelComposition.ts index 1e11bea8d..90d8de7e2 100644 --- a/src/openApi/v3/parser/getModelComposition.ts +++ b/src/openApi/v3/parser/getModelComposition.ts @@ -1,4 +1,4 @@ -import type { Model, ModelComposition } from '../../../types/client'; +import type { Model, ModelComposition } from '../../common/interfaces/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 a35ba89be..2828f3091 100644 --- a/src/openApi/v3/parser/getModelDefault.ts +++ b/src/openApi/v3/parser/getModelDefault.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../types/client'; +import type { Model } from '../../common/interfaces/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 5c2322aae..45091d311 100644 --- a/src/openApi/v3/parser/getModelProperties.ts +++ b/src/openApi/v3/parser/getModelProperties.ts @@ -1,6 +1,6 @@ -import type { Model } from '../../../types/client'; import { escapeName } from '../../../utils/escapeName'; -import { getPattern } from '../../../utils/getPattern'; +import type { Model } from '../../common/interfaces/client'; +import { getPattern } from '../../common/parser/getPattern'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; diff --git a/src/openApi/v3/parser/getModels.ts b/src/openApi/v3/parser/getModels.ts index 0cd340462..f8c11fe7c 100644 --- a/src/openApi/v3/parser/getModels.ts +++ b/src/openApi/v3/parser/getModels.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../types/client'; +import type { Model } from '../../common/interfaces/client'; import { reservedWords } from '../../common/parser/reservedWords'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getOperationParameter.ts b/src/openApi/v3/parser/getOperationParameter.ts index 8071c6d44..ffd96f837 100644 --- a/src/openApi/v3/parser/getOperationParameter.ts +++ b/src/openApi/v3/parser/getOperationParameter.ts @@ -1,5 +1,5 @@ -import type { OperationParameter } from '../../../types/client'; -import { getPattern } from '../../../utils/getPattern'; +import type { OperationParameter } from '../../common/interfaces/client'; +import { getPattern } from '../../common/parser/getPattern'; import { getRef } from '../../common/parser/getRef'; import { getOperationParameterName } from '../../common/parser/operation'; import { getType } from '../../common/parser/type'; diff --git a/src/openApi/v3/parser/getOperationParameters.ts b/src/openApi/v3/parser/getOperationParameters.ts index babb072d0..e223c85e4 100644 --- a/src/openApi/v3/parser/getOperationParameters.ts +++ b/src/openApi/v3/parser/getOperationParameters.ts @@ -1,4 +1,4 @@ -import type { OperationParameters } from '../../../types/client'; +import type { OperationParameters } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; diff --git a/src/openApi/v3/parser/getOperationRequestBody.ts b/src/openApi/v3/parser/getOperationRequestBody.ts index e5b75627d..a436ad1d5 100644 --- a/src/openApi/v3/parser/getOperationRequestBody.ts +++ b/src/openApi/v3/parser/getOperationRequestBody.ts @@ -1,5 +1,5 @@ -import type { OperationParameter } from '../../../types/client'; -import { getPattern } from '../../../utils/getPattern'; +import type { OperationParameter } from '../../common/interfaces/client'; +import { getPattern } from '../../common/parser/getPattern'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiRequestBody } from '../interfaces/OpenApiRequestBody'; diff --git a/src/openApi/v3/parser/getOperationResponse.ts b/src/openApi/v3/parser/getOperationResponse.ts index 0f410ed70..c3ba94ee5 100644 --- a/src/openApi/v3/parser/getOperationResponse.ts +++ b/src/openApi/v3/parser/getOperationResponse.ts @@ -1,5 +1,5 @@ -import type { OperationResponse } from '../../../types/client'; -import { getPattern } from '../../../utils/getPattern'; +import type { OperationResponse } from '../../common/interfaces/client'; +import { getPattern } from '../../common/parser/getPattern'; import { getRef } from '../../common/parser/getRef'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getOperationResponses.ts b/src/openApi/v3/parser/getOperationResponses.ts index a50fc6767..6a6b940c4 100644 --- a/src/openApi/v3/parser/getOperationResponses.ts +++ b/src/openApi/v3/parser/getOperationResponses.ts @@ -1,4 +1,4 @@ -import type { OperationResponse } from '../../../types/client'; +import type { OperationResponse } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import { getOperationResponseCode } from '../../common/parser/operation'; import type { OpenApi } from '../interfaces/OpenApi'; diff --git a/src/openApi/v3/parser/getOperationResults.ts b/src/openApi/v3/parser/getOperationResults.ts index 5146808f4..08ecfea6c 100644 --- a/src/openApi/v3/parser/getOperationResults.ts +++ b/src/openApi/v3/parser/getOperationResults.ts @@ -1,4 +1,4 @@ -import type { Model, OperationResponse } from '../../../types/client'; +import type { Model, OperationResponse } from '../../common/interfaces/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 d8ce87b67..9be2fede5 100644 --- a/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts +++ b/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts @@ -1,4 +1,4 @@ -import type { Model } from '../../../types/client'; +import type { Model } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; diff --git a/src/openApi/v3/parser/getServices.ts b/src/openApi/v3/parser/getServices.ts index 2c938e6f8..69f5800b4 100644 --- a/src/openApi/v3/parser/getServices.ts +++ b/src/openApi/v3/parser/getServices.ts @@ -1,6 +1,6 @@ -import type { Operation, Service } from '../../../types/client'; import type { Config } from '../../../types/config'; import { unique } from '../../../utils/unique'; +import type { Operation, Service } from '../../common/interfaces/client'; import type { OpenApi } from '../interfaces/OpenApi'; import { getOperationParameters } from './getOperationParameters'; import { getOperation } from './operation'; diff --git a/src/openApi/v3/parser/operation.ts b/src/openApi/v3/parser/operation.ts index 6b084c1ce..b002324e4 100644 --- a/src/openApi/v3/parser/operation.ts +++ b/src/openApi/v3/parser/operation.ts @@ -1,5 +1,5 @@ -import type { Operation, OperationParameter, OperationParameters } from '../../../types/client'; import type { Config } from '../../../types/config'; +import type { Operation, OperationParameter, OperationParameters } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import { getOperationErrors, getOperationName, getOperationResponseHeader } from '../../common/parser/operation'; import { toSortedByRequired } from '../../common/parser/sort'; diff --git a/src/types/client.ts b/src/types/client.ts index a920a2eac..26dfa63b2 100644 --- a/src/types/client.ts +++ b/src/types/client.ts @@ -1,133 +1,4 @@ -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[]; -} - +import { Model, Service } from '../openApi'; export interface Client { models: Model[]; server: string; diff --git a/src/utils/__tests__/handlebars.spec.ts b/src/utils/__tests__/handlebars.spec.ts index b8b6e42be..41e9a196a 100644 --- a/src/utils/__tests__/handlebars.spec.ts +++ b/src/utils/__tests__/handlebars.spec.ts @@ -36,7 +36,6 @@ describe('registerHandlebarHelpers', () => { const helpers = Object.keys(Handlebars.helpers); expect(helpers).toContain('camelCase'); expect(helpers).toContain('dataParameters'); - expect(helpers).toContain('debugThis'); expect(helpers).toContain('enumKey'); expect(helpers).toContain('enumName'); expect(helpers).toContain('enumUnionType'); diff --git a/src/utils/__tests__/sort.spec.ts b/src/utils/__tests__/sort.spec.ts index 751425050..f4f7e5267 100644 --- a/src/utils/__tests__/sort.spec.ts +++ b/src/utils/__tests__/sort.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import type { Model, Service } from '../../types/client'; +import type { Model, Service } from '../../openApi'; import { sort, sortByName } from '../sort'; describe('sort', () => { diff --git a/src/utils/enum.ts b/src/utils/enum.ts index a26f4f0e4..cf11524ea 100644 --- a/src/utils/enum.ts +++ b/src/utils/enum.ts @@ -1,4 +1,4 @@ -import type { Enum } from '../types/client'; +import type { Enum } from '../openApi'; import { unescapeName } from './escapeName'; import { unique } from './unique'; diff --git a/src/utils/handlebars.ts b/src/utils/handlebars.ts index 9fb880d24..5da111e33 100644 --- a/src/utils/handlebars.ts +++ b/src/utils/handlebars.ts @@ -2,6 +2,7 @@ import camelCase from 'camelcase'; import Handlebars from 'handlebars/runtime'; import { EOL } from 'os'; +import type { Model, OperationParameter, Service } from '../openApi'; import templateClient from '../templates/client.hbs'; import angularGetHeaders from '../templates/core/angular/getHeaders.hbs'; import angularGetRequestBody from '../templates/core/angular/getRequestBody.hbs'; @@ -86,7 +87,7 @@ 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 { Client } from '../types/client'; import type { Config } from '../types/config'; import { enumKey, enumName, enumUnionType, enumValue } from './enum'; import { escapeName } from './escapeName'; @@ -119,11 +120,6 @@ const dataParameters = (parameters: OperationParameter[]) => { return output.join(', '); }; -const debugThis = (value: unknown) => { - console.log(value); - return ''; -}; - // same as `>isRequired` partial const isRequired = (model: Pick) => (model.isRequired && !model.default ? '' : '?'); @@ -160,7 +156,6 @@ const operationDataType = (config: Config, service: Service) => { export const registerHandlebarHelpers = (config: Config, client: Client): void => { Handlebars.registerHelper('camelCase', camelCase); Handlebars.registerHelper('dataParameters', dataParameters); - Handlebars.registerHelper('debugThis', debugThis); Handlebars.registerHelper('enumKey', enumKey); Handlebars.registerHelper('enumName', enumName); Handlebars.registerHelper('enumUnionType', enumUnionType); diff --git a/src/utils/postprocess.ts b/src/utils/postprocess.ts index ca259f475..f51f8539c 100644 --- a/src/utils/postprocess.ts +++ b/src/utils/postprocess.ts @@ -1,4 +1,5 @@ -import type { Client, Enum, Model, Operation, Service } from '../types/client'; +import type { Enum, Model, Operation, Service } from '../openApi'; +import type { Client } from '../types/client'; import { sort } from './sort'; import { unique } from './unique';