Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/io-ts-http/src/combinators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe } from 'fp-ts/pipeable';
import { pipe } from 'fp-ts/function';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: This line is actually a breaking change! @ad-world, can you please open a PR to import from pipeable instead?

The issue is the io-ts-http package manifest advertises compatibility with fp-ts version 2.0.0 or higher1, while pipe was only added to the function module in fp-ts v2.6.32. It is important to import from pipeable (even though marked as deprecated) because that import does exist from v2.0.03. This guarantees even when consumers are using an fp-ts version between 2.0.0 and 2.6.3, the pipe import can still be found.

Footnotes

  1. https://github.com/BitGo/api-ts/blob/a7cd268553e3b068996596f4e09600fa913f700f/packages/io-ts-http/package.json#L23

  2. https://gcanti.github.io/fp-ts/modules/function.ts.html#pipe

  3. https://gcanti.github.io/fp-ts/modules/pipeable.ts.html#pipe

import * as E from 'fp-ts/Either';
import * as R from 'fp-ts/Record';
import * as t from 'io-ts';
Expand Down Expand Up @@ -101,12 +101,12 @@ export const flattened = <Props extends NestedProps>(
const innerProps = props[key];
flatProps = { ...flatProps, ...innerProps };
}
const flatCodec = t.exact(optionalized(flatProps));
const flatCodec = t.exact(optionalized(flatProps), name);

const nestedProps = R.map((innerProps: t.Props) => t.exact(optionalized(innerProps)))(
props,
);
const nestedCodec = t.strict(nestedProps);
const nestedCodec = t.strict(nestedProps, name);

return new t.Type(
name,
Expand Down
4 changes: 2 additions & 2 deletions packages/io-ts-http/src/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type EmitPropsErrors<P extends HttpRequestCombinatorProps> = {

export function httpRequest<
Props extends HttpRequestCombinatorProps & EmitPropsErrors<Props>,
>(props: Props) {
return flattened('httpRequest', {
>(props: Props, name?: string) {
return flattened(name ?? 'httpRequest', {
query: {},
params: {},
...(props as Omit<Props, 'query' | 'params'>),
Expand Down
39 changes: 37 additions & 2 deletions packages/io-ts-http/test/httpRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, it } from 'node:test';
import { strict as assert } from 'node:assert';

import * as PathReporter from 'io-ts/lib/PathReporter';
import * as NEA from 'fp-ts/NonEmptyArray';
import * as t from 'io-ts';
import { nonEmptyArray, JsonFromString, NumberFromString } from 'io-ts-types';
import { assertRight } from './utils';

import { assertLeft, assertRight } from './utils';

import { optional } from '../src/combinators';
import * as h from '../src/httpRequest';
Expand Down Expand Up @@ -138,4 +139,38 @@ describe('httpRequest', () => {
// tslint:disable-next-line: no-unused-expression
void _codec;
});

it('Displays error with codec name on decode', () => {
const request = h.httpRequest(
{
params: {},
query: {
foo: t.string,
},
body: {
bar: t.number,
},
},
'TestRequestWithCodecName',
);

const test = {
params: {},
query: {
foo: 'hello',
},
body: {
bar: 'world',
},
};

const errors = assertLeft(request.decode(test));
const validationErrors = PathReporter.failure(errors);
const validationMessage = validationErrors.join('\n');

assert(
validationMessage.includes('TestRequestWithCodecName'),
'Expected error to include codec name',
);
});
});
5 changes: 5 additions & 0 deletions packages/io-ts-http/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const assertRight = E.getOrElseW(() => {
throw new Error('Failed to decode object');
});

export const assertLeft = <T>(e: E.Either<t.Errors, T>) => {
assert(E.isLeft(e), 'Expected a failure, got a success');
return e.left;
};

export const assertEncodes = (codec: t.Mixed, test: unknown, expected = test) => {
const encoded = codec.encode(test);
assert.deepEqual(encoded, expected);
Expand Down