Skip to content

feat: produce helpful error messages on httpRequest codec errors #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2022
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
42 changes: 37 additions & 5 deletions packages/io-ts-http/src/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as t from 'io-ts';
import { Json } from 'io-ts-types';
import { flattened, optional, optionalized } from './combinators';
import { OutputConstrainedProps } from './utils';

export const GenericHttpRequest = optionalized({
// DISCUSS: renaming this to something more specific, e.g. route, or path, or routeParams, or pathParams
Expand All @@ -18,13 +17,46 @@ export type HttpRequestCodec<T> = t.Type<
>;

export type HttpRequestCombinatorProps = {
params?: NonNullable<OutputConstrainedProps<string | undefined>>;
query?: NonNullable<OutputConstrainedProps<string | string[] | undefined>>;
headers?: NonNullable<OutputConstrainedProps<string | undefined>>;
params?: NonNullable<t.Props>;
query?: NonNullable<t.Props>;
headers?: NonNullable<t.Props>;
body?: NonNullable<t.Props>;
};

export function httpRequest<Props extends HttpRequestCombinatorProps>(props: Props) {
/**
* Attempts to produce a helpful error message when invalid codecs are passed to `httpRequest`
* It is a workaround until something like https://github.com/microsoft/TypeScript/pull/40468
* is merged.
*/
type EmitOutputTypeErrors<
P extends t.Props | undefined,
O,
OName extends string,
> = P extends undefined
? P
: {
[K in keyof P & string]: P[K] extends t.Type<any, O, any>
? P[K]
: `Codec's output type is not assignable to ${OName}. Try using one like \`NumberFromString\``;
};

type EmitPropsErrors<P extends HttpRequestCombinatorProps> = {
params?: EmitOutputTypeErrors<P['params'], string | undefined, 'string | undefined'>;
query?: EmitOutputTypeErrors<
P['query'],
string | string[] | undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

@bitgopatmcl is there already a type for ParamValue, QueryValue and HeaderValue? I was wondering why params and headers were a single string, before I realized it was actually the values' types. Using a type would make it more explicit, and hopefully the error output would remain readable.

'string | string[] | undefined'
>;
headers?: EmitOutputTypeErrors<
P['headers'],
string | undefined,
'string | undefined'
>;
};

export function httpRequest<
Props extends HttpRequestCombinatorProps & EmitPropsErrors<Props>,
>(props: Props) {
return flattened('httpRequest', {
query: {},
params: {},
Expand Down
4 changes: 0 additions & 4 deletions packages/io-ts-http/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export type OptionalizedC<Props extends t.Props> = t.IntersectionC<
[t.TypeC<RequiredProps<Props>>, t.PartialC<OptionalProps<Props>>]
>;

export type OutputConstrainedProps<O> = {
[K: string]: t.Type<any, O, unknown>;
};

export type NestedProps = {
[K: string]: t.Props;
};
Expand Down