Skip to content
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

Add the json-deserializer middleware package #57

Merged
merged 27 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
57fe6a9
feat(json-deserilalizer): initial implementation of json- deserialize…
matt-jenner Jul 25, 2021
ed72ce7
docs(json-deserializer): Added readme.md to json-deserializer
matt-jenner Jul 25, 2021
5165316
docs: Added packages/json-deserializer to main readme.md
matt-jenner Jul 25, 2021
0312425
fix(json-deserializer): fixed code-coverage
matt-jenner Jul 28, 2021
890e9dd
refactor(json-deserializer): remove should from unit-test statements
matt-jenner Jul 28, 2021
ab9a9ab
refactor(json-deserializer): replace let with const in jsonDeserializer
matt-jenner Jul 28, 2021
12f7383
feat(json-deserializer): do not overwrite body use bodyObject
matt-jenner Jul 28, 2021
c29dbbe
feat(json-deserilalizer): initial implementation of json- deserialize…
matt-jenner Jul 25, 2021
dee51cf
docs(json-deserializer): Added readme.md to json-deserializer
matt-jenner Jul 25, 2021
c4bfa4e
docs: Added packages/json-deserializer to main readme.md
matt-jenner Jul 25, 2021
7d0ab74
fix(json-deserializer): fixed code-coverage
matt-jenner Jul 28, 2021
8b12ef5
refactor(json-deserializer): remove should from unit-test statements
matt-jenner Jul 28, 2021
42c6ce5
refactor(json-deserializer): replace let with const in jsonDeserializer
matt-jenner Jul 28, 2021
972a978
feat(json-deserializer): do not overwrite body use bodyObject
matt-jenner Jul 28, 2021
fe5e1b2
Merge branch 'feature/json-deserializer' of https://github.com/matt-j…
matt-jenner Aug 30, 2021
c9b14ba
feat(json-deserialiser): add statusCode to error response
matt-jenner Aug 30, 2021
f689404
fix(json-deserializer): fixed codeQL vulnerability
matt-jenner Aug 30, 2021
95d87dc
chore(json-deserializer): added changes after calling rush change
matt-jenner Aug 30, 2021
f4c0c3c
Merge remote-tracking branch 'origin/main' into feature/json-deserial…
dbartholomae Sep 1, 2021
f54cd0d
fix: solve TypeScript problem in example
dbartholomae Sep 1, 2021
a72b369
chore: update serverless dependencies
dbartholomae Sep 1, 2021
deed126
fix: update example to use bodyObject instead of body
dbartholomae Sep 1, 2021
3fb29a3
chore: add changelog update
dbartholomae Sep 1, 2021
63db063
chore: update serverless runtime
dbartholomae Sep 1, 2021
7524415
chore: update serverless versions in template
dbartholomae Sep 1, 2021
c016054
chore: set service name directly in serverless.yml files
dbartholomae Sep 1, 2021
dfa8dbd
fix: use example compatible with serverless
dbartholomae Sep 1, 2021
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ This monorepo is a collection of middleware for AWS lambda functions.
AWS lambdas.[![downloads](https://img.shields.io/npm/dw/%40lambda-middleware%2Fie-no-open.svg)](https://npmjs.org/package/@lambda-middleware/ie-no-open)
* [@lambda-middleware/json-serializer](packages/json-serializer): A middleware for AWS http lambda functions to
serialize JSON responses.[![downloads](https://img.shields.io/npm/dw/%40lambda-middleware%2Fjson-serializer.svg)](https://npmjs.org/package/@lambda-middleware/json-serializer)
* [@lambda-middleware/json-deserializer](packages/json-deserializer): A middleware for AWS http lambda functions to
deserialize JSON request bodies.[![downloads](https://img.shields.io/npm/dw/%40lambda-middleware%2Fjson-deserializer.svg)](https://npmjs.org/package/@lambda-middleware/json-deserializer)
* [@lambda-middleware/jwt-auth](packages/jwt-auth): A middleware for AWS http lambda functions to verify JWT auth
tokens inspired by express-jwt.[![downloads](https://img.shields.io/npm/dw/%40lambda-middleware%2Fjwt-auth.svg)](https://npmjs.org/package/@lambda-middleware/jwt-auth)
* [@lambda-middleware/middy-adaptor](packages/middy-adaptor): An adaptor to use middy middleware as functional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@lambda-middleware/json-deserializer",
"comment": "Added json-deserializer lambda-middleware package",
"type": "minor"
}
],
"packageName": "@lambda-middleware/json-deserializer",
"email": "matt@jenner.me.uk"
}
79 changes: 52 additions & 27 deletions common/config/rush/pnpm-lock.yaml

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

12 changes: 12 additions & 0 deletions packages/json-deserializer/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
extends: [
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
sourceType: "module",
},
};
29 changes: 29 additions & 0 deletions packages/json-deserializer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# IDEs
.idea

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
package-lock.json
yarn-debug.log*
yarn-error.log*
yarn.lock

# Built library
lib
21 changes: 21 additions & 0 deletions packages/json-deserializer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Matt Jenner

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.
54 changes: 54 additions & 0 deletions packages/json-deserializer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# @lambda-middleware/json-deserializer

[![npm version](https://badge.fury.io/js/%40lambda-middleware%2Fjson-deserializer.svg)](https://npmjs.org/package/@lambda-middleware/json-deserializer)
[![downloads](https://img.shields.io/npm/dw/%40lambda-middleware%2Fjson-deserializer.svg)](https://npmjs.org/package/@lambda-middleware/json-deserializer)
[![open issues](https://img.shields.io/github/issues-raw/dbartholomae/lambda-middleware.svg)](https://github.com/dbartholomae/lambda-middleware/issues)
[![debug](https://img.shields.io/badge/debug-blue.svg)](https://github.com/visionmedia/debug#readme)
[![build status](https://github.com/dbartholomae/lambda-middleware/workflows/.github/workflows/build.yml/badge.svg?branch=main)](https://github.com/dbartholomae/lambda-middleware/actions?query=workflow%3A.github%2Fworkflows%2Fbuild.yml)
[![codecov](https://codecov.io/gh/dbartholomae/lambda-middleware/branch/main/graph/badge.svg)](https://codecov.io/gh/dbartholomae/lambda-middleware)
[![dependency status](https://david-dm.org/dbartholomae/lambda-middleware.svg?theme=shields.io)](https://david-dm.org/dbartholomae/lambda-middleware)
[![devDependency status](https://david-dm.org/dbartholomae/lambda-middleware/dev-status.svg)](https://david-dm.org/dbartholomae/lambda-middleware?type=dev)

A middleware for AWS http lambda functions to deserialize incoming requests containing a json body.

Depending on the request payload and header the following can happen:

- **The event has a valid json content-type header** (eg. `application/json`), and:
- **The body contains a valid JSON payload** - `body` is deserialized and added back to the event object as `bodyObject` with a type of `Record<string, unknown>`.
- **The body has invalid JSON payload** - the middleware throws a `RequestBodyNotJsonError`.
- **The event has a non json content-type header** - the middleware will add a `bodyObject` property with `null`.
- **The event has no body set** - the middleware will add a `bodyObject` property with `null`.

Please note that this middleware just provides a basic object to the handler without typing for the properties, you will need to handle validation of the request body object separately.

## Lambda middleware

This middleware is part of the [lambda middleware series](https://dbartholomae.github.io/lambda-middleware/). It can be used independently.

## Usage

```typescript
import { jsonDeserializer } from "@lambda-middleware/json-deserializer";
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
import { APIGatewayProxyObjectEvent } from "../lib/types/APIGatewayProxyObjectEvent";

// This is your AWS handler
async function helloWorld(
request: APIGatewayProxyObjectEvent<APIGatewayProxyEvent>
): Promise<APIGatewayProxyResult> {
// We can simply pick out the body object from the request and use it
const { bodyObject } = request ?? {};

// Do something with the object and return it
return {
statusCode: 200,
body: JSON.stringify({
...bodyObject,
additionalThing: "addedInHandler",
}),
};
}

// Wrap the handler with the middleware
export const handler = jsonDeserializer()(helloWorld);
```
Loading