bc-api-client 0.1.0-beta.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2025 Arthur Nikolaienko <kernelpanic99.npm@pm.me>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ # Bigcommerce management API client and JWT authenticator
2
+
3
+ <span style="color:orange">BETA VERSION - Use at your own risk</span>
4
+
5
+ An opinionated and minimalistic client focusing on simplicity and performance.
6
+ Features (or antifeatures - depends on your opinion)
7
+ - Node 20+ LTS, ESM
8
+ - Bring Your Own Types
9
+ - Basic API methods (get, post, put, delete)
10
+ - Advanced methods for concurrent querying and fetching
11
+ - All methods are generic
12
+ - Rate limit handling
13
+ - App authenticator module. Request token and verify JWT.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install bc-api-client
19
+ # or
20
+ pnpm add bc-api-client
21
+ # or
22
+ yarn add bc-api-client
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### API Client
28
+
29
+ ```typescript
30
+ import { BigCommerceClient, V3Response } from 'bigcommerce-client';
31
+ import { bc } from 'bigcommerce-client/endpoints';
32
+
33
+ type MyProduct = {
34
+ id: number;
35
+ name: string;
36
+ sku: string;
37
+ inventory_level: number;
38
+ }
39
+
40
+ const fields = 'id,name,sku,inventory_level';
41
+
42
+ const client = new BigCommerceClient({
43
+ storeHash: 'your-store-hash',
44
+ accessToken: 'your-access-token'
45
+ });
46
+
47
+ // Basic GET request
48
+ const products = await client.get<V3Response<MyProduct[]>>({
49
+ endpoint: bc.products.path,
50
+ query: { 'include_fields': fields },
51
+ });
52
+
53
+ // Low level concurrent requests with error handling
54
+ const results = await client.concurrent<never, V3Response<MyProduct>>(
55
+ [
56
+ { method: 'GET', endpoint: bc.products.byId(1), query: { include_fields: fields }},
57
+ { method: 'GET', endpoint: bc.products.byId(2), query: { include_fields: fields }},
58
+ ],
59
+ {
60
+ concurrency: 10,
61
+ skipErrors: true // Optional: skip failed requests instead of throwing
62
+ }
63
+ );
64
+
65
+ // Collect all pages from v3 endpoint
66
+ const allProducts = await client.collect<MyProduct>({
67
+ endpoint: bc.products.path,
68
+ query: {
69
+ include_fields: fields,
70
+ },
71
+ concurrency: 10, // Optional: control concurrent requests
72
+ skipErrors: true // Optional: skip failed requests
73
+ });
74
+
75
+ // Collect all pages from v2 endpoint
76
+ type MyOrder = {
77
+ id: number;
78
+ status: string;
79
+ }
80
+
81
+ const orders = await client.collectV2<MyOrder>({
82
+ endpoint: bc.orders.v2.path,
83
+ query: {
84
+ limit: '5',
85
+ },
86
+ concurrency: 10, // Optional: control concurrent requests
87
+ skipErrors: true // Optional: skip failed requests
88
+ });
89
+
90
+ // Query with multiple filter values
91
+ // Note: productIds would be a large array of IDs in a real scenario
92
+ const productIds = [1, 2, 3, 4, 5]; // Example IDs
93
+
94
+ const filteredProducts = await client.query<MyProduct>({
95
+ endpoint: bc.products.path,
96
+ key: 'id:in',
97
+ values: productIds,
98
+ query: {
99
+ include_fields: fields,
100
+ },
101
+ concurrency: 10, // Optional: control concurrent requests
102
+ skipErrors: true // Optional: skip failed requests
103
+ });
104
+ ```
105
+
106
+ ### Authentication
107
+
108
+ ```typescript
109
+ import { BigCommerceAuth } from 'bigcommerce-client';
110
+
111
+ const auth = new BigCommerceAuth({
112
+ clientId: 'your-client-id',
113
+ secret: 'your-client-secret',
114
+ redirectUri: 'your-redirect-uri',
115
+ storeHash: 'your-store-hash'
116
+ });
117
+
118
+ // Request token
119
+ const token = await auth.requestToken(authQuery);
120
+
121
+ // Verify JWT
122
+ const claims = await auth.verify(jwtPayload);
123
+ ```
124
+
125
+ ## API
126
+
127
+ ### BigCommerceClient
128
+
129
+ #### `get<R>(options: GetOptions): Promise<R>`
130
+ Makes a GET request to the BigCommerce API.
131
+
132
+ #### `post<T, R>(options: PostOptions<T>): Promise<R>`
133
+ Makes a POST request to the BigCommerce API.
134
+
135
+ #### `put<T, R>(options: PostOptions<T>): Promise<R>`
136
+ Makes a PUT request to the BigCommerce API.
137
+
138
+ #### `delete<R>(endpoint: string): Promise<void>`
139
+ Makes a DELETE request to the BigCommerce API.
140
+
141
+ #### `concurrent<T, R>(requests: RequestOptions<T>[], options: ConcurrencyOptions): Promise<R[]>`
142
+ Executes multiple requests concurrently with rate limit handling. Options:
143
+ - `concurrency`: number of concurrent requests (default: 10)
144
+ - `skipErrors`: whether to skip failed requests instead of throwing (default: false)
145
+
146
+ #### `collect<T>(options: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]>`
147
+ Automatically fetches all pages of a paginated v3 endpoint. Supports the same concurrency options as `concurrent`.
148
+
149
+ #### `collectV2<T>(options: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]>`
150
+ Automatically fetches all pages of a paginated v2 endpoint. Supports the same concurrency options as `concurrent`.
151
+
152
+ #### `query<T>(options: QueryOptions): Promise<T[]>`
153
+ Executes a query with multiple filter values, automatically handling large value sets by chunking them into smaller requests. Options:
154
+ - `endpoint`: API endpoint to query
155
+ - `key`: Filter key (e.g. 'id:in')
156
+ - `values`: Array of values to filter by
157
+ - `query`: Additional query parameters
158
+ - `concurrency`: number of concurrent requests (default: 10)
159
+ - `skipErrors`: whether to skip failed requests instead of throwing (default: false)
160
+
161
+ ### BigCommerceAuth
162
+
163
+ #### `requestToken(data: string | AuthQuery): Promise<TokenResponse>`
164
+ Requests an access token from BigCommerce OAuth.
165
+
166
+ #### `verify(jwtPayload: string): Promise<Claims>`
167
+ Verifies a JWT payload from BigCommerce.
168
+
169
+ ## License
170
+
171
+ MIT
172
+
package/dist/auth.d.ts ADDED
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Configuration options for BigCommerce authentication
3
+ */
4
+ type Config = {
5
+ /** The OAuth client ID from BigCommerce */
6
+ clientId: string;
7
+ /** The OAuth client secret from BigCommerce */
8
+ secret: string;
9
+ /** The redirect URI registered with BigCommerce */
10
+ redirectUri: string;
11
+ /** The store hash for the BigCommerce store */
12
+ storeHash: string;
13
+ /** Optional array of scopes to validate during auth callback */
14
+ scopes?: string[];
15
+ };
16
+ /**
17
+ * Query parameters received from BigCommerce auth callback
18
+ */
19
+ type AuthQuery = {
20
+ /** The BigCommerce account UUID */
21
+ account_uuid: string;
22
+ /** The authorization code from BigCommerce */
23
+ code: string;
24
+ /** The granted OAuth scopes */
25
+ scope: string;
26
+ /** The store context */
27
+ context: string;
28
+ };
29
+ /**
30
+ * User information returned from BigCommerce
31
+ */
32
+ export type User = {
33
+ /** The user's ID */
34
+ id: number;
35
+ /** The user's username */
36
+ username: string;
37
+ /** The user's email address */
38
+ email: string;
39
+ };
40
+ /**
41
+ * Response from BigCommerce token endpoint
42
+ */
43
+ export type TokenResponse = {
44
+ /** The OAuth access token */
45
+ access_token: string;
46
+ /** The granted OAuth scopes */
47
+ scope: string;
48
+ /** Information about the authenticated user */
49
+ user: User;
50
+ /** Information about the store owner */
51
+ owner: User;
52
+ /** The store context */
53
+ context: string;
54
+ /** The BigCommerce account UUID */
55
+ account_uuid: string;
56
+ };
57
+ /**
58
+ * JWT claims from BigCommerce
59
+ */
60
+ export type Claims = {
61
+ /** JWT audience */
62
+ aud: string;
63
+ /** JWT issuer */
64
+ iss: string;
65
+ /** JWT issued at timestamp */
66
+ iat: number;
67
+ /** JWT not before timestamp */
68
+ nbf: number;
69
+ /** JWT expiration timestamp */
70
+ exp: number;
71
+ /** JWT unique identifier */
72
+ jti: string;
73
+ /** JWT subject */
74
+ sub: string;
75
+ /** Information about the authenticated user */
76
+ user: {
77
+ id: number;
78
+ email: string;
79
+ locale: string;
80
+ };
81
+ /** Information about the store owner */
82
+ owner: {
83
+ id: number;
84
+ email: string;
85
+ };
86
+ /** The store URL */
87
+ url: string;
88
+ /** The channel ID (if applicable) */
89
+ channel_id: number | null;
90
+ };
91
+ /**
92
+ * Handles authentication with BigCommerce OAuth
93
+ */
94
+ export declare class BigCommerceAuth {
95
+ private readonly config;
96
+ /**
97
+ * Creates a new BigCommerceAuth instance
98
+ * @param config - Configuration options for BigCommerce authentication
99
+ * @throws {Error} If the redirect URI is invalid
100
+ */
101
+ constructor(config: Config);
102
+ /**
103
+ * Requests an access token from BigCommerce
104
+ * @param data - Either a query string or AuthQuery object containing auth callback data
105
+ * @returns Promise resolving to the token response
106
+ */
107
+ requestToken(data: string | AuthQuery): Promise<TokenResponse>;
108
+ /**
109
+ * Verifies a JWT payload from BigCommerce
110
+ * @param jwtPayload - The JWT string to verify
111
+ * @returns Promise resolving to the verified JWT claims
112
+ * @throws {Error} If the JWT is invalid
113
+ */
114
+ verify(jwtPayload: string): Promise<Claims>;
115
+ /**
116
+ * Parses and validates a query string from BigCommerce auth callback
117
+ * @param queryString - The query string to parse
118
+ * @returns The parsed auth query parameters
119
+ * @throws {Error} If required parameters are missing or scopes are invalid
120
+ */
121
+ private parseQueryString;
122
+ /**
123
+ * Validates that the granted scopes match the expected scopes
124
+ * @param scopes - Space-separated list of granted scopes
125
+ * @throws {Error} If the scopes don't match the expected scopes
126
+ */
127
+ private validateScopes;
128
+ }
129
+ export {};
@@ -0,0 +1,30 @@
1
+ import { RateLimitOptions, RequestOptions, StoreOptions } from './net';
2
+ export type GetOptions = {
3
+ endpoint: string;
4
+ query?: Record<string, string>;
5
+ version?: 'v2' | 'v3';
6
+ };
7
+ export type PostOptions<T> = GetOptions & {
8
+ body: T;
9
+ };
10
+ export type ConcurrencyOptions = {
11
+ concurrency?: number;
12
+ skipErrors?: boolean;
13
+ };
14
+ export type QueryOptions = Omit<GetOptions, 'version'> & ConcurrencyOptions & {
15
+ key: string;
16
+ values: (string | number)[];
17
+ };
18
+ export type Config = StoreOptions & RateLimitOptions;
19
+ export declare class BigCommerceClient {
20
+ private readonly config;
21
+ constructor(config: Config);
22
+ get<R>(options: GetOptions): Promise<R>;
23
+ post<T, R>(options: PostOptions<T>): Promise<R>;
24
+ put<T, R>(options: PostOptions<T>): Promise<R>;
25
+ delete<R>(endpoint: string): Promise<void>;
26
+ concurrent<T, R>(requests: RequestOptions<T>[], options: ConcurrencyOptions): Promise<R[]>;
27
+ collect<T>(options: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]>;
28
+ collectV2<T>(options: Omit<GetOptions, 'version'> & ConcurrencyOptions): Promise<T[]>;
29
+ query<T>(options: QueryOptions): Promise<T[]>;
30
+ }
package/dist/core.d.ts ADDED
@@ -0,0 +1,18 @@
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
+ };
@@ -0,0 +1,3 @@
1
+ export * from './client';
2
+ export * from './core';
3
+ export * from './auth';