-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from hey-api/chore/cleanup-files
- Loading branch information
Showing
30 changed files
with
288 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { getOperationName, getOperationParameterName, getOperationResponseCode } from '../operation'; | ||
|
||
describe('getOperationName', () => { | ||
const options1: Parameters<typeof getOperationName>[2] = { | ||
operationId: true, | ||
}; | ||
|
||
const options2: Parameters<typeof getOperationName>[2] = { | ||
operationId: false, | ||
}; | ||
|
||
it.each([ | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'GetAllUsers', | ||
expected: 'getAllUsers', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: undefined, | ||
expected: 'getApiUsers', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'POST', | ||
options: options1, | ||
operationId: undefined, | ||
expected: 'postApiUsers', | ||
}, | ||
{ url: '/api/v1/users', method: 'GET', options: options1, operationId: 'GetAllUsers', expected: 'getAllUsers' }, | ||
{ url: '/api/v1/users', method: 'GET', options: options1, operationId: undefined, expected: 'getApiV1Users' }, | ||
{ url: '/api/v1/users', method: 'POST', options: options1, operationId: undefined, expected: 'postApiV1Users' }, | ||
{ | ||
url: '/api/v1/users/{id}', | ||
method: 'GET', | ||
options: options1, | ||
operationId: undefined, | ||
expected: 'getApiV1UsersById', | ||
}, | ||
{ | ||
url: '/api/v1/users/{id}', | ||
method: 'POST', | ||
options: options1, | ||
operationId: undefined, | ||
expected: 'postApiV1UsersById', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'fooBar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'FooBar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'Foo Bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'foo bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'foo-bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'foo_bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: 'foo.bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: '@foo.bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: '$foo.bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: '_foo.bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: '-foo.bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options1, | ||
operationId: '123.foo.bar', | ||
expected: 'fooBar', | ||
}, | ||
{ | ||
url: '/api/v1/users', | ||
method: 'GET', | ||
options: options2, | ||
operationId: 'GetAllUsers', | ||
expected: 'getApiV1Users', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users', | ||
method: 'GET', | ||
options: options2, | ||
operationId: 'fooBar', | ||
expected: 'getApiUsers', | ||
}, | ||
{ | ||
url: '/api/v{api-version}/users/{userId}/location/{locationId}', | ||
method: 'GET', | ||
options: options2, | ||
operationId: 'fooBar', | ||
expected: 'getApiUsersByUserIdLocationByLocationId', | ||
}, | ||
])( | ||
'getOperationName($url, $method, { operationId: $useOperationId }, $operationId) -> $expected', | ||
({ url, method, options, operationId, expected }) => { | ||
expect(getOperationName(url, method, options, operationId)).toEqual(expected); | ||
} | ||
); | ||
}); | ||
|
||
describe('getOperationParameterName', () => { | ||
it.each([ | ||
{ input: '', expected: '' }, | ||
{ input: 'foobar', expected: 'foobar' }, | ||
{ input: 'fooBar', expected: 'fooBar' }, | ||
{ input: 'foo_bar', expected: 'fooBar' }, | ||
{ input: 'foo-bar', expected: 'fooBar' }, | ||
{ input: 'foo.bar', expected: 'fooBar' }, | ||
{ input: '@foo.bar', expected: 'fooBar' }, | ||
{ input: '$foo.bar', expected: 'fooBar' }, | ||
{ input: '123.foo.bar', expected: 'fooBar' }, | ||
{ input: 'Foo-Bar', expected: 'fooBar' }, | ||
{ input: 'FOO-BAR', expected: 'fooBar' }, | ||
{ input: 'foo[bar]', expected: 'fooBar' }, | ||
{ input: 'foo.bar[]', expected: 'fooBarArray' }, | ||
])('getOperationParameterName($input) -> $expected', ({ input, expected }) => { | ||
expect(getOperationParameterName(input)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('getOperationResponseCode', () => { | ||
it.each([ | ||
{ input: '', expected: null }, | ||
{ input: 'default', expected: 200 }, | ||
{ input: '200', expected: 200 }, | ||
{ input: '300', expected: 300 }, | ||
{ input: '400', expected: 400 }, | ||
{ input: 'abc', expected: null }, | ||
{ input: '-100', expected: 100 }, | ||
])('getOperationResponseCode($input) -> $expected', ({ input, expected }) => { | ||
expect(getOperationResponseCode(input)).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import camelCase from 'camelcase'; | ||
|
||
import type { OperationError, OperationResponse } from '../../../types/client'; | ||
import type { Config } from '../../../types/config'; | ||
import { reservedWords } from '../../../utils/reservedWords'; | ||
import { sanitizeOperationName, sanitizeOperationParameterName } from '../../../utils/sanitize'; | ||
|
||
/** | ||
* Convert the input value to a correct operation (method) classname. | ||
* This will use the operation ID - if available - and otherwise fallback | ||
* on a generated name from the URL | ||
*/ | ||
export const getOperationName = ( | ||
url: string, | ||
method: string, | ||
options: Pick<Config, 'operationId'>, | ||
operationId?: string | ||
): string => { | ||
if (options.operationId && operationId) { | ||
return camelCase(sanitizeOperationName(operationId).trim()); | ||
} | ||
|
||
const urlWithoutPlaceholders = url | ||
.replace(/[^/]*?{api-version}.*?\//g, '') | ||
.replace(/{(.*?)}/g, 'by-$1') | ||
.replace(/\//g, '-'); | ||
|
||
return camelCase(`${method}-${urlWithoutPlaceholders}`); | ||
}; | ||
|
||
/** | ||
* Replaces any invalid characters from a parameter name. | ||
* For example: 'filter.someProperty' becomes 'filterSomeProperty'. | ||
*/ | ||
export const getOperationParameterName = (value: string): string => { | ||
const clean = sanitizeOperationParameterName(value).trim(); | ||
return camelCase(clean).replace(reservedWords, '_$1'); | ||
}; | ||
|
||
export const getOperationResponseHeader = (operationResponses: OperationResponse[]): string | null => { | ||
const header = operationResponses.find(operationResponses => operationResponses.in === 'header'); | ||
if (header) { | ||
return header.name; | ||
} | ||
return null; | ||
}; | ||
|
||
export const getOperationResponseCode = (value: string | 'default'): number | null => { | ||
// You can specify a "default" response, this is treated as HTTP code 200 | ||
if (value === 'default') { | ||
return 200; | ||
} | ||
|
||
// Check if we can parse the code and return of successful. | ||
if (/[0-9]+/g.test(value)) { | ||
const code = parseInt(value); | ||
if (Number.isInteger(code)) { | ||
return Math.abs(code); | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const getOperationErrors = (operationResponses: OperationResponse[]): OperationError[] => | ||
operationResponses | ||
.filter(operationResponse => operationResponse.code >= 300 && operationResponse.description) | ||
.map(response => ({ | ||
code: response.code, | ||
description: response.description!, | ||
})); |
2 changes: 1 addition & 1 deletion
2
src/openApi/v2/parser/getServer.spec.ts → ...Api/v2/parser/__tests__/getServer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/openApi/v2/parser/getServiceName.spec.ts → ...2/parser/__tests__/getServiceName.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/openApi/v2/parser/getServices.spec.ts → ...i/v2/parser/__tests__/getServices.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.