Skip to content

Commit

Permalink
Merge pull request #150 from hey-api/chore/more-parsing-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford authored Mar 26, 2024
2 parents e093b95 + 5ff2a3e commit b264d32
Show file tree
Hide file tree
Showing 47 changed files with 82 additions and 75 deletions.
File renamed without changes.
8 changes: 8 additions & 0 deletions src/openApi/common/interfaces/Type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Type {
$refs: string[];
base: string;
imports: string[];
isNullable: boolean;
template: string | null;
type: string;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/openApi/common/parser/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import camelCase from 'camelcase';

import type { OperationError, OperationResponse } from '../../../types/client';
import type { Config } from '../../../types/config';
import { reservedWords } from '../../../utils/reservedWords';
import { sanitizeOperationName, sanitizeOperationParameterName } from '../../../utils/sanitize';
import { reservedWords } from './reservedWords';
import { sanitizeOperationName, sanitizeOperationParameterName } from './sanitize';

/**
* Convert the input value to a correct operation (method) classname.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
56 changes: 31 additions & 25 deletions src/utils/type.ts → src/openApi/common/parser/type.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
import type { Type } from '../types/client';
import type { Type } from '../interfaces/Type';
import { sanitizeTypeName } from './sanitize';
import { stripNamespace } from './stripNamespace';

const TYPE_MAPPINGS = new Map<string, string>([
['any', 'unknown'],
['array', 'unknown[]'],
['boolean', 'boolean'],
['byte', 'number'],
['char', 'string'],
['date-time', 'string'],
['date', 'string'],
['double', 'number'],
['file', 'binary'],
['float', 'number'],
['int', 'number'],
['integer', 'number'],
['long', 'number'],
['null', 'null'],
['number', 'number'],
['object', 'unknown'],
['password', 'string'],
['short', 'number'],
['string', 'string'],
['void', 'void'],
]);

/**
* Get mapped type for given type to basic Typescript/Javascript type.
*/
export const getMappedType = (type: string, format?: string): string | undefined => {
if (format === 'binary') {
return 'binary';
}
return TYPE_MAPPINGS.get(type);
switch (type) {
case 'any':
case 'object':
return 'unknown';
case 'array':
return 'unknown[]';
case 'boolean':
return 'boolean';
case 'byte':
case 'double':
case 'float':
case 'int':
case 'integer':
case 'long':
case 'number':
case 'short':
return 'number';
case 'char':
case 'date':
case 'date-time':
case 'password':
case 'string':
return 'string';
case 'file':
return 'binary';
case 'null':
return 'null';
case 'void':
return 'void';
}
};

const encode = (value: string): string => sanitizeTypeName(value);
Expand Down
2 changes: 2 additions & 0 deletions src/openApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { OpenApi } from './common/interfaces/OpenApi';
import { parse as parseV2 } from './v2/index';
import { parse as parseV3 } from './v3/index';

export { OpenApi } from './common/interfaces/OpenApi';

/**
* Parse the OpenAPI specification to a Client model that contains
* all the models, services and schema's we should output.
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/interfaces/OpenApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiExternalDocs } from './OpenApiExternalDocs';
import type { OpenApiInfo } from './OpenApiInfo';
import type { OpenApiParameter } from './OpenApiParameter';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/interfaces/OpenApiHeader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiItems } from './OpenApiItems';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/interfaces/OpenApiResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiExample } from './OpenApiExample';
import type { OpenApiHeader } from './OpenApiHeader';
import type { OpenApiReference } from './OpenApiReference';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/interfaces/OpenApiSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WithEnumExtension } from '../../../types/client';
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { WithNullableExtension } from './Extensions/WithNullableExtension';
import type { OpenApiExternalDocs } from './OpenApiExternalDocs';
import type { OpenApiReference } from './OpenApiReference';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/interfaces/OpenApiSecurityScheme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';

/**
* {@link} https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-scheme-object
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Model } from '../../../types/client';
import { getEnums } from '../../../utils/getEnums';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { getModelComposition } from './getModelComposition';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Model } from '../../../types/client';
import { escapeName } from '../../../utils/escapeName';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import type { getModel } from './getModel';
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getModels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Model } from '../../../types/client';
import { reservedWords } from '../../../utils/reservedWords';
import { getType } from '../../../utils/type';
import { reservedWords } from '../../common/parser/reservedWords';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import { getModel } from './getModel';

Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { OperationParameter } from '../../../types/client';
import { getEnums } from '../../../utils/getEnums';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getRef } from '../../common/parser/getRef';
import { getOperationParameterName } from '../../common/parser/operation';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiParameter } from '../interfaces/OpenApiParameter';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getOperationResponse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OperationResponse } from '../../../types/client';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getRef } from '../../common/parser/getRef';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiResponse } from '../interfaces/OpenApiResponse';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getServiceName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import camelCase from 'camelcase';

import { sanitizeServiceName } from '../../../utils/sanitize';
import { sanitizeServiceName } from '../../common/parser/sanitize';

/**
* Convert the input value to a correct service name. This converts
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiComponents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiCallback } from './OpenApiCallback';
import type { OpenApiExample } from './OpenApiExample';
import type { OpenApiHeader } from './OpenApiHeader';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiDiscriminator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';

/**
* {@link} https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#discriminator-object
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiEncoding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiHeader } from './OpenApiHeader';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiHeader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiExample } from './OpenApiExample';
import type { OpenApiReference } from './OpenApiReference';
import type { OpenApiSchema } from './OpenApiSchema';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiReference } from './OpenApiReference';
import type { OpenApiServer } from './OpenApiServer';

Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiMediaType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiEncoding } from './OpenApiEncoding';
import type { OpenApiExample } from './OpenApiExample';
import type { OpenApiReference } from './OpenApiReference';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiOAuthFlow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';

/**
* {@link} https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#oauth-flow-object
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiOperation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiCallback } from './OpenApiCallback';
import type { OpenApiExternalDocs } from './OpenApiExternalDocs';
import type { OpenApiParameter } from './OpenApiParameter';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiParameter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiExample } from './OpenApiExample';
import type { OpenApiReference } from './OpenApiReference';
import type { OpenApiSchema } from './OpenApiSchema';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiRequestBody.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiMediaType } from './OpenApiMediaType';
import type { OpenApiReference } from './OpenApiReference';

Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiHeader } from './OpenApiHeader';
import type { OpenApiLink } from './OpenApiLink';
import type { OpenApiMediaType } from './OpenApiMediaType';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WithEnumExtension } from '../../../types/client';
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiDiscriminator } from './OpenApiDiscriminator';
import type { OpenApiExternalDocs } from './OpenApiExternalDocs';
import type { OpenApiReference } from './OpenApiReference';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/interfaces/OpenApiServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApiServerVariable } from './OpenApiServerVariable';

/**
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v3/parser/__tests__/getModel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';

import { reservedWords } from '../../../../utils/reservedWords';
import { getType } from '../../../../utils/type';
import { reservedWords } from '../../../common/parser/reservedWords';
import { getType } from '../../../common/parser/type';
import { getModel } from '../getModel';

const openApi = {
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v3/parser/discriminator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Model } from '../../../types/client';
import type { Dictionary } from '../../../types/generic';
import { stripNamespace } from '../../../utils/stripNamespace';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import { stripNamespace } from '../../common/parser/stripNamespace';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiDiscriminator } from '../interfaces/OpenApiDiscriminator';

Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getContent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Dictionary } from '../../../types/generic';
import type { Dictionary } from '../../common/interfaces/Dictionary';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiMediaType } from '../interfaces/OpenApiMediaType';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Model } from '../../../types/client';
import { getEnums } from '../../../utils/getEnums';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { findModelComposition, getModelComposition } from './getModelComposition';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Model } from '../../../types/client';
import { escapeName } from '../../../utils/escapeName';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { findOneOfParentDiscriminator, mapPropertyValue } from './discriminator';
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v3/parser/getModels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Model } from '../../../types/client';
import { reservedWords } from '../../../utils/reservedWords';
import { getType } from '../../../utils/type';
import { reservedWords } from '../../common/parser/reservedWords';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import { getModel } from './getModel';

Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { OperationParameter } from '../../../types/client';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getRef } from '../../common/parser/getRef';
import { getOperationParameterName } from '../../common/parser/operation';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiParameter } from '../interfaces/OpenApiParameter';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getOperationRequestBody.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { OperationParameter } from '../../../types/client';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiRequestBody } from '../interfaces/OpenApiRequestBody';
import { getContent } from './getContent';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getOperationResponse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OperationResponse } from '../../../types/client';
import { getPattern } from '../../../utils/getPattern';
import { getType } from '../../../utils/type';
import { getRef } from '../../common/parser/getRef';
import { getType } from '../../common/parser/type';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiResponse } from '../interfaces/OpenApiResponse';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import camelCase from 'camelcase';

import { sanitizeServiceName } from '../../../utils/sanitize';
import { sanitizeServiceName } from '../../common/parser/sanitize';

export const allowedServiceMethods = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'] as const;

Expand Down
9 changes: 0 additions & 9 deletions src/types/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
export interface Type {
$refs: string[];
base: string;
imports: string[];
isNullable: boolean;
template: string | null;
type: string;
}

export interface ModelComposition extends Pick<Model, '$refs' | 'enums' | 'imports' | 'properties'> {
export: Extract<Model['export'], 'all-of' | 'any-of' | 'one-of'>;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getOpenApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';

import $RefParser from '@apidevtools/json-schema-ref-parser';

import type { OpenApi } from '../openApi/common/interfaces/OpenApi';
import type { OpenApi } from '../openApi';

/**
* Load and parse te open api spec. If the file extension is ".yml" or ".yaml"
Expand Down

0 comments on commit b264d32

Please sign in to comment.