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

@ckpack/mock-axios

v0.0.6

Published

Mock axios requests for testing and development

Downloads

11

Readme

mock-axios

Intercept Axios requests and return Mock data for testing and development.

API

mockAxios

(mockDatas: MockConfig[], mockOptions?: MockOptions | undefined) => void: Mock Axios

  • mockDatas: an array, when axios requests, it will check the corresponding url, method, and return the corresponding response data when it matches.
    • url: string | RegExp type, the url to match, can be a string or a regular expression. When it is a string, it will check whether the url requested by axios contains the url. When it is a regular expression, it will check whether the url requested by axios matches the url.
    • method: GET | POST | PUT | DELETE | PATCH, default is empty to match all types of requests
    • timeout: number type, default value: 300, unit: ms, the delay time of returning the result
    • response: the data object of the returned mock
    • adapter: custom adapter function, refer to adapter, this function accepts two parameters, one is config, one is It is the matched mockData object, and the return value is the data object of response. Through this parameter, you can define the returned data more freely (such as verifying interface permissions).
  • mockOptions
    • isUseDefaultAdapter: Boolean, defaults to true, if enabled, requests that are not intercepted will be sent in the default mode of axios
    • isEffect: Boolean, default is true, with this parameter you can enable mockAxios in test environment and disable mockAxios in production environment
    • isLog: Boolean, the default is true, whether to print the request log of mockAxios
    • logger: Function, replace the default log function
import { mockAxios } from '@ckpack/mock-axios';

mockAxios([{
  url: 'https://test.com/v1/user/1',
  response: { 
    data: { 
      id: 1,
      name: 'admin',
    }, 
  },
}], {
  isEffect: process.env.NODE_ENV === 'development',
});

defineConfig

(mockDatas: MockConfig[]) => MockConfig[]: Helper function for constructing mockDatas. This function can be used with the IDE to get type hints

  • mockDatas: Same as the mockDatas parameter of mockAxios.
import { defineConfig } from '@ckpack/mock-axios';

const mockDatas = defineConfig([
  {
    url: 'https://test.com/v1/user/1',
    response: {
      data: {
        id: 1,
        name: 'admin',
      },
    },
  },
]);

Example

// mockAxios.js
import { mockAxios } from '@ckpack/mock-axios';

mockAxios([{
  method: 'GET',
  url: /https:\/\/test.com\/v1\/user\/\d+/,
  response: { 
    data: [{ 
      id: 1,
      name: 'admin',
    }], 
  },
}, {
  method: 'POST',
  url: 'https://test.com/v1/user/create',
  adapter: (axiosConfig) => {
    return {
      data: axiosConfig.data,
    };
  },
}]);

in other files

import axios from 'axios';
import './mockAxios.js';

await axios.get('https://test.com/v1/user/1');
// return { data: [{ id: 1, name: 'admin' }] }

await axios.post('https://test.com/v1/user/create', {
  id: 1,
  name: 'admin',
});
// return { data: { id: 1, name: 'admin' } }
await axios.post('https://test.com/v1/user/create', {
  id: 2,
  name: 'test',
});
// return { data: { id: 2, name: 'test' } }