bc-api-client
Version:
A client for the BigCommerce management API and app authentication
64 lines (63 loc) • 2.1 kB
TypeScript
/**
* Network utilities for interacting with the BigCommerce API.
* Provides rate-limited request handling, error management, and type-safe API calls.
*/
import { Logger } from './core';
/** HTTP methods supported by the API */
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
export declare const Methods: Record<string, Method>;
export declare const BASE_URL = "https://api.bigcommerce.com/stores/";
/** Supported BigCommerce API versions */
export type ApiVersion = 'v3' | 'v2';
/**
* Options for making API requests
* @template T - Type of the request body
*/
export type RequestOptions<T> = {
/** API endpoint to call */
endpoint: string;
/** HTTP method to use */
method?: Method;
/** Request body data */
body?: T;
/** API version to use */
version?: ApiVersion;
/** Query parameters to append to the URL */
query?: Record<string, string>;
};
export type StoreOptions = {
/** BigCommerce store hash */
storeHash: string;
/** API access token */
accessToken: string;
};
/**
* Options for rate limit handling
*/
export type RateLimitOptions = {
/** Maximum delay in milliseconds before giving up on rate-limited requests */
maxDelay?: number;
/** Maximum number of retries for rate-limited requests */
maxRetries?: number;
};
/**
* Custom error class for API request failures
* @template T - Type of the error data
*/
export declare class RequestError<T> extends Error {
status: number;
message: string;
data: T | string;
cause?: unknown | undefined;
constructor(status: number, message: string, data: T | string, cause?: unknown | undefined);
}
/**
* Makes an API request with rate limit handling
* @template T - Type of the request body and response
* @param options - Request options including rate limit settings
* @returns Promise resolving to the API response
* @throws {RequestError} If the request fails or rate limit is exceeded
*/
export declare const request: <T, R>(options: RequestOptions<T> & RateLimitOptions & StoreOptions & {
logger?: Logger;
}) => Promise<R>;