# @schornio/express-api-interface

> Interface for schorn.io-normalized JSON APIs

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

## Install

```sh
npm install @schornio/express-api-interface
pnpm add @schornio/express-api-interface
yarn add @schornio/express-api-interface
bun add @schornio/express-api-interface
```

## 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-07-27 |
| First published | 2020-02-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 32.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Thomas Schorn |
| Maintainers | schornio |

## Links

- npm: https://www.npmjs.com/package/@schornio/express-api-interface
- Repository: https://github.com/schornio/express-api-interface
- Homepage: https://github.com/schornio/express-api-interface#readme
- Issues: https://github.com/schornio/express-api-interface/issues
- npm.io page: https://npm.io/package/@schornio/express-api-interface

## Recent versions

- 1.5.0 (latest) — 2021-07-27
- 1.4.0 — 2021-07-27
- 1.3.0 — 2021-07-13
- 1.2.1 — 2021-07-12
- 1.2.0 — 2021-07-12
- 1.1.0 — 2021-03-02
- 1.0.5 — 2021-01-09
- 1.0.4 — 2020-04-11
- 1.0.3 — 2020-02-19
- 1.0.2 — 2020-02-18
- 1.0.0 — 2020-02-18

## README

# @schornio/express-api-interface

[![Build Status](https://travis-ci.org/schornio/express-api-interface.svg?branch=master)](https://travis-ci.org/schornio/express-api-interface)

## `Router`

```javascript
import { HTTPNotFoundError, Router } from '@schornio/express-api-interface';

export const router = new Router();

router.get('/endpoint_sync', (request) => {
  const q = request.ensureQuery('q');
  const result = searchSync(q);
  return result;
});

router.get('/endpoint_async', async (request) => {
  const q = request.ensureQuery('q');
  const result = await searchAsync(q);
  return result;
});

router.get('/endpoint/:param1', (request) => {
  const q = request.ensureParam('param1');
  const result = searchSync(q);
  return result;
});

router.post('/endpoint', (request) => {
  const body = request.ensureBody(assertBodyFN);
  saveBody(body);
});

router.post('/redirect', (request) => {
  return new Redirect(Redirect.TemporaryRedirect, '/endpoint');
});

router.get('/error', async (request) => {
  throw new HTTPNotFoundError('Gets converted to a 404 response');
});
```

## `App`

```javascript
import { App } from '@schornio/express-api-interface';

const PORT = process.env.PORT || '80';

const app = new App();

app.addSetting('trust proxy', true);

app.registerRoutes([['/endpoint', router]]);

/* eslint-disable-next-line no-console */
app.start(PORT).then(() => console.log(`Listening on port ${PORT}...`));
```

## `Request`

- `ensureBody<T>(assert: AssertBody<T>): T`
- `ensureParam(name: string): string`
- `ensureQuery(name: string): string`
- `setPayload<T>(name: string, payload: T): void`
- `getPayload<T>(name: string): T | undefined`
- `ensurePayload<T>(name: string): T`
- `getRequestCore(): ExpressRequest`

### Static

- `setPayload<T>(expressRequest: ExpressRequest, name: string, payload: T): void`
- `getPayload<T>(expressRequest: ExpressRequest, name: string): T | undefined`
- `ensurePayload<T>(expressRequest: ExpressRequest, name: string): T`

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