class-serialisation-helper
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

class-serialisation-helper

Decorators that helps you serialise your response and request

Installation

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

Documentation

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 :)

Readme

Keywords

none

Package Sidebar

Install

npm i class-serialisation-helper

Weekly Downloads

1

Version

0.0.8

License

MIT

Unpacked Size

6 kB

Total Files

6

Last publish

Collaborators

  • samsan1212