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

express-middleware-mock

v0.5.2

Published

Middleware to mock requests using jsons and semantic folders on express

Downloads

643

Readme

express-middleware-mock

Middleware to mock requests using jsons and semantic folders on express

Installation

npm i express-middleware-mock

Usage

import express, { Request, Response, NextFunction } from "express";
import cors from "cors"; //optional
import mockJson from "express-middleware-mock";

const USE_MOCK = process.env.USE_MOCK || false;
const MOCK_FOLDER = process.env.MOCK_FOLDER || './jsons';

const app = express();
app.use(cors());
app.use(express.json());

//here your router
const router = express.Router();
router.get(
  "/my/router/example",
  async (req: Request, res: Response) => {
    res.status(200).send({
      success: true,
      messages: ["ok"]
    })
  }
);

if (USE_MOCK) {
  //if enter here
  app.use(mockJson(MOCK_FOLDER));
}

Creating Requests and Responses

To create requests and responses you need to create a folder with semantic name as a request path, and put two files request.json and response.json inside OR you can create just a single file named redirect.json

request.json file

Request file simbolise what do you need to receive as query params, body or parameter, and the syntax need to be:

{
  "params": {
    "someValueFromQueryParams": true
  },
  "header":{
    "someHeadersParamsHere": true
  },
  "body":{
    "myPostParamHere": true
  },
  "query":{
    "myQueryParamHere": true
  }
}
  • body: for params inside a body request (not work for GET methods)
  • query: for params on url, after ? simble, like ?skip=0
  • params: it is not possible to check url params yet

Examples:

To recieve userId and userEmail on post method

{
    "body": {
        "userId": true,
        "userEmail": true
    }
}

To recieve skip and limit on get method as url params, but not optionaly, and category need to be setted

{
    "query": {
        "skip": false,
        "limit": false,
        "category": true
    }
}

response.json file

Just need to be a valid json file and they will be used in body result.

The middleware will be put automatic properties to indentify this result as a fake result

{
  isRedirected: true,
  fakeResult: true
}

And if you put some values that exists on faker as a method or inside a objectHandler as a methodName, the value will be put with a method result.

You also can create a response using a list response pattern like this:

| tip: If you use this struture to result list, the fake result layer will simulate a list of result

{
  "total": 405,
  "skiped": 0,
  "limited": 10,
  "list": [
    {
      "id": "uuid",
      "name": "name.fullName",
      "description": "lorem",
      "otherThing": "{query.skip}"
    }
  ]
}

And they will be simulate 405 results on database to create a pagination system. Notice that you just need to put 1 object inside an array and they will use this as a pattern to create others.

You can response asingle result:

{
  "id": "uuid",
  "category": "that|this|other"
}

| tips: if you put pipe | to split string, they will random someone to result

Create your own fake result

You can create your own result handler

const myHandler = {
  mySecret:()=>{
    return "my secret word here"
  }
}

if (USE_MOCK) {
  //using your own handler
  app.use(mockJson(MOCK_FOLDER, myHandler));
}

| tip: Use relative path with ./ or absolute path

And now you can use:

{
  "category": "that|this|other",
  "test" : "mySecret"
}

The result will be:

{
  "category": "other",
  "test" : "my secret word here"
}

Folder Exemple

.
 /mock
    /test
      /a
        request.json
        response.json
    /my
      /router
        /example
          request.json
          response.json
        /example-2
          request.json
          response.json
          errors.json
    /other
      /[paramName] <- this variable goes to params variable as paramName
        /test
    /api
      /users
        /list
          redirect.json

We have 3 examples here and all of them inside a 'jsons' folder.

  • test/a using request/response files
  • my/router/example using request/response files BUT using a existed router path
  • my/router/example-2 using request/response and chance to result error
  • api/users/list using redirect.json file