3.0.3 • Published 2 years ago

@evokegroup/aws v3.0.3

Weekly downloads
6
License
ISC
Repository
bitbucket
Last release
2 years ago

@evokegroup/aws

AWS utilities

Requires NodeJS 18+

Class: LambdaEvent

Properties

PropertyTypeDescription
eventobjectThe event data from Lambda

Class: ApiGatewayEvent

extends LambdaEvent

Properties

PropertyTypeDescription
headersobjectThe event headers
stageVariablesobjectThe stage variables sent into the event by API Gateway

Methods

getBody()

Returns the event request body.

setBody(value)

Overwrite the event request body.

ParameterTypeDefaultDescription
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.

ParameterTypeDefaultDescription
namestringThe case-insensitive header name

setHeader(name, value)

Sets a header.

ParameterTypeDefaultDescription
namestringThe case-insensitive header name
valuestringThe header value

getStage()

Gets the stage sent by API Gateway

getStageVariable(name)

Gets a stage variable by name. Returns null if not found.

ParameterTypeDefaultDescription
namestringThe stage variable name

setStageVariable(name, value)

Overrides a stage variable.

ParameterTypeDefaultDescription
namestringThe stage variable name
valuestringThe 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

ParameterTypeDescription
namestringThe 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.

ParameterTypeDescription
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)

ParametersTypeDefaultDescription
eventEvokeAws.LambdaEvent, objectThe 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)

ParameterTypeDefaultDescription
statusCodenumber200The HTTP status code
headersobject{ "Access-Control-Allow-Origin": "*" }The response headers. These headers will be merged with the default headers.
bodystringThe response body
isBase64EncodedbooleanfalseThe 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)

ParameterTypeDefaultDescription
statusCodenumber200The HTTP status code
headersobject{ "Access-Control-Allow-Origin": "*" }The response headers. These headers will be merged with the default headers.
messagestring""
dataobjectnull

Class: SuccessResponse

extends JsonResponse

Creates a JsonResponse with a default statusCode = 200 and body success = true.

constructor(object)

ParameterTypeDefaultDescription
statusCodenumber400The HTTP status code
headersobject{ "Content-Type": "application/json" }See EvokeAws.LambdaResponse
messagestring""The response message
dataobjectnullAn 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)

ParameterTypeDefaultDescription
statusCodenumber400The HTTP status code
headersobject{ "Content-Type": "application/json" }See EvokeAws.LambdaResponse
messagestring""The response message
dataobjectnullAn object to send back in the response
errobjectnullAny error information
const { ErrorResponse } = require('@evokegroup/aws');

export const handler = async (event) => {
  return new Promise((resolve, reject) => {
    resolve(ErrorResponse.json({ statusCode: 403, message: 'Unauthorized' }));
  });
};
3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.6

4 years ago

1.2.2

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago