ApiService class is used to create an instance that manages HTTP requests, allowing the configuration of a base URL, headers, and authorization token to be used in all calls.ts
import { ApiService } from "@arkyn/server";
ApiServiceConstructorProps): The configuration properties for the API instance.
baseUrl (string): The base URL for the API.baseHeaders (HeadersInit, optional): Base headers to include in all requests.baseToken (string | null, optional): Base token for authorization.ApiService provides methods for the most common HTTP verbs. All methods return a Promise that resolves to an ApiResponseDTO object.GETroute (string): The API route.data (ApiRequestDataWithoutBodyProps, optional): Request data, including headers and token.POSTroute (string): The API route.data (ApiRequestDataWithBodyProps, optional): Request data, including body, headers, and token.PUTroute (string): The API route.data (ApiRequestDataWithBodyProps, optional): Request data, including body, headers, and token.PATCHroute (string): The API route.data (ApiRequestDataWithBodyProps, optional): Request data, including body, headers, and token.DELETEroute (string): The API route.data (ApiRequestDataWithBodyProps, optional): Request data, including body, headers, and token.GET, POST, etc.) of the ApiService uses a corresponding helper function (getRequest, postRequest, etc.). These functions, in turn, are wrappers for the central makeRequest function, which is responsible for executing the fetch call and standardizing the response.makeRequest function performs the following actions:fetch request with the correct method, URL, headers, and body.Content-Type to application/json and serializes the request body.arkynLogRequest.ApiResponseDTO object.arkynLogRequestmakeRequest function uses arkynLogRequest to send detailed data about each HTTP request to a centralized logging service. This is useful for monitoring and debugging in production environments.arkynLogRequest performs the following actions:ArkynLogService. If the configuration is not available, the function does nothing.process.env.NODE_ENV === "development") to avoid sending unnecessary logs.POST to the logging API. The collected data includes:
elapsedTime).queryParams).ApiResponseDTO objectPromise that resolves with an ApiResponseDTO<T> object, which has the following structure:success (boolean): true if the request was successful (HTTP status 2xx), false otherwise.status (number): The HTTP status code of the response.message (string): A descriptive message about the result of the request.response (T | null): The response data, usually in JSON format. It is null if the response has no body or if a parsing error occurs.cause (any | null): Additional information about the error, if any.api.PATCH("/users/1", { body: { name: "John" } }) is called.PATCH method of ApiService calls patchRequest.patchRequest calls makeRequest with the "PATCH" method.makeRequest executes the fetch request and returns the ApiResponseDTO.js
// Example of useconst api = new ApiService({ baseUrl: "https://api.example.com" });async function updateUser() {const result = await api.PATCH("/users/1", {body: { name: "Jane Doe" },});if (result.success) {console.log(result.message, result.response);} else {console.error(`Error ${result.status}: ${result.message}`);}}