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

aspida-mock

v0.11.1

Published

TypeScript friendly RESTful API mock for aspida

Downloads

3,442

Readme

aspida-mock

Features

  • API mock dedicated to TypeScript using the type definition of aspida.
  • Supports all HTTP methods such as GET/POST/PUT/DELETE in a few lines.
  • No server required, works only with browser.

Usage

Installation

  • Using npm:

    $ npm install aspida-mock @aspida/axios axios
    # $ npm install aspida-mock @aspida/fetch
    # $ npm install aspida-mock @aspida/node-fetch node-fetch
    # $ npm install aspida-mock @aspida/ky ky
  • Using Yarn:

    $ yarn add aspida-mock @aspida/axios axios
    # $ yarn add aspida-mock @aspida/fetch
    # $ yarn add aspida-mock @aspida/node-fetch node-fetch
    # $ yarn add aspida-mock @aspida/ky ky

Creating API endpoints

Export mockMethods in aspida type definition file.

api/users/index.ts

import { mockMethods } from "aspida-mock";

export type Methods = {
  post: {
    query: { id: number };
    reqHeaders: { val: string };
    reqBody: { name: string };
    resHeaders: { token: string };
    resBody: {
      id: number;
      name: string;
    };
  };
};

export default mockMethods<Methods>({
  post: ({ query, reqHeaders, reqBody }) => ({
    status: 200,
    resHeaders: { token: reqHeaders.val },
    resBody: {
      id: query.id,
      name: reqBody.name,
    },
  }),
});

package.json

{
  "scripts": {
    "build": "aspida && aspida-mock"
  }
}

tarminal

$ npm run build

index.ts

import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import api from "./api/$api";
import mock from "./api/$mock";

const client = process.env.NODE_ENV === "development" ? mock(aspidaClient()) : api(aspidaClient());

(async () => {
  const res = await client.users.post({
    query: { id: 0 },
    headers: { val: "hoge" },
    data: { name: "fuga" },
  });

  console.log(res);
  /*
  {
    status: 200,
    headers: { token: "hoge" },
    data: { id: 0, name: "fuga" }
  }
  */
})();

Middleware

For every request, you can insert processing before reaching mockMethods.

api/@middleware.ts

import { mockMiddleware } from "aspida-mock";

export default mockMiddleware([
  (req, _res, next) => {
    next({ ...req, query: { hoge: req.query.hoge + 1 } });
  },
  (req, res) => {
    res({ status: 200, resBody: { fuga: req.query.hoge + 2 } });
  },
]);

api/users/index.ts

import { mockMethods } from "aspida-mock";

export type Methods = {
  get: {
    query: { hoge: number };
    resBody: {
      fuga: number;
    };
  };
};

export default mockMethods<Methods>({
  get: ({ query }) => ({
    status: 200,
    resBody: { fuga: query.hoge + 4 },
  }),
});

index.ts

import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import mock from "./api/$mock";

const client = mock(aspidaClient());

(async () => {
  const res = await client.users.get({
    query: { hoge: 0 },
  });

  console.log(res);
  /*
  {
    status: 200,
    data: { fuga: 3 }
  }
  */
})();

Options

aspida-mock has several options available.

delayMSec: number

Simulate response delay.

import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import mock from "./api/$mock";

const client = mock(aspidaClient(), { delayMSec: 500 });

(async () => {
  console.time();
  await client.users.$get();
  console.timeEnd(); // default: 506.590ms
})();

log: boolean

Switch request log output.

import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import mock from "./api/$mock";

const client = mock(aspidaClient(), { log: true });

(async () => {
  await client.users.$get({ query: { bar: "baz" } });
  // [mock] get: /users?bar=baz => 200
})();

Cautions

.gitignore

Exclude $mock.ts generated by aspida-mock in the build from Git monitoring.

$ echo "\$mock.ts" >> .gitignore

# If Windows (Command Prompt)
> echo $mock.ts >> .gitignore

Command Line Interface Options

The following options can be specified in the Command Line Interface.

Configuration

aspida-mock refers to only "input" among the items of the aspida configuration file aspida.config.js.
This allows you to always generate a mock from the same directory as aspida.
Options of aspida.config.js

License

aspida-mock is licensed under a MIT License.