Skip to content

Commit

Permalink
Merge pull request #4168 from BitGo/aloe/removeUnusedHeaders
Browse files Browse the repository at this point in the history
fix: remove unused dynamic headers
  • Loading branch information
bitgoAaron authored Dec 20, 2023
2 parents 70b53c7 + 4243c1d commit 070d057
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 91 deletions.
46 changes: 1 addition & 45 deletions modules/sdk-core/src/bitgo/utils/postWithCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,6 @@ import * as t from 'io-ts';
import { BitGoBase } from '../bitgoBase';
import { BitGoRequest } from '../../api';
import { SuperAgent, SuperAgentRequest } from 'superagent';
import { isLeft } from 'fp-ts/Either';

/**
* @param body
* @param encodedBody
* @returns a list of unknown properties that are present in the body but not the codec.
*/
function getUnknownProperties(body: Record<string, unknown>, encodedBody: Record<string, unknown>): string[] {
const unknownProperties: string[] = [];
if (body && encodedBody) {
const bodyKeys = Object.keys(body);
const encodedBodyKeys = Object.keys(encodedBody);
const unknownKeys = bodyKeys.filter((key) => !encodedBodyKeys.includes(key));
unknownProperties.push(...unknownKeys);
}
return unknownProperties;
}

function getDecodeErrorKeys<A extends Record<string, unknown>, O extends Record<string, unknown>>(
codec: t.Type<A, O>,
body: A
): string[] {
function toKeyPath(context: t.Context): string {
return context.flatMap((c) => (c.key ? [c.key] : [])).join('.');
}
const errors = codec.decode(body);
if (isLeft(errors)) {
return errors.left.map((error) => toKeyPath(error.context));
}
return [];
}

/**
* Try to encode the body with the codec and send the request.
Expand Down Expand Up @@ -60,25 +29,12 @@ export function postWithCodec<
} = {}
): TAgent extends BitGoBase ? BitGoRequest : SuperAgentRequest {
let encodedBody: O | undefined;
let codecError;
try {
encodedBody = codec.encode(body);
codecError = false;
} catch (e) {
console.error('error encoding request body for url', url, e);
codecError = true;
}
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');
const postRequest = agent.post(url);

return postRequest.send(useEncodedBody && encodedBody ? encodedBody : body);
}
55 changes: 9 additions & 46 deletions modules/sdk-core/test/unit/bitgo/utils/postWithCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,67 +35,30 @@ describe('postWithCodec', function () {
body: unknown;
headers: Headers;
},
body: unknown,
headers: Headers
body: unknown
) {
assert.deepStrictEqual(request.body, body);
for (const [key, value] of Object.entries(headers)) {
assert.deepStrictEqual(request.headers[key], value, `header ${key} does not match`);
}
}

const codec = t.exact(t.intersection([t.type({ foo: t.string }), t.partial({ bar: t.unknown })]));

it('has expected values with value matching codec', function () {
assertRequestContains(
getRequest(codec, { foo: 'bar' }),
{ foo: 'bar' },
{
'io-ts-codec-encode-error': 'false',
'io-ts-unknown-properties': 'NA',
}
);
assertRequestContains(getRequest(codec, { foo: 'bar' }), { foo: 'bar' });

assertRequestContains(
getRequest(codec, { foo: 'bar', bar: null }),
{ foo: 'bar', bar: null },
{
'io-ts-codec-encode-error': 'false',
'io-ts-unknown-properties': 'NA',
}
);
assertRequestContains(getRequest(codec, { foo: 'bar', bar: null }), { foo: 'bar', bar: null });
});

it('has expected values with value not matching codec', function () {
// invalid value
assertRequestContains(
getRequest(codec, { foo: null } as any),
{ foo: null },
{
'io-ts-codec-encode-error': 'false',
'io-ts-codec-decode-error': '0.foo',
'io-ts-unknown-properties': 'NA',
}
);
assertRequestContains(getRequest(codec, { foo: null } as any), { foo: null });

// non-exact value
assertRequestContains(
getRequest(codec, { foo: 'bar', boo: 1 } as any),
{ foo: 'bar' },
{
'io-ts-codec-encode-error': 'false',
'io-ts-unknown-properties': 'boo',
}
);
assertRequestContains(getRequest(codec, { foo: 'bar', boo: 1 } as any), { foo: 'bar' });

// non-exact value, useEncodedBody=false
assertRequestContains(
getRequest(codec, { foo: 'bar', boo: 1 } as any, { useEncodedBody: false }),
{ foo: 'bar', boo: 1 },
{
'io-ts-codec-encode-error': 'false',
'io-ts-unknown-properties': 'boo',
}
);
assertRequestContains(getRequest(codec, { foo: 'bar', boo: 1 } as any, { useEncodedBody: false }), {
foo: 'bar',
boo: 1,
});
});
});

0 comments on commit 070d057

Please sign in to comment.