@tapsioss/tapi-core
v0.0.1
Published
### Installation
Downloads
66
Readme
@tapsioss/tapi-core
Installation
pnpm install @tapsioss/tapi-core @tapsioss/tapi-client
# or
npm install @tapsioss/tapi-core @tapsioss/tapi-client
# or
yarn add @tapsioss/tapi-core @tapsioss/tapi-client
Usage
import { Core } from "@tapsioss/tapi-core";
import { Client } from "@tapsioss/tapi-client";
// Define the service factory
const postServiceFactory = (client: Client) => ({
getPost(id: string) {
return client.request<{ id: string; content: string }>({
url: `https://api.example.com/v1/posts/${id}`,
});
},
getPosts(query: { page: number }) {
return client.request<{ id: string; content: string }[]>({
url: "https://api.example.com/v1/posts",
query,
});
},
createPost(body: { content: string }) {
return client.request<{ id: string; content: string }>({
method: "POST",
url: "https://api.example.com/v1/posts",
body,
});
},
});
// Extend Core to include the `post` service
const Api = Core.with("post", postServiceFactory);
const api = new Api();
const created = await api.post.createPost({
content: "This is a new post",
});
console.log("Created Post:", created);
const post = await api.post.getPost(created.data?.id!);
console.log("Fetched Post:", post);
const posts = await api.post.getPosts({ page: 1 });
console.log("All Posts:", posts);
// Adding Multiple Services
const Api = Core.with("post", postServiceFactory)
.with("user", userServiceFactory)
.with("comments", commentServiceFactory);
const api = new Api();
// Using a custom Client
const client = new Client({
baseURL: "https://api.example.com/v1",
});
const api = new Core(client);