Skip to content

Commit

Permalink
GH-157 Add ApiGateway typings
Browse files Browse the repository at this point in the history
  • Loading branch information
ceilfors committed Sep 23, 2019
1 parent a7c337f commit 11453d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
32 changes: 30 additions & 2 deletions packages/laconia-event/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
S3Event as AWSS3Event,
SNSEvent as AWSSNSEvent,
SQSEvent as AWSSQSEvent,
KinesisStreamEvent as AWSKinesisStreamEvent
KinesisStreamEvent as AWSKinesisStreamEvent,
APIGatewayEvent as AWSAPIGatewayEvent,
APIGatewayProxyResult as AWSAPIGatewayProxyResult
} from "aws-lambda";
import { Readable } from "stream";

Expand Down Expand Up @@ -38,7 +40,33 @@ declare namespace event {
interface KinesisEvent {
records: KinesisRecord[];
}
function kinesis(AWSKinesisStreamEvent: AWSKinesisStreamEvent): KinesisEvent;
function kinesis(awsKinesisStreamEvent: AWSKinesisStreamEvent): KinesisEvent;

namespace apigateway {
interface ApiGatewayInputHeaders {
[key: string]: string;
}

interface ApiGatewayOutputHeaders {
[key: string]: string;
}

interface ApiGatewayInputParams {
[key: string]: string;
}
interface ApiGatewayEvent {
body: any;
headers: ApiGatewayInputHeaders;
params: ApiGatewayInputParams;
}
function req(awsAPIGatewayEvent: AWSAPIGatewayEvent): ApiGatewayEvent;

function res(
output: any,
statusCode?: number,
headers?: ApiGatewayOutputHeaders
): AWSAPIGatewayProxyResult;
}
}

export = event;
23 changes: 21 additions & 2 deletions packages/laconia-event/test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import {
SQSHandler,
SQSEvent,
KinesisStreamHandler,
KinesisStreamEvent
KinesisStreamEvent,
APIGatewayProxyHandler,
APIGatewayEvent
} from "aws-lambda";
import { s3, sns, sqs, kinesis } from "../../src/index";
import { s3, sns, sqs, kinesis, apigateway } from "../../src/index";
const { req, res } = apigateway;

import AWS from "aws-sdk";
import { integer } from "aws-sdk/clients/lightsail";

const s3Handler: S3Handler = (event: S3Event) => {
const s3Event = s3(event);
Expand Down Expand Up @@ -41,3 +46,17 @@ const kinesisHandler: KinesisStreamHandler = (event: KinesisStreamEvent) => {
console.log(r.data);
});
};

const apiGatewayHandler: APIGatewayProxyHandler = (event: APIGatewayEvent) => {
const r = req(event);
console.log(r.body);
console.log(r.headers.TEST);
console.log(r.params.id);

res("Not found", 404);
res({ hello: "world" });
res({ error: "not found" }, 404, {
"Access-Allow-Control-Origin": "*"
});
return Promise.resolve(res("Success"));
};

0 comments on commit 11453d1

Please sign in to comment.