11import * as t from 'io-ts' ;
2- import { apiSpec , httpRoute , httpRequest , HttpResponse } from '@api-ts/io-ts-http' ;
3- import { createRouter , type WrappedRouter } from '@api-ts/typed-express-router' ;
2+ import {
3+ apiSpec ,
4+ httpRoute ,
5+ httpRequest ,
6+ HttpResponse ,
7+ Method as HttpMethod ,
8+ } from '@api-ts/io-ts-http' ;
9+ import {
10+ createRouter ,
11+ TypedRequestHandler ,
12+ type WrappedRouter ,
13+ } from '@api-ts/typed-express-router' ;
414import { Response } from '@api-ts/response' ;
5- import express , { Request } from 'express' ;
15+ import express from 'express' ;
616import { BitGo } from 'bitgo' ;
7- import { BitGoRequest , isBitGoRequest } from '../../types/request' ;
17+ import { BitGoRequest } from '../../types/request' ;
818import { MasterExpressConfig } from '../../config' ;
919import { handleGenerateWalletOnPrem } from '../generateWallet' ;
1020import { withResponseHandler } from '../../shared/responseHandler' ;
@@ -86,6 +96,20 @@ export const MasterApiSpec = apiSpec({
8696 } ,
8797} ) ;
8898
99+ export type MasterApiSpec = typeof MasterApiSpec ;
100+
101+ export type MasterApiSpecRouteHandler <
102+ ApiName extends keyof MasterApiSpec ,
103+ Method extends keyof MasterApiSpec [ ApiName ] & HttpMethod ,
104+ > = TypedRequestHandler < MasterApiSpec , ApiName , Method > ;
105+
106+ export type MasterApiSpecRouteRequest <
107+ ApiName extends keyof MasterApiSpec ,
108+ Method extends keyof MasterApiSpec [ ApiName ] & HttpMethod ,
109+ > = BitGoRequest & Parameters < MasterApiSpecRouteHandler < ApiName , Method > > [ 0 ] ;
110+
111+ export type GenericMasterApiSpecRouteRequest = MasterApiSpecRouteRequest < any , any > ;
112+
89113// Create router with handlers
90114export function createMasterApiRouter (
91115 cfg : MasterExpressConfig ,
@@ -98,10 +122,7 @@ export function createMasterApiRouter(
98122
99123 // Generate wallet endpoint handler
100124 router . post ( 'v1.wallet.generate' , [
101- withResponseHandler ( async ( req : BitGoRequest | Request ) => {
102- if ( ! isBitGoRequest ( req ) ) {
103- throw new Error ( 'Invalid request type' ) ;
104- }
125+ withResponseHandler ( async ( req : GenericMasterApiSpecRouteRequest ) => {
105126 const result = await handleGenerateWalletOnPrem ( req ) ;
106127 return Response . ok ( result ) ;
107128 } ) ,
0 commit comments