Skip to content

avinashcodelabs/mock-api-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock-api-server

Installtion

npm install @avinashcodelabs/mock-api-server

Setup

At the entry point of the app, for React apps, the index.js file is preferred.

# index.js

import { startServer } from "@avinashcodelabs/mock-api-server";
startServer();

API usage

/**
 * 
 * @returns [
  {
    id: 1,
    name: "text",
    description: "text",
    price: 10.5,
    imgUrl: "url",
  },
  {
    id: 2,
    name: "text",
    description: "text",
    price: 10,
    imgUrl: "url",
  },
]
 */
function getProducts() {
  return fetch("http://api.avinashcodelabs.com/api/products")
    .then((res) => res.json())
    .then((products) => {
      return products;
    });
}

/**
 * 
 * @param {number} id 1to5 
 * @returns {
    id: 1,
    name: "text",
    description: "text",
    price: 10.5,
    imgUrl: "url",
  }
 */
function getProduct(id) {
  return fetch(`http://api.avinashcodelabs.com/api/products/${id}`)
    .then((res) => res.json())
    .then((product) => product);
}

/**
 * 
 * @param {object} p  {
    name: "text",
    description: "text",
    price: 10.5,
    imgUrl: "url",
  }
 * @returns  {
    id: 1,
    name: "text",
    description: "text",
    price: 10.5,
    imgUrl: "url",
  }
 */
function createProduct(p) {
  return fetch(`http://api.avinashcodelabs.com/api/products`, {
    method: "POST",
    body: JSON.stringify(p),
  }).then((res) => res.json());
}

/**
 * 
 * @param {object} p  {
    id: 1,
    name: "text",
    description: "text",
    price: 10.5,
    imgUrl: "url",
  }
 * @returns {
    id: 1,
    name: "text",
    description: "text",
    price: 10.5,
    imgUrl: "url",
  }
 */
function updateProduct(p) {
  return fetch(`http://api.avinashcodelabs.com/api/products/${p.id}`, {
    method: "PUT",
    body: JSON.stringify(p),
  })
    .then((res) => res.json())
    .then((data) => data);
}

/**
 *
 * @param {number} id 1to5
 * @returns null
 */
function deleteProduct(id) {
  return fetch(`http://api.avinashcodelabs.com/api/products/${id}`, {
    method: "DELETE",
  }).then((d) => d);
}

export { getProducts, getProduct, createProduct, updateProduct, deleteProduct };

Tech Stack

Built with:

  • JavaScript
  • miragejs

Authors

License

MIT

About

Fake REST APIs for CURD operation for any JavaScript applications

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published