UNPKG

bc-node-sdk

Version:

BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.

115 lines (114 loc) 6.27 kB
import { AxiosRequestConfig, AxiosResponse } from "../api"; import IApiService from '../../domain/contracts/api/IApiService'; /** * Class {@link ApiService} is a concrete implementation of the {@link IApiService} interface. * It encapsulates the logic to make API calls to the {@link https://localhost:3000/api/v1|API server} and handles token retrieval and refresh. * * @see https://github.com/axios/axios * @see https://github.com/nodejs/node/blob/master/doc/api/https.md#httpsrequesturl-options-callback * @see https://github.com/nodejs/node/blob/master/doc/api/http.md#httprequesturl-options-callback */ declare class ApiService implements IApiService { private static instance; private authBaseUrl; private axiosInstance; private authToken; private tokenExpiration; /** * Constructor for the ApiService class. * It initializes the API base URL and if specified, the keep-alive agent configuration. * It also creates an instance of the axios client and sets up the request and response interceptors. * The request interceptor adds the authorization header to the request if the token is available. * The response interceptor handles 401 responses by refreshing the token and retrying the original request. * If the token refresh fails, it rejects the promise with the original error. * @param {string} clientId - The client ID to use for token refresh. * @param {string} sharedSecret - The shared secret to use for token refresh. * @param {string} apiBaseUrl - The base URL of the API server. * @param {string} authBaseUrl - The base URL of the authorization server. */ private constructor(); /** * Returns an instance of the ApiService class, creating a new one if it doesn't exist already. * @param {string} clientId - The client ID to use for authentication. * @param {string} sharedSecret - The shared secret to use for authentication. * @param {string} apiBaseUrl - The base URL of the API endpoints. * @param {string} authBaseUrl - The base URL of the authentication endpoints. * @returns {ApiService} The instance of the ApiService class. */ static getInstance(clientId: string, sharedSecret: string, apiBaseUrl: string, authBaseUrl: string): ApiService; /** * Sets the authentication token for the API service. * * @param {string} token - The authentication token to be set. * @returns {Promise<void>} A promise that resolves when the token is set. */ setAuthToken(token: string): Promise<void>; /** * Retrieves the authentication token for the API service. * If the token does not exist or is expired, it refreshes the token. * * @returns {Promise<string | null>} A promise that resolves to the current authentication token or null if unavailable. */ getAuthToken(): Promise<string | null>; /** * Sends a GET request to the specified URL using the provided Axios request configuration. * * @template T - The expected response data type. * @param {string} url - The URL to send the GET request to. * @param {AxiosRequestConfig} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse<any>>} A promise that resolves to the Axios response. */ get<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<any>>; /** * Sends a POST request to the specified URL using the provided Axios request configuration. * * @template T - The expected response data type. * @param {string} url - The URL to send the POST request to. * @param {any} [data] - The data to be sent as the request body. * @param {AxiosRequestConfig} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse<T>>} A promise that resolves to the Axios response. */ post<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>; /** * Sends a PUT request to the specified URL using the provided Axios request configuration. * * @template T - The expected response data type. * @param {string} url - The URL to send the PUT request to. * @param {any} [data] - The data to be sent as the request body. * @param {AxiosRequestConfig} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse<T>>} A promise that resolves to the Axios response. */ put<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>; /** * Sends a PATCH request to the specified URL using the provided Axios request configuration. * * @template T - The expected response data type. * @param {string} url - The URL to send the PATCH request to. * @param {any} [data] - The data to be sent as the request body. * @param {AxiosRequestConfig} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse<T>>} A promise that resolves to the Axios response. */ patch<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>; /** * Sends a DELETE request to the specified URL using the provided Axios request configuration. * * @template T - The expected response data type. * @param {string} url - The URL to send the DELETE request to. * @param {AxiosRequestConfig} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse<T>>} A promise that resolves to the Axios response. */ delete<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>; /** * Checks if the authentication token has expired. * @returns {boolean} True if the token has expired, false otherwise. */ private isTokenExpired; /** * Refreshes the authentication token by making an API call to the /auth/refresh endpoint. * The new token is then stored in the AuthService instance. * @private * @returns {Promise<void>} A promise that resolves when the token has been refreshed. */ private refreshToken; } export default ApiService;