Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

miragejs

Package Overview
Dependencies
Maintainers
3
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

miragejs

A client-side server to help you build, test and demo your JavaScript app

  • 0.1.48
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
236K
decreased by-16.12%
Maintainers
3
Weekly downloads
 
Created

What is miragejs?

MirageJS is a client-side server to help you build, test, and share your JavaScript application. It allows you to mock APIs and create a simulated backend environment, which can be very useful for frontend development and testing.

What are miragejs's main functionalities?

Mocking API Endpoints

MirageJS allows you to mock API endpoints easily. In this example, a GET request to '/api/users' returns a list of users.

const { createServer } = require('miragejs');

createServer({
  routes() {
    this.get('/api/users', () => {
      return {
        users: [
          { id: 1, name: 'Alice' },
          { id: 2, name: 'Bob' }
        ]
      };
    });
  }
});

Defining Models and Factories

MirageJS allows you to define models and factories to generate mock data. This example creates a 'user' model and a factory to generate 10 users with names 'User 1' to 'User 10'.

const { createServer, Model, Factory } = require('miragejs');

createServer({
  models: {
    user: Model
  },

  factories: {
    user: Factory.extend({
      name(i) {
        return `User ${i + 1}`;
      }
    })
  },

  seeds(server) {
    server.createList('user', 10);
  },

  routes() {
    this.get('/api/users', (schema) => {
      return schema.users.all();
    });
  }
});

Handling Relationships

MirageJS supports defining relationships between models. In this example, a 'user' has many 'posts' and a 'post' belongs to a 'user'.

const { createServer, Model, hasMany, belongsTo } = require('miragejs');

createServer({
  models: {
    user: Model.extend({
      posts: hasMany()
    }),
    post: Model.extend({
      user: belongsTo()
    })
  },

  seeds(server) {
    let user = server.create('user', { name: 'Alice' });
    server.create('post', { title: 'Post 1', user });
    server.create('post', { title: 'Post 2', user });
  },

  routes() {
    this.get('/api/users', (schema) => {
      return schema.users.all();
    });
    this.get('/api/posts', (schema) => {
      return schema.posts.all();
    });
  }
});

Other packages similar to miragejs

Keywords

FAQs

Package last updated on 30 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc