bc-api-client 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +15 -4
- package/dist/index.js +6 -3
- package/dist/index.js.map +2 -2
- package/dist/net.d.ts +4 -0
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Logger } from './core';
|
|
2
|
-
import { RateLimitOptions, RequestOptions, StoreOptions } from './net';
|
|
2
|
+
import { RateLimitOptions, RequestOptions, StoreOptions, KyOptions } from './net';
|
|
3
3
|
/**
|
|
4
4
|
* Options for GET requests to the BigCommerce API
|
|
5
5
|
*/
|
|
@@ -8,6 +8,8 @@ export type GetOptions = {
|
|
|
8
8
|
query?: Record<string, string>;
|
|
9
9
|
/** API version to use (v2 or v3) */
|
|
10
10
|
version?: 'v2' | 'v3';
|
|
11
|
+
/** Options to pass directly to ky */
|
|
12
|
+
kyOptions?: KyOptions;
|
|
11
13
|
};
|
|
12
14
|
/**
|
|
13
15
|
* Options for POST/PUT requests to the BigCommerce API
|
|
@@ -33,6 +35,9 @@ export type QueryOptions = Omit<GetOptions, 'version'> & ConcurrencyOptions & {
|
|
|
33
35
|
key: string;
|
|
34
36
|
/** Array of values to query for */
|
|
35
37
|
values: (string | number)[];
|
|
38
|
+
} & {
|
|
39
|
+
/** Options to pass directly to ky */
|
|
40
|
+
kyOptions?: KyOptions;
|
|
36
41
|
};
|
|
37
42
|
/**
|
|
38
43
|
* Configuration options for the BigCommerce client
|
|
@@ -94,7 +99,9 @@ export declare class BigCommerceClient {
|
|
|
94
99
|
* @param options.version - API version to use (v2 or v3) (default: v3)
|
|
95
100
|
* @returns Promise resolving to void
|
|
96
101
|
*/
|
|
97
|
-
delete<R>(endpoint: string, options?: Pick<GetOptions, 'version' | 'query'>
|
|
102
|
+
delete<R>(endpoint: string, options?: Pick<GetOptions, 'version' | 'query'> & {
|
|
103
|
+
kyOptions?: KyOptions;
|
|
104
|
+
}): Promise<void>;
|
|
98
105
|
/**
|
|
99
106
|
* Executes multiple requests concurrently with controlled concurrency
|
|
100
107
|
* @param requests - Array of request options to execute
|
|
@@ -121,7 +128,9 @@ export declare class BigCommerceClient {
|
|
|
121
128
|
* @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)
|
|
122
129
|
* @returns Promise resolving to array of all items across all pages
|
|
123
130
|
*/
|
|
124
|
-
collect<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions
|
|
131
|
+
collect<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions & {
|
|
132
|
+
kyOptions?: KyOptions;
|
|
133
|
+
}): Promise<T[]>;
|
|
125
134
|
/**
|
|
126
135
|
* Collects all pages of data from a paginated v2 API endpoint.
|
|
127
136
|
* This method simply pulls all pages concurrently until a 204 is returned in a batch.
|
|
@@ -131,7 +140,9 @@ export declare class BigCommerceClient {
|
|
|
131
140
|
* @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)
|
|
132
141
|
* @returns Promise resolving to array of all items across all pages
|
|
133
142
|
*/
|
|
134
|
-
collectV2<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions
|
|
143
|
+
collectV2<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions & {
|
|
144
|
+
kyOptions?: KyOptions;
|
|
145
|
+
}): Promise<T[]>;
|
|
135
146
|
/**
|
|
136
147
|
* Queries multiple values against a single field using the v3 API.
|
|
137
148
|
* If the url + query params are too long, the query will be chunked. Otherwise, this method acts like `collect`.
|
package/dist/index.js
CHANGED
|
@@ -194,7 +194,8 @@ var call = async (options) => {
|
|
|
194
194
|
version = CONFIG.DEFAULT_VERSION,
|
|
195
195
|
query,
|
|
196
196
|
logger,
|
|
197
|
-
baseUrl = CONFIG.BASE_URL
|
|
197
|
+
baseUrl = CONFIG.BASE_URL,
|
|
198
|
+
kyOptions
|
|
198
199
|
} = options;
|
|
199
200
|
const url = `${baseUrl}${storeHash}/${version}/${endpoint.replace(/^\//, "")}`;
|
|
200
201
|
const searchParams = query ? new URLSearchParams(query).toString() : "";
|
|
@@ -220,7 +221,8 @@ var call = async (options) => {
|
|
|
220
221
|
Accept: "application/json",
|
|
221
222
|
"X-Auth-Token": accessToken
|
|
222
223
|
},
|
|
223
|
-
json: body
|
|
224
|
+
json: body,
|
|
225
|
+
...kyOptions
|
|
224
226
|
};
|
|
225
227
|
const response = await ky(fullUrl, request2);
|
|
226
228
|
return response;
|
|
@@ -451,7 +453,8 @@ var BigCommerceClient = class {
|
|
|
451
453
|
query: {
|
|
452
454
|
...options.query,
|
|
453
455
|
page: page.toString()
|
|
454
|
-
}
|
|
456
|
+
},
|
|
457
|
+
kyOptions: options.kyOptions
|
|
455
458
|
}));
|
|
456
459
|
const remainingPages = await this.concurrent(pageRequests, options);
|
|
457
460
|
remainingPages.forEach((page) => {
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/net.ts", "../src/util.ts", "../src/client.ts", "../src/auth.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Network utilities for interacting with the BigCommerce API.\n * Provides rate-limited request handling, error management, and type-safe API calls.\n */\n\nimport ky, { KyResponse, HTTPError } from 'ky';\nimport { Logger } from './core';\n\n/** HTTP methods supported by the API */\nexport type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';\n\nexport const Methods: Record<string, Method> = {\n GET: 'GET',\n POST: 'POST',\n PUT: 'PUT',\n DELETE: 'DELETE',\n} as const;\n\nexport const BASE_URL = 'https://api.bigcommerce.com/stores/';\n\n/** Configuration for the BigCommerce API client */\nconst CONFIG = {\n /** Base URL for BigCommerce API */\n BASE_URL,\n /** Default API version to use */\n DEFAULT_VERSION: 'v3',\n /** Maximum delay in milliseconds for rate limit retries */\n DEFAULT_MAX_DELAY: 60e3,\n /** Maximum allowed URL length */\n MAX_URL_LENGTH: 2048,\n /** Default maximum number of retries for rate-limited requests */\n DEFAULT_MAX_RETRIES: 5,\n /** Rate limit header names */\n HEADERS: {\n /** Time window for rate limiting in milliseconds */\n WINDOW: 'x-rate-limit-time-window-ms',\n /** Time to wait before retrying after rate limit in milliseconds */\n RETRY_AFTER: 'x-rate-limit-time-reset-ms',\n /** Total request quota for the time window */\n REQUEST_QUOTA: 'x-rate-limit-requests-quota',\n /** Number of requests remaining in the current window */\n REQUESTS_LEFT: 'x-rate-limit-requests-left',\n },\n} as const;\n\n/** Supported BigCommerce API versions */\nexport type ApiVersion = 'v3' | 'v2';\n\n/**\n * Options for making API requests\n * @template T - Type of the request body\n */\nexport type RequestOptions<T> = {\n /** Base URL to use for the request */\n baseUrl?: string;\n /** API endpoint to call */\n endpoint: string;\n /** HTTP method to use */\n method?: Method;\n /** Request body data */\n body?: T;\n /** API version to use */\n version?: ApiVersion;\n /** Query parameters to append to the URL */\n query?: Record<string, string>;\n};\n\nexport type StoreOptions = {\n /** Base URL to use for the request */\n baseUrl?: string;\n /** BigCommerce store hash */\n storeHash: string;\n /** API access token */\n accessToken: string;\n};\n\n/**\n * Options for rate limit handling\n */\nexport type RateLimitOptions = {\n /** Maximum delay in milliseconds before giving up on rate-limited requests */\n maxDelay?: number;\n /** Maximum number of retries for rate-limited requests */\n maxRetries?: number;\n};\n\n/**\n * Custom error class for API request failures\n * @template T - Type of the error data\n */\nexport class RequestError<T> extends Error {\n constructor(\n public status: number,\n public message: string,\n public data: T | string,\n public cause?: unknown,\n ) {\n super(message, { cause });\n }\n}\n\n/**\n * Makes an API request with rate limit handling\n * @template T - Type of the request body and response\n * @param options - Request options including rate limit settings\n * @returns Promise resolving to the API response\n * @throws {RequestError} If the request fails or rate limit is exceeded\n */\nexport const request = async <T, R>(\n options: RequestOptions<T> &\n RateLimitOptions &\n StoreOptions & {\n logger?: Logger;\n },\n): Promise<R> => {\n const {\n baseUrl = CONFIG.BASE_URL,\n maxDelay = CONFIG.DEFAULT_MAX_DELAY,\n maxRetries = CONFIG.DEFAULT_MAX_RETRIES,\n logger,\n } = options;\n\n let retries = 0;\n let lastError: RequestError<T> | null = null;\n\n while (retries < maxRetries) {\n try {\n return await safeRequest<T, R>({ ...options, baseUrl });\n } catch (error) {\n const err = error as RequestError<T>;\n lastError = err;\n\n if (err.status === 429 && typeof err.data === 'object' && err.data !== null && 'headers' in err.data) {\n const headers = err.data.headers as Record<string, string>;\n const retryAfter = Number.parseInt(headers[CONFIG.HEADERS.RETRY_AFTER]);\n\n logger?.debug(\n {\n retryAfter,\n retries,\n remaining: headers[CONFIG.HEADERS.REQUESTS_LEFT],\n },\n 'Rate limit hit, retrying',\n );\n\n if (Number.isNaN(retryAfter)) {\n throw new RequestError(\n err.status,\n `Failed to parse retry after: ${headers[CONFIG.HEADERS.RETRY_AFTER]}, ${err.message}`,\n err.data,\n err.cause,\n );\n }\n\n if (retryAfter > maxDelay) {\n logger?.warn(\n {\n retryAfter,\n maxDelay,\n },\n 'Rate limit delay exceeds maximum allowed delay',\n );\n throw new RequestError(\n err.status,\n `Rate limit exceeded: ${retryAfter}ms, ${err.message}`,\n err.data,\n err.cause,\n );\n }\n\n await new Promise((resolve) => setTimeout(resolve, retryAfter));\n retries++;\n continue;\n }\n\n throw err;\n }\n }\n\n logger?.error(\n {\n retries,\n error: lastError,\n },\n 'Request failed after maximum retries',\n );\n\n throw lastError ?? new RequestError(500, 'Failed to make request', 'Too many retries after rate limit');\n};\n\n/**\n * Makes a single API request with error handling\n * @template T - Type of the request body and response\n * @param options - Request options\n * @returns Promise resolving to the API response\n * @throws {RequestError} If the request fails\n */\nconst safeRequest = async <T, R>(\n options: RequestOptions<T> & StoreOptions & { logger?: Logger; baseUrl?: string },\n): Promise<R> => {\n const { logger, baseUrl = CONFIG.BASE_URL } = options;\n let res: KyResponse<T>;\n\n try {\n res = await call<T, R>({ ...options, baseUrl });\n } catch (error) {\n if (error instanceof RequestError) {\n throw error;\n }\n\n if (!(error instanceof HTTPError)) {\n logger?.error(\n {\n error:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n },\n 'Unexpected error during request',\n );\n throw error;\n }\n\n let data: unknown;\n let errorMessage = error.message;\n\n try {\n data = await error.response.text();\n try {\n data = JSON.parse(data as string);\n\n if (typeof data === 'object' && data !== null && 'message' in data) {\n errorMessage = data.message as string;\n }\n } catch {\n // If JSON parsing fails, keep the text response\n }\n } catch {\n data = 'Failed to read error response';\n }\n\n logger?.error(\n {\n status: error?.response?.status,\n errorMessage,\n data,\n endpoint: options.endpoint,\n query: options.query,\n body: options.body,\n headers: Object.fromEntries(error?.response?.headers?.entries() ?? []),\n },\n 'HTTP error during request',\n );\n\n throw new RequestError(\n error?.response?.status ?? 500,\n errorMessage,\n {\n data,\n endpoint: options.endpoint,\n query: options.query,\n body: options.body,\n headers: Object.fromEntries(error?.response?.headers?.entries() ?? []),\n },\n error,\n );\n }\n\n const text = await res.text();\n\n if (res.status === 204) {\n return undefined as unknown as R;\n }\n\n try {\n return JSON.parse(text);\n } catch (error) {\n logger?.error(\n {\n status: res.status,\n error:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n },\n 'Failed to parse response',\n );\n throw new RequestError(res.status, `Failed to parse response: ${text}`, text, error);\n }\n};\n\n/**\n * Internal function to make the actual HTTP request\n * @template T - Type of the request body and response\n * @param options - Request options\n * @returns Promise resolving to the raw response\n * @throws {RequestError} If the URL is too long or request fails\n */\nconst call = async <T, R>(\n options: RequestOptions<T> & StoreOptions & { logger?: Logger; baseUrl?: string },\n): Promise<KyResponse<R>> => {\n const {\n storeHash,\n accessToken,\n endpoint,\n method = 'GET',\n body,\n version = CONFIG.DEFAULT_VERSION,\n query,\n logger,\n baseUrl = CONFIG.BASE_URL,\n } = options;\n\n const url = `${baseUrl}${storeHash}/${version}/${endpoint.replace(/^\\//, '')}`;\n\n // Check URL length including search params\n const searchParams = query ? new URLSearchParams(query).toString() : '';\n const fullUrl = searchParams ? `${url}?${searchParams}` : url;\n\n if (fullUrl.length > CONFIG.MAX_URL_LENGTH) {\n logger?.error(\n {\n urlLength: fullUrl.length,\n maxLength: CONFIG.MAX_URL_LENGTH,\n },\n 'URL length exceeds maximum allowed length',\n );\n throw new RequestError(\n 400,\n 'URL too long',\n `URL length ${fullUrl.length} exceeds maximum allowed length of ${CONFIG.MAX_URL_LENGTH}`,\n );\n }\n\n const request = {\n method,\n headers: {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'X-Auth-Token': accessToken,\n },\n json: body,\n };\n\n const response = await ky<R>(fullUrl, request);\n return response;\n};\n", "/**\n * Split an array of strings into chunks by following logic\n *\n * 1. add length of each string + separatorSize to offset\n * 2. if result is greater than `maxLength`, start a new chunk\n * 3. otherwise, add the string to the current chunk until the chunk is of `chunkLength`\n *\n * This function to be used for splitting query params to avoid url length limit\n *\n * @param items array of strings\n * @param options\n * @param options.maxLength max length of the combined strings\n * @param options.chunkLength max length of each chunk\n * @param options.offset offset of the first chunk\n * @param options.separatorSize size of the separator\n */\nexport const chunkStrLength = (\n items: string[],\n options: {\n maxLength?: number;\n chunkLength?: number;\n offset?: number;\n separatorSize?: number;\n } = {},\n) => {\n const { maxLength = 2048, chunkLength = 250, offset = 0, separatorSize = 1 } = options;\n\n const chunks: string[][] = [];\n let currentStrLength = offset;\n let currentChunk: string[] = [];\n\n for (const item of items) {\n const itemLength = encodeURIComponent(item).length;\n const separatorLength = currentChunk.length > 0 ? separatorSize : 0;\n const totalItemLength = itemLength + separatorLength;\n\n // Check if adding this item would exceed limits\n const wouldExceedLength = currentStrLength + totalItemLength > maxLength;\n const wouldExceedCount = currentChunk.length >= chunkLength;\n\n if ((wouldExceedLength || wouldExceedCount) && currentChunk.length > 0) {\n chunks.push(currentChunk);\n currentChunk = [];\n currentStrLength = offset;\n }\n\n // Handle items that are too large even for a new chunk\n if (itemLength + offset > maxLength) {\n // Either skip, truncate, or throw error depending on requirements\n throw new Error(`Item too large: ${itemLength} exceeds maxLength ${maxLength}`);\n }\n\n currentChunk.push(item);\n currentStrLength += totalItemLength;\n }\n\n if (currentChunk.length > 0) {\n chunks.push(currentChunk);\n }\n\n return chunks;\n};\n", "import { V3Resource, Logger } from './core';\nimport { BASE_URL, RateLimitOptions, RequestError, RequestOptions, StoreOptions, request } from './net';\nimport { chunkStrLength } from './util';\n\nconst MAX_PAGE_SIZE = 250;\nconst DEFAULT_CONCURRENCY = 10;\n\n// Helper function to chunk array into smaller arrays\nfunction chunkArray<T>(array: T[], size: number): T[][] {\n return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size));\n}\n\n// Helper function to create range array\nfunction rangeArray(start: number, end: number): number[] {\n return Array.from({ length: end - start + 1 }, (_, i) => start + i);\n}\n\n/**\n * Options for GET requests to the BigCommerce API\n */\nexport type GetOptions = {\n /** Query parameters to include in the request */\n query?: Record<string, string>;\n /** API version to use (v2 or v3) */\n version?: 'v2' | 'v3';\n};\n\n/**\n * Options for POST/PUT requests to the BigCommerce API\n */\nexport type PostOptions<T> = GetOptions & {\n /** Request body data */\n body: T;\n};\n\n/**\n * Options for controlling concurrent request behavior\n */\nexport type ConcurrencyOptions = {\n /** Maximum number of concurrent requests (default: 10) */\n concurrency?: number;\n /** Whether to skip errors and continue processing (default: false) */\n skipErrors?: boolean;\n};\n\n/**\n * Options for querying multiple values against a single filter field\n */\nexport type QueryOptions = Omit<GetOptions, 'version'> &\n ConcurrencyOptions & {\n /** The field name to query against */\n key: string;\n /** Array of values to query for */\n values: (string | number)[];\n };\n\n/**\n * Configuration options for the BigCommerce client\n */\nexport type Config = StoreOptions &\n RateLimitOptions &\n ConcurrencyOptions & {\n /** Logger instance */\n logger?: Logger;\n };\n\n/**\n * Client for interacting with the BigCommerce API\n *\n * This client provides methods for making HTTP requests to the BigCommerce API,\n * with support for both v2 and v3 endpoints, pagination, and concurrent requests.\n */\nexport class BigCommerceClient {\n /**\n * Creates a new BigCommerce client instance\n * @param config - Configuration options for the client\n * @param config.baseUrl - The base URL to use for the client (default: https://api.bigcommerce.com)\n * @param config.storeHash - The store hash to use for the client\n * @param config.accessToken - The API access token to use for the client\n * @param config.maxRetries - The maximum number of retries for rate limit errors (default: 5)\n * @param config.maxDelay - Maximum time to wait to retry in case of rate limit errors in milliseconds (default: 60000 - 1 minute). If `X-Rate-Limit-Time-Reset-Ms` header is higher than `maxDelay`, the request will fail immediately.\n * @param config.concurrency - The default concurrency for concurrent methods (default: 10)\n * @param config.skipErrors - Whether to skip errors during concurrent requests (default: false)\n * @param config.logger - Optional logger instance for debugging and error tracking\n */\n constructor(private readonly config: Config) {}\n\n /**\n * Makes a GET request to the BigCommerce API\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @returns Promise resolving to the response data of type `R`\n */\n async get<R>(endpoint: string, options?: GetOptions): Promise<R> {\n return request<never, R>({\n endpoint,\n method: 'GET',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Makes a POST request to the BigCommerce API\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @param options.body - Request body data of type `T`\n * @returns Promise resolving to the response data of type `R`\n */\n async post<T, R>(endpoint: string, options?: PostOptions<T>): Promise<R> {\n return request<T, R>({\n endpoint,\n method: 'POST',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Makes a PUT request to the BigCommerce API\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @param options.body - Request body data of type `T`\n * @returns Promise resolving to the response data of type `R`\n */\n async put<T, R>(endpoint: string, options?: PostOptions<T>): Promise<R> {\n return request<T, R>({\n endpoint,\n method: 'PUT',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Makes a DELETE request to the BigCommerce API\n * @param endpoint - The API endpoint to delete\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @returns Promise resolving to void\n */\n async delete<R>(endpoint: string, options?: Pick<GetOptions, 'version' | 'query'>): Promise<void> {\n await request<never, R>({\n endpoint,\n method: 'DELETE',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Executes multiple requests concurrently with controlled concurrency\n * @param requests - Array of request options to execute\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of response data\n */\n async concurrent<T, R>(requests: RequestOptions<T>[], options?: ConcurrencyOptions): Promise<R[]> {\n const skipErrors = options?.skipErrors ?? this.config.skipErrors ?? false;\n const results = await this.concurrentSettled<T, R>(requests, options);\n\n const successfulResults: R[] = [];\n\n for (const result of results) {\n if (result.status === 'fulfilled') {\n successfulResults.push(result.value);\n } else {\n if (!skipErrors) {\n throw result.reason;\n } else {\n this.config.logger?.warn(\n {\n error: result.reason,\n },\n 'Error in concurrent request',\n );\n }\n }\n }\n\n return successfulResults;\n }\n\n /**\n * Lowest level concurrent request method.\n * This method executes requests in chunks and returns bare PromiseSettledResult objects.\n * Use this method if you need to handle errors in a custom way.\n * @param requests - Array of request options to execute\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @returns Promise resolving to array of PromiseSettledResult containing both successful and failed requests\n */\n async concurrentSettled<T, R>(\n requests: RequestOptions<T>[],\n options?: Pick<ConcurrencyOptions, 'concurrency'>,\n ): Promise<PromiseSettledResult<R>[]> {\n const chunkSize = options?.concurrency ?? this.config.concurrency ?? DEFAULT_CONCURRENCY;\n const chunks = chunkArray(requests, chunkSize);\n\n this.config.logger?.debug(\n {\n totalRequests: requests.length,\n chunkSize,\n chunks: chunks.length,\n },\n 'Starting concurrent requests with detailed results',\n );\n\n const allResults: PromiseSettledResult<R>[] = [];\n\n for (const [index, chunk] of chunks.entries()) {\n const responses = await Promise.allSettled(\n chunk.map((opt) =>\n request<T, R>({\n ...opt,\n ...this.config,\n }),\n ),\n );\n\n this.config.logger?.debug(\n {\n chunkIndex: index,\n chunkSize: chunk.length,\n totalRequests: requests.length,\n totalChunks: chunks.length,\n responses: responses.map((response) => response.status),\n },\n 'Completed chunk',\n );\n\n allResults.push(...responses);\n }\n\n return allResults;\n }\n\n /**\n * Collects all pages of data from a paginated v3 API endpoint.\n * This method pulls the first page and uses pagination meta to collect the remaining pages concurrently.\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of all items across all pages\n */\n async collect<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]> {\n options = options ?? {};\n\n if (options.query) {\n if (!options.query.limit) {\n options.query.limit = MAX_PAGE_SIZE.toString();\n }\n } else {\n options.query = { limit: MAX_PAGE_SIZE.toString() };\n }\n\n const first = await this.get<V3Resource<T[]>>(endpoint, options);\n\n if (!Array.isArray(first.data) || !first?.meta?.pagination?.total_pages) {\n return first.data;\n }\n\n const results: T[] = [...first.data];\n const pages = first.meta.pagination.total_pages;\n\n if (pages > 1) {\n this.config.logger?.debug(\n {\n totalPages: pages,\n itemsPerPage: first.data.length,\n },\n 'Collecting remaining pages',\n );\n\n const pageRequests = rangeArray(2, pages).map((page) => ({\n endpoint,\n method: 'GET' as const,\n query: {\n ...options.query,\n page: page.toString(),\n },\n }));\n\n const remainingPages = await this.concurrent<never, V3Resource<T[]>>(pageRequests, options);\n\n remainingPages.forEach((page) => {\n if (Array.isArray(page.data)) {\n results.push(...page.data);\n }\n });\n }\n\n return results;\n }\n\n /**\n * Collects all pages of data from a paginated v2 API endpoint.\n * This method simply pulls all pages concurrently until a 204 is returned in a batch.\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of all items across all pages\n */\n async collectV2<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]> {\n options = options ?? {};\n\n if (options.query) {\n if (!options.query.limit) {\n options.query.limit = MAX_PAGE_SIZE.toString();\n }\n } else {\n options.query = { limit: MAX_PAGE_SIZE.toString() };\n }\n\n let done = false;\n const results: T[] = [];\n let page = 1;\n const concurrency = options.concurrency ?? this.config.concurrency ?? DEFAULT_CONCURRENCY;\n\n while (!done) {\n const pages = rangeArray(page, page + concurrency);\n page += concurrency;\n\n const requests = pages.map((page) => ({\n ...options,\n endpoint,\n version: 'v2' as const,\n query: { ...options.query, page: page.toString() },\n }));\n\n const responses = await Promise.allSettled(requests.map((request) => this.get<T[]>(endpoint, request)));\n\n responses.forEach((response) => {\n if (response.status === 'fulfilled') {\n if (response.value) {\n results.push(...response.value);\n } else {\n done = true;\n }\n } else {\n if (response.reason instanceof RequestError && response.reason.status === 404) {\n done = true;\n } else {\n if (!(options.skipErrors ?? this.config.skipErrors ?? false)) {\n throw response.reason;\n } else {\n this.config.logger?.warn(\n {\n error:\n response.reason instanceof Error\n ? {\n name: response.reason.name,\n message: response.reason.message,\n }\n : response.reason,\n },\n 'Error in collectV2',\n );\n }\n }\n }\n });\n }\n\n return results;\n }\n\n /**\n * Queries multiple values against a single field using the v3 API.\n * If the url + query params are too long, the query will be chunked. Otherwise, this method acts like `collect`.\n * This method does not check for uniqueness of the `values` array.\n *\n * @param endpoint - The API endpoint to request\n * @param options.key - The field name to query against e.g. `sku:in`\n * @param options.values - Array of values to query for e.g. `['123', '456', ...]`\n * @param options.query - Additional query parameters\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of matching items\n */\n async query<T>(endpoint: string, options: QueryOptions): Promise<T[]> {\n if (options.query) {\n if (!options.query.limit) {\n options.query.limit = MAX_PAGE_SIZE.toString();\n }\n } else {\n options.query = { limit: MAX_PAGE_SIZE.toString() };\n }\n\n const keySize = encodeURIComponent(options.key).length;\n const fullUrl = `${BASE_URL}${this.config.storeHash}/v3/${endpoint}?${new URLSearchParams(options.query).toString()}`;\n const offset = fullUrl.length + keySize + 1;\n const chunkLength = Number.parseInt(options.query?.limit) || MAX_PAGE_SIZE;\n const separatorSize = encodeURIComponent(',').length;\n\n const queryStr = options.values.map((value) => `${value}`);\n const chunks = chunkStrLength(queryStr, {\n separatorSize,\n offset,\n chunkLength,\n });\n\n this.config.logger?.debug(\n {\n offset,\n totalValues: options.values.length,\n chunks: chunks.length,\n valuesPerChunk: chunks[0]?.length,\n separatorSize,\n },\n 'Querying with chunked values',\n );\n\n const requests = chunks.map((chunk) => ({\n ...options,\n endpoint,\n query: { ...options.query, [options.key]: chunk.join(',') },\n }));\n\n const responses = await this.concurrent<never, V3Resource<T[]>>(requests, options);\n\n return responses.flatMap((response) => response.data);\n }\n}\n", "import ky, { HTTPError, KyResponse } from 'ky';\nimport * as jose from 'jose';\nimport { Logger } from './core';\n\n/**\n * Configuration options for BigCommerce authentication\n */\ntype Config = {\n /** The OAuth client ID from BigCommerce */\n clientId: string;\n /** The OAuth client secret from BigCommerce */\n secret: string;\n /** The redirect URI registered with BigCommerce */\n redirectUri: string;\n /** Optional array of scopes to validate during auth callback */\n scopes?: string[];\n /** Optional logger instance */\n logger?: Logger;\n};\n\nconst GRANT_TYPE = 'authorization_code';\nconst TOKEN_ENDPOINT = 'https://login.bigcommerce.com/oauth2/token';\nconst ISSUER = 'bc';\n\n/**\n * Query parameters received from BigCommerce auth callback\n */\ntype AuthQuery = {\n /** The authorization code from BigCommerce */\n code: string;\n /** The granted OAuth scopes */\n scope: string;\n /** The store context */\n context: string;\n};\n\n/**\n * Request payload for token endpoint\n */\ntype TokenRequest = {\n client_id: string;\n client_secret: string;\n code: string;\n context: string;\n scope: string;\n grant_type: typeof GRANT_TYPE;\n redirect_uri: string;\n};\n\n/**\n * User information returned from BigCommerce\n */\nexport type User = {\n /** The user's ID */\n id: number;\n /** The user's username */\n username: string;\n /** The user's email address */\n email: string;\n};\n\n/**\n * Response from BigCommerce token endpoint\n */\nexport type TokenResponse = {\n /** The OAuth access token */\n access_token: string;\n /** The granted OAuth scopes */\n scope: string;\n /** Information about the authenticated user */\n user: User;\n /** Information about the store owner */\n owner: User;\n /** The store context */\n context: string;\n /** The BigCommerce account UUID */\n account_uuid: string;\n};\n\n/**\n * JWT claims from BigCommerce\n */\nexport type Claims = {\n /** JWT audience */\n aud: string;\n /** JWT issuer */\n iss: string;\n /** JWT issued at timestamp */\n iat: number;\n /** JWT not before timestamp */\n nbf: number;\n /** JWT expiration timestamp */\n exp: number;\n /** JWT unique identifier */\n jti: string;\n /** JWT subject */\n sub: string;\n /** Information about the authenticated user */\n user: {\n id: number;\n email: string;\n locale: string;\n };\n /** Information about the store owner */\n owner: {\n id: number;\n email: string;\n };\n /** The store URL */\n url: string;\n /** The channel ID (if applicable) */\n channel_id: number | null;\n};\n\n/**\n * Handles authentication with BigCommerce OAuth\n */\nexport class BigCommerceAuth {\n /**\n * Creates a new BigCommerceAuth instance for handling OAuth authentication\n * @param config - Configuration options for BigCommerce authentication\n * @param config.clientId - The OAuth client ID from BigCommerce\n * @param config.secret - The OAuth client secret from BigCommerce\n * @param config.redirectUri - The redirect URI registered with BigCommerce\n * @param config.scopes - Optional array of scopes to validate during auth callback\n * @param config.logger - Optional logger instance for debugging and error tracking\n * @throws {Error} If the redirect URI is invalid\n */\n constructor(private readonly config: Config) {\n try {\n new URL(this.config.redirectUri);\n } catch (error) {\n throw new Error('Invalid redirect URI', { cause: error });\n }\n }\n\n /**\n * Requests an access token from BigCommerce\n * @param data - Either a query string, URLSearchParams, or AuthQuery object containing auth callback data\n * @returns Promise resolving to the token response\n */\n async requestToken(data: string | AuthQuery | URLSearchParams): Promise<TokenResponse> {\n const query = typeof data === 'string' || data instanceof URLSearchParams ? this.parseQueryString(data) : data;\n\n this.validateScopes(query.scope);\n\n const tokenRequest: TokenRequest = {\n client_id: this.config.clientId,\n client_secret: this.config.secret,\n ...query,\n grant_type: GRANT_TYPE,\n redirect_uri: this.config.redirectUri,\n };\n\n this.config.logger?.debug(\n {\n clientId: this.config.clientId,\n context: query.context,\n scopes: query.scope,\n },\n 'Requesting OAuth token',\n );\n\n let res: KyResponse;\n\n try {\n res = await ky<TokenResponse>(TOKEN_ENDPOINT, {\n method: 'POST',\n json: tokenRequest,\n });\n } catch (error) {\n if (error instanceof HTTPError) {\n const text = await error.response.text();\n\n this.config.logger?.error({\n err: {\n name: error.name,\n message: error.message,\n text,\n },\n });\n\n throw new Error(`Failed to request token. BC returned: ${text}`, { cause: error });\n }\n\n this.config.logger?.error({\n err:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n });\n\n throw new Error(`Failed to request token`, { cause: error });\n }\n\n return res.json();\n }\n\n /**\n * Verifies a JWT payload from BigCommerce\n * @param jwtPayload - The JWT string to verify\n * @param storeHash - The store hash for the BigCommerce store\n * @returns Promise resolving to the verified JWT claims\n * @throws {Error} If the JWT is invalid\n */\n async verify(jwtPayload: string, storeHash: string): Promise<Claims> {\n try {\n const secret = new TextEncoder().encode(this.config.secret);\n\n const { payload }: { payload: Claims } = await jose.jwtVerify(jwtPayload, secret, {\n audience: this.config.clientId,\n issuer: ISSUER,\n subject: `stores/${storeHash}`,\n });\n\n this.config.logger?.debug(\n {\n userId: payload.user?.id,\n storeHash: payload.sub.split('/')[1],\n },\n 'JWT verified successfully',\n );\n\n return payload;\n } catch (error) {\n this.config.logger?.error({\n error:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n });\n\n throw new Error('Invalid JWT payload', { cause: error });\n }\n }\n\n /**\n * Parses and validates a query string from BigCommerce auth callback\n * @param queryString - The query string to parse\n * @returns The parsed auth query parameters\n * @throws {Error} If required parameters are missing or scopes are invalid\n */\n private parseQueryString(queryString: string | URLSearchParams): AuthQuery {\n const params = typeof queryString === 'string' ? new URLSearchParams(queryString) : queryString;\n\n // Get required parameters\n const code = params.get('code');\n const scope = params.get('scope');\n const context = params.get('context');\n\n // Validate required parameters\n if (!code) {\n throw new Error('No code found in query string');\n }\n\n if (!scope) {\n throw new Error('No scope found in query string');\n } else if (this.config.scopes?.length) {\n this.validateScopes(scope);\n }\n\n if (!context) {\n throw new Error('No context found in query string');\n }\n\n return {\n code,\n scope,\n context,\n };\n }\n\n /**\n * Validates that the granted scopes match the expected scopes\n * @param scopes - Space-separated list of granted scopes\n * @throws {Error} If the scopes don't match the expected scopes\n */\n private validateScopes(scopes: string) {\n if (!this.config.scopes) {\n return;\n }\n\n const grantedScopes = scopes.split(' ');\n const requiredScopes = this.config.scopes;\n const missingScopes = requiredScopes.filter((scope) => !grantedScopes.includes(scope));\n\n if (missingScopes.length) {\n throw new Error(`Scope mismatch: ${scopes}; expected: ${this.config.scopes.join(' ')}`);\n }\n }\n}\n"],
|
|
5
|
-
"mappings": ";AAKA,OAAO,MAAkB,iBAAiB;AAMnC,IAAM,UAAkC;AAAA,EAC3C,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AAAA,EACL,QAAQ;AACZ;AAEO,IAAM,WAAW;AAGxB,IAAM,SAAS;AAAA;AAAA,EAEX;AAAA;AAAA,EAEA,iBAAiB;AAAA;AAAA,EAEjB,mBAAmB;AAAA;AAAA,EAEnB,gBAAgB;AAAA;AAAA,EAEhB,qBAAqB;AAAA;AAAA,EAErB,SAAS;AAAA;AAAA,IAEL,QAAQ;AAAA;AAAA,IAER,aAAa;AAAA;AAAA,IAEb,eAAe;AAAA;AAAA,IAEf,eAAe;AAAA,EACnB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AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,iBAAiB,aAAkD;AACvE,UAAM,SAAS,OAAO,gBAAgB,WAAW,IAAI,gBAAgB,WAAW,IAAI;AAGpF,UAAM,OAAO,OAAO,IAAI,MAAM;AAC9B,UAAM,QAAQ,OAAO,IAAI,OAAO;AAChC,UAAM,UAAU,OAAO,IAAI,SAAS;AAGpC,QAAI,CAAC,MAAM;AACP,YAAM,IAAI,MAAM,+BAA+B;AAAA,IACnD;AAEA,QAAI,CAAC,OAAO;AACR,YAAM,IAAI,MAAM,gCAAgC;AAAA,IACpD,WAAW,KAAK,OAAO,QAAQ,QAAQ;AACnC,WAAK,eAAe,KAAK;AAAA,IAC7B;AAEA,QAAI,CAAC,SAAS;AACV,YAAM,IAAI,MAAM,kCAAkC;AAAA,IACtD;AAEA,WAAO;AAAA,MACH;AAAA,MACA;AAAA,MACA;AAAA,IACJ;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,eAAe,QAAgB;AACnC,QAAI,CAAC,KAAK,OAAO,QAAQ;AACrB;AAAA,IACJ;AAEA,UAAM,gBAAgB,OAAO,MAAM,GAAG;AACtC,UAAM,iBAAiB,KAAK,OAAO;AACnC,UAAM,gBAAgB,eAAe,OAAO,CAAC,UAAU,CAAC,cAAc,SAAS,KAAK,CAAC;AAErF,QAAI,cAAc,QAAQ;AACtB,YAAM,IAAI,MAAM,mBAAmB,MAAM,eAAe,KAAK,OAAO,OAAO,KAAK,GAAG,CAAC,EAAE;AAAA,IAC1F;AAAA,EACJ;AACJ;",
|
|
4
|
+
"sourcesContent": ["/**\n * Network utilities for interacting with the BigCommerce API.\n * Provides rate-limited request handling, error management, and type-safe API calls.\n */\n\nimport ky, { KyResponse, HTTPError } from 'ky';\nimport type { Options as KyOptions } from 'ky';\nimport { Logger } from './core';\n\n/** HTTP methods supported by the API */\nexport type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';\nexport type { KyOptions };\n\nexport const Methods: Record<string, Method> = {\n GET: 'GET',\n POST: 'POST',\n PUT: 'PUT',\n DELETE: 'DELETE',\n} as const;\n\nexport const BASE_URL = 'https://api.bigcommerce.com/stores/';\n\n/** Configuration for the BigCommerce API client */\nconst CONFIG = {\n /** Base URL for BigCommerce API */\n BASE_URL,\n /** Default API version to use */\n DEFAULT_VERSION: 'v3',\n /** Maximum delay in milliseconds for rate limit retries */\n DEFAULT_MAX_DELAY: 60e3,\n /** Maximum allowed URL length */\n MAX_URL_LENGTH: 2048,\n /** Default maximum number of retries for rate-limited requests */\n DEFAULT_MAX_RETRIES: 5,\n /** Rate limit header names */\n HEADERS: {\n /** Time window for rate limiting in milliseconds */\n WINDOW: 'x-rate-limit-time-window-ms',\n /** Time to wait before retrying after rate limit in milliseconds */\n RETRY_AFTER: 'x-rate-limit-time-reset-ms',\n /** Total request quota for the time window */\n REQUEST_QUOTA: 'x-rate-limit-requests-quota',\n /** Number of requests remaining in the current window */\n REQUESTS_LEFT: 'x-rate-limit-requests-left',\n },\n} as const;\n\n/** Supported BigCommerce API versions */\nexport type ApiVersion = 'v3' | 'v2';\n\n/**\n * Options for making API requests\n * @template T - Type of the request body\n */\nexport type RequestOptions<T> = {\n /** Base URL to use for the request */\n baseUrl?: string;\n /** API endpoint to call */\n endpoint: string;\n /** HTTP method to use */\n method?: Method;\n /** Request body data */\n body?: T;\n /** API version to use */\n version?: ApiVersion;\n /** Query parameters to append to the URL */\n query?: Record<string, string>;\n /** Options to pass directly to ky */\n kyOptions?: KyOptions;\n};\n\nexport type StoreOptions = {\n /** Base URL to use for the request */\n baseUrl?: string;\n /** BigCommerce store hash */\n storeHash: string;\n /** API access token */\n accessToken: string;\n};\n\n/**\n * Options for rate limit handling\n */\nexport type RateLimitOptions = {\n /** Maximum delay in milliseconds before giving up on rate-limited requests */\n maxDelay?: number;\n /** Maximum number of retries for rate-limited requests */\n maxRetries?: number;\n};\n\n/**\n * Custom error class for API request failures\n * @template T - Type of the error data\n */\nexport class RequestError<T> extends Error {\n constructor(\n public status: number,\n public message: string,\n public data: T | string,\n public cause?: unknown,\n ) {\n super(message, { cause });\n }\n}\n\n/**\n * Makes an API request with rate limit handling\n * @template T - Type of the request body and response\n * @param options - Request options including rate limit settings\n * @returns Promise resolving to the API response\n * @throws {RequestError} If the request fails or rate limit is exceeded\n */\nexport const request = async <T, R>(\n options: RequestOptions<T> &\n RateLimitOptions &\n StoreOptions & {\n logger?: Logger;\n },\n): Promise<R> => {\n const {\n baseUrl = CONFIG.BASE_URL,\n maxDelay = CONFIG.DEFAULT_MAX_DELAY,\n maxRetries = CONFIG.DEFAULT_MAX_RETRIES,\n logger,\n } = options;\n\n let retries = 0;\n let lastError: RequestError<T> | null = null;\n\n while (retries < maxRetries) {\n try {\n return await safeRequest<T, R>({ ...options, baseUrl });\n } catch (error) {\n const err = error as RequestError<T>;\n lastError = err;\n\n if (err.status === 429 && typeof err.data === 'object' && err.data !== null && 'headers' in err.data) {\n const headers = err.data.headers as Record<string, string>;\n const retryAfter = Number.parseInt(headers[CONFIG.HEADERS.RETRY_AFTER]);\n\n logger?.debug(\n {\n retryAfter,\n retries,\n remaining: headers[CONFIG.HEADERS.REQUESTS_LEFT],\n },\n 'Rate limit hit, retrying',\n );\n\n if (Number.isNaN(retryAfter)) {\n throw new RequestError(\n err.status,\n `Failed to parse retry after: ${headers[CONFIG.HEADERS.RETRY_AFTER]}, ${err.message}`,\n err.data,\n err.cause,\n );\n }\n\n if (retryAfter > maxDelay) {\n logger?.warn(\n {\n retryAfter,\n maxDelay,\n },\n 'Rate limit delay exceeds maximum allowed delay',\n );\n throw new RequestError(\n err.status,\n `Rate limit exceeded: ${retryAfter}ms, ${err.message}`,\n err.data,\n err.cause,\n );\n }\n\n await new Promise((resolve) => setTimeout(resolve, retryAfter));\n retries++;\n continue;\n }\n\n throw err;\n }\n }\n\n logger?.error(\n {\n retries,\n error: lastError,\n },\n 'Request failed after maximum retries',\n );\n\n throw lastError ?? new RequestError(500, 'Failed to make request', 'Too many retries after rate limit');\n};\n\n/**\n * Makes a single API request with error handling\n * @template T - Type of the request body and response\n * @param options - Request options\n * @returns Promise resolving to the API response\n * @throws {RequestError} If the request fails\n */\nconst safeRequest = async <T, R>(\n options: RequestOptions<T> & StoreOptions & { logger?: Logger; baseUrl?: string },\n): Promise<R> => {\n const { logger, baseUrl = CONFIG.BASE_URL } = options;\n let res: KyResponse<T>;\n\n try {\n res = await call<T, R>({ ...options, baseUrl });\n } catch (error) {\n if (error instanceof RequestError) {\n throw error;\n }\n\n if (!(error instanceof HTTPError)) {\n logger?.error(\n {\n error:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n },\n 'Unexpected error during request',\n );\n throw error;\n }\n\n let data: unknown;\n let errorMessage = error.message;\n\n try {\n data = await error.response.text();\n try {\n data = JSON.parse(data as string);\n\n if (typeof data === 'object' && data !== null && 'message' in data) {\n errorMessage = data.message as string;\n }\n } catch {\n // If JSON parsing fails, keep the text response\n }\n } catch {\n data = 'Failed to read error response';\n }\n\n logger?.error(\n {\n status: error?.response?.status,\n errorMessage,\n data,\n endpoint: options.endpoint,\n query: options.query,\n body: options.body,\n headers: Object.fromEntries(error?.response?.headers?.entries() ?? []),\n },\n 'HTTP error during request',\n );\n\n throw new RequestError(\n error?.response?.status ?? 500,\n errorMessage,\n {\n data,\n endpoint: options.endpoint,\n query: options.query,\n body: options.body,\n headers: Object.fromEntries(error?.response?.headers?.entries() ?? []),\n },\n error,\n );\n }\n\n const text = await res.text();\n\n if (res.status === 204) {\n return undefined as unknown as R;\n }\n\n try {\n return JSON.parse(text);\n } catch (error) {\n logger?.error(\n {\n status: res.status,\n error:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n },\n 'Failed to parse response',\n );\n throw new RequestError(res.status, `Failed to parse response: ${text}`, text, error);\n }\n};\n\n/**\n * Internal function to make the actual HTTP request\n * @template T - Type of the request body and response\n * @param options - Request options\n * @returns Promise resolving to the raw response\n * @throws {RequestError} If the URL is too long or request fails\n */\nconst call = async <T, R>(\n options: RequestOptions<T> & StoreOptions & { logger?: Logger; baseUrl?: string },\n): Promise<KyResponse<R>> => {\n const {\n storeHash,\n accessToken,\n endpoint,\n method = 'GET',\n body,\n version = CONFIG.DEFAULT_VERSION,\n query,\n logger,\n baseUrl = CONFIG.BASE_URL,\n kyOptions,\n } = options;\n\n const url = `${baseUrl}${storeHash}/${version}/${endpoint.replace(/^\\//, '')}`;\n\n // Check URL length including search params\n const searchParams = query ? new URLSearchParams(query).toString() : '';\n const fullUrl = searchParams ? `${url}?${searchParams}` : url;\n\n if (fullUrl.length > CONFIG.MAX_URL_LENGTH) {\n logger?.error(\n {\n urlLength: fullUrl.length,\n maxLength: CONFIG.MAX_URL_LENGTH,\n },\n 'URL length exceeds maximum allowed length',\n );\n throw new RequestError(\n 400,\n 'URL too long',\n `URL length ${fullUrl.length} exceeds maximum allowed length of ${CONFIG.MAX_URL_LENGTH}`,\n );\n }\n\n const request = {\n method,\n headers: {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'X-Auth-Token': accessToken,\n },\n json: body,\n ...kyOptions,\n };\n\n const response = await ky<R>(fullUrl, request);\n return response;\n};\n", "/**\n * Split an array of strings into chunks by following logic\n *\n * 1. add length of each string + separatorSize to offset\n * 2. if result is greater than `maxLength`, start a new chunk\n * 3. otherwise, add the string to the current chunk until the chunk is of `chunkLength`\n *\n * This function to be used for splitting query params to avoid url length limit\n *\n * @param items array of strings\n * @param options\n * @param options.maxLength max length of the combined strings\n * @param options.chunkLength max length of each chunk\n * @param options.offset offset of the first chunk\n * @param options.separatorSize size of the separator\n */\nexport const chunkStrLength = (\n items: string[],\n options: {\n maxLength?: number;\n chunkLength?: number;\n offset?: number;\n separatorSize?: number;\n } = {},\n) => {\n const { maxLength = 2048, chunkLength = 250, offset = 0, separatorSize = 1 } = options;\n\n const chunks: string[][] = [];\n let currentStrLength = offset;\n let currentChunk: string[] = [];\n\n for (const item of items) {\n const itemLength = encodeURIComponent(item).length;\n const separatorLength = currentChunk.length > 0 ? separatorSize : 0;\n const totalItemLength = itemLength + separatorLength;\n\n // Check if adding this item would exceed limits\n const wouldExceedLength = currentStrLength + totalItemLength > maxLength;\n const wouldExceedCount = currentChunk.length >= chunkLength;\n\n if ((wouldExceedLength || wouldExceedCount) && currentChunk.length > 0) {\n chunks.push(currentChunk);\n currentChunk = [];\n currentStrLength = offset;\n }\n\n // Handle items that are too large even for a new chunk\n if (itemLength + offset > maxLength) {\n // Either skip, truncate, or throw error depending on requirements\n throw new Error(`Item too large: ${itemLength} exceeds maxLength ${maxLength}`);\n }\n\n currentChunk.push(item);\n currentStrLength += totalItemLength;\n }\n\n if (currentChunk.length > 0) {\n chunks.push(currentChunk);\n }\n\n return chunks;\n};\n", "import { V3Resource, Logger } from './core';\nimport { BASE_URL, RateLimitOptions, RequestError, RequestOptions, StoreOptions, request, KyOptions } from './net';\nimport { chunkStrLength } from './util';\n\nconst MAX_PAGE_SIZE = 250;\nconst DEFAULT_CONCURRENCY = 10;\n\n// Helper function to chunk array into smaller arrays\nfunction chunkArray<T>(array: T[], size: number): T[][] {\n return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size));\n}\n\n// Helper function to create range array\nfunction rangeArray(start: number, end: number): number[] {\n return Array.from({ length: end - start + 1 }, (_, i) => start + i);\n}\n\n/**\n * Options for GET requests to the BigCommerce API\n */\nexport type GetOptions = {\n /** Query parameters to include in the request */\n query?: Record<string, string>;\n /** API version to use (v2 or v3) */\n version?: 'v2' | 'v3';\n /** Options to pass directly to ky */\n kyOptions?: KyOptions;\n};\n\n/**\n * Options for POST/PUT requests to the BigCommerce API\n */\nexport type PostOptions<T> = GetOptions & {\n /** Request body data */\n body: T;\n};\n\n/**\n * Options for controlling concurrent request behavior\n */\nexport type ConcurrencyOptions = {\n /** Maximum number of concurrent requests (default: 10) */\n concurrency?: number;\n /** Whether to skip errors and continue processing (default: false) */\n skipErrors?: boolean;\n};\n\n/**\n * Options for querying multiple values against a single filter field\n */\nexport type QueryOptions = Omit<GetOptions, 'version'> &\n ConcurrencyOptions & {\n /** The field name to query against */\n key: string;\n /** Array of values to query for */\n values: (string | number)[];\n } & {\n /** Options to pass directly to ky */\n kyOptions?: KyOptions;\n };\n\n/**\n * Configuration options for the BigCommerce client\n */\nexport type Config = StoreOptions &\n RateLimitOptions &\n ConcurrencyOptions & {\n /** Logger instance */\n logger?: Logger;\n };\n\n/**\n * Client for interacting with the BigCommerce API\n *\n * This client provides methods for making HTTP requests to the BigCommerce API,\n * with support for both v2 and v3 endpoints, pagination, and concurrent requests.\n */\nexport class BigCommerceClient {\n /**\n * Creates a new BigCommerce client instance\n * @param config - Configuration options for the client\n * @param config.baseUrl - The base URL to use for the client (default: https://api.bigcommerce.com)\n * @param config.storeHash - The store hash to use for the client\n * @param config.accessToken - The API access token to use for the client\n * @param config.maxRetries - The maximum number of retries for rate limit errors (default: 5)\n * @param config.maxDelay - Maximum time to wait to retry in case of rate limit errors in milliseconds (default: 60000 - 1 minute). If `X-Rate-Limit-Time-Reset-Ms` header is higher than `maxDelay`, the request will fail immediately.\n * @param config.concurrency - The default concurrency for concurrent methods (default: 10)\n * @param config.skipErrors - Whether to skip errors during concurrent requests (default: false)\n * @param config.logger - Optional logger instance for debugging and error tracking\n */\n constructor(private readonly config: Config) {}\n\n /**\n * Makes a GET request to the BigCommerce API\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @returns Promise resolving to the response data of type `R`\n */\n async get<R>(endpoint: string, options?: GetOptions): Promise<R> {\n return request<never, R>({\n endpoint,\n method: 'GET',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Makes a POST request to the BigCommerce API\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @param options.body - Request body data of type `T`\n * @returns Promise resolving to the response data of type `R`\n */\n async post<T, R>(endpoint: string, options?: PostOptions<T>): Promise<R> {\n return request<T, R>({\n endpoint,\n method: 'POST',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Makes a PUT request to the BigCommerce API\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @param options.body - Request body data of type `T`\n * @returns Promise resolving to the response data of type `R`\n */\n async put<T, R>(endpoint: string, options?: PostOptions<T>): Promise<R> {\n return request<T, R>({\n endpoint,\n method: 'PUT',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Makes a DELETE request to the BigCommerce API\n * @param endpoint - The API endpoint to delete\n * @param options.version - API version to use (v2 or v3) (default: v3)\n * @returns Promise resolving to void\n */\n async delete<R>(endpoint: string, options?: Pick<GetOptions, 'version' | 'query'> & { kyOptions?: KyOptions }): Promise<void> {\n await request<never, R>({\n endpoint,\n method: 'DELETE',\n ...options,\n ...this.config,\n });\n }\n\n /**\n * Executes multiple requests concurrently with controlled concurrency\n * @param requests - Array of request options to execute\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of response data\n */\n async concurrent<T, R>(requests: RequestOptions<T>[], options?: ConcurrencyOptions): Promise<R[]> {\n const skipErrors = options?.skipErrors ?? this.config.skipErrors ?? false;\n const results = await this.concurrentSettled<T, R>(requests, options);\n\n const successfulResults: R[] = [];\n\n for (const result of results) {\n if (result.status === 'fulfilled') {\n successfulResults.push(result.value);\n } else {\n if (!skipErrors) {\n throw result.reason;\n } else {\n this.config.logger?.warn(\n {\n error: result.reason,\n },\n 'Error in concurrent request',\n );\n }\n }\n }\n\n return successfulResults;\n }\n\n /**\n * Lowest level concurrent request method.\n * This method executes requests in chunks and returns bare PromiseSettledResult objects.\n * Use this method if you need to handle errors in a custom way.\n * @param requests - Array of request options to execute\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @returns Promise resolving to array of PromiseSettledResult containing both successful and failed requests\n */\n async concurrentSettled<T, R>(\n requests: RequestOptions<T>[],\n options?: Pick<ConcurrencyOptions, 'concurrency'>,\n ): Promise<PromiseSettledResult<R>[]> {\n const chunkSize = options?.concurrency ?? this.config.concurrency ?? DEFAULT_CONCURRENCY;\n const chunks = chunkArray(requests, chunkSize);\n\n this.config.logger?.debug(\n {\n totalRequests: requests.length,\n chunkSize,\n chunks: chunks.length,\n },\n 'Starting concurrent requests with detailed results',\n );\n\n const allResults: PromiseSettledResult<R>[] = [];\n\n for (const [index, chunk] of chunks.entries()) {\n const responses = await Promise.allSettled(\n chunk.map((opt) =>\n request<T, R>({\n ...opt,\n ...this.config,\n }),\n ),\n );\n\n this.config.logger?.debug(\n {\n chunkIndex: index,\n chunkSize: chunk.length,\n totalRequests: requests.length,\n totalChunks: chunks.length,\n responses: responses.map((response) => response.status),\n },\n 'Completed chunk',\n );\n\n allResults.push(...responses);\n }\n\n return allResults;\n }\n\n /**\n * Collects all pages of data from a paginated v3 API endpoint.\n * This method pulls the first page and uses pagination meta to collect the remaining pages concurrently.\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of all items across all pages\n */\n async collect<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions & { kyOptions?: KyOptions }): Promise<T[]> {\n options = options ?? {};\n\n if (options.query) {\n if (!options.query.limit) {\n options.query.limit = MAX_PAGE_SIZE.toString();\n }\n } else {\n options.query = { limit: MAX_PAGE_SIZE.toString() };\n }\n\n const first = await this.get<V3Resource<T[]>>(endpoint, options);\n\n if (!Array.isArray(first.data) || !first?.meta?.pagination?.total_pages) {\n return first.data;\n }\n\n const results: T[] = [...first.data];\n const pages = first.meta.pagination.total_pages;\n\n if (pages > 1) {\n this.config.logger?.debug(\n {\n totalPages: pages,\n itemsPerPage: first.data.length,\n },\n 'Collecting remaining pages',\n );\n\n const pageRequests = rangeArray(2, pages).map((page) => ({\n endpoint,\n method: 'GET' as const,\n query: {\n ...options.query,\n page: page.toString(),\n },\n kyOptions: options.kyOptions,\n }));\n\n const remainingPages = await this.concurrent<never, V3Resource<T[]>>(pageRequests, options);\n\n remainingPages.forEach((page) => {\n if (Array.isArray(page.data)) {\n results.push(...page.data);\n }\n });\n }\n\n return results;\n }\n\n /**\n * Collects all pages of data from a paginated v2 API endpoint.\n * This method simply pulls all pages concurrently until a 204 is returned in a batch.\n * @param endpoint - The API endpoint to request\n * @param options.query - Query parameters to include in the request\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of all items across all pages\n */\n async collectV2<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions & { kyOptions?: KyOptions }): Promise<T[]> {\n options = options ?? {};\n\n if (options.query) {\n if (!options.query.limit) {\n options.query.limit = MAX_PAGE_SIZE.toString();\n }\n } else {\n options.query = { limit: MAX_PAGE_SIZE.toString() };\n }\n\n let done = false;\n const results: T[] = [];\n let page = 1;\n const concurrency = options.concurrency ?? this.config.concurrency ?? DEFAULT_CONCURRENCY;\n\n while (!done) {\n const pages = rangeArray(page, page + concurrency);\n page += concurrency;\n\n const requests = pages.map((page) => ({\n ...options,\n endpoint,\n version: 'v2' as const,\n query: { ...options.query, page: page.toString() },\n }));\n\n const responses = await Promise.allSettled(requests.map((request) => this.get<T[]>(endpoint, request)));\n\n responses.forEach((response) => {\n if (response.status === 'fulfilled') {\n if (response.value) {\n results.push(...response.value);\n } else {\n done = true;\n }\n } else {\n if (response.reason instanceof RequestError && response.reason.status === 404) {\n done = true;\n } else {\n if (!(options.skipErrors ?? this.config.skipErrors ?? false)) {\n throw response.reason;\n } else {\n this.config.logger?.warn(\n {\n error:\n response.reason instanceof Error\n ? {\n name: response.reason.name,\n message: response.reason.message,\n }\n : response.reason,\n },\n 'Error in collectV2',\n );\n }\n }\n }\n });\n }\n\n return results;\n }\n\n /**\n * Queries multiple values against a single field using the v3 API.\n * If the url + query params are too long, the query will be chunked. Otherwise, this method acts like `collect`.\n * This method does not check for uniqueness of the `values` array.\n *\n * @param endpoint - The API endpoint to request\n * @param options.key - The field name to query against e.g. `sku:in`\n * @param options.values - Array of values to query for e.g. `['123', '456', ...]`\n * @param options.query - Additional query parameters\n * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)\n * @param options.skipErrors - Whether to skip errors and continue processing (the errors will be logged if logger is provided), overrides the client's skipErrors setting (default: false)\n * @returns Promise resolving to array of matching items\n */\n async query<T>(endpoint: string, options: QueryOptions): Promise<T[]> {\n if (options.query) {\n if (!options.query.limit) {\n options.query.limit = MAX_PAGE_SIZE.toString();\n }\n } else {\n options.query = { limit: MAX_PAGE_SIZE.toString() };\n }\n\n const keySize = encodeURIComponent(options.key).length;\n const fullUrl = `${BASE_URL}${this.config.storeHash}/v3/${endpoint}?${new URLSearchParams(options.query).toString()}`;\n const offset = fullUrl.length + keySize + 1;\n const chunkLength = Number.parseInt(options.query?.limit) || MAX_PAGE_SIZE;\n const separatorSize = encodeURIComponent(',').length;\n\n const queryStr = options.values.map((value) => `${value}`);\n const chunks = chunkStrLength(queryStr, {\n separatorSize,\n offset,\n chunkLength,\n });\n\n this.config.logger?.debug(\n {\n offset,\n totalValues: options.values.length,\n chunks: chunks.length,\n valuesPerChunk: chunks[0]?.length,\n separatorSize,\n },\n 'Querying with chunked values',\n );\n\n const requests = chunks.map((chunk) => ({\n ...options,\n endpoint,\n query: { ...options.query, [options.key]: chunk.join(',') },\n }));\n\n const responses = await this.concurrent<never, V3Resource<T[]>>(requests, options);\n\n return responses.flatMap((response) => response.data);\n }\n}\n", "import ky, { HTTPError, KyResponse } from 'ky';\nimport * as jose from 'jose';\nimport { Logger } from './core';\n\n/**\n * Configuration options for BigCommerce authentication\n */\ntype Config = {\n /** The OAuth client ID from BigCommerce */\n clientId: string;\n /** The OAuth client secret from BigCommerce */\n secret: string;\n /** The redirect URI registered with BigCommerce */\n redirectUri: string;\n /** Optional array of scopes to validate during auth callback */\n scopes?: string[];\n /** Optional logger instance */\n logger?: Logger;\n};\n\nconst GRANT_TYPE = 'authorization_code';\nconst TOKEN_ENDPOINT = 'https://login.bigcommerce.com/oauth2/token';\nconst ISSUER = 'bc';\n\n/**\n * Query parameters received from BigCommerce auth callback\n */\ntype AuthQuery = {\n /** The authorization code from BigCommerce */\n code: string;\n /** The granted OAuth scopes */\n scope: string;\n /** The store context */\n context: string;\n};\n\n/**\n * Request payload for token endpoint\n */\ntype TokenRequest = {\n client_id: string;\n client_secret: string;\n code: string;\n context: string;\n scope: string;\n grant_type: typeof GRANT_TYPE;\n redirect_uri: string;\n};\n\n/**\n * User information returned from BigCommerce\n */\nexport type User = {\n /** The user's ID */\n id: number;\n /** The user's username */\n username: string;\n /** The user's email address */\n email: string;\n};\n\n/**\n * Response from BigCommerce token endpoint\n */\nexport type TokenResponse = {\n /** The OAuth access token */\n access_token: string;\n /** The granted OAuth scopes */\n scope: string;\n /** Information about the authenticated user */\n user: User;\n /** Information about the store owner */\n owner: User;\n /** The store context */\n context: string;\n /** The BigCommerce account UUID */\n account_uuid: string;\n};\n\n/**\n * JWT claims from BigCommerce\n */\nexport type Claims = {\n /** JWT audience */\n aud: string;\n /** JWT issuer */\n iss: string;\n /** JWT issued at timestamp */\n iat: number;\n /** JWT not before timestamp */\n nbf: number;\n /** JWT expiration timestamp */\n exp: number;\n /** JWT unique identifier */\n jti: string;\n /** JWT subject */\n sub: string;\n /** Information about the authenticated user */\n user: {\n id: number;\n email: string;\n locale: string;\n };\n /** Information about the store owner */\n owner: {\n id: number;\n email: string;\n };\n /** The store URL */\n url: string;\n /** The channel ID (if applicable) */\n channel_id: number | null;\n};\n\n/**\n * Handles authentication with BigCommerce OAuth\n */\nexport class BigCommerceAuth {\n /**\n * Creates a new BigCommerceAuth instance for handling OAuth authentication\n * @param config - Configuration options for BigCommerce authentication\n * @param config.clientId - The OAuth client ID from BigCommerce\n * @param config.secret - The OAuth client secret from BigCommerce\n * @param config.redirectUri - The redirect URI registered with BigCommerce\n * @param config.scopes - Optional array of scopes to validate during auth callback\n * @param config.logger - Optional logger instance for debugging and error tracking\n * @throws {Error} If the redirect URI is invalid\n */\n constructor(private readonly config: Config) {\n try {\n new URL(this.config.redirectUri);\n } catch (error) {\n throw new Error('Invalid redirect URI', { cause: error });\n }\n }\n\n /**\n * Requests an access token from BigCommerce\n * @param data - Either a query string, URLSearchParams, or AuthQuery object containing auth callback data\n * @returns Promise resolving to the token response\n */\n async requestToken(data: string | AuthQuery | URLSearchParams): Promise<TokenResponse> {\n const query = typeof data === 'string' || data instanceof URLSearchParams ? this.parseQueryString(data) : data;\n\n this.validateScopes(query.scope);\n\n const tokenRequest: TokenRequest = {\n client_id: this.config.clientId,\n client_secret: this.config.secret,\n ...query,\n grant_type: GRANT_TYPE,\n redirect_uri: this.config.redirectUri,\n };\n\n this.config.logger?.debug(\n {\n clientId: this.config.clientId,\n context: query.context,\n scopes: query.scope,\n },\n 'Requesting OAuth token',\n );\n\n let res: KyResponse;\n\n try {\n res = await ky<TokenResponse>(TOKEN_ENDPOINT, {\n method: 'POST',\n json: tokenRequest,\n });\n } catch (error) {\n if (error instanceof HTTPError) {\n const text = await error.response.text();\n\n this.config.logger?.error({\n err: {\n name: error.name,\n message: error.message,\n text,\n },\n });\n\n throw new Error(`Failed to request token. BC returned: ${text}`, { cause: error });\n }\n\n this.config.logger?.error({\n err:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n });\n\n throw new Error(`Failed to request token`, { cause: error });\n }\n\n return res.json();\n }\n\n /**\n * Verifies a JWT payload from BigCommerce\n * @param jwtPayload - The JWT string to verify\n * @param storeHash - The store hash for the BigCommerce store\n * @returns Promise resolving to the verified JWT claims\n * @throws {Error} If the JWT is invalid\n */\n async verify(jwtPayload: string, storeHash: string): Promise<Claims> {\n try {\n const secret = new TextEncoder().encode(this.config.secret);\n\n const { payload }: { payload: Claims } = await jose.jwtVerify(jwtPayload, secret, {\n audience: this.config.clientId,\n issuer: ISSUER,\n subject: `stores/${storeHash}`,\n });\n\n this.config.logger?.debug(\n {\n userId: payload.user?.id,\n storeHash: payload.sub.split('/')[1],\n },\n 'JWT verified successfully',\n );\n\n return payload;\n } catch (error) {\n this.config.logger?.error({\n error:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n }\n : error,\n });\n\n throw new Error('Invalid JWT payload', { cause: error });\n }\n }\n\n /**\n * Parses and validates a query string from BigCommerce auth callback\n * @param queryString - The query string to parse\n * @returns The parsed auth query parameters\n * @throws {Error} If required parameters are missing or scopes are invalid\n */\n private parseQueryString(queryString: string | URLSearchParams): AuthQuery {\n const params = typeof queryString === 'string' ? new URLSearchParams(queryString) : queryString;\n\n // Get required parameters\n const code = params.get('code');\n const scope = params.get('scope');\n const context = params.get('context');\n\n // Validate required parameters\n if (!code) {\n throw new Error('No code found in query string');\n }\n\n if (!scope) {\n throw new Error('No scope found in query string');\n } else if (this.config.scopes?.length) {\n this.validateScopes(scope);\n }\n\n if (!context) {\n throw new Error('No context found in query string');\n }\n\n return {\n code,\n scope,\n context,\n };\n }\n\n /**\n * Validates that the granted scopes match the expected scopes\n * @param scopes - Space-separated list of granted scopes\n * @throws {Error} If the scopes don't match the expected scopes\n */\n private validateScopes(scopes: string) {\n if (!this.config.scopes) {\n return;\n }\n\n const grantedScopes = scopes.split(' ');\n const requiredScopes = this.config.scopes;\n const missingScopes = requiredScopes.filter((scope) => !grantedScopes.includes(scope));\n\n if (missingScopes.length) {\n throw new Error(`Scope mismatch: ${scopes}; expected: ${this.config.scopes.join(' ')}`);\n }\n }\n}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,UAAM,iBAAiB,KAAK,OAAO;AACnC,UAAM,gBAAgB,eAAe,OAAO,CAAC,UAAU,CAAC,cAAc,SAAS,KAAK,CAAC;AAErF,QAAI,cAAc,QAAQ;AACtB,YAAM,IAAI,MAAM,mBAAmB,MAAM,eAAe,KAAK,OAAO,OAAO,KAAK,GAAG,CAAC,EAAE;AAAA,IAC1F;AAAA,EACJ;AACJ;",
|
|
6
6
|
"names": ["request", "page", "request", "ky", "HTTPError"]
|
|
7
7
|
}
|
package/dist/net.d.ts
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
* Network utilities for interacting with the BigCommerce API.
|
|
3
3
|
* Provides rate-limited request handling, error management, and type-safe API calls.
|
|
4
4
|
*/
|
|
5
|
+
import type { Options as KyOptions } from 'ky';
|
|
5
6
|
import { Logger } from './core';
|
|
6
7
|
/** HTTP methods supported by the API */
|
|
7
8
|
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
9
|
+
export type { KyOptions };
|
|
8
10
|
export declare const Methods: Record<string, Method>;
|
|
9
11
|
export declare const BASE_URL = "https://api.bigcommerce.com/stores/";
|
|
10
12
|
/** Supported BigCommerce API versions */
|
|
@@ -26,6 +28,8 @@ export type RequestOptions<T> = {
|
|
|
26
28
|
version?: ApiVersion;
|
|
27
29
|
/** Query parameters to append to the URL */
|
|
28
30
|
query?: Record<string, string>;
|
|
31
|
+
/** Options to pass directly to ky */
|
|
32
|
+
kyOptions?: KyOptions;
|
|
29
33
|
};
|
|
30
34
|
export type StoreOptions = {
|
|
31
35
|
/** Base URL to use for the request */
|