bc-api-client 0.1.4 → 0.2.0

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/auth.d.ts ADDED
@@ -0,0 +1,134 @@
1
+ import { Logger } from './core';
2
+ /**
3
+ * Configuration options for BigCommerce authentication
4
+ */
5
+ type Config = {
6
+ /** The OAuth client ID from BigCommerce */
7
+ clientId: string;
8
+ /** The OAuth client secret from BigCommerce */
9
+ secret: string;
10
+ /** The redirect URI registered with BigCommerce */
11
+ redirectUri: string;
12
+ /** Optional array of scopes to validate during auth callback */
13
+ scopes?: string[];
14
+ /** Optional logger instance */
15
+ logger?: Logger;
16
+ };
17
+ /**
18
+ * Query parameters received from BigCommerce auth callback
19
+ */
20
+ type AuthQuery = {
21
+ /** The authorization code from BigCommerce */
22
+ code: string;
23
+ /** The granted OAuth scopes */
24
+ scope: string;
25
+ /** The store context */
26
+ context: string;
27
+ };
28
+ /**
29
+ * User information returned from BigCommerce
30
+ */
31
+ export type User = {
32
+ /** The user's ID */
33
+ id: number;
34
+ /** The user's username */
35
+ username: string;
36
+ /** The user's email address */
37
+ email: string;
38
+ };
39
+ /**
40
+ * Response from BigCommerce token endpoint
41
+ */
42
+ export type TokenResponse = {
43
+ /** The OAuth access token */
44
+ access_token: string;
45
+ /** The granted OAuth scopes */
46
+ scope: string;
47
+ /** Information about the authenticated user */
48
+ user: User;
49
+ /** Information about the store owner */
50
+ owner: User;
51
+ /** The store context */
52
+ context: string;
53
+ /** The BigCommerce account UUID */
54
+ account_uuid: string;
55
+ };
56
+ /**
57
+ * JWT claims from BigCommerce
58
+ */
59
+ export type Claims = {
60
+ /** JWT audience */
61
+ aud: string;
62
+ /** JWT issuer */
63
+ iss: string;
64
+ /** JWT issued at timestamp */
65
+ iat: number;
66
+ /** JWT not before timestamp */
67
+ nbf: number;
68
+ /** JWT expiration timestamp */
69
+ exp: number;
70
+ /** JWT unique identifier */
71
+ jti: string;
72
+ /** JWT subject */
73
+ sub: string;
74
+ /** Information about the authenticated user */
75
+ user: {
76
+ id: number;
77
+ email: string;
78
+ locale: string;
79
+ };
80
+ /** Information about the store owner */
81
+ owner: {
82
+ id: number;
83
+ email: string;
84
+ };
85
+ /** The store URL */
86
+ url: string;
87
+ /** The channel ID (if applicable) */
88
+ channel_id: number | null;
89
+ };
90
+ /**
91
+ * Handles authentication with BigCommerce OAuth
92
+ */
93
+ export declare class BigCommerceAuth {
94
+ private readonly config;
95
+ /**
96
+ * Creates a new BigCommerceAuth instance for handling OAuth authentication
97
+ * @param config - Configuration options for BigCommerce authentication
98
+ * @param config.clientId - The OAuth client ID from BigCommerce
99
+ * @param config.secret - The OAuth client secret from BigCommerce
100
+ * @param config.redirectUri - The redirect URI registered with BigCommerce
101
+ * @param config.scopes - Optional array of scopes to validate during auth callback
102
+ * @param config.logger - Optional logger instance for debugging and error tracking
103
+ * @throws {Error} If the redirect URI is invalid
104
+ */
105
+ constructor(config: Config);
106
+ /**
107
+ * Requests an access token from BigCommerce
108
+ * @param data - Either a query string, URLSearchParams, or AuthQuery object containing auth callback data
109
+ * @returns Promise resolving to the token response
110
+ */
111
+ requestToken(data: string | AuthQuery | URLSearchParams): Promise<TokenResponse>;
112
+ /**
113
+ * Verifies a JWT payload from BigCommerce
114
+ * @param jwtPayload - The JWT string to verify
115
+ * @param storeHash - The store hash for the BigCommerce store
116
+ * @returns Promise resolving to the verified JWT claims
117
+ * @throws {Error} If the JWT is invalid
118
+ */
119
+ verify(jwtPayload: string, storeHash: string): Promise<Claims>;
120
+ /**
121
+ * Parses and validates a query string from BigCommerce auth callback
122
+ * @param queryString - The query string to parse
123
+ * @returns The parsed auth query parameters
124
+ * @throws {Error} If required parameters are missing or scopes are invalid
125
+ */
126
+ private parseQueryString;
127
+ /**
128
+ * Validates that the granted scopes match the expected scopes
129
+ * @param scopes - Space-separated list of granted scopes
130
+ * @throws {Error} If the scopes don't match the expected scopes
131
+ */
132
+ private validateScopes;
133
+ }
134
+ export {};
@@ -0,0 +1,149 @@
1
+ import { Logger } from './core';
2
+ import { RateLimitOptions, RequestOptions, StoreOptions } from './net';
3
+ /**
4
+ * Options for GET requests to the BigCommerce API
5
+ */
6
+ export type GetOptions = {
7
+ /** Query parameters to include in the request */
8
+ query?: Record<string, string>;
9
+ /** API version to use (v2 or v3) */
10
+ version?: 'v2' | 'v3';
11
+ };
12
+ /**
13
+ * Options for POST/PUT requests to the BigCommerce API
14
+ */
15
+ export type PostOptions<T> = GetOptions & {
16
+ /** Request body data */
17
+ body: T;
18
+ };
19
+ /**
20
+ * Options for controlling concurrent request behavior
21
+ */
22
+ export type ConcurrencyOptions = {
23
+ /** Maximum number of concurrent requests (default: 10) */
24
+ concurrency?: number;
25
+ /** Whether to skip errors and continue processing (default: false) */
26
+ skipErrors?: boolean;
27
+ };
28
+ /**
29
+ * Options for querying multiple values against a single filter field
30
+ */
31
+ export type QueryOptions = Omit<GetOptions, 'version'> & ConcurrencyOptions & {
32
+ /** The field name to query against */
33
+ key: string;
34
+ /** Array of values to query for */
35
+ values: (string | number)[];
36
+ };
37
+ /**
38
+ * Configuration options for the BigCommerce client
39
+ */
40
+ export type Config = StoreOptions & RateLimitOptions & ConcurrencyOptions & {
41
+ /** Logger instance */
42
+ logger?: Logger;
43
+ };
44
+ /**
45
+ * Client for interacting with the BigCommerce API
46
+ *
47
+ * This client provides methods for making HTTP requests to the BigCommerce API,
48
+ * with support for both v2 and v3 endpoints, pagination, and concurrent requests.
49
+ */
50
+ export declare class BigCommerceClient {
51
+ private readonly config;
52
+ /**
53
+ * Creates a new BigCommerce client instance
54
+ * @param config - Configuration options for the client
55
+ * @param config.baseUrl - The base URL to use for the client (default: https://api.bigcommerce.com)
56
+ * @param config.storeHash - The store hash to use for the client
57
+ * @param config.accessToken - The API access token to use for the client
58
+ * @param config.maxRetries - The maximum number of retries for rate limit errors (default: 5)
59
+ * @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.
60
+ * @param config.concurrency - The default concurrency for concurrent methods (default: 10)
61
+ * @param config.skipErrors - Whether to skip errors during concurrent requests (default: false)
62
+ * @param config.logger - Optional logger instance for debugging and error tracking
63
+ */
64
+ constructor(config: Config);
65
+ /**
66
+ * Makes a GET request to the BigCommerce API
67
+ * @param endpoint - The API endpoint to request
68
+ * @param options.query - Query parameters to include in the request
69
+ * @param options.version - API version to use (v2 or v3) (default: v3)
70
+ * @returns Promise resolving to the response data of type `R`
71
+ */
72
+ get<R>(endpoint: string, options?: GetOptions): Promise<R>;
73
+ /**
74
+ * Makes a POST request to the BigCommerce API
75
+ * @param endpoint - The API endpoint to request
76
+ * @param options.query - Query parameters to include in the request
77
+ * @param options.version - API version to use (v2 or v3) (default: v3)
78
+ * @param options.body - Request body data of type `T`
79
+ * @returns Promise resolving to the response data of type `R`
80
+ */
81
+ post<T, R>(endpoint: string, options?: PostOptions<T>): Promise<R>;
82
+ /**
83
+ * Makes a PUT request to the BigCommerce API
84
+ * @param endpoint - The API endpoint to request
85
+ * @param options.query - Query parameters to include in the request
86
+ * @param options.version - API version to use (v2 or v3) (default: v3)
87
+ * @param options.body - Request body data of type `T`
88
+ * @returns Promise resolving to the response data of type `R`
89
+ */
90
+ put<T, R>(endpoint: string, options?: PostOptions<T>): Promise<R>;
91
+ /**
92
+ * Makes a DELETE request to the BigCommerce API
93
+ * @param endpoint - The API endpoint to delete
94
+ * @param options.version - API version to use (v2 or v3) (default: v3)
95
+ * @returns Promise resolving to void
96
+ */
97
+ delete<R>(endpoint: string, options?: Pick<GetOptions, 'version' | 'query'>): Promise<void>;
98
+ /**
99
+ * Executes multiple requests concurrently with controlled concurrency
100
+ * @param requests - Array of request options to execute
101
+ * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)
102
+ * @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)
103
+ * @returns Promise resolving to array of response data
104
+ */
105
+ concurrent<T, R>(requests: RequestOptions<T>[], options?: ConcurrencyOptions): Promise<R[]>;
106
+ /**
107
+ * Lowest level concurrent request method.
108
+ * This method executes requests in chunks and returns bare PromiseSettledResult objects.
109
+ * Use this method if you need to handle errors in a custom way.
110
+ * @param requests - Array of request options to execute
111
+ * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)
112
+ * @returns Promise resolving to array of PromiseSettledResult containing both successful and failed requests
113
+ */
114
+ concurrentSettled<T, R>(requests: RequestOptions<T>[], options?: Pick<ConcurrencyOptions, 'concurrency'>): Promise<PromiseSettledResult<R>[]>;
115
+ /**
116
+ * Collects all pages of data from a paginated v3 API endpoint.
117
+ * This method pulls the first page and uses pagination meta to collect the remaining pages concurrently.
118
+ * @param endpoint - The API endpoint to request
119
+ * @param options.query - Query parameters to include in the request
120
+ * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)
121
+ * @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
+ * @returns Promise resolving to array of all items across all pages
123
+ */
124
+ collect<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]>;
125
+ /**
126
+ * Collects all pages of data from a paginated v2 API endpoint.
127
+ * This method simply pulls all pages concurrently until a 204 is returned in a batch.
128
+ * @param endpoint - The API endpoint to request
129
+ * @param options.query - Query parameters to include in the request
130
+ * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)
131
+ * @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
+ * @returns Promise resolving to array of all items across all pages
133
+ */
134
+ collectV2<T>(endpoint: string, options?: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]>;
135
+ /**
136
+ * Queries multiple values against a single field using the v3 API.
137
+ * If the url + query params are too long, the query will be chunked. Otherwise, this method acts like `collect`.
138
+ * This method does not check for uniqueness of the `values` array.
139
+ *
140
+ * @param endpoint - The API endpoint to request
141
+ * @param options.key - The field name to query against e.g. `sku:in`
142
+ * @param options.values - Array of values to query for e.g. `['123', '456', ...]`
143
+ * @param options.query - Additional query parameters
144
+ * @param options.concurrency - Maximum number of concurrent requests, overrides the client's concurrency setting (default: 10)
145
+ * @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)
146
+ * @returns Promise resolving to array of matching items
147
+ */
148
+ query<T>(endpoint: string, options: QueryOptions): Promise<T[]>;
149
+ }
package/dist/core.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ export type Pagination = {
2
+ total: number;
3
+ count: number;
4
+ per_page: number;
5
+ current_page: number;
6
+ total_pages: number;
7
+ links: {
8
+ previous: string | null;
9
+ current: string;
10
+ next: string | null;
11
+ };
12
+ };
13
+ export type V3Resource<T> = {
14
+ data: T;
15
+ meta: {
16
+ pagination: Pagination;
17
+ };
18
+ };
19
+ /**
20
+ * Logger interface for logging messages and data, Pino compatible by default
21
+ */
22
+ export interface Logger {
23
+ debug: (data: unknown, message?: string) => void;
24
+ info: (data: unknown, message?: string) => void;
25
+ warn: (data: unknown, message?: string) => void;
26
+ error: (data: unknown, message?: string) => void;
27
+ }