@sandeep3180/prototyped
v0.0.1
Published
Build UIs without worrying about APIs existing. Works great with redux.
Downloads
2
Readme
Prototyped
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: '[email protected]',
});
// 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: '[email protected]',
},
];
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