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

@turquoise/mock

v1.1.3

Published

- [@ turquoise/mock](#-turquoisemock) - [installation](#installation) - [Custom model](#custom-model) - [File structure](#file-structure) - [Custom routing](#custom-routing) - [Extended random generator](#extended-random-generator) - [Middlewa

Downloads

12

Readme

@ turquoise/mock

installation

# Use npm
npm i @ turquoise/mock -D

# Use the Yarn
yarn add turquoise/mock -D

Custom model

Sample file schema/api.ts, mock rules can find at mockjs

export default {
  'user|100': [
    {
      'id|+1': 1,
      name: '@cname',
      age: 4,
      mobile: '@mobile',
      createdAt: '@datetime',
      'status|1': ['enabled', 'disabled'],
    },
  ],
};

File structure

Project file structure that must be followed

|-- mock
    |-- routes // custom routes
    |-- schemas // schamas
    |-- random // extend mockjs Generator
    |-- middlewares // express middlewares
|-- .mockrc.ts // configuration

Custom routing

Sample file routes/user.ts

import { Request, Response } from 'express';

export default [
  {
    path: '/me',
    controller: (req: Request, res: Response): void => {
      res.json({
        mobile: '@mobile',
        username: 'xiaoming',
        role: ['admin', 'test'],
      });
    },
  },
];

Extended random generator

Sample file random/ext.ts

import Mock from 'mockjs';

export default {
  mobile(): string {
    return Mock.mock(/^1(9|3|4|5|7|8)[0-9]{9}$/);
  },
  lon(): number {
    return Mock.Random.float(121.140308, 121.82558, 7, 8);
  },
  lat(): number {
    return Mock.Random.float(30.853426, 31.363719, 7, 6);
  },
};

Middleware

Sample file middlewares/query.ts

import { Request, Response, NextFunction } from 'express';

module.exports = [
  function (req: Request, res: Response, next: NextFunction): void {
    if (req.method === 'PUT') {
      req.method = 'PATCH';
    }

    console.log('before hook');

    next();

    console.log('after hook');
  },
];

Launch configuration

Read the project root directory by default .mockrc.ts

| Field | Types of | Defaults | description | | -------- | -------- | ------------------------ | ---------------------------------- | | port | number | 3000 | Service port | | delay | number | 0 | Simulated network latency | | rewriter | object | {"/ api / *": "/ $ 1"} | Rewrite routing | | render | function | - | json-serverIn the rendermethod |

export default {
  // change port
  port: 3002,
};

Usage example

In package.json use in

{
   "scripts" : {
     "mock" : "turquoise-mock"
  }
}

start a mock server

# Use npm
npm run mock

# Use the Yarn
yarn run mock

REST API Routes

Based on the previous schema/api.ts , here are all the default routes.

reference json-server

Plural routes

GET    /users
GET    /users/1
POST   /users
PUT    /users/1
PATCH  /users/1
DELETE /users/1

Singular routes

GET    /profile
POST   /profile
PUT    /profile
PATCH  /profile

Filter

Use . to access deep properties

GET /users?title=json-server&author=typicode
GET /users?id=1&id=2
GET /comments?author.name=typicode

Paginate

Use _page and optionally _limit to paginate returned data.

In the Link header you'll get first, prev, next and last links.

GET /users?_page=7
GET /users?_page=7&_limit=20

10 items are returned by default

Sort

Add _sort and _order (ascending order by default)

GET /users?_sort=views&_order=asc
GET /users/1/comments?_sort=votes&_order=asc

For multiple fields, use the following format:

GET /users?_sort=user,views&_order=desc,asc

Slice

Add _start and _end or _limit (an X-Total-Count header is included in the response)

GET /users?_start=20&_end=30
GET /users/1/comments?_start=20&_end=30
GET /users/1/comments?_start=20&_limit=10

Works exactly as Array.slice (i.e. _start is inclusive and _end exclusive)

Operators

Add _gte or _lte for getting a range

GET /users?views_gte=10&views_lte=20

Add _ne to exclude a value

GET /users?id_ne=1

Add _like to filter (RegExp supported)

GET /users?title_like=server

Full-text search

Add q

GET /users?q=internet

Relationships

To include children resources, add _embed

GET /users?_embed=comments
GET /users/1?_embed=comments

To include parent resource, add _expand

GET /comments?_expand=post
GET /comments/1?_expand=post

To get or create nested resources

GET  /users/1/comments
POST /users/1/comments

Database

GET /db

Homepage

Returns default index file or serves ./public directory

GET /