From 280d196f99b9635a22ac6af4d73c2d024544689f Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Tue, 25 Jan 2022 13:22:21 +0100 Subject: [PATCH] - Moved to const definitions for functions --- rollup.config.js | 1 + src/index.ts | 6 +++--- src/openApi/v2/index.ts | 4 ++-- src/openApi/v2/parser/escapeName.ts | 4 ++-- src/openApi/v2/parser/extendEnum.ts | 4 ++-- src/openApi/v2/parser/getEnum.ts | 4 ++-- src/openApi/v2/parser/getEnumFromDescription.ts | 4 ++-- src/openApi/v2/parser/getMappedType.ts | 4 ++-- src/openApi/v2/parser/getModel.ts | 6 +++--- src/openApi/v2/parser/getModelComposition.ts | 6 +++--- src/openApi/v2/parser/getModelProperties.ts | 4 ++-- src/openApi/v2/parser/getModelTemplate.ts | 4 ++-- src/openApi/v2/parser/getModels.ts | 4 ++-- src/openApi/v2/parser/getOperation.ts | 9 ++++----- src/openApi/v2/parser/getOperationErrors.ts | 4 ++-- src/openApi/v2/parser/getOperationName.ts | 4 ++-- src/openApi/v2/parser/getOperationParameter.ts | 4 ++-- .../v2/parser/getOperationParameterDefault.ts | 6 +++--- src/openApi/v2/parser/getOperationParameterName.ts | 4 ++-- src/openApi/v2/parser/getOperationParameters.ts | 4 ++-- src/openApi/v2/parser/getOperationPath.ts | 4 ++-- src/openApi/v2/parser/getOperationResponse.ts | 6 +++--- src/openApi/v2/parser/getOperationResponseCode.ts | 4 ++-- .../v2/parser/getOperationResponseHeader.ts | 4 ++-- src/openApi/v2/parser/getOperationResponses.ts | 4 ++-- src/openApi/v2/parser/getOperationResults.ts | 8 ++++---- src/openApi/v2/parser/getRef.ts | 4 ++-- .../parser/getRequiredPropertiesFromComposition.ts | 6 +++--- src/openApi/v2/parser/getServer.ts | 4 ++-- src/openApi/v2/parser/getServiceName.ts | 4 ++-- src/openApi/v2/parser/getServiceVersion.ts | 4 ++-- src/openApi/v2/parser/getServices.ts | 6 +++--- src/openApi/v2/parser/getType.ts | 8 ++++---- src/openApi/v2/parser/sortByRequired.ts | 4 ++-- src/openApi/v2/parser/stripNamespace.ts | 4 ++-- src/openApi/v3/index.ts | 4 ++-- src/openApi/v3/parser/escapeName.ts | 4 ++-- src/openApi/v3/parser/extendEnum.ts | 4 ++-- src/openApi/v3/parser/getContent.ts | 4 ++-- src/openApi/v3/parser/getEnum.ts | 4 ++-- src/openApi/v3/parser/getEnumFromDescription.ts | 4 ++-- src/openApi/v3/parser/getMappedType.ts | 4 ++-- src/openApi/v3/parser/getModel.ts | 6 +++--- src/openApi/v3/parser/getModelComposition.ts | 6 +++--- src/openApi/v3/parser/getModelDefault.ts | 4 ++-- src/openApi/v3/parser/getModelProperties.ts | 6 +++--- src/openApi/v3/parser/getModelTemplate.ts | 4 ++-- src/openApi/v3/parser/getModels.ts | 4 ++-- src/openApi/v3/parser/getOperation.ts | 9 ++++----- src/openApi/v3/parser/getOperationErrors.ts | 4 ++-- src/openApi/v3/parser/getOperationName.ts | 4 ++-- src/openApi/v3/parser/getOperationParameter.ts | 4 ++-- src/openApi/v3/parser/getOperationParameterName.ts | 4 ++-- src/openApi/v3/parser/getOperationParameters.ts | 4 ++-- src/openApi/v3/parser/getOperationPath.ts | 4 ++-- src/openApi/v3/parser/getOperationRequestBody.ts | 4 ++-- src/openApi/v3/parser/getOperationResponse.ts | 6 +++--- src/openApi/v3/parser/getOperationResponseCode.ts | 4 ++-- .../v3/parser/getOperationResponseHeader.ts | 4 ++-- src/openApi/v3/parser/getOperationResponses.ts | 4 ++-- src/openApi/v3/parser/getOperationResults.ts | 8 ++++---- src/openApi/v3/parser/getRef.ts | 4 ++-- .../parser/getRequiredPropertiesFromComposition.ts | 6 +++--- src/openApi/v3/parser/getServer.ts | 4 ++-- src/openApi/v3/parser/getServiceName.ts | 4 ++-- src/openApi/v3/parser/getServiceVersion.ts | 4 ++-- src/openApi/v3/parser/getServices.ts | 6 +++--- src/openApi/v3/parser/getType.ts | 8 ++++---- src/openApi/v3/parser/sortByRequired.ts | 4 ++-- src/openApi/v3/parser/stripNamespace.ts | 4 ++-- src/templates/__mocks__/index.ts | 2 +- src/templates/client.hbs | 4 ++-- src/utils/discriminator.ts | 12 ++++++------ src/utils/flatMap.ts | 6 +++--- src/utils/formatCode.ts | 4 ++-- src/utils/formatIndentation.ts | 4 ++-- src/utils/getModelNames.ts | 4 ++-- src/utils/getOpenApiSpec.ts | 4 ++-- src/utils/getOpenApiVersion.ts | 4 ++-- src/utils/getPattern.ts | 4 ++-- src/utils/getServiceNames.ts | 4 ++-- src/utils/isDefined.ts | 4 ++-- src/utils/isEqual.ts | 4 ++-- src/utils/isString.ts | 4 ++-- src/utils/isSubdirectory.ts | 4 ++-- src/utils/postProcessClient.ts | 4 ++-- src/utils/postProcessModel.ts | 4 ++-- src/utils/postProcessModelEnum.ts | 4 ++-- src/utils/postProcessModelEnums.ts | 4 ++-- src/utils/postProcessModelImports.ts | 4 ++-- src/utils/postProcessService.ts | 4 ++-- src/utils/postProcessServiceImports.ts | 4 ++-- src/utils/postProcessServiceOperations.ts | 4 ++-- src/utils/readSpec.ts | 4 ++-- src/utils/readSpecFromDisk.ts | 4 ++-- src/utils/readSpecFromHttp.ts | 4 ++-- src/utils/readSpecFromHttps.ts | 4 ++-- src/utils/registerHandlebarHelpers.spec.ts | 1 + src/utils/registerHandlebarHelpers.ts | 11 ++++++++--- src/utils/registerHandlebarTemplates.ts | 6 +++--- src/utils/sort.ts | 4 ++-- src/utils/sortModelsByName.ts | 4 ++-- src/utils/sortServicesByName.ts | 4 ++-- src/utils/unique.ts | 4 ++-- src/utils/writeClient.ts | 6 +++--- src/utils/writeClientClass.ts | 6 +++--- src/utils/writeClientCore.ts | 9 ++++----- src/utils/writeClientIndex.ts | 6 +++--- src/utils/writeClientModels.ts | 6 +++--- src/utils/writeClientSchemas.ts | 6 +++--- src/utils/writeClientServices.ts | 6 +++--- test/custom/request.ts | 8 ++++---- test/spec/v2.json | 6 ++++++ test/spec/v3.json | 14 ++++++++++++-- 114 files changed, 286 insertions(+), 266 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 59e8d39e3..6f085f8d8 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -36,6 +36,7 @@ const handlebarsPlugin = () => ({ enumerator: true, escapeComment: true, escapeDescription: true, + camelCase: true, }, }); return `export default ${templateSpec};`; diff --git a/src/index.ts b/src/index.ts index fc7335406..eef0c2dc5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,7 +48,7 @@ export type Options = { * @param request: Path to custom request file * @param write Write the files to disk (true or false) */ -export async function generate({ +export const generate = async ({ input, output, httpClient = HttpClient.FETCH, @@ -63,7 +63,7 @@ export async function generate({ postfix = 'Service', request, write = true, -}: Options): Promise { +}: Options): Promise => { const openApi = isString(input) ? await getOpenApiSpec(input) : input; const openApiVersion = getOpenApiVersion(openApi); const templates = registerHandlebarTemplates({ @@ -119,7 +119,7 @@ export async function generate({ break; } } -} +}; export default { HttpClient, diff --git a/src/openApi/v2/index.ts b/src/openApi/v2/index.ts index 30bd05ccc..9dbdadb34 100644 --- a/src/openApi/v2/index.ts +++ b/src/openApi/v2/index.ts @@ -10,11 +10,11 @@ import { getServiceVersion } from './parser/getServiceVersion'; * all the models, services and schema's we should output. * @param openApi The OpenAPI spec that we have loaded from disk. */ -export function parse(openApi: OpenApi): Client { +export const parse = (openApi: OpenApi): Client => { const version = getServiceVersion(openApi.info.version); const server = getServer(openApi); const models = getModels(openApi); const services = getServices(openApi); return { version, server, models, services }; -} +}; diff --git a/src/openApi/v2/parser/escapeName.ts b/src/openApi/v2/parser/escapeName.ts index 0a26cb659..6b23d934b 100644 --- a/src/openApi/v2/parser/escapeName.ts +++ b/src/openApi/v2/parser/escapeName.ts @@ -1,4 +1,4 @@ -export function escapeName(value: string): string { +export const escapeName = (value: string): string => { if (value) { const validName = /^[a-zA-Z_$][\w$]+$/g.test(value); if (!validName) { @@ -6,4 +6,4 @@ export function escapeName(value: string): string { } } return value; -} +}; diff --git a/src/openApi/v2/parser/extendEnum.ts b/src/openApi/v2/parser/extendEnum.ts index 81e85fdf3..0a5ebeb1e 100644 --- a/src/openApi/v2/parser/extendEnum.ts +++ b/src/openApi/v2/parser/extendEnum.ts @@ -7,7 +7,7 @@ import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtensi * @param enumerators * @param definition */ -export function extendEnum(enumerators: Enum[], definition: WithEnumExtension): Enum[] { +export const extendEnum = (enumerators: Enum[], definition: WithEnumExtension): Enum[] => { const names = definition['x-enum-varnames']; const descriptions = definition['x-enum-descriptions']; @@ -17,4 +17,4 @@ export function extendEnum(enumerators: Enum[], definition: WithEnumExtension): value: enumerator.value, type: enumerator.type, })); -} +}; diff --git a/src/openApi/v2/parser/getEnum.ts b/src/openApi/v2/parser/getEnum.ts index 5558db68b..c684b466a 100644 --- a/src/openApi/v2/parser/getEnum.ts +++ b/src/openApi/v2/parser/getEnum.ts @@ -1,7 +1,7 @@ import type { Enum } from '../../../client/interfaces/Enum'; import { isDefined } from '../../../utils/isDefined'; -export function getEnum(values?: (string | number)[]): Enum[] { +export const getEnum = (values?: (string | number)[]): Enum[] => { if (Array.isArray(values)) { return values .filter((value, index, arr) => { @@ -30,4 +30,4 @@ export function getEnum(values?: (string | number)[]): Enum[] { }); } return []; -} +}; diff --git a/src/openApi/v2/parser/getEnumFromDescription.ts b/src/openApi/v2/parser/getEnumFromDescription.ts index d1154e6e3..0f49cf408 100644 --- a/src/openApi/v2/parser/getEnumFromDescription.ts +++ b/src/openApi/v2/parser/getEnumFromDescription.ts @@ -3,7 +3,7 @@ import type { Enum } from '../../../client/interfaces/Enum'; /** * @deprecated */ -export function getEnumFromDescription(description: string): Enum[] { +export const getEnumFromDescription = (description: string): Enum[] => { // Check if we can find this special format string: // None=0,Something=1,AnotherThing=2 if (/^(\w+=[0-9]+)/g.test(description)) { @@ -36,4 +36,4 @@ export function getEnumFromDescription(description: string): Enum[] { } return []; -} +}; diff --git a/src/openApi/v2/parser/getMappedType.ts b/src/openApi/v2/parser/getMappedType.ts index e44434e2a..a7c32fd84 100644 --- a/src/openApi/v2/parser/getMappedType.ts +++ b/src/openApi/v2/parser/getMappedType.ts @@ -24,9 +24,9 @@ const TYPE_MAPPINGS = new Map([ /** * Get mapped type for given type to any basic Typescript/Javascript type. */ -export function getMappedType(type: string, format?: string): string | undefined { +export const getMappedType = (type: string, format?: string): string | undefined => { if (format === 'binary') { return 'binary'; } return TYPE_MAPPINGS.get(type); -} +}; diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index d5683f070..8ac1bc115 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -9,12 +9,12 @@ import { getModelComposition } from './getModelComposition'; import { getModelProperties } from './getModelProperties'; import { getType } from './getType'; -export function getModel( +export const getModel = ( openApi: OpenApi, definition: OpenApiSchema, isDefinition: boolean = false, name: string = '' -): Model { +): Model => { const model: Model = { name, export: 'interface', @@ -162,4 +162,4 @@ export function getModel( } return model; -} +}; diff --git a/src/openApi/v2/parser/getModelComposition.ts b/src/openApi/v2/parser/getModelComposition.ts index 468dbe262..6b5e4c305 100644 --- a/src/openApi/v2/parser/getModelComposition.ts +++ b/src/openApi/v2/parser/getModelComposition.ts @@ -9,13 +9,13 @@ import { getRequiredPropertiesFromComposition } from './getRequiredPropertiesFro // Fix for circular dependency export type GetModelFn = typeof getModel; -export function getModelComposition( +export const getModelComposition = ( openApi: OpenApi, definition: OpenApiSchema, definitions: OpenApiSchema[], type: 'one-of' | 'any-of' | 'all-of', getModel: GetModelFn -): ModelComposition { +): ModelComposition => { const composition: ModelComposition = { type, imports: [], @@ -87,4 +87,4 @@ export function getModelComposition( } return composition; -} +}; diff --git a/src/openApi/v2/parser/getModelProperties.ts b/src/openApi/v2/parser/getModelProperties.ts index fcb07fc9e..7560be765 100644 --- a/src/openApi/v2/parser/getModelProperties.ts +++ b/src/openApi/v2/parser/getModelProperties.ts @@ -9,7 +9,7 @@ import { getType } from './getType'; // Fix for circular dependency export type GetModelFn = typeof getModel; -export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema, getModel: GetModelFn): Model[] { +export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema, getModel: GetModelFn): Model[] => { const models: Model[] = []; for (const propertyName in definition.properties) { if (definition.properties.hasOwnProperty(propertyName)) { @@ -85,4 +85,4 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema, } } return models; -} +}; diff --git a/src/openApi/v2/parser/getModelTemplate.ts b/src/openApi/v2/parser/getModelTemplate.ts index 26902ea05..b2aa0b33a 100644 --- a/src/openApi/v2/parser/getModelTemplate.ts +++ b/src/openApi/v2/parser/getModelTemplate.ts @@ -6,6 +6,6 @@ import type { Type } from '../../../client/interfaces/Type'; * @param modelClass The parsed model class type. * @returns The model template type ( or empty). */ -export function getModelTemplate(modelClass: Type): string { +export const getModelTemplate = (modelClass: Type): string => { return modelClass.template ? '' : ''; -} +}; diff --git a/src/openApi/v2/parser/getModels.ts b/src/openApi/v2/parser/getModels.ts index b22e5c126..d9febc065 100644 --- a/src/openApi/v2/parser/getModels.ts +++ b/src/openApi/v2/parser/getModels.ts @@ -3,7 +3,7 @@ import type { OpenApi } from '../interfaces/OpenApi'; import { getModel } from './getModel'; import { getType } from './getType'; -export function getModels(openApi: OpenApi): Model[] { +export const getModels = (openApi: OpenApi): Model[] => { const models: Model[] = []; for (const definitionName in openApi.definitions) { if (openApi.definitions.hasOwnProperty(definitionName)) { @@ -14,4 +14,4 @@ export function getModels(openApi: OpenApi): Model[] { } } return models; -} +}; diff --git a/src/openApi/v2/parser/getOperation.ts b/src/openApi/v2/parser/getOperation.ts index f70e7f214..e344c7f92 100644 --- a/src/openApi/v2/parser/getOperation.ts +++ b/src/openApi/v2/parser/getOperation.ts @@ -12,17 +12,16 @@ import { getOperationResults } from './getOperationResults'; import { getServiceName } from './getServiceName'; import { sortByRequired } from './sortByRequired'; -export function getOperation( +export const getOperation = ( openApi: OpenApi, url: string, method: string, tag: string, op: OpenApiOperation, pathParams: OperationParameters -): Operation { +): Operation => { const serviceName = getServiceName(tag); - const operationNameFallback = `${method}${serviceName}`; - const operationName = getOperationName(op.operationId || operationNameFallback); + const operationName = getOperationName(op.operationId || `${method}`); const operationPath = getOperationPath(url); // Create a new operation object for this method. @@ -76,4 +75,4 @@ export function getOperation( operation.parameters = operation.parameters.sort(sortByRequired); return operation; -} +}; diff --git a/src/openApi/v2/parser/getOperationErrors.ts b/src/openApi/v2/parser/getOperationErrors.ts index 3e0ed0b9e..ef4c12e54 100644 --- a/src/openApi/v2/parser/getOperationErrors.ts +++ b/src/openApi/v2/parser/getOperationErrors.ts @@ -5,7 +5,7 @@ import type { OperationResponse } from '../../../client/interfaces/OperationResp * * @param operationResponses */ -export function getOperationErrors(operationResponses: OperationResponse[]): OperationError[] { +export const getOperationErrors = (operationResponses: OperationResponse[]): OperationError[] => { return operationResponses .filter(operationResponse => { return operationResponse.code >= 300 && operationResponse.description; @@ -14,4 +14,4 @@ export function getOperationErrors(operationResponses: OperationResponse[]): Ope code: response.code, description: response.description!, })); -} +}; diff --git a/src/openApi/v2/parser/getOperationName.ts b/src/openApi/v2/parser/getOperationName.ts index 9a993d8d4..663a758de 100644 --- a/src/openApi/v2/parser/getOperationName.ts +++ b/src/openApi/v2/parser/getOperationName.ts @@ -5,10 +5,10 @@ import camelCase from 'camelcase'; * This converts the input string to camelCase, so the method name follows * the most popular Javascript and Typescript writing style. */ -export function getOperationName(value: string): string { +export const getOperationName = (value: string): string => { const clean = value .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); return camelCase(clean); -} +}; diff --git a/src/openApi/v2/parser/getOperationParameter.ts b/src/openApi/v2/parser/getOperationParameter.ts index 7bfb136d1..31fdab6a2 100644 --- a/src/openApi/v2/parser/getOperationParameter.ts +++ b/src/openApi/v2/parser/getOperationParameter.ts @@ -12,7 +12,7 @@ import { getOperationParameterName } from './getOperationParameterName'; import { getRef } from './getRef'; import { getType } from './getType'; -export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParameter): OperationParameter { +export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => { const operationParameter: OperationParameter = { in: parameter.in, prop: parameter.name, @@ -147,4 +147,4 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame } return operationParameter; -} +}; diff --git a/src/openApi/v2/parser/getOperationParameterDefault.ts b/src/openApi/v2/parser/getOperationParameterDefault.ts index c7f97e149..87ace2b95 100644 --- a/src/openApi/v2/parser/getOperationParameterDefault.ts +++ b/src/openApi/v2/parser/getOperationParameterDefault.ts @@ -1,10 +1,10 @@ import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; -export function getOperationParameterDefault( +export const getOperationParameterDefault = ( parameter: OpenApiParameter, operationParameter: OperationParameter -): string | undefined { +): string | undefined => { if (parameter.default === undefined) { return; } @@ -39,4 +39,4 @@ export function getOperationParameterDefault( } return; -} +}; diff --git a/src/openApi/v2/parser/getOperationParameterName.ts b/src/openApi/v2/parser/getOperationParameterName.ts index 0954d7365..de9b154b7 100644 --- a/src/openApi/v2/parser/getOperationParameterName.ts +++ b/src/openApi/v2/parser/getOperationParameterName.ts @@ -7,10 +7,10 @@ const reservedWords = * Replaces any invalid characters from a parameter name. * For example: 'filter.someProperty' becomes 'filterSomeProperty'. */ -export function getOperationParameterName(value: string): string { +export const getOperationParameterName = (value: string): string => { const clean = value .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); return camelCase(clean).replace(reservedWords, '_$1'); -} +}; diff --git a/src/openApi/v2/parser/getOperationParameters.ts b/src/openApi/v2/parser/getOperationParameters.ts index 6bba9bc49..f265d1ccc 100644 --- a/src/openApi/v2/parser/getOperationParameters.ts +++ b/src/openApi/v2/parser/getOperationParameters.ts @@ -4,7 +4,7 @@ import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import { getOperationParameter } from './getOperationParameter'; import { getRef } from './getRef'; -export function getOperationParameters(openApi: OpenApi, parameters: OpenApiParameter[]): OperationParameters { +export const getOperationParameters = (openApi: OpenApi, parameters: OpenApiParameter[]): OperationParameters => { const operationParameters: OperationParameters = { imports: [], parameters: [], @@ -58,4 +58,4 @@ export function getOperationParameters(openApi: OpenApi, parameters: OpenApiPara } }); return operationParameters; -} +}; diff --git a/src/openApi/v2/parser/getOperationPath.ts b/src/openApi/v2/parser/getOperationPath.ts index 7d2a07cff..17594a2cb 100644 --- a/src/openApi/v2/parser/getOperationPath.ts +++ b/src/openApi/v2/parser/getOperationPath.ts @@ -7,10 +7,10 @@ import { getOperationParameterName } from './getOperationParameterName'; * Plus we return the correct parameter names to replace in the URL. * @param path */ -export function getOperationPath(path: string): string { +export const getOperationPath = (path: string): string => { return path .replace(/\{(.*?)\}/g, (_, w: string) => { return `\${${getOperationParameterName(w)}}`; }) .replace('${apiVersion}', '${OpenAPI.VERSION}'); -} +}; diff --git a/src/openApi/v2/parser/getOperationResponse.ts b/src/openApi/v2/parser/getOperationResponse.ts index c76119153..8f6c3ca56 100644 --- a/src/openApi/v2/parser/getOperationResponse.ts +++ b/src/openApi/v2/parser/getOperationResponse.ts @@ -7,11 +7,11 @@ import { getModel } from './getModel'; import { getRef } from './getRef'; import { getType } from './getType'; -export function getOperationResponse( +export const getOperationResponse = ( openApi: OpenApi, response: OpenApiResponse, responseCode: number -): OperationResponse { +): OperationResponse => { const operationResponse: OperationResponse = { in: 'response', name: '', @@ -96,4 +96,4 @@ export function getOperationResponse( } return operationResponse; -} +}; diff --git a/src/openApi/v2/parser/getOperationResponseCode.ts b/src/openApi/v2/parser/getOperationResponseCode.ts index c56099381..f34c99b75 100644 --- a/src/openApi/v2/parser/getOperationResponseCode.ts +++ b/src/openApi/v2/parser/getOperationResponseCode.ts @@ -1,4 +1,4 @@ -export function getOperationResponseCode(value: string | 'default'): number | null { +export const getOperationResponseCode = (value: string | 'default'): number | null => { // You can specify a "default" response, this is treated as HTTP code 200 if (value === 'default') { return 200; @@ -13,4 +13,4 @@ export function getOperationResponseCode(value: string | 'default'): number | nu } return null; -} +}; diff --git a/src/openApi/v2/parser/getOperationResponseHeader.ts b/src/openApi/v2/parser/getOperationResponseHeader.ts index 79659ae2b..09a810916 100644 --- a/src/openApi/v2/parser/getOperationResponseHeader.ts +++ b/src/openApi/v2/parser/getOperationResponseHeader.ts @@ -1,6 +1,6 @@ import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; -export function getOperationResponseHeader(operationResponses: OperationResponse[]): string | null { +export const getOperationResponseHeader = (operationResponses: OperationResponse[]): string | null => { const header = operationResponses.find(operationResponses => { return operationResponses.in === 'header'; }); @@ -8,4 +8,4 @@ export function getOperationResponseHeader(operationResponses: OperationResponse return header.name; } return null; -} +}; diff --git a/src/openApi/v2/parser/getOperationResponses.ts b/src/openApi/v2/parser/getOperationResponses.ts index 2ed23f14e..ed628e857 100644 --- a/src/openApi/v2/parser/getOperationResponses.ts +++ b/src/openApi/v2/parser/getOperationResponses.ts @@ -6,7 +6,7 @@ import { getOperationResponse } from './getOperationResponse'; import { getOperationResponseCode } from './getOperationResponseCode'; import { getRef } from './getRef'; -export function getOperationResponses(openApi: OpenApi, responses: OpenApiResponses): OperationResponse[] { +export const getOperationResponses = (openApi: OpenApi, responses: OpenApiResponses): OperationResponse[] => { const operationResponses: OperationResponse[] = []; // Iterate over each response code and get the @@ -28,4 +28,4 @@ export function getOperationResponses(openApi: OpenApi, responses: OpenApiRespon return operationResponses.sort((a, b): number => { return a.code < b.code ? -1 : a.code > b.code ? 1 : 0; }); -} +}; diff --git a/src/openApi/v2/parser/getOperationResults.ts b/src/openApi/v2/parser/getOperationResults.ts index 5d4ad2d0d..9d8111fe8 100644 --- a/src/openApi/v2/parser/getOperationResults.ts +++ b/src/openApi/v2/parser/getOperationResults.ts @@ -1,15 +1,15 @@ import type { Model } from '../../../client/interfaces/Model'; import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; -function areEqual(a: Model, b: Model): boolean { +const areEqual = (a: Model, b: Model): boolean => { const equal = a.type === b.type && a.base === b.base && a.template === b.template; if (equal && a.link && b.link) { return areEqual(a.link, b.link); } return equal; -} +}; -export function getOperationResults(operationResponses: OperationResponse[]): OperationResponse[] { +export const getOperationResults = (operationResponses: OperationResponse[]): OperationResponse[] => { const operationResults: OperationResponse[] = []; // Filter out success response codes, but skip "204 No Content" @@ -49,4 +49,4 @@ export function getOperationResults(operationResponses: OperationResponse[]): Op }) === index ); }); -} +}; diff --git a/src/openApi/v2/parser/getRef.ts b/src/openApi/v2/parser/getRef.ts index 6e67d2089..f92dfcf67 100644 --- a/src/openApi/v2/parser/getRef.ts +++ b/src/openApi/v2/parser/getRef.ts @@ -4,7 +4,7 @@ import type { OpenApiReference } from '../interfaces/OpenApiReference'; const ESCAPED_REF_SLASH = /~1/g; const ESCAPED_REF_TILDE = /~0/g; -export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { +export const getRef = (openApi: OpenApi, item: T & OpenApiReference): T => { if (item.$ref) { // Fetch the paths to the definitions, this converts: // "#/definitions/Form" to ["definitions", "Form"] @@ -29,4 +29,4 @@ export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { return result as T; } return item as T; -} +}; diff --git a/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts b/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts index 0794d6162..0b1f1859f 100644 --- a/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts +++ b/src/openApi/v2/parser/getRequiredPropertiesFromComposition.ts @@ -7,12 +7,12 @@ import { getRef } from './getRef'; // Fix for circular dependency export type GetModelFn = typeof getModel; -export function getRequiredPropertiesFromComposition( +export const getRequiredPropertiesFromComposition = ( openApi: OpenApi, required: string[], definitions: OpenApiSchema[], getModel: GetModelFn -): Model[] { +): Model[] => { return definitions .reduce((properties, definition) => { if (definition.$ref) { @@ -30,4 +30,4 @@ export function getRequiredPropertiesFromComposition( isRequired: true, }; }); -} +}; diff --git a/src/openApi/v2/parser/getServer.ts b/src/openApi/v2/parser/getServer.ts index 642fe34de..0b1913883 100644 --- a/src/openApi/v2/parser/getServer.ts +++ b/src/openApi/v2/parser/getServer.ts @@ -4,10 +4,10 @@ import type { OpenApi } from '../interfaces/OpenApi'; * Get the base server url. * @param openApi */ -export function getServer(openApi: OpenApi): string { +export const getServer = (openApi: OpenApi): string => { const scheme = openApi.schemes?.[0] || 'http'; const host = openApi.host; const basePath = openApi.basePath || ''; const url = host ? `${scheme}://${host}${basePath}` : basePath; return url.replace(/\/$/g, ''); -} +}; diff --git a/src/openApi/v2/parser/getServiceName.ts b/src/openApi/v2/parser/getServiceName.ts index 14a003b15..b5b1718f2 100644 --- a/src/openApi/v2/parser/getServiceName.ts +++ b/src/openApi/v2/parser/getServiceName.ts @@ -4,10 +4,10 @@ import camelCase from 'camelcase'; * Convert the input value to a correct service name. This converts * the input string to PascalCase. */ -export function getServiceName(value: string): string { +export const getServiceName = (value: string): string => { const clean = value .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); return camelCase(clean, { pascalCase: true }); -} +}; diff --git a/src/openApi/v2/parser/getServiceVersion.ts b/src/openApi/v2/parser/getServiceVersion.ts index 7d860d6db..9c7a8a04b 100644 --- a/src/openApi/v2/parser/getServiceVersion.ts +++ b/src/openApi/v2/parser/getServiceVersion.ts @@ -3,6 +3,6 @@ * This basically removes any "v" prefix from the version string. * @param version */ -export function getServiceVersion(version = '1.0'): string { +export const getServiceVersion = (version = '1.0'): string => { return String(version).replace(/^v/gi, ''); -} +}; diff --git a/src/openApi/v2/parser/getServices.ts b/src/openApi/v2/parser/getServices.ts index e52f84967..d8fe411bb 100644 --- a/src/openApi/v2/parser/getServices.ts +++ b/src/openApi/v2/parser/getServices.ts @@ -7,7 +7,7 @@ import { getOperationParameters } from './getOperationParameters'; /** * Get the OpenAPI services */ -export function getServices(openApi: OpenApi): Service[] { +export const getServices = (openApi: OpenApi): Service[] => { const services = new Map(); for (const url in openApi.paths) { if (openApi.paths.hasOwnProperty(url)) { @@ -28,7 +28,7 @@ export function getServices(openApi: OpenApi): Service[] { case 'patch': // Each method contains an OpenAPI operation, we parse the operation const op = path[method]!; - const tags = op.tags?.length ? op.tags.filter(unique) : ['']; + const tags = op.tags?.length ? op.tags.filter(unique) : ['Default']; tags.forEach(tag => { const operation = getOperation(openApi, url, method, tag, op, pathParams); @@ -52,4 +52,4 @@ export function getServices(openApi: OpenApi): Service[] { } } return Array.from(services.values()); -} +}; diff --git a/src/openApi/v2/parser/getType.ts b/src/openApi/v2/parser/getType.ts index 499056233..6caa1e015 100644 --- a/src/openApi/v2/parser/getType.ts +++ b/src/openApi/v2/parser/getType.ts @@ -2,16 +2,16 @@ import type { Type } from '../../../client/interfaces/Type'; import { getMappedType } from './getMappedType'; import { stripNamespace } from './stripNamespace'; -function encode(value: string): string { +const encode = (value: string): string => { return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^\w$]+/g, '_'); -} +}; /** * Parse any string value into a type object. * @param type String value like "integer" or "Link[Model]". * @param format String value like "binary" or "date". */ -export function getType(type: string = 'any', format?: string): Type { +export const getType = (type: string = 'any', format?: string): Type => { const result: Type = { type: 'any', base: 'any', @@ -64,4 +64,4 @@ export function getType(type: string = 'any', format?: string): Type { } return result; -} +}; diff --git a/src/openApi/v2/parser/sortByRequired.ts b/src/openApi/v2/parser/sortByRequired.ts index d95332644..6519bb17e 100644 --- a/src/openApi/v2/parser/sortByRequired.ts +++ b/src/openApi/v2/parser/sortByRequired.ts @@ -1,9 +1,9 @@ import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; -export function sortByRequired(a: OperationParameter, b: OperationParameter): number { +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/v2/parser/stripNamespace.ts b/src/openApi/v2/parser/stripNamespace.ts index fa124be9b..ff543b3c9 100644 --- a/src/openApi/v2/parser/stripNamespace.ts +++ b/src/openApi/v2/parser/stripNamespace.ts @@ -2,11 +2,11 @@ * Strip (OpenAPI) namespaces fom values. * @param value */ -export function stripNamespace(value: string): string { +export const stripNamespace = (value: string): string => { return value .trim() .replace(/^#\/definitions\//, '') .replace(/^#\/parameters\//, '') .replace(/^#\/responses\//, '') .replace(/^#\/securityDefinitions\//, ''); -} +}; diff --git a/src/openApi/v3/index.ts b/src/openApi/v3/index.ts index 30bd05ccc..9dbdadb34 100644 --- a/src/openApi/v3/index.ts +++ b/src/openApi/v3/index.ts @@ -10,11 +10,11 @@ import { getServiceVersion } from './parser/getServiceVersion'; * all the models, services and schema's we should output. * @param openApi The OpenAPI spec that we have loaded from disk. */ -export function parse(openApi: OpenApi): Client { +export const parse = (openApi: OpenApi): Client => { const version = getServiceVersion(openApi.info.version); const server = getServer(openApi); const models = getModels(openApi); const services = getServices(openApi); return { version, server, models, services }; -} +}; diff --git a/src/openApi/v3/parser/escapeName.ts b/src/openApi/v3/parser/escapeName.ts index 0a26cb659..6b23d934b 100644 --- a/src/openApi/v3/parser/escapeName.ts +++ b/src/openApi/v3/parser/escapeName.ts @@ -1,4 +1,4 @@ -export function escapeName(value: string): string { +export const escapeName = (value: string): string => { if (value) { const validName = /^[a-zA-Z_$][\w$]+$/g.test(value); if (!validName) { @@ -6,4 +6,4 @@ export function escapeName(value: string): string { } } return value; -} +}; diff --git a/src/openApi/v3/parser/extendEnum.ts b/src/openApi/v3/parser/extendEnum.ts index 81e85fdf3..0a5ebeb1e 100644 --- a/src/openApi/v3/parser/extendEnum.ts +++ b/src/openApi/v3/parser/extendEnum.ts @@ -7,7 +7,7 @@ import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtensi * @param enumerators * @param definition */ -export function extendEnum(enumerators: Enum[], definition: WithEnumExtension): Enum[] { +export const extendEnum = (enumerators: Enum[], definition: WithEnumExtension): Enum[] => { const names = definition['x-enum-varnames']; const descriptions = definition['x-enum-descriptions']; @@ -17,4 +17,4 @@ export function extendEnum(enumerators: Enum[], definition: WithEnumExtension): value: enumerator.value, type: enumerator.type, })); -} +}; diff --git a/src/openApi/v3/parser/getContent.ts b/src/openApi/v3/parser/getContent.ts index 84f5cc119..976625817 100644 --- a/src/openApi/v3/parser/getContent.ts +++ b/src/openApi/v3/parser/getContent.ts @@ -21,7 +21,7 @@ const BASIC_MEDIA_TYPES = [ 'multipart/batch', ]; -export function getContent(openApi: OpenApi, content: Dictionary): Content | null { +export const getContent = (openApi: OpenApi, content: Dictionary): Content | null => { const basicMediaTypeWithSchema = Object.keys(content) .filter(mediaType => { const cleanMediaType = mediaType.split(';')[0].trim(); @@ -43,4 +43,4 @@ export function getContent(openApi: OpenApi, content: Dictionary { if (Array.isArray(values)) { return values .filter((value, index, arr) => { @@ -30,4 +30,4 @@ export function getEnum(values?: (string | number)[]): Enum[] { }); } return []; -} +}; diff --git a/src/openApi/v3/parser/getEnumFromDescription.ts b/src/openApi/v3/parser/getEnumFromDescription.ts index d1154e6e3..0f49cf408 100644 --- a/src/openApi/v3/parser/getEnumFromDescription.ts +++ b/src/openApi/v3/parser/getEnumFromDescription.ts @@ -3,7 +3,7 @@ import type { Enum } from '../../../client/interfaces/Enum'; /** * @deprecated */ -export function getEnumFromDescription(description: string): Enum[] { +export const getEnumFromDescription = (description: string): Enum[] => { // Check if we can find this special format string: // None=0,Something=1,AnotherThing=2 if (/^(\w+=[0-9]+)/g.test(description)) { @@ -36,4 +36,4 @@ export function getEnumFromDescription(description: string): Enum[] { } return []; -} +}; diff --git a/src/openApi/v3/parser/getMappedType.ts b/src/openApi/v3/parser/getMappedType.ts index e44434e2a..a7c32fd84 100644 --- a/src/openApi/v3/parser/getMappedType.ts +++ b/src/openApi/v3/parser/getMappedType.ts @@ -24,9 +24,9 @@ const TYPE_MAPPINGS = new Map([ /** * Get mapped type for given type to any basic Typescript/Javascript type. */ -export function getMappedType(type: string, format?: string): string | undefined { +export const getMappedType = (type: string, format?: string): string | undefined => { if (format === 'binary') { return 'binary'; } return TYPE_MAPPINGS.get(type); -} +}; diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index a44ebb0dd..4b0068300 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -10,12 +10,12 @@ import { getModelDefault } from './getModelDefault'; import { getModelProperties } from './getModelProperties'; import { getType } from './getType'; -export function getModel( +export const getModel = ( openApi: OpenApi, definition: OpenApiSchema, isDefinition: boolean = false, name: string = '' -): Model { +): Model => { const model: Model = { name, export: 'interface', @@ -191,4 +191,4 @@ export function getModel( } return model; -} +}; diff --git a/src/openApi/v3/parser/getModelComposition.ts b/src/openApi/v3/parser/getModelComposition.ts index 468dbe262..6b5e4c305 100644 --- a/src/openApi/v3/parser/getModelComposition.ts +++ b/src/openApi/v3/parser/getModelComposition.ts @@ -9,13 +9,13 @@ import { getRequiredPropertiesFromComposition } from './getRequiredPropertiesFro // Fix for circular dependency export type GetModelFn = typeof getModel; -export function getModelComposition( +export const getModelComposition = ( openApi: OpenApi, definition: OpenApiSchema, definitions: OpenApiSchema[], type: 'one-of' | 'any-of' | 'all-of', getModel: GetModelFn -): ModelComposition { +): ModelComposition => { const composition: ModelComposition = { type, imports: [], @@ -87,4 +87,4 @@ export function getModelComposition( } return composition; -} +}; diff --git a/src/openApi/v3/parser/getModelDefault.ts b/src/openApi/v3/parser/getModelDefault.ts index e56594e68..3182d7ac6 100644 --- a/src/openApi/v3/parser/getModelDefault.ts +++ b/src/openApi/v3/parser/getModelDefault.ts @@ -1,7 +1,7 @@ import type { Model } from '../../../client/interfaces/Model'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; -export function getModelDefault(definition: OpenApiSchema, model?: Model): string | undefined { +export const getModelDefault = (definition: OpenApiSchema, model?: Model): string | undefined => { if (definition.default === undefined) { return; } @@ -36,4 +36,4 @@ export function getModelDefault(definition: OpenApiSchema, model?: Model): strin } return; -} +}; diff --git a/src/openApi/v3/parser/getModelProperties.ts b/src/openApi/v3/parser/getModelProperties.ts index 9c9833da1..1c2cbff05 100644 --- a/src/openApi/v3/parser/getModelProperties.ts +++ b/src/openApi/v3/parser/getModelProperties.ts @@ -10,12 +10,12 @@ import { getType } from './getType'; // Fix for circular dependency export type GetModelFn = typeof getModel; -export function getModelProperties( +export const getModelProperties = ( openApi: OpenApi, definition: OpenApiSchema, getModel: GetModelFn, parent?: Model -): Model[] { +): Model[] => { const models: Model[] = []; const discriminator = findOneOfParentDiscriminator(openApi, parent); for (const propertyName in definition.properties) { @@ -104,4 +104,4 @@ export function getModelProperties( } return models; -} +}; diff --git a/src/openApi/v3/parser/getModelTemplate.ts b/src/openApi/v3/parser/getModelTemplate.ts index 26902ea05..b2aa0b33a 100644 --- a/src/openApi/v3/parser/getModelTemplate.ts +++ b/src/openApi/v3/parser/getModelTemplate.ts @@ -6,6 +6,6 @@ import type { Type } from '../../../client/interfaces/Type'; * @param modelClass The parsed model class type. * @returns The model template type ( or empty). */ -export function getModelTemplate(modelClass: Type): string { +export const getModelTemplate = (modelClass: Type): string => { return modelClass.template ? '' : ''; -} +}; diff --git a/src/openApi/v3/parser/getModels.ts b/src/openApi/v3/parser/getModels.ts index 6294597d6..3ca6393e3 100644 --- a/src/openApi/v3/parser/getModels.ts +++ b/src/openApi/v3/parser/getModels.ts @@ -3,7 +3,7 @@ import type { OpenApi } from '../interfaces/OpenApi'; import { getModel } from './getModel'; import { getType } from './getType'; -export function getModels(openApi: OpenApi): Model[] { +export const getModels = (openApi: OpenApi): Model[] => { const models: Model[] = []; if (openApi.components) { for (const definitionName in openApi.components.schemas) { @@ -16,4 +16,4 @@ export function getModels(openApi: OpenApi): Model[] { } } return models; -} +}; diff --git a/src/openApi/v3/parser/getOperation.ts b/src/openApi/v3/parser/getOperation.ts index de452712b..027d9f136 100644 --- a/src/openApi/v3/parser/getOperation.ts +++ b/src/openApi/v3/parser/getOperation.ts @@ -15,17 +15,16 @@ import { getRef } from './getRef'; import { getServiceName } from './getServiceName'; import { sortByRequired } from './sortByRequired'; -export function getOperation( +export const getOperation = ( openApi: OpenApi, url: string, method: string, tag: string, op: OpenApiOperation, pathParams: OperationParameters -): Operation { +): Operation => { const serviceName = getServiceName(tag); - const operationNameFallback = `${method}${serviceName}`; - const operationName = getOperationName(op.operationId || operationNameFallback); + const operationName = getOperationName(op.operationId || `${method}`); const operationPath = getOperationPath(url); // Create a new operation object for this method. @@ -87,4 +86,4 @@ export function getOperation( operation.parameters = operation.parameters.sort(sortByRequired); return operation; -} +}; diff --git a/src/openApi/v3/parser/getOperationErrors.ts b/src/openApi/v3/parser/getOperationErrors.ts index 9703f91b3..e7624adb5 100644 --- a/src/openApi/v3/parser/getOperationErrors.ts +++ b/src/openApi/v3/parser/getOperationErrors.ts @@ -1,7 +1,7 @@ import type { OperationError } from '../../../client/interfaces/OperationError'; import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; -export function getOperationErrors(operationResponses: OperationResponse[]): OperationError[] { +export const getOperationErrors = (operationResponses: OperationResponse[]): OperationError[] => { return operationResponses .filter(operationResponse => { return operationResponse.code >= 300 && operationResponse.description; @@ -10,4 +10,4 @@ export function getOperationErrors(operationResponses: OperationResponse[]): Ope code: response.code, description: response.description!, })); -} +}; diff --git a/src/openApi/v3/parser/getOperationName.ts b/src/openApi/v3/parser/getOperationName.ts index 9a993d8d4..663a758de 100644 --- a/src/openApi/v3/parser/getOperationName.ts +++ b/src/openApi/v3/parser/getOperationName.ts @@ -5,10 +5,10 @@ import camelCase from 'camelcase'; * This converts the input string to camelCase, so the method name follows * the most popular Javascript and Typescript writing style. */ -export function getOperationName(value: string): string { +export const getOperationName = (value: string): string => { const clean = value .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); return camelCase(clean); -} +}; diff --git a/src/openApi/v3/parser/getOperationParameter.ts b/src/openApi/v3/parser/getOperationParameter.ts index 89ef5f355..90b8a8e70 100644 --- a/src/openApi/v3/parser/getOperationParameter.ts +++ b/src/openApi/v3/parser/getOperationParameter.ts @@ -9,7 +9,7 @@ import { getOperationParameterName } from './getOperationParameterName'; import { getRef } from './getRef'; import { getType } from './getType'; -export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParameter): OperationParameter { +export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => { const operationParameter: OperationParameter = { in: parameter.in, prop: parameter.name, @@ -89,4 +89,4 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame } return operationParameter; -} +}; diff --git a/src/openApi/v3/parser/getOperationParameterName.ts b/src/openApi/v3/parser/getOperationParameterName.ts index 0954d7365..de9b154b7 100644 --- a/src/openApi/v3/parser/getOperationParameterName.ts +++ b/src/openApi/v3/parser/getOperationParameterName.ts @@ -7,10 +7,10 @@ const reservedWords = * Replaces any invalid characters from a parameter name. * For example: 'filter.someProperty' becomes 'filterSomeProperty'. */ -export function getOperationParameterName(value: string): string { +export const getOperationParameterName = (value: string): string => { const clean = value .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); return camelCase(clean).replace(reservedWords, '_$1'); -} +}; diff --git a/src/openApi/v3/parser/getOperationParameters.ts b/src/openApi/v3/parser/getOperationParameters.ts index f4cc7503e..051b4a0a9 100644 --- a/src/openApi/v3/parser/getOperationParameters.ts +++ b/src/openApi/v3/parser/getOperationParameters.ts @@ -4,7 +4,7 @@ import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import { getOperationParameter } from './getOperationParameter'; import { getRef } from './getRef'; -export function getOperationParameters(openApi: OpenApi, parameters: OpenApiParameter[]): OperationParameters { +export const getOperationParameters = (openApi: OpenApi, parameters: OpenApiParameter[]): OperationParameters => { const operationParameters: OperationParameters = { imports: [], parameters: [], @@ -58,4 +58,4 @@ export function getOperationParameters(openApi: OpenApi, parameters: OpenApiPara } }); return operationParameters; -} +}; diff --git a/src/openApi/v3/parser/getOperationPath.ts b/src/openApi/v3/parser/getOperationPath.ts index 7d2a07cff..17594a2cb 100644 --- a/src/openApi/v3/parser/getOperationPath.ts +++ b/src/openApi/v3/parser/getOperationPath.ts @@ -7,10 +7,10 @@ import { getOperationParameterName } from './getOperationParameterName'; * Plus we return the correct parameter names to replace in the URL. * @param path */ -export function getOperationPath(path: string): string { +export const getOperationPath = (path: string): string => { return path .replace(/\{(.*?)\}/g, (_, w: string) => { return `\${${getOperationParameterName(w)}}`; }) .replace('${apiVersion}', '${OpenAPI.VERSION}'); -} +}; diff --git a/src/openApi/v3/parser/getOperationRequestBody.ts b/src/openApi/v3/parser/getOperationRequestBody.ts index be0b8eebf..9f9cca241 100644 --- a/src/openApi/v3/parser/getOperationRequestBody.ts +++ b/src/openApi/v3/parser/getOperationRequestBody.ts @@ -6,7 +6,7 @@ import { getContent } from './getContent'; import { getModel } from './getModel'; import { getType } from './getType'; -export function getOperationRequestBody(openApi: OpenApi, body: OpenApiRequestBody): OperationParameter { +export const getOperationRequestBody = (openApi: OpenApi, body: OpenApiRequestBody): OperationParameter => { const requestBody: OperationParameter = { in: 'body', export: 'interface', @@ -83,4 +83,4 @@ export function getOperationRequestBody(openApi: OpenApi, body: OpenApiRequestBo } return requestBody; -} +}; diff --git a/src/openApi/v3/parser/getOperationResponse.ts b/src/openApi/v3/parser/getOperationResponse.ts index 8b91980e0..dff19ec13 100644 --- a/src/openApi/v3/parser/getOperationResponse.ts +++ b/src/openApi/v3/parser/getOperationResponse.ts @@ -8,11 +8,11 @@ import { getModel } from './getModel'; import { getRef } from './getRef'; import { getType } from './getType'; -export function getOperationResponse( +export const getOperationResponse = ( openApi: OpenApi, response: OpenApiResponse, responseCode: number -): OperationResponse { +): OperationResponse => { const operationResponse: OperationResponse = { in: 'response', name: '', @@ -95,4 +95,4 @@ export function getOperationResponse( } return operationResponse; -} +}; diff --git a/src/openApi/v3/parser/getOperationResponseCode.ts b/src/openApi/v3/parser/getOperationResponseCode.ts index c56099381..f34c99b75 100644 --- a/src/openApi/v3/parser/getOperationResponseCode.ts +++ b/src/openApi/v3/parser/getOperationResponseCode.ts @@ -1,4 +1,4 @@ -export function getOperationResponseCode(value: string | 'default'): number | null { +export const getOperationResponseCode = (value: string | 'default'): number | null => { // You can specify a "default" response, this is treated as HTTP code 200 if (value === 'default') { return 200; @@ -13,4 +13,4 @@ export function getOperationResponseCode(value: string | 'default'): number | nu } return null; -} +}; diff --git a/src/openApi/v3/parser/getOperationResponseHeader.ts b/src/openApi/v3/parser/getOperationResponseHeader.ts index 79659ae2b..09a810916 100644 --- a/src/openApi/v3/parser/getOperationResponseHeader.ts +++ b/src/openApi/v3/parser/getOperationResponseHeader.ts @@ -1,6 +1,6 @@ import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; -export function getOperationResponseHeader(operationResponses: OperationResponse[]): string | null { +export const getOperationResponseHeader = (operationResponses: OperationResponse[]): string | null => { const header = operationResponses.find(operationResponses => { return operationResponses.in === 'header'; }); @@ -8,4 +8,4 @@ export function getOperationResponseHeader(operationResponses: OperationResponse return header.name; } return null; -} +}; diff --git a/src/openApi/v3/parser/getOperationResponses.ts b/src/openApi/v3/parser/getOperationResponses.ts index 2ed23f14e..ed628e857 100644 --- a/src/openApi/v3/parser/getOperationResponses.ts +++ b/src/openApi/v3/parser/getOperationResponses.ts @@ -6,7 +6,7 @@ import { getOperationResponse } from './getOperationResponse'; import { getOperationResponseCode } from './getOperationResponseCode'; import { getRef } from './getRef'; -export function getOperationResponses(openApi: OpenApi, responses: OpenApiResponses): OperationResponse[] { +export const getOperationResponses = (openApi: OpenApi, responses: OpenApiResponses): OperationResponse[] => { const operationResponses: OperationResponse[] = []; // Iterate over each response code and get the @@ -28,4 +28,4 @@ export function getOperationResponses(openApi: OpenApi, responses: OpenApiRespon return operationResponses.sort((a, b): number => { return a.code < b.code ? -1 : a.code > b.code ? 1 : 0; }); -} +}; diff --git a/src/openApi/v3/parser/getOperationResults.ts b/src/openApi/v3/parser/getOperationResults.ts index 5d4ad2d0d..9d8111fe8 100644 --- a/src/openApi/v3/parser/getOperationResults.ts +++ b/src/openApi/v3/parser/getOperationResults.ts @@ -1,15 +1,15 @@ import type { Model } from '../../../client/interfaces/Model'; import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; -function areEqual(a: Model, b: Model): boolean { +const areEqual = (a: Model, b: Model): boolean => { const equal = a.type === b.type && a.base === b.base && a.template === b.template; if (equal && a.link && b.link) { return areEqual(a.link, b.link); } return equal; -} +}; -export function getOperationResults(operationResponses: OperationResponse[]): OperationResponse[] { +export const getOperationResults = (operationResponses: OperationResponse[]): OperationResponse[] => { const operationResults: OperationResponse[] = []; // Filter out success response codes, but skip "204 No Content" @@ -49,4 +49,4 @@ export function getOperationResults(operationResponses: OperationResponse[]): Op }) === index ); }); -} +}; diff --git a/src/openApi/v3/parser/getRef.ts b/src/openApi/v3/parser/getRef.ts index d37373118..2c42690ee 100644 --- a/src/openApi/v3/parser/getRef.ts +++ b/src/openApi/v3/parser/getRef.ts @@ -4,7 +4,7 @@ import type { OpenApiReference } from '../interfaces/OpenApiReference'; const ESCAPED_REF_SLASH = /~1/g; const ESCAPED_REF_TILDE = /~0/g; -export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { +export const getRef = (openApi: OpenApi, item: T & OpenApiReference): T => { if (item.$ref) { // Fetch the paths to the definitions, this converts: // "#/components/schemas/Form" to ["components", "schemas", "Form"] @@ -29,4 +29,4 @@ export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { return result as T; } return item as T; -} +}; diff --git a/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts b/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts index 0794d6162..0b1f1859f 100644 --- a/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts +++ b/src/openApi/v3/parser/getRequiredPropertiesFromComposition.ts @@ -7,12 +7,12 @@ import { getRef } from './getRef'; // Fix for circular dependency export type GetModelFn = typeof getModel; -export function getRequiredPropertiesFromComposition( +export const getRequiredPropertiesFromComposition = ( openApi: OpenApi, required: string[], definitions: OpenApiSchema[], getModel: GetModelFn -): Model[] { +): Model[] => { return definitions .reduce((properties, definition) => { if (definition.$ref) { @@ -30,4 +30,4 @@ export function getRequiredPropertiesFromComposition( isRequired: true, }; }); -} +}; diff --git a/src/openApi/v3/parser/getServer.ts b/src/openApi/v3/parser/getServer.ts index 57d5447ba..dd60b1cf3 100644 --- a/src/openApi/v3/parser/getServer.ts +++ b/src/openApi/v3/parser/getServer.ts @@ -1,6 +1,6 @@ import type { OpenApi } from '../interfaces/OpenApi'; -export function getServer(openApi: OpenApi): string { +export const getServer = (openApi: OpenApi): string => { const server = openApi.servers?.[0]; const variables = server?.variables || {}; let url = server?.url || ''; @@ -10,4 +10,4 @@ export function getServer(openApi: OpenApi): string { } } return url.replace(/\/$/g, ''); -} +}; diff --git a/src/openApi/v3/parser/getServiceName.ts b/src/openApi/v3/parser/getServiceName.ts index 14a003b15..b5b1718f2 100644 --- a/src/openApi/v3/parser/getServiceName.ts +++ b/src/openApi/v3/parser/getServiceName.ts @@ -4,10 +4,10 @@ import camelCase from 'camelcase'; * Convert the input value to a correct service name. This converts * the input string to PascalCase. */ -export function getServiceName(value: string): string { +export const getServiceName = (value: string): string => { const clean = value .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); return camelCase(clean, { pascalCase: true }); -} +}; diff --git a/src/openApi/v3/parser/getServiceVersion.ts b/src/openApi/v3/parser/getServiceVersion.ts index 7d860d6db..9c7a8a04b 100644 --- a/src/openApi/v3/parser/getServiceVersion.ts +++ b/src/openApi/v3/parser/getServiceVersion.ts @@ -3,6 +3,6 @@ * This basically removes any "v" prefix from the version string. * @param version */ -export function getServiceVersion(version = '1.0'): string { +export const getServiceVersion = (version = '1.0'): string => { return String(version).replace(/^v/gi, ''); -} +}; diff --git a/src/openApi/v3/parser/getServices.ts b/src/openApi/v3/parser/getServices.ts index e52f84967..d8fe411bb 100644 --- a/src/openApi/v3/parser/getServices.ts +++ b/src/openApi/v3/parser/getServices.ts @@ -7,7 +7,7 @@ import { getOperationParameters } from './getOperationParameters'; /** * Get the OpenAPI services */ -export function getServices(openApi: OpenApi): Service[] { +export const getServices = (openApi: OpenApi): Service[] => { const services = new Map(); for (const url in openApi.paths) { if (openApi.paths.hasOwnProperty(url)) { @@ -28,7 +28,7 @@ export function getServices(openApi: OpenApi): Service[] { case 'patch': // Each method contains an OpenAPI operation, we parse the operation const op = path[method]!; - const tags = op.tags?.length ? op.tags.filter(unique) : ['']; + const tags = op.tags?.length ? op.tags.filter(unique) : ['Default']; tags.forEach(tag => { const operation = getOperation(openApi, url, method, tag, op, pathParams); @@ -52,4 +52,4 @@ export function getServices(openApi: OpenApi): Service[] { } } return Array.from(services.values()); -} +}; diff --git a/src/openApi/v3/parser/getType.ts b/src/openApi/v3/parser/getType.ts index d62453db8..e8ef4733d 100644 --- a/src/openApi/v3/parser/getType.ts +++ b/src/openApi/v3/parser/getType.ts @@ -3,16 +3,16 @@ import { isDefined } from '../../../utils/isDefined'; import { getMappedType } from './getMappedType'; import { stripNamespace } from './stripNamespace'; -function encode(value: string): string { +const encode = (value: string): string => { return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^\w$]+/g, '_'); -} +}; /** * Parse any string value into a type object. * @param type String or String[] value like "integer", "Link[Model]" or ["string", "null"]. * @param format String value like "binary" or "date". */ -export function getType(type: string | string[] = 'any', format?: string): Type { +export const getType = (type: string | string[] = 'any', format?: string): Type => { const result: Type = { type: 'any', base: 'any', @@ -79,4 +79,4 @@ export function getType(type: string | string[] = 'any', format?: string): Type } return result; -} +}; diff --git a/src/openApi/v3/parser/sortByRequired.ts b/src/openApi/v3/parser/sortByRequired.ts index d95332644..6519bb17e 100644 --- a/src/openApi/v3/parser/sortByRequired.ts +++ b/src/openApi/v3/parser/sortByRequired.ts @@ -1,9 +1,9 @@ import type { OperationParameter } from '../../../client/interfaces/OperationParameter'; -export function sortByRequired(a: OperationParameter, b: OperationParameter): number { +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/parser/stripNamespace.ts b/src/openApi/v3/parser/stripNamespace.ts index 6ff55b7a7..76b9d02d2 100644 --- a/src/openApi/v3/parser/stripNamespace.ts +++ b/src/openApi/v3/parser/stripNamespace.ts @@ -2,7 +2,7 @@ * Strip (OpenAPI) namespaces fom values. * @param value */ -export function stripNamespace(value: string): string { +export const stripNamespace = (value: string): string => { return value .trim() .replace(/^#\/components\/schemas\//, '') @@ -14,4 +14,4 @@ export function stripNamespace(value: string): string { .replace(/^#\/components\/securitySchemes\//, '') .replace(/^#\/components\/links\//, '') .replace(/^#\/components\/callbacks\//, ''); -} +}; diff --git a/src/templates/__mocks__/index.ts b/src/templates/__mocks__/index.ts index ba8c8cdbb..7f78e06f9 100644 --- a/src/templates/__mocks__/index.ts +++ b/src/templates/__mocks__/index.ts @@ -1,7 +1,7 @@ export default { compiler: [8, '>= 4.3.0'], useData: true, - main: function () { + main: () => { return ''; }, }; diff --git a/src/templates/client.hbs b/src/templates/client.hbs index aea06c0e9..33cc0f1d9 100644 --- a/src/templates/client.hbs +++ b/src/templates/client.hbs @@ -14,7 +14,7 @@ type HttpRequestConstructor = new (config: OpenAPIConfig) => BaseHttpRequest; export class {{{clientName}}} { {{#each services}} - public readonly {{{name}}}: {{{name}}}{{{@root.postfix}}}; + public readonly {{{camelCase name}}}: {{{name}}}{{{@root.postfix}}}; {{/each}} public readonly request: BaseHttpRequest; @@ -33,7 +33,7 @@ export class {{{clientName}}} { }); {{#each services}} - this.{{{name}}} = new {{{name}}}{{{@root.postfix}}}(this.request); + this.{{{camelCase name}}} = new {{{name}}}{{{@root.postfix}}}(this.request); {{/each}} } } diff --git a/src/utils/discriminator.ts b/src/utils/discriminator.ts index 1bbffa3ea..3b45c4eb7 100644 --- a/src/utils/discriminator.ts +++ b/src/utils/discriminator.ts @@ -4,15 +4,15 @@ import type { OpenApiDiscriminator } from '../openApi/v3/interfaces/OpenApiDiscr import { stripNamespace } from '../openApi/v3/parser/stripNamespace'; import type { Dictionary } from './types'; -function inverseDictionary(map: Dictionary): Dictionary { +const inverseDictionary = (map: Dictionary): Dictionary => { const m2: Dictionary = {}; for (const key in map) { m2[map[key]] = key; } return m2; -} +}; -export function findOneOfParentDiscriminator(openApi: OpenApi, parent?: Model): OpenApiDiscriminator | undefined { +export const findOneOfParentDiscriminator = (openApi: OpenApi, parent?: Model): OpenApiDiscriminator | undefined => { if (openApi.components && parent) { for (const definitionName in openApi.components.schemas) { if (openApi.components.schemas.hasOwnProperty(definitionName)) { @@ -28,9 +28,9 @@ export function findOneOfParentDiscriminator(openApi: OpenApi, parent?: Model): } } return; -} +}; -export function mapPropertyValue(discriminator: OpenApiDiscriminator, parent: Model): string { +export const mapPropertyValue = (discriminator: OpenApiDiscriminator, parent: Model): string => { if (discriminator.mapping) { const mapping = inverseDictionary(discriminator.mapping); const key = Object.keys(mapping).find(item => stripNamespace(item) == parent.name); @@ -39,4 +39,4 @@ export function mapPropertyValue(discriminator: OpenApiDiscriminator, parent: Mo } } return parent.name; -} +}; diff --git a/src/utils/flatMap.ts b/src/utils/flatMap.ts index 26fd4f28e..7dbcb3150 100644 --- a/src/utils/flatMap.ts +++ b/src/utils/flatMap.ts @@ -1,11 +1,11 @@ /** - * Calls a defined callback function on each element of an array. + * Calls a defined callback on each element of an array. * Then, flattens the result into a new array. */ -export function flatMap(array: T[], callback: (value: T, index: number, array: T[]) => U[]): U[] { +export const flatMap = (array: T[], callback: (value: T, index: number, array: T[]) => U[]): U[] => { const result: U[] = []; array.map(callback).forEach(arr => { result.push(...arr); }); return result; -} +}; diff --git a/src/utils/formatCode.ts b/src/utils/formatCode.ts index 17e457744..42cfd5cf4 100644 --- a/src/utils/formatCode.ts +++ b/src/utils/formatCode.ts @@ -1,6 +1,6 @@ import { EOL } from 'os'; -export function formatCode(s: string): string { +export const formatCode = (s: string): string => { let indent: number = 0; let lines = s.split(EOL); lines = lines.map(line => { @@ -20,4 +20,4 @@ export function formatCode(s: string): string { return result; }); return lines.join(EOL); -} +}; diff --git a/src/utils/formatIndentation.ts b/src/utils/formatIndentation.ts index 903eec791..a2764d99c 100644 --- a/src/utils/formatIndentation.ts +++ b/src/utils/formatIndentation.ts @@ -2,7 +2,7 @@ import { EOL } from 'os'; import { Indent } from '../Indent'; -export function formatIndentation(s: string, indent: Indent): string { +export const formatIndentation = (s: string, indent: Indent): string => { let lines = s.split(EOL); lines = lines.map(line => { switch (indent) { @@ -15,4 +15,4 @@ export function formatIndentation(s: string, indent: Indent): string { } }); return lines.join(EOL); -} +}; diff --git a/src/utils/getModelNames.ts b/src/utils/getModelNames.ts index 78cd58b18..26b76f713 100644 --- a/src/utils/getModelNames.ts +++ b/src/utils/getModelNames.ts @@ -1,6 +1,6 @@ import type { Model } from '../client/interfaces/Model'; import { sort } from './sort'; -export function getModelNames(models: Model[]): string[] { +export const getModelNames = (models: Model[]): string[] => { return models.map(model => model.name).sort(sort); -} +}; diff --git a/src/utils/getOpenApiSpec.ts b/src/utils/getOpenApiSpec.ts index 0d6407f0f..9b52ccd2e 100644 --- a/src/utils/getOpenApiSpec.ts +++ b/src/utils/getOpenApiSpec.ts @@ -6,6 +6,6 @@ import RefParser from 'json-schema-ref-parser'; * on parsing the file as JSON. * @param location: Path or url */ -export async function getOpenApiSpec(location: string): Promise { +export const getOpenApiSpec = async (location: string): Promise => { return await RefParser.bundle(location, location, {}); -} +}; diff --git a/src/utils/getOpenApiVersion.ts b/src/utils/getOpenApiVersion.ts index 8c88d497c..9c7cc7e47 100644 --- a/src/utils/getOpenApiVersion.ts +++ b/src/utils/getOpenApiVersion.ts @@ -9,7 +9,7 @@ export enum OpenApiVersion { * an incompatible type. Or if the type is missing... * @param openApi The loaded spec (can be any object) */ -export function getOpenApiVersion(openApi: any): OpenApiVersion { +export const getOpenApiVersion = (openApi: any): OpenApiVersion => { const info: any = openApi.swagger || openApi.openapi; if (typeof info === 'string') { const c = info.charAt(0); @@ -19,4 +19,4 @@ export function getOpenApiVersion(openApi: any): OpenApiVersion { } } throw new Error(`Unsupported Open API version: "${String(info)}"`); -} +}; diff --git a/src/utils/getPattern.ts b/src/utils/getPattern.ts index a4f54bfae..58ee78532 100644 --- a/src/utils/getPattern.ts +++ b/src/utils/getPattern.ts @@ -5,6 +5,6 @@ * to make it a valid regexp string. * @param pattern */ -export function getPattern(pattern?: string): string | undefined { +export const getPattern = (pattern?: string): string | undefined => { return pattern?.replace(/\\/g, '\\\\'); -} +}; diff --git a/src/utils/getServiceNames.ts b/src/utils/getServiceNames.ts index 78f18469f..913b746cf 100644 --- a/src/utils/getServiceNames.ts +++ b/src/utils/getServiceNames.ts @@ -1,6 +1,6 @@ import type { Service } from '../client/interfaces/Service'; import { sort } from './sort'; -export function getServiceNames(services: Service[]): string[] { +export const getServiceNames = (services: Service[]): string[] => { return services.map(service => service.name).sort(sort); -} +}; diff --git a/src/utils/isDefined.ts b/src/utils/isDefined.ts index 7a9615b61..470b6f9d4 100644 --- a/src/utils/isDefined.ts +++ b/src/utils/isDefined.ts @@ -2,6 +2,6 @@ * Check if a value is defined * @param value */ -export function isDefined(value: T | undefined | null | ''): value is Exclude { +export const isDefined = (value: T | undefined | null | ''): value is Exclude => { return value !== undefined && value !== null && value !== ''; -} +}; diff --git a/src/utils/isEqual.ts b/src/utils/isEqual.ts index d3fe474e6..f0d1c2a39 100644 --- a/src/utils/isEqual.ts +++ b/src/utils/isEqual.ts @@ -1,4 +1,4 @@ -export function isEqual(a: any, b: any): boolean { +export const isEqual = (a: any, b: any): boolean => { if (a === b) { return true; } @@ -35,4 +35,4 @@ export function isEqual(a: any, b: any): boolean { } return a !== a && b !== b; -} +}; diff --git a/src/utils/isString.ts b/src/utils/isString.ts index 4f5dd3746..581c602c9 100644 --- a/src/utils/isString.ts +++ b/src/utils/isString.ts @@ -1,3 +1,3 @@ -export function isString(val: any): val is string { +export const isString = (val: any): val is string => { return typeof val === 'string'; -} +}; diff --git a/src/utils/isSubdirectory.ts b/src/utils/isSubdirectory.ts index 5a403fe8e..761eb89d0 100644 --- a/src/utils/isSubdirectory.ts +++ b/src/utils/isSubdirectory.ts @@ -1,5 +1,5 @@ import { relative } from 'path'; -export function isSubDirectory(parent: string, child: string) { +export const isSubDirectory = (parent: string, child: string) => { return relative(child, parent).startsWith('..'); -} +}; diff --git a/src/utils/postProcessClient.ts b/src/utils/postProcessClient.ts index 9e0e00dd7..0033923d4 100644 --- a/src/utils/postProcessClient.ts +++ b/src/utils/postProcessClient.ts @@ -6,10 +6,10 @@ import { postProcessService } from './postProcessService'; * Post process client * @param client Client object with all the models, services, etc. */ -export function postProcessClient(client: Client): Client { +export const postProcessClient = (client: Client): Client => { return { ...client, models: client.models.map(model => postProcessModel(model)), services: client.services.map(service => postProcessService(service)), }; -} +}; diff --git a/src/utils/postProcessModel.ts b/src/utils/postProcessModel.ts index c2e066223..3c8e3f01e 100644 --- a/src/utils/postProcessModel.ts +++ b/src/utils/postProcessModel.ts @@ -8,11 +8,11 @@ import { postProcessModelImports } from './postProcessModelImports'; * This will cleanup any double imports or enum values. * @param model */ -export function postProcessModel(model: Model): Model { +export const postProcessModel = (model: Model): Model => { return { ...model, imports: postProcessModelImports(model), enums: postProcessModelEnums(model), enum: postProcessModelEnum(model), }; -} +}; diff --git a/src/utils/postProcessModelEnum.ts b/src/utils/postProcessModelEnum.ts index c2655726f..f442b369a 100644 --- a/src/utils/postProcessModelEnum.ts +++ b/src/utils/postProcessModelEnum.ts @@ -5,8 +5,8 @@ import type { Model } from '../client/interfaces/Model'; * Set unique enum values for the model * @param model */ -export function postProcessModelEnum(model: Model): Enum[] { +export const postProcessModelEnum = (model: Model): Enum[] => { return model.enum.filter((property, index, arr) => { return arr.findIndex(item => item.name === property.name) === index; }); -} +}; diff --git a/src/utils/postProcessModelEnums.ts b/src/utils/postProcessModelEnums.ts index e220e2133..2f06127aa 100644 --- a/src/utils/postProcessModelEnums.ts +++ b/src/utils/postProcessModelEnums.ts @@ -4,8 +4,8 @@ import type { Model } from '../client/interfaces/Model'; * Set unique enum values for the model * @param model The model that is post-processed */ -export function postProcessModelEnums(model: Model): Model[] { +export const postProcessModelEnums = (model: Model): Model[] => { return model.enums.filter((property, index, arr) => { return arr.findIndex(item => item.name === property.name) === index; }); -} +}; diff --git a/src/utils/postProcessModelImports.ts b/src/utils/postProcessModelImports.ts index 949dd7b3c..00c0d4539 100644 --- a/src/utils/postProcessModelImports.ts +++ b/src/utils/postProcessModelImports.ts @@ -6,9 +6,9 @@ import { unique } from './unique'; * Set unique imports, sorted by name * @param model The model that is post-processed */ -export function postProcessModelImports(model: Model): string[] { +export const postProcessModelImports = (model: Model): string[] => { return model.imports .filter(unique) .sort(sort) .filter(name => model.name !== name); -} +}; diff --git a/src/utils/postProcessService.ts b/src/utils/postProcessService.ts index d604dcdd7..2851f4a6a 100644 --- a/src/utils/postProcessService.ts +++ b/src/utils/postProcessService.ts @@ -2,7 +2,7 @@ import type { Service } from '../client/interfaces/Service'; import { postProcessServiceImports } from './postProcessServiceImports'; import { postProcessServiceOperations } from './postProcessServiceOperations'; -export function postProcessService(service: Service): Service { +export const postProcessService = (service: Service): Service => { const clone = { ...service }; clone.operations = postProcessServiceOperations(clone); clone.operations.forEach(operation => { @@ -10,4 +10,4 @@ export function postProcessService(service: Service): Service { }); clone.imports = postProcessServiceImports(clone); return clone; -} +}; diff --git a/src/utils/postProcessServiceImports.ts b/src/utils/postProcessServiceImports.ts index 50598a413..b47cfe793 100644 --- a/src/utils/postProcessServiceImports.ts +++ b/src/utils/postProcessServiceImports.ts @@ -6,6 +6,6 @@ import { unique } from './unique'; * Set unique imports, sorted by name * @param service */ -export function postProcessServiceImports(service: Service): string[] { +export const postProcessServiceImports = (service: Service): string[] => { return service.imports.filter(unique).sort(sort); -} +}; diff --git a/src/utils/postProcessServiceOperations.ts b/src/utils/postProcessServiceOperations.ts index 62faad724..5b88a5b21 100644 --- a/src/utils/postProcessServiceOperations.ts +++ b/src/utils/postProcessServiceOperations.ts @@ -2,7 +2,7 @@ import type { Operation } from '../client/interfaces/Operation'; import type { Service } from '../client/interfaces/Service'; import { flatMap } from './flatMap'; -export function postProcessServiceOperations(service: Service): Operation[] { +export const postProcessServiceOperations = (service: Service): Operation[] => { const names = new Map(); return service.operations.map(operation => { @@ -23,4 +23,4 @@ export function postProcessServiceOperations(service: Service): Operation[] { return clone; }); -} +}; diff --git a/src/utils/readSpec.ts b/src/utils/readSpec.ts index ade195253..016bf929e 100644 --- a/src/utils/readSpec.ts +++ b/src/utils/readSpec.ts @@ -2,7 +2,7 @@ import { readSpecFromDisk } from './readSpecFromDisk'; import { readSpecFromHttp } from './readSpecFromHttp'; import { readSpecFromHttps } from './readSpecFromHttps'; -export async function readSpec(input: string): Promise { +export const readSpec = async (input: string): Promise => { if (input.startsWith('https://')) { return await readSpecFromHttps(input); } @@ -10,4 +10,4 @@ export async function readSpec(input: string): Promise { return await readSpecFromHttp(input); } return await readSpecFromDisk(input); -} +}; diff --git a/src/utils/readSpecFromDisk.ts b/src/utils/readSpecFromDisk.ts index 0c37c37e6..7a5b76671 100644 --- a/src/utils/readSpecFromDisk.ts +++ b/src/utils/readSpecFromDisk.ts @@ -6,7 +6,7 @@ import { exists, readFile } from './fileSystem'; * Check if given file exists and try to read the content as string. * @param input */ -export async function readSpecFromDisk(input: string): Promise { +export const readSpecFromDisk = async (input: string): Promise => { const filePath = resolve(process.cwd(), input); const fileExists = await exists(filePath); if (fileExists) { @@ -18,4 +18,4 @@ export async function readSpecFromDisk(input: string): Promise { } } throw new Error(`Could not find OpenApi spec: "${filePath}"`); -} +}; diff --git a/src/utils/readSpecFromHttp.ts b/src/utils/readSpecFromHttp.ts index 3131dd9c7..e4cc828a5 100644 --- a/src/utils/readSpecFromHttp.ts +++ b/src/utils/readSpecFromHttp.ts @@ -4,7 +4,7 @@ import { get } from 'http'; * Download the spec file from a HTTP resource * @param url */ -export async function readSpecFromHttp(url: string): Promise { +export const readSpecFromHttp = async (url: string): Promise => { return new Promise((resolve, reject) => { get(url, response => { let body = ''; @@ -19,4 +19,4 @@ export async function readSpecFromHttp(url: string): Promise { }); }); }); -} +}; diff --git a/src/utils/readSpecFromHttps.ts b/src/utils/readSpecFromHttps.ts index 5d9f12bda..52cdc57db 100644 --- a/src/utils/readSpecFromHttps.ts +++ b/src/utils/readSpecFromHttps.ts @@ -4,7 +4,7 @@ import { get } from 'https'; * Download the spec file from a HTTPS resource * @param url */ -export async function readSpecFromHttps(url: string): Promise { +export const readSpecFromHttps = async (url: string): Promise => { return new Promise((resolve, reject) => { get(url, response => { let body = ''; @@ -19,4 +19,4 @@ export async function readSpecFromHttps(url: string): Promise { }); }); }); -} +}; diff --git a/src/utils/registerHandlebarHelpers.spec.ts b/src/utils/registerHandlebarHelpers.spec.ts index adc4200b2..f71ca0008 100644 --- a/src/utils/registerHandlebarHelpers.spec.ts +++ b/src/utils/registerHandlebarHelpers.spec.ts @@ -19,5 +19,6 @@ describe('registerHandlebarHelpers', () => { expect(helpers).toContain('enumerator'); expect(helpers).toContain('escapeComment'); expect(helpers).toContain('escapeDescription'); + expect(helpers).toContain('camelCase'); }); }); diff --git a/src/utils/registerHandlebarHelpers.ts b/src/utils/registerHandlebarHelpers.ts index a1ac44546..f03ab5f00 100644 --- a/src/utils/registerHandlebarHelpers.ts +++ b/src/utils/registerHandlebarHelpers.ts @@ -1,3 +1,4 @@ +import camelCase from 'camelcase'; import Handlebars from 'handlebars/runtime'; import { EOL } from 'os'; @@ -6,11 +7,11 @@ import { Model } from '../client/interfaces/Model'; import { HttpClient } from '../HttpClient'; import { unique } from './unique'; -export function registerHandlebarHelpers(root: { +export const registerHandlebarHelpers = (root: { httpClient: HttpClient; useOptions: boolean; useUnionTypes: boolean; -}): void { +}): void => { Handlebars.registerHelper( 'equals', function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string { @@ -91,4 +92,8 @@ export function registerHandlebarHelpers(root: { Handlebars.registerHelper('escapeDescription', function (value: string): string { return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${'); }); -} + + Handlebars.registerHelper('camelCase', function (value: string): string { + return camelCase(value); + }); +}; diff --git a/src/utils/registerHandlebarTemplates.ts b/src/utils/registerHandlebarTemplates.ts index a64a6f61c..ee2a30869 100644 --- a/src/utils/registerHandlebarTemplates.ts +++ b/src/utils/registerHandlebarTemplates.ts @@ -103,11 +103,11 @@ export interface Templates { * Read all the Handlebar templates that we need and return on wrapper object * so we can easily access the templates in out generator / write functions. */ -export function registerHandlebarTemplates(root: { +export const registerHandlebarTemplates = (root: { httpClient: HttpClient; useOptions: boolean; useUnionTypes: boolean; -}): Templates { +}): Templates => { registerHandlebarHelpers(root); // Main templates (entry points for the files we write to disk) @@ -207,4 +207,4 @@ export function registerHandlebarTemplates(root: { Handlebars.registerPartial('axios/request', Handlebars.template(axiosRequest)); return templates; -} +}; diff --git a/src/utils/sort.ts b/src/utils/sort.ts index 9d41d8405..1d76998f4 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -1,5 +1,5 @@ -export function sort(a: string, b: string): number { +export const sort = (a: string, b: string): number => { const nameA = a.toLowerCase(); const nameB = b.toLowerCase(); return nameA.localeCompare(nameB, 'en'); -} +}; diff --git a/src/utils/sortModelsByName.ts b/src/utils/sortModelsByName.ts index c4d272e12..33fae3990 100644 --- a/src/utils/sortModelsByName.ts +++ b/src/utils/sortModelsByName.ts @@ -1,9 +1,9 @@ import type { Model } from '../client/interfaces/Model'; -export function sortModelsByName(models: Model[]): Model[] { +export const sortModelsByName = (models: Model[]): Model[] => { return models.sort((a, b) => { const nameA = a.name.toLowerCase(); const nameB = b.name.toLowerCase(); return nameA.localeCompare(nameB, 'en'); }); -} +}; diff --git a/src/utils/sortServicesByName.ts b/src/utils/sortServicesByName.ts index 9ff4d8717..4bbef1269 100644 --- a/src/utils/sortServicesByName.ts +++ b/src/utils/sortServicesByName.ts @@ -1,9 +1,9 @@ import type { Service } from '../client/interfaces/Service'; -export function sortServicesByName(services: Service[]): Service[] { +export const sortServicesByName = (services: Service[]): Service[] => { return services.sort((a, b) => { const nameA = a.name.toLowerCase(); const nameB = b.name.toLowerCase(); return nameA.localeCompare(nameB, 'en'); }); -} +}; diff --git a/src/utils/unique.ts b/src/utils/unique.ts index 02ca1378e..65c0f7c7a 100644 --- a/src/utils/unique.ts +++ b/src/utils/unique.ts @@ -1,3 +1,3 @@ -export function unique(val: T, index: number, arr: T[]): boolean { +export const unique = (val: T, index: number, arr: T[]): boolean => { return arr.indexOf(val) === index; -} +}; diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index a00d00487..4a1feaab5 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -32,7 +32,7 @@ import { writeClientServices } from './writeClientServices'; * @param clientName: Custom client class name * @param request: Path to custom request file */ -export async function writeClient( +export const writeClient = async ( client: Client, templates: Templates, output: string, @@ -47,7 +47,7 @@ export async function writeClient( postfix: string, clientName?: string, request?: string -): Promise { +): Promise => { const outputPath = resolve(process.cwd(), output); const outputPathCore = resolve(outputPath, 'core'); const outputPathModels = resolve(outputPath, 'models'); @@ -110,4 +110,4 @@ export async function writeClient( postfix ); } -} +}; diff --git a/src/utils/writeClientClass.ts b/src/utils/writeClientClass.ts index e0a7cb49d..2b06d7d6b 100644 --- a/src/utils/writeClientClass.ts +++ b/src/utils/writeClientClass.ts @@ -21,7 +21,7 @@ import { sortServicesByName } from './sortServicesByName'; * @param indent: Indentation options (4, 2 or tab) * @param postfix: Service name postfix */ -export async function writeClientClass( +export const writeClientClass = async ( client: Client, templates: Templates, outputPath: string, @@ -29,7 +29,7 @@ export async function writeClientClass( clientName: string, indent: Indent, postfix: string -): Promise { +): Promise => { const templateResult = templates.client({ clientName, postfix, @@ -41,4 +41,4 @@ export async function writeClientClass( }); await writeFile(resolve(outputPath, 'client.ts'), i(f(templateResult), indent)); -} +}; diff --git a/src/utils/writeClientCore.ts b/src/utils/writeClientCore.ts index 87d9846ba..2e641e1ae 100644 --- a/src/utils/writeClientCore.ts +++ b/src/utils/writeClientCore.ts @@ -3,11 +3,10 @@ import { resolve } from 'path'; import type { Client } from '../client/interfaces/Client'; import { HttpClient } from '../HttpClient'; import { Indent } from '../Indent'; -import { copyFile, exists, mkdir, writeFile } from './fileSystem'; +import { copyFile, exists, writeFile } from './fileSystem'; import { formatIndentation as i } from './formatIndentation'; import { isDefined } from './isDefined'; import { Templates } from './registerHandlebarTemplates'; -import { writeClientClass } from './writeClientClass'; /** * Generate OpenAPI core files, this includes the basic boilerplate code to handle requests. @@ -18,7 +17,7 @@ import { writeClientClass } from './writeClientClass'; * @param indent: Indentation options (4, 2 or tab) * @param request: Path to custom request file */ -export async function writeClientCore( +export const writeClientCore = async ( client: Client, templates: Templates, outputPath: string, @@ -26,7 +25,7 @@ export async function writeClientCore( indent: Indent, clientName?: string, request?: string -): Promise { +): Promise => { const context = { httpClient, clientName, @@ -55,4 +54,4 @@ export async function writeClientCore( } await copyFile(requestFile, resolve(outputPath, 'request.ts')); } -} +}; diff --git a/src/utils/writeClientIndex.ts b/src/utils/writeClientIndex.ts index 6fe74fa20..eb13cbb13 100644 --- a/src/utils/writeClientIndex.ts +++ b/src/utils/writeClientIndex.ts @@ -20,7 +20,7 @@ import { sortServicesByName } from './sortServicesByName'; * @param exportSchemas: Generate schemas * @param postfix: Service name postfix */ -export async function writeClientIndex( +export const writeClientIndex = async ( client: Client, templates: Templates, outputPath: string, @@ -30,7 +30,7 @@ export async function writeClientIndex( exportModels: boolean, exportSchemas: boolean, postfix: string -): Promise { +): Promise => { const templateResult = templates.index({ exportCore, exportServices, @@ -45,4 +45,4 @@ export async function writeClientIndex( }); await writeFile(resolve(outputPath, 'index.ts'), templateResult); -} +}; diff --git a/src/utils/writeClientModels.ts b/src/utils/writeClientModels.ts index 7c58bd853..8ea5fc224 100644 --- a/src/utils/writeClientModels.ts +++ b/src/utils/writeClientModels.ts @@ -17,14 +17,14 @@ import { Templates } from './registerHandlebarTemplates'; * @param useUnionTypes Use union types instead of enums * @param indent: Indentation options (4, 2 or tab) */ -export async function writeClientModels( +export const writeClientModels = async ( models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, indent: Indent -): Promise { +): Promise => { for (const model of models) { const file = resolve(outputPath, `${model.name}.ts`); const templateResult = templates.exports.model({ @@ -34,4 +34,4 @@ export async function writeClientModels( }); await writeFile(file, i(f(templateResult), indent)); } -} +}; diff --git a/src/utils/writeClientSchemas.ts b/src/utils/writeClientSchemas.ts index 74fdc0c9f..187271982 100644 --- a/src/utils/writeClientSchemas.ts +++ b/src/utils/writeClientSchemas.ts @@ -17,14 +17,14 @@ import { Templates } from './registerHandlebarTemplates'; * @param useUnionTypes Use union types instead of enums * @param indent: Indentation options (4, 2 or tab) */ -export async function writeClientSchemas( +export const writeClientSchemas = async ( models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, indent: Indent -): Promise { +): Promise => { for (const model of models) { const file = resolve(outputPath, `$${model.name}.ts`); const templateResult = templates.exports.schema({ @@ -34,4 +34,4 @@ export async function writeClientSchemas( }); await writeFile(file, i(f(templateResult), indent)); } -} +}; diff --git a/src/utils/writeClientServices.ts b/src/utils/writeClientServices.ts index f094f69b5..7bf476529 100644 --- a/src/utils/writeClientServices.ts +++ b/src/utils/writeClientServices.ts @@ -21,7 +21,7 @@ const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION'; * @param indent: Indentation options (4, 2 or tab) * @param postfix: Service name postfix */ -export async function writeClientServices( +export const writeClientServices = async ( services: Service[], templates: Templates, outputPath: string, @@ -30,7 +30,7 @@ export async function writeClientServices( useOptions: boolean, indent: Indent, postfix: string -): Promise { +): Promise => { for (const service of services) { const file = resolve(outputPath, `${service.name}${postfix}.ts`); const useVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING)); @@ -44,4 +44,4 @@ export async function writeClientServices( }); await writeFile(file, i(f(templateResult), indent)); } -} +}; diff --git a/test/custom/request.ts b/test/custom/request.ts index 277a8fa40..522f85cfe 100644 --- a/test/custom/request.ts +++ b/test/custom/request.ts @@ -1,10 +1,10 @@ import type { ApiRequestOptions } from './ApiRequestOptions'; import { CancelablePromise } from './CancelablePromise'; -import { OpenAPI } from './OpenAPI'; +import type { OpenAPIConfig } from './OpenAPI'; -export function request(options: ApiRequestOptions): CancelablePromise { +export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise => { return new CancelablePromise((resolve, reject, onCancel) => { - const url = `${OpenAPI.BASE}${options.path}`; + const url = `${config.BASE}${options.path}`; try { // Do your request... @@ -28,4 +28,4 @@ export function request(options: ApiRequestOptions): CancelablePromise { reject(e); } }); -} +}; diff --git a/test/spec/v2.json b/test/spec/v2.json index bf13a84e3..8171551fd 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -10,6 +10,12 @@ "http" ], "paths": { + "/api/v{api-version}/no-tag": { + "tags": [], + "get": { + "operationId": "ServiceWithEmptyTag" + } + }, "/api/v{api-version}/simple": { "get": { "tags": [ diff --git a/test/spec/v3.json b/test/spec/v3.json index 2d816d701..dcfd011ea 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -10,6 +10,12 @@ } ], "paths": { + "/api/v{api-version}/no-tag": { + "tags": [], + "get": { + "operationId": "ServiceWithEmptyTag" + } + }, "/api/v{api-version}/simple": { "get": { "tags": [ @@ -1886,7 +1892,9 @@ "ModelCircle": { "description": "Circle", "type": "object", - "required": ["kind"], + "required": [ + "kind" + ], "properties": { "kind": { "type": "string" @@ -1899,7 +1907,9 @@ "ModelSquare": { "description": "Square", "type": "object", - "required": ["kind"], + "required": [ + "kind" + ], "properties": { "kind": { "type": "string"