1.0.7 • Published 3 years ago
wah v1.0.7
Wah
Fake HTTP requests
Install
$ npm install --save wah
Usage
import Wah from "wah";
const http = new Wah({
store: { users: [{ id: 1, username: "john" }] },
delay: 1500
});
http.mock("GET", "users/:username", (store, request) => {
const { username } = request.params;
const data = store.users.find(user => user.username === username);
if (!data) {
throw new Error("User not found");
}
return data;
});
http.mock("POST", "users", (store, request) => {
const data = {};
data.id = store.users.length + 1;
Object.assign(data, request.body);
return data;
});
http
.get("users/john")
.then(user => console.log(user)) // { id: 1, username: "john" }
.catch(error => console.log(error.message)); // User not found
http
.post("users", { body: { username: "don" } })
.then(user => console.log(user)); // { id: 2, username: "don" }