Skip to content

Commit 7374276

Browse files
authored
Merge pull request BitGo#25 from bitgopatmcl/BG-45933-http-status-codes
feat: use HTTP status codes in superagent-codec-adapter
2 parents f05fe43 + 5816f2d commit 7374276

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

packages/io-ts-express/test/test-server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ test('should offer a delightful developer experience', async (t) => {
106106
// application layer :thonking:
107107
const response = await apiClient['hello.world']
108108
.put({ secretCode: 1000 })
109-
.decodeExpecting('ok')
109+
.decodeExpecting(200)
110110
.then((res) => res.body);
111111

112112
t.like(response, { message: "Who's there?" });
@@ -130,7 +130,7 @@ test('should invoke app-level middleware', async (t) => {
130130

131131
const response = await apiClient['hello.world']
132132
.put({ secretCode: 1000 })
133-
.decodeExpecting('ok')
133+
.decodeExpecting(200)
134134
.then((res) => res.body);
135135

136136
t.like(response, { message: "Who's there?", appMiddlewareRan: true });
@@ -153,7 +153,7 @@ test('should invoke route-level middleware', async (t) => {
153153

154154
const response = await apiClient['hello.world']
155155
.put({ secretCode: 1000 })
156-
.decodeExpecting('ok')
156+
.decodeExpecting(200)
157157
.then((res) => res.body);
158158

159159
t.like(response, { message: "Who's there?", routeMiddlewareRan: true });
@@ -176,7 +176,7 @@ test('should infer status code from response type', async (t) => {
176176

177177
const response = await apiClient['hello.world']
178178
.put({ secretCode: 0 })
179-
.decodeExpecting('invalidRequest')
179+
.decodeExpecting(400)
180180
.then((res) => res.body);
181181

182182
t.like(response, { errors: 'Please do not tell me zero! I will now explode' });

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ export const HttpResponseCodes: { [K in Status]: number } = {
1818
internalError: 500,
1919
serviceUnavailable: 503,
2020
};
21+
22+
export type KnownHttpStatusCodes<Response extends HttpResponse> =
23+
typeof HttpResponseCodes[KnownResponses<Response>];

packages/superagent-codec-adapter/src/request.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import type { Response, SuperAgent, SuperAgentRequest } from 'superagent';
55
import type { SuperTest } from 'supertest';
66
import { URL } from 'url';
77
import { pipe } from 'fp-ts/function';
8-
import { HttpResponseCodes } from '@bitgo/io-ts-http';
98

109
type SuccessfulResponses<Route extends h.HttpRoute> = {
11-
[Status in h.KnownResponses<Route['response']>]: {
10+
[Status in h.KnownHttpStatusCodes<Route['response']>]: {
1211
status: Status;
1312
error?: undefined;
1413
body: t.TypeOf<Route['response'][Status]>;
1514
original: Response;
1615
};
17-
}[h.KnownResponses<Route['response']>];
16+
}[h.KnownHttpStatusCodes<Route['response']>];
1817

1918
type DecodedResponse<Route extends h.HttpRoute> =
2019
| SuccessfulResponses<Route>
@@ -29,15 +28,15 @@ const decodedResponse = <Route extends h.HttpRoute>(res: DecodedResponse<Route>)
2928

3029
type ExpectedDecodedResponse<
3130
Route extends h.HttpRoute,
32-
Status extends h.KnownResponses<Route['response']>,
31+
Status extends h.KnownHttpStatusCodes<Route['response']>,
3332
> = {
3433
body: t.TypeOf<Route['response'][Status]>;
3534
original: Response;
3635
};
3736

3837
type PatchedRequest<Req extends SuperAgentRequest, Route extends h.HttpRoute> = Req & {
3938
decode: () => Promise<DecodedResponse<Route>>;
40-
decodeExpecting: <Status extends h.KnownResponses<Route['response']>>(
39+
decodeExpecting: <Status extends h.KnownHttpStatusCodes<Route['response']>>(
4140
status: Status,
4241
) => Promise<ExpectedDecodedResponse<Route, Status>>;
4342
};
@@ -97,7 +96,7 @@ const patchRequest = <Req extends SuperAgentRequest, Route extends h.HttpRoute>(
9796

9897
let status: string | undefined;
9998
// DISCUSS: Should we have this as a preprocessed const in io-ts-http?
100-
for (const [name, code] of Object.entries(HttpResponseCodes)) {
99+
for (const [name, code] of Object.entries(h.HttpResponseCodes)) {
101100
if (statusCode === code) {
102101
status = name;
103102
break;
@@ -125,7 +124,7 @@ const patchRequest = <Req extends SuperAgentRequest, Route extends h.HttpRoute>(
125124
route.response[status].decode(res.body),
126125
E.map((body) =>
127126
decodedResponse<Route>({
128-
status: status as h.KnownResponses<Route['response']>,
127+
status: statusCode,
129128
body,
130129
original: res,
131130
}),
@@ -142,7 +141,9 @@ const patchRequest = <Req extends SuperAgentRequest, Route extends h.HttpRoute>(
142141
);
143142
});
144143

145-
patchedReq.decodeExpecting = <Status extends h.KnownResponses<Route['response']>>(
144+
patchedReq.decodeExpecting = <
145+
Status extends h.KnownHttpStatusCodes<Route['response']>,
146+
>(
146147
status: Status,
147148
) =>
148149
patchedReq.decode().then((res) => {

packages/superagent-codec-adapter/test/request.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('request', () => {
105105
.post({ id: 1337, foo: 'test', bar: 42 })
106106
.decode();
107107

108-
assert.equal(response.status, 'ok');
108+
assert.equal(response.status, 200);
109109
assert.deepEqual(response.body, {
110110
id: 1337,
111111
foo: 'test',
@@ -152,7 +152,7 @@ describe('request', () => {
152152
it('narrows expected response types', async () => {
153153
const response = await apiClient['api.v1.test']
154154
.post({ id: 1337, foo: 'test', bar: 42 })
155-
.decodeExpecting('ok');
155+
.decodeExpecting(200);
156156

157157
assert.deepEqual(response.body, {
158158
id: 1337,
@@ -166,7 +166,7 @@ describe('request', () => {
166166
const result = await apiClient['api.v1.test']
167167
.post({ id: 1337, foo: 'test', bar: 42 })
168168
.set('x-send-unexpected-status-code', 'true')
169-
.decodeExpecting('ok')
169+
.decodeExpecting(200)
170170
.then(() => false)
171171
.catch(() => true);
172172

@@ -177,7 +177,7 @@ describe('request', () => {
177177
const result = await apiClient['api.v1.test']
178178
.post({ id: 1337, foo: 'test', bar: 42 })
179179
.set('x-send-invalid-response-body', 'true')
180-
.decodeExpecting('ok')
180+
.decodeExpecting(200)
181181
.then(() => false)
182182
.catch(() => true);
183183

0 commit comments

Comments
 (0)