Skip to content

Commit

Permalink
Merge pull request #154 from hey-api/chore/unify-spec-parsing-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford authored Mar 27, 2024
2 parents b202265 + f0979d2 commit 052bc6c
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 61 deletions.
17 changes: 16 additions & 1 deletion src/openApi/common/parser/__tests__/service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { getServiceVersion } from '../service';
import { getServiceName, getServiceVersion } from '../service';

describe('getServiceVersion', () => {
it.each([
Expand All @@ -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);
});
});
13 changes: 13 additions & 0 deletions src/openApi/common/parser/service.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 });
};
15 changes: 0 additions & 15 deletions src/openApi/v2/parser/__tests__/getServiceName.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getOperation.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
12 changes: 0 additions & 12 deletions src/openApi/v2/parser/getServiceName.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/openApi/v3/parser/__tests__/service.spec.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/openApi/v3/parser/getServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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[] => {
Expand Down
14 changes: 0 additions & 14 deletions src/openApi/v3/parser/service.ts

This file was deleted.

0 comments on commit 052bc6c

Please sign in to comment.