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

@klient/mock

v1.1.2

Published

Mock Klient requests with fake responses

Downloads

3

Readme

Klient Mock

badge-coverage

Introduction

This Klient extension allows you to mock Klient responses. That's means that you can work on an entrypoints even if it's not really ready yet.

Setup

Install package with your favorite package manager :

# With NPM
$ npm install @klient/mock

# With YARN
$ yarn add @klient/mock

Then import the extension in your code :

import Klient from '@klient/core';

//
// Register extension
//
import '@klient/mock';


//
// Build Klient instance
//
const klient = new Klient('...');


//
// See loaded extension
//
console.log(klient.extensions); // Print ["@klient/mock"]

Usage

import Klient from '@klient/core';

//
// Register extension
//
import '@klient/mock';


//
// Build Klient instance
//
const klient = new Klient(...);


//
// Mock a route - see Mock method doc for more details
//
klient.mock({
  req: { url: '/posts' },         // Request criterias
  res: { status: 200, data: [] }  // Mocked response
});


//
// Consume a mocked route
//
klient.request('/posts').then(response => {
  console.log(response.data); // Print []
});

Parameters

const klient = new Klient({
  delay: 800, // Default response delay to apply to all responses (milliseconds)
  mock: {
    // This parameter is analyzed only on Klient instanciation 
    load: [
      // Mock GET /posts
      {
        delay: 800,  // Simulate response delay (milliseconds)
        req: { url: '/posts' },
        res: { status: 200, data: [] }
      },
    ]
  }
});

Mock

A mock is a fake response build for a request whose matches some criterias.

The mocked response must be an object contening at least the "status" property. It must contains only Axios object properties, that will be "normalized" by mock extension to a final Axios result. The returned status code determines if the request must be rejected or resolved (as Axios does).

To match a mock, the request config must contain all properties/values defined in criterias (it's not strict, request config can contains more properties not defined in criterias). The mock request url can contains dynamic parameters whoses will be treated separately of the rest of config, then they will be injected in mock response callback if match.

mock({
  req: AxiosRequestConfig,
  res: AxiosResponse | ((config: AxiosRequestConfig, parameters: object) => AxiosResponse)
}): Klient

// Can be multiple
mock(...Mocks): Klient

Example

import Klient from '@klient/core';

//
// Register extension
//
import '@klient/mock';


//
// Build Klient instance
//
const klient = new Klient(...);


//
// Mock a route
//
klient.mock({
  req: { url: '/posts' },         // Request criterias
  res: { status: 200, data: [] }  // Mocked response
});


//
// Mock multiple routes
//
klient.mock(
  {
    req: { url: '/posts', method: 'GET' },
    res: { status: 200, data: [] }
  },
  {
    req: { url: '/posts', method: 'POST' },
    res: { status: 400, data: {...} }
  }
);


//
// Mock complexe routes
//
klient.mock({
  delay: 600, // (milliseconds)
  req: { url: '/posts', method: 'POST' },
  // Use a callback to make complex response
  res: (config) => {
    // Simulate API validation errors
    if (!config.data.title) {
      return { status: 400, data: {...} };
    }

    return { status: 200, data: {...} };
  }
});

//
// Mock routes with params
//
klient.mock({
  req: { url: '/posts/{id}', method: 'GET' },
  // Use a callback to make complex response
  res: (config, parameters) => {
    // Simulate API not found
    if (parameters.id !== '1') {
      return {
        status: 404,
        data: {...}
      };
    }

    return { status: 200, data: {...} };
  }
});