0.0.1 • Published 2 years ago

@sandeep3180/prototyped v0.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Prototyped Node.js CI

A library to create mock APIs for UI prototypes.

Installing

npm install prototyped

OR

yarn add prototyped

Usage

Initializing

import { createApi } from 'prototyped';

interface User {
  id: string;
  name: string;
  email: string;
}

const userApi = createApi<User>();

The userApi object can be used as an abstraction for the backend. It has methods that use the Promise API to handle data.

Using the API

const allUsers = await userApi.all();

const newUser = await userApi.post({
  id: '1',
  name: 'John Doe',
  email: 'john.doe@iamadonut.com',
});

// This is a custom filter function for the User interface.
const getUser = (id: string) => (user: User) => user.id === id;

const oneUser = await userApi.get(getUser('1'));

const updateUser = await userApi.put(getUser('1'), { name: 'Jane Doe' });

const deletedUser = await userApi.remove(getUser('1'));

Working with Initial Mock Data

If you have initial mock data that you want to use, it can be passed in while creating the API object.

const mockUsers: User[] = [
  {
    id: '1',
    name: 'Jane Doe',
    email: 'jane.doe@iamadonut.com',
  },
];

const userApi = createApi<User>(mockUsers);

The API object will add the mock data to its internal storage.

const users = await userApi.all(); // users.length = 1