You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Configuring Routes in API Gateway](#configuring-routes-in-api-gateway)
103
+
-[TypeScript Support](#typescript-support)
101
104
-[Contributions](#contributions)
102
105
103
106
## Installation
@@ -118,6 +121,7 @@ Require the `lambda-api` module into your Lambda handler script and instantiate
118
121
| callbackName |`String`| Override the default callback query parameter name for JSONP calls |
119
122
| logger |`boolean` or `object`| Enables default [logging](#logging) or allows for configuration through a [Logging Configuration](#logging-configuration) object. |
120
123
| mimeTypes |`Object`| Name/value pairs of additional MIME types to be supported by the `type()`. The key should be the file extension (without the `.`) and the value should be the expected MIME type, e.g. `application/json`|
124
+
| serializer |`Function`| Optional object serializer function. This function receives the `body` of a response and must return a string. Defaults to `JSON.stringify`|
121
125
| version |`String`| Version number accessible via the `REQUEST` object |
For detailed release notes see [Releases](https://github.com/jeremydaly/lambda-api/releases).
131
135
136
+
### v0.9: New error types, custom serializers, and TypeScript support
137
+
Lambda API now generates typed errors for easier parsing in middleware. You can also supply your own custom serializer for formatting output rather than using the default `JSON.stringify`. And thanks to @hassankhan, a TypeScript declaration file is now available.
138
+
132
139
### v0.8: Logging Support with Sampling
133
140
Lambda API has added a powerful (and customizable) logging engine that utilizes native JSON support for CloudWatch Logs. Log entries can be manually added using standard severities like `info` and `warn`. In addition, "access logs" can be automatically generated with detailed information about each requests. See [Logging](#logging) for more information about logging and auto sampling for request tracing.
134
141
@@ -390,6 +397,9 @@ The `REQUEST` object contains a parsed and normalized request from API Gateway.
390
397
-`rawBody`: If the `isBase64Encoded` flag is `true`, this is a copy of the original, base64 encoded body
391
398
-`route`: The matched route of the request
392
399
-`requestContext`: The `requestContext` passed from the API Gateway
400
+
-`pathParameters`: The `pathParameters` passed from the API Gateway
401
+
-`stageVariables`: The `stageVariables` passed from the API Gateway
402
+
-`isBase64Encoded`: The `isBase64Encoded` boolean passed from the API Gateway
393
403
-`auth`: An object containing the `type` and `value` of an authorization header. Currently supports `Bearer`, `Basic`, `OAuth`, and `Digest` schemas. For the `Basic` schema, the object is extended with additional fields for username/password. For the `OAuth` schema, the object is extended with key/value pairs of the supplied OAuth 1.0 values.
394
404
-`namespace` or `ns`: A reference to modules added to the app's namespace (see [namespaces](#namespaces))
395
405
-`cookies`: An object containing cookies sent from the browser (see the [cookie](#cookiename-value-options)`RESPONSE` method)
@@ -950,7 +960,7 @@ const api = require('lambda-api')({
950
960
})
951
961
```
952
962
953
-
Additional rules can be added by specify a `rules` parameter in the `sampling` configuration object. The `rules` should contain an `array` of "rule" objects with the following properties:
963
+
Additional rules can be added by specifying a `rules` parameter in the `sampling` configuration object. The `rules` should contain an `array` of "rule" objects with the following properties:
**NOTE:** Error handling middleware runs on *ALL* paths. If paths are passed in as the first parameter, they will be ignored by the error handling middleware.
1124
1134
1125
1135
### Error Types
1126
-
Error logs are generate using either the `error` or `fatal` logging level. Errors can be triggered from within routes and middleware by calling the `error()` method on the `RESPONSE` object. If provided a `string` as an error message, this will generate an `error` level log entry. If you supply a JavaScript `Error` object, or you `throw` an error, a `fatal` log entry will be generated.
1136
+
Lambda API provides several different types of errors that can be used by your application. `RouteError`, `MethodError`, `ResponseError`, and `FileError` will all be passed to your error middleware. `ConfigurationError`s will throw an exception when you attempt to `.run()` your route and can be caught in a `try/catch` block. Most error types contain additional properties that further detail the issue.
1137
+
1138
+
```javascript
1139
+
consterrorHandler= (err,req,res,next) => {
1140
+
1141
+
if (err.name==='RouteError') {
1142
+
// do something with route error
1143
+
} elseif (err.name==='FileError') {
1144
+
// do something with file error
1145
+
}
1146
+
// continue
1147
+
next()
1148
+
})
1149
+
```
1150
+
1151
+
### Error Logging
1152
+
Error logs are generated using either the `error` or `fatal` logging level. Errors can be triggered from within routes and middleware by calling the `error()` method on the `RESPONSE` object. If provided a `string` as an error message, this will generate an `error` level log entry. If you supply a JavaScript `Error` object, or you `throw` an error, a `fatal` log entry will be generated.
1127
1153
1128
1154
```javascript
1129
1155
api.get('/somePath', (res,req) => {
@@ -1271,6 +1297,14 @@ Simply create a `{proxy+}` route that uses the `ANY` method and all requests wil
1271
1297
## Reusing Persistent Connections
1272
1298
If you are using persistent connections in your function routes (such as AWS RDS or Elasticache), be sure to set `context.callbackWaitsForEmptyEventLoop = false;` in your main handler. This will allow the freezing of connections and will prevent Lambda from hanging on open connections. See [here](https://www.jeremydaly.com/reuse-database-connections-aws-lambda/) for more information.
1273
1299
1300
+
## TypeScript Support
1301
+
An `index.d.ts` declaration file has been included for use with your TypeScript projects (thanks @hassankhan). Please feel free to make suggestions and contributions to keep this up-to-date with future releases.
1302
+
1303
+
```javascript
1304
+
// import Lambda API and TypeScript declarations
1305
+
importAPIfrom'lambda-api'
1306
+
```
1307
+
1274
1308
## Contributions
1275
1309
Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/lambda-api/issues) for suggestions and bug reports or create a pull request.
0 commit comments