# express-scrubbr

> Serialization middleware using TypeScript as the schema.

Latest version **1.1.1** (published 2022-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-scrubbr
pnpm add express-scrubbr
yarn add express-scrubbr
bun add express-scrubbr
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-01-06 |
| First published | 2021-08-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jeremy Gillick |
| Maintainers | jgillick |
| Keywords | express, middleware, serialize, scrubbr |

## Links

- npm: https://www.npmjs.com/package/express-scrubbr
- Repository: https://github.com/jgillick/express-scrubbr
- Homepage: https://github.com/jgillick/express-scrubbr#readme
- Issues: https://github.com/jgillick/express-scrubbr/issues
- npm.io page: https://npm.io/package/express-scrubbr

## Dependencies (3)

- [scrubbr](https://npm.io/package/scrubbr.md) ^1.1.1
- [typescript](https://npm.io/package/typescript.md) ^4.3.5
- [@types/express](https://npm.io/package/@types/express.md) ^4.17.13

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2022-01-06
- 1.1.0 — 2021-10-08
- 0.0.4 — 2021-08-12
- 0.0.3 — 2021-08-12
- 0.0.2 — 2021-08-12
- 0.0.1 — 2021-08-12

## README

# Scrubbr Express TypeScript Serializer

Seamlessly serialize JSON data using TypeScript in express.

![Simple Example](https://github.com/jgillick/scrubbr/raw/main/example.png)

## Setup & Installation

This middleware requires [Scrubbr](https://github.com/jgillick/scrubbr) to be installed and setup.

```shell
npm i -S scrubbr
npm i -S express-scrubbr
```

Add middleware to your express app

```ts
import express from "express";
import Scrubbr from "scrubbr";
import scrubbrMiddleware from "express-scrubbr";

var app = express();

// Load typescript schema and set any scrubbr options
const scrubbr = new Scrubbr("./schema.ts");
app.use(scrubbrMiddleware(scrubbr));
```

Use it in your routes

```ts
app.get('/users', (req, res) => {
  const userData = fetchDataHere();
  resp.status(200)
    .scrubbr('UserList') // serialize userData with the UserList typescript type
    .send(userData);
}
```

## Setting route-level options

You can pass scrubbr options at the route level:

```ts
resp
  .status(200)
  .scrubbr("UserList", { logLevel: LogLevel.DEBUG })
  .send(userData);
```

You can also pass a custom scrubbr instance:

```ts
const customScrubbr = scrubbr.clone();
customScrubbr.addTypeSerializer("User", userTransformer);

resp.status(200).scrubbr("UserList", customScrubbr).send(userData);
```

## Set global state

The middleware will clone the scrubbr instance, so if you want to add global state you can do that through the express response locals object.

For example, adding the authenticated user to the global state:

```ts

app.use(scrubbrMiddleware(scrubbr));

// Auth middleware
app.use((req, res, next) => {
  const user = ... // do authy stuff here.

  // Access the scrubbr instance on the response locals object
  res.locals.scrubbr.setGlobalContext({ user }, true);
})


```

# License

[MIT](https://github.com/ajv-validator/ajv/blob/HEAD/LICENSE)

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