1.0.14 • Published 3 years ago

aws-ts v1.0.14

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

AWS-TS

This package lets you handle and send responses from AWS lambda with ease. You have the ability to send various types of responses such as JSON or Plain Text without worrying about headers and status codes. You can also enable or disable cors for all or specific responses or set custom headers.

Installation

yarn add aws-ts

OR

npm install aws-ts

Basic Example

import { HttpResponse } from "aws-ts";
// or
// const { HttpResponse } = require('aws-ts')

const httpResponse = new HttpResponse();

export const handler = async (event) => {
  const data = {
    message: "Hey there",
  };

  return httpResponse.successResponse(data).json;
};

Sending HTML as response

import { HttpResponse } from "aws-ts";

const httpResponse = new HttpResponse();

export const handler = async (event) => {
  const html = "<h1>Hey there</h1>";

  return httpResponse.html(html);
};

Setting up custom headers

import { HttpResponse } from "aws-ts";

const httpResponse = new HttpResponse();

export const handler = async (event) => {
  const data = {
    message: "Hey there",
  };

  const headers = {
    Authorization: "Bearer x3*********",
  };

  return httpResponse.successResponse(data, headers).json;
};

Setting up custom statusCodes

import { HttpResponse } from "aws-ts";

const httpResponse = new HttpResponse();

export const handler = async (event) => {
  const data = {
    message: "Hey there",
  };

  const headers = {
    Authorization: "Bearer x3*********",
  };

  return httpResponse.status(304).successResponse(data, headers).json;
};

Enabling CORS

const httpResponse = new HttpResponse({ enableCors: true });

Example

import { HttpResponse } from "aws-ts";

const httpResponse = new HttpResponse({ enableCors: true });

export const handler = async (event) => {
  const data = {
    message: "Hey there",
  };

  const headers = {
    Authorization: "Bearer x3*********",
  };

  return httpResponse.status(304).successResponse(data, headers).json;
};

Enabling CORS for specific handler

return httpResponse.successResponse(data).withCors().json;

Example:

import { HttpResponse } from "aws-ts";

const httpResponse = new HttpResponse();

export const handler = async (event) => {
  const data = {
    message: "Hey there",
  };

  const headers = {
    Authorization: "Bearer x3*********",
  };

  return httpResponse.status(304).successResponse(data, headers).withCors()
    .json;
};
1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago