Skip to content

Commit

Permalink
Merge pull request #4165 from BitGo/fix(core)--only-include-headers-i…
Browse files Browse the repository at this point in the history
…f-they-exist-inside-postWithCodec-call

fix(sdk-core): only add headers if they exist and nonempty
  • Loading branch information
ayush-9 authored Dec 20, 2023
2 parents 49f8063 + e85c55a commit 70b53c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
19 changes: 13 additions & 6 deletions modules/sdk-core/src/bitgo/utils/postWithCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@ export function postWithCodec<
console.error('error encoding request body for url', url, e);
codecError = true;
}
return agent
.post(url)
.set('io-ts-codec-encode-error', codecError ? 'true' : 'false')
.set('io-ts-codec-decode-error', getDecodeErrorKeys(codec, body).join(','))
.set('io-ts-unknown-properties', encodedBody ? getUnknownProperties(body, encodedBody).join(',') : 'NA')
.send(useEncodedBody && encodedBody ? encodedBody : body);
const postRequest = agent.post(url).set('io-ts-codec-encode-error', codecError ? 'true' : 'false');

if (codec) {
const decodeErrorKeys = getDecodeErrorKeys(codec, body).join(',');
if (decodeErrorKeys.trim() !== '') {
postRequest.set('io-ts-codec-decode-error', decodeErrorKeys);
}
}

const unknownProperties = encodedBody ? getUnknownProperties(body, encodedBody).join(',') : 'NA';
postRequest.set('io-ts-unknown-properties', unknownProperties || 'NA');

return postRequest.send(useEncodedBody && encodedBody ? encodedBody : body);
}
10 changes: 3 additions & 7 deletions modules/sdk-core/test/unit/bitgo/utils/postWithCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ describe('postWithCodec', function () {
{ foo: 'bar' },
{
'io-ts-codec-encode-error': 'false',
'io-ts-codec-decode-error': '',
'io-ts-unknown-properties': '',
'io-ts-unknown-properties': 'NA',
}
);

Expand All @@ -62,8 +61,7 @@ describe('postWithCodec', function () {
{ foo: 'bar', bar: null },
{
'io-ts-codec-encode-error': 'false',
'io-ts-codec-decode-error': '',
'io-ts-unknown-properties': '',
'io-ts-unknown-properties': 'NA',
}
);
});
Expand All @@ -76,7 +74,7 @@ describe('postWithCodec', function () {
{
'io-ts-codec-encode-error': 'false',
'io-ts-codec-decode-error': '0.foo',
'io-ts-unknown-properties': '',
'io-ts-unknown-properties': 'NA',
}
);

Expand All @@ -86,7 +84,6 @@ describe('postWithCodec', function () {
{ foo: 'bar' },
{
'io-ts-codec-encode-error': 'false',
'io-ts-codec-decode-error': '',
'io-ts-unknown-properties': 'boo',
}
);
Expand All @@ -97,7 +94,6 @@ describe('postWithCodec', function () {
{ foo: 'bar', boo: 1 },
{
'io-ts-codec-encode-error': 'false',
'io-ts-codec-decode-error': '',
'io-ts-unknown-properties': 'boo',
}
);
Expand Down

0 comments on commit 70b53c7

Please sign in to comment.