@@ -59,18 +59,25 @@ import { handleUpdateLightningWalletCoinSpecific } from './lightning/lightningWa
5959import { ProxyAgent } from 'proxy-agent' ;
6060import { isLightningCoinName } from '@bitgo/abstract-lightning' ;
6161import { handleLightningWithdraw } from './lightning/lightningWithdrawRoutes' ;
62+ import createExpressRouter from './typedRoutes' ;
63+ import { ExpressApiRouteRequest } from './typedRoutes/api' ;
64+ import { TypedRequestHandler , WrappedRequest , WrappedResponse } from '@api-ts/typed-express-router' ;
6265
6366const { version } = require ( 'bitgo/package.json' ) ;
6467const pjson = require ( '../package.json' ) ;
6568const debug = debugLib ( 'bitgo:express' ) ;
6669
6770const BITGOEXPRESS_USER_AGENT = `BitGoExpress/${ pjson . version } BitGoJS/${ version } ` ;
6871
69- function handlePing ( req : express . Request , res : express . Response , next : express . NextFunction ) {
72+ function handlePing (
73+ req : ExpressApiRouteRequest < 'express.ping' , 'get' > ,
74+ res : express . Response ,
75+ next : express . NextFunction
76+ ) {
7077 return req . bitgo . ping ( ) ;
7178}
7279
73- function handlePingExpress ( req : express . Request ) {
80+ function handlePingExpress ( req : ExpressApiRouteRequest < ' express.pingExpress' , 'get' > ) {
7481 return {
7582 status : 'express server is ok!' ,
7683 } ;
@@ -1358,6 +1365,23 @@ export function promiseWrapper(promiseRequestHandler: RequestHandler) {
13581365 } ;
13591366}
13601367
1368+ export function typedPromiseWrapper ( promiseRequestHandler : TypedRequestHandler ) {
1369+ return async function ( req : WrappedRequest , res : WrappedResponse , next : express . NextFunction ) {
1370+ debug ( `handle: ${ req . method } ${ req . originalUrl } ` ) ;
1371+ try {
1372+ const result = await promiseRequestHandler ( req , res , next ) ;
1373+ if ( typeof result === 'object' && result !== null && 'body' in result && 'status' in result ) {
1374+ const { status, body } = result as { status : number ; body : unknown } ;
1375+ res . status ( status ) . send ( body ) ;
1376+ } else {
1377+ res . status ( 200 ) . send ( result ) ;
1378+ }
1379+ } catch ( e ) {
1380+ handleRequestHandlerError ( res , e ) ;
1381+ }
1382+ } ;
1383+ }
1384+
13611385export function createCustomSigningFunction ( externalSignerUrl : string ) : CustomSigningFunction {
13621386 return async function ( params ) : Promise < SignedTransaction > {
13631387 const { body : signedTx } = await retryPromise (
@@ -1530,8 +1554,11 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
15301554 // ping
15311555 // /api/v[12]/pingexpress is the only exception to the rule above, as it explicitly checks the health of the
15321556 // express server without running into rate limiting with the BitGo server.
1533- app . get ( '/api/v[12]/ping' , prepareBitGo ( config ) , promiseWrapper ( handlePing ) ) ;
1534- app . get ( '/api/v[12]/pingexpress' , promiseWrapper ( handlePingExpress ) ) ;
1557+ const router = createExpressRouter ( ) ;
1558+ app . use ( router ) ;
1559+
1560+ router . get ( 'express.ping' , [ prepareBitGo ( config ) , typedPromiseWrapper ( handlePing ) ] ) ;
1561+ router . get ( 'express.pingExpress' , [ typedPromiseWrapper ( handlePingExpress ) ] ) ;
15351562
15361563 // auth
15371564 app . post ( '/api/v[12]/user/login' , parseBody , prepareBitGo ( config ) , promiseWrapper ( handleLogin ) ) ;
0 commit comments