Skip to content

Commit 6329dea

Browse files
authored
Add HttpMethod to "ProxyIntegrationRoute" (spring-media#64)
This typing makes it easy to check the right options to use for "method".
1 parent 66f8728 commit 6329dea

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

lib/EventProcessor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ export interface EventProcessor<TConfig = any, TEvent = any, TContext = any, TOu
44

55
export type ProcessMethod<TConfig, TEvent, TContext, TOutput = any> =
66
(config: TConfig, event: TEvent, context: TContext) => null | TOutput | Promise<TOutput>
7+
8+
export type HttpMethod =
9+
| 'GET' | 'HEAD' | 'POST' | 'PUT' |'DELETE'
10+
| 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';

lib/proxyIntegration.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
22

3-
import { ProcessMethod } from './EventProcessor'
3+
import { HttpMethod, ProcessMethod } from './EventProcessor'
44
import { addCorsHeaders, CorsOptions } from './cors';
55

66
type ProxyIntegrationParams = {
77
paths?: { [paramId: string]: string }
88
routePath?: string
99
}
10+
1011
type ProxyIntegrationBody<T = unknown> = {
1112
body: T
1213
}
14+
1315
type ErrorHandler = (error?: Error, request?: APIGatewayProxyEvent, context?: APIGatewayEventRequestContext) => Promise<APIGatewayProxyResult | void> | APIGatewayProxyResult | void
1416
export type ProxyIntegrationEvent<T = unknown> = Omit<APIGatewayProxyEvent, 'body'> & ProxyIntegrationParams & ProxyIntegrationBody<T>
1517
export type ProxyIntegrationResult = Omit<APIGatewayProxyResult, 'statusCode'> & { statusCode?: APIGatewayProxyResult['statusCode'] }
1618

1719
export interface ProxyIntegrationRoute {
1820
path: string
19-
method: string
21+
method: HttpMethod
2022
action: (
2123
request: ProxyIntegrationEvent<unknown>,
2224
context: APIGatewayEventRequestContext
@@ -94,7 +96,7 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
9496
}
9597

9698
const headers: APIGatewayProxyResult['headers'] = proxyIntegrationConfig.cors ? addCorsHeaders(proxyIntegrationConfig.cors, event) : {};
97-
99+
98100
if (event.httpMethod === 'OPTIONS') {
99101
Object.assign(headers, proxyIntegrationConfig.defaultHeaders)
100102
return Promise.resolve({
@@ -105,7 +107,7 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
105107
}
106108

107109
Object.assign(headers, { 'Content-Type': 'application/json' }, proxyIntegrationConfig.defaultHeaders)
108-
110+
109111
// assure necessary values have sane defaults:
110112
const errorMapping = proxyIntegrationConfig.errorMapping || {}
111113
errorMapping['NO_MATCHING_ACTION'] = 404
@@ -121,7 +123,8 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
121123
}
122124

123125
try {
124-
const actionConfig = findMatchingActionConfig(event.httpMethod, event.path, proxyIntegrationConfig) || {
126+
const httpMethod = event.httpMethod as HttpMethod;
127+
const actionConfig = findMatchingActionConfig(httpMethod, event.path, proxyIntegrationConfig) || {
125128
action: NO_MATCHING_ACTION,
126129
routePath: undefined,
127130
paths: undefined
@@ -163,7 +166,7 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
163166
if (result != undefined) {
164167
return result
165168
}
166-
169+
167170
return convertError(error, errorMapping, headers)
168171
})
169172
}
@@ -220,7 +223,7 @@ const convertError = (error: ProxyIntegrationError | Error, errorMapping?: Proxy
220223
}
221224
}
222225

223-
const findMatchingActionConfig = (httpMethod: string, httpPath: string, routeConfig: ProxyIntegrationConfig):
226+
const findMatchingActionConfig = (httpMethod: HttpMethod, httpPath: string, routeConfig: ProxyIntegrationConfig):
224227
ProxyIntegrationRoute & ProxyIntegrationParams | null => {
225228

226229
const paths: ProxyIntegrationParams['paths'] = {}

test/proxyIntegration.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { ProxyIntegrationConfig, process as proxyIntegration } from '../lib/proxyIntegration'
33

44
import { APIGatewayProxyEvent } from 'aws-lambda'
5+
import { HttpMethod } from '../lib/EventProcessor'
56

67
function forEach(arrayOfArrays: any) {
78
return {
@@ -266,7 +267,7 @@ describe('proxyIntegration.routeHandler', () => {
266267
['GET', '/abc/def'],
267268
['POST', '/abc'],
268269
['PUT', '/abc/def/ghi']
269-
]).it('should call action for on method/staticPath', async (method: string, path: string) => {
270+
]).it('should call action for on method/staticPath', async (method: HttpMethod, path: string) => {
270271
const routeConfig: ProxyIntegrationConfig = {
271272
routes: [
272273
{ method, path, action: () => ({ foo: 'bar' }) as any }
@@ -353,7 +354,7 @@ describe('proxyIntegration.routeHandler', () => {
353354
})
354355

355356
it('should return error headers', async () => {
356-
const routeConfig = {
357+
const routeConfig: ProxyIntegrationConfig = {
357358
routes: [
358359
{
359360
method: 'GET',
@@ -374,7 +375,7 @@ describe('proxyIntegration.routeHandler', () => {
374375
})
375376

376377
it('should return error including CORS header', async () => {
377-
const routeConfig = {
378+
const routeConfig: ProxyIntegrationConfig = {
378379
cors: true,
379380
routes: [
380381
{
@@ -399,7 +400,7 @@ describe('proxyIntegration.routeHandler', () => {
399400
})
400401
it('should modify incorrect error', async () => {
401402
const incorrectError = { body: { reason: 'oops' } }
402-
const routeConfig = {
403+
const routeConfig: ProxyIntegrationConfig = {
403404
routes: [
404405
{
405406
method: 'GET',
@@ -423,7 +424,7 @@ describe('proxyIntegration.routeHandler', () => {
423424

424425
it('should pass through error statuscode', async () => {
425426
const statusCodeError = { statusCode: 666, message: { reason: 'oops' } }
426-
const routeConfig = {
427+
const routeConfig: ProxyIntegrationConfig = {
427428
routes: [
428429
{
429430
method: 'GET',
@@ -547,7 +548,7 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
547548
body: JSON.stringify({ foo: 'bar' })
548549
}
549550

550-
const routeConfig = {
551+
const routeConfig: ProxyIntegrationConfig = {
551552
routes: [
552553
{ method: 'GET', path: '/', action: () => Promise.resolve(customBody) }
553554
]
@@ -578,7 +579,7 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
578579
[1234, '1234'],
579580
[undefined, '{}']
580581
]).it('should return async result', async (returnValue, expectedBody) => {
581-
const routeConfig = {
582+
const routeConfig: ProxyIntegrationConfig = {
582583
routes: [
583584
{ method: 'GET', path: '/', action: () => Promise.resolve(returnValue) }
584585
]
@@ -595,9 +596,9 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
595596
})
596597

597598
it('should return async error', async () => {
598-
const routeConfig = {
599+
const routeConfig: ProxyIntegrationConfig = {
599600
routes: [
600-
{ method: 'GET', path: '/', action: () => Promise.reject({ reason: 'myError', message: 'doof' }) }
601+
{ method: 'GET', path: '/', action: () => Promise.reject({ reason: 'myError', message: 'doof' }) as any }
601602
],
602603
errorMapping: { 'myError': 599 }
603604
}

0 commit comments

Comments
 (0)