Skip to content

Commit 7b81c1f

Browse files
tdt17chgohlke
authored andcommitted
migrate to typescript (spring-media#29)
* add typescript + tests and dist/js for comparison * remove old javascript implementation
1 parent 171b726 commit 7b81c1f

29 files changed

+4864
-1603
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
indent_style = space
9+
indent_size = 2
10+
11+
end_of_line = lf
12+
charset = utf-8
13+
trim_trailing_whitespace = true
14+
insert_final_newline = true
15+
16+
[*.md]
17+
trim_trailing_whitespace = false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.idea
33
*.iml
4+
dist

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: node_js
22

3-
# test on two node.js versions: 0.6 and 0.8
43
node_js:
5-
- 6
4+
- 8

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ $ yarn add aws-lambda-router
3636
This is a simple example of `aws-lambda-router` in conjunction with ANY method and the API Gateway proxy integration. The following code will respond with a message when executed using an AWS API Gateway with a `GET` request on URL path `<base-url-of-gateway>/gateway-mapping/article/123`.
3737

3838
```js
39-
const router = require('aws-lambda-router');
39+
import * as router from 'aws-lambda-router'
4040

41-
// handler for an api gateway event
42-
exports.handler = router.handler({
41+
export const handler = router.handler({
4342
// for handling an http-call from an AWS API Gateway proxyIntegration we provide the following config:
4443
proxyIntegration: {
4544
routes: [
@@ -66,9 +65,9 @@ See the following example:
6665
6766
6867
```js
69-
const router = require('aws-lambda-router');
68+
import * as router from 'aws-lambda-router'
7069

71-
exports.handler = router.handler({
70+
export const handler = router.handler({
7271
// for handling an http-call from an AWS Apigateway proxyIntegration we provide the following config:
7372
proxyIntegration: {
7473
cors: true,
@@ -94,9 +93,9 @@ If CORS is activated, these default headers will be sent on every response:
9493
## Errormapping
9594
9695
```js
97-
const router = require('aws-lambda-router');
96+
import * as router from 'aws-lambda-router'
9897

99-
exports.handler = router.handler({
98+
export const handler = router.handler({
10099
// for handling an http-call from an AWS Apigateway proxyIntegration we provide the following config:
101100
proxyIntegration: {
102101
routes: [
@@ -135,9 +134,9 @@ SNS Event Structure: https://docs.aws.amazon.com/sns/latest/dg/sns-message-and-j
135134
For handling calls in Lambdas initiated from AWS-SNS you can use the following code snippet:
136135
137136
```js
138-
const router = require('aws-lambda-router');
137+
import * as router from 'aws-lambda-router'
139138

140-
exports.handler = router.handler({
139+
export const handler = router.handler({
141140
sns: {
142141
routes: [
143142
{
@@ -156,9 +155,9 @@ exports.handler = router.handler({
156155
For handling calls in Lambdas initiated from AWS-SQS you can use the following code snippet:
157156
158157
```js
159-
const router = require('aws-lambda-router');
158+
import * as router from 'aws-lambda-router'
160159

161-
exports.handler = router.handler({
160+
export const handler = router.handler({
162161
sqs: {
163162
routes: [
164163
{
@@ -200,9 +199,9 @@ The action method will be called with the records of the [S3Event Structure](htt
200199
The following examples demonstrates the most use cases:
201200
202201
```js
203-
const router = require('aws-lambda-router');
202+
import * as router from 'aws-lambda-router'
204203

205-
exports.handler = router.handler({
204+
export const handler = router.handler({
206205
s3: {
207206
routes: [
208207
{
@@ -290,6 +289,7 @@ See here: https://yarnpkg.com/en/docs/cli/link
290289
291290
## Release History
292291
292+
* 0.7.0 migrate to typescript; using aws-lambda typings; proxyIntegration: cors is now optional (default: false);
293293
* 0.6.2 take away old gulp dependency to run tests, works now with scripts in package.json; normalize request path to start from local host (thanks to [@napicella](https://github.com/napicella))
294294
* 0.6.1 s3: fix: aggregate result promises to one promise; fix: s3Route interface
295295
* 0.6.0 new feature: S3 routes available.

index.d.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { ProxyIntegrationConfig, ProxyIntegrationEvent } from "./lib/proxyIntegration";
2+
import { SnsConfig, SnsEvent } from "./lib/sns";
3+
import { SqsConfig, SqsEvent } from "./lib/sqs";
4+
import { S3Config, S3Event } from "./lib/s3";
5+
import { Context } from "aws-lambda";
6+
import { EventProcessor } from "./lib/EventProcessor";
7+
8+
export interface RouteConfig {
9+
proxyIntegration?: ProxyIntegrationConfig
10+
sns?: SnsConfig
11+
sqs?: SqsConfig
12+
s3?: S3Config
13+
debug?: boolean
14+
}
15+
16+
export type RouterEvent = ProxyIntegrationEvent | SnsEvent | SqsEvent | S3Event
17+
18+
export const handler = (routeConfig: RouteConfig) => {
19+
const eventProcessorMapping = extractEventProcessorMapping(routeConfig);
20+
21+
return async <TContext extends Context>(event: RouterEvent, context: TContext) => {
22+
if (routeConfig.debug) {
23+
console.log("Lambda invoked with request:", event);
24+
console.log("Lambda invoked with context:", context);
25+
}
26+
27+
for (const [eventProcessorName, eventProcessor] of eventProcessorMapping.entries()) {
28+
29+
try {
30+
// the contract of 'processors' is as follows:
31+
// - their method 'process' is called with (config, event)
32+
// - the method...
33+
// - returns null: the processor does not feel responsible for the event
34+
// - throws Error: the 'error.toString()' is taken as the error message of processing the event
35+
// - returns object: this is taken as the result of processing the event
36+
// - returns promise: when the promise is resolved, this is taken as the result of processing the event
37+
const result = eventProcessor.process((routeConfig as any)[eventProcessorName], event, context);
38+
if (result) {
39+
// be resilient against a processor returning a value instead of a promise:
40+
return await result
41+
} else {
42+
if (routeConfig.debug) {
43+
console.log("Event processor couldn't handle request.")
44+
}
45+
}
46+
} catch (error) {
47+
if (error.stack) {
48+
console.log(error.stack)
49+
}
50+
throw error.toString()
51+
}
52+
}
53+
throw 'No event processor found to handle this kind of event!'
54+
}
55+
}
56+
57+
const extractEventProcessorMapping = (routeConfig: RouteConfig) => {
58+
const processorMap = new Map<string, EventProcessor>()
59+
for (let key of Object.keys(routeConfig)) {
60+
if (key === 'debug') continue
61+
try {
62+
processorMap.set(key, require(`./lib/${key}`))
63+
} catch (error) {
64+
throw new Error(`The event processor '${key}', that is mentioned in the routerConfig, cannot be instantiated (${error.toString()})`)
65+
}
66+
}
67+
return processorMap;
68+
}

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
};

lib/EventProcessor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface EventProcessor<TConfig = any, TEvent = any, TContext = any, TOutput = any> {
2+
process: ProcessMethod<TConfig, TEvent, TContext, TOutput>
3+
}
4+
5+
export type ProcessMethod<TConfig, TEvent, TContext, TOutput = any> =
6+
(config: TConfig, event: TEvent, context: TContext) => null | TOutput | Promise<TOutput>

0 commit comments

Comments
 (0)