/** * @module SDK/API */ import { EventEmitter } from "events"; import { RequestErrorCode, ResponseErrorCode } from "../errors/APIError.js"; import { ProcessingMode } from "../resources/common/enums.js"; import { JWTPayload } from "./utils/JWT.js"; export type PollParams = { /** * Condition for which the response is considered to be successful and stop the polling */ successCondition: (response: Response) => boolean; /** * Condition to cancel the polling without triggering error */ cancelCondition?: (response: Response) => boolean; /** * Time after which a TimeoutError will be triggered and the poll will be canceled */ timeout?: number; /** * Interval between polls or function to compute the interval */ interval?: number | (() => number); /** * Callback to be triggered at each poll iteration */ iterationCallback?: (response: Response) => void; /** * Boolean when the call should not be executed when the tab is inactive. Defaults to false. * Only works on a valid webbrowser environment */ browserSkipCallForInactiveTabs?: boolean; }; export type BodyTransferType = "entity" | "message"; export declare enum HTTPMethod { GET = "GET", POST = "POST", PATCH = "PATCH", PUT = "PUT", DELETE = "DELETE", OPTION = "OPTION", HEAD = "HEAD" } export interface RestAPIOptions { endpoint?: string; jwt?: string; handleUpdateJWT?: (jwt: string) => void; secret?: string | null; origin?: string | null; authParams?: DefaultAuthParams; authToken?: string; appId?: string; } export interface ApplicationToken { sub: "app_token"; } export interface StoreToken extends ApplicationToken { storeId: string; mode: ProcessingMode; domains: string[]; } export interface SubError { reason: RequestErrorCode | ResponseErrorCode; rawError?: boolean | number | string | SubError | ValidationError; } export interface ValidationError extends SubError { field: string | null; } export interface ErrorResponse { httpCode?: number; status: string; code: ResponseErrorCode | RequestErrorCode; errors: (SubError | ValidationError)[]; } export interface AuthParams { jwt?: string; secret?: string; idempotentKey?: string; origin?: string; useCredentials?: boolean; authToken?: string; appId?: string; } export type DefaultAuthParams = { useCredentials?: boolean; }; export type PollData = { polling?: boolean; }; export type PollExecute = () => Promise; export type SendData = Data; export type ApiSendOptions = { /** * Validate the JWT before calling the route and adds the authorization header to the request. */ requireAuth?: boolean; /** * Add the custom type to the header. Defaults to `application/json` */ acceptType?: string; /** * Custom formatter to format the request object to the API (skipped for blobs) */ keyFormatter?: (key: string) => string; /** * List of key to ignore formatting on when parsing the response */ ignoreKeysFormatting?: string[]; /** * Overrides default body transfer encoding of the request. * * Note: Can not for GET and HEAD request * * Default is "message-body" for DELETE, HEAD and GET and "entity-body" for all other call methods. */ bodyTransferEncoding?: BodyTransferType; /** * Some of the services require query impersonate to be present. */ queryImpersonate?: string; /** * Fetch request init parameters * * Using `redirect` as `manual` will automatically do the redirect from the SDK to preserve the origin */ requestInit?: RequestInit; /** * Boolean when debug logs should be shown for debugging purpose */ debug?: boolean; }; export declare class RestAPI extends EventEmitter { endpoint: string; jwt: JWTPayload | null; origin?: string; secret?: string; defaultAuthParams?: DefaultAuthParams; /** * @deprecated */ appId?: string; /** * @deprecated */ authToken?: string; private _jwtRaw; protected handleUpdateJWT?: (jwt: string) => void; constructor(options?: RestAPIOptions); set jwtRaw(jwtRaw: string | undefined); get jwtRaw(): string | undefined; /** * @internal */ send(method: HTTPMethod, uri: string, data?: SendData, auth?: AuthParams | null, options?: ApiSendOptions): Promise; protected getHeaders(data: SendData, auth: AuthParams, payload: boolean, acceptType?: string): Headers; /** * @internal */ longPolling(execute: PollExecute, pollParams: PollParams): Promise; ping(): Promise; }