Skip to content

Commit

Permalink
- Moved to const definitions for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdikoomen committed Jan 25, 2022
1 parent c6c9d2b commit 280d196
Show file tree
Hide file tree
Showing 114 changed files with 286 additions and 266 deletions.
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const handlebarsPlugin = () => ({
enumerator: true,
escapeComment: true,
escapeDescription: true,
camelCase: true,
},
});
return `export default ${templateSpec};`;
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -63,7 +63,7 @@ export async function generate({
postfix = 'Service',
request,
write = true,
}: Options): Promise<void> {
}: Options): Promise<void> => {
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
const openApiVersion = getOpenApiVersion(openApi);
const templates = registerHandlebarTemplates({
Expand Down Expand Up @@ -119,7 +119,7 @@ export async function generate({
break;
}
}
}
};

export default {
HttpClient,
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/escapeName.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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) {
return `'${value}'`;
}
}
return value;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/extendEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -17,4 +17,4 @@ export function extendEnum(enumerators: Enum[], definition: WithEnumExtension):
value: enumerator.value,
type: enumerator.type,
}));
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getEnum.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -30,4 +30,4 @@ export function getEnum(values?: (string | number)[]): Enum[] {
});
}
return [];
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getEnumFromDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -36,4 +36,4 @@ export function getEnumFromDescription(description: string): Enum[] {
}

return [];
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getMappedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ const TYPE_MAPPINGS = new Map<string, string>([
/**
* 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);
}
};
6 changes: 3 additions & 3 deletions src/openApi/v2/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -162,4 +162,4 @@ export function getModel(
}

return model;
}
};
6 changes: 3 additions & 3 deletions src/openApi/v2/parser/getModelComposition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -87,4 +87,4 @@ export function getModelComposition(
}

return composition;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -85,4 +85,4 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
}
}
return models;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getModelTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import type { Type } from '../../../client/interfaces/Type';
* @param modelClass The parsed model class type.
* @returns The model template type (<T> or empty).
*/
export function getModelTemplate(modelClass: Type): string {
export const getModelTemplate = (modelClass: Type): string => {
return modelClass.template ? '<T>' : '';
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -14,4 +14,4 @@ export function getModels(openApi: OpenApi): Model[] {
}
}
return models;
}
};
9 changes: 4 additions & 5 deletions src/openApi/v2/parser/getOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -76,4 +75,4 @@ export function getOperation(
operation.parameters = operation.parameters.sort(sortByRequired);

return operation;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,4 +14,4 @@ export function getOperationErrors(operationResponses: OperationResponse[]): Ope
code: response.code,
description: response.description!,
}));
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -147,4 +147,4 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame
}

return operationParameter;
}
};
6 changes: 3 additions & 3 deletions src/openApi/v2/parser/getOperationParameterDefault.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -39,4 +39,4 @@ export function getOperationParameterDefault(
}

return;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationParameterName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -58,4 +58,4 @@ export function getOperationParameters(openApi: OpenApi, parameters: OpenApiPara
}
});
return operationParameters;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
}
};
6 changes: 3 additions & 3 deletions src/openApi/v2/parser/getOperationResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down Expand Up @@ -96,4 +96,4 @@ export function getOperationResponse(
}

return operationResponse;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationResponseCode.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,4 +13,4 @@ export function getOperationResponseCode(value: string | 'default'): number | nu
}

return null;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationResponseHeader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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';
});
if (header) {
return header.name;
}
return null;
}
};
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getOperationResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
});
}
};
Loading

0 comments on commit 280d196

Please sign in to comment.