Skip to content

Commit 82d10af

Browse files
Merge pull request #186 from bitgopatmcl/express-response-type-experiment
fix: attempt to handle response types more cleanly
2 parents 039e72e + 7143029 commit 82d10af

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

packages/express-wrapper/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import { apiTsPathToExpress } from './path';
1111
import { decodeRequestAndEncodeResponse, RouteHandler } from './request';
1212
import { defaultResponseEncoder, ResponseEncoder } from './response';
1313

14-
export { middlewareFn } from './middleware';
15-
export type { ResponseEncoder, NumericOrKeyedResponseType } from './response';
16-
export { routeHandler } from './request';
14+
export { middlewareFn, MiddlewareChain, MiddlewareChainOutput } from './middleware';
15+
export type { ResponseEncoder, KeyedResponseType } from './response';
16+
export { routeHandler, ServiceFunction } from './request';
1717

1818
const isHttpVerb = (verb: string): verb is 'get' | 'put' | 'post' | 'delete' =>
1919
verb === 'get' || verb === 'put' || verb === 'post' || verb === 'delete';

packages/express-wrapper/src/request.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import express from 'express';
77
import * as E from 'fp-ts/Either';
88
import * as PathReporter from 'io-ts/lib/PathReporter';
99

10-
import { HttpRoute, RequestType } from '@api-ts/io-ts-http';
10+
import { HttpRoute, RequestType, ResponseType } from '@api-ts/io-ts-http';
1111

1212
import {
1313
runMiddlewareChain,
@@ -16,11 +16,11 @@ import {
1616
MiddlewareChain,
1717
MiddlewareChainOutput,
1818
} from './middleware';
19-
import type { NumericOrKeyedResponseType, ResponseEncoder } from './response';
19+
import type { KeyedResponseType, ResponseEncoder } from './response';
2020

21-
export type ServiceFunction<R extends HttpRoute, Input = RequestType<R>> = (
22-
input: Input,
23-
) => NumericOrKeyedResponseType<R> | Promise<NumericOrKeyedResponseType<R>>;
21+
export type ServiceFunction<R extends HttpRoute, Input = RequestType<R>> =
22+
| ((input: Input) => ResponseType<R> | Promise<ResponseType<R>>)
23+
| ((input: Input) => KeyedResponseType<R> | Promise<KeyedResponseType<R>>);
2424

2525
// The first two alternatives are here to maintain backwards compatibility
2626
export type RouteHandler<R extends HttpRoute> =
@@ -109,7 +109,10 @@ export const decodeRequestAndEncodeResponse = (
109109
return;
110110
}
111111

112-
let rawResponse: NumericOrKeyedResponseType<HttpRoute> | undefined;
112+
let rawResponse:
113+
| ResponseType<HttpRoute>
114+
| KeyedResponseType<HttpRoute>
115+
| undefined;
113116
try {
114117
const handlerParams =
115118
MiddlewareBrand in handler

packages/express-wrapper/src/response.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ import {
88
ResponseType,
99
} from '@api-ts/io-ts-http';
1010

11-
export type NumericOrKeyedResponseType<R extends HttpRoute> =
12-
| ResponseType<R>
13-
| {
14-
[Key in keyof R['response'] & keyof HttpToKeyStatus]: {
15-
type: HttpToKeyStatus[Key];
16-
payload: t.TypeOf<R['response'][Key]>;
17-
};
18-
}[keyof R['response'] & keyof HttpToKeyStatus];
11+
export type KeyedResponseType<R extends HttpRoute> = {
12+
[Key in keyof R['response'] & keyof HttpToKeyStatus]: {
13+
type: HttpToKeyStatus[Key];
14+
payload: t.TypeOf<R['response'][Key]>;
15+
};
16+
}[keyof R['response'] & keyof HttpToKeyStatus];
1917

2018
// TODO: Use HKT (using fp-ts or a similar workaround method, or who knows maybe they'll add
2119
// official support) to allow for polymorphic ResponseType<_>.
2220
export type ResponseEncoder = (
2321
route: HttpRoute,
24-
serviceFnResponse: NumericOrKeyedResponseType<HttpRoute>,
22+
serviceFnResponse: ResponseType<HttpRoute>,
2523
) => express.RequestHandler;
2624

2725
export const defaultResponseEncoder: ResponseEncoder =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as t from 'io-ts';
22

33
export type HttpResponse = {
4-
[K: number]: t.Mixed;
4+
[K: number | string]: t.Mixed;
55
};
66

77
export type ResponseTypeForStatus<

0 commit comments

Comments
 (0)