Socket
Socket
Sign inDemoInstall

scrubbr

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

scrubbr

Serialize your JSON API data using your TypeScript as the schema.


Version published
Weekly downloads
1
decreased by-94.44%
Maintainers
1
Weekly downloads
 
Created
Source

Scrubbr

Tests

Serialize and sanitize JSON API data using your TypeScript as the schema.

Simple Example

Serializing and sanitizing data sent from the webserver to the client shouldn't be hard. If you're already using TypeScript, you have everything you need. Scrubbr will use your TypeScript types to deeply transform and sanitize your data.

Install

npm i -S scrubbr

Quickstart

  1. Define a TypeScript file as your master schema:
// schema.ts

type UserList = {
  users: User[];
};

type User = {
  name: string;
  image: string;
};
  1. Load it into Scrubbr and serialize your data:
import Scrubbr from 'scrubbr';

// Load the typescript file and convert it to a schema that will be used later.
// Performance note: this is a synchronous file load. Load early and cache to a shared variable.
const scrubbr = new Scrubbr('./schema.ts');

async function api() {
  const data = getUsers();

  // Serialize the data based on the PostList type defined in schema.ts
  return await scrubbr.serialize(data, 'UserList');
}

// Raw unsanitized data
function getUsers() {
  return {
    users: [
      {
        name: 'John Doe',
        image: 'http://i.pravatar.cc/300',
        email: 'donotspam@me.com',
        password: 'xxxsecretxxx',
      },
    ],
  };
}
  1. Output
{
  "users": [
    {
      "name": "John Doe",
      "image": "http://i.pravatar.cc/300"
    }
  ]
}

Custom Serializers

You can define custom functions to change how the data is serialized.

Type Serializer

This function is called every time a matching TypeScript type is encountered.

For example, if you want to use another type to serialize a user who is logged in:

import Scrubbr, { useType } from 'scrubbr';

// Called ever time scrubbr finds a User type object
scrubbr.addTypeSerializer('User', (data, state) => {
  // This uses the context object that can be passed when serializing (see below)
  if (data.id === state.context.loggedInUserId) {
    return useType('UserPrivileged');
  }

  // You can also manually transform the data here
  return data;
});

// Context is passed to the serializers
const context = {
  loggedInUserId: 10,
};
const serialized = await scrubbr.serialize(data, 'PostList', context);

Path serializer

This serializer is called at each node of the data object regardless of type. It's called a path serializer because you'll use the state.path value to determine which node you're serializing.

In this example we want to convert every createdAt date value to the local timezone.

import moment from 'moment-timezone';
import Scrubbr, { useType } from 'scrubbr';

// This function is called ever time scrubbr finds a User type object
scrubbr.addPathSerializer('User', (data, state) => {
  // Convert all date-like strings from UTC to local time
  const path = state.path;
  if (path.match(/\.createdAt$/)) {
    return moment(data).tz(state.context.timezone).format();
  }
  return data;
});

const context = {
  timezone: 'America/Los_Angeles',
};
const serialized = await scrubbr.serialize(data, 'PostList', context);

Try the example yourself

It's easy to try it yourself with the included example in example/index.ts. Just clone this repo, install the dependencies (npm install) and then run the example app with:

npm run example

FAQs

Package last updated on 15 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc