1.1.1 • Published 4 years ago

@powerkernel/sls-utils v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

sls-utils

Maintainability Test Coverage Build Status Dependency Status Download Status

Power Kernel utils for serverless

Installation

npm install @powerkernel/sls-utils --save

Documentation

Common utils to use with serverless framework

Available utils

The following utils are svailable: response

Auto add headers

headers: {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Credentials': true,
}

Success response

response.success = ({ headers = {}, code = 200, data = {} });

Failure response

response.failure = ({ headers = {}, code = 500, error = {} });

Complete Example

Here's an example of a serverless function that uses response to output the response.

import { response } from "@powerkernel/sls-utils";

import uuid from "uuid";
import * as dynamoDbLib from "./libs/dynamodb-lib";

export async function main(event, context) {
  const data = JSON.parse(event.body);
  const params = {
    TableName: process.env.tableName,
    Item: {
      userId: event.requestContext.identity.cognitoIdentityId,
      noteId: uuid.v1(),
      content: data.content,
      attachment: data.attachment,
      createdAt: Date.now()
    }
  };

  try {
    await dynamoDbLib.call("put", params);
    return response.success({ data: params.Item});
  } catch (e) {
    return response.failure({ error: { message: "Something went wrong." }});
  }

License

Copyright (c) 2020 Power Kernel

Licensed under the MIT license.

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago