@evokegroup/aws
v3.0.3
Published
AWS utilities
Downloads
29
Keywords
Readme
@evokegroup/aws
AWS utilities
Requires NodeJS 18+
Class: LambdaEvent
Properties
| Property | Type | Description |
| -------- | ---- | ----------- |
| event | object
| The event data from Lambda |
Class: ApiGatewayEvent
extends LambdaEvent
Properties
| Property | Type | Description |
| -------- | ---- | ----------- |
| headers | object
| The event headers |
| stageVariables | object
| The stage variables sent into the event by API Gateway |
Methods
getBody()
Returns the event request body.
setBody(value)
Overwrite the event request body.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| value | *
| | The body value. Will be stringified. |
getBodyJSON()
Returns the event request body as JSON or Error
if the data cannot be parsed.
isJsonRequest()
Returns true if the Content-Type
header is set to application/json
getHeader(name)
Gets a header by a case-insensitive name.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| name | string
| | The case-insensitive header name |
setHeader(name, value)
Sets a header.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| name | string
| | The case-insensitive header name |
| value | string
| | The header value |
getStage()
Gets the stage sent by API Gateway
getStageVariable(name)
Gets a stage variable by name. Returns null if not found.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| name | string
| | The stage variable name |
setStageVariable(name, value)
Overrides a stage variable.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| name | string
| | The stage variable name |
| value | string
| | The stage variable value |
Class: Config
Base class for configurations classes that can pass their configuration values via an object
Methods
init(config = this)
Initializes the configuration properties.
set(config = {})
Sets properties.
initProperty(name, value)
Initializes a single property
| Parameter | Type | Description |
| --------- | ---- | ----------- |
| name | string
| The property name |
| value | *
| The default property value |
getTypedEnvValueOrDefault(value, defaultValue)
Attempts to convert value
to the defaultValue
type. If defaultValue
is a function, value
will be passed as the argument and the result will be returned.
| Parameter | Type | Description |
| --------- | ---- | ----------- |
| value | *
| The value |
| defaultValue | *
| The default typed value to fallback on |
Class: EnvironmentConfig
extends Config
Base class for configurations classes using environment variables (process.env)
Class: LambdaConfig
extends EnvironmentConfig
Base class for configurations classes within a Lambda function
constructor(event)
| Parameters | Type | Default | Description |
| ---------- | ---- | ------- | ----------- |
| event | EvokeAws.LambdaEvent
, object
| | The event data passed to the Lambda function |
const { LambdaConfig, SuccessResponse } = require('@evokegroup/aws');
class MyConfig extends LambdaConfig {
site = null;
environment = null;
constructor(event) {
super(event);
this.init();
}
}
export const handler = async (event) => {
return new Promise((resolve, reject) => {
const config = new MyConfig(event);
// do stuff
resolve(SuccessResponse.json());
});
};
Properties
event
The EvokeAws.LambdaEvent
created from the event data passed to the function
Class: ApiGatewayConfig
extends EnvironmentConfig
Base class for configurations classes within a Lambda function being exexuted via API Gateway and using stage variables
Class: LambdaResponse
A basic Lambda response object.
{
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: "Hello world",
isBase64Encoded: false
}
constructor(object)
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| statusCode | number
| 200
| The HTTP status code |
| headers | object
| { "Access-Control-Allow-Origin": "*" }
| The response headers. These headers will be merged with the default headers. |
| body | string
| | The response body |
| isBase64Encoded | boolean
| false
| The body is base64 encoded |
// Return a base64 encoded Buffer
import { LambdaResponse } from '@evokegroup/aws';
export const handler = async (event) => {
return new Promise((resolve, reject) => {
resolve(LambdaResponse.json({ body: Buffer.from('data', 'ascii'), isBase64Encoded: true }));
});
}
Methods
serialize()
Serializes the response.
toJSON()
See serialize
.
Class: JsonResponse
extends LambdaResponse
Base class for JSON-based responses.
{
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
},
body: "{\"success\":true,\"message\":\"OK\",\"data\":{}}",
isBase64Encoded: false
}
constructor(object)
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| statusCode | number
| 200
| The HTTP status code |
| headers | object
| { "Access-Control-Allow-Origin": "*" }
| The response headers. These headers will be merged with the default headers. |
| message | string
| ""
| |
| data | object
| null
| |
Class: SuccessResponse
extends JsonResponse
Creates a JsonResponse
with a default statusCode = 200
and body success = true
.
constructor(object)
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| statusCode | number
| 400
| The HTTP status code |
| headers | object
| { "Content-Type": "application/json" }
| See EvokeAws.LambdaResponse
|
| message | string
| ""
| The response message |
| data | object
| null
| An object to send back in the response |
const { SuccessResponse } = require('@evokegroup/aws');
export const handler = async (event) => {
return new Promise((resolve, reject) => {
resolve(SuccessResponse.json());
});
};
Class: ErrorResponse
extends JsonResponse
Creates a JsonResponse
with a default statusCode = 400
and body success = false
.
{
statusCode: 400,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
},
body: "{\"success\":false,\"message\":\"An error has occurred.\",\"data\":null}",
isBase64Encoded: false
}
constructor(object)
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| statusCode | number
| 400
| The HTTP status code |
| headers | object
| { "Content-Type": "application/json" }
| See EvokeAws.LambdaResponse
|
| message | string
| ""
| The response message |
| data | object
| null
| An object to send back in the response |
| err | object
| null
| Any error information |
const { ErrorResponse } = require('@evokegroup/aws');
export const handler = async (event) => {
return new Promise((resolve, reject) => {
resolve(ErrorResponse.json({ statusCode: 403, message: 'Unauthorized' }));
});
};