request-payload-factory

1.0.0 • Public • Published

Request Factory

About

This library is inspired by and based on nestjs Seeder (MIT) with customizations on Factory to support payloads generation for API testing. The customization includes adding few operations to remove, add and edit elements from payloads as required.

How to Use?

1- Install the package

npm install request-payload-factory --save-dev

2- Define Model class

The model class will represent the payload you want to generate. As an example, our model class will be "User", but it can be any custom class.

import { Factory } from "request-payload-factory";

export class User {
    @Factory("User Name")
    name: string;
    
    @Factory("0512345678")
    phonenumber: string;
    
    @Factory("Main st. B1")
    address: string;
}

Notice that @Factory decorator is used to specify the value for each field. This value will be used when generating payload for the class.

@Factory supports multiple types:

Static value:

The value can be of any of the basic types string, number, boolean or date

    @Factory("User Name")
    name: string;

Faker generated value:

    @Factory((faker) => faker.phone.imei())
    phonenumber: string;

Custom function:

    @Factory(() => {
      const st = "Main st."
      const buildingNum = "B1"
      return `${st} ${buildingNum}`;
    })
    address: string;

3- Generating payload for a class:

Using createForClass to create generate() function that will return the complete payload based on @Factory decorator.

import { RequestFactory } from 'request-payload-factory';

const requestPayload = RequestFactory.createForClass(User).generate({});

4- Customizing generated payload:

generate() function takes an optional parameter to specify the specs for the payload. Specs are set of defined rules to delete, add, or override a field in the generated payload.

There are 3 types of operations

  1. Delete
    • Takes no parameters
  2. Override
    • Takes one parameter to specify the new value
  3. Add
    • An alias to override and behaves exactly the same

Example:

import { Operations, RequestFactory } from 'request-payload-factory';

const requestPayload = 
  RequestFactory.createForClass(User).generate({
    name: Operations.Override("Another Name"),
    phoneNumber: Operations.Delete,
    workAddress: Operations.Add("Another st Floor 5"),
  });

Advanced Usage

Field of type of another custom Class

We will refer to the following two classes as an example for advanced usage, but you can apply it to any custom class:

Example 1:

import { Operations, RequestFactory } from 'request-payload-factory';

export class Trip {
    @Factory("Trip Name")
    name: string;
    
    @Factory("Trip organizer name")
    organizer: string;
    
    @Factory([
    RequestFactory.createForClass(Attraction).generate({})
    ])
    attraction: Attraction[];
}

export class Attraction {
    @Factory("Attraction Name")
    name: string;
    
    @Factory("Details of the attraction goes here")
    details: string;
}

const requestPayload = RequestFactory.createForClass(Trip).generate({});

Example 2:

In example 1, Attraction could have been modified as well be generation specs

import { Operations, RequestFactory } from 'request-payload-factory';

export class Trip {
    @Factory("Trip Name")
    name: string;
    
    @Factory("Trip organizer name")
    organizer: string;
    
    @Factory([
    RequestFactory.createForClass(Attraction).generate({})
    ])
    attraction: Attraction[];
}

export class Attraction {
    @Factory("Attraction Name")
    name: string;
    
    @Factory("Details of the attraction goes here")
    details: string;
}

const requestPayload = 
    RequestFactory.createForClass(Trip).generate({
        attraction: Operations.Override(
            RequestFactory.createForClass(Attraction).generate({
                location: Operations.Add("somewhere"),
                details: Operations.Delete
                advices: Operations.Override("some advices goes here")
            })
    });

Package Sidebar

Install

npm i request-payload-factory

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

21 kB

Total Files

26

Last publish

Collaborators

  • majsl243