1.0.0 • Published 4 years ago
pett v1.0.0
Pett
Simple fake HTTP requests ✈️
Install
$ npm install --save pett
Example
import Pett from "pett";
const http = new Pett({
store: { users: [] },
delay: 1000
});
http.mock("POST", "/users", (store, ctx) => {
const { authorization } = ctx.headers;
if (authorization !== "Bearer token") {
throw new Error("Token not provided");
}
store.users.push(ctx.body);
});
http.mock("GET", "/", (store, ctx) => {
const { page = 1, limit = 5 } = ctx.query;
return store.users.slice((page - 1) * limit, page * limit)
});
http.mock("GET", "/users/:username", (store, ctx) => {
const { username } = ctx.params;
const result = store.users.find(user => user.username === username);
if (!result) {
throw new Error("User not found");
}
return result;
});
...
async function main() {
try {
await http.post("/users", {
body: { id: 1, username: "lex" },
headers: { authorization: "Bearer token" }
});
const users = await http.get("/users?page=1&limit=10");
console.log(users);
const user = await http.get("/users/lex");
console.log(user);
} catch (error) {
console.log(error);
}
}
1.0.0
4 years ago