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

@tramvai/module-mocker

v4.41.39

Published

Module uses library [@tinkoff/mocker](references/libs/mocker.md) to add a mocker functionality to the `tramvai` app. Mocker will be available as [papi](references/modules/server.md#papi) route with path `/mocker`.

Downloads

2,183

Readme

@tramvai/module-mocker

Module uses library @tinkoff/mocker to add a mocker functionality to the tramvai app. Mocker will be available as papi route with path /mocker.

Installation

First, install @tramvai/module-mocker:

npm install @tramvai/module-mocker

Then, add your first mock to a new file mocks/my-api.js. In this file add export of object literal with the field api that should be specified as a name of environment variable for the API url that should be mocked:

module.exports = {
  api: 'MY_API',
  mocks: {
    'GET /endpoint?foo=bar': {
      status: 200,
      headers: {},
      payload: {
        result: {
          type: 'json',
          value: {
            a: 'b',
          },
        },
      },
    },
  },
};

Add module to the project:

import { createApp } from '@tramvai/core';
import { MockerModule } from '@tramvai/module-mocker';

createApp({
  name: 'tincoin',
  modules: [MockerModule],
});

Run app with env MOCKER_ENABLED, e.g.:

MOCKER_ENABLED="true" tramvai start tincoin

After that, all of the requests to MY_API in browser and on server will be automatically sent to mocker. In case mocker doesn't have a suitable mock the request, the request will be proxied to the original API.

Explanation

Most of the mocker features are described in the lib documentation.

Module adds mocker middleware to papi route /mocker and replaces all of the env variables that were defined in mocks by links to the papi. After that all of the request to the original API are routed first to mocker that accepts requests from the client and the server side.

By default, all of the API that were defined mocks are mocked, but it might be overridden.

Mocker us enabled only when env variable MOCKER_ENABLED is defined.

Env variables replacement

Let's say app has env variable MY_API: https://www.my-api.com/ and for that api some mock is defined.

The module can work locally, on dynamic stand, in test/stage environments. But this flexibility leads to the following problems when resolving path to the papi endpoint:

  1. On server we should execute requests with absolute path. In this case we know that app is always available at localhost that mean we can replace API env variables by urls like http://localhost:3000/tincoin/papi/mocker/MY_API/
  2. On client test stands we do not known the domain of the app. In this case we should make requests by relative urls that mean we can replace API env variables by urls like /tincoin/papi/mocker/MY_API/

Thanks to this env replacement we can redirect all of the request to the APIs to our mocker first automatically.

How to

Mock only specific API

By default, all of the API that has corresponding mock will be mocked. It might be overridden by passing list of the APIs to mock when initializing module:

MockerModule.forRoot({
  config: async () => ({
    apis: ['MY_API'],
  }),
});

Exported tokens

@inline src/tokens.ts