@@ -3,12 +3,14 @@ import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
33import { beforeEach , describe , expect , it , vi } from 'vitest' ;
44import { BaseRouter } from '../../../src/rest/BaseRouter.js' ;
55import { HttpErrorCodes , HttpVerbs } from '../../../src/rest/constants.js' ;
6+ import { proxyEventToWebRequest } from '../../../src/rest/converters.js' ;
67import {
78 BadRequestError ,
89 InternalServerError ,
910 MethodNotAllowedError ,
1011 NotFoundError ,
1112} from '../../../src/rest/errors.js' ;
13+ import { isAPIGatewayProxyEvent } from '../../../src/rest/utils.js' ;
1214import type {
1315 HttpMethod ,
1416 Path ,
@@ -43,39 +45,19 @@ describe('Class: BaseRouter', () => {
4345 this . logger . error ( 'test error' ) ;
4446 }
4547
46- #isEvent( obj : unknown ) : asserts obj is APIGatewayProxyEvent {
47- if (
48- typeof obj !== 'object' ||
49- obj === null ||
50- ! ( 'path' in obj ) ||
51- ! ( 'httpMethod' in obj ) ||
52- typeof ( obj as any ) . path !== 'string' ||
53- ! ( obj as any ) . path . startsWith ( '/' ) ||
54- typeof ( obj as any ) . httpMethod !== 'string' ||
55- ! Object . values ( HttpVerbs ) . includes (
56- ( obj as any ) . httpMethod as HttpMethod
57- )
58- ) {
59- throw new Error ( 'Invalid event object' ) ;
60- }
61- }
62-
6348 public async resolve (
6449 event : unknown ,
6550 context : Context ,
6651 options ?: any
6752 ) : Promise < unknown > {
68- this . #isEvent( event ) ;
53+ if ( ! isAPIGatewayProxyEvent ( event ) )
54+ throw new Error ( 'not an API Gateway event!' ) ;
6955 const { httpMethod : method , path } = event ;
7056 const route = this . routeRegistry . resolve (
7157 method as HttpMethod ,
7258 path as Path
7359 ) ;
74- const request = new Request ( `http://localhost${ path } ` , {
75- method,
76- headers : event . headers as Record < string , string > ,
77- body : event . body ,
78- } ) ;
60+ const request = proxyEventToWebRequest ( event ) ;
7961 try {
8062 if ( route == null )
8163 throw new NotFoundError ( `Route ${ method } ${ path } not found` ) ;
@@ -838,9 +820,9 @@ describe('Class: BaseRouter', () => {
838820 statusCode : HttpErrorCodes . BAD_REQUEST ,
839821 error : 'Bad Request' ,
840822 message : error . message ,
841- hasRequest : options . request instanceof Request ,
842- hasEvent : options . event === testEvent ,
843- hasContext : options . context === context ,
823+ hasRequest : options ? .request instanceof Request ,
824+ hasEvent : options ? .event === testEvent ,
825+ hasContext : options ? .context === context ,
844826 } ) ) ;
845827
846828 app . get ( '/test' , ( ) => {
0 commit comments