aws-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.14 • Public • Published

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;
};

Package Sidebar

Install

npm i aws-ts

Weekly Downloads

0

Version

1.0.14

License

ISC

Unpacked Size

21.6 kB

Total Files

9

Last publish

Collaborators

  • gargpiyush195