Skip to content

Commit

Permalink
fix(types): verify response types (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck authored Feb 27, 2023
1 parent f09a8a2 commit 888111d
Show file tree
Hide file tree
Showing 57 changed files with 1,132 additions and 495 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ root = true

[*]
end_of_line = lf
insert_final_newline = true
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 80
indent_style = space
indent_size = 4
indent_size = 2
charset = utf-8
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions packages/server-client/__tests__/__dataSets__/camelCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,26 @@ export default [
fooBar: null,
},
},
{
label: 'Transform to camel deep array only',
transformFn: 'camelCaseObjectKeys',
original: {
foo_bar: [
{
fizz_buzz: 'baz_bat',
},
],
},
parameters: [
true, // deep
false, // preserve
],
expected: {
fooBar: [
{
fizzBuzz: 'baz_bat',
},
],
},
},
];
49 changes: 31 additions & 18 deletions packages/server-client/lib/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
import camelCase from 'lodash.camelcase';
import snakeCase from 'lodash.snakecase';
import partial from 'lodash.partial';
import isObject from 'lodash.isobject';

export const transformObjectKeys = (
transformFn: (key: string | number) => string,
objectToTransform: Record<string | number, unknown>,
deep = false,
preserve = false,
): Record<string | number, unknown> => {
const transformedObject = {};
const transformedObject = {
...(preserve ? objectToTransform : {}),
};

for (const prop in objectToTransform) {
if (!Object.prototype.hasOwnProperty.call(objectToTransform, prop)) {
continue;
}

const newKey = transformFn(prop);
const value = objectToTransform[prop];
transformedObject[transformFn(prop)]
= deep
&& typeof value === 'object'
&& value !== null
&& !Array.isArray(value)
? transformObjectKeys(
transformFn,
value as Record<string | number, unknown>,
deep,
preserve,
)
: value;
if (!deep || !isObject(value)) {
transformedObject[newKey] = value;
continue;
}

if (Array.isArray(value)) {
transformedObject[newKey] = value.map((t) =>
isObject(t)
? transformObjectKeys(
transformFn,
t as Record<string | number, unknown>,
deep,
preserve,
)
: t,
);
continue;
}

transformedObject[newKey] = transformObjectKeys(
transformFn,
value as Record<string | number, unknown>,
deep,
preserve,
);
}

return {
...transformedObject,
...(preserve ? objectToTransform : {}),
};
return transformedObject;
};

export const camelCaseObjectKeys = partial(transformObjectKeys, camelCase);
Expand Down
1 change: 1 addition & 0 deletions packages/server-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@vonage/auth": "^1.0.9",
"@vonage/vetch": "^1.0.10",
"lodash.camelcase": "^4.3.0",
"lodash.isobject": "3.0.2",
"lodash.partial": "^4.2.1",
"lodash.snakecase": "^4.1.1"
},
Expand Down
64 changes: 64 additions & 0 deletions packages/verify/__tests__/__dataSets__/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Command, CheckStatus } from '../../lib/enums/index';
import {
VerifyCancelResponse,
VerifyControlErrorResponse,
} from '../../lib/interfaces/Response/index';
import { VerifyControl, VerifyControlError } from '../../lib/interfaces/index';

export default [
{
label: 'cancel a verify request',
request: [
'/verify/control/json',
'POST',
{
api_key: '12345',
api_secret: 'ABCDE',
request_id: 'abcdef0123456789abcdef0123456789',
cmd: 'cancel',
},
],
response: [
200,
{
status: CheckStatus.SUCCESS,
command: Command.CANCEL,
} as VerifyCancelResponse,
],
method: 'post',
clientMethod: 'cancel',
parameters: ['abcdef0123456789abcdef0123456789'],
expected: {
status: CheckStatus.SUCCESS,
command: Command.CANCEL,
} as VerifyControl,
},
{
label: 'cancel a verify request with an error',
request: [
'/verify/control/json',
'POST',
{
api_key: '12345',
api_secret: 'ABCDE',
request_id: 'abcdef0123456789abcdef0123456789',
cmd: 'cancel',
},
],
response: [
200,
{
status: CheckStatus.THROTTLED,
error_text: 'Your request is throttled',
} as VerifyControlErrorResponse,
],
method: 'post',
clientMethod: 'cancel',
parameters: ['abcdef0123456789abcdef0123456789'],
expected: {
status: CheckStatus.THROTTLED,
error_text: 'Your request is throttled',
errorText: 'Your request is throttled',
} as VerifyControlError,
},
];
42 changes: 42 additions & 0 deletions packages/verify/__tests__/__dataSets__/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { VerifyCheckResponse, VerifyCheck } from '../../lib/interfaces/index';

export default [
{
label: 'check a verify request',
request: [
'/verify/check/json',
'POST',
{
api_key: '12345',
api_secret: 'ABCDE',
request_id: 'abcdef0123456789abcdef0123456789',
code: '1234',
},
],
response: [
200,
{
request_id: 'abcdef0123456789abcdef0123456789',
event_id: '0A00000012345678',
status: '0',
price: '0.10000000',
currency: 'EUR',
estimated_price_messages_sent: '0.03330000',
} as VerifyCheckResponse,
],
method: 'post',
clientMethod: 'check',
parameters: ['abcdef0123456789abcdef0123456789', '1234'],
expected: {
request_id: 'abcdef0123456789abcdef0123456789',
requestId: 'abcdef0123456789abcdef0123456789',
event_id: '0A00000012345678',
eventId: '0A00000012345678',
status: '0',
price: '0.10000000',
currency: 'EUR',
estimated_price_messages_sent: '0.03330000',
estimatedPriceMessagesSent: '0.03330000',
} as VerifyCheck,
},
];
23 changes: 23 additions & 0 deletions packages/verify/__tests__/__dataSets__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import checkTests from './check';
import cancelTests from './cancel';
import searchTests from './search';
import startTests from './start';

export default [
{
label: 'Verify Check',
tests: checkTests,
},
{
label: 'Verify Cancel',
tests: cancelTests,
},
{
label: 'Verify Search',
tests: searchTests,
},
{
label: 'Verify Start',
tests: startTests,
},
];
Loading

0 comments on commit 888111d

Please sign in to comment.