Skip to content

Commit a7cd268

Browse files
authored
Merge pull request #863 from BitGo/DX-634
feat: add optional name param to httpRequest
2 parents ae9d1db + 84f31c5 commit a7cd268

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

packages/io-ts-http/src/combinators.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pipe } from 'fp-ts/pipeable';
1+
import { pipe } from 'fp-ts/function';
22
import * as E from 'fp-ts/Either';
33
import * as R from 'fp-ts/Record';
44
import * as t from 'io-ts';
@@ -101,12 +101,12 @@ export const flattened = <Props extends NestedProps>(
101101
const innerProps = props[key];
102102
flatProps = { ...flatProps, ...innerProps };
103103
}
104-
const flatCodec = t.exact(optionalized(flatProps));
104+
const flatCodec = t.exact(optionalized(flatProps), name);
105105

106106
const nestedProps = R.map((innerProps: t.Props) => t.exact(optionalized(innerProps)))(
107107
props,
108108
);
109-
const nestedCodec = t.strict(nestedProps);
109+
const nestedCodec = t.strict(nestedProps, name);
110110

111111
return new t.Type(
112112
name,

packages/io-ts-http/src/httpRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ type EmitPropsErrors<P extends HttpRequestCombinatorProps> = {
5252

5353
export function httpRequest<
5454
Props extends HttpRequestCombinatorProps & EmitPropsErrors<Props>,
55-
>(props: Props) {
56-
return flattened('httpRequest', {
55+
>(props: Props, name?: string) {
56+
return flattened(name ?? 'httpRequest', {
5757
query: {},
5858
params: {},
5959
...(props as Omit<Props, 'query' | 'params'>),

packages/io-ts-http/test/httpRequest.test.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, it } from 'node:test';
22
import { strict as assert } from 'node:assert';
3-
3+
import * as PathReporter from 'io-ts/lib/PathReporter';
44
import * as NEA from 'fp-ts/NonEmptyArray';
55
import * as t from 'io-ts';
66
import { nonEmptyArray, JsonFromString, NumberFromString } from 'io-ts-types';
7-
import { assertRight } from './utils';
7+
8+
import { assertLeft, assertRight } from './utils';
89

910
import { optional } from '../src/combinators';
1011
import * as h from '../src/httpRequest';
@@ -138,4 +139,38 @@ describe('httpRequest', () => {
138139
// tslint:disable-next-line: no-unused-expression
139140
void _codec;
140141
});
142+
143+
it('Displays error with codec name on decode', () => {
144+
const request = h.httpRequest(
145+
{
146+
params: {},
147+
query: {
148+
foo: t.string,
149+
},
150+
body: {
151+
bar: t.number,
152+
},
153+
},
154+
'TestRequestWithCodecName',
155+
);
156+
157+
const test = {
158+
params: {},
159+
query: {
160+
foo: 'hello',
161+
},
162+
body: {
163+
bar: 'world',
164+
},
165+
};
166+
167+
const errors = assertLeft(request.decode(test));
168+
const validationErrors = PathReporter.failure(errors);
169+
const validationMessage = validationErrors.join('\n');
170+
171+
assert(
172+
validationMessage.includes('TestRequestWithCodecName'),
173+
'Expected error to include codec name',
174+
);
175+
});
141176
});

packages/io-ts-http/test/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export const assertRight = E.getOrElseW(() => {
77
throw new Error('Failed to decode object');
88
});
99

10+
export const assertLeft = <T>(e: E.Either<t.Errors, T>) => {
11+
assert(E.isLeft(e), 'Expected a failure, got a success');
12+
return e.left;
13+
};
14+
1015
export const assertEncodes = (codec: t.Mixed, test: unknown, expected = test) => {
1116
const encoded = codec.encode(test);
1217
assert.deepEqual(encoded, expected);

0 commit comments

Comments
 (0)