Skip to content

Commit

Permalink
slightly more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Oct 30, 2024
1 parent 142c165 commit 526d1d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/kbn-router-to-openapispec/src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ describe('createOpIdGenerator', () => {
});
test.each([
{ input: { method: 'GET', path: '/api/file' }, output: 'get-file' },
{ input: { method: 'GET', path: '///api/file///' }, output: 'get-file' },
{ input: { method: 'POST', path: '/internal/api/file' }, output: 'post-file' },
{ input: { method: 'PUT', path: '/internal/file' }, output: 'put-file' },
{ input: { method: 'Put', path: 'fOO/fILe' }, output: 'put-foo-file' },
Expand Down
22 changes: 13 additions & 9 deletions packages/kbn-router-to-openapispec/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,26 @@ export const createOpIdGenerator = (): GetOpId => {
);
}

path = path.trim().toLowerCase();
path = path
.trim()
.replace(/^[\/]+/, '')
.replace(/[\/]+$/, '')
.toLowerCase();

const removePrefixes = ['/internal/api/', '/internal/', '/api/']; // longest to shortest
const removePrefixes = ['internal/api/', 'internal/', 'api/']; // longest to shortest
for (const prefix of removePrefixes) {
if (path.startsWith(prefix)) {
path = path.substring(prefix.length - 1);
path = path.substring(prefix.length);
break;
}
}

const opId = `${method.toLowerCase()}-${path
.replace(/^\//, '')
.replace(/\/$/, '')
.replace(/[\{\}\?\*]/g, '')
.replace(/[\/_]/g, '-')
.replace(/[-]+/g, '-')}`;
path = path
.replace(/[\{\}\?\*]/g, '') // remove special chars
.replace(/[\/_]/g, '-') // everything else to dashes
.replace(/[-]+/g, '-'); // single dashes

const opId = `${method.toLowerCase()}-${path}`;

const cachedCount = idMap.get(opId) ?? 0;
idMap.set(opId, cachedCount + 1);
Expand Down

0 comments on commit 526d1d3

Please sign in to comment.