# pambda-json-api

> Pambda for JSON API

Latest version **0.1.1** (published 2020-08-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install pambda-json-api
pnpm add pambda-json-api
yarn add pambda-json-api
bun add pambda-json-api
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2020-08-16 |
| First published | 2017-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kengo Nakatsuka |
| Maintainers | nak2k |
| Keywords | aws, jsonapi, lambda, pambda |

## Links

- npm: https://www.npmjs.com/package/pambda-json-api
- Repository: https://github.com/pambda/pambda-json-api
- Issues: https://github.com/pambda/pambda-json-api/issues
- npm.io page: https://npm.io/package/pambda-json-api

## Dependencies (5)

- [err-json](https://npm.io/package/err-json.md) ^0.1.0
- [caseless-get](https://npm.io/package/caseless-get.md) ^0.1.0
- [content-type](https://npm.io/package/content-type.md) ^1.0.4
- [lambda-console](https://npm.io/package/lambda-console.md) ^0.1.2
- [lambda-callbackify](https://npm.io/package/lambda-callbackify.md) ^0.1.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2020-08-16
- 0.1.0 — 2019-05-09
- 0.0.7 — 2018-07-31
- 0.0.6 — 2018-04-18
- 0.0.5 — 2018-03-15
- 0.0.4 — 2017-07-19
- 0.0.3 — 2017-07-12
- 0.0.2 — 2017-07-12
- 0.0.1 — 2017-06-13

## README

# pambda-json-api

Pambda for JSON API.

## Installation

```
npm i pambda-json-api
```

## Usage

``` javascript
const { compose, createLambda } = require('pambda');
const { router } = require('pambda-router');
const { jsonApi } = require('pambda-json-api');

export const handler = createLambda(
  compose(
    router()
      .post('/api/(.*)', compose(
         jsonApi({
           reqJsonOnly: true,
           resJsonOnly: true,
           errorHandler: (err, event, context, callback) => {
             // Custom error handling
           },
         }),
         next => (event, context, callback) => {
           // API handling
         },
      ))
      .toPambda()
  )
);
```

## jsonApi(options)

- `options.reqJsonOnly`
    - The boolean value on whether to accept a JSON request only.
      If true, an error occurs for requests that `Content-Type` is not `application/json`.
    - The default value is true.
- `options.resJsonOnly`
    - The boolean value to whether to return a JSON response only.
      If true, call `JSON.stringify()` and set `Content-Type`.
    - The default value is true.
- `options.errorHandler`
    - The function with argument `(err, event, context, callback)`, called when an error occurs.
    - The default is to return Internal Server Error.

When receiving a JSON request, set the result of parsing JSON to `event.body`. Subsequent Pambda can use parsed `body`.

Also, if necessary, return `body` as a JSON string for the result that subsequent Pambda passes to `callback`.

## License

MIT

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