From 394a26b7fc959eef5bdd54a8ddaf57e74db8d92c Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Wed, 27 Mar 2024 13:58:08 +1100 Subject: [PATCH 1/2] chore(parser): move getServiceName to common parsing --- .../common/parser/__tests__/service.spec.ts | 17 ++++++++++++++++- src/openApi/common/parser/service.ts | 13 +++++++++++++ .../v2/parser/__tests__/getServiceName.spec.ts | 15 --------------- src/openApi/v2/parser/getOperation.ts | 2 +- src/openApi/v2/parser/getServiceName.ts | 12 ------------ src/openApi/v3/parser/__tests__/service.spec.ts | 16 ---------------- src/openApi/v3/parser/operation.ts | 2 +- src/openApi/v3/parser/service.ts | 13 ------------- 8 files changed, 31 insertions(+), 59 deletions(-) delete mode 100644 src/openApi/v2/parser/__tests__/getServiceName.spec.ts delete mode 100644 src/openApi/v2/parser/getServiceName.ts delete mode 100644 src/openApi/v3/parser/__tests__/service.spec.ts diff --git a/src/openApi/common/parser/__tests__/service.spec.ts b/src/openApi/common/parser/__tests__/service.spec.ts index 4bc2eac1e..574ca72aa 100644 --- a/src/openApi/common/parser/__tests__/service.spec.ts +++ b/src/openApi/common/parser/__tests__/service.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { getServiceVersion } from '../service'; +import { getServiceName, getServiceVersion } from '../service'; describe('getServiceVersion', () => { it.each([ @@ -11,3 +11,18 @@ describe('getServiceVersion', () => { expect(getServiceVersion(input)).toEqual(expected); }); }); + +describe('getServiceName', () => { + it.each([ + { input: '', expected: '' }, + { input: 'FooBar', expected: 'FooBar' }, + { input: 'Foo Bar', expected: 'FooBar' }, + { input: 'foo bar', expected: 'FooBar' }, + { input: '@fooBar', expected: 'FooBar' }, + { input: '$fooBar', expected: 'FooBar' }, + { input: '123fooBar', expected: 'FooBar' }, + { input: 'non-ascii-æøåÆØÅöôêÊ字符串', expected: 'NonAsciiÆøåÆøÅöôêÊ字符串' }, + ])('getServiceName($input) -> $expected', ({ input, expected }) => { + expect(getServiceName(input)).toEqual(expected); + }); +}); diff --git a/src/openApi/common/parser/service.ts b/src/openApi/common/parser/service.ts index 7d860d6db..4dacf8cdd 100644 --- a/src/openApi/common/parser/service.ts +++ b/src/openApi/common/parser/service.ts @@ -1,3 +1,7 @@ +import camelCase from 'camelcase'; + +import { sanitizeServiceName } from './sanitize'; + /** * Convert the service version to 'normal' version. * This basically removes any "v" prefix from the version string. @@ -6,3 +10,12 @@ export function getServiceVersion(version = '1.0'): string { return String(version).replace(/^v/gi, ''); } + +/** + * Convert the input value to a correct service name. This converts + * the input string to PascalCase. + */ +export const getServiceName = (value: string): string => { + const clean = sanitizeServiceName(value).trim(); + return camelCase(clean, { pascalCase: true }); +}; diff --git a/src/openApi/v2/parser/__tests__/getServiceName.spec.ts b/src/openApi/v2/parser/__tests__/getServiceName.spec.ts deleted file mode 100644 index 8925658b1..000000000 --- a/src/openApi/v2/parser/__tests__/getServiceName.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { describe, expect, it } from 'vitest'; - -import { getServiceName } from '../getServiceName'; - -describe('getServiceName', () => { - it('should produce correct result', () => { - expect(getServiceName('')).toEqual(''); - expect(getServiceName('FooBar')).toEqual('FooBar'); - expect(getServiceName('Foo Bar')).toEqual('FooBar'); - expect(getServiceName('foo bar')).toEqual('FooBar'); - expect(getServiceName('@fooBar')).toEqual('FooBar'); - expect(getServiceName('$fooBar')).toEqual('FooBar'); - expect(getServiceName('123fooBar')).toEqual('FooBar'); - }); -}); diff --git a/src/openApi/v2/parser/getOperation.ts b/src/openApi/v2/parser/getOperation.ts index 985c7b95b..c3bb067f2 100644 --- a/src/openApi/v2/parser/getOperation.ts +++ b/src/openApi/v2/parser/getOperation.ts @@ -1,13 +1,13 @@ import type { Config } from '../../../types/config'; import type { Operation, OperationParameters } from '../../common/interfaces/client'; import { getOperationErrors, getOperationName, getOperationResponseHeader } from '../../common/parser/operation'; +import { getServiceName } from '../../common/parser/service'; import { toSortedByRequired } from '../../common/parser/sort'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiOperation } from '../interfaces/OpenApiOperation'; import { getOperationParameters } from './getOperationParameters'; import { getOperationResponses } from './getOperationResponses'; import { getOperationResults } from './getOperationResults'; -import { getServiceName } from './getServiceName'; export const getOperation = ( openApi: OpenApi, diff --git a/src/openApi/v2/parser/getServiceName.ts b/src/openApi/v2/parser/getServiceName.ts deleted file mode 100644 index 9acae9c10..000000000 --- a/src/openApi/v2/parser/getServiceName.ts +++ /dev/null @@ -1,12 +0,0 @@ -import camelCase from 'camelcase'; - -import { sanitizeServiceName } from '../../common/parser/sanitize'; - -/** - * Convert the input value to a correct service name. This converts - * the input string to PascalCase. - */ -export const getServiceName = (value: string): string => { - const clean = sanitizeServiceName(value).trim(); - return camelCase(clean, { pascalCase: true }); -}; diff --git a/src/openApi/v3/parser/__tests__/service.spec.ts b/src/openApi/v3/parser/__tests__/service.spec.ts deleted file mode 100644 index 4f7966db9..000000000 --- a/src/openApi/v3/parser/__tests__/service.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { describe, expect, it } from 'vitest'; - -import { getServiceName } from '../service'; - -describe('getServiceName', () => { - it('should produce correct result', () => { - expect(getServiceName('')).toEqual(''); - expect(getServiceName('FooBar')).toEqual('FooBar'); - expect(getServiceName('Foo Bar')).toEqual('FooBar'); - expect(getServiceName('foo bar')).toEqual('FooBar'); - expect(getServiceName('@fooBar')).toEqual('FooBar'); - expect(getServiceName('$fooBar')).toEqual('FooBar'); - expect(getServiceName('123fooBar')).toEqual('FooBar'); - expect(getServiceName('non-ascii-æøåÆØÅöôêÊ字符串')).toEqual('NonAsciiÆøåÆøÅöôêÊ字符串'); - }); -}); diff --git a/src/openApi/v3/parser/operation.ts b/src/openApi/v3/parser/operation.ts index b002324e4..ab0958ff4 100644 --- a/src/openApi/v3/parser/operation.ts +++ b/src/openApi/v3/parser/operation.ts @@ -2,6 +2,7 @@ import type { Config } from '../../../types/config'; import type { Operation, OperationParameter, OperationParameters } from '../../common/interfaces/client'; import { getRef } from '../../common/parser/getRef'; import { getOperationErrors, getOperationName, getOperationResponseHeader } from '../../common/parser/operation'; +import { getServiceName } from '../../common/parser/service'; import { toSortedByRequired } from '../../common/parser/sort'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiOperation } from '../interfaces/OpenApiOperation'; @@ -10,7 +11,6 @@ import { getOperationParameters } from './getOperationParameters'; import { getOperationRequestBody } from './getOperationRequestBody'; import { getOperationResponses } from './getOperationResponses'; import { getOperationResults } from './getOperationResults'; -import { getServiceName } from './service'; // add global path parameters, skip duplicate names const mergeParameters = (opParams: OperationParameter[], globalParams: OperationParameter[]): OperationParameter[] => { diff --git a/src/openApi/v3/parser/service.ts b/src/openApi/v3/parser/service.ts index 1e0bf8059..15f32fb27 100644 --- a/src/openApi/v3/parser/service.ts +++ b/src/openApi/v3/parser/service.ts @@ -1,14 +1 @@ -import camelCase from 'camelcase'; - -import { sanitizeServiceName } from '../../common/parser/sanitize'; - export const allowedServiceMethods = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'] as const; - -/** - * Convert the input value to a correct service name. This converts - * the input string to PascalCase. - */ -export const getServiceName = (value: string): string => { - const clean = sanitizeServiceName(value).trim(); - return camelCase(clean, { pascalCase: true }); -}; From bf9627b27ef1c14069b71fd9c8ba90bbd5ca95a5 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Wed, 27 Mar 2024 14:01:29 +1100 Subject: [PATCH 2/2] chore(parser): move allowed service methods array --- src/openApi/v3/parser/getServices.ts | 3 ++- src/openApi/v3/parser/service.ts | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 src/openApi/v3/parser/service.ts diff --git a/src/openApi/v3/parser/getServices.ts b/src/openApi/v3/parser/getServices.ts index 69f5800b4..ec70a080e 100644 --- a/src/openApi/v3/parser/getServices.ts +++ b/src/openApi/v3/parser/getServices.ts @@ -4,7 +4,8 @@ import type { Operation, Service } from '../../common/interfaces/client'; import type { OpenApi } from '../interfaces/OpenApi'; import { getOperationParameters } from './getOperationParameters'; import { getOperation } from './operation'; -import { allowedServiceMethods } from './service'; + +const allowedServiceMethods = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'] as const; const getNewService = (operation: Operation): Service => ({ $refs: [], diff --git a/src/openApi/v3/parser/service.ts b/src/openApi/v3/parser/service.ts deleted file mode 100644 index 15f32fb27..000000000 --- a/src/openApi/v3/parser/service.ts +++ /dev/null @@ -1 +0,0 @@ -export const allowedServiceMethods = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'] as const;