Skip to content

Commit ca9b741

Browse files
feat(express): migrate decrypt to typed routes
2 parents 37d55a3 + dcddaf6 commit ca9b741

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

modules/express/src/clientRoutes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function handleLogin(req: ExpressApiRouteRequest<'express.login', 'post'>) {
9090
return req.bitgo.authenticate(body);
9191
}
9292

93-
function handleDecrypt(req: express.Request) {
93+
function handleDecrypt(req: ExpressApiRouteRequest<'express.decrypt', 'post'>) {
9494
return {
9595
decrypted: req.bitgo.decrypt(req.body),
9696
};
@@ -1563,7 +1563,7 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
15631563
// auth
15641564
router.post('express.login', [prepareBitGo(config), typedPromiseWrapper(handleLogin)]);
15651565

1566-
app.post('/api/v[12]/decrypt', parseBody, prepareBitGo(config), promiseWrapper(handleDecrypt));
1566+
router.post('express.decrypt', [prepareBitGo(config), typedPromiseWrapper(handleDecrypt)]);
15671567
app.post('/api/v[12]/encrypt', parseBody, prepareBitGo(config), promiseWrapper(handleEncrypt));
15681568
app.post('/api/v[12]/verifyaddress', parseBody, prepareBitGo(config), promiseWrapper(handleVerifyAddress));
15691569
app.post(
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as t from 'io-ts';
2+
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
3+
import { BitgoExpressError } from '../../schemas/error';
4+
5+
export const DecryptRequestBody = {
6+
input: t.string,
7+
password: optional(t.string),
8+
};
9+
10+
/**
11+
* Decrypt
12+
*
13+
* @operationId express.decrypt
14+
*/
15+
export const PostDecrypt = httpRoute({
16+
path: '/api/v[12]/decrypt',
17+
method: 'POST',
18+
request: httpRequest({
19+
body: DecryptRequestBody,
20+
}),
21+
response: {
22+
200: t.type({
23+
decrypted: t.string,
24+
}),
25+
404: BitgoExpressError,
26+
},
27+
});

modules/express/src/typedRoutes/api/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as express from 'express';
55
import { GetPing } from './common/ping';
66
import { GetPingExpress } from './common/pingExpress';
77
import { PostLogin } from './common/login';
8+
import { PostDecrypt } from './common/decrypt';
89

910
export const ExpressApi = apiSpec({
1011
'express.ping': {
@@ -16,6 +17,9 @@ export const ExpressApi = apiSpec({
1617
'express.login': {
1718
post: PostLogin,
1819
},
20+
'express.decrypt': {
21+
post: PostDecrypt,
22+
},
1923
});
2024

2125
export type ExpressApi = typeof ExpressApi;

modules/express/test/unit/typedRoutes/decode.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as assert from 'assert';
22
import * as t from 'io-ts';
3+
import { DecryptRequestBody } from '../../../src/typedRoutes/api/common/decrypt';
34
import { LoginRequest } from '../../../src/typedRoutes/api/common/login';
45

56
export function assertDecode<T>(codec: t.Type<T, unknown>, input: unknown): T {
@@ -25,4 +26,17 @@ describe('io-ts decode tests', function () {
2526
password: 'password',
2627
});
2728
});
29+
it('express.decrypt', function () {
30+
// input is required field
31+
assert.throws(() =>
32+
assertDecode(t.type(DecryptRequestBody), {
33+
password: 'hello',
34+
})
35+
);
36+
37+
assertDecode(t.type(DecryptRequestBody), {
38+
input: 'input',
39+
password: 'password',
40+
});
41+
});
2842
});

0 commit comments

Comments
 (0)