···11import camelCase from 'camelcase';
2233import { reservedWords } from '../../../utils/reservedWords';
44-import sanitizeOperationParameterName from '../../../utils/sanitizeOperationParameterName';
44+import { sanitizeOperationParameterName } from '../../../utils/sanitize';
5566/**
77 * Replaces any invalid characters from a parameter name.
+1-1
src/openApi/v2/parser/getServiceName.ts
···11import camelCase from 'camelcase';
2233-import sanitizeServiceName from '../../../utils/sanitizeServiceName';
33+import { sanitizeServiceName } from '../../../utils/sanitize';
4455/**
66 * Convert the input value to a correct service name. This converts
···11import camelCase from 'camelcase';
2233import { reservedWords } from '../../../utils/reservedWords';
44-import sanitizeOperationParameterName from '../../../utils/sanitizeOperationParameterName';
44+import { sanitizeOperationParameterName } from '../../../utils/sanitize';
5566/**
77 * Replaces any invalid characters from a parameter name.
+1-1
src/openApi/v3/parser/service.ts
···11import camelCase from 'camelcase';
2233-import sanitizeServiceName from '../../../utils/sanitizeServiceName';
33+import { sanitizeServiceName } from '../../../utils/sanitize';
4455export const allowedServiceMethods = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'] as const;
66
···11import camelCase from 'camelcase';
2233import type { Config } from '../types/config';
44-import sanitizeOperationName from './sanitizeOperationName';
44+import { sanitizeOperationName } from './sanitize';
5566/**
77 * Convert the input value to a correct operation (method) classname.
-13
src/utils/postProcessClient.ts
···11-import type { Client } from '../types/client';
22-import { postProcessModel } from './postProcessModel';
33-import { postProcessService } from './postProcessService';
44-55-/**
66- * Post process client
77- * @param client Client object with all the models, services, etc.
88- */
99-export const postProcessClient = (client: Client): Client => ({
1010- ...client,
1111- models: client.models.map(model => postProcessModel(model)),
1212- services: client.services.map(service => postProcessService(service)),
1313-});
-16
src/utils/postProcessModel.ts
···11-import type { Model } from '../types/client';
22-import { postProcessModelEnum } from './postProcessModelEnum';
33-import { postProcessModelEnums } from './postProcessModelEnums';
44-import { postProcessModelImports } from './postProcessModelImports';
55-66-/**
77- * Post processes the model.
88- * This will clean up any double imports or enum values.
99- * @param model
1010- */
1111-export const postProcessModel = (model: Model): Model => ({
1212- ...model,
1313- imports: postProcessModelImports(model),
1414- enums: postProcessModelEnums(model),
1515- enum: postProcessModelEnum(model),
1616-});
-8
src/utils/postProcessModelEnum.ts
···11-import type { Model } from '../types/client';
22-33-/**
44- * Set unique enum values for the model
55- * @param model
66- */
77-export const postProcessModelEnum = (model: Model) =>
88- model.enum.filter((property, index, arr) => arr.findIndex(item => item.value === property.value) === index);
-8
src/utils/postProcessModelEnums.ts
···11-import type { Model } from '../types/client';
22-33-/**
44- * Set unique enum values for the model
55- * @param model The model that is post-processed
66- */
77-export const postProcessModelEnums = (model: Model): Model[] =>
88- model.enums.filter((property, index, arr) => arr.findIndex(item => item.name === property.name) === index);
-13
src/utils/postProcessModelImports.ts
···11-import type { Model } from '../types/client';
22-import { sort } from './sort';
33-import { unique } from './unique';
44-55-/**
66- * Set unique imports, sorted by name
77- * @param model The model that is post-processed
88- */
99-export const postProcessModelImports = (model: Model): string[] =>
1010- model.imports
1111- .filter(unique)
1212- .sort(sort)
1313- .filter(name => model.name !== name);
-13
src/utils/postProcessService.ts
···11-import type { Service } from '../types/client';
22-import { postProcessServiceImports } from './postProcessServiceImports';
33-import { postProcessServiceOperations } from './postProcessServiceOperations';
44-55-export const postProcessService = (service: Service): Service => {
66- const clone = { ...service };
77- clone.operations = postProcessServiceOperations(clone);
88- clone.operations.forEach(operation => {
99- clone.imports.push(...operation.imports);
1010- });
1111- clone.imports = postProcessServiceImports(clone);
1212- return clone;
1313-};
-9
src/utils/postProcessServiceImports.ts
···11-import type { Service } from '../types/client';
22-import { sort } from './sort';
33-import { unique } from './unique';
44-55-/**
66- * Set unique imports, sorted by name
77- * @param service
88- */
99-export const postProcessServiceImports = (service: Service): string[] => service.imports.filter(unique).sort(sort);
-24
src/utils/postProcessServiceOperations.ts
···11-import type { Operation, Service } from '../types/client';
22-33-export const postProcessServiceOperations = (service: Service): Operation[] => {
44- const names = new Map<string, number>();
55-66- return service.operations.map(operation => {
77- const clone = { ...operation };
88-99- // Parse the service parameters and results, very similar to how we parse
1010- // properties of models. These methods will extend the type if needed.
1111- clone.imports.push(...clone.parameters.flatMap(parameter => parameter.imports));
1212- clone.imports.push(...clone.results.flatMap(result => result.imports));
1313-1414- // Check if the operation name is unique, if not then prefix this with a number
1515- const name = clone.name;
1616- const index = names.get(name) || 0;
1717- if (index > 0) {
1818- clone.name = `${name}${index}`;
1919- }
2020- names.set(name, index + 1);
2121-2222- return clone;
2323- });
2424-};
+97
src/utils/postprocess.ts
···11+import type { Client, Enum, Model, Operation, Service } from '../types/client';
22+import { sort } from './sort';
33+import { unique } from './unique';
44+55+/**
66+ * Post process client
77+ * @param client Client object with all the models, services, etc.
88+ */
99+export function postProcessClient(client: Client): Client {
1010+ return {
1111+ ...client,
1212+ models: client.models.map(model => postProcessModel(model)),
1313+ services: client.services.map(service => postProcessService(service)),
1414+ };
1515+}
1616+1717+/**
1818+ * Post processes the model.
1919+ * This will clean up any double imports or enum values.
2020+ * @param model
2121+ */
2222+export function postProcessModel(model: Model): Model {
2323+ return {
2424+ ...model,
2525+ imports: postProcessModelImports(model),
2626+ enums: postProcessModelEnums(model),
2727+ enum: postProcessModelEnum(model),
2828+ };
2929+}
3030+3131+/**
3232+ * Set unique enum values for the model
3333+ * @param model
3434+ */
3535+export function postProcessModelEnum(model: Model): Enum[] {
3636+ return model.enum.filter((property, index, arr) => arr.findIndex(item => item.value === property.value) === index);
3737+}
3838+3939+/**
4040+ * Set unique enum values for the model
4141+ * @param model The model that is post-processed
4242+ */
4343+export function postProcessModelEnums(model: Model): Model[] {
4444+ return model.enums.filter((property, index, arr) => arr.findIndex(item => item.name === property.name) === index);
4545+}
4646+4747+/**
4848+ * Set unique imports, sorted by name
4949+ * @param model The model that is post-processed
5050+ */
5151+export function postProcessModelImports(model: Model): string[] {
5252+ return model.imports
5353+ .filter(unique)
5454+ .sort(sort)
5555+ .filter(name => model.name !== name);
5656+}
5757+5858+export function postProcessService(service: Service): Service {
5959+ const clone = { ...service };
6060+ clone.operations = postProcessServiceOperations(clone);
6161+ clone.operations.forEach(operation => {
6262+ clone.imports.push(...operation.imports);
6363+ });
6464+ clone.imports = postProcessServiceImports(clone);
6565+ return clone;
6666+}
6767+6868+/**
6969+ * Set unique imports, sorted by name
7070+ * @param service
7171+ */
7272+export function postProcessServiceImports(service: Service): string[] {
7373+ return service.imports.filter(unique).sort(sort);
7474+}
7575+7676+export function postProcessServiceOperations(service: Service): Operation[] {
7777+ const names = new Map<string, number>();
7878+7979+ return service.operations.map(operation => {
8080+ const clone = { ...operation };
8181+8282+ // Parse the service parameters and results, very similar to how we parse
8383+ // properties of models. These methods will extend the type if needed.
8484+ clone.imports.push(...clone.parameters.flatMap(parameter => parameter.imports));
8585+ clone.imports.push(...clone.results.flatMap(result => result.imports));
8686+8787+ // Check if the operation name is unique, if not then prefix this with a number
8888+ const name = clone.name;
8989+ const index = names.get(name) || 0;
9090+ if (index > 0) {
9191+ clone.name = `${name}${index}`;
9292+ }
9393+ names.set(name, index + 1);
9494+9595+ return clone;
9696+ });
9797+}
+45
src/utils/sanitize.ts
···11+/**
22+ * Sanitizes names of types, so they are valid typescript identifiers of a certain form.
33+ *
44+ * 1: Remove any leading characters that are illegal as starting character of a typescript identifier.
55+ * 2: Replace illegal characters in remaining part of type name with underscore (_).
66+ *
77+ * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName
88+ * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It
99+ * would be sort of a breaking change to do so, though, previously generated code might change then.
1010+ *
1111+ * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers
1212+ */
1313+export function sanitizeTypeName(name: string): string {
1414+ return name.replace(/^[^$_\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '_');
1515+}
1616+1717+/**
1818+ * Sanitizes service names, so they are valid typescript identifiers of a certain form.
1919+ *
2020+ * 1: Remove any leading characters that are illegal as starting character of a typescript identifier.
2121+ * 2: Replace illegal characters in remaining part of type name with hyphen (-).
2222+ *
2323+ * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName
2424+ * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It
2525+ * would be sort of a breaking change to do so, though, previously generated code might change then.
2626+ *
2727+ * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers
2828+ *
2929+ * The output of this is expected to be converted to PascalCase
3030+ */
3131+export function sanitizeServiceName(name: string): string {
3232+ return name.replace(/^[^\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '-');
3333+}
3434+3535+/**
3636+ * sanitizeOperationName does the same as sanitizeServiceName.
3737+ */
3838+export function sanitizeOperationName(name: string): string {
3939+ return sanitizeServiceName(name);
4040+}
4141+4242+export function sanitizeOperationParameterName(name: string): string {
4343+ const withoutBrackets = name.replace('[]', 'Array');
4444+ return sanitizeOperationName(withoutBrackets);
4545+}
-7
src/utils/sanitizeOperationName.ts
···11-import sanitizeServiceName from './sanitizeServiceName';
22-33-/**
44- * sanitizeOperationName does the same as sanitizeServiceName.
55- */
66-const sanitizeOperationName = sanitizeServiceName;
77-export default sanitizeOperationName;
···11-/**
22- * Sanitizes service names, so they are valid typescript identifiers of a certain form.
33- *
44- * 1: Remove any leading characters that are illegal as starting character of a typescript identifier.
55- * 2: Replace illegal characters in remaining part of type name with underscore (-).
66- *
77- * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName
88- * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It
99- * would be sort of a breaking change to do so, though, previously generated code might change then.
1010- *
1111- * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers
1212- *
1313- * The output of this is expected to be converted to PascalCase
1414- */
1515-const sanitizeServiceName = (name: string) =>
1616- name.replace(/^[^\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '-');
1717-1818-export default sanitizeServiceName;
-16
src/utils/sanitizeTypeName.ts
···11-/**
22- * Sanitizes names of types, so they are valid typescript identifiers of a certain form.
33- *
44- * 1: Remove any leading characters that are illegal as starting character of a typescript identifier.
55- * 2: Replace illegal characters in remaining part of type name with underscore (_).
66- *
77- * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName
88- * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It
99- * would be sort of a breaking change to do so, though, previously generated code might change then.
1010- *
1111- * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers
1212- */
1313-const sanitizeTypeName = (name: string) =>
1414- name.replace(/^[^$_\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '_');
1515-1616-export default sanitizeTypeName;
···11import type { Type } from '../types/client';
22-import sanitizeTypeName from './sanitizeTypeName';
22+import { sanitizeTypeName } from './sanitize';
33import { stripNamespace } from './stripNamespace';
4455const TYPE_MAPPINGS = new Map<string, string>([