@01.software/sdk 0.29.0 → 0.30.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/README.md +273 -73
- package/dist/analytics/react.cjs +4 -1
- package/dist/analytics/react.cjs.map +1 -1
- package/dist/analytics/react.js +4 -1
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics.cjs +4 -1
- package/dist/analytics.cjs.map +1 -1
- package/dist/analytics.js +4 -1
- package/dist/analytics.js.map +1 -1
- package/dist/client.cjs +1476 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +28 -0
- package/dist/client.d.ts +28 -0
- package/dist/client.js +1453 -0
- package/dist/client.js.map +1 -0
- package/dist/collection-client-B9d9kr1d.d.ts +218 -0
- package/dist/collection-client-QPbwimkU.d.cts +218 -0
- package/dist/{const-DAjQYNuM.d.ts → const-B75IFDRi.d.ts} +2 -4
- package/dist/{const-Dsixdi6z.d.cts → const-VZuk2tWc.d.cts} +2 -4
- package/dist/index-B2WbhEgT.d.cts +106 -0
- package/dist/index-B2WbhEgT.d.ts +106 -0
- package/dist/index.cjs +784 -1530
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -115
- package/dist/index.d.ts +11 -115
- package/dist/index.js +784 -1548
- package/dist/index.js.map +1 -1
- package/dist/metadata.cjs +91 -0
- package/dist/metadata.cjs.map +1 -0
- package/dist/metadata.d.cts +58 -0
- package/dist/metadata.d.ts +58 -0
- package/dist/metadata.js +68 -0
- package/dist/metadata.js.map +1 -0
- package/dist/{payload-types-Ci-ZA7aM.d.cts → payload-types-DPjO_IbQ.d.cts} +9 -3
- package/dist/{payload-types-Ci-ZA7aM.d.ts → payload-types-DPjO_IbQ.d.ts} +9 -3
- package/dist/query.cjs +1791 -0
- package/dist/query.cjs.map +1 -0
- package/dist/query.d.cts +244 -0
- package/dist/query.d.ts +244 -0
- package/dist/query.js +1786 -0
- package/dist/query.js.map +1 -0
- package/dist/realtime.cjs +4 -1
- package/dist/realtime.cjs.map +1 -1
- package/dist/realtime.d.cts +2 -2
- package/dist/realtime.d.ts +2 -2
- package/dist/realtime.js +4 -1
- package/dist/realtime.js.map +1 -1
- package/dist/{server-BINWywT8.d.cts → server-CrsPyqEc.d.cts} +14 -31
- package/dist/{server-BINWywT8.d.ts → server-CrsPyqEc.d.ts} +14 -31
- package/dist/server.cjs +299 -840
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +112 -7
- package/dist/server.d.ts +112 -7
- package/dist/server.js +299 -858
- package/dist/server.js.map +1 -1
- package/dist/{types-BWq_WlbB.d.ts → types-1fBLrYU7.d.ts} +1 -1
- package/dist/{types-zKjATmDK.d.cts → types-BwT0eeaz.d.cts} +1 -1
- package/dist/{server-Cv0Q4dPQ.d.ts → types-Dlb2mwpX.d.cts} +228 -741
- package/dist/{server-C0C8dtms.d.cts → types-DuSKPiY5.d.ts} +228 -741
- package/dist/ui/canvas/server.cjs +7 -6
- package/dist/ui/canvas/server.cjs.map +1 -1
- package/dist/ui/canvas/server.d.cts +1 -3
- package/dist/ui/canvas/server.d.ts +1 -3
- package/dist/ui/canvas/server.js +7 -6
- package/dist/ui/canvas/server.js.map +1 -1
- package/dist/ui/canvas.cjs +11 -10
- package/dist/ui/canvas.cjs.map +1 -1
- package/dist/ui/canvas.d.cts +29 -6
- package/dist/ui/canvas.d.ts +29 -6
- package/dist/ui/canvas.js +11 -10
- package/dist/ui/canvas.js.map +1 -1
- package/dist/ui/form.d.cts +1 -1
- package/dist/ui/form.d.ts +1 -1
- package/dist/ui/video.d.cts +1 -1
- package/dist/ui/video.d.ts +1 -1
- package/dist/webhook.d.cts +3 -3
- package/dist/webhook.d.ts +3 -3
- package/package.json +84 -15
package/dist/query.cjs
ADDED
|
@@ -0,0 +1,1791 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/query.ts
|
|
21
|
+
var query_exports = {};
|
|
22
|
+
__export(query_exports, {
|
|
23
|
+
CollectionHooks: () => CollectionHooks,
|
|
24
|
+
CustomerHooks: () => CustomerHooks,
|
|
25
|
+
QueryHooks: () => QueryHooks,
|
|
26
|
+
RealtimeConnection: () => RealtimeConnection,
|
|
27
|
+
collectionKeys: () => collectionKeys,
|
|
28
|
+
createQueryHooks: () => createQueryHooks,
|
|
29
|
+
createServerQueryHooks: () => createServerQueryHooks,
|
|
30
|
+
customerKeys: () => customerKeys,
|
|
31
|
+
getQueryClient: () => getQueryClient,
|
|
32
|
+
productKeys: () => productKeys
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(query_exports);
|
|
35
|
+
|
|
36
|
+
// src/core/query/get-query-client.ts
|
|
37
|
+
var import_react_query = require("@tanstack/react-query");
|
|
38
|
+
function makeQueryClient() {
|
|
39
|
+
return new import_react_query.QueryClient({
|
|
40
|
+
defaultOptions: {
|
|
41
|
+
queries: {
|
|
42
|
+
// Infinite staleTime: server-fetched data persists until explicitly invalidated.
|
|
43
|
+
// For browser clients needing fresher data, override per-query:
|
|
44
|
+
// useQuery({ ..., staleTime: 5 * 60 * 1000 })
|
|
45
|
+
staleTime: Number.POSITIVE_INFINITY,
|
|
46
|
+
refetchOnWindowFocus: false
|
|
47
|
+
},
|
|
48
|
+
dehydrate: {
|
|
49
|
+
shouldDehydrateQuery: (query) => (0, import_react_query.defaultShouldDehydrateQuery)(query) || query.state.status === "pending",
|
|
50
|
+
shouldRedactErrors: () => false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
var browserQueryClient;
|
|
56
|
+
function getQueryClient() {
|
|
57
|
+
if (import_react_query.isServer) {
|
|
58
|
+
return makeQueryClient();
|
|
59
|
+
}
|
|
60
|
+
if (!browserQueryClient) {
|
|
61
|
+
browserQueryClient = makeQueryClient();
|
|
62
|
+
}
|
|
63
|
+
return browserQueryClient;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/core/query/query-keys.ts
|
|
67
|
+
function collectionKeys(collection) {
|
|
68
|
+
return {
|
|
69
|
+
all: [collection],
|
|
70
|
+
lists: () => [collection, "list"],
|
|
71
|
+
list: (options) => [collection, "list", options],
|
|
72
|
+
details: () => [collection, "detail"],
|
|
73
|
+
detail: (id, options) => [collection, "detail", id, options],
|
|
74
|
+
infinites: () => [collection, "infinite"],
|
|
75
|
+
infinite: (options) => [collection, "infinite", options]
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
var customerKeys = {
|
|
79
|
+
all: ["customer"],
|
|
80
|
+
me: () => ["customer", "me"]
|
|
81
|
+
};
|
|
82
|
+
var productKeys = {
|
|
83
|
+
listingGroups: (options) => ["products", "listing-groups", "list", options],
|
|
84
|
+
listingGroupsInfinite: (options) => ["products", "listing-groups", "infinite", options],
|
|
85
|
+
detail: (params) => ["products", "detail", params],
|
|
86
|
+
detailAll: () => ["products", "detail"]
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// src/core/query/collection-hooks.ts
|
|
90
|
+
var import_react_query2 = require("@tanstack/react-query");
|
|
91
|
+
var PRODUCT_DETAIL_INVALIDATING_COLLECTIONS = /* @__PURE__ */ new Set([
|
|
92
|
+
"products",
|
|
93
|
+
"product-variants",
|
|
94
|
+
"product-options",
|
|
95
|
+
"product-option-values",
|
|
96
|
+
"product-categories",
|
|
97
|
+
"product-tags",
|
|
98
|
+
"product-collections",
|
|
99
|
+
"brands",
|
|
100
|
+
"brand-logos",
|
|
101
|
+
"images"
|
|
102
|
+
]);
|
|
103
|
+
var DEFAULT_PAGE_SIZE = 20;
|
|
104
|
+
var CollectionHooks = class {
|
|
105
|
+
constructor(queryClient, collectionClient) {
|
|
106
|
+
this.queryClient = queryClient;
|
|
107
|
+
this.collectionClient = collectionClient;
|
|
108
|
+
}
|
|
109
|
+
// ===== useQuery =====
|
|
110
|
+
useQuery(params, options) {
|
|
111
|
+
const { collection, options: queryOptions } = params;
|
|
112
|
+
const { placeholderData, ...restOptions } = options ?? {};
|
|
113
|
+
return (0, import_react_query2.useQuery)({
|
|
114
|
+
queryKey: collectionKeys(collection).list(queryOptions),
|
|
115
|
+
queryFn: async () => {
|
|
116
|
+
return await this.collectionClient.from(collection).find(queryOptions);
|
|
117
|
+
},
|
|
118
|
+
...restOptions,
|
|
119
|
+
// NonFunctionGuard<T> incompatible with generic union types — safe cast
|
|
120
|
+
...placeholderData !== void 0 && {
|
|
121
|
+
placeholderData
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// ===== useSuspenseQuery =====
|
|
126
|
+
useSuspenseQuery(params, options) {
|
|
127
|
+
const { collection, options: queryOptions } = params;
|
|
128
|
+
return (0, import_react_query2.useSuspenseQuery)({
|
|
129
|
+
queryKey: collectionKeys(collection).list(queryOptions),
|
|
130
|
+
queryFn: async () => {
|
|
131
|
+
return await this.collectionClient.from(collection).find(queryOptions);
|
|
132
|
+
},
|
|
133
|
+
...options
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
// ===== useQueryById =====
|
|
137
|
+
useQueryById(params, options) {
|
|
138
|
+
const { collection, id, options: queryOptions } = params;
|
|
139
|
+
const { placeholderData, ...restOptions } = options ?? {};
|
|
140
|
+
return (0, import_react_query2.useQuery)({
|
|
141
|
+
queryKey: collectionKeys(collection).detail(id, queryOptions),
|
|
142
|
+
queryFn: async () => {
|
|
143
|
+
return await this.collectionClient.from(collection).findById(id, queryOptions);
|
|
144
|
+
},
|
|
145
|
+
...restOptions,
|
|
146
|
+
// NonFunctionGuard<T> incompatible with generic union types — safe cast
|
|
147
|
+
...placeholderData !== void 0 && {
|
|
148
|
+
placeholderData
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
// ===== useSuspenseQueryById =====
|
|
153
|
+
useSuspenseQueryById(params, options) {
|
|
154
|
+
const { collection, id, options: queryOptions } = params;
|
|
155
|
+
return (0, import_react_query2.useSuspenseQuery)({
|
|
156
|
+
queryKey: collectionKeys(collection).detail(id, queryOptions),
|
|
157
|
+
queryFn: async () => {
|
|
158
|
+
return await this.collectionClient.from(collection).findById(id, queryOptions);
|
|
159
|
+
},
|
|
160
|
+
...options
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
// ===== useInfiniteQuery =====
|
|
164
|
+
useInfiniteQuery(params, options) {
|
|
165
|
+
const {
|
|
166
|
+
collection,
|
|
167
|
+
options: queryOptions,
|
|
168
|
+
pageSize = DEFAULT_PAGE_SIZE
|
|
169
|
+
} = params;
|
|
170
|
+
return (0, import_react_query2.useInfiniteQuery)({
|
|
171
|
+
queryKey: collectionKeys(collection).infinite(queryOptions),
|
|
172
|
+
queryFn: async ({ pageParam }) => {
|
|
173
|
+
const response = await this.collectionClient.from(collection).find({ ...queryOptions, page: pageParam, limit: pageSize });
|
|
174
|
+
return response;
|
|
175
|
+
},
|
|
176
|
+
initialPageParam: 1,
|
|
177
|
+
getNextPageParam: (lastPage) => {
|
|
178
|
+
return lastPage.hasNextPage ? lastPage.nextPage : void 0;
|
|
179
|
+
},
|
|
180
|
+
...options
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
// ===== useSuspenseInfiniteQuery =====
|
|
184
|
+
useSuspenseInfiniteQuery(params, options) {
|
|
185
|
+
const {
|
|
186
|
+
collection,
|
|
187
|
+
options: queryOptions,
|
|
188
|
+
pageSize = DEFAULT_PAGE_SIZE
|
|
189
|
+
} = params;
|
|
190
|
+
return (0, import_react_query2.useSuspenseInfiniteQuery)({
|
|
191
|
+
queryKey: collectionKeys(collection).infinite(queryOptions),
|
|
192
|
+
queryFn: async ({ pageParam }) => {
|
|
193
|
+
const response = await this.collectionClient.from(collection).find({ ...queryOptions, page: pageParam, limit: pageSize });
|
|
194
|
+
return response;
|
|
195
|
+
},
|
|
196
|
+
initialPageParam: 1,
|
|
197
|
+
getNextPageParam: (lastPage) => {
|
|
198
|
+
return lastPage.hasNextPage ? lastPage.nextPage : void 0;
|
|
199
|
+
},
|
|
200
|
+
...options
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
// ===== prefetchQuery =====
|
|
204
|
+
async prefetchQuery(params, options) {
|
|
205
|
+
const { collection, options: queryOptions } = params;
|
|
206
|
+
return this.queryClient.prefetchQuery({
|
|
207
|
+
queryKey: collectionKeys(collection).list(queryOptions),
|
|
208
|
+
queryFn: async () => {
|
|
209
|
+
return await this.collectionClient.from(collection).find(queryOptions);
|
|
210
|
+
},
|
|
211
|
+
...options
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
// ===== prefetchQueryById =====
|
|
215
|
+
async prefetchQueryById(params, options) {
|
|
216
|
+
const { collection, id, options: queryOptions } = params;
|
|
217
|
+
return this.queryClient.prefetchQuery({
|
|
218
|
+
queryKey: collectionKeys(collection).detail(id, queryOptions),
|
|
219
|
+
queryFn: async () => {
|
|
220
|
+
return await this.collectionClient.from(collection).findById(id, queryOptions);
|
|
221
|
+
},
|
|
222
|
+
...options
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
// ===== prefetchInfiniteQuery =====
|
|
226
|
+
async prefetchInfiniteQuery(params, options) {
|
|
227
|
+
const {
|
|
228
|
+
collection,
|
|
229
|
+
options: queryOptions,
|
|
230
|
+
pageSize = DEFAULT_PAGE_SIZE
|
|
231
|
+
} = params;
|
|
232
|
+
return this.queryClient.prefetchInfiniteQuery({
|
|
233
|
+
queryKey: collectionKeys(collection).infinite(queryOptions),
|
|
234
|
+
queryFn: async ({ pageParam }) => {
|
|
235
|
+
const response = await this.collectionClient.from(collection).find({ ...queryOptions, page: pageParam, limit: pageSize });
|
|
236
|
+
return response;
|
|
237
|
+
},
|
|
238
|
+
initialPageParam: 1,
|
|
239
|
+
getNextPageParam: (lastPage) => {
|
|
240
|
+
return lastPage.hasNextPage ? lastPage.nextPage : void 0;
|
|
241
|
+
},
|
|
242
|
+
pages: options?.pages ?? 1,
|
|
243
|
+
staleTime: options?.staleTime
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
// ===== Mutation Hooks =====
|
|
247
|
+
useCreate(params, options) {
|
|
248
|
+
const { collection } = params;
|
|
249
|
+
return (0, import_react_query2.useMutation)({
|
|
250
|
+
mutationFn: async (variables) => {
|
|
251
|
+
return await this.collectionClient.from(collection).create(
|
|
252
|
+
variables.data,
|
|
253
|
+
variables.file ? { file: variables.file, filename: variables.filename } : void 0
|
|
254
|
+
);
|
|
255
|
+
},
|
|
256
|
+
onSuccess: (data) => {
|
|
257
|
+
this.queryClient.invalidateQueries({
|
|
258
|
+
queryKey: collectionKeys(collection).all
|
|
259
|
+
});
|
|
260
|
+
if (PRODUCT_DETAIL_INVALIDATING_COLLECTIONS.has(collection)) {
|
|
261
|
+
this.queryClient.invalidateQueries({ queryKey: ["products", "detail"] });
|
|
262
|
+
}
|
|
263
|
+
options?.onSuccess?.(data);
|
|
264
|
+
},
|
|
265
|
+
onError: options?.onError,
|
|
266
|
+
onSettled: options?.onSettled
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
useUpdate(params, options) {
|
|
270
|
+
const { collection } = params;
|
|
271
|
+
return (0, import_react_query2.useMutation)({
|
|
272
|
+
mutationFn: async (variables) => {
|
|
273
|
+
return await this.collectionClient.from(collection).update(
|
|
274
|
+
variables.id,
|
|
275
|
+
variables.data,
|
|
276
|
+
variables.file ? { file: variables.file, filename: variables.filename } : void 0
|
|
277
|
+
);
|
|
278
|
+
},
|
|
279
|
+
onSuccess: (data) => {
|
|
280
|
+
this.queryClient.invalidateQueries({
|
|
281
|
+
queryKey: collectionKeys(collection).all
|
|
282
|
+
});
|
|
283
|
+
if (PRODUCT_DETAIL_INVALIDATING_COLLECTIONS.has(collection)) {
|
|
284
|
+
this.queryClient.invalidateQueries({ queryKey: ["products", "detail"] });
|
|
285
|
+
}
|
|
286
|
+
options?.onSuccess?.(data);
|
|
287
|
+
},
|
|
288
|
+
onError: options?.onError,
|
|
289
|
+
onSettled: options?.onSettled
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
useRemove(params, options) {
|
|
293
|
+
const { collection } = params;
|
|
294
|
+
return (0, import_react_query2.useMutation)({
|
|
295
|
+
mutationFn: async (id) => {
|
|
296
|
+
return await this.collectionClient.from(collection).remove(id);
|
|
297
|
+
},
|
|
298
|
+
onSuccess: (data) => {
|
|
299
|
+
this.queryClient.invalidateQueries({
|
|
300
|
+
queryKey: collectionKeys(collection).all
|
|
301
|
+
});
|
|
302
|
+
if (PRODUCT_DETAIL_INVALIDATING_COLLECTIONS.has(collection)) {
|
|
303
|
+
this.queryClient.invalidateQueries({ queryKey: ["products", "detail"] });
|
|
304
|
+
}
|
|
305
|
+
options?.onSuccess?.(data);
|
|
306
|
+
},
|
|
307
|
+
onError: options?.onError,
|
|
308
|
+
onSettled: options?.onSettled
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
// ===== Cache Utilities =====
|
|
312
|
+
invalidateQueries(collection, type) {
|
|
313
|
+
const queryKey = type ? [collection, type] : [collection];
|
|
314
|
+
return this.queryClient.invalidateQueries({ queryKey });
|
|
315
|
+
}
|
|
316
|
+
getQueryData(collection, type, idOrOptions, options) {
|
|
317
|
+
if (type === "list") {
|
|
318
|
+
return this.queryClient.getQueryData(
|
|
319
|
+
collectionKeys(collection).list(idOrOptions)
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
return this.queryClient.getQueryData(
|
|
323
|
+
collectionKeys(collection).detail(idOrOptions, options)
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
setQueryData(collection, type, dataOrId, dataOrOptions, options) {
|
|
327
|
+
if (type === "list") {
|
|
328
|
+
this.queryClient.setQueryData(
|
|
329
|
+
collectionKeys(collection).list(dataOrOptions),
|
|
330
|
+
dataOrId
|
|
331
|
+
);
|
|
332
|
+
} else {
|
|
333
|
+
this.queryClient.setQueryData(
|
|
334
|
+
collectionKeys(collection).detail(dataOrId, options),
|
|
335
|
+
dataOrOptions
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// src/core/query/customer-hooks.ts
|
|
342
|
+
var import_react_query3 = require("@tanstack/react-query");
|
|
343
|
+
|
|
344
|
+
// src/core/internal/errors/index.ts
|
|
345
|
+
var SDKError = class extends Error {
|
|
346
|
+
constructor(code, message, status, details, userMessage, suggestion, requestId) {
|
|
347
|
+
super(message);
|
|
348
|
+
this.name = "SDKError";
|
|
349
|
+
this.code = code;
|
|
350
|
+
this.status = status;
|
|
351
|
+
this.details = details;
|
|
352
|
+
this.userMessage = userMessage;
|
|
353
|
+
this.suggestion = suggestion;
|
|
354
|
+
this.requestId = requestId;
|
|
355
|
+
if (Error.captureStackTrace) {
|
|
356
|
+
Error.captureStackTrace(this, new.target);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
getUserMessage() {
|
|
360
|
+
return this.userMessage || this.message;
|
|
361
|
+
}
|
|
362
|
+
toJSON() {
|
|
363
|
+
return {
|
|
364
|
+
name: this.name,
|
|
365
|
+
code: this.code,
|
|
366
|
+
message: this.message,
|
|
367
|
+
status: this.status,
|
|
368
|
+
details: this.details,
|
|
369
|
+
userMessage: this.userMessage,
|
|
370
|
+
suggestion: this.suggestion,
|
|
371
|
+
...this.requestId !== void 0 && { requestId: this.requestId }
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
var NetworkError = class extends SDKError {
|
|
376
|
+
constructor(message, status, details, userMessage, suggestion) {
|
|
377
|
+
super("NETWORK_ERROR", message, status, details, userMessage, suggestion);
|
|
378
|
+
this.name = "NetworkError";
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
var ValidationError = class extends SDKError {
|
|
382
|
+
constructor(message, details, userMessage, suggestion, status = 400) {
|
|
383
|
+
super("VALIDATION_ERROR", message, status, details, userMessage, suggestion);
|
|
384
|
+
this.name = "ValidationError";
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
var ApiError = class extends SDKError {
|
|
388
|
+
constructor(message, status, details, userMessage, suggestion) {
|
|
389
|
+
super("API_ERROR", message, status, details, userMessage, suggestion);
|
|
390
|
+
this.name = "ApiError";
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
var ConfigError = class extends SDKError {
|
|
394
|
+
constructor(message, details, userMessage, suggestion) {
|
|
395
|
+
super("CONFIG_ERROR", message, void 0, details, userMessage, suggestion);
|
|
396
|
+
this.name = "ConfigError";
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
var TimeoutError = class extends SDKError {
|
|
400
|
+
constructor(message = "Request timed out.", details, userMessage, suggestion) {
|
|
401
|
+
super("TIMEOUT_ERROR", message, 408, details, userMessage, suggestion);
|
|
402
|
+
this.name = "TimeoutError";
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
var UsageLimitError = class extends SDKError {
|
|
406
|
+
constructor(message, usage, details, userMessage, suggestion) {
|
|
407
|
+
super("USAGE_LIMIT_ERROR", message, 429, details, userMessage, suggestion);
|
|
408
|
+
this.name = "UsageLimitError";
|
|
409
|
+
this.usage = usage;
|
|
410
|
+
}
|
|
411
|
+
toJSON() {
|
|
412
|
+
return {
|
|
413
|
+
...super.toJSON(),
|
|
414
|
+
usage: this.usage
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
var AuthError = class extends SDKError {
|
|
419
|
+
constructor(message, details, userMessage, suggestion, requestId) {
|
|
420
|
+
super("auth_error", message, 401, details, userMessage, suggestion, requestId);
|
|
421
|
+
this.name = "AuthError";
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
var PermissionError = class extends SDKError {
|
|
425
|
+
constructor(message, details, userMessage, suggestion, requestId) {
|
|
426
|
+
super("permission_error", message, 403, details, userMessage, suggestion, requestId);
|
|
427
|
+
this.name = "PermissionError";
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
var NotFoundError = class extends SDKError {
|
|
431
|
+
constructor(message, details, userMessage, suggestion, requestId) {
|
|
432
|
+
super("not_found", message, 404, details, userMessage, suggestion, requestId);
|
|
433
|
+
this.name = "NotFoundError";
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
var ConflictError = class extends SDKError {
|
|
437
|
+
constructor(message, details, userMessage, suggestion, requestId) {
|
|
438
|
+
super("conflict", message, 409, details, userMessage, suggestion, requestId);
|
|
439
|
+
this.name = "ConflictError";
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
var createNetworkError = (message, status, details, userMessage, suggestion) => new NetworkError(message, status, details, userMessage, suggestion);
|
|
443
|
+
var createValidationError = (message, details, userMessage, suggestion, status) => new ValidationError(message, details, userMessage, suggestion, status);
|
|
444
|
+
var createApiError = (message, status, details, userMessage, suggestion) => new ApiError(message, status, details, userMessage, suggestion);
|
|
445
|
+
var createConfigError = (message, details, userMessage, suggestion) => new ConfigError(message, details, userMessage, suggestion);
|
|
446
|
+
var createTimeoutError = (message, details, userMessage, suggestion) => new TimeoutError(message, details, userMessage, suggestion);
|
|
447
|
+
var createUsageLimitError = (message, usage, details, userMessage, suggestion) => new UsageLimitError(message, usage, details, userMessage, suggestion);
|
|
448
|
+
var createAuthError = (message, details, userMessage, suggestion, requestId) => new AuthError(message, details, userMessage, suggestion, requestId);
|
|
449
|
+
var createPermissionError = (message, details, userMessage, suggestion, requestId) => new PermissionError(message, details, userMessage, suggestion, requestId);
|
|
450
|
+
var createNotFoundError = (message, details, userMessage, suggestion, requestId) => new NotFoundError(message, details, userMessage, suggestion, requestId);
|
|
451
|
+
var createConflictError = (message, details, userMessage, suggestion, requestId) => new ConflictError(message, details, userMessage, suggestion, requestId);
|
|
452
|
+
|
|
453
|
+
// src/core/query/customer-hooks.ts
|
|
454
|
+
function createMutation(mutationFn, callbacks, onSuccessExtra) {
|
|
455
|
+
return (0, import_react_query3.useMutation)({
|
|
456
|
+
mutationFn,
|
|
457
|
+
onSuccess: (data) => {
|
|
458
|
+
onSuccessExtra?.(data);
|
|
459
|
+
callbacks?.onSuccess?.(data);
|
|
460
|
+
},
|
|
461
|
+
onError: callbacks?.onError,
|
|
462
|
+
onSettled: callbacks?.onSettled
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
var CustomerHooks = class {
|
|
466
|
+
constructor(queryClient, customerAuth) {
|
|
467
|
+
this.invalidateMe = () => {
|
|
468
|
+
this.queryClient.invalidateQueries({ queryKey: customerKeys.me() });
|
|
469
|
+
};
|
|
470
|
+
this.queryClient = queryClient;
|
|
471
|
+
this.customerAuth = customerAuth;
|
|
472
|
+
}
|
|
473
|
+
ensureCustomerAuth() {
|
|
474
|
+
if (!this.customerAuth) {
|
|
475
|
+
throw createConfigError(
|
|
476
|
+
"Customer hooks require Client. Use createClient() instead of createServerClient()."
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
return this.customerAuth;
|
|
480
|
+
}
|
|
481
|
+
// ===== useCustomerMe =====
|
|
482
|
+
useCustomerMe(options) {
|
|
483
|
+
return (0, import_react_query3.useQuery)({
|
|
484
|
+
queryKey: customerKeys.me(),
|
|
485
|
+
queryFn: async () => {
|
|
486
|
+
return await this.ensureCustomerAuth().me();
|
|
487
|
+
},
|
|
488
|
+
...options,
|
|
489
|
+
enabled: (options?.enabled ?? true) && !!this.customerAuth?.isAuthenticated()
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
// ===== Mutations =====
|
|
493
|
+
useCustomerLogin(options) {
|
|
494
|
+
return createMutation(
|
|
495
|
+
(data) => this.ensureCustomerAuth().login(data),
|
|
496
|
+
options,
|
|
497
|
+
this.invalidateMe
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
useCustomerRegister(options) {
|
|
501
|
+
return createMutation(
|
|
502
|
+
(data) => this.ensureCustomerAuth().register(data),
|
|
503
|
+
options
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
useCustomerLogout(options) {
|
|
507
|
+
return (0, import_react_query3.useMutation)({
|
|
508
|
+
mutationFn: async () => {
|
|
509
|
+
this.ensureCustomerAuth().logout();
|
|
510
|
+
},
|
|
511
|
+
onSuccess: () => {
|
|
512
|
+
this.queryClient.removeQueries({ queryKey: customerKeys.all });
|
|
513
|
+
options?.onSuccess?.();
|
|
514
|
+
},
|
|
515
|
+
onError: options?.onError,
|
|
516
|
+
onSettled: options?.onSettled
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
useCustomerForgotPassword(options) {
|
|
520
|
+
return createMutation(
|
|
521
|
+
(email) => this.ensureCustomerAuth().forgotPassword(email).then(() => {
|
|
522
|
+
}),
|
|
523
|
+
options
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
useCustomerResetPassword(options) {
|
|
527
|
+
return createMutation(
|
|
528
|
+
(data) => this.ensureCustomerAuth().resetPassword(data.token, data.password).then(() => {
|
|
529
|
+
}),
|
|
530
|
+
options
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
useCustomerRefreshToken(options) {
|
|
534
|
+
return createMutation(
|
|
535
|
+
() => this.ensureCustomerAuth().refreshToken(),
|
|
536
|
+
options,
|
|
537
|
+
this.invalidateMe
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
useCustomerUpdateProfile(options) {
|
|
541
|
+
return createMutation(
|
|
542
|
+
(data) => this.ensureCustomerAuth().updateProfile(data),
|
|
543
|
+
options,
|
|
544
|
+
this.invalidateMe
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
useCustomerChangePassword(options) {
|
|
548
|
+
return createMutation(
|
|
549
|
+
(data) => this.ensureCustomerAuth().changePassword(data.currentPassword, data.newPassword).then(() => {
|
|
550
|
+
}),
|
|
551
|
+
options
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
// ===== Customer Cache Utilities =====
|
|
555
|
+
invalidateCustomerQueries() {
|
|
556
|
+
return this.queryClient.invalidateQueries({ queryKey: customerKeys.all });
|
|
557
|
+
}
|
|
558
|
+
getCustomerData() {
|
|
559
|
+
return this.queryClient.getQueryData(customerKeys.me());
|
|
560
|
+
}
|
|
561
|
+
setCustomerData(data) {
|
|
562
|
+
this.queryClient.setQueryData(customerKeys.me(), data);
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
// src/core/query/query-hooks.ts
|
|
567
|
+
var import_react_query4 = require("@tanstack/react-query");
|
|
568
|
+
var QueryHooks = class extends CollectionHooks {
|
|
569
|
+
constructor(queryClient, collectionClient, customerAuth, commerceClient) {
|
|
570
|
+
super(queryClient, collectionClient);
|
|
571
|
+
// --- Customer hooks delegation ---
|
|
572
|
+
this.useCustomerMe = (...args) => this._customer.useCustomerMe(...args);
|
|
573
|
+
this.useCustomerLogin = (...args) => this._customer.useCustomerLogin(...args);
|
|
574
|
+
this.useCustomerRegister = (...args) => this._customer.useCustomerRegister(...args);
|
|
575
|
+
this.useCustomerLogout = (...args) => this._customer.useCustomerLogout(...args);
|
|
576
|
+
this.useCustomerForgotPassword = (...args) => this._customer.useCustomerForgotPassword(...args);
|
|
577
|
+
this.useCustomerResetPassword = (...args) => this._customer.useCustomerResetPassword(...args);
|
|
578
|
+
this.useCustomerRefreshToken = (...args) => this._customer.useCustomerRefreshToken(...args);
|
|
579
|
+
this.useCustomerUpdateProfile = (...args) => this._customer.useCustomerUpdateProfile(...args);
|
|
580
|
+
this.useCustomerChangePassword = (...args) => this._customer.useCustomerChangePassword(...args);
|
|
581
|
+
// --- Customer cache delegation ---
|
|
582
|
+
this.invalidateCustomerQueries = () => this._customer.invalidateCustomerQueries();
|
|
583
|
+
this.getCustomerData = () => this._customer.getCustomerData();
|
|
584
|
+
this.setCustomerData = (data) => this._customer.setCustomerData(data);
|
|
585
|
+
this._customer = new CustomerHooks(queryClient, customerAuth);
|
|
586
|
+
this._commerce = commerceClient;
|
|
587
|
+
}
|
|
588
|
+
useProductListingGroupsQuery(params, options) {
|
|
589
|
+
const queryOptions = params.options;
|
|
590
|
+
const { placeholderData, ...restOptions } = options ?? {};
|
|
591
|
+
return (0, import_react_query4.useQuery)({
|
|
592
|
+
queryKey: productKeys.listingGroups(queryOptions),
|
|
593
|
+
queryFn: async () => this.collectionClient.requestFindEndpoint(
|
|
594
|
+
"/api/products/listing-groups/query",
|
|
595
|
+
{ options: queryOptions }
|
|
596
|
+
),
|
|
597
|
+
...restOptions,
|
|
598
|
+
...placeholderData !== void 0 && {
|
|
599
|
+
placeholderData
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
useSuspenseProductListingGroupsQuery(params, options) {
|
|
604
|
+
const queryOptions = params.options;
|
|
605
|
+
return (0, import_react_query4.useSuspenseQuery)({
|
|
606
|
+
queryKey: productKeys.listingGroups(queryOptions),
|
|
607
|
+
queryFn: async () => this.collectionClient.requestFindEndpoint(
|
|
608
|
+
"/api/products/listing-groups/query",
|
|
609
|
+
{ options: queryOptions }
|
|
610
|
+
),
|
|
611
|
+
...options
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
useInfiniteProductListingGroupsQuery(params, options) {
|
|
615
|
+
const {
|
|
616
|
+
options: queryOptions,
|
|
617
|
+
pageSize = 20
|
|
618
|
+
} = params;
|
|
619
|
+
return (0, import_react_query4.useInfiniteQuery)({
|
|
620
|
+
queryKey: productKeys.listingGroupsInfinite(queryOptions),
|
|
621
|
+
queryFn: async ({ pageParam }) => this.collectionClient.requestFindEndpoint(
|
|
622
|
+
"/api/products/listing-groups/query",
|
|
623
|
+
{
|
|
624
|
+
options: { ...queryOptions, page: pageParam, limit: pageSize }
|
|
625
|
+
}
|
|
626
|
+
),
|
|
627
|
+
initialPageParam: 1,
|
|
628
|
+
getNextPageParam: (lastPage) => lastPage.hasNextPage ? lastPage.nextPage : void 0,
|
|
629
|
+
...options
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
useSuspenseInfiniteProductListingGroupsQuery(params, options) {
|
|
633
|
+
const {
|
|
634
|
+
options: queryOptions,
|
|
635
|
+
pageSize = 20
|
|
636
|
+
} = params;
|
|
637
|
+
return (0, import_react_query4.useSuspenseInfiniteQuery)({
|
|
638
|
+
queryKey: productKeys.listingGroupsInfinite(queryOptions),
|
|
639
|
+
queryFn: async ({ pageParam }) => this.collectionClient.requestFindEndpoint(
|
|
640
|
+
"/api/products/listing-groups/query",
|
|
641
|
+
{
|
|
642
|
+
options: { ...queryOptions, page: pageParam, limit: pageSize }
|
|
643
|
+
}
|
|
644
|
+
),
|
|
645
|
+
initialPageParam: 1,
|
|
646
|
+
getNextPageParam: (lastPage) => lastPage.hasNextPage ? lastPage.nextPage : void 0,
|
|
647
|
+
...options
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
async prefetchProductListingGroupsQuery(params, options) {
|
|
651
|
+
const queryOptions = params.options;
|
|
652
|
+
return this.queryClient.prefetchQuery({
|
|
653
|
+
queryKey: productKeys.listingGroups(queryOptions),
|
|
654
|
+
queryFn: async () => this.collectionClient.requestFindEndpoint(
|
|
655
|
+
"/api/products/listing-groups/query",
|
|
656
|
+
{ options: queryOptions }
|
|
657
|
+
),
|
|
658
|
+
...options
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
async prefetchInfiniteProductListingGroupsQuery(params, options) {
|
|
662
|
+
const {
|
|
663
|
+
options: queryOptions,
|
|
664
|
+
pageSize = 20
|
|
665
|
+
} = params;
|
|
666
|
+
return this.queryClient.prefetchInfiniteQuery({
|
|
667
|
+
queryKey: productKeys.listingGroupsInfinite(queryOptions),
|
|
668
|
+
queryFn: async ({ pageParam }) => this.collectionClient.requestFindEndpoint(
|
|
669
|
+
"/api/products/listing-groups/query",
|
|
670
|
+
{
|
|
671
|
+
options: { ...queryOptions, page: pageParam, limit: pageSize }
|
|
672
|
+
}
|
|
673
|
+
),
|
|
674
|
+
initialPageParam: 1,
|
|
675
|
+
getNextPageParam: (lastPage) => lastPage.hasNextPage ? lastPage.nextPage : void 0,
|
|
676
|
+
pages: options?.pages ?? 1,
|
|
677
|
+
staleTime: options?.staleTime
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
useProductDetail(params, options) {
|
|
681
|
+
const discriminator = "slug" in params ? params.slug : params.id;
|
|
682
|
+
const enabled = options?.enabled !== false && Boolean(discriminator);
|
|
683
|
+
return (0, import_react_query4.useQuery)({
|
|
684
|
+
queryKey: productKeys.detail(params),
|
|
685
|
+
queryFn: () => this._commerce.product.detail(params),
|
|
686
|
+
enabled
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
useProductDetailBySlug(slug, options) {
|
|
690
|
+
return this.useProductDetail({ slug }, options);
|
|
691
|
+
}
|
|
692
|
+
useProductDetailById(id, options) {
|
|
693
|
+
return this.useProductDetail({ id }, options);
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
// src/core/query/realtime.ts
|
|
698
|
+
var INITIAL_RECONNECT_DELAY = 1e3;
|
|
699
|
+
var MAX_RECONNECT_DELAY = 3e4;
|
|
700
|
+
var RECONNECT_BACKOFF_FACTOR = 2;
|
|
701
|
+
var MAX_NO_TOKEN_RETRIES = 5;
|
|
702
|
+
var RealtimeConnection = class {
|
|
703
|
+
constructor(baseUrl, publishableKey, getToken, collections) {
|
|
704
|
+
this.baseUrl = baseUrl;
|
|
705
|
+
this.publishableKey = publishableKey;
|
|
706
|
+
this.getToken = getToken;
|
|
707
|
+
this.collections = collections;
|
|
708
|
+
this.abortController = null;
|
|
709
|
+
this.reconnectAttempt = 0;
|
|
710
|
+
this.noTokenAttempts = 0;
|
|
711
|
+
this.reconnectTimer = null;
|
|
712
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
713
|
+
this._connected = false;
|
|
714
|
+
}
|
|
715
|
+
get connected() {
|
|
716
|
+
return this._connected;
|
|
717
|
+
}
|
|
718
|
+
addListener(fn) {
|
|
719
|
+
this.listeners.add(fn);
|
|
720
|
+
return () => this.listeners.delete(fn);
|
|
721
|
+
}
|
|
722
|
+
connect() {
|
|
723
|
+
if (this.abortController) return;
|
|
724
|
+
this.abortController = new AbortController();
|
|
725
|
+
this.startStream(this.abortController.signal);
|
|
726
|
+
}
|
|
727
|
+
disconnect() {
|
|
728
|
+
this._connected = false;
|
|
729
|
+
if (this.reconnectTimer) {
|
|
730
|
+
clearTimeout(this.reconnectTimer);
|
|
731
|
+
this.reconnectTimer = null;
|
|
732
|
+
}
|
|
733
|
+
if (this.abortController) {
|
|
734
|
+
this.abortController.abort();
|
|
735
|
+
this.abortController = null;
|
|
736
|
+
}
|
|
737
|
+
this.reconnectAttempt = 0;
|
|
738
|
+
this.noTokenAttempts = 0;
|
|
739
|
+
}
|
|
740
|
+
async startStream(signal) {
|
|
741
|
+
const token = this.getToken();
|
|
742
|
+
if (!token) {
|
|
743
|
+
this.noTokenAttempts++;
|
|
744
|
+
if (this.noTokenAttempts >= MAX_NO_TOKEN_RETRIES) {
|
|
745
|
+
this._connected = false;
|
|
746
|
+
this.abortController = null;
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
this.scheduleReconnect();
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
this.noTokenAttempts = 0;
|
|
753
|
+
const params = this.collections?.length ? `?collections=${this.collections.join(",")}` : "";
|
|
754
|
+
const url = `${this.baseUrl}/api/events/stream${params}`;
|
|
755
|
+
try {
|
|
756
|
+
const response = await fetch(url, {
|
|
757
|
+
headers: {
|
|
758
|
+
"X-Publishable-Key": this.publishableKey,
|
|
759
|
+
Authorization: `Bearer ${token}`
|
|
760
|
+
},
|
|
761
|
+
signal
|
|
762
|
+
});
|
|
763
|
+
if (!response.ok) {
|
|
764
|
+
if (response.status === 401) {
|
|
765
|
+
this.scheduleReconnect();
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
throw new Error(`SSE connection failed: ${response.status}`);
|
|
769
|
+
}
|
|
770
|
+
if (!response.body) {
|
|
771
|
+
throw new Error("SSE response has no body");
|
|
772
|
+
}
|
|
773
|
+
this._connected = true;
|
|
774
|
+
this.reconnectAttempt = 0;
|
|
775
|
+
await this.readStream(response.body, signal);
|
|
776
|
+
} catch {
|
|
777
|
+
if (signal.aborted) return;
|
|
778
|
+
this._connected = false;
|
|
779
|
+
this.scheduleReconnect();
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
async readStream(body, signal) {
|
|
783
|
+
const reader = body.getReader();
|
|
784
|
+
const decoder = new TextDecoder();
|
|
785
|
+
let buffer = "";
|
|
786
|
+
let currentEvent = "";
|
|
787
|
+
let currentData = "";
|
|
788
|
+
try {
|
|
789
|
+
while (true) {
|
|
790
|
+
const { done, value } = await reader.read();
|
|
791
|
+
if (done || signal.aborted) break;
|
|
792
|
+
buffer += decoder.decode(value, { stream: true });
|
|
793
|
+
const lines = buffer.split("\n");
|
|
794
|
+
buffer = lines.pop() ?? "";
|
|
795
|
+
for (const line of lines) {
|
|
796
|
+
if (line.startsWith("event: ")) {
|
|
797
|
+
currentEvent = line.slice(7);
|
|
798
|
+
} else if (line.startsWith("data: ")) {
|
|
799
|
+
currentData += (currentData ? "\n" : "") + line.slice(6);
|
|
800
|
+
} else if (line === "") {
|
|
801
|
+
if (currentEvent === "collection:change" && currentData) {
|
|
802
|
+
try {
|
|
803
|
+
const event = JSON.parse(currentData);
|
|
804
|
+
for (const listener of this.listeners) {
|
|
805
|
+
try {
|
|
806
|
+
listener(event);
|
|
807
|
+
} catch {
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
} catch {
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
currentEvent = "";
|
|
814
|
+
currentData = "";
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
} catch {
|
|
819
|
+
} finally {
|
|
820
|
+
reader.releaseLock();
|
|
821
|
+
this._connected = false;
|
|
822
|
+
if (!signal.aborted) {
|
|
823
|
+
this.scheduleReconnect();
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
scheduleReconnect() {
|
|
828
|
+
if (this.reconnectTimer) return;
|
|
829
|
+
const delay2 = Math.min(
|
|
830
|
+
INITIAL_RECONNECT_DELAY * Math.pow(RECONNECT_BACKOFF_FACTOR, this.reconnectAttempt),
|
|
831
|
+
MAX_RECONNECT_DELAY
|
|
832
|
+
);
|
|
833
|
+
this.reconnectAttempt++;
|
|
834
|
+
this.reconnectTimer = setTimeout(() => {
|
|
835
|
+
this.reconnectTimer = null;
|
|
836
|
+
this.abortController = new AbortController();
|
|
837
|
+
this.startStream(this.abortController.signal);
|
|
838
|
+
}, delay2);
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
// src/utils/types.ts
|
|
843
|
+
var resolveRelation = (ref) => {
|
|
844
|
+
if (typeof ref === "string" || typeof ref === "number" || ref === null || ref === void 0)
|
|
845
|
+
return null;
|
|
846
|
+
return ref;
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
// src/core/metadata/index.ts
|
|
850
|
+
function extractSeo(doc) {
|
|
851
|
+
const seo = doc.seo ?? {};
|
|
852
|
+
const og = seo.openGraph ?? {};
|
|
853
|
+
return {
|
|
854
|
+
title: seo.title ?? doc.title ?? null,
|
|
855
|
+
description: seo.description ?? null,
|
|
856
|
+
noIndex: seo.noIndex ?? null,
|
|
857
|
+
canonical: seo.canonical ?? null,
|
|
858
|
+
openGraph: {
|
|
859
|
+
title: og.title ?? null,
|
|
860
|
+
description: og.description ?? null,
|
|
861
|
+
image: og.image ?? null
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
function generateMetadata(input, options) {
|
|
866
|
+
const title = input.title ?? void 0;
|
|
867
|
+
const description = input.description ?? void 0;
|
|
868
|
+
const ogTitle = input.openGraph?.title ?? title;
|
|
869
|
+
const ogDescription = input.openGraph?.description ?? description;
|
|
870
|
+
const image = resolveMetaImage(input.openGraph?.image);
|
|
871
|
+
return {
|
|
872
|
+
title,
|
|
873
|
+
description,
|
|
874
|
+
...input.noIndex && { robots: { index: false, follow: false } },
|
|
875
|
+
...input.canonical && { alternates: { canonical: input.canonical } },
|
|
876
|
+
openGraph: {
|
|
877
|
+
...ogTitle && { title: ogTitle },
|
|
878
|
+
...ogDescription && { description: ogDescription },
|
|
879
|
+
...options?.siteName && { siteName: options.siteName },
|
|
880
|
+
...image && { images: [image] }
|
|
881
|
+
},
|
|
882
|
+
twitter: {
|
|
883
|
+
card: image ? "summary_large_image" : "summary",
|
|
884
|
+
...ogTitle && { title: ogTitle },
|
|
885
|
+
...ogDescription && { description: ogDescription },
|
|
886
|
+
...image && { images: [image.url] }
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
function resolveMetaImage(ref) {
|
|
891
|
+
const image = resolveRelation(ref);
|
|
892
|
+
if (!image) return null;
|
|
893
|
+
const sized = image.sizes?.["1536"];
|
|
894
|
+
const url = sized?.url || image.url;
|
|
895
|
+
if (!url) return null;
|
|
896
|
+
const width = sized?.url ? sized.width : image.width;
|
|
897
|
+
const height = sized?.url ? sized.height : image.height;
|
|
898
|
+
return {
|
|
899
|
+
url,
|
|
900
|
+
...width && { width },
|
|
901
|
+
...height && { height },
|
|
902
|
+
...image.alt && { alt: image.alt }
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
// src/core/collection/query-builder.ts
|
|
907
|
+
var CollectionQueryBuilder = class {
|
|
908
|
+
constructor(api, collection) {
|
|
909
|
+
this.api = api;
|
|
910
|
+
this.collection = collection;
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* Find documents (list query)
|
|
914
|
+
* GET /api/{collection}
|
|
915
|
+
* @returns Payload CMS find response with docs array and pagination
|
|
916
|
+
*/
|
|
917
|
+
async find(options) {
|
|
918
|
+
return this.api.requestFind(
|
|
919
|
+
`/api/${String(this.collection)}`,
|
|
920
|
+
options
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Find document by ID
|
|
925
|
+
* GET /api/{collection}/{id}
|
|
926
|
+
* @returns Document object directly (no wrapper)
|
|
927
|
+
*/
|
|
928
|
+
async findById(id, options) {
|
|
929
|
+
return this.api.requestFindById(
|
|
930
|
+
`/api/${String(this.collection)}/${String(id)}`,
|
|
931
|
+
options
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Create a new document
|
|
936
|
+
* POST /api/{collection}
|
|
937
|
+
* @returns Payload CMS mutation response with doc and message
|
|
938
|
+
*/
|
|
939
|
+
async create(data, options) {
|
|
940
|
+
const endpoint = `/api/${String(this.collection)}`;
|
|
941
|
+
if (options?.file) {
|
|
942
|
+
return this.api.requestCreateWithFile(
|
|
943
|
+
endpoint,
|
|
944
|
+
data,
|
|
945
|
+
options.file,
|
|
946
|
+
options.filename
|
|
947
|
+
);
|
|
948
|
+
}
|
|
949
|
+
return this.api.requestCreate(endpoint, data);
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Update a document by ID
|
|
953
|
+
* PATCH /api/{collection}/{id}
|
|
954
|
+
* @returns Payload CMS mutation response with doc and message
|
|
955
|
+
*/
|
|
956
|
+
async update(id, data, options) {
|
|
957
|
+
const endpoint = `/api/${String(this.collection)}/${String(id)}`;
|
|
958
|
+
if (options?.file) {
|
|
959
|
+
return this.api.requestUpdateWithFile(
|
|
960
|
+
endpoint,
|
|
961
|
+
data,
|
|
962
|
+
options.file,
|
|
963
|
+
options.filename
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
return this.api.requestUpdate(endpoint, data);
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Count documents
|
|
970
|
+
* GET /api/{collection}/count
|
|
971
|
+
* @returns Count response with totalDocs
|
|
972
|
+
*/
|
|
973
|
+
async count(options) {
|
|
974
|
+
return this.api.requestCount(
|
|
975
|
+
`/api/${String(this.collection)}/count`,
|
|
976
|
+
options
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Find first matching document and return its Next.js Metadata.
|
|
981
|
+
* Applies depth: 1 (SEO image populate) and limit: 1 automatically.
|
|
982
|
+
* @returns Metadata or null if no document matches
|
|
983
|
+
*/
|
|
984
|
+
async findMetadata(options, metadataOptions) {
|
|
985
|
+
const { docs } = await this.find({ ...options, limit: 1, depth: 1 });
|
|
986
|
+
const doc = docs[0];
|
|
987
|
+
if (!doc) return null;
|
|
988
|
+
return generateMetadata(
|
|
989
|
+
extractSeo(doc),
|
|
990
|
+
metadataOptions
|
|
991
|
+
);
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Find document by ID and return its Next.js Metadata.
|
|
995
|
+
* Applies depth: 1 (SEO image populate) automatically.
|
|
996
|
+
* @returns Metadata (throws on 404)
|
|
997
|
+
*/
|
|
998
|
+
async findMetadataById(id, metadataOptions) {
|
|
999
|
+
const doc = await this.findById(id, { depth: 1 });
|
|
1000
|
+
return generateMetadata(
|
|
1001
|
+
extractSeo(doc),
|
|
1002
|
+
metadataOptions
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Update multiple documents (bulk update)
|
|
1007
|
+
* PATCH /api/{collection}
|
|
1008
|
+
* @returns Payload CMS find response with updated docs
|
|
1009
|
+
*/
|
|
1010
|
+
async updateMany(where, data) {
|
|
1011
|
+
return this.api.requestUpdateMany(
|
|
1012
|
+
`/api/${String(this.collection)}`,
|
|
1013
|
+
{ where, data }
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Delete a document by ID
|
|
1018
|
+
* DELETE /api/{collection}/{id}
|
|
1019
|
+
* @returns Deleted document object directly (no wrapper)
|
|
1020
|
+
*/
|
|
1021
|
+
async remove(id) {
|
|
1022
|
+
return this.api.requestDelete(
|
|
1023
|
+
`/api/${String(this.collection)}/${String(id)}`
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Delete multiple documents (bulk delete)
|
|
1028
|
+
* DELETE /api/{collection}
|
|
1029
|
+
* @returns Payload CMS find response with deleted docs
|
|
1030
|
+
*/
|
|
1031
|
+
async removeMany(where) {
|
|
1032
|
+
return this.api.requestDeleteMany(
|
|
1033
|
+
`/api/${String(this.collection)}`,
|
|
1034
|
+
{ where }
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
// src/core/collection/http-client.ts
|
|
1040
|
+
var import_qs_esm = require("qs-esm");
|
|
1041
|
+
|
|
1042
|
+
// src/core/internal/utils/credentials.ts
|
|
1043
|
+
function requirePublishableKeyForSecret(apiName, publishableKey, secretKey) {
|
|
1044
|
+
if (secretKey && !publishableKey) {
|
|
1045
|
+
throw createConfigError(
|
|
1046
|
+
`publishableKey is required for ${apiName} when secretKey is used. It is sent as X-Publishable-Key for tenant routing, rate limiting, and quota enforcement.`
|
|
1047
|
+
);
|
|
1048
|
+
}
|
|
1049
|
+
return publishableKey ?? "";
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
// src/core/client/types.ts
|
|
1053
|
+
function resolveApiUrl(apiUrl) {
|
|
1054
|
+
if (apiUrl) {
|
|
1055
|
+
return apiUrl.replace(/\/$/, "");
|
|
1056
|
+
}
|
|
1057
|
+
if (typeof process !== "undefined" && process.env) {
|
|
1058
|
+
const envUrl = process.env.SOFTWARE_API_URL || process.env.NEXT_PUBLIC_SOFTWARE_API_URL;
|
|
1059
|
+
if (envUrl) {
|
|
1060
|
+
return envUrl.replace(/\/$/, "");
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
return "https://api.01.software";
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
// src/core/internal/utils/http.ts
|
|
1067
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
1068
|
+
var DEFAULT_RETRYABLE_STATUSES = [408, 429, 500, 502, 503, 504];
|
|
1069
|
+
var NON_RETRYABLE_STATUSES = [400, 401, 403, 404, 409, 422];
|
|
1070
|
+
var SAFE_METHODS = ["GET", "HEAD", "OPTIONS"];
|
|
1071
|
+
function debugLog(debug, type, message, data) {
|
|
1072
|
+
if (!debug) return;
|
|
1073
|
+
const shouldLog = debug === true || type === "request" && debug.logRequests || type === "response" && debug.logResponses || type === "error" && debug.logErrors;
|
|
1074
|
+
if (shouldLog) {
|
|
1075
|
+
console.group(`[SDK ${type.toUpperCase()}] ${message}`);
|
|
1076
|
+
if (data) console.log(data);
|
|
1077
|
+
console.groupEnd();
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
function getErrorSuggestion(status) {
|
|
1081
|
+
if (status === 400)
|
|
1082
|
+
return "The request data failed validation. Check field values and types.";
|
|
1083
|
+
if (status === 401) return "Please check your authentication credentials.";
|
|
1084
|
+
if (status === 403)
|
|
1085
|
+
return "Access denied. Check your credentials or permissions.";
|
|
1086
|
+
if (status === 404) return "The requested resource was not found.";
|
|
1087
|
+
if (status === 422) return "The request data failed validation.";
|
|
1088
|
+
if (status >= 500) return "A server error occurred. Please try again later.";
|
|
1089
|
+
return void 0;
|
|
1090
|
+
}
|
|
1091
|
+
function isUsageLimitExceededResponse(response) {
|
|
1092
|
+
if (response.status !== 429) return false;
|
|
1093
|
+
const limit = parseInt(response.headers.get("X-Usage-Limit") || "", 10);
|
|
1094
|
+
const current = parseInt(response.headers.get("X-Usage-Current") || "", 10);
|
|
1095
|
+
if (!Number.isFinite(limit) || !Number.isFinite(current)) return false;
|
|
1096
|
+
return response.headers.get("X-Usage-Exceeded") === "true" || current > limit;
|
|
1097
|
+
}
|
|
1098
|
+
async function parseErrorBody(response) {
|
|
1099
|
+
const fallback = {
|
|
1100
|
+
errorMessage: `HTTP ${response.status}: ${response.statusText}`,
|
|
1101
|
+
userMessage: `Request failed (status: ${response.status})`
|
|
1102
|
+
};
|
|
1103
|
+
try {
|
|
1104
|
+
const body = await response.json();
|
|
1105
|
+
const reason = typeof body.reason === "string" ? body.reason : typeof body.code === "string" ? body.code : void 0;
|
|
1106
|
+
if (body.errors && Array.isArray(body.errors)) {
|
|
1107
|
+
const fieldErrors = [];
|
|
1108
|
+
for (const e of body.errors) {
|
|
1109
|
+
if (e.data?.errors && Array.isArray(e.data.errors) && e.data.errors.length > 0) {
|
|
1110
|
+
for (const fe of e.data.errors) {
|
|
1111
|
+
fieldErrors.push({
|
|
1112
|
+
field: fe.path || fe.field,
|
|
1113
|
+
message: fe.message
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
} else if (e.field || e.message) {
|
|
1117
|
+
fieldErrors.push({ field: e.field, message: e.message });
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
const details = (fieldErrors.length > 0 ? fieldErrors : body.errors).map(
|
|
1121
|
+
(e) => e.field ? `${e.field}: ${e.message}` : e.message
|
|
1122
|
+
).filter(Boolean).join("; ");
|
|
1123
|
+
if (details) {
|
|
1124
|
+
return {
|
|
1125
|
+
errorMessage: `HTTP ${response.status}: ${details}`,
|
|
1126
|
+
userMessage: details,
|
|
1127
|
+
reason,
|
|
1128
|
+
body,
|
|
1129
|
+
errors: fieldErrors.length > 0 ? fieldErrors : body.errors
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
if (typeof body.error === "string") {
|
|
1134
|
+
return {
|
|
1135
|
+
errorMessage: `HTTP ${response.status}: ${body.error}`,
|
|
1136
|
+
userMessage: body.error,
|
|
1137
|
+
reason,
|
|
1138
|
+
body
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
if (body.message) {
|
|
1142
|
+
return {
|
|
1143
|
+
errorMessage: `HTTP ${response.status}: ${body.message}`,
|
|
1144
|
+
userMessage: body.message,
|
|
1145
|
+
reason,
|
|
1146
|
+
body
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
return { ...fallback, reason, body };
|
|
1150
|
+
} catch {
|
|
1151
|
+
return fallback;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
async function delay(ms) {
|
|
1155
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1156
|
+
}
|
|
1157
|
+
function attachRequestId(err, id) {
|
|
1158
|
+
if (id) err.requestId = id;
|
|
1159
|
+
return err;
|
|
1160
|
+
}
|
|
1161
|
+
function createHttpStatusError(status, parsed, details, requestId) {
|
|
1162
|
+
const errorDetails = {
|
|
1163
|
+
...details,
|
|
1164
|
+
...parsed.errors && { errors: parsed.errors },
|
|
1165
|
+
...parsed.body && { body: parsed.body }
|
|
1166
|
+
};
|
|
1167
|
+
const suggestion = getErrorSuggestion(status);
|
|
1168
|
+
if (status === 400 || status === 422) {
|
|
1169
|
+
return attachRequestId(
|
|
1170
|
+
createValidationError(
|
|
1171
|
+
parsed.errorMessage,
|
|
1172
|
+
errorDetails,
|
|
1173
|
+
parsed.userMessage,
|
|
1174
|
+
suggestion,
|
|
1175
|
+
status
|
|
1176
|
+
),
|
|
1177
|
+
requestId
|
|
1178
|
+
);
|
|
1179
|
+
}
|
|
1180
|
+
if (status === 401) {
|
|
1181
|
+
return attachRequestId(
|
|
1182
|
+
createAuthError(
|
|
1183
|
+
parsed.errorMessage,
|
|
1184
|
+
errorDetails,
|
|
1185
|
+
parsed.userMessage,
|
|
1186
|
+
suggestion
|
|
1187
|
+
),
|
|
1188
|
+
requestId
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
if (status === 403) {
|
|
1192
|
+
return attachRequestId(
|
|
1193
|
+
createPermissionError(
|
|
1194
|
+
parsed.errorMessage,
|
|
1195
|
+
errorDetails,
|
|
1196
|
+
parsed.userMessage,
|
|
1197
|
+
suggestion
|
|
1198
|
+
),
|
|
1199
|
+
requestId
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
if (status === 404) {
|
|
1203
|
+
return attachRequestId(
|
|
1204
|
+
createNotFoundError(
|
|
1205
|
+
parsed.errorMessage,
|
|
1206
|
+
errorDetails,
|
|
1207
|
+
parsed.userMessage,
|
|
1208
|
+
suggestion
|
|
1209
|
+
),
|
|
1210
|
+
requestId
|
|
1211
|
+
);
|
|
1212
|
+
}
|
|
1213
|
+
if (status === 409) {
|
|
1214
|
+
return attachRequestId(
|
|
1215
|
+
createConflictError(
|
|
1216
|
+
parsed.errorMessage,
|
|
1217
|
+
errorDetails,
|
|
1218
|
+
parsed.userMessage,
|
|
1219
|
+
suggestion
|
|
1220
|
+
),
|
|
1221
|
+
requestId
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
return attachRequestId(
|
|
1225
|
+
createNetworkError(
|
|
1226
|
+
parsed.errorMessage,
|
|
1227
|
+
status,
|
|
1228
|
+
errorDetails,
|
|
1229
|
+
parsed.userMessage,
|
|
1230
|
+
suggestion
|
|
1231
|
+
),
|
|
1232
|
+
requestId
|
|
1233
|
+
);
|
|
1234
|
+
}
|
|
1235
|
+
async function httpFetch(url, options) {
|
|
1236
|
+
const {
|
|
1237
|
+
apiUrl,
|
|
1238
|
+
publishableKey,
|
|
1239
|
+
secretKey,
|
|
1240
|
+
customerToken,
|
|
1241
|
+
timeout = DEFAULT_TIMEOUT,
|
|
1242
|
+
debug,
|
|
1243
|
+
retry,
|
|
1244
|
+
onUnauthorized,
|
|
1245
|
+
...requestInit
|
|
1246
|
+
} = options || {};
|
|
1247
|
+
const baseUrl = resolveApiUrl(apiUrl);
|
|
1248
|
+
const retryConfig = {
|
|
1249
|
+
maxRetries: retry?.maxRetries ?? 3,
|
|
1250
|
+
retryableStatuses: retry?.retryableStatuses ?? DEFAULT_RETRYABLE_STATUSES,
|
|
1251
|
+
retryDelay: retry?.retryDelay ?? ((attempt) => Math.min(1e3 * 2 ** attempt, 1e4))
|
|
1252
|
+
};
|
|
1253
|
+
let authToken;
|
|
1254
|
+
if (secretKey) {
|
|
1255
|
+
authToken = secretKey;
|
|
1256
|
+
} else if (customerToken) {
|
|
1257
|
+
authToken = customerToken;
|
|
1258
|
+
}
|
|
1259
|
+
let lastError;
|
|
1260
|
+
let hasRetried401 = false;
|
|
1261
|
+
for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
|
|
1262
|
+
try {
|
|
1263
|
+
const headers = new Headers(requestInit.headers);
|
|
1264
|
+
if (publishableKey) {
|
|
1265
|
+
headers.set("X-Publishable-Key", publishableKey);
|
|
1266
|
+
}
|
|
1267
|
+
if (authToken) {
|
|
1268
|
+
headers.set("Authorization", `Bearer ${authToken}`);
|
|
1269
|
+
}
|
|
1270
|
+
if (!headers.has("Content-Type") && requestInit.body && !(requestInit.body instanceof FormData)) {
|
|
1271
|
+
headers.set("Content-Type", "application/json");
|
|
1272
|
+
}
|
|
1273
|
+
const redactedHeaders = Object.fromEntries(headers.entries());
|
|
1274
|
+
if (redactedHeaders["authorization"]) {
|
|
1275
|
+
const token = redactedHeaders["authorization"];
|
|
1276
|
+
redactedHeaders["authorization"] = token.length > 20 ? `Bearer ...****${token.slice(-8)}` : "****";
|
|
1277
|
+
}
|
|
1278
|
+
debugLog(debug, "request", url, {
|
|
1279
|
+
method: requestInit.method || "GET",
|
|
1280
|
+
headers: redactedHeaders,
|
|
1281
|
+
attempt: attempt + 1
|
|
1282
|
+
});
|
|
1283
|
+
const controller = new AbortController();
|
|
1284
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
1285
|
+
const response = await fetch(`${baseUrl}${url}`, {
|
|
1286
|
+
...requestInit,
|
|
1287
|
+
headers,
|
|
1288
|
+
signal: controller.signal
|
|
1289
|
+
});
|
|
1290
|
+
clearTimeout(timeoutId);
|
|
1291
|
+
const requestId = response.headers.get("x-request-id") ?? void 0;
|
|
1292
|
+
debugLog(debug, "response", url, {
|
|
1293
|
+
status: response.status,
|
|
1294
|
+
statusText: response.statusText,
|
|
1295
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
1296
|
+
});
|
|
1297
|
+
if (!response.ok) {
|
|
1298
|
+
if (isUsageLimitExceededResponse(response)) {
|
|
1299
|
+
const limit = parseInt(
|
|
1300
|
+
response.headers.get("X-Usage-Limit") || "0",
|
|
1301
|
+
10
|
|
1302
|
+
);
|
|
1303
|
+
const current = parseInt(
|
|
1304
|
+
response.headers.get("X-Usage-Current") || "0",
|
|
1305
|
+
10
|
|
1306
|
+
);
|
|
1307
|
+
const remaining = parseInt(
|
|
1308
|
+
response.headers.get("X-Usage-Remaining") || "0",
|
|
1309
|
+
10
|
|
1310
|
+
);
|
|
1311
|
+
throw attachRequestId(
|
|
1312
|
+
createUsageLimitError(
|
|
1313
|
+
`Monthly API usage limit exceeded (${current.toLocaleString()}/${limit.toLocaleString()})`,
|
|
1314
|
+
{ limit, current, remaining },
|
|
1315
|
+
{
|
|
1316
|
+
url,
|
|
1317
|
+
method: requestInit.method || "GET",
|
|
1318
|
+
attempt: attempt + 1
|
|
1319
|
+
},
|
|
1320
|
+
"Monthly API call limit exceeded. Please upgrade your plan.",
|
|
1321
|
+
"Upgrade your tenant plan to increase the monthly API call limit."
|
|
1322
|
+
),
|
|
1323
|
+
requestId
|
|
1324
|
+
);
|
|
1325
|
+
}
|
|
1326
|
+
const parsed = await parseErrorBody(response);
|
|
1327
|
+
if (response.status === 401 && onUnauthorized && customerToken && !hasRetried401 && parsed.reason === "token_expired") {
|
|
1328
|
+
hasRetried401 = true;
|
|
1329
|
+
try {
|
|
1330
|
+
const newToken = await onUnauthorized();
|
|
1331
|
+
if (newToken) {
|
|
1332
|
+
authToken = newToken;
|
|
1333
|
+
continue;
|
|
1334
|
+
}
|
|
1335
|
+
} catch {
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
const details = {
|
|
1339
|
+
url,
|
|
1340
|
+
method: requestInit.method || "GET",
|
|
1341
|
+
attempt: attempt + 1
|
|
1342
|
+
};
|
|
1343
|
+
if (NON_RETRYABLE_STATUSES.includes(response.status)) {
|
|
1344
|
+
throw createHttpStatusError(
|
|
1345
|
+
response.status,
|
|
1346
|
+
parsed,
|
|
1347
|
+
details,
|
|
1348
|
+
requestId
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
const error = attachRequestId(
|
|
1352
|
+
createNetworkError(
|
|
1353
|
+
parsed.errorMessage,
|
|
1354
|
+
response.status,
|
|
1355
|
+
details,
|
|
1356
|
+
parsed.userMessage,
|
|
1357
|
+
getErrorSuggestion(response.status)
|
|
1358
|
+
),
|
|
1359
|
+
requestId
|
|
1360
|
+
);
|
|
1361
|
+
const method = (requestInit.method || "GET").toUpperCase();
|
|
1362
|
+
if (attempt < retryConfig.maxRetries && SAFE_METHODS.includes(method) && retryConfig.retryableStatuses.includes(response.status)) {
|
|
1363
|
+
lastError = error;
|
|
1364
|
+
const retryDelay = retryConfig.retryDelay(attempt);
|
|
1365
|
+
debugLog(debug, "error", `Retrying in ${retryDelay}ms...`, error);
|
|
1366
|
+
await delay(retryDelay);
|
|
1367
|
+
continue;
|
|
1368
|
+
}
|
|
1369
|
+
throw error;
|
|
1370
|
+
}
|
|
1371
|
+
return response;
|
|
1372
|
+
} catch (error) {
|
|
1373
|
+
debugLog(debug, "error", url, error);
|
|
1374
|
+
const method = (requestInit.method || "GET").toUpperCase();
|
|
1375
|
+
const isSafe = SAFE_METHODS.includes(method);
|
|
1376
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
1377
|
+
const timeoutError = createTimeoutError(
|
|
1378
|
+
`Request timed out after ${timeout}ms.`,
|
|
1379
|
+
{ url, timeout, attempt: attempt + 1 },
|
|
1380
|
+
"The request timed out.",
|
|
1381
|
+
"Please check your network connection or try again later."
|
|
1382
|
+
);
|
|
1383
|
+
if (isSafe && attempt < retryConfig.maxRetries) {
|
|
1384
|
+
lastError = timeoutError;
|
|
1385
|
+
await delay(retryConfig.retryDelay(attempt));
|
|
1386
|
+
continue;
|
|
1387
|
+
}
|
|
1388
|
+
throw timeoutError;
|
|
1389
|
+
}
|
|
1390
|
+
if (error instanceof TypeError) {
|
|
1391
|
+
const networkError = createNetworkError(
|
|
1392
|
+
"Network connection failed.",
|
|
1393
|
+
void 0,
|
|
1394
|
+
{ url, originalError: error.message, attempt: attempt + 1 },
|
|
1395
|
+
"Network connection failed.",
|
|
1396
|
+
"Please check your internet connection and try again."
|
|
1397
|
+
);
|
|
1398
|
+
if (isSafe && attempt < retryConfig.maxRetries) {
|
|
1399
|
+
lastError = networkError;
|
|
1400
|
+
await delay(retryConfig.retryDelay(attempt));
|
|
1401
|
+
continue;
|
|
1402
|
+
}
|
|
1403
|
+
throw networkError;
|
|
1404
|
+
}
|
|
1405
|
+
if (error instanceof NetworkError || error instanceof TimeoutError) {
|
|
1406
|
+
if (isSafe && attempt < retryConfig.maxRetries && error.status && !NON_RETRYABLE_STATUSES.includes(error.status) && retryConfig.retryableStatuses.includes(error.status)) {
|
|
1407
|
+
lastError = error;
|
|
1408
|
+
await delay(retryConfig.retryDelay(attempt));
|
|
1409
|
+
continue;
|
|
1410
|
+
}
|
|
1411
|
+
throw error;
|
|
1412
|
+
}
|
|
1413
|
+
if (error instanceof SDKError) {
|
|
1414
|
+
throw error;
|
|
1415
|
+
}
|
|
1416
|
+
const unknownError = createNetworkError(
|
|
1417
|
+
error instanceof Error ? error.message : "An unknown network error occurred.",
|
|
1418
|
+
void 0,
|
|
1419
|
+
{ url, originalError: error, attempt: attempt + 1 },
|
|
1420
|
+
"An unknown error occurred.",
|
|
1421
|
+
"Please try again later."
|
|
1422
|
+
);
|
|
1423
|
+
if (isSafe && attempt < retryConfig.maxRetries) {
|
|
1424
|
+
lastError = unknownError;
|
|
1425
|
+
await delay(retryConfig.retryDelay(attempt));
|
|
1426
|
+
continue;
|
|
1427
|
+
}
|
|
1428
|
+
throw unknownError;
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
throw lastError ?? new NetworkError("Request failed after retries");
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
// src/core/collection/http-client.ts
|
|
1435
|
+
var HttpClient = class {
|
|
1436
|
+
constructor(publishableKey, secretKey, getCustomerToken, onUnauthorized, onRequestId, apiUrl) {
|
|
1437
|
+
this.publishableKey = requirePublishableKeyForSecret(
|
|
1438
|
+
"CollectionClient",
|
|
1439
|
+
publishableKey,
|
|
1440
|
+
secretKey
|
|
1441
|
+
);
|
|
1442
|
+
this.secretKey = secretKey;
|
|
1443
|
+
this.getCustomerToken = getCustomerToken;
|
|
1444
|
+
this.onUnauthorized = onUnauthorized;
|
|
1445
|
+
this.onRequestId = onRequestId;
|
|
1446
|
+
this.apiUrl = apiUrl;
|
|
1447
|
+
}
|
|
1448
|
+
get defaultOptions() {
|
|
1449
|
+
const opts = {
|
|
1450
|
+
apiUrl: this.apiUrl,
|
|
1451
|
+
publishableKey: this.publishableKey,
|
|
1452
|
+
secretKey: this.secretKey
|
|
1453
|
+
};
|
|
1454
|
+
const token = this.getCustomerToken?.();
|
|
1455
|
+
if (token) {
|
|
1456
|
+
opts.customerToken = token;
|
|
1457
|
+
if (this.onUnauthorized) {
|
|
1458
|
+
opts.onUnauthorized = this.onUnauthorized;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
return opts;
|
|
1462
|
+
}
|
|
1463
|
+
async fetchWithTracking(url, opts) {
|
|
1464
|
+
try {
|
|
1465
|
+
const response = await httpFetch(url, opts);
|
|
1466
|
+
this.onRequestId?.(response.headers.get("x-request-id") ?? null);
|
|
1467
|
+
return response;
|
|
1468
|
+
} catch (err) {
|
|
1469
|
+
const id = err instanceof SDKError ? err.requestId ?? null : null;
|
|
1470
|
+
this.onRequestId?.(id);
|
|
1471
|
+
throw err;
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
buildUrl(endpoint, options) {
|
|
1475
|
+
if (!options) return endpoint;
|
|
1476
|
+
const queryString = (0, import_qs_esm.stringify)(options, { addQueryPrefix: true });
|
|
1477
|
+
return queryString ? `${endpoint}${queryString}` : endpoint;
|
|
1478
|
+
}
|
|
1479
|
+
assertJsonResponse(response) {
|
|
1480
|
+
const contentType = response.headers.get("content-type");
|
|
1481
|
+
if (!contentType?.includes("application/json")) {
|
|
1482
|
+
throw createApiError("Response is not in JSON format.", response.status, {
|
|
1483
|
+
contentType
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
/**
|
|
1488
|
+
* Parse Payload CMS find response (list query)
|
|
1489
|
+
* Returns native Payload response structure
|
|
1490
|
+
*/
|
|
1491
|
+
async parseFindResponse(response) {
|
|
1492
|
+
const contentType = response.headers.get("content-type");
|
|
1493
|
+
try {
|
|
1494
|
+
this.assertJsonResponse(response);
|
|
1495
|
+
const jsonData = await response.json();
|
|
1496
|
+
if (jsonData.docs === void 0) {
|
|
1497
|
+
throw createApiError("Invalid find response.", response.status, {
|
|
1498
|
+
jsonData
|
|
1499
|
+
});
|
|
1500
|
+
}
|
|
1501
|
+
return {
|
|
1502
|
+
docs: jsonData.docs,
|
|
1503
|
+
totalDocs: jsonData.totalDocs ?? 0,
|
|
1504
|
+
limit: jsonData.limit || 20,
|
|
1505
|
+
totalPages: jsonData.totalPages ?? 0,
|
|
1506
|
+
page: jsonData.page || 1,
|
|
1507
|
+
pagingCounter: jsonData.pagingCounter || 1,
|
|
1508
|
+
hasPrevPage: jsonData.hasPrevPage ?? false,
|
|
1509
|
+
hasNextPage: jsonData.hasNextPage ?? false,
|
|
1510
|
+
prevPage: jsonData.prevPage ?? null,
|
|
1511
|
+
nextPage: jsonData.nextPage ?? null
|
|
1512
|
+
};
|
|
1513
|
+
} catch (error) {
|
|
1514
|
+
if (error instanceof SDKError) throw error;
|
|
1515
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
1516
|
+
contentType,
|
|
1517
|
+
error: error instanceof Error ? error.message : error
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
/**
|
|
1522
|
+
* Parse Payload CMS mutation response (create/update)
|
|
1523
|
+
* Returns native Payload response structure
|
|
1524
|
+
*/
|
|
1525
|
+
async parseMutationResponse(response) {
|
|
1526
|
+
const contentType = response.headers.get("content-type");
|
|
1527
|
+
try {
|
|
1528
|
+
this.assertJsonResponse(response);
|
|
1529
|
+
const jsonData = await response.json();
|
|
1530
|
+
if (jsonData.doc === void 0) {
|
|
1531
|
+
throw createApiError("Invalid mutation response.", response.status, {
|
|
1532
|
+
jsonData
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
return {
|
|
1536
|
+
message: jsonData.message || "",
|
|
1537
|
+
doc: jsonData.doc,
|
|
1538
|
+
errors: jsonData.errors
|
|
1539
|
+
};
|
|
1540
|
+
} catch (error) {
|
|
1541
|
+
if (error instanceof SDKError) throw error;
|
|
1542
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
1543
|
+
contentType,
|
|
1544
|
+
error: error instanceof Error ? error.message : error
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
/**
|
|
1549
|
+
* Parse Payload CMS document response (findById/delete)
|
|
1550
|
+
* Returns document directly without wrapper
|
|
1551
|
+
*/
|
|
1552
|
+
async parseDocumentResponse(response) {
|
|
1553
|
+
const contentType = response.headers.get("content-type");
|
|
1554
|
+
try {
|
|
1555
|
+
this.assertJsonResponse(response);
|
|
1556
|
+
const jsonData = await response.json();
|
|
1557
|
+
return jsonData;
|
|
1558
|
+
} catch (error) {
|
|
1559
|
+
if (error instanceof SDKError) throw error;
|
|
1560
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
1561
|
+
contentType,
|
|
1562
|
+
error: error instanceof Error ? error.message : error
|
|
1563
|
+
});
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
};
|
|
1567
|
+
|
|
1568
|
+
// src/core/collection/collection-client.ts
|
|
1569
|
+
function buildPayloadFormData(data, file, filename) {
|
|
1570
|
+
const formData = new FormData();
|
|
1571
|
+
formData.append("file", file, filename);
|
|
1572
|
+
if (data != null) {
|
|
1573
|
+
formData.append("_payload", JSON.stringify(data));
|
|
1574
|
+
}
|
|
1575
|
+
return formData;
|
|
1576
|
+
}
|
|
1577
|
+
var CollectionClient = class extends HttpClient {
|
|
1578
|
+
from(collection) {
|
|
1579
|
+
return new CollectionQueryBuilder(this, collection);
|
|
1580
|
+
}
|
|
1581
|
+
// ============================================================================
|
|
1582
|
+
// Payload-native methods
|
|
1583
|
+
// ============================================================================
|
|
1584
|
+
/**
|
|
1585
|
+
* Find documents (list query)
|
|
1586
|
+
* GET /api/{collection}
|
|
1587
|
+
*/
|
|
1588
|
+
async requestFind(endpoint, options) {
|
|
1589
|
+
const url = this.buildUrl(endpoint, options);
|
|
1590
|
+
const response = await this.fetchWithTracking(url, {
|
|
1591
|
+
...this.defaultOptions,
|
|
1592
|
+
method: "GET"
|
|
1593
|
+
});
|
|
1594
|
+
return this.parseFindResponse(response);
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Find-like response from a custom endpoint
|
|
1598
|
+
* POST /api/...custom-endpoint
|
|
1599
|
+
*/
|
|
1600
|
+
async requestFindEndpoint(endpoint, data) {
|
|
1601
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1602
|
+
...this.defaultOptions,
|
|
1603
|
+
method: "POST",
|
|
1604
|
+
body: data ? JSON.stringify(data) : void 0
|
|
1605
|
+
});
|
|
1606
|
+
return this.parseFindResponse(response);
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Find document by ID
|
|
1610
|
+
* GET /api/{collection}/{id}
|
|
1611
|
+
*/
|
|
1612
|
+
async requestFindById(endpoint, options) {
|
|
1613
|
+
const url = this.buildUrl(endpoint, options);
|
|
1614
|
+
const response = await this.fetchWithTracking(url, {
|
|
1615
|
+
...this.defaultOptions,
|
|
1616
|
+
method: "GET"
|
|
1617
|
+
});
|
|
1618
|
+
return this.parseDocumentResponse(response);
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* Create document
|
|
1622
|
+
* POST /api/{collection}
|
|
1623
|
+
*/
|
|
1624
|
+
async requestCreate(endpoint, data) {
|
|
1625
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1626
|
+
...this.defaultOptions,
|
|
1627
|
+
method: "POST",
|
|
1628
|
+
body: data ? JSON.stringify(data) : void 0
|
|
1629
|
+
});
|
|
1630
|
+
return this.parseMutationResponse(response);
|
|
1631
|
+
}
|
|
1632
|
+
/**
|
|
1633
|
+
* Update document
|
|
1634
|
+
* PATCH /api/{collection}/{id}
|
|
1635
|
+
*/
|
|
1636
|
+
async requestUpdate(endpoint, data) {
|
|
1637
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1638
|
+
...this.defaultOptions,
|
|
1639
|
+
method: "PATCH",
|
|
1640
|
+
body: data ? JSON.stringify(data) : void 0
|
|
1641
|
+
});
|
|
1642
|
+
return this.parseMutationResponse(response);
|
|
1643
|
+
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Count documents
|
|
1646
|
+
* GET /api/{collection}/count
|
|
1647
|
+
*/
|
|
1648
|
+
async requestCount(endpoint, options) {
|
|
1649
|
+
const url = this.buildUrl(endpoint, options);
|
|
1650
|
+
const response = await this.fetchWithTracking(url, {
|
|
1651
|
+
...this.defaultOptions,
|
|
1652
|
+
method: "GET"
|
|
1653
|
+
});
|
|
1654
|
+
return this.parseDocumentResponse(response);
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Update multiple documents (bulk update)
|
|
1658
|
+
* PATCH /api/{collection}
|
|
1659
|
+
*/
|
|
1660
|
+
async requestUpdateMany(endpoint, data) {
|
|
1661
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1662
|
+
...this.defaultOptions,
|
|
1663
|
+
method: "PATCH",
|
|
1664
|
+
body: JSON.stringify(data)
|
|
1665
|
+
});
|
|
1666
|
+
return this.parseFindResponse(response);
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1669
|
+
* Delete document
|
|
1670
|
+
* DELETE /api/{collection}/{id}
|
|
1671
|
+
*/
|
|
1672
|
+
async requestDelete(endpoint) {
|
|
1673
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1674
|
+
...this.defaultOptions,
|
|
1675
|
+
method: "DELETE"
|
|
1676
|
+
});
|
|
1677
|
+
return this.parseDocumentResponse(response);
|
|
1678
|
+
}
|
|
1679
|
+
/**
|
|
1680
|
+
* Delete multiple documents (bulk delete)
|
|
1681
|
+
* DELETE /api/{collection}
|
|
1682
|
+
*/
|
|
1683
|
+
async requestDeleteMany(endpoint, data) {
|
|
1684
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1685
|
+
...this.defaultOptions,
|
|
1686
|
+
method: "DELETE",
|
|
1687
|
+
body: JSON.stringify(data)
|
|
1688
|
+
});
|
|
1689
|
+
return this.parseFindResponse(response);
|
|
1690
|
+
}
|
|
1691
|
+
/**
|
|
1692
|
+
* Create document with file upload
|
|
1693
|
+
* POST /api/{collection} (multipart/form-data)
|
|
1694
|
+
*/
|
|
1695
|
+
async requestCreateWithFile(endpoint, data, file, filename) {
|
|
1696
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1697
|
+
...this.defaultOptions,
|
|
1698
|
+
method: "POST",
|
|
1699
|
+
body: buildPayloadFormData(data, file, filename)
|
|
1700
|
+
});
|
|
1701
|
+
return this.parseMutationResponse(response);
|
|
1702
|
+
}
|
|
1703
|
+
/**
|
|
1704
|
+
* Update document with file upload
|
|
1705
|
+
* PATCH /api/{collection}/{id} (multipart/form-data)
|
|
1706
|
+
*/
|
|
1707
|
+
async requestUpdateWithFile(endpoint, data, file, filename) {
|
|
1708
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1709
|
+
...this.defaultOptions,
|
|
1710
|
+
method: "PATCH",
|
|
1711
|
+
body: buildPayloadFormData(data, file, filename)
|
|
1712
|
+
});
|
|
1713
|
+
return this.parseMutationResponse(response);
|
|
1714
|
+
}
|
|
1715
|
+
};
|
|
1716
|
+
|
|
1717
|
+
// src/core/query/query-client.ts
|
|
1718
|
+
function createReadOnlyQueryHooksFacade(hooks) {
|
|
1719
|
+
return {
|
|
1720
|
+
useQuery: hooks.useQuery.bind(hooks),
|
|
1721
|
+
useSuspenseQuery: hooks.useSuspenseQuery.bind(hooks),
|
|
1722
|
+
useQueryById: hooks.useQueryById.bind(hooks),
|
|
1723
|
+
useSuspenseQueryById: hooks.useSuspenseQueryById.bind(hooks),
|
|
1724
|
+
useInfiniteQuery: hooks.useInfiniteQuery.bind(hooks),
|
|
1725
|
+
useSuspenseInfiniteQuery: hooks.useSuspenseInfiniteQuery.bind(hooks),
|
|
1726
|
+
prefetchQuery: hooks.prefetchQuery.bind(hooks),
|
|
1727
|
+
prefetchQueryById: hooks.prefetchQueryById.bind(hooks),
|
|
1728
|
+
prefetchInfiniteQuery: hooks.prefetchInfiniteQuery.bind(hooks),
|
|
1729
|
+
invalidateQueries: hooks.invalidateQueries.bind(hooks),
|
|
1730
|
+
getQueryData: hooks.getQueryData.bind(hooks),
|
|
1731
|
+
setQueryData: hooks.setQueryData.bind(hooks),
|
|
1732
|
+
useCustomerMe: hooks.useCustomerMe.bind(hooks),
|
|
1733
|
+
useCustomerLogin: hooks.useCustomerLogin.bind(hooks),
|
|
1734
|
+
useCustomerRegister: hooks.useCustomerRegister.bind(hooks),
|
|
1735
|
+
useCustomerLogout: hooks.useCustomerLogout.bind(hooks),
|
|
1736
|
+
useCustomerForgotPassword: hooks.useCustomerForgotPassword.bind(hooks),
|
|
1737
|
+
useCustomerResetPassword: hooks.useCustomerResetPassword.bind(hooks),
|
|
1738
|
+
useCustomerRefreshToken: hooks.useCustomerRefreshToken.bind(hooks),
|
|
1739
|
+
useCustomerUpdateProfile: hooks.useCustomerUpdateProfile.bind(hooks),
|
|
1740
|
+
useCustomerChangePassword: hooks.useCustomerChangePassword.bind(hooks),
|
|
1741
|
+
invalidateCustomerQueries: hooks.invalidateCustomerQueries.bind(hooks),
|
|
1742
|
+
getCustomerData: hooks.getCustomerData.bind(hooks),
|
|
1743
|
+
setCustomerData: hooks.setCustomerData.bind(hooks),
|
|
1744
|
+
useProductListingGroupsQuery: hooks.useProductListingGroupsQuery.bind(hooks),
|
|
1745
|
+
useSuspenseProductListingGroupsQuery: hooks.useSuspenseProductListingGroupsQuery.bind(hooks),
|
|
1746
|
+
useInfiniteProductListingGroupsQuery: hooks.useInfiniteProductListingGroupsQuery.bind(hooks),
|
|
1747
|
+
useSuspenseInfiniteProductListingGroupsQuery: hooks.useSuspenseInfiniteProductListingGroupsQuery.bind(hooks),
|
|
1748
|
+
prefetchProductListingGroupsQuery: hooks.prefetchProductListingGroupsQuery.bind(hooks),
|
|
1749
|
+
prefetchInfiniteProductListingGroupsQuery: hooks.prefetchInfiniteProductListingGroupsQuery.bind(hooks),
|
|
1750
|
+
useProductDetail: hooks.useProductDetail.bind(hooks),
|
|
1751
|
+
useProductDetailBySlug: hooks.useProductDetailBySlug.bind(hooks),
|
|
1752
|
+
useProductDetailById: hooks.useProductDetailById.bind(hooks)
|
|
1753
|
+
};
|
|
1754
|
+
}
|
|
1755
|
+
function createQueryHooks(client, queryClient = getQueryClient()) {
|
|
1756
|
+
const config = client.getConfig();
|
|
1757
|
+
const onUnauthorized = async () => {
|
|
1758
|
+
try {
|
|
1759
|
+
const result = await client.customer.auth.refreshToken();
|
|
1760
|
+
return result.token ?? null;
|
|
1761
|
+
} catch {
|
|
1762
|
+
return null;
|
|
1763
|
+
}
|
|
1764
|
+
};
|
|
1765
|
+
const collectionClient = new CollectionClient(
|
|
1766
|
+
config.publishableKey,
|
|
1767
|
+
void 0,
|
|
1768
|
+
() => client.customer.auth.getToken(),
|
|
1769
|
+
onUnauthorized,
|
|
1770
|
+
(id) => {
|
|
1771
|
+
client.lastRequestId = id;
|
|
1772
|
+
},
|
|
1773
|
+
config.apiUrl
|
|
1774
|
+
);
|
|
1775
|
+
const hooks = new QueryHooks(
|
|
1776
|
+
queryClient,
|
|
1777
|
+
collectionClient,
|
|
1778
|
+
client.customer.auth,
|
|
1779
|
+
client.commerce
|
|
1780
|
+
);
|
|
1781
|
+
return createReadOnlyQueryHooksFacade(hooks);
|
|
1782
|
+
}
|
|
1783
|
+
function createServerQueryHooks(client, queryClient = getQueryClient()) {
|
|
1784
|
+
return new QueryHooks(
|
|
1785
|
+
queryClient,
|
|
1786
|
+
client.collections,
|
|
1787
|
+
void 0,
|
|
1788
|
+
client.commerce
|
|
1789
|
+
);
|
|
1790
|
+
}
|
|
1791
|
+
//# sourceMappingURL=query.cjs.map
|