@tapsioss/tapi-client
v0.0.5
Published
### Installation
Downloads
73
Readme
@tapsioss/tapi-client
Installation
pnpm install @tapsioss/tapi-client
# or
npm install @tapsioss/tapi-client
# or
yarn add @tapsioss/tapi-client
Usage
import { Client } from "@tapsioss/tapi-client";
const client = new Client({
baseURL: "https://api.example.com/v1",
headers: { Authorization: "Bearer YOUR_TOKEN" },
});
// Basic usage
try {
const response = await client.request<{ content: string }[]>({
url: "/posts",
method: "GET",
});
console.log(response.data);
} catch (error) {
console.error("Request failed:", error);
}
// Error handling
try {
const response = await client.request({ url: "/data" });
} catch (error) {
// Triggered when there’s a network issue.
if (error instanceof NetworkError) {
console.error("Network issue:", error);
// Triggered when the server responds with an error (e.g., 404, 500).
} else if (error instanceof ServerError) {
console.error("Server issue:", error.response.status);
// Triggered when the request times out.
} else if (error instanceof TimeoutError) {
console.error("Request timed out");
// Base error class for all client-side errors.
} else {
console.error("An unexpected error occurred:", error);
}
}
// Setting request timeout
const response = await client.request({
url: "/slow-endpoint",
timeout: 3000, // Request will abort if it exceeds 3 seconds
});
// retry
const response = await client.request({
url: "/unstable-endpoint",
retry: {
attempts: 3,
delay: 1000, // 1 second delay between retries
condition: (error) => error instanceof NetworkError,
},
});