diff --git a/modules/sdk-core/src/bitgo/utils/postWithCodec.ts b/modules/sdk-core/src/bitgo/utils/postWithCodec.ts index fb418ee19a..7a7294c7e2 100644 --- a/modules/sdk-core/src/bitgo/utils/postWithCodec.ts +++ b/modules/sdk-core/src/bitgo/utils/postWithCodec.ts @@ -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, encodedBody: Record): 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, O extends Record>( - codec: t.Type, - 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. @@ -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); } diff --git a/modules/sdk-core/test/unit/bitgo/utils/postWithCodec.ts b/modules/sdk-core/test/unit/bitgo/utils/postWithCodec.ts index a1f29de2bc..5995b5f9c9 100644 --- a/modules/sdk-core/test/unit/bitgo/utils/postWithCodec.ts +++ b/modules/sdk-core/test/unit/bitgo/utils/postWithCodec.ts @@ -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, + }); }); });