Socket
Socket
Sign inDemoInstall

class-serialisation-helper

Package Overview
Dependencies
1
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    class-serialisation-helper

Decorators that helps you serialise your response and request


Version published
Weekly downloads
7
Maintainers
1
Install size
5.86 kB
Created
Weekly downloads
 

Readme

Source

class-serialisation-helper

Decorators that helps you serialise your response and request

Installation

yarn add class-serialisation-helper
npm i class-serialisation-helper

Documentation

  • Serialise
  • SerialiseParam

To enable serialisation, put the Serialise decorator on the class method, e.g.

Serialise response

import { Serialise } from "class-serialisation-helper";

class App {
  @Serialise()
  hello() {
    return "hello";
  }
}

By default, Serialise will have no effect on the function's response.

To serialise the response, add a converter function in the Serialise decorator, i.e.

const HelloResponseConverter = (v: string) => ({ message: "hello" });

class App {
  @Serialise(HelloResponseConverter)
  hello() {
    return "hello";
  }
}

// the response will become an object
expect(new App().hello()).toEqual({ message: "hello" });

Serialise parameters

To serialise a parameter, we will need SerialiseParam, e.g.

import { Serialise, SerialiseParam } from "class-serialisation-helper";

type Request = {
  name: string;
  date: Date;
};

const HelloRequestConverter = (name: string) => {
  // you can also validate the input if you like
  if (typeof name !== "string") {
    throw new Error(`"name" is not a string`);
  }
  return { name, date: new Date() };
};

class App {
  @Serialise(HelloResponseConverter)
  hello(@SerialiseParam(HelloRequestConverter) req: Request) {
    return `hello, ${req.name} at ${req.date.toLocaleString()}`;
  }
}

expect(new App().hello("sam")).toEqual(
  expect.objectContaining({
    message: expect.any(String),
  })
);

API

Serialise

Work in progress

SerialiseParam

Work in progress

Contributions

PRs are always welcome :)

FAQs

Last updated on 14 Sep 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc