# api-request-handler

> API Request Handler

Latest version **0.10.0** (published 2024-01-16) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install api-request-handler
pnpm add api-request-handler
yarn add api-request-handler
bun add api-request-handler
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.10.0 |
| Published | 2024-01-16 |
| First published | 2019-01-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.0.0 |
| Dependencies | 2 |
| Unpacked size | 19.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Junmin Ahn |
| Maintainers | smartahn, junminahn |
| Keywords | express, api, request, handler |

## Links

- npm: https://www.npmjs.com/package/api-request-handler
- Repository: https://github.com/junminahn/express-toolkit
- Homepage: https://github.com/junminahn/express-toolkit#readme
- Issues: https://github.com/junminahn/express-toolkit/issues
- npm.io page: https://npm.io/package/api-request-handler

## Dependencies (2)

- [client-errors](https://npm.io/package/client-errors.md) 0.9.2
- [@fast-csv/format](https://npm.io/package/@fast-csv/format.md) ^4.3.5

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.10.0 (latest) — 2024-01-16
- 0.9.0 — 2024-01-10
- 0.8.7 — 2023-08-21
- 0.8.6 — 2023-08-21
- 0.8.5 — 2023-08-21
- 0.8.4 — 2023-08-21
- 0.8.3 — 2023-06-28
- 0.8.2 — 2022-04-09
- 0.7.2 — 2022-04-09
- 0.6.2 — 2022-04-09
- 0.5.2 — 2021-12-22
- 0.5.1 — 2021-07-20
- 0.4.1 — 2021-03-22
- 0.4.0 — 2021-01-06
- 0.3.1 — 2020-12-21
- … 1 more at https://npm.io/package/api-request-handler/versions

## README

# api-request-handler

API Request Handler

## Installation

```sh
$ npm install api-request-handler
```

## Usage

### handleResponse

- handleResponse (callback)
- handleResponse (callback, callback2)
- handleResponse ([callback, callback2])

### errorMessageProvider

- apiHandler.errorMessageProvider = customErrorMessageProvider

### Hooks

- apiHandler.preJson = preJsonHookFn
- apiHandler.postJson = postJsonHookFn
- apiHandler.preError = preErrorHookFn
- apiHandler.postError = postErrorHookFn

### Example

```js
const http = require('http');
const express = require('express');

const apiHandler = require('api-request-handler');
const baseUrl = 'http://localhost:8080';

const handleResponse = apiHandler.handleResponse;

const app = express();
app.set('port', 8080);
const server = http.createServer(app);
server.listen(8080);

// Single Middleware
app.get('/single-middleware', handleResponse(fnApple)); // apple; status 200

// Multiple Middlewares
app.get('/multiple-middlewares', handleResponse(fnAppleNext, fnPear)); // pear; status 200

// Multiple Middlewares Array
app.get('/multiple-middlewares-array', handleResponse([fnAppleNext, fnPear])); // pear; status 200

// Multiple Async Middlewares
app.get('/multiple-async-middlewares', handleResponse(fnAppleNext, fnPearPromise)); // pear; status 200

// Error Handling
app.get('/error-handling', handleResponse(fnAppleNext, fnError1, fnPear)); // error1; status 422

// Async Error Handling
app.get('/async-error-handling', handleResponse(fnAppleNext, fnError1Promise, fnPear)); // error1; status 422

// Multiple Async Error Handling
app.get('/multiple-async-error-handling', handleResponse(fnError2Next, fnError1)); // error1; status 422

// Custom Error Message Provider
apiHandler.errorMessageProvider = function (err) {
  return 'customError';
};
app.get('/custom-error-message-provider', handleResponse(fnError1)); // customError; status 422

function fnApple(req, res, next) {
  return 'apple';
}

function fnApplePromise(req, res, next) {
  return Promise.resolve('apple');
}

function fnAppleNext(req, res, next) {
  next();
  return 'apple';
}

function fnPear(req, res, next) {
  return 'pear';
}

function fnPearPromise(req, res, next) {
  return Promise.resolve('pear');
}

function fnPearNext(req, res, next) {
  next();
  return Promise.resolve('pear');
}

function fnError1(req, res, next) {
  throw new Error('error1');
}

function fnError1Promise(req, res, next) {
  return Promise.reject(new Error('error1'));
}

function fnError2Next(req, res, next) {
  next();
  throw new Error('error2');
}
```

### [MIT Licensed](LICENSE)

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