npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

request-payload-factory

v1.0.0

Published

It is inspired by and based on nestjs Seeder with additional operations to generate payloads API testing

Downloads

5

Readme

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")
            })
    });