Strike - @mvdlei/zap
Strike class for making requests to a single endpoint.
Installation
npm install @mvdlei/zap
Overview
The Strike
class allows you to make requests to a single endpoint. It provides methods for common CRUD operations, including getting a single item, creating a new item, listing items, updating an item, and deleting an item.
Strike follows the Restful API conventions. So far no customization is allowed.
Differences from Zap
With the Strike
module you can define 1 model and make REST requests to it. With the Zap
module you can define 1 action and use that to make a specific request.
Usage
Under the hood, Strike
uses the zap
module to make requests. The zap
module is a wrapper around the fetch
API. It is recommended to read the zap documentation before using Strike
.
Creating a Strike Instance
import { Strike } from "@mvdlei/zap";
// There is no exported `strike` instance because we cannot guess the origin.
const strike = new Strike({
origin: "https://example.com",
});
Creating a Strike Request
const strikeRequest = strike.make(
"/users",
z.object({ id: z.string(), name: z.string() }),
{
new: {
// optional, will fallback to model above.
input: z.object({ name: z.string() }), // or any
output: z.object({ id: z.string(), name: z.string() }), // or any
},
},
);
Making Requests
const getItem = await strikeRequest.get("1");
console.log("Get Item:", getItem);
const newItem = await strikeRequest.new({ id: "2", name: "John Doe" });
console.log("New Item:", newItem);
const listItems = await strikeRequest.list({ limit: 10, offset: 0 });
console.log("List Items:", listItems);
const updatedItem = await strikeRequest.update(userId, { name: "Updated Name" });
console.log("Updated Item:", updatedItem);
await strikeRequest.delete(userId);
console.log("Item Deleted");
Suggestions
Grouping
With this module you can group your requests by endpoint. This is useful when you have multiple endpoints in your API.
import { Strike } from "@mvdlei/zap";
import { z } from "zod";
const s = new Strike({
origin: "https://jsonplaceholder.typicode.com",
});
const todos = s.make(
"/todos",
z.object({
userId: z.number(),
id: z.number(),
title: z.string(),
completed: z.boolean(),
}),
);
const router = {
todos,
//...other endpoints
};
const main = async () => {
const result = await router.todos.get("123");
console.log("result", result);
};
Example
import { Strike } from "@mvdlei/zap";
import { z } from "zod";
const s = new Strike({
origin: "https://jsonplaceholder.typicode.com",
});
const todos = s.make(
"/todos",
z.object({
userId: z.number(),
id: z.number(),
title: z.string(),
completed: z.boolean(),
}),
);
const main = async () => {
const getted = await todos.get("1");
console.log("Getted", getted);
const posted = await todos.new({ title: "test", completed: false, userId: 1 });
console.log("Posted", posted);
const listed = await todos.list({ limit: 2, offset: 3 });
console.log("Listed", listed);
const updated = await todos.update("1", { title: "test2" });
console.log("Updated", updated);
};
main();