Git Product home page Git Product logo

express-scrubbr's Introduction

Scrubbr Express TypeScript Serializer

Seamlessly serialize JSON data using TypeScript in express.

Simple Example

Setup & Installation

This middleware requires Scrubbr to be installed and setup.

npm i -S scrubbr
npm i -S express-scrubbr

Add middleware to your express app

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

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:

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

You can also pass a custom scrubbr instance:

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:

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

express-scrubbr's People

Contributors

jgillick avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.