-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de2c1ba
commit 55ecc6e
Showing
7 changed files
with
158 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
"keywords": [ | ||
"serverless", | ||
"middleware", | ||
"laconia" | ||
"apigateway" | ||
], | ||
"author": "Thomas Schoffelen <[email protected]>", | ||
"license": "MIT", | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const Response = require("./Response"); | ||
const Request = require("./Request"); | ||
|
||
class ApiAdapter { | ||
constructor(app, policies, errorConverter) { | ||
this.app = app; | ||
this.policies = policies || []; | ||
this.errorConverter = errorConverter; | ||
this.statusCode = 200; | ||
this.additionalHeaders = {}; | ||
} | ||
|
||
async handle(event, context) { | ||
if (event && event.source && event.source === "serverless-plugin-warmup") { | ||
return "Lambda is warm"; | ||
} | ||
|
||
try { | ||
let input = event; | ||
|
||
// EventBridge events shouldn't be parsed | ||
if (event.headers || !event["detail-type"] || !event.source) { | ||
// Process events from API Gateway | ||
const { body, headers, params, method } = new Request(event); | ||
const { pathParameters, queryStringParameters } = event; | ||
|
||
input = { | ||
method, | ||
...event, | ||
...(params || {}), | ||
headers, | ||
params, | ||
query: queryStringParameters || {}, | ||
body: body || {}, | ||
path: pathParameters || {}, | ||
}; | ||
} | ||
|
||
for (let i = 0; i < this.policies.length; i++) { | ||
await this.policies[i](input, context); | ||
} | ||
|
||
let output = await this.app(input, context); | ||
|
||
if (output instanceof Response) { | ||
return { | ||
...output, | ||
headers: Object.assign(this.additionalHeaders, output.headers), | ||
}; | ||
} | ||
|
||
if (typeof output === "object" && output.statusCode && output.body) { | ||
this.statusCode = output.statusCode; | ||
if (output.headers && typeof output.headers === "object") { | ||
this.additionalHeaders = { | ||
...this.additionalHeaders, | ||
...output.headers, | ||
}; | ||
} | ||
output = output.body; | ||
} | ||
|
||
return new Response(output, this.statusCode, this.additionalHeaders); | ||
} catch (error) { | ||
console.error(error); | ||
return this.errorConverter.convert(error); | ||
} | ||
} | ||
|
||
toFunction() { | ||
return this.handle.bind(this); | ||
} | ||
} | ||
|
||
module.exports = ApiAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const Response = require("./Response"); | ||
|
||
const getMappingEntries = (mappings) => | ||
mappings instanceof Map ? mappings.entries() : Object.entries(mappings); | ||
|
||
const getMappingResponse = (mappings, error) => { | ||
let mappingResponse = {}; | ||
for (const [errorRegex, mapping] of getMappingEntries(mappings)) { | ||
if (error.name.match(errorRegex) || error.message.match(errorRegex)) { | ||
mappingResponse = mapping(error); | ||
break; | ||
} | ||
} | ||
|
||
return mappingResponse; | ||
}; | ||
|
||
class ErrorConverter { | ||
constructor(mappings) { | ||
this.mappings = mappings; | ||
} | ||
|
||
convert(error) { | ||
const mappingResponse = getMappingResponse(this.mappings, error); | ||
const body = mappingResponse.body || error.message; | ||
const statusCode = mappingResponse.statusCode || error.statusCode || 500; | ||
const headers = mappingResponse.headers || {}; | ||
|
||
return new Response(body, statusCode, headers); | ||
} | ||
} | ||
|
||
module.exports = ErrorConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const middleware = require("../src"); | ||
const event = require("lambda-sample-events/events/aws/apigateway-aws-proxy.json"); | ||
|
||
it("responds with correct error message and status", async () => { | ||
const exampleApp = middleware(async () => { | ||
throw new Error("page not found"); | ||
}); | ||
|
||
const response = await exampleApp(event); | ||
expect(response.statusCode).toEqual(404); | ||
expect(JSON.parse(response.body).error).toEqual( | ||
expect.objectContaining({ | ||
message: "page not found", | ||
}), | ||
); | ||
}); |