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

@xlou/simulate

v1.0.8

Published

A tool for simulating backend API services in the frontend.

Downloads

6

Readme

Language

Introduction

  • A pure front-end tool for simulating backend API interfaces.
  • Supports XMLHttpRequest and fetch requests.
  • Can be used in Node.js projects after bundling.

Usage

Using Script Tags

<script src="https://unpkg.com/@xlou/[email protected]/dist/umd/simulate.min.js"></script>
<!-- It is recommended to download and use the JS file locally -->
<script>
  /* After including this JS file, a Simulate object will be assigned to the window */
  Simulate.serve({
    "/updateById": {
      type: 'post',
      response({ data }) {
        return {
          code: 200,
          data: {
            id: data.id
          }
        };
      }
    }
  });
</script>

Using in Node.js and Modular Projects

Installation

npm i @xlou/simulate -S

Import

import { serve } from '@xlou/simulate';

serve({
  "/getById": {
    type: 'get',
    response({ params }) {
      return {
        code: 200,
        data: {
          id: params.id
        }
      };
    }
  },
  "/updateById": {
    type: 'post',
    response({ data }) {
      return {
        code: 200,
        data: {
          id: data.id
        }
      };
    }
  }
});

API

serve

Define API interfaces.

interface SetConfig {
  getConfig: () => SimulateConfig;
  setConfig: (obj: SimulateConfig) => void;
}
interface SimulateConfig {
  wait: number;
}
const serve: ((obj: object) => void) | SetConfig;

Usage

/* Configure API interfaces */
serve({
  "/getById": { // Set the request path
    type: 'get', // Set the request type, e.g., post, get
    response({ url, type, params, data }) {
      /* 
        You can access the request parameters through the input parameters
        url: Request URL
        type: Request type
        params: URL parameters of the request
        data: Request body parameters (usually available for POST requests)
      */
    }
  }
});

/* Query and configure Simulate */
serve.setConfig({
  wait: 1000 // Set the response time for requests to 1 second
});
serve.getConfig(); // Get configuration information
// { wait: 1000 }

int

Generate a random integer with a specified number of digits.

int: (n: string | number) => number

int(3); // Generate a random integer with 3 digits

fixed

Generate a random decimal number with the option to specify the number of integer and decimal digits. The default number of decimal digits is 2.

fixed: (n: string | number, f?: string | number) => string

fixed(8, 3); // Generate a random decimal number with 8 integer digits and 3 decimal digits

id

Generate a unique and non-repeating random string.

id: () => string

id();

img

Generate a random grid image and return it in Base64 format. You can specify the width, height, and color. If width and height are not specified, they default to 512, and the color is randomly generated.

img: (width?: number, height?: number, color?: string) => string

img(256, 256, '#f00'); // The color supports hexadecimal strings and RGB function strings

img()