Skip to content

Commit

Permalink
fix(getters): path to operationId accept dash
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Mar 8, 2022
1 parent 37e363e commit 48c39af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/core/getters/operation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { OperationObject } from 'openapi3-ts';
import { getOperationId } from './operation';

describe('getOperationId getter', () => {
[
['/api/test/{id}', 'ApiTestId'],
['/api/test/{user_id}', 'ApiTestUserId'],
['/api/test/{locale}.js', 'ApiTestLocaleJs'],
['/api/test/i18n-{locale}.js', 'ApiTestI18nLocaleJs'],
['/api/test/{param1}-{param2}.js', 'ApiTestParam1Param2Js'],
['/api/test/user{param1}-{param2}.html', 'ApiTestUserparam1Param2Html'],
].forEach(([input, expected]) => {
it(`should process ${input} to ${expected}`, () => {
expect(getOperationId({} as OperationObject, input, '')).toBe(expected);
});
});
});
16 changes: 15 additions & 1 deletion src/core/getters/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,19 @@ export const getOperationId = (
return operation.operationId;
}

return pascal([verb, ...route.split('/').map((p) => sanitize(p))].join('-'));
return pascal(
[
verb,
...route
.split('/')
.map((p) =>
sanitize(p, {
dash: true,
underscore: '-',
dot: '-',
whitespace: '-',
}),
),
].join('-'),
);
};
7 changes: 6 additions & 1 deletion src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ export const sanitize = (
dash?: string | true;
},
) => {
const { whitespace = '', underscore = '', dot = '', dash = '' } = options || {};
const {
whitespace = '',
underscore = '',
dot = '',
dash = '',
} = options || {};
let newValue = value.replace(/[^\w\s.-]/g, '');

if (whitespace !== true) {
Expand Down

0 comments on commit 48c39af

Please sign in to comment.