@spree/sdk 0.6.8 → 0.7.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 +118 -120
- package/dist/index.cjs +51 -242
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +87 -124
- package/dist/index.d.ts +87 -124
- package/dist/index.js +39 -230
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.cts +129 -418
- package/dist/types/index.d.ts +129 -418
- package/dist/zod/index.cjs +311 -550
- package/dist/zod/index.cjs.map +1 -1
- package/dist/zod/index.d.cts +120 -713
- package/dist/zod/index.d.ts +120 -713
- package/dist/zod/index.js +257 -489
- package/dist/zod/index.js.map +1 -1
- package/package.json +5 -2
package/dist/index.cjs
CHANGED
|
@@ -1,155 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var SpreeError = class extends Error {
|
|
5
|
-
code;
|
|
6
|
-
status;
|
|
7
|
-
details;
|
|
8
|
-
constructor(response, status) {
|
|
9
|
-
super(response.error.message);
|
|
10
|
-
this.name = "SpreeError";
|
|
11
|
-
this.code = response.error.code;
|
|
12
|
-
this.status = status;
|
|
13
|
-
this.details = response.error.details;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
function calculateDelay(attempt, config) {
|
|
17
|
-
const exponentialDelay = config.baseDelay * Math.pow(2, attempt);
|
|
18
|
-
const jitter = Math.random() * config.baseDelay;
|
|
19
|
-
return Math.min(exponentialDelay + jitter, config.maxDelay);
|
|
20
|
-
}
|
|
21
|
-
function sleep(ms) {
|
|
22
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
23
|
-
}
|
|
24
|
-
function generateIdempotencyKey() {
|
|
25
|
-
return `spree-sdk-retry-${crypto.randomUUID()}`;
|
|
26
|
-
}
|
|
27
|
-
function shouldRetryOnStatus(method, status, config, hasIdempotencyKey) {
|
|
28
|
-
const isIdempotent = method === "GET" || method === "HEAD" || hasIdempotencyKey;
|
|
29
|
-
if (isIdempotent) {
|
|
30
|
-
return config.retryOnStatus.includes(status);
|
|
31
|
-
}
|
|
32
|
-
return status === 429;
|
|
33
|
-
}
|
|
34
|
-
function shouldRetryOnNetworkError(method, config, hasIdempotencyKey) {
|
|
35
|
-
if (!config.retryOnNetworkError) return false;
|
|
36
|
-
return method === "GET" || method === "HEAD" || hasIdempotencyKey;
|
|
37
|
-
}
|
|
38
|
-
function createRequestFn(config, basePath, auth, defaults) {
|
|
39
|
-
return async function request(method, path, options = {}) {
|
|
40
|
-
const { token, orderToken, idempotencyKey, headers = {}, body, params } = options;
|
|
41
|
-
const locale = options.locale ?? defaults?.locale;
|
|
42
|
-
const currency = options.currency ?? defaults?.currency;
|
|
43
|
-
const country = options.country ?? defaults?.country;
|
|
44
|
-
const url = new URL(`${config.baseUrl}${basePath}${path}`);
|
|
45
|
-
if (params) {
|
|
46
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
47
|
-
if (value !== void 0) {
|
|
48
|
-
if (Array.isArray(value)) {
|
|
49
|
-
value.forEach((v) => url.searchParams.append(key, String(v)));
|
|
50
|
-
} else {
|
|
51
|
-
url.searchParams.set(key, String(value));
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
const requestHeaders = {
|
|
57
|
-
"Content-Type": "application/json",
|
|
58
|
-
...headers
|
|
59
|
-
};
|
|
60
|
-
if (auth.headerName && auth.headerValue) {
|
|
61
|
-
requestHeaders[auth.headerName] = auth.headerValue;
|
|
62
|
-
}
|
|
63
|
-
if (token) {
|
|
64
|
-
requestHeaders["Authorization"] = `Bearer ${token}`;
|
|
65
|
-
}
|
|
66
|
-
if (orderToken) {
|
|
67
|
-
requestHeaders["x-spree-order-token"] = orderToken;
|
|
68
|
-
}
|
|
69
|
-
if (locale) {
|
|
70
|
-
requestHeaders["x-spree-locale"] = locale;
|
|
71
|
-
}
|
|
72
|
-
if (currency) {
|
|
73
|
-
requestHeaders["x-spree-currency"] = currency;
|
|
74
|
-
}
|
|
75
|
-
if (country) {
|
|
76
|
-
requestHeaders["x-spree-country"] = country;
|
|
77
|
-
}
|
|
78
|
-
const isMutating = method !== "GET" && method !== "HEAD";
|
|
79
|
-
const effectiveIdempotencyKey = idempotencyKey ?? (isMutating && config.retryConfig ? generateIdempotencyKey() : void 0);
|
|
80
|
-
if (effectiveIdempotencyKey) {
|
|
81
|
-
requestHeaders["Idempotency-Key"] = effectiveIdempotencyKey;
|
|
82
|
-
}
|
|
83
|
-
const hasIdempotencyKey = !!effectiveIdempotencyKey;
|
|
84
|
-
const maxAttempts = config.retryConfig ? config.retryConfig.maxRetries + 1 : 1;
|
|
85
|
-
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
86
|
-
try {
|
|
87
|
-
const response = await config.fetchFn(url.toString(), {
|
|
88
|
-
method,
|
|
89
|
-
headers: requestHeaders,
|
|
90
|
-
body: body ? JSON.stringify(body) : void 0
|
|
91
|
-
});
|
|
92
|
-
if (!response.ok) {
|
|
93
|
-
const isLastAttempt = attempt >= maxAttempts - 1;
|
|
94
|
-
if (!isLastAttempt && config.retryConfig && shouldRetryOnStatus(method, response.status, config.retryConfig, hasIdempotencyKey)) {
|
|
95
|
-
const retryAfter = response.headers.get("Retry-After");
|
|
96
|
-
const delay = retryAfter ? Math.min(parseInt(retryAfter, 10) * 1e3, config.retryConfig.maxDelay) : calculateDelay(attempt, config.retryConfig);
|
|
97
|
-
await sleep(delay);
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
const errorBody = await response.json();
|
|
101
|
-
throw new SpreeError(errorBody, response.status);
|
|
102
|
-
}
|
|
103
|
-
if (response.status === 204) {
|
|
104
|
-
return void 0;
|
|
105
|
-
}
|
|
106
|
-
return response.json();
|
|
107
|
-
} catch (error) {
|
|
108
|
-
if (error instanceof SpreeError) {
|
|
109
|
-
throw error;
|
|
110
|
-
}
|
|
111
|
-
const isLastAttempt = attempt >= maxAttempts - 1;
|
|
112
|
-
if (!isLastAttempt && config.retryConfig && shouldRetryOnNetworkError(method, config.retryConfig, hasIdempotencyKey)) {
|
|
113
|
-
const delay = calculateDelay(attempt, config.retryConfig);
|
|
114
|
-
await sleep(delay);
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
throw error;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
throw new Error("Unexpected end of retry loop");
|
|
121
|
-
};
|
|
122
|
-
}
|
|
3
|
+
var sdkCore = require('@spree/sdk-core');
|
|
123
4
|
|
|
124
|
-
// src/
|
|
125
|
-
var PASSTHROUGH_KEYS = /* @__PURE__ */ new Set(["page", "limit", "expand", "sort"]);
|
|
126
|
-
function transformListParams(params) {
|
|
127
|
-
const result = {};
|
|
128
|
-
for (const [key, value] of Object.entries(params)) {
|
|
129
|
-
if (value === void 0) continue;
|
|
130
|
-
if (PASSTHROUGH_KEYS.has(key)) {
|
|
131
|
-
result[key] = Array.isArray(value) ? value.join(",") : value;
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
if (key.startsWith("q[")) {
|
|
135
|
-
result[key] = value;
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
if (Array.isArray(value)) {
|
|
139
|
-
const base = key.endsWith("[]") ? key.slice(0, -2) : key;
|
|
140
|
-
result[`q[${base}][]`] = value;
|
|
141
|
-
} else {
|
|
142
|
-
result[`q[${key}]`] = value;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return result;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// src/store-client.ts
|
|
149
|
-
function expandParams(params) {
|
|
150
|
-
if (!params?.expand?.length) return void 0;
|
|
151
|
-
return { expand: params.expand.join(",") };
|
|
152
|
-
}
|
|
5
|
+
// src/client.ts
|
|
153
6
|
var StoreClient = class {
|
|
154
7
|
request;
|
|
155
8
|
constructor(request) {
|
|
@@ -181,14 +34,14 @@ var StoreClient = class {
|
|
|
181
34
|
*/
|
|
182
35
|
list: (params, options) => this.request("GET", "/products", {
|
|
183
36
|
...options,
|
|
184
|
-
params: transformListParams({ ...params })
|
|
37
|
+
params: sdkCore.transformListParams({ ...params })
|
|
185
38
|
}),
|
|
186
39
|
/**
|
|
187
40
|
* Get a product by ID or slug
|
|
188
41
|
*/
|
|
189
42
|
get: (idOrSlug, params, options) => this.request("GET", `/products/${idOrSlug}`, {
|
|
190
43
|
...options,
|
|
191
|
-
params:
|
|
44
|
+
params: sdkCore.getParams(params)
|
|
192
45
|
}),
|
|
193
46
|
/**
|
|
194
47
|
* Get available filters for products
|
|
@@ -208,14 +61,14 @@ var StoreClient = class {
|
|
|
208
61
|
*/
|
|
209
62
|
list: (params, options) => this.request("GET", "/taxonomies", {
|
|
210
63
|
...options,
|
|
211
|
-
params: transformListParams({ ...params })
|
|
64
|
+
params: sdkCore.transformListParams({ ...params })
|
|
212
65
|
}),
|
|
213
66
|
/**
|
|
214
67
|
* Get a taxonomy by ID
|
|
215
68
|
*/
|
|
216
69
|
get: (id, params, options) => this.request("GET", `/taxonomies/${id}`, {
|
|
217
70
|
...options,
|
|
218
|
-
params:
|
|
71
|
+
params: sdkCore.getParams(params)
|
|
219
72
|
})
|
|
220
73
|
};
|
|
221
74
|
taxons = {
|
|
@@ -224,14 +77,14 @@ var StoreClient = class {
|
|
|
224
77
|
*/
|
|
225
78
|
list: (params, options) => this.request("GET", "/taxons", {
|
|
226
79
|
...options,
|
|
227
|
-
params: transformListParams({ ...params })
|
|
80
|
+
params: sdkCore.transformListParams({ ...params })
|
|
228
81
|
}),
|
|
229
82
|
/**
|
|
230
83
|
* Get a taxon by ID or permalink
|
|
231
84
|
*/
|
|
232
85
|
get: (idOrPermalink, params, options) => this.request("GET", `/taxons/${idOrPermalink}`, {
|
|
233
86
|
...options,
|
|
234
|
-
params:
|
|
87
|
+
params: sdkCore.getParams(params)
|
|
235
88
|
}),
|
|
236
89
|
/**
|
|
237
90
|
* Nested resource: Products in a taxon
|
|
@@ -246,7 +99,7 @@ var StoreClient = class {
|
|
|
246
99
|
`/taxons/${taxonId}/products`,
|
|
247
100
|
{
|
|
248
101
|
...options,
|
|
249
|
-
params: transformListParams({ ...params })
|
|
102
|
+
params: sdkCore.transformListParams({ ...params })
|
|
250
103
|
}
|
|
251
104
|
)
|
|
252
105
|
}
|
|
@@ -267,7 +120,7 @@ var StoreClient = class {
|
|
|
267
120
|
*/
|
|
268
121
|
get: (iso, params, options) => this.request("GET", `/countries/${iso}`, {
|
|
269
122
|
...options,
|
|
270
|
-
params:
|
|
123
|
+
params: sdkCore.getParams(params)
|
|
271
124
|
})
|
|
272
125
|
};
|
|
273
126
|
currencies = {
|
|
@@ -324,7 +177,7 @@ var StoreClient = class {
|
|
|
324
177
|
get: (marketId, iso, params, options) => this.request(
|
|
325
178
|
"GET",
|
|
326
179
|
`/markets/${marketId}/countries/${iso}`,
|
|
327
|
-
{ ...options, params:
|
|
180
|
+
{ ...options, params: sdkCore.getParams(params) }
|
|
328
181
|
)
|
|
329
182
|
}
|
|
330
183
|
};
|
|
@@ -361,7 +214,7 @@ var StoreClient = class {
|
|
|
361
214
|
*/
|
|
362
215
|
get: (idOrNumber, params, options) => this.request("GET", `/orders/${idOrNumber}`, {
|
|
363
216
|
...options,
|
|
364
|
-
params:
|
|
217
|
+
params: sdkCore.getParams(params)
|
|
365
218
|
}),
|
|
366
219
|
/**
|
|
367
220
|
* Update an order
|
|
@@ -579,7 +432,7 @@ var StoreClient = class {
|
|
|
579
432
|
list: (params, options) => this.request(
|
|
580
433
|
"GET",
|
|
581
434
|
"/customer/addresses",
|
|
582
|
-
{ ...options, params: transformListParams({ ...params }) }
|
|
435
|
+
{ ...options, params: sdkCore.transformListParams({ ...params }) }
|
|
583
436
|
),
|
|
584
437
|
/**
|
|
585
438
|
* Get an address by ID
|
|
@@ -621,7 +474,7 @@ var StoreClient = class {
|
|
|
621
474
|
list: (params, options) => this.request(
|
|
622
475
|
"GET",
|
|
623
476
|
"/customer/credit_cards",
|
|
624
|
-
{ ...options, params: transformListParams({ ...params }) }
|
|
477
|
+
{ ...options, params: sdkCore.transformListParams({ ...params }) }
|
|
625
478
|
),
|
|
626
479
|
/**
|
|
627
480
|
* Get a credit card by ID
|
|
@@ -643,7 +496,7 @@ var StoreClient = class {
|
|
|
643
496
|
list: (params, options) => this.request(
|
|
644
497
|
"GET",
|
|
645
498
|
"/customer/gift_cards",
|
|
646
|
-
{ ...options, params: transformListParams({ ...params }) }
|
|
499
|
+
{ ...options, params: sdkCore.transformListParams({ ...params }) }
|
|
647
500
|
),
|
|
648
501
|
/**
|
|
649
502
|
* Get a gift card by ID
|
|
@@ -659,7 +512,7 @@ var StoreClient = class {
|
|
|
659
512
|
*/
|
|
660
513
|
list: (params, options) => this.request("GET", "/customer/orders", {
|
|
661
514
|
...options,
|
|
662
|
-
params: transformListParams({ ...params })
|
|
515
|
+
params: sdkCore.transformListParams({ ...params })
|
|
663
516
|
})
|
|
664
517
|
},
|
|
665
518
|
/**
|
|
@@ -699,14 +552,14 @@ var StoreClient = class {
|
|
|
699
552
|
*/
|
|
700
553
|
list: (params, options) => this.request("GET", "/wishlists", {
|
|
701
554
|
...options,
|
|
702
|
-
params: transformListParams({ ...params })
|
|
555
|
+
params: sdkCore.transformListParams({ ...params })
|
|
703
556
|
}),
|
|
704
557
|
/**
|
|
705
558
|
* Get a wishlist by ID
|
|
706
559
|
*/
|
|
707
560
|
get: (id, params, options) => this.request("GET", `/wishlists/${id}`, {
|
|
708
561
|
...options,
|
|
709
|
-
params:
|
|
562
|
+
params: sdkCore.getParams(params)
|
|
710
563
|
}),
|
|
711
564
|
/**
|
|
712
565
|
* Create a wishlist
|
|
@@ -757,86 +610,42 @@ var StoreClient = class {
|
|
|
757
610
|
};
|
|
758
611
|
};
|
|
759
612
|
|
|
760
|
-
// src/admin-client.ts
|
|
761
|
-
var AdminClient = class {
|
|
762
|
-
/** @internal */
|
|
763
|
-
request;
|
|
764
|
-
constructor(request) {
|
|
765
|
-
this.request = request;
|
|
766
|
-
void this.request;
|
|
767
|
-
}
|
|
768
|
-
};
|
|
769
|
-
|
|
770
613
|
// src/client.ts
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
}
|
|
800
|
-
const requestConfig = { baseUrl, fetchFn, retryConfig };
|
|
801
|
-
const storeRequestFn = createRequestFn(
|
|
802
|
-
requestConfig,
|
|
803
|
-
"/api/v3/store",
|
|
804
|
-
config.publishableKey ? { headerName: "x-spree-api-key", headerValue: config.publishableKey } : { headerName: "", headerValue: "" },
|
|
805
|
-
this._defaults
|
|
806
|
-
);
|
|
807
|
-
const adminRequestFn = createRequestFn(
|
|
808
|
-
requestConfig,
|
|
809
|
-
"/api/v3/admin",
|
|
810
|
-
{
|
|
811
|
-
headerName: "Authorization",
|
|
812
|
-
headerValue: config.secretKey ? `Bearer ${config.secretKey}` : ""
|
|
813
|
-
},
|
|
814
|
-
this._defaults
|
|
815
|
-
);
|
|
816
|
-
this.store = new StoreClient(storeRequestFn);
|
|
817
|
-
this.admin = new AdminClient(adminRequestFn);
|
|
818
|
-
}
|
|
819
|
-
/** Set default locale for all subsequent requests */
|
|
820
|
-
setLocale(locale) {
|
|
821
|
-
this._defaults.locale = locale;
|
|
822
|
-
}
|
|
823
|
-
/** Set default currency for all subsequent requests */
|
|
824
|
-
setCurrency(currency) {
|
|
825
|
-
this._defaults.currency = currency;
|
|
826
|
-
}
|
|
827
|
-
/** Set default country for all subsequent requests */
|
|
828
|
-
setCountry(country) {
|
|
829
|
-
this._defaults.country = country;
|
|
830
|
-
}
|
|
831
|
-
};
|
|
832
|
-
function createSpreeClient(config) {
|
|
833
|
-
return new SpreeClient(config);
|
|
614
|
+
function createClient(config) {
|
|
615
|
+
const baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
616
|
+
const fetchFn = config.fetch || fetch.bind(globalThis);
|
|
617
|
+
const defaults = {
|
|
618
|
+
locale: config.locale,
|
|
619
|
+
currency: config.currency,
|
|
620
|
+
country: config.country
|
|
621
|
+
};
|
|
622
|
+
const retryConfig = sdkCore.resolveRetryConfig(config.retry);
|
|
623
|
+
const requestConfig = { baseUrl, fetchFn, retryConfig };
|
|
624
|
+
const requestFn = sdkCore.createRequestFn(
|
|
625
|
+
requestConfig,
|
|
626
|
+
"/api/v3/store",
|
|
627
|
+
{ headerName: "x-spree-api-key", headerValue: config.publishableKey },
|
|
628
|
+
defaults
|
|
629
|
+
);
|
|
630
|
+
const storeClient = new StoreClient(requestFn);
|
|
631
|
+
const client = Object.create(storeClient);
|
|
632
|
+
client.setLocale = (locale) => {
|
|
633
|
+
defaults.locale = locale;
|
|
634
|
+
};
|
|
635
|
+
client.setCurrency = (currency) => {
|
|
636
|
+
defaults.currency = currency;
|
|
637
|
+
};
|
|
638
|
+
client.setCountry = (country) => {
|
|
639
|
+
defaults.country = country;
|
|
640
|
+
};
|
|
641
|
+
return client;
|
|
834
642
|
}
|
|
835
643
|
|
|
836
|
-
exports
|
|
837
|
-
|
|
838
|
-
|
|
644
|
+
Object.defineProperty(exports, "SpreeError", {
|
|
645
|
+
enumerable: true,
|
|
646
|
+
get: function () { return sdkCore.SpreeError; }
|
|
647
|
+
});
|
|
839
648
|
exports.StoreClient = StoreClient;
|
|
840
|
-
exports.
|
|
649
|
+
exports.createClient = createClient;
|
|
841
650
|
//# sourceMappingURL=index.cjs.map
|
|
842
651
|
//# sourceMappingURL=index.cjs.map
|