Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: various cleanup changes and test changes #106

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Config, UserConfig } from './types/config';
import { getOpenApiSpec } from './utils/getOpenApiSpec';
import { registerHandlebarTemplates } from './utils/handlebars';
import { isSubDirectory } from './utils/isSubdirectory';
import { postProcessClient } from './utils/postProcessClient';
import { postProcessClient } from './utils/postprocess';
import { writeClient } from './utils/write/client';

type Dependencies = Record<string, unknown>;
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getOperationParameterName.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import camelCase from 'camelcase';

import { reservedWords } from '../../../utils/reservedWords';
import sanitizeOperationParameterName from '../../../utils/sanitizeOperationParameterName';
import { sanitizeOperationParameterName } from '../../../utils/sanitize';

/**
* Replaces any invalid characters from a parameter name.
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getServiceName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import camelCase from 'camelcase';

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

/**
* Convert the input value to a correct service name. This converts
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getOperationParameterName.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import camelCase from 'camelcase';

import { reservedWords } from '../../../utils/reservedWords';
import sanitizeOperationParameterName from '../../../utils/sanitizeOperationParameterName';
import { sanitizeOperationParameterName } from '../../../utils/sanitize';

/**
* Replaces any invalid characters from a parameter name.
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import camelCase from 'camelcase';

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

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

Expand Down
44 changes: 20 additions & 24 deletions src/utils/__tests__/escapeName.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,32 @@ import { describe, expect, it } from 'vitest';

import { escapeName, unescapeName } from '../escapeName';

const data = [
['', "''"],
['fooBar', 'fooBar'],
['Foo Bar', `'Foo Bar'`],
['foo bar', `'foo bar'`],
['foo-bar', `'foo-bar'`],
['foo.bar', `'foo.bar'`],
['foo_bar', 'foo_bar'],
['123foo.bar', `'123foo.bar'`],
['@foo.bar', `'@foo.bar'`],
['$foo.bar', `'$foo.bar'`],
['_foo.bar', `'_foo.bar'`],
['123foobar', `'123foobar'`],
['@foobar', `'@foobar'`],
['$foobar', '$foobar'],
['_foobar', '_foobar'],
const toCheck: { unescaped: string; escaped: string }[] = [
{ unescaped: '', escaped: "''" },
{ unescaped: 'fooBar', escaped: 'fooBar' },
{ unescaped: 'Foo Bar', escaped: `'Foo Bar'` },
{ unescaped: 'foo bar', escaped: `'foo bar'` },
{ unescaped: 'foo-bar', escaped: `'foo-bar'` },
{ unescaped: 'foo.bar', escaped: `'foo.bar'` },
{ unescaped: 'foo_bar', escaped: 'foo_bar' },
{ unescaped: '123foo.bar', escaped: `'123foo.bar'` },
{ unescaped: '@foo.bar', escaped: `'@foo.bar'` },
{ unescaped: '$foo.bar', escaped: `'$foo.bar'` },
{ unescaped: '_foo.bar', escaped: `'_foo.bar'` },
{ unescaped: '123foobar', escaped: `'123foobar'` },
{ unescaped: '@foobar', escaped: `'@foobar'` },
{ unescaped: '$foobar', escaped: '$foobar' },
{ unescaped: '_foobar', escaped: '_foobar' },
];

describe('escapeName', () => {
it('should escape', () => {
data.forEach(([unescaped, escaped]) => {
expect(escapeName(unescaped)).toBe(escaped);
});
it.each(toCheck)('should escape $unescaped to $escaped', ({ unescaped, escaped }) => {
expect(escapeName(unescaped)).toBe(escaped);
});
});

describe('unescapeName', () => {
it('should unescape', () => {
data.forEach(([unescaped, escaped]) => {
expect(unescapeName(escaped)).toBe(unescaped);
});
it.each(toCheck)('should unescape $escaped to $unescaped', ({ unescaped, escaped }) => {
expect(unescapeName(escaped)).toBe(unescaped);
});
});
22 changes: 12 additions & 10 deletions src/utils/__tests__/getPattern.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { describe, expect, it } from 'vitest';
import { getPattern } from '../getPattern';

describe('getPattern', () => {
it('should produce correct result', () => {
expect(getPattern()).toEqual(undefined);
expect(getPattern('')).toEqual('');
expect(getPattern('^[a-zA-Z]')).toEqual('^[a-zA-Z]');
expect(getPattern('^\\w+$')).toEqual('^\\\\w+$');
expect(getPattern('^\\d{3}-\\d{2}-\\d{4}$')).toEqual('^\\\\d{3}-\\\\d{2}-\\\\d{4}$');
expect(getPattern('\\')).toEqual('\\\\');
expect(getPattern('\\/')).toEqual('\\\\/');
expect(getPattern('\\/\\/')).toEqual('\\\\/\\\\/');
expect(getPattern("'")).toEqual("\\'");
it.each([
{ pattern: undefined, expected: undefined },
{ pattern: '', expected: '' },
{ pattern: '^[a-zA-Z]', expected: '^[a-zA-Z]' },
{ pattern: '^\\w+$', expected: '^\\\\w+$' },
{ pattern: '^\\d{3}-\\d{2}-\\d{4}$', expected: '^\\\\d{3}-\\\\d{2}-\\\\d{4}$' },
{ pattern: '\\', expected: '\\\\' },
{ pattern: '\\/', expected: '\\\\/' },
{ pattern: '\\/\\/', expected: '\\\\/\\\\/' },
{ pattern: "'", expected: "\\'" },
])('getPattern($pattern) -> $expected', ({ pattern, expected }) => {
expect(getPattern(pattern)).toEqual(expected);
});
});
54 changes: 54 additions & 0 deletions src/utils/__tests__/sanitize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, expect, it } from 'vitest';

import {
sanitizeOperationName,
sanitizeOperationParameterName,
sanitizeServiceName,
sanitizeTypeName,
} from '../sanitize';

describe('sanitizeOperationName', () => {
it('should remove/replace illegal characters', () => {
expect(sanitizeOperationName('abc')).toEqual('abc');
expect(sanitizeOperationName('æbc')).toEqual('æbc');
expect(sanitizeOperationName('æb.c')).toEqual('æb-c');
expect(sanitizeOperationName('1æb.c')).toEqual('æb-c');
});
});

describe('sanitizeOperationParameterName', () => {
it('should remove/replace illegal characters', () => {
expect(sanitizeOperationParameterName('abc')).toEqual('abc');
expect(sanitizeOperationParameterName('æbc')).toEqual('æbc');
expect(sanitizeOperationParameterName('æb.c')).toEqual('æb-c');
expect(sanitizeOperationParameterName('1æb.c')).toEqual('æb-c');
expect(sanitizeOperationParameterName('unknown[]')).toEqual('unknownArray');
});
});

describe('sanitizeServiceName', () => {
it('should remove/replace illegal characters', () => {
expect(sanitizeServiceName('abc')).toEqual('abc');
expect(sanitizeServiceName('æbc')).toEqual('æbc');
expect(sanitizeServiceName('æb.c')).toEqual('æb-c');
expect(sanitizeServiceName('1æb.c')).toEqual('æb-c');
});
});

describe('sanitizeTypeName', () => {
it('should remove/replace illegal characters', () => {
expect(sanitizeTypeName('abc')).toEqual('abc');
expect(sanitizeTypeName('æbc')).toEqual('æbc');
expect(sanitizeTypeName('æb.c')).toEqual('æb_c');
expect(sanitizeTypeName('1æb.c')).toEqual('æb_c');
});
});

describe('sanitizeTypeName', () => {
it('should remove/replace illegal characters', () => {
expect(sanitizeTypeName('abc')).toEqual('abc');
expect(sanitizeTypeName('æbc')).toEqual('æbc');
expect(sanitizeTypeName('æb.c')).toEqual('æb_c');
expect(sanitizeTypeName('1æb.c')).toEqual('æb_c');
});
});
12 changes: 0 additions & 12 deletions src/utils/__tests__/sanitizeTypeName.spec.ts

This file was deleted.

46 changes: 24 additions & 22 deletions src/utils/__tests__/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ import { describe, expect, it } from 'vitest';
import { getMappedType, getType } from '../type';

describe('getMappedType', () => {
it('should map types to the basics', () => {
expect(getMappedType('')).toEqual(undefined);
expect(getMappedType('any')).toEqual('unknown');
expect(getMappedType('array')).toEqual('unknown[]');
expect(getMappedType('boolean')).toEqual('boolean');
expect(getMappedType('byte')).toEqual('number');
expect(getMappedType('char')).toEqual('string');
expect(getMappedType('date-time')).toEqual('string');
expect(getMappedType('date')).toEqual('string');
expect(getMappedType('double')).toEqual('number');
expect(getMappedType('file')).toEqual('binary');
expect(getMappedType('float')).toEqual('number');
expect(getMappedType('int')).toEqual('number');
expect(getMappedType('integer')).toEqual('number');
expect(getMappedType('long')).toEqual('number');
expect(getMappedType('null')).toEqual('null');
expect(getMappedType('number')).toEqual('number');
expect(getMappedType('object')).toEqual('unknown');
expect(getMappedType('password')).toEqual('string');
expect(getMappedType('short')).toEqual('number');
expect(getMappedType('string')).toEqual('string');
expect(getMappedType('void')).toEqual('void');
it.each([
{ type: '', expected: undefined },
{ type: 'any', expected: 'unknown' },
{ type: 'array', expected: 'unknown[]' },
{ type: 'boolean', expected: 'boolean' },
{ type: 'byte', expected: 'number' },
{ type: 'char', expected: 'string' },
{ type: 'date-time', expected: 'string' },
{ type: 'date', expected: 'string' },
{ type: 'double', expected: 'number' },
{ type: 'file', expected: 'binary' },
{ type: 'float', expected: 'number' },
{ type: 'int', expected: 'number' },
{ type: 'integer', expected: 'number' },
{ type: 'long', expected: 'number' },
{ type: 'null', expected: 'null' },
{ type: 'number', expected: 'number' },
{ type: 'object', expected: 'unknown' },
{ type: 'password', expected: 'string' },
{ type: 'short', expected: 'number' },
{ type: 'string', expected: 'string' },
{ type: 'void', expected: 'void' },
])('should map type $type to $expected', ({ type, expected }) => {
expect(getMappedType(type)).toEqual(expected);
});
});

Expand Down
22 changes: 15 additions & 7 deletions src/utils/__tests__/unique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { describe, expect, it } from 'vitest';
import { unique } from '../unique';

describe('unique', () => {
it('should return correct index', () => {
expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy();
expect(unique('a', 1, ['a', 'b', 'c'])).toBeFalsy();
expect(unique('a', 2, ['a', 'b', 'c'])).toBeFalsy();
expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy();
expect(unique('a', 1, ['z', 'a', 'b'])).toBeTruthy();
expect(unique('a', 2, ['y', 'z', 'a'])).toBeTruthy();
it.each([
{ value: 'a', index: 0, arr: ['a', 'b', 'c'], result: true },
{ value: 'a', index: 1, arr: ['a', 'b', 'c'], result: false },
{ value: 'a', index: 2, arr: ['a', 'b', 'c'], result: false },
{ value: 'a', index: 1, arr: ['z', 'a', 'b'], result: true },
{ value: 'a', index: 2, arr: ['y', 'z', 'a'], result: true },
])('unique($value, $index, $arr) -> $result', ({ value, index, arr, result }) => {
expect(unique(value, index, arr)).toEqual(result);
});

it.each([
{ input: ['a', 'a', 'b', 'c', 'b', 'b'], expected: ['a', 'b', 'c'] },
{ input: [1, 2, 3, 4, 4, 5, 6, 3], expected: [1, 2, 3, 4, 5, 6] },
])('should filter: $input to the unique array: $expected', ({ input, expected }) => {
expect(input.filter(unique)).toEqual(expected);
});
});
2 changes: 1 addition & 1 deletion src/utils/operation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import camelCase from 'camelcase';

import type { Config } from '../types/config';
import sanitizeOperationName from './sanitizeOperationName';
import { sanitizeOperationName } from './sanitize';

/**
* Convert the input value to a correct operation (method) classname.
Expand Down
13 changes: 0 additions & 13 deletions src/utils/postProcessClient.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/utils/postProcessModel.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/utils/postProcessModelEnum.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/utils/postProcessModelEnums.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/utils/postProcessModelImports.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/utils/postProcessService.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/utils/postProcessServiceImports.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/utils/postProcessServiceOperations.ts

This file was deleted.

Loading
Loading