fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #106 from jordanshatford/chore/various-small-changes

chore: various cleanup changes and test changes

authored by

Lubos and committed by
GitHub
d3cb1c80 a848dba7

+282 -244
+1 -1
src/index.ts
··· 10 10 import { getOpenApiSpec } from './utils/getOpenApiSpec'; 11 11 import { registerHandlebarTemplates } from './utils/handlebars'; 12 12 import { isSubDirectory } from './utils/isSubdirectory'; 13 - import { postProcessClient } from './utils/postProcessClient'; 13 + import { postProcessClient } from './utils/postprocess'; 14 14 import { writeClient } from './utils/write/client'; 15 15 16 16 type Dependencies = Record<string, unknown>;
+1 -1
src/openApi/v2/parser/getOperationParameterName.ts
··· 1 1 import camelCase from 'camelcase'; 2 2 3 3 import { reservedWords } from '../../../utils/reservedWords'; 4 - import sanitizeOperationParameterName from '../../../utils/sanitizeOperationParameterName'; 4 + import { sanitizeOperationParameterName } from '../../../utils/sanitize'; 5 5 6 6 /** 7 7 * Replaces any invalid characters from a parameter name.
+1 -1
src/openApi/v2/parser/getServiceName.ts
··· 1 1 import camelCase from 'camelcase'; 2 2 3 - import sanitizeServiceName from '../../../utils/sanitizeServiceName'; 3 + import { sanitizeServiceName } from '../../../utils/sanitize'; 4 4 5 5 /** 6 6 * Convert the input value to a correct service name. This converts
+1 -1
src/openApi/v3/parser/getOperationParameterName.ts
··· 1 1 import camelCase from 'camelcase'; 2 2 3 3 import { reservedWords } from '../../../utils/reservedWords'; 4 - import sanitizeOperationParameterName from '../../../utils/sanitizeOperationParameterName'; 4 + import { sanitizeOperationParameterName } from '../../../utils/sanitize'; 5 5 6 6 /** 7 7 * Replaces any invalid characters from a parameter name.
+1 -1
src/openApi/v3/parser/service.ts
··· 1 1 import camelCase from 'camelcase'; 2 2 3 - import sanitizeServiceName from '../../../utils/sanitizeServiceName'; 3 + import { sanitizeServiceName } from '../../../utils/sanitize'; 4 4 5 5 export const allowedServiceMethods = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'] as const; 6 6
+20 -24
src/utils/__tests__/escapeName.spec.ts
··· 2 2 3 3 import { escapeName, unescapeName } from '../escapeName'; 4 4 5 - const data = [ 6 - ['', "''"], 7 - ['fooBar', 'fooBar'], 8 - ['Foo Bar', `'Foo Bar'`], 9 - ['foo bar', `'foo bar'`], 10 - ['foo-bar', `'foo-bar'`], 11 - ['foo.bar', `'foo.bar'`], 12 - ['foo_bar', 'foo_bar'], 13 - ['123foo.bar', `'123foo.bar'`], 14 - ['@foo.bar', `'@foo.bar'`], 15 - ['$foo.bar', `'$foo.bar'`], 16 - ['_foo.bar', `'_foo.bar'`], 17 - ['123foobar', `'123foobar'`], 18 - ['@foobar', `'@foobar'`], 19 - ['$foobar', '$foobar'], 20 - ['_foobar', '_foobar'], 5 + const toCheck: { unescaped: string; escaped: string }[] = [ 6 + { unescaped: '', escaped: "''" }, 7 + { unescaped: 'fooBar', escaped: 'fooBar' }, 8 + { unescaped: 'Foo Bar', escaped: `'Foo Bar'` }, 9 + { unescaped: 'foo bar', escaped: `'foo bar'` }, 10 + { unescaped: 'foo-bar', escaped: `'foo-bar'` }, 11 + { unescaped: 'foo.bar', escaped: `'foo.bar'` }, 12 + { unescaped: 'foo_bar', escaped: 'foo_bar' }, 13 + { unescaped: '123foo.bar', escaped: `'123foo.bar'` }, 14 + { unescaped: '@foo.bar', escaped: `'@foo.bar'` }, 15 + { unescaped: '$foo.bar', escaped: `'$foo.bar'` }, 16 + { unescaped: '_foo.bar', escaped: `'_foo.bar'` }, 17 + { unescaped: '123foobar', escaped: `'123foobar'` }, 18 + { unescaped: '@foobar', escaped: `'@foobar'` }, 19 + { unescaped: '$foobar', escaped: '$foobar' }, 20 + { unescaped: '_foobar', escaped: '_foobar' }, 21 21 ]; 22 22 23 23 describe('escapeName', () => { 24 - it('should escape', () => { 25 - data.forEach(([unescaped, escaped]) => { 26 - expect(escapeName(unescaped)).toBe(escaped); 27 - }); 24 + it.each(toCheck)('should escape $unescaped to $escaped', ({ unescaped, escaped }) => { 25 + expect(escapeName(unescaped)).toBe(escaped); 28 26 }); 29 27 }); 30 28 31 29 describe('unescapeName', () => { 32 - it('should unescape', () => { 33 - data.forEach(([unescaped, escaped]) => { 34 - expect(unescapeName(escaped)).toBe(unescaped); 35 - }); 30 + it.each(toCheck)('should unescape $escaped to $unescaped', ({ unescaped, escaped }) => { 31 + expect(unescapeName(escaped)).toBe(unescaped); 36 32 }); 37 33 });
+12 -10
src/utils/__tests__/getPattern.spec.ts
··· 3 3 import { getPattern } from '../getPattern'; 4 4 5 5 describe('getPattern', () => { 6 - it('should produce correct result', () => { 7 - expect(getPattern()).toEqual(undefined); 8 - expect(getPattern('')).toEqual(''); 9 - expect(getPattern('^[a-zA-Z]')).toEqual('^[a-zA-Z]'); 10 - expect(getPattern('^\\w+$')).toEqual('^\\\\w+$'); 11 - expect(getPattern('^\\d{3}-\\d{2}-\\d{4}$')).toEqual('^\\\\d{3}-\\\\d{2}-\\\\d{4}$'); 12 - expect(getPattern('\\')).toEqual('\\\\'); 13 - expect(getPattern('\\/')).toEqual('\\\\/'); 14 - expect(getPattern('\\/\\/')).toEqual('\\\\/\\\\/'); 15 - expect(getPattern("'")).toEqual("\\'"); 6 + it.each([ 7 + { pattern: undefined, expected: undefined }, 8 + { pattern: '', expected: '' }, 9 + { pattern: '^[a-zA-Z]', expected: '^[a-zA-Z]' }, 10 + { pattern: '^\\w+$', expected: '^\\\\w+$' }, 11 + { pattern: '^\\d{3}-\\d{2}-\\d{4}$', expected: '^\\\\d{3}-\\\\d{2}-\\\\d{4}$' }, 12 + { pattern: '\\', expected: '\\\\' }, 13 + { pattern: '\\/', expected: '\\\\/' }, 14 + { pattern: '\\/\\/', expected: '\\\\/\\\\/' }, 15 + { pattern: "'", expected: "\\'" }, 16 + ])('getPattern($pattern) -> $expected', ({ pattern, expected }) => { 17 + expect(getPattern(pattern)).toEqual(expected); 16 18 }); 17 19 });
+54
src/utils/__tests__/sanitize.spec.ts
··· 1 + import { describe, expect, it } from 'vitest'; 2 + 3 + import { 4 + sanitizeOperationName, 5 + sanitizeOperationParameterName, 6 + sanitizeServiceName, 7 + sanitizeTypeName, 8 + } from '../sanitize'; 9 + 10 + describe('sanitizeOperationName', () => { 11 + it('should remove/replace illegal characters', () => { 12 + expect(sanitizeOperationName('abc')).toEqual('abc'); 13 + expect(sanitizeOperationName('æbc')).toEqual('æbc'); 14 + expect(sanitizeOperationName('æb.c')).toEqual('æb-c'); 15 + expect(sanitizeOperationName('1æb.c')).toEqual('æb-c'); 16 + }); 17 + }); 18 + 19 + describe('sanitizeOperationParameterName', () => { 20 + it('should remove/replace illegal characters', () => { 21 + expect(sanitizeOperationParameterName('abc')).toEqual('abc'); 22 + expect(sanitizeOperationParameterName('æbc')).toEqual('æbc'); 23 + expect(sanitizeOperationParameterName('æb.c')).toEqual('æb-c'); 24 + expect(sanitizeOperationParameterName('1æb.c')).toEqual('æb-c'); 25 + expect(sanitizeOperationParameterName('unknown[]')).toEqual('unknownArray'); 26 + }); 27 + }); 28 + 29 + describe('sanitizeServiceName', () => { 30 + it('should remove/replace illegal characters', () => { 31 + expect(sanitizeServiceName('abc')).toEqual('abc'); 32 + expect(sanitizeServiceName('æbc')).toEqual('æbc'); 33 + expect(sanitizeServiceName('æb.c')).toEqual('æb-c'); 34 + expect(sanitizeServiceName('1æb.c')).toEqual('æb-c'); 35 + }); 36 + }); 37 + 38 + describe('sanitizeTypeName', () => { 39 + it('should remove/replace illegal characters', () => { 40 + expect(sanitizeTypeName('abc')).toEqual('abc'); 41 + expect(sanitizeTypeName('æbc')).toEqual('æbc'); 42 + expect(sanitizeTypeName('æb.c')).toEqual('æb_c'); 43 + expect(sanitizeTypeName('1æb.c')).toEqual('æb_c'); 44 + }); 45 + }); 46 + 47 + describe('sanitizeTypeName', () => { 48 + it('should remove/replace illegal characters', () => { 49 + expect(sanitizeTypeName('abc')).toEqual('abc'); 50 + expect(sanitizeTypeName('æbc')).toEqual('æbc'); 51 + expect(sanitizeTypeName('æb.c')).toEqual('æb_c'); 52 + expect(sanitizeTypeName('1æb.c')).toEqual('æb_c'); 53 + }); 54 + });
-12
src/utils/__tests__/sanitizeTypeName.spec.ts
··· 1 - import { describe, expect, it } from 'vitest'; 2 - 3 - import sanitizeTypeName from '../sanitizeTypeName'; 4 - 5 - describe('sanitizeTypeName', () => { 6 - it('should remove/replace illegal characters', () => { 7 - expect(sanitizeTypeName('abc')).toEqual('abc'); 8 - expect(sanitizeTypeName('æbc')).toEqual('æbc'); 9 - expect(sanitizeTypeName('æb.c')).toEqual('æb_c'); 10 - expect(sanitizeTypeName('1æb.c')).toEqual('æb_c'); 11 - }); 12 - });
+24 -22
src/utils/__tests__/type.spec.ts
··· 3 3 import { getMappedType, getType } from '../type'; 4 4 5 5 describe('getMappedType', () => { 6 - it('should map types to the basics', () => { 7 - expect(getMappedType('')).toEqual(undefined); 8 - expect(getMappedType('any')).toEqual('unknown'); 9 - expect(getMappedType('array')).toEqual('unknown[]'); 10 - expect(getMappedType('boolean')).toEqual('boolean'); 11 - expect(getMappedType('byte')).toEqual('number'); 12 - expect(getMappedType('char')).toEqual('string'); 13 - expect(getMappedType('date-time')).toEqual('string'); 14 - expect(getMappedType('date')).toEqual('string'); 15 - expect(getMappedType('double')).toEqual('number'); 16 - expect(getMappedType('file')).toEqual('binary'); 17 - expect(getMappedType('float')).toEqual('number'); 18 - expect(getMappedType('int')).toEqual('number'); 19 - expect(getMappedType('integer')).toEqual('number'); 20 - expect(getMappedType('long')).toEqual('number'); 21 - expect(getMappedType('null')).toEqual('null'); 22 - expect(getMappedType('number')).toEqual('number'); 23 - expect(getMappedType('object')).toEqual('unknown'); 24 - expect(getMappedType('password')).toEqual('string'); 25 - expect(getMappedType('short')).toEqual('number'); 26 - expect(getMappedType('string')).toEqual('string'); 27 - expect(getMappedType('void')).toEqual('void'); 6 + it.each([ 7 + { type: '', expected: undefined }, 8 + { type: 'any', expected: 'unknown' }, 9 + { type: 'array', expected: 'unknown[]' }, 10 + { type: 'boolean', expected: 'boolean' }, 11 + { type: 'byte', expected: 'number' }, 12 + { type: 'char', expected: 'string' }, 13 + { type: 'date-time', expected: 'string' }, 14 + { type: 'date', expected: 'string' }, 15 + { type: 'double', expected: 'number' }, 16 + { type: 'file', expected: 'binary' }, 17 + { type: 'float', expected: 'number' }, 18 + { type: 'int', expected: 'number' }, 19 + { type: 'integer', expected: 'number' }, 20 + { type: 'long', expected: 'number' }, 21 + { type: 'null', expected: 'null' }, 22 + { type: 'number', expected: 'number' }, 23 + { type: 'object', expected: 'unknown' }, 24 + { type: 'password', expected: 'string' }, 25 + { type: 'short', expected: 'number' }, 26 + { type: 'string', expected: 'string' }, 27 + { type: 'void', expected: 'void' }, 28 + ])('should map type $type to $expected', ({ type, expected }) => { 29 + expect(getMappedType(type)).toEqual(expected); 28 30 }); 29 31 }); 30 32
+15 -7
src/utils/__tests__/unique.spec.ts
··· 3 3 import { unique } from '../unique'; 4 4 5 5 describe('unique', () => { 6 - it('should return correct index', () => { 7 - expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy(); 8 - expect(unique('a', 1, ['a', 'b', 'c'])).toBeFalsy(); 9 - expect(unique('a', 2, ['a', 'b', 'c'])).toBeFalsy(); 10 - expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy(); 11 - expect(unique('a', 1, ['z', 'a', 'b'])).toBeTruthy(); 12 - expect(unique('a', 2, ['y', 'z', 'a'])).toBeTruthy(); 6 + it.each([ 7 + { value: 'a', index: 0, arr: ['a', 'b', 'c'], result: true }, 8 + { value: 'a', index: 1, arr: ['a', 'b', 'c'], result: false }, 9 + { value: 'a', index: 2, arr: ['a', 'b', 'c'], result: false }, 10 + { value: 'a', index: 1, arr: ['z', 'a', 'b'], result: true }, 11 + { value: 'a', index: 2, arr: ['y', 'z', 'a'], result: true }, 12 + ])('unique($value, $index, $arr) -> $result', ({ value, index, arr, result }) => { 13 + expect(unique(value, index, arr)).toEqual(result); 14 + }); 15 + 16 + it.each([ 17 + { input: ['a', 'a', 'b', 'c', 'b', 'b'], expected: ['a', 'b', 'c'] }, 18 + { input: [1, 2, 3, 4, 4, 5, 6, 3], expected: [1, 2, 3, 4, 5, 6] }, 19 + ])('should filter: $input to the unique array: $expected', ({ input, expected }) => { 20 + expect(input.filter(unique)).toEqual(expected); 13 21 }); 14 22 });
+1 -1
src/utils/operation.ts
··· 1 1 import camelCase from 'camelcase'; 2 2 3 3 import type { Config } from '../types/config'; 4 - import sanitizeOperationName from './sanitizeOperationName'; 4 + import { sanitizeOperationName } from './sanitize'; 5 5 6 6 /** 7 7 * Convert the input value to a correct operation (method) classname.
-13
src/utils/postProcessClient.ts
··· 1 - import type { Client } from '../types/client'; 2 - import { postProcessModel } from './postProcessModel'; 3 - import { postProcessService } from './postProcessService'; 4 - 5 - /** 6 - * Post process client 7 - * @param client Client object with all the models, services, etc. 8 - */ 9 - export const postProcessClient = (client: Client): Client => ({ 10 - ...client, 11 - models: client.models.map(model => postProcessModel(model)), 12 - services: client.services.map(service => postProcessService(service)), 13 - });
-16
src/utils/postProcessModel.ts
··· 1 - import type { Model } from '../types/client'; 2 - import { postProcessModelEnum } from './postProcessModelEnum'; 3 - import { postProcessModelEnums } from './postProcessModelEnums'; 4 - import { postProcessModelImports } from './postProcessModelImports'; 5 - 6 - /** 7 - * Post processes the model. 8 - * This will clean up any double imports or enum values. 9 - * @param model 10 - */ 11 - export const postProcessModel = (model: Model): Model => ({ 12 - ...model, 13 - imports: postProcessModelImports(model), 14 - enums: postProcessModelEnums(model), 15 - enum: postProcessModelEnum(model), 16 - });
-8
src/utils/postProcessModelEnum.ts
··· 1 - import type { Model } from '../types/client'; 2 - 3 - /** 4 - * Set unique enum values for the model 5 - * @param model 6 - */ 7 - export const postProcessModelEnum = (model: Model) => 8 - model.enum.filter((property, index, arr) => arr.findIndex(item => item.value === property.value) === index);
-8
src/utils/postProcessModelEnums.ts
··· 1 - import type { Model } from '../types/client'; 2 - 3 - /** 4 - * Set unique enum values for the model 5 - * @param model The model that is post-processed 6 - */ 7 - export const postProcessModelEnums = (model: Model): Model[] => 8 - model.enums.filter((property, index, arr) => arr.findIndex(item => item.name === property.name) === index);
-13
src/utils/postProcessModelImports.ts
··· 1 - import type { Model } from '../types/client'; 2 - import { sort } from './sort'; 3 - import { unique } from './unique'; 4 - 5 - /** 6 - * Set unique imports, sorted by name 7 - * @param model The model that is post-processed 8 - */ 9 - export const postProcessModelImports = (model: Model): string[] => 10 - model.imports 11 - .filter(unique) 12 - .sort(sort) 13 - .filter(name => model.name !== name);
-13
src/utils/postProcessService.ts
··· 1 - import type { Service } from '../types/client'; 2 - import { postProcessServiceImports } from './postProcessServiceImports'; 3 - import { postProcessServiceOperations } from './postProcessServiceOperations'; 4 - 5 - export const postProcessService = (service: Service): Service => { 6 - const clone = { ...service }; 7 - clone.operations = postProcessServiceOperations(clone); 8 - clone.operations.forEach(operation => { 9 - clone.imports.push(...operation.imports); 10 - }); 11 - clone.imports = postProcessServiceImports(clone); 12 - return clone; 13 - };
-9
src/utils/postProcessServiceImports.ts
··· 1 - import type { Service } from '../types/client'; 2 - import { sort } from './sort'; 3 - import { unique } from './unique'; 4 - 5 - /** 6 - * Set unique imports, sorted by name 7 - * @param service 8 - */ 9 - export const postProcessServiceImports = (service: Service): string[] => service.imports.filter(unique).sort(sort);
-24
src/utils/postProcessServiceOperations.ts
··· 1 - import type { Operation, Service } from '../types/client'; 2 - 3 - export const postProcessServiceOperations = (service: Service): Operation[] => { 4 - const names = new Map<string, number>(); 5 - 6 - return service.operations.map(operation => { 7 - const clone = { ...operation }; 8 - 9 - // Parse the service parameters and results, very similar to how we parse 10 - // properties of models. These methods will extend the type if needed. 11 - clone.imports.push(...clone.parameters.flatMap(parameter => parameter.imports)); 12 - clone.imports.push(...clone.results.flatMap(result => result.imports)); 13 - 14 - // Check if the operation name is unique, if not then prefix this with a number 15 - const name = clone.name; 16 - const index = names.get(name) || 0; 17 - if (index > 0) { 18 - clone.name = `${name}${index}`; 19 - } 20 - names.set(name, index + 1); 21 - 22 - return clone; 23 - }); 24 - };
+97
src/utils/postprocess.ts
··· 1 + import type { Client, Enum, Model, Operation, Service } from '../types/client'; 2 + import { sort } from './sort'; 3 + import { unique } from './unique'; 4 + 5 + /** 6 + * Post process client 7 + * @param client Client object with all the models, services, etc. 8 + */ 9 + export function postProcessClient(client: Client): Client { 10 + return { 11 + ...client, 12 + models: client.models.map(model => postProcessModel(model)), 13 + services: client.services.map(service => postProcessService(service)), 14 + }; 15 + } 16 + 17 + /** 18 + * Post processes the model. 19 + * This will clean up any double imports or enum values. 20 + * @param model 21 + */ 22 + export function postProcessModel(model: Model): Model { 23 + return { 24 + ...model, 25 + imports: postProcessModelImports(model), 26 + enums: postProcessModelEnums(model), 27 + enum: postProcessModelEnum(model), 28 + }; 29 + } 30 + 31 + /** 32 + * Set unique enum values for the model 33 + * @param model 34 + */ 35 + export function postProcessModelEnum(model: Model): Enum[] { 36 + return model.enum.filter((property, index, arr) => arr.findIndex(item => item.value === property.value) === index); 37 + } 38 + 39 + /** 40 + * Set unique enum values for the model 41 + * @param model The model that is post-processed 42 + */ 43 + export function postProcessModelEnums(model: Model): Model[] { 44 + return model.enums.filter((property, index, arr) => arr.findIndex(item => item.name === property.name) === index); 45 + } 46 + 47 + /** 48 + * Set unique imports, sorted by name 49 + * @param model The model that is post-processed 50 + */ 51 + export function postProcessModelImports(model: Model): string[] { 52 + return model.imports 53 + .filter(unique) 54 + .sort(sort) 55 + .filter(name => model.name !== name); 56 + } 57 + 58 + export function postProcessService(service: Service): Service { 59 + const clone = { ...service }; 60 + clone.operations = postProcessServiceOperations(clone); 61 + clone.operations.forEach(operation => { 62 + clone.imports.push(...operation.imports); 63 + }); 64 + clone.imports = postProcessServiceImports(clone); 65 + return clone; 66 + } 67 + 68 + /** 69 + * Set unique imports, sorted by name 70 + * @param service 71 + */ 72 + export function postProcessServiceImports(service: Service): string[] { 73 + return service.imports.filter(unique).sort(sort); 74 + } 75 + 76 + export function postProcessServiceOperations(service: Service): Operation[] { 77 + const names = new Map<string, number>(); 78 + 79 + return service.operations.map(operation => { 80 + const clone = { ...operation }; 81 + 82 + // Parse the service parameters and results, very similar to how we parse 83 + // properties of models. These methods will extend the type if needed. 84 + clone.imports.push(...clone.parameters.flatMap(parameter => parameter.imports)); 85 + clone.imports.push(...clone.results.flatMap(result => result.imports)); 86 + 87 + // Check if the operation name is unique, if not then prefix this with a number 88 + const name = clone.name; 89 + const index = names.get(name) || 0; 90 + if (index > 0) { 91 + clone.name = `${name}${index}`; 92 + } 93 + names.set(name, index + 1); 94 + 95 + return clone; 96 + }); 97 + }
+45
src/utils/sanitize.ts
··· 1 + /** 2 + * Sanitizes names of types, so they are valid typescript identifiers of a certain form. 3 + * 4 + * 1: Remove any leading characters that are illegal as starting character of a typescript identifier. 5 + * 2: Replace illegal characters in remaining part of type name with underscore (_). 6 + * 7 + * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName 8 + * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It 9 + * would be sort of a breaking change to do so, though, previously generated code might change then. 10 + * 11 + * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers 12 + */ 13 + export function sanitizeTypeName(name: string): string { 14 + return name.replace(/^[^$_\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '_'); 15 + } 16 + 17 + /** 18 + * Sanitizes service names, so they are valid typescript identifiers of a certain form. 19 + * 20 + * 1: Remove any leading characters that are illegal as starting character of a typescript identifier. 21 + * 2: Replace illegal characters in remaining part of type name with hyphen (-). 22 + * 23 + * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName 24 + * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It 25 + * would be sort of a breaking change to do so, though, previously generated code might change then. 26 + * 27 + * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers 28 + * 29 + * The output of this is expected to be converted to PascalCase 30 + */ 31 + export function sanitizeServiceName(name: string): string { 32 + return name.replace(/^[^\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '-'); 33 + } 34 + 35 + /** 36 + * sanitizeOperationName does the same as sanitizeServiceName. 37 + */ 38 + export function sanitizeOperationName(name: string): string { 39 + return sanitizeServiceName(name); 40 + } 41 + 42 + export function sanitizeOperationParameterName(name: string): string { 43 + const withoutBrackets = name.replace('[]', 'Array'); 44 + return sanitizeOperationName(withoutBrackets); 45 + }
-7
src/utils/sanitizeOperationName.ts
··· 1 - import sanitizeServiceName from './sanitizeServiceName'; 2 - 3 - /** 4 - * sanitizeOperationName does the same as sanitizeServiceName. 5 - */ 6 - const sanitizeOperationName = sanitizeServiceName; 7 - export default sanitizeOperationName;
-7
src/utils/sanitizeOperationParameterName.ts
··· 1 - import sanitizeOperationName from './sanitizeOperationName'; 2 - 3 - const sanitizeOperationParameterName = (name: string): string => { 4 - const withoutBrackets = name.replace('[]', 'Array'); 5 - return sanitizeOperationName(withoutBrackets); 6 - }; 7 - export default sanitizeOperationParameterName;
-18
src/utils/sanitizeServiceName.ts
··· 1 - /** 2 - * Sanitizes service names, so they are valid typescript identifiers of a certain form. 3 - * 4 - * 1: Remove any leading characters that are illegal as starting character of a typescript identifier. 5 - * 2: Replace illegal characters in remaining part of type name with underscore (-). 6 - * 7 - * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName 8 - * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It 9 - * would be sort of a breaking change to do so, though, previously generated code might change then. 10 - * 11 - * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers 12 - * 13 - * The output of this is expected to be converted to PascalCase 14 - */ 15 - const sanitizeServiceName = (name: string) => 16 - name.replace(/^[^\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '-'); 17 - 18 - export default sanitizeServiceName;
-16
src/utils/sanitizeTypeName.ts
··· 1 - /** 2 - * Sanitizes names of types, so they are valid typescript identifiers of a certain form. 3 - * 4 - * 1: Remove any leading characters that are illegal as starting character of a typescript identifier. 5 - * 2: Replace illegal characters in remaining part of type name with underscore (_). 6 - * 7 - * Step 1 should perhaps instead also replace illegal characters with underscore, or prefix with it, like sanitizeEnumName 8 - * does. The way this is now one could perhaps end up removing all characters, if all are illegal start characters. It 9 - * would be sort of a breaking change to do so, though, previously generated code might change then. 10 - * 11 - * Javascript identifier regexp pattern retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers 12 - */ 13 - const sanitizeTypeName = (name: string) => 14 - name.replace(/^[^$_\p{ID_Start}]+/u, '').replace(/[^$\u200c\u200d\p{ID_Continue}]/gu, '_'); 15 - 16 - export default sanitizeTypeName;
+5 -9
src/utils/sort.ts
··· 1 - export const sort = (a: string, b: string): number => { 2 - const nameA = a.toLowerCase(); 3 - const nameB = b.toLowerCase(); 1 + export function sort(a: string, b: string): number { 2 + const nameA = a.toLocaleLowerCase(); 3 + const nameB = b.toLocaleLowerCase(); 4 4 return nameA.localeCompare(nameB, 'en'); 5 - }; 5 + } 6 6 7 7 export function sortByName<T extends { name: string }>(items: T[]): T[] { 8 - return items.sort((a, b) => { 9 - const nameA = a.name.toLocaleLowerCase(); 10 - const nameB = b.name.toLocaleLowerCase(); 11 - return nameA.localeCompare(nameB, 'en'); 12 - }); 8 + return items.sort((a, b) => sort(a.name, b.name)); 13 9 }
+1 -1
src/utils/type.ts
··· 1 1 import type { Type } from '../types/client'; 2 - import sanitizeTypeName from './sanitizeTypeName'; 2 + import { sanitizeTypeName } from './sanitize'; 3 3 import { stripNamespace } from './stripNamespace'; 4 4 5 5 const TYPE_MAPPINGS = new Map<string, string>([
+3 -1
src/utils/unique.ts
··· 1 - export const unique = <T>(value: T, index: number, arr: T[]): boolean => arr.indexOf(value) === index; 1 + export function unique<T>(value: T, index: number, arr: T[]): boolean { 2 + return arr.indexOf(value) === index; 3 + }