UNPKG

@selldone/sdk-storefront

Version:

A TypeScript SDK to connect to your shop and build a fully functional storefront and website by simply developing a frontend web application. All backend operations are seamlessly managed by the serverless Selldone solution.

215 lines (214 loc) 10.4 kB
import { AxiosError, type AxiosInstance, type CancelTokenSource } from "axios"; import { LRUCache } from "../helper/cache/LRUCache"; export interface IAPIConfig { axiosInstance: AxiosInstance; debounceTime?: number; } type DebounceGetOptions = { max_valid_status_code?: number | null; debounce_time?: number | null; }; type PostOptions = { query?: any; accept_error_response?: boolean; }; export interface IErrorResponse { error: string; code?: number; error_msg: string; } export declare abstract class APIAbstract { /** * Duration (in seconds) within which, if data was previously cached, the cached value will be returned in the `then()`. If the last cached data exceeds this duration, a fresh fetch will occur. */ optimizer_cache_duration?: number; /** * Block last request (used when we don't want to previous result from server trigger then()) * e.g. in products list fast navigation. */ has_cancellation?: boolean; protected axiosInstance: AxiosInstance; protected debounceTime: number; constructor(config?: IAPIConfig); /** * Optimizes the data fetch by utilizing cached data if available within the specified duration. * * @param {number} [duration=60] - The duration (in seconds) within which, if data was previously cached, the cached value will be returned in the `then()`. If the last cached data exceeds this duration, a fresh fetch will occur. Default is 60 seconds. * @returns {Object} The current object instance for method chaining. * * @example * * window.$backoffice.product.optimize(120).get('someEndpoint').cache(handleSuccessResponse).then(handleSuccessResponse); // Will use cache if data is less than 120 seconds old. * */ optimize(duration?: number): this; /** * Forces a fresh data fetch regardless of any available cached data or specified optimization duration. * * @returns {Object} The current object instance for method chaining. * * @example * * window.$backoffice.product.force().get('someEndpoint'); // Ignores cache and forces a fresh data fetch. * */ force(): this; /** * Cancels the previous request. * This is particularly useful in scenarios like rapidly navigating through a products list * where we want to ensure that only the result of the most recent request is processed, * ignoring any previous yet-to-complete requests. * * @param {boolean} has_cancellation - Indicates if the request should be cancellable. Default is true. * @returns {Object} The current object instance for method chaining. * * @example * * window.$backoffice.product.cancellation(false).get('someEndpoint'); * * Note: The key to cancel previous request is the url! If the cancellation activate, * then only one request will be sent to each url simultaneously and all previous requests will be discarded. * */ cancellation(has_cancellation?: boolean): this; /** * Sends an HTTP GET request with specified parameters, applying a debouncing effect. * This means that the function will delay the processing of the request until after * the specified debounce time has elapsed since the last time the function was invoked. * * @param {string} url The URL endpoint to send the GET request to. * * @param {any} params The parameters to be included with the GET request. * * @param {Function} onSuccess A callback function that is executed when the request is successful. * It takes the response data as its only argument. * * @param {Function} [onError] An optional callback function that is executed when there's an error with the request or * if the response contains an error. It takes the error or error message as its only argument. * * @param {DebounceGetOptions} [options] Optional settings for the request: * * @param {number | null} [options.max_valid_status_code=null] The maximum valid status code to be treated as successful. * The function is checking if the status code is less than this value. For example, if set to 500, all 4xx * and lower status codes (e.g., 400 Bad Request, 404 Not Found, etc.) are treated as successful, * while 5xx status codes (e.g., 500 Internal Server Error, 502 Bad Gateway, etc.) are treated as errors. * * @param {number | null} [options.debounce_time=null] Override the default debounce time. If not provided, the default debounce time is used. * * @returns {void} */ protected getDebounce(url: string, params: any, onSuccess: (data: any) => void, onError?: (error: IErrorResponse | AxiosError) => void, options?: DebounceGetOptions): void; /** * Sends a POST request to the specified URL and returns the response. * * @template T The expected response data type. Defaults to `any` if not specified. * * @param {string} url - The endpoint URL to which the POST request should be sent. * @param {any} params - The body data to be sent with the POST request. * @param {PostOptions?} options - Optional configuration for the request, * currently supports only additional query parameters. * * @returns {Promise<T>} - Returns a promise that resolves with the data from the response * if the request was successful or an `IErrorResponse` object if there was an error. * In case of any other errors (e.g., network issues), the promise is rejected * with an AxiosError. * * @throws {IErrorResponse} - Throws an error if the response contains an `error` field, * indicating a problem with the request. * @throws {AxiosError} - Throws an error for any issues related to making the request, * such as network errors. * * @example * * interface MyExpectedResponse { * id: number; * name: string; * } * * postNow<MyExpectedResponse>('https://api.example.com/data', { key: 'value' }) * .then(data => console.log(data.id, data.name)) * .catch(error => console.error(error)); * */ protected postNow<T = any>(url: string, params: any, options?: PostOptions): Promise<T>; /** * Sends a GET request to the specified URL and returns the response. * * @template T The expected response data type. Defaults to `any` if not specified. * * @param {string} url - The endpoint URL to which the GET request should be sent. * @param {Record<string, any>} query - Query parameters for the GET request. * * @param __deep_cache_founder_function * @returns {Promise<T>} - Returns a promise that resolves with the data from the response * if the request was successful or an `IErrorResponse` object if there was an error. * In case of any other errors (e.g., network issues), the promise is rejected * with an AxiosError. * * @throws {IErrorResponse} - Throws an error if the response contains an `error` field, * indicating a problem with the request. * @throws {AxiosError} - Throws an error for any issues related to making the request, * such as network errors. * * @example * * interface MyExpectedResponse { * id: number; * name: string; * } * * getNow<MyExpectedResponse>('https://api.example.com/data', { key: 'value' }) * .then(data => console.log(data.id, data.name)) * .catch(error => console.error(error)); * */ protected getNow<T = any>(url: string, query?: Record<string, any> | null, // default value set to an empty object __deep_cache_founder_function?: (caches: LRUCache<string, (any & { __date?: Date; }) | null>) => any | null): IExtendedPromiseWithCache<T>; /** * Executes a PUT HTTP request using the provided URL, parameters, and optional configurations. * * @template T - The expected return type of the response. Default is `any`. * * @param {string} url - The endpoint URL to which the PUT request should be made. * @param {any} params - The payload or body data to be sent in the PUT request. * @param {PostOptions} [options] - Optional configurations for the request. Contains a `query` field for URL query parameters. * * @returns {Promise<T>} - Returns a promise that resolves with the data from the response. * If the response contains an error field, the promise is rejected with the error data. * * @throws {IErrorResponse} - If the response contains an 'error' field, the function throws an error of type `IErrorResponse`. * @throws {AxiosError} - If the request encounters an Axios-specific error, the function throws that error. * * @example * putNow<SomeResponseType>('/some-endpoint', { key: 'value' }, { query: { filter: 'someFilter' }}) * .then(data => console.log(data)) * .catch(error => console.error(error)); */ protected putNow<T = any>(url: string, params?: any, options?: PostOptions): Promise<T>; protected deleteNow<T = any>(url: string, query?: Record<string, any> | null): Promise<T>; static CleanedQuery(query: Record<string, any> | null): {} | null; } /** * Cache get responses. */ /** * Interface extending the native Promise to support cache functionality. */ export interface IExtendedPromiseWithCache<T> extends Promise<T> { /** Cache key for the current promise. */ __cache_key?: string; __cache_callback_disabled?: boolean; __deep_cache_founder_function?: (caches: LRUCache<string, (CancelTokenSource & { __date?: Date; }) | null>) => any | null; /** * Cache function that can be chained to a promise to handle cached responses. * @param {cacheResponse} - Callback function to handle the cache value. * @returns {this} - Returns the current promise for chaining. */ cache?: (cacheResponse: (value: any) => void) => this; } export declare function CleanCache(): void; export {};