omni-sync-sdk 0.2.0 → 0.4.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/README.md +1430 -191
- package/dist/index.d.mts +186 -27
- package/dist/index.d.ts +186 -27
- package/dist/index.js +481 -37
- package/dist/index.mjs +481 -37
- package/package.json +24 -3
package/dist/index.js
CHANGED
|
@@ -35,18 +35,86 @@ var DEFAULT_BASE_URL = "https://api.omni-sync.com";
|
|
|
35
35
|
var DEFAULT_TIMEOUT = 3e4;
|
|
36
36
|
var OmniSyncClient = class {
|
|
37
37
|
constructor(options) {
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
this.customerToken = null;
|
|
39
|
+
if (!options.apiKey && !options.storeId && !options.connectionId) {
|
|
40
|
+
throw new Error("OmniSyncClient: either connectionId, apiKey, or storeId is required");
|
|
40
41
|
}
|
|
41
|
-
if (!options.apiKey.startsWith("omni_")) {
|
|
42
|
+
if (options.apiKey && !options.apiKey.startsWith("omni_")) {
|
|
42
43
|
console.warn('OmniSyncClient: apiKey should start with "omni_"');
|
|
43
44
|
}
|
|
45
|
+
if (options.connectionId && !options.connectionId.startsWith("vc_")) {
|
|
46
|
+
console.warn('OmniSyncClient: connectionId should start with "vc_"');
|
|
47
|
+
}
|
|
48
|
+
if (options.apiKey && typeof window !== "undefined") {
|
|
49
|
+
console.warn(
|
|
50
|
+
"OmniSyncClient: WARNING - API key detected in browser environment. This is a security risk! Use connectionId or storeId for frontend applications."
|
|
51
|
+
);
|
|
52
|
+
}
|
|
44
53
|
this.apiKey = options.apiKey;
|
|
54
|
+
this.storeId = options.storeId;
|
|
55
|
+
this.connectionId = options.connectionId;
|
|
45
56
|
this.baseUrl = (options.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
46
57
|
this.timeout = options.timeout || DEFAULT_TIMEOUT;
|
|
47
58
|
}
|
|
59
|
+
// -------------------- Mode Detection --------------------
|
|
60
|
+
/**
|
|
61
|
+
* Check if client is in vibe-coded mode (using connectionId)
|
|
62
|
+
*/
|
|
63
|
+
isVibeCodedMode() {
|
|
64
|
+
return !!this.connectionId && !this.apiKey;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if client is in storefront mode (using storeId)
|
|
68
|
+
*/
|
|
69
|
+
isStorefrontMode() {
|
|
70
|
+
return !!this.storeId && !this.apiKey && !this.connectionId;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Check if client is in admin mode (using apiKey)
|
|
74
|
+
*/
|
|
75
|
+
isAdminMode() {
|
|
76
|
+
return !!this.apiKey;
|
|
77
|
+
}
|
|
78
|
+
// -------------------- Customer Token Management --------------------
|
|
79
|
+
/**
|
|
80
|
+
* Set the customer authentication token (obtained from login/register).
|
|
81
|
+
* Required for accessing customer-specific data in storefront mode.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const auth = await omni.loginCustomer('user@example.com', 'password');
|
|
86
|
+
* omni.setCustomerToken(auth.token);
|
|
87
|
+
*
|
|
88
|
+
* // Now can access customer data
|
|
89
|
+
* const profile = await omni.getMyProfile();
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
setCustomerToken(token) {
|
|
93
|
+
this.customerToken = token;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get the current customer token
|
|
97
|
+
*/
|
|
98
|
+
getCustomerToken() {
|
|
99
|
+
return this.customerToken;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Clear the customer token (logout)
|
|
103
|
+
*/
|
|
104
|
+
clearCustomerToken() {
|
|
105
|
+
this.customerToken = null;
|
|
106
|
+
}
|
|
48
107
|
// -------------------- Private Methods --------------------
|
|
49
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Make a request to the Admin API (requires apiKey)
|
|
110
|
+
*/
|
|
111
|
+
async adminRequest(method, path, body, queryParams) {
|
|
112
|
+
if (!this.apiKey) {
|
|
113
|
+
throw new OmniSyncError(
|
|
114
|
+
"This operation requires an API key. Initialize with apiKey instead of storeId.",
|
|
115
|
+
403
|
|
116
|
+
);
|
|
117
|
+
}
|
|
50
118
|
const url = new URL(`${this.baseUrl}${path}`);
|
|
51
119
|
if (queryParams) {
|
|
52
120
|
Object.entries(queryParams).forEach(([key, value]) => {
|
|
@@ -63,7 +131,7 @@ var OmniSyncClient = class {
|
|
|
63
131
|
headers: {
|
|
64
132
|
Authorization: `Bearer ${this.apiKey}`,
|
|
65
133
|
"Content-Type": "application/json",
|
|
66
|
-
"X-SDK-Version": "0.
|
|
134
|
+
"X-SDK-Version": "0.3.0",
|
|
67
135
|
"ngrok-skip-browser-warning": "true"
|
|
68
136
|
},
|
|
69
137
|
body: body ? JSON.stringify(body) : void 0,
|
|
@@ -95,12 +163,149 @@ var OmniSyncClient = class {
|
|
|
95
163
|
throw new OmniSyncError("Unknown error occurred", 0);
|
|
96
164
|
}
|
|
97
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Make a request to the Vibe-Coded API (public, uses connectionId)
|
|
168
|
+
*/
|
|
169
|
+
async vibeCodedRequest(method, path, body, queryParams) {
|
|
170
|
+
if (!this.connectionId) {
|
|
171
|
+
throw new OmniSyncError("connectionId is required for vibe-coded requests", 400);
|
|
172
|
+
}
|
|
173
|
+
const url = new URL(`${this.baseUrl}/api/vc/${this.connectionId}${path}`);
|
|
174
|
+
if (queryParams) {
|
|
175
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
176
|
+
if (value !== void 0) {
|
|
177
|
+
url.searchParams.set(key, String(value));
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
const controller = new AbortController();
|
|
182
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
183
|
+
try {
|
|
184
|
+
const headers = {
|
|
185
|
+
"Content-Type": "application/json",
|
|
186
|
+
"X-SDK-Version": "0.3.0",
|
|
187
|
+
"ngrok-skip-browser-warning": "true"
|
|
188
|
+
};
|
|
189
|
+
const response = await fetch(url.toString(), {
|
|
190
|
+
method,
|
|
191
|
+
headers,
|
|
192
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
193
|
+
signal: controller.signal
|
|
194
|
+
});
|
|
195
|
+
clearTimeout(timeoutId);
|
|
196
|
+
if (!response.ok) {
|
|
197
|
+
const errorData = await response.json().catch(() => ({}));
|
|
198
|
+
throw new OmniSyncError(
|
|
199
|
+
errorData.message || `Request failed with status ${response.status}`,
|
|
200
|
+
response.status,
|
|
201
|
+
errorData
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
const text = await response.text();
|
|
205
|
+
if (!text) return {};
|
|
206
|
+
return JSON.parse(text);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
clearTimeout(timeoutId);
|
|
209
|
+
if (error instanceof OmniSyncError) {
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
if (error instanceof Error) {
|
|
213
|
+
if (error.name === "AbortError") {
|
|
214
|
+
throw new OmniSyncError("Request timeout", 408);
|
|
215
|
+
}
|
|
216
|
+
throw new OmniSyncError(error.message, 0);
|
|
217
|
+
}
|
|
218
|
+
throw new OmniSyncError("Unknown error occurred", 0);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Make a request to the Storefront API (public, uses storeId)
|
|
223
|
+
*/
|
|
224
|
+
async storefrontRequest(method, path, body, queryParams) {
|
|
225
|
+
if (!this.storeId) {
|
|
226
|
+
throw new OmniSyncError("storeId is required for storefront requests", 400);
|
|
227
|
+
}
|
|
228
|
+
const url = new URL(`${this.baseUrl}/stores/${this.storeId}${path}`);
|
|
229
|
+
if (queryParams) {
|
|
230
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
231
|
+
if (value !== void 0) {
|
|
232
|
+
url.searchParams.set(key, String(value));
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
const controller = new AbortController();
|
|
237
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
238
|
+
try {
|
|
239
|
+
const headers = {
|
|
240
|
+
"Content-Type": "application/json",
|
|
241
|
+
"X-SDK-Version": "0.3.0",
|
|
242
|
+
"ngrok-skip-browser-warning": "true"
|
|
243
|
+
};
|
|
244
|
+
if (this.customerToken) {
|
|
245
|
+
headers["Authorization"] = `Bearer ${this.customerToken}`;
|
|
246
|
+
}
|
|
247
|
+
const response = await fetch(url.toString(), {
|
|
248
|
+
method,
|
|
249
|
+
headers,
|
|
250
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
251
|
+
signal: controller.signal
|
|
252
|
+
});
|
|
253
|
+
clearTimeout(timeoutId);
|
|
254
|
+
if (!response.ok) {
|
|
255
|
+
const errorData = await response.json().catch(() => ({}));
|
|
256
|
+
throw new OmniSyncError(
|
|
257
|
+
errorData.message || `Request failed with status ${response.status}`,
|
|
258
|
+
response.status,
|
|
259
|
+
errorData
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
const text = await response.text();
|
|
263
|
+
if (!text) return {};
|
|
264
|
+
return JSON.parse(text);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
clearTimeout(timeoutId);
|
|
267
|
+
if (error instanceof OmniSyncError) {
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
if (error instanceof Error) {
|
|
271
|
+
if (error.name === "AbortError") {
|
|
272
|
+
throw new OmniSyncError("Request timeout", 408);
|
|
273
|
+
}
|
|
274
|
+
throw new OmniSyncError(error.message, 0);
|
|
275
|
+
}
|
|
276
|
+
throw new OmniSyncError("Unknown error occurred", 0);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Smart request - uses storefront or admin API based on client mode
|
|
281
|
+
*/
|
|
282
|
+
async request(method, path, body, queryParams) {
|
|
283
|
+
if (this.apiKey) {
|
|
284
|
+
return this.adminRequest(method, path, body, queryParams);
|
|
285
|
+
}
|
|
286
|
+
return this.storefrontRequest(method, path, body, queryParams);
|
|
287
|
+
}
|
|
288
|
+
// -------------------- Store Info --------------------
|
|
289
|
+
/**
|
|
290
|
+
* Get store information
|
|
291
|
+
* Works in vibe-coded, storefront, and admin mode
|
|
292
|
+
*/
|
|
293
|
+
async getStoreInfo() {
|
|
294
|
+
if (this.isVibeCodedMode()) {
|
|
295
|
+
return this.vibeCodedRequest("GET", "/info");
|
|
296
|
+
}
|
|
297
|
+
if (this.storeId && !this.apiKey) {
|
|
298
|
+
return this.storefrontRequest("GET", "/info");
|
|
299
|
+
}
|
|
300
|
+
return this.adminRequest("GET", "/api/v1/store");
|
|
301
|
+
}
|
|
98
302
|
// -------------------- Products --------------------
|
|
99
303
|
/**
|
|
100
304
|
* Get a list of products with pagination
|
|
305
|
+
* Works in vibe-coded, storefront (public), and admin mode
|
|
101
306
|
*/
|
|
102
307
|
async getProducts(params) {
|
|
103
|
-
|
|
308
|
+
const queryParams = {
|
|
104
309
|
page: params?.page,
|
|
105
310
|
limit: params?.limit,
|
|
106
311
|
search: params?.search,
|
|
@@ -108,13 +313,42 @@ var OmniSyncClient = class {
|
|
|
108
313
|
type: params?.type,
|
|
109
314
|
sortBy: params?.sortBy,
|
|
110
315
|
sortOrder: params?.sortOrder
|
|
111
|
-
}
|
|
316
|
+
};
|
|
317
|
+
if (this.isVibeCodedMode()) {
|
|
318
|
+
return this.vibeCodedRequest(
|
|
319
|
+
"GET",
|
|
320
|
+
"/products",
|
|
321
|
+
void 0,
|
|
322
|
+
queryParams
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
if (this.storeId && !this.apiKey) {
|
|
326
|
+
return this.storefrontRequest(
|
|
327
|
+
"GET",
|
|
328
|
+
"/products",
|
|
329
|
+
void 0,
|
|
330
|
+
queryParams
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
return this.adminRequest(
|
|
334
|
+
"GET",
|
|
335
|
+
"/api/v1/products",
|
|
336
|
+
void 0,
|
|
337
|
+
queryParams
|
|
338
|
+
);
|
|
112
339
|
}
|
|
113
340
|
/**
|
|
114
341
|
* Get a single product by ID
|
|
342
|
+
* Works in vibe-coded, storefront (public), and admin mode
|
|
115
343
|
*/
|
|
116
344
|
async getProduct(productId) {
|
|
117
|
-
|
|
345
|
+
if (this.isVibeCodedMode()) {
|
|
346
|
+
return this.vibeCodedRequest("GET", `/products/${productId}`);
|
|
347
|
+
}
|
|
348
|
+
if (this.storeId && !this.apiKey) {
|
|
349
|
+
return this.storefrontRequest("GET", `/products/${productId}`);
|
|
350
|
+
}
|
|
351
|
+
return this.adminRequest("GET", `/api/v1/products/${productId}`);
|
|
118
352
|
}
|
|
119
353
|
/**
|
|
120
354
|
* Create a new product
|
|
@@ -604,13 +838,6 @@ var OmniSyncClient = class {
|
|
|
604
838
|
async getSyncStatus(jobId) {
|
|
605
839
|
return this.request("GET", `/api/v1/sync/${jobId}`);
|
|
606
840
|
}
|
|
607
|
-
// -------------------- Store Info --------------------
|
|
608
|
-
/**
|
|
609
|
-
* Get information about the connected store
|
|
610
|
-
*/
|
|
611
|
-
async getStoreInfo() {
|
|
612
|
-
return this.request("GET", "/api/v1/store");
|
|
613
|
-
}
|
|
614
841
|
// -------------------- Coupons --------------------
|
|
615
842
|
/**
|
|
616
843
|
* Get a list of coupons with pagination
|
|
@@ -753,7 +980,13 @@ var OmniSyncClient = class {
|
|
|
753
980
|
* ```
|
|
754
981
|
*/
|
|
755
982
|
async loginCustomer(email, password) {
|
|
756
|
-
|
|
983
|
+
if (this.storeId && !this.apiKey) {
|
|
984
|
+
return this.storefrontRequest("POST", "/customers/login", {
|
|
985
|
+
email,
|
|
986
|
+
password
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
return this.adminRequest("POST", "/api/v1/customers/login", {
|
|
757
990
|
email,
|
|
758
991
|
password
|
|
759
992
|
});
|
|
@@ -772,13 +1005,21 @@ var OmniSyncClient = class {
|
|
|
772
1005
|
* ```
|
|
773
1006
|
*/
|
|
774
1007
|
async registerCustomer(data) {
|
|
775
|
-
|
|
1008
|
+
if (this.storeId && !this.apiKey) {
|
|
1009
|
+
return this.storefrontRequest("POST", "/customers/register", data);
|
|
1010
|
+
}
|
|
1011
|
+
return this.adminRequest("POST", "/api/v1/customers/register", data);
|
|
776
1012
|
}
|
|
777
1013
|
/**
|
|
778
1014
|
* Request a password reset email for a customer
|
|
779
1015
|
*/
|
|
780
1016
|
async forgotPassword(email) {
|
|
781
|
-
|
|
1017
|
+
if (this.storeId && !this.apiKey) {
|
|
1018
|
+
return this.storefrontRequest("POST", "/customers/forgot-password", {
|
|
1019
|
+
email
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
return this.adminRequest("POST", "/api/v1/customers/forgot-password", {
|
|
782
1023
|
email
|
|
783
1024
|
});
|
|
784
1025
|
}
|
|
@@ -858,7 +1099,10 @@ var OmniSyncClient = class {
|
|
|
858
1099
|
* ```
|
|
859
1100
|
*/
|
|
860
1101
|
async createCart() {
|
|
861
|
-
|
|
1102
|
+
if (this.storeId && !this.apiKey) {
|
|
1103
|
+
return this.storefrontRequest("POST", "/cart");
|
|
1104
|
+
}
|
|
1105
|
+
return this.adminRequest("POST", "/api/v1/cart");
|
|
862
1106
|
}
|
|
863
1107
|
/**
|
|
864
1108
|
* Get a cart by session token (for guest users)
|
|
@@ -870,7 +1114,10 @@ var OmniSyncClient = class {
|
|
|
870
1114
|
* ```
|
|
871
1115
|
*/
|
|
872
1116
|
async getCartBySession(sessionToken) {
|
|
873
|
-
|
|
1117
|
+
if (this.storeId && !this.apiKey) {
|
|
1118
|
+
return this.storefrontRequest("GET", `/cart/session/${sessionToken}`);
|
|
1119
|
+
}
|
|
1120
|
+
return this.adminRequest("GET", `/api/v1/cart/session/${sessionToken}`);
|
|
874
1121
|
}
|
|
875
1122
|
/**
|
|
876
1123
|
* Get a cart by customer ID (for authenticated users)
|
|
@@ -882,13 +1129,19 @@ var OmniSyncClient = class {
|
|
|
882
1129
|
* ```
|
|
883
1130
|
*/
|
|
884
1131
|
async getCartByCustomer(customerId) {
|
|
885
|
-
|
|
1132
|
+
if (this.storeId && !this.apiKey) {
|
|
1133
|
+
return this.storefrontRequest("GET", `/cart/customer/${customerId}`);
|
|
1134
|
+
}
|
|
1135
|
+
return this.adminRequest("GET", `/api/v1/cart/customer/${customerId}`);
|
|
886
1136
|
}
|
|
887
1137
|
/**
|
|
888
1138
|
* Get a cart by ID
|
|
889
1139
|
*/
|
|
890
1140
|
async getCart(cartId) {
|
|
891
|
-
|
|
1141
|
+
if (this.storeId && !this.apiKey) {
|
|
1142
|
+
return this.storefrontRequest("GET", `/cart/${cartId}`);
|
|
1143
|
+
}
|
|
1144
|
+
return this.adminRequest("GET", `/api/v1/cart/${cartId}`);
|
|
892
1145
|
}
|
|
893
1146
|
/**
|
|
894
1147
|
* Add an item to the cart
|
|
@@ -903,7 +1156,10 @@ var OmniSyncClient = class {
|
|
|
903
1156
|
* ```
|
|
904
1157
|
*/
|
|
905
1158
|
async addToCart(cartId, item) {
|
|
906
|
-
|
|
1159
|
+
if (this.storeId && !this.apiKey) {
|
|
1160
|
+
return this.storefrontRequest("POST", `/cart/${cartId}/items`, item);
|
|
1161
|
+
}
|
|
1162
|
+
return this.adminRequest("POST", `/api/v1/cart/${cartId}/items`, item);
|
|
907
1163
|
}
|
|
908
1164
|
/**
|
|
909
1165
|
* Update an item quantity in the cart
|
|
@@ -914,7 +1170,10 @@ var OmniSyncClient = class {
|
|
|
914
1170
|
* ```
|
|
915
1171
|
*/
|
|
916
1172
|
async updateCartItem(cartId, itemId, data) {
|
|
917
|
-
|
|
1173
|
+
if (this.storeId && !this.apiKey) {
|
|
1174
|
+
return this.storefrontRequest("PATCH", `/cart/${cartId}/items/${itemId}`, data);
|
|
1175
|
+
}
|
|
1176
|
+
return this.adminRequest("PATCH", `/api/v1/cart/${cartId}/items/${itemId}`, data);
|
|
918
1177
|
}
|
|
919
1178
|
/**
|
|
920
1179
|
* Remove an item from the cart
|
|
@@ -925,7 +1184,10 @@ var OmniSyncClient = class {
|
|
|
925
1184
|
* ```
|
|
926
1185
|
*/
|
|
927
1186
|
async removeCartItem(cartId, itemId) {
|
|
928
|
-
|
|
1187
|
+
if (this.storeId && !this.apiKey) {
|
|
1188
|
+
return this.storefrontRequest("DELETE", `/cart/${cartId}/items/${itemId}`);
|
|
1189
|
+
}
|
|
1190
|
+
return this.adminRequest("DELETE", `/api/v1/cart/${cartId}/items/${itemId}`);
|
|
929
1191
|
}
|
|
930
1192
|
/**
|
|
931
1193
|
* Clear all items from the cart
|
|
@@ -936,7 +1198,11 @@ var OmniSyncClient = class {
|
|
|
936
1198
|
* ```
|
|
937
1199
|
*/
|
|
938
1200
|
async clearCart(cartId) {
|
|
939
|
-
|
|
1201
|
+
if (this.storeId && !this.apiKey) {
|
|
1202
|
+
await this.storefrontRequest("DELETE", `/cart/${cartId}/items`);
|
|
1203
|
+
return;
|
|
1204
|
+
}
|
|
1205
|
+
await this.adminRequest("DELETE", `/api/v1/cart/${cartId}/items`);
|
|
940
1206
|
}
|
|
941
1207
|
/**
|
|
942
1208
|
* Apply a coupon to the cart
|
|
@@ -948,7 +1214,10 @@ var OmniSyncClient = class {
|
|
|
948
1214
|
* ```
|
|
949
1215
|
*/
|
|
950
1216
|
async applyCoupon(cartId, code) {
|
|
951
|
-
|
|
1217
|
+
if (this.storeId && !this.apiKey) {
|
|
1218
|
+
return this.storefrontRequest("POST", `/cart/${cartId}/coupon`, { code });
|
|
1219
|
+
}
|
|
1220
|
+
return this.adminRequest("POST", `/api/v1/cart/${cartId}/coupon`, { code });
|
|
952
1221
|
}
|
|
953
1222
|
/**
|
|
954
1223
|
* Remove a coupon from the cart
|
|
@@ -959,7 +1228,10 @@ var OmniSyncClient = class {
|
|
|
959
1228
|
* ```
|
|
960
1229
|
*/
|
|
961
1230
|
async removeCoupon(cartId) {
|
|
962
|
-
|
|
1231
|
+
if (this.storeId && !this.apiKey) {
|
|
1232
|
+
return this.storefrontRequest("DELETE", `/cart/${cartId}/coupon`);
|
|
1233
|
+
}
|
|
1234
|
+
return this.adminRequest("DELETE", `/api/v1/cart/${cartId}/coupon`);
|
|
963
1235
|
}
|
|
964
1236
|
/**
|
|
965
1237
|
* Merge a guest cart into a customer's cart (after login)
|
|
@@ -975,7 +1247,10 @@ var OmniSyncClient = class {
|
|
|
975
1247
|
* ```
|
|
976
1248
|
*/
|
|
977
1249
|
async mergeCarts(data) {
|
|
978
|
-
|
|
1250
|
+
if (this.storeId && !this.apiKey) {
|
|
1251
|
+
return this.storefrontRequest("POST", "/cart/merge", data);
|
|
1252
|
+
}
|
|
1253
|
+
return this.adminRequest("POST", "/api/v1/cart/merge", data);
|
|
979
1254
|
}
|
|
980
1255
|
// -------------------- Checkout --------------------
|
|
981
1256
|
/**
|
|
@@ -989,7 +1264,10 @@ var OmniSyncClient = class {
|
|
|
989
1264
|
* ```
|
|
990
1265
|
*/
|
|
991
1266
|
async createCheckout(data) {
|
|
992
|
-
|
|
1267
|
+
if (this.storeId && !this.apiKey) {
|
|
1268
|
+
return this.storefrontRequest("POST", "/checkout", data);
|
|
1269
|
+
}
|
|
1270
|
+
return this.adminRequest("POST", "/api/v1/checkout", data);
|
|
993
1271
|
}
|
|
994
1272
|
/**
|
|
995
1273
|
* Get a checkout by ID
|
|
@@ -1001,7 +1279,10 @@ var OmniSyncClient = class {
|
|
|
1001
1279
|
* ```
|
|
1002
1280
|
*/
|
|
1003
1281
|
async getCheckout(checkoutId) {
|
|
1004
|
-
|
|
1282
|
+
if (this.storeId && !this.apiKey) {
|
|
1283
|
+
return this.storefrontRequest("GET", `/checkout/${checkoutId}`);
|
|
1284
|
+
}
|
|
1285
|
+
return this.adminRequest("GET", `/api/v1/checkout/${checkoutId}`);
|
|
1005
1286
|
}
|
|
1006
1287
|
/**
|
|
1007
1288
|
* Set customer information on checkout
|
|
@@ -1016,7 +1297,10 @@ var OmniSyncClient = class {
|
|
|
1016
1297
|
* ```
|
|
1017
1298
|
*/
|
|
1018
1299
|
async setCheckoutCustomer(checkoutId, data) {
|
|
1019
|
-
|
|
1300
|
+
if (this.storeId && !this.apiKey) {
|
|
1301
|
+
return this.storefrontRequest("PATCH", `/checkout/${checkoutId}/customer`, data);
|
|
1302
|
+
}
|
|
1303
|
+
return this.adminRequest("PATCH", `/api/v1/checkout/${checkoutId}/customer`, data);
|
|
1020
1304
|
}
|
|
1021
1305
|
/**
|
|
1022
1306
|
* Set shipping address on checkout
|
|
@@ -1037,7 +1321,14 @@ var OmniSyncClient = class {
|
|
|
1037
1321
|
* ```
|
|
1038
1322
|
*/
|
|
1039
1323
|
async setShippingAddress(checkoutId, address) {
|
|
1040
|
-
|
|
1324
|
+
if (this.storeId && !this.apiKey) {
|
|
1325
|
+
return this.storefrontRequest(
|
|
1326
|
+
"PATCH",
|
|
1327
|
+
`/checkout/${checkoutId}/shipping-address`,
|
|
1328
|
+
address
|
|
1329
|
+
);
|
|
1330
|
+
}
|
|
1331
|
+
return this.adminRequest(
|
|
1041
1332
|
"PATCH",
|
|
1042
1333
|
`/api/v1/checkout/${checkoutId}/shipping-address`,
|
|
1043
1334
|
address
|
|
@@ -1054,7 +1345,16 @@ var OmniSyncClient = class {
|
|
|
1054
1345
|
* ```
|
|
1055
1346
|
*/
|
|
1056
1347
|
async getShippingRates(checkoutId) {
|
|
1057
|
-
|
|
1348
|
+
if (this.storeId && !this.apiKey) {
|
|
1349
|
+
return this.storefrontRequest(
|
|
1350
|
+
"GET",
|
|
1351
|
+
`/checkout/${checkoutId}/shipping-rates`
|
|
1352
|
+
);
|
|
1353
|
+
}
|
|
1354
|
+
return this.adminRequest(
|
|
1355
|
+
"GET",
|
|
1356
|
+
`/api/v1/checkout/${checkoutId}/shipping-rates`
|
|
1357
|
+
);
|
|
1058
1358
|
}
|
|
1059
1359
|
/**
|
|
1060
1360
|
* Select a shipping method for checkout
|
|
@@ -1066,7 +1366,12 @@ var OmniSyncClient = class {
|
|
|
1066
1366
|
* ```
|
|
1067
1367
|
*/
|
|
1068
1368
|
async selectShippingMethod(checkoutId, shippingRateId) {
|
|
1069
|
-
|
|
1369
|
+
if (this.storeId && !this.apiKey) {
|
|
1370
|
+
return this.storefrontRequest("PATCH", `/checkout/${checkoutId}/shipping-method`, {
|
|
1371
|
+
shippingRateId
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
return this.adminRequest("PATCH", `/api/v1/checkout/${checkoutId}/shipping-method`, {
|
|
1070
1375
|
shippingRateId
|
|
1071
1376
|
});
|
|
1072
1377
|
}
|
|
@@ -1095,7 +1400,14 @@ var OmniSyncClient = class {
|
|
|
1095
1400
|
* ```
|
|
1096
1401
|
*/
|
|
1097
1402
|
async setBillingAddress(checkoutId, address) {
|
|
1098
|
-
|
|
1403
|
+
if (this.storeId && !this.apiKey) {
|
|
1404
|
+
return this.storefrontRequest(
|
|
1405
|
+
"PATCH",
|
|
1406
|
+
`/checkout/${checkoutId}/billing-address`,
|
|
1407
|
+
address
|
|
1408
|
+
);
|
|
1409
|
+
}
|
|
1410
|
+
return this.adminRequest(
|
|
1099
1411
|
"PATCH",
|
|
1100
1412
|
`/api/v1/checkout/${checkoutId}/billing-address`,
|
|
1101
1413
|
address
|
|
@@ -1112,11 +1424,143 @@ var OmniSyncClient = class {
|
|
|
1112
1424
|
* ```
|
|
1113
1425
|
*/
|
|
1114
1426
|
async completeCheckout(checkoutId) {
|
|
1115
|
-
|
|
1427
|
+
if (this.storeId && !this.apiKey) {
|
|
1428
|
+
return this.storefrontRequest(
|
|
1429
|
+
"POST",
|
|
1430
|
+
`/checkout/${checkoutId}/complete`
|
|
1431
|
+
);
|
|
1432
|
+
}
|
|
1433
|
+
return this.adminRequest(
|
|
1116
1434
|
"POST",
|
|
1117
1435
|
`/api/v1/checkout/${checkoutId}/complete`
|
|
1118
1436
|
);
|
|
1119
1437
|
}
|
|
1438
|
+
// -------------------- Storefront Customer Self-Service --------------------
|
|
1439
|
+
// These methods require customerToken to be set (call setCustomerToken after login)
|
|
1440
|
+
/**
|
|
1441
|
+
* Get the current customer's profile (requires customerToken)
|
|
1442
|
+
* Only available in storefront mode
|
|
1443
|
+
*
|
|
1444
|
+
* @example
|
|
1445
|
+
* ```typescript
|
|
1446
|
+
* const auth = await omni.loginCustomer('user@example.com', 'password');
|
|
1447
|
+
* omni.setCustomerToken(auth.token);
|
|
1448
|
+
* const profile = await omni.getMyProfile();
|
|
1449
|
+
* ```
|
|
1450
|
+
*/
|
|
1451
|
+
async getMyProfile() {
|
|
1452
|
+
if (!this.storeId) {
|
|
1453
|
+
throw new OmniSyncError("getMyProfile is only available in storefront mode", 400);
|
|
1454
|
+
}
|
|
1455
|
+
if (!this.customerToken) {
|
|
1456
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1457
|
+
}
|
|
1458
|
+
return this.storefrontRequest("GET", "/customers/me");
|
|
1459
|
+
}
|
|
1460
|
+
/**
|
|
1461
|
+
* Update the current customer's profile (requires customerToken)
|
|
1462
|
+
* Only available in storefront mode
|
|
1463
|
+
*/
|
|
1464
|
+
async updateMyProfile(data) {
|
|
1465
|
+
if (!this.storeId) {
|
|
1466
|
+
throw new OmniSyncError("updateMyProfile is only available in storefront mode", 400);
|
|
1467
|
+
}
|
|
1468
|
+
if (!this.customerToken) {
|
|
1469
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1470
|
+
}
|
|
1471
|
+
return this.storefrontRequest("PATCH", "/customers/me", data);
|
|
1472
|
+
}
|
|
1473
|
+
/**
|
|
1474
|
+
* Get the current customer's orders (requires customerToken)
|
|
1475
|
+
* Only available in storefront mode
|
|
1476
|
+
*/
|
|
1477
|
+
async getMyOrders(params) {
|
|
1478
|
+
if (!this.storeId) {
|
|
1479
|
+
throw new OmniSyncError("getMyOrders is only available in storefront mode", 400);
|
|
1480
|
+
}
|
|
1481
|
+
if (!this.customerToken) {
|
|
1482
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1483
|
+
}
|
|
1484
|
+
return this.storefrontRequest(
|
|
1485
|
+
"GET",
|
|
1486
|
+
"/customers/me/orders",
|
|
1487
|
+
void 0,
|
|
1488
|
+
{
|
|
1489
|
+
page: params?.page,
|
|
1490
|
+
limit: params?.limit
|
|
1491
|
+
}
|
|
1492
|
+
);
|
|
1493
|
+
}
|
|
1494
|
+
/**
|
|
1495
|
+
* Get the current customer's addresses (requires customerToken)
|
|
1496
|
+
* Only available in storefront mode
|
|
1497
|
+
*/
|
|
1498
|
+
async getMyAddresses() {
|
|
1499
|
+
if (!this.storeId) {
|
|
1500
|
+
throw new OmniSyncError("getMyAddresses is only available in storefront mode", 400);
|
|
1501
|
+
}
|
|
1502
|
+
if (!this.customerToken) {
|
|
1503
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1504
|
+
}
|
|
1505
|
+
return this.storefrontRequest("GET", "/customers/me/addresses");
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* Add an address to the current customer (requires customerToken)
|
|
1509
|
+
* Only available in storefront mode
|
|
1510
|
+
*/
|
|
1511
|
+
async addMyAddress(address) {
|
|
1512
|
+
if (!this.storeId) {
|
|
1513
|
+
throw new OmniSyncError("addMyAddress is only available in storefront mode", 400);
|
|
1514
|
+
}
|
|
1515
|
+
if (!this.customerToken) {
|
|
1516
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1517
|
+
}
|
|
1518
|
+
return this.storefrontRequest("POST", "/customers/me/addresses", address);
|
|
1519
|
+
}
|
|
1520
|
+
/**
|
|
1521
|
+
* Update a customer address (requires customerToken)
|
|
1522
|
+
* Only available in storefront mode
|
|
1523
|
+
*/
|
|
1524
|
+
async updateMyAddress(addressId, data) {
|
|
1525
|
+
if (!this.storeId) {
|
|
1526
|
+
throw new OmniSyncError("updateMyAddress is only available in storefront mode", 400);
|
|
1527
|
+
}
|
|
1528
|
+
if (!this.customerToken) {
|
|
1529
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1530
|
+
}
|
|
1531
|
+
return this.storefrontRequest(
|
|
1532
|
+
"PATCH",
|
|
1533
|
+
`/customers/me/addresses/${addressId}`,
|
|
1534
|
+
data
|
|
1535
|
+
);
|
|
1536
|
+
}
|
|
1537
|
+
/**
|
|
1538
|
+
* Delete a customer address (requires customerToken)
|
|
1539
|
+
* Only available in storefront mode
|
|
1540
|
+
*/
|
|
1541
|
+
async deleteMyAddress(addressId) {
|
|
1542
|
+
if (!this.storeId) {
|
|
1543
|
+
throw new OmniSyncError("deleteMyAddress is only available in storefront mode", 400);
|
|
1544
|
+
}
|
|
1545
|
+
if (!this.customerToken) {
|
|
1546
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1547
|
+
}
|
|
1548
|
+
await this.storefrontRequest("DELETE", `/customers/me/addresses/${addressId}`);
|
|
1549
|
+
}
|
|
1550
|
+
/**
|
|
1551
|
+
* Get the current customer's cart (requires customerToken)
|
|
1552
|
+
* Creates a new cart if none exists
|
|
1553
|
+
* Only available in storefront mode
|
|
1554
|
+
*/
|
|
1555
|
+
async getMyCart() {
|
|
1556
|
+
if (!this.storeId) {
|
|
1557
|
+
throw new OmniSyncError("getMyCart is only available in storefront mode", 400);
|
|
1558
|
+
}
|
|
1559
|
+
if (!this.customerToken) {
|
|
1560
|
+
throw new OmniSyncError("Customer token required. Call setCustomerToken() after login.", 401);
|
|
1561
|
+
}
|
|
1562
|
+
return this.storefrontRequest("GET", "/customers/me/cart");
|
|
1563
|
+
}
|
|
1120
1564
|
};
|
|
1121
1565
|
var OmniSyncError = class extends Error {
|
|
1122
1566
|
constructor(message, statusCode, details) {
|