Skip to content

feat(event-handler): API Event handler for AWS Lambda #1727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"packages/parameters",
"packages/idempotency",
"packages/batch",
"packages/event-handler",
"packages/testing",
"docs/snippets",
"layers",
Expand Down
Empty file.
83 changes: 83 additions & 0 deletions packages/event-handler/LICENSE-THIRD-PARTY
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
@aws-lambda-powertools/commons
0.0.2 <https://github.com/aws-powertools/powertools-lambda-typescript>
license: MIT*
authors: Amazon Web Services <https://aws.amazon.com>

******************************

@types/aws-lambda
8.10.87 <https://github.com/DefinitelyTyped/DefinitelyTyped>
MIT License

Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE


******************************

lodash
4.17.21 <https://github.com/lodash/lodash>
Copyright OpenJS Foundation and other contributors <https://openjsf.org/>

Based on Underscore.js, copyright Jeremy Ashkenas,
DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>

This software consists of voluntary contributions made by many
individuals. For exact contribution history, see the revision history
available at https://github.com/lodash/lodash

The following license applies to all parts of this software except as
documented below:

====

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

====

Copyright and related rights for sample code are waived via CC0. Sample
code is defined as all source code displayed within the prose of the
documentation.

CC0: http://creativecommons.org/publicdomain/zero/1.0/

====

Files located in the node_modules and vendor directories are externally
maintained libraries used by this software which have their own
licenses; we recommend you read them, as their terms may differ from the
terms above.
85 changes: 85 additions & 0 deletions packages/event-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# event-handler

Minimalistic event handler & HTTP router for Serverless applications

## Simple Example

```typescript
// Import API Gateway Event handler
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { ApiGatewayResolver } from './ApiGateway';
import { AsyncFunction, BaseProxyEvent, JSONData } from 'types';

// Initialize the event handler
const app = new ApiGatewayResolver();

// Define a route
const helloHandler =
async (_event: BaseProxyEvent, _context: Context) : Promise<JSONData> => Promise.resolve({ message: 'Hello World' });

// Register Route
app.addRoute('GET', '/v1/hello', helloHandler as AsyncFunction) ;

// Declare your Lambda handler
// Declare your Lambda handler
exports.handler = (
_event: APIGatewayProxyEvent,
_context: Context
): Promise<JSONData> =>
// Resolve routes
app.resolve(_event, _context)
;
```

## Register Route with Decorators

```typescript
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { ApiGatewayResolver } from './ApiGateway';
import { BaseProxyEvent, JSONData } from 'types';

// Initialize the event handler
const app = new ApiGatewayResolver();

// Define a Controller class
export class HelloController{

// Register a route
@app.get('/v1/hello')
public hello (_event: BaseProxyEvent, _context: Context) : Promise<JSONData> {
return Promise.resolve({ message: 'Hello World' });
}

@app.post('/v1/hello')
public postHello (_event: BaseProxyEvent, _context: Context) : Promise<JSONData> {
return Promise.resolve({ message: 'Resource created' });
}

}

// Declare your Lambda handler
exports.handler = (
_event: APIGatewayProxyEvent,
_context: Context
): Promise<JSONData> =>
// Resolve routes
app.resolve(_event, _context)
;

```

## CORS Support

```typescript
// Import API Gateway Event handler
import { CORSConfig } from 'types';
import { ApiGatewayResolver, ProxyEventType } from './ApiGateway';

// App with CORS Configurattion
const app = new ApiGatewayResolver(
ProxyEventType.APIGatewayProxyEvent,
new CORSConfig()
);

```

28 changes: 28 additions & 0 deletions packages/event-handler/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
displayName: {
name: 'Powertools for AWS Lambda (TypeScript) utility: EVENT-HANDLER',
color: 'yellow',
},
runner: 'groups',
preset: 'ts-jest',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
moduleFileExtensions: ['js', 'ts'],
collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'],
testMatch: ['**/?(*.)+(spec|test).ts'],
roots: ['<rootDir>/src', '<rootDir>/tests'],
testPathIgnorePatterns: ['/node_modules/'],
testEnvironment: 'node',
coveragePathIgnorePatterns: ['/node_modules/', '/types/'],
coverageThreshold: {
global: {
statements: 100,
branches: 100,
functions: 100,
lines: 100,
},
},
coverageReporters: ['json-summary', 'text', 'lcov'],
setupFiles: [],
};
55 changes: 55 additions & 0 deletions packages/event-handler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@aws-lambda-powertools/event-handler",
"version": "0.0.1",
"description": "Minimalistic event handler & http router for the Powertools for AWS Lambda (TypeScript) library.",
"author": {
"name": "Amazon Web Services",
"url": "https://aws.amazon.com"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "npm run test:unit",
"test:unit": "jest --group=unit --detectOpenHandles --coverage --verbose",
"test:e2e:nodejs14x": "echo 'Not Implemented'",
"test:e2e:nodejs16x": "echo 'Not Implemented'",
"test:e2e:nodejs18x": "echo 'Not Implemented'",
"test:e2e": "echo 'Not Implemented'",
"watch": "jest --watch",
"build": "tsc --build --force",
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
"prebuild": "rimraf ./lib",
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
},
"lint-staged": {
"*.{js,ts}": "npm run lint-fix"
},
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/event-handler#readme",
"license": "MIT-0",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"
},
"bugs": {
"url": "https://github.com/aws-powertools/powertools-lambda-typescript/issues"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.111"
},
"keywords": [
"aws",
"lambda",
"powertools",
"event-handler",
"http-router",
"serverless",
"nodejs"
]
}
Loading