ApiService is a class that provides a simple and consistent interface for making HTTP requests to REST APIs. It supports all common HTTP methods (GET, POST, PUT, PATCH, DELETE), automatic header management, token-based authentication, and optional debug logging.ts
import { ApiService } from "@arkyn/server";
baseUrl (required): The base URL for all API requests. This will be prepended to all endpoint paths.baseHeaders (optional): Default headers to include in every request. These can be overridden by request-specific headers.baseToken (optional): A default Bearer token for authorization. This can be overridden by request-specific tokens.enableDebug (optional): Enable debug logging for requests. When true, request details will be logged using flushDebugLogs. Default is false.typescript
const api = new ApiService({baseUrl: "https://api.example.com",baseHeaders: { "Content-Type": "application/json" },baseToken: "your-api-token",enableDebug: true,});
status, message, and data.get(endpoint, data?) - Sends a GET request to the specified endpoint.post(endpoint, data?) - Sends a POST request with an optional body.put(endpoint, data?) - Sends a PUT request with an optional body.patch(endpoint, data?) - Sends a PATCH request with an optional body.delete(endpoint, data?) - Sends a DELETE request with an optional body.headers: Additional headers for this requesttoken: Bearer token (overrides baseToken)urlParams: Query parameters as key-value pairsbody: Request body (any serializable data)headers: Additional headers for this requesttoken: Bearer token (overrides baseToken)urlParams: Query parameters as key-value pairstypescript
import { ApiService } from "@arkyn/server";const api = new ApiService({baseUrl: "https://api.example.com",baseHeaders: { "Content-Type": "application/json" },enableDebug: process.env.NODE_ENV === "development",});// GET request with query parametersconst users = await api.get("/users", {urlParams: { page: "1", limit: "10" },});// GET request with custom tokenconst profile = await api.get("/me", {token: "user-specific-token",});// POST request with bodyconst newUser = await api.post("/users", {body: { name: "John", email: "john@example.com" },});// PUT requestconst updatedUser = await api.put("/users/:userId", {body: { name: "John Doe" },urlParams: { userId: "123" },});// PATCH requestconst patchedUser = await api.patch("/users/:userId", {body: { status: "active" },urlParams: { userId: "123" },});// DELETE requestconst deleted = await api.delete("/users/:userId", {urlParams: { userId: "123" },});// Handling responsesif (users.status === 200) {console.log(users.data); // Response data} else {console.log(users.message); // Error message}
baseToken (as Authorization: Bearer {token})baseHeaders (from constructor)headers (from request data)token (from request data, as Authorization: Bearer {token})enableDebug is enabled, each request logs the following information:flushDebugLogs with the "yellow" color scheme./users/:userId becomes /users/123):param syntax is optional, but recommended when integrating with LogService. The log system generates logs based on URLs, so having a consistent base URL with variables as route parameters makes logs more readable and aggregatable.typescript
// Without route variables - each userId creates a different log entryawait api.get("/users/123");await api.get("/users/456");// Logs: GET /users/123, GET /users/456// With route variables - logs are grouped by the base patternawait api.get("/users/:userId", { urlParams: { userId: "123" } });await api.get("/users/:userId", { urlParams: { userId: "456" } });// Logs: GET /users/:userId (with userId as metadata)
baseUrl to all endpoint paths, so endpoints should start with /.status, message, and data properties.getRequest, postRequest, putRequest, patchRequest, deleteRequest.