types/aws-serverless-express: extend http.IncomingMessage (#41420)

This commit is contained in:
hirochachacha 2020-01-18 01:45:35 +09:00 committed by Eli Barzilay
parent 699dd6b2e2
commit e6fb7b9391
3 changed files with 32 additions and 12 deletions

View File

@ -2,14 +2,7 @@ import * as awsServerlessExpress from 'aws-serverless-express';
import express = require('express');
import { eventContext } from 'aws-serverless-express/middleware';
const app = express();
app.use(eventContext());
const server = awsServerlessExpress.createServer(app, () => {}, []);
const mockEvent = {
key: 'value'
};
declare let mockEvent: AWSLambda.APIGatewayEvent;
const mockContext = {
callbackWaitsForEmptyEventLoop: true,
@ -26,6 +19,17 @@ const mockContext = {
succeed: (message: string) => false
};
const app = express();
app.use(eventContext());
app.get('/', (req, res) => {
if (req.apiGateway) {
req.apiGateway.event;
req.apiGateway.context;
}
});
const server = awsServerlessExpress.createServer(app, () => {}, []);
awsServerlessExpress.proxy(server, mockEvent, mockContext);
awsServerlessExpress.proxy(server, mockEvent, mockContext, 'CALLBACK', () => {});
awsServerlessExpress.proxy(server, mockEvent, mockContext, 'CONTEXT_SUCCEED');

View File

@ -5,7 +5,7 @@
// Matthias Meyer <https://github.com/mattmeye>
// Alberto Vasquez <https://github.com/albertovasquez>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// TypeScript Version: 3.5
/// <reference types="node"/>
import * as http from 'http';
@ -29,20 +29,20 @@ export function createServer(
export function proxy(
server: http.Server,
event: any,
event: lambda.APIGatewayProxyEvent,
context: lambda.Context,
): http.Server;
export function proxy(
server: http.Server,
event: any,
event: lambda.APIGatewayProxyEvent,
context: lambda.Context,
resolutionMode: 'CONTEXT_SUCCEED' | 'PROMISE',
): ProxyResult;
export function proxy(
server: http.Server,
event: any,
event: lambda.APIGatewayProxyEvent,
context: lambda.Context,
resolutionMode: 'CALLBACK',
callback?: (error: any, response: Response) => void

View File

@ -1,8 +1,24 @@
import { IncomingMessage } from 'http';
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { RequestHandler } from 'express';
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]; // tslint:disable-line:ban-types
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
declare module 'http' {
interface IncomingMessage {
apiGateway?: {
event: Omit<APIGatewayProxyEvent, 'body'>;
context: NonFunctionProperties<Context>;
};
}
}
export interface Options {
reqPropKey?: string;
deleteHeaders?: boolean;
}
export function eventContext(options?: Options): RequestHandler;
export {};