# @janiscommerce/sls-api-response

> A package to handle Serverless API Responses

Latest version **1.5.0** (published 2021-11-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install @janiscommerce/sls-api-response
pnpm add @janiscommerce/sls-api-response
yarn add @janiscommerce/sls-api-response
bun add @janiscommerce/sls-api-response
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2021-11-11 |
| First published | 2019-06-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Janis |
| Maintainers | janiscommerce |

## Links

- npm: https://www.npmjs.com/package/@janiscommerce/sls-api-response
- Repository: https://github.com/janis-commerce/sls-api-response
- Homepage: https://github.com/janis-commerce/sls-api-response#readme
- Issues: https://github.com/janis-commerce/sls-api-response/issues
- npm.io page: https://npm.io/package/@janiscommerce/sls-api-response

## Dependencies (1)

- [lllog](https://npm.io/package/lllog.md) ^1.1.2

## Recent versions

- 1.5.0 (latest) — 2021-11-11
- 1.4.0 — 2021-11-10
- 1.3.1 — 2020-06-24
- 1.3.0 — 2020-06-23
- 1.2.1 — 2019-10-30
- 1.2.0 — 2019-08-05
- 1.1.1 — 2019-06-25
- 1.1.0 — 2019-06-14
- 1.0.1 — 2019-06-14

## README

# Serverless API Response

![Build Status](https://github.com/janis-commerce/sls-api-response/workflows/Build%20Status/badge.svg?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/sls-api-response/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/sls-api-response?branch=master)
[![npm version](https://badge.fury.io/js/%40janiscommerce%2Fsls-api-response.svg)](https://www.npmjs.com/package/@janiscommerce/sls-api-response)

A package to handle Serverless API Responses

## Installation

```
npm install @janiscommerce/sls-api-response
```

## Usage

```js
'use strict';

const { ApiResponse } = require('@janiscommerce/sls-api-response');

// Successful response (default statusCode is 200)
ApiResponse.send({
	body: {
		foo: 'bar'
	}
});

// Successful response with response headers
ApiResponse.send({
	body: {
		foo: 'bar'
	},
	headers: {
		'x-foo': 'bar'
	}
});

// Successful response with simple cookie
ApiResponse.send({
	body: {
		foo: 'bar'
	},
	cookies: {
		'my-cookie': 'bar'
	}
});

// Successful response with complex cookie
ApiResponse.send({
	body: {
		foo: 'bar'
	},
	cookies: {
		'my-cookie': {
			value: 'bar',
			httpOnly: true,
			secure: true,
			path: '/',
			expires: new Date(), // Or (new Date()).toUTCString()
			domain: '.example.com'
		}
	}
});

// Error response (default statusCode is 500)
ApiResponse.sendError(new Error('We have a problem'));
// Response body: { "message": "We have a problem" }

// Error response for a request with a client
ApiResponse.sendError(new Error('We have a problem'), 'some-client-code');
// Response body: { "message": "We have a problem" }

// Error response with custom status code
const error = new Error('You have a problem');
error.statusCode = 400;
ApiResponse.sendError(error);
// Response body: { "message": "You have a problem" }

// Error response with custom response body
const error = new Error('You have a problem');
error.body = { customErrorMessage: "Bad request body" }
ApiResponse.sendError(error);
// Response body: { "customErrorMessage": "Bad request body" }
```

## Configuration

In order to work properly, you have to set valid response templates in each function configuration. An example can be found [here](https://github.com/janis-commerce/sls-api-rest#function-minimal-configuration)

---
_Source: https://npm.io/package/@janiscommerce/sls-api-response · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
