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

@moneko/mock

v2.0.0

Published

mock

Downloads

22

Readme

Mock

在项目启动目录 mock 文件夹下创建mock脚本,达到模拟响应数据的效果

函数方式

(req: RequestFormData, res: Response) => void

import type { MockConfiguration } from '@moneko/mock';

const conf: MockConfiguration = {
  'GET /api/account': (req, res) => {
    const resp = {
      success: true,
      message: '请求成功',
      result: {
        inme: '人大',
        effte: '2022/7/1',
      },
    };
    res.status(200).send(resp);
  },
};

export default conf;

获取Restful Api参数

import type { MockConfiguration } from '@moneko/mock';

const conf: MockConfiguration = {
  'GET /api/account/:id': (req, res) => {
    const resp = {
      success: true,
      message: '请求成功',
      result: {
        id: req.params.id,
        csaa: '人额',
        currency: req.params.id,
        effee: '2022/7/1',
      },
    };
    res.status(200).send(resp);
  },
};

export default conf;

获取POST请求体数据

import type { MockConfiguration } from '@moneko/mock';

const conf: MockConfiguration = {
  'POST /api/login_by_username': (req, res) => {
    const resp = {
      status: 200,
      message: '请求成功',
      result: {
        password: req.body.password,
        username: req.body.username,
      },
    };

    res.status(resp.status).send(resp);
  },
};

export default conf;

模拟上传文件,返回文件base64

import type { MockConfiguration } from '@moneko/core';

const conf: MockConfiguration = {
  'POST /api/upload_file': (req, res) => {
    const { files } = req;

    const strBase64 = Buffer.from(files[0].buffer).toString('base64');

    const resp = {
      status: 200,
      message: '上传成功',
      result: 'data:image/jpeg;base64,' + strBase64,
    };

    res.status(resp.status).send(resp);
  },
};

export default conf;

使用 YApi JSON Schema 生成数据

import { yApiSchemaMock } from '@moneko/mock';
import type { MockConfiguration } from '@moneko/mock';

const getYApiOption = (id: string) => {
  return {
    // yapi host
    host: 'http://yapihost:8080',
    // yapi open api token
    token: 'yapi open api token',
    // yapi 接口id
    id,
  };
};

const conf: MockConfiguration = {
  'POST /getids/list':  async (req, res) => {
    const mockData = await yApiSchemaMock(getYApiOption('7610'), {
      result: {
        page: req.body.pageNum,
        itemsPerPage: req.body.pageSize,
        keyword: req.body.keyword,
      },
    });

    res.status(200).send(mockData);
  },
};

export default conf;

使用 YApi 高级Mock

import { yApiMock } from '@moneko/mock';
import type { MockConfiguration, YApiOption } from '@moneko/mock';

const yApi: YApiOption = {
  // yapi host
  host: 'http://yapihost:8080',
  projectId: 143,
  pathRewrite: '^/api/',
};

const conf: MockConfiguration = {
  'POST /api/getids/list':  async (req, res) => {
    // 此时 mockData 将是来自 POST http://yapihost:8080/mock/143/getids/list 响应的数据
    const resp = await yApiMock(req, yApi);

    res.status(200).send(resp);
  },
};

export default conf;

KV方式 Record<string, any>

import type { MockConfiguration } from '@moneko/mock';

const conf: MockConfiguration = {
  'POST /api/use/fun': {
      status: 200,
      message: '上传成功',
      effectiveDate: '2022/7/1',
  },
};

export default conf;