Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
Add HttpMethod to "ProxyIntegrationRoute" (#64)
Browse files Browse the repository at this point in the history
This typing makes it easy to check the right options to use for "method".
  • Loading branch information
JHPG authored Apr 8, 2021
1 parent 66f8728 commit 6329dea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/EventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export interface EventProcessor<TConfig = any, TEvent = any, TContext = any, TOu

export type ProcessMethod<TConfig, TEvent, TContext, TOutput = any> =
(config: TConfig, event: TEvent, context: TContext) => null | TOutput | Promise<TOutput>

export type HttpMethod =
| 'GET' | 'HEAD' | 'POST' | 'PUT' |'DELETE'
| 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
17 changes: 10 additions & 7 deletions lib/proxyIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'

import { ProcessMethod } from './EventProcessor'
import { HttpMethod, ProcessMethod } from './EventProcessor'
import { addCorsHeaders, CorsOptions } from './cors';

type ProxyIntegrationParams = {
paths?: { [paramId: string]: string }
routePath?: string
}

type ProxyIntegrationBody<T = unknown> = {
body: T
}

type ErrorHandler = (error?: Error, request?: APIGatewayProxyEvent, context?: APIGatewayEventRequestContext) => Promise<APIGatewayProxyResult | void> | APIGatewayProxyResult | void
export type ProxyIntegrationEvent<T = unknown> = Omit<APIGatewayProxyEvent, 'body'> & ProxyIntegrationParams & ProxyIntegrationBody<T>
export type ProxyIntegrationResult = Omit<APIGatewayProxyResult, 'statusCode'> & { statusCode?: APIGatewayProxyResult['statusCode'] }

export interface ProxyIntegrationRoute {
path: string
method: string
method: HttpMethod
action: (
request: ProxyIntegrationEvent<unknown>,
context: APIGatewayEventRequestContext
Expand Down Expand Up @@ -94,7 +96,7 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
}

const headers: APIGatewayProxyResult['headers'] = proxyIntegrationConfig.cors ? addCorsHeaders(proxyIntegrationConfig.cors, event) : {};

if (event.httpMethod === 'OPTIONS') {
Object.assign(headers, proxyIntegrationConfig.defaultHeaders)
return Promise.resolve({
Expand All @@ -105,7 +107,7 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
}

Object.assign(headers, { 'Content-Type': 'application/json' }, proxyIntegrationConfig.defaultHeaders)

// assure necessary values have sane defaults:
const errorMapping = proxyIntegrationConfig.errorMapping || {}
errorMapping['NO_MATCHING_ACTION'] = 404
Expand All @@ -121,7 +123,8 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
}

try {
const actionConfig = findMatchingActionConfig(event.httpMethod, event.path, proxyIntegrationConfig) || {
const httpMethod = event.httpMethod as HttpMethod;
const actionConfig = findMatchingActionConfig(httpMethod, event.path, proxyIntegrationConfig) || {
action: NO_MATCHING_ACTION,
routePath: undefined,
paths: undefined
Expand Down Expand Up @@ -163,7 +166,7 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
if (result != undefined) {
return result
}

return convertError(error, errorMapping, headers)
})
}
Expand Down Expand Up @@ -220,7 +223,7 @@ const convertError = (error: ProxyIntegrationError | Error, errorMapping?: Proxy
}
}

const findMatchingActionConfig = (httpMethod: string, httpPath: string, routeConfig: ProxyIntegrationConfig):
const findMatchingActionConfig = (httpMethod: HttpMethod, httpPath: string, routeConfig: ProxyIntegrationConfig):
ProxyIntegrationRoute & ProxyIntegrationParams | null => {

const paths: ProxyIntegrationParams['paths'] = {}
Expand Down
19 changes: 10 additions & 9 deletions test/proxyIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ProxyIntegrationConfig, process as proxyIntegration } from '../lib/proxyIntegration'

import { APIGatewayProxyEvent } from 'aws-lambda'
import { HttpMethod } from '../lib/EventProcessor'

function forEach(arrayOfArrays: any) {
return {
Expand Down Expand Up @@ -266,7 +267,7 @@ describe('proxyIntegration.routeHandler', () => {
['GET', '/abc/def'],
['POST', '/abc'],
['PUT', '/abc/def/ghi']
]).it('should call action for on method/staticPath', async (method: string, path: string) => {
]).it('should call action for on method/staticPath', async (method: HttpMethod, path: string) => {
const routeConfig: ProxyIntegrationConfig = {
routes: [
{ method, path, action: () => ({ foo: 'bar' }) as any }
Expand Down Expand Up @@ -353,7 +354,7 @@ describe('proxyIntegration.routeHandler', () => {
})

it('should return error headers', async () => {
const routeConfig = {
const routeConfig: ProxyIntegrationConfig = {
routes: [
{
method: 'GET',
Expand All @@ -374,7 +375,7 @@ describe('proxyIntegration.routeHandler', () => {
})

it('should return error including CORS header', async () => {
const routeConfig = {
const routeConfig: ProxyIntegrationConfig = {
cors: true,
routes: [
{
Expand All @@ -399,7 +400,7 @@ describe('proxyIntegration.routeHandler', () => {
})
it('should modify incorrect error', async () => {
const incorrectError = { body: { reason: 'oops' } }
const routeConfig = {
const routeConfig: ProxyIntegrationConfig = {
routes: [
{
method: 'GET',
Expand All @@ -423,7 +424,7 @@ describe('proxyIntegration.routeHandler', () => {

it('should pass through error statuscode', async () => {
const statusCodeError = { statusCode: 666, message: { reason: 'oops' } }
const routeConfig = {
const routeConfig: ProxyIntegrationConfig = {
routes: [
{
method: 'GET',
Expand Down Expand Up @@ -547,7 +548,7 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
body: JSON.stringify({ foo: 'bar' })
}

const routeConfig = {
const routeConfig: ProxyIntegrationConfig = {
routes: [
{ method: 'GET', path: '/', action: () => Promise.resolve(customBody) }
]
Expand Down Expand Up @@ -578,7 +579,7 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
[1234, '1234'],
[undefined, '{}']
]).it('should return async result', async (returnValue, expectedBody) => {
const routeConfig = {
const routeConfig: ProxyIntegrationConfig = {
routes: [
{ method: 'GET', path: '/', action: () => Promise.resolve(returnValue) }
]
Expand All @@ -595,9 +596,9 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
})

it('should return async error', async () => {
const routeConfig = {
const routeConfig: ProxyIntegrationConfig = {
routes: [
{ method: 'GET', path: '/', action: () => Promise.reject({ reason: 'myError', message: 'doof' }) }
{ method: 'GET', path: '/', action: () => Promise.reject({ reason: 'myError', message: 'doof' }) as any }
],
errorMapping: { 'myError': 599 }
}
Expand Down

0 comments on commit 6329dea

Please sign in to comment.