wcz-test 6.9.3 → 6.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DialogsHooks-Bi8dZoyu.js +338 -0
- package/dist/DialogsHooks-Bi8dZoyu.js.map +1 -0
- package/dist/{RouterListItemButton-Cx7rXEfm.js → RouterListItemButton-CHS7rofI.js} +4 -4
- package/dist/{RouterListItemButton-Cx7rXEfm.js.map → RouterListItemButton-CHS7rofI.js.map} +1 -1
- package/dist/auth-client-B6cIXYDV.js +1417 -0
- package/dist/auth-client-B6cIXYDV.js.map +1 -0
- package/dist/client.js +1 -1
- package/dist/components.js +65 -66
- package/dist/components.js.map +1 -1
- package/dist/error-BhAKg8LX-X0sdNXNa.js +195 -0
- package/dist/error-BhAKg8LX-X0sdNXNa.js.map +1 -0
- package/dist/hooks.js +31 -31
- package/dist/hooks.js.map +1 -1
- package/dist/index.js +83 -84
- package/dist/index.js.map +1 -1
- package/dist/server-mxQ3s5dx-CC81W42s.js +644 -0
- package/dist/server-mxQ3s5dx-CC81W42s.js.map +1 -0
- package/dist/server.js +8659 -7
- package/dist/server.js.map +1 -1
- package/package.json +29 -29
- package/dist/DialogsHooks-BlUsVlfv.js +0 -39
- package/dist/DialogsHooks-BlUsVlfv.js.map +0 -1
- package/dist/auth-client-o9U0_qmf.js +0 -79
- package/dist/auth-client-o9U0_qmf.js.map +0 -1
|
@@ -0,0 +1,1417 @@
|
|
|
1
|
+
import { createTheme, darken, lighten } from "@mui/material";
|
|
2
|
+
import { grey } from "@mui/material/colors";
|
|
3
|
+
import { enUS as enUS$2, csCZ as csCZ$2 } from "@mui/material/locale";
|
|
4
|
+
import { enUS, csCZ } from "@mui/x-data-grid/locales";
|
|
5
|
+
import { enUS as enUS$1, csCZ as csCZ$1 } from "@mui/x-date-pickers/locales";
|
|
6
|
+
import { useTranslation } from "react-i18next";
|
|
7
|
+
import { e as env, B as BetterAuthError } from "./error-BhAKg8LX-X0sdNXNa.js";
|
|
8
|
+
import { useRef, useCallback, useSyncExternalStore } from "react";
|
|
9
|
+
import { c as clientEnv } from "./env-CoxTjaDr.js";
|
|
10
|
+
const WISTRON_PRIMARY_COLOR = "#00506E";
|
|
11
|
+
const WISTRON_SECONDARY_COLOR = "#64DC00";
|
|
12
|
+
const LOCALE_MAP = {
|
|
13
|
+
cs: [csCZ, csCZ$1, csCZ$2],
|
|
14
|
+
en: [enUS, enUS$1, enUS$2]
|
|
15
|
+
};
|
|
16
|
+
const useGetTheme = (theme) => {
|
|
17
|
+
const { i18n } = useTranslation();
|
|
18
|
+
return createTheme(
|
|
19
|
+
{
|
|
20
|
+
cssVariables: {
|
|
21
|
+
colorSchemeSelector: "data-mui-color-scheme"
|
|
22
|
+
},
|
|
23
|
+
colorSchemes: {
|
|
24
|
+
light: {
|
|
25
|
+
palette: {
|
|
26
|
+
primary: { main: WISTRON_PRIMARY_COLOR },
|
|
27
|
+
secondary: { main: WISTRON_SECONDARY_COLOR }
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
dark: {
|
|
31
|
+
palette: {
|
|
32
|
+
primary: { main: lighten(WISTRON_PRIMARY_COLOR, 0.5) },
|
|
33
|
+
secondary: { main: darken(WISTRON_SECONDARY_COLOR, 0.5) }
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
...theme?.colorSchemes
|
|
37
|
+
},
|
|
38
|
+
components: {
|
|
39
|
+
MuiCssBaseline: {
|
|
40
|
+
styleOverrides: ({ palette }) => {
|
|
41
|
+
return {
|
|
42
|
+
body: {
|
|
43
|
+
"&::-webkit-scrollbar, & *::-webkit-scrollbar": {
|
|
44
|
+
width: "0.7em",
|
|
45
|
+
height: "0.7em"
|
|
46
|
+
},
|
|
47
|
+
"&::-webkit-scrollbar-track, & *::-webkit-scrollbar-track": {
|
|
48
|
+
backgroundColor: palette.mode === "dark" ? grey[900] : grey[200],
|
|
49
|
+
borderRadius: "5px"
|
|
50
|
+
},
|
|
51
|
+
"&::-webkit-scrollbar-thumb, & *::-webkit-scrollbar-thumb": {
|
|
52
|
+
backgroundColor: palette.mode === "dark" ? grey[800] : grey[400],
|
|
53
|
+
borderRadius: "10px"
|
|
54
|
+
},
|
|
55
|
+
"&::-webkit-scrollbar-thumb:hover, & *::-webkit-scrollbar-thumb:hover": {
|
|
56
|
+
backgroundColor: palette.mode === "dark" ? grey[700] : grey[500]
|
|
57
|
+
},
|
|
58
|
+
"&::-webkit-scrollbar-corner, & *::-webkit-scrollbar-corner": {
|
|
59
|
+
backgroundColor: "transparent"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
...theme?.components
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
...LOCALE_MAP[i18n.language]
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
function checkHasPath(url) {
|
|
72
|
+
try {
|
|
73
|
+
return (new URL(url).pathname.replace(/\/+$/, "") || "/") !== "/";
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw new BetterAuthError(`Invalid base URL: ${url}. Please provide a valid base URL.`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function assertHasProtocol(url) {
|
|
79
|
+
try {
|
|
80
|
+
const parsedUrl = new URL(url);
|
|
81
|
+
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") throw new BetterAuthError(`Invalid base URL: ${url}. URL must include 'http://' or 'https://'`);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (error instanceof BetterAuthError) throw error;
|
|
84
|
+
throw new BetterAuthError(`Invalid base URL: ${url}. Please provide a valid base URL.`, String(error));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function withPath(url, path = "/api/auth") {
|
|
88
|
+
assertHasProtocol(url);
|
|
89
|
+
if (checkHasPath(url)) return url;
|
|
90
|
+
const trimmedUrl = url.replace(/\/+$/, "");
|
|
91
|
+
if (!path || path === "/") return trimmedUrl;
|
|
92
|
+
path = path.startsWith("/") ? path : `/${path}`;
|
|
93
|
+
return `${trimmedUrl}${path}`;
|
|
94
|
+
}
|
|
95
|
+
function getBaseURL(url, path, request, loadEnv) {
|
|
96
|
+
if (url) return withPath(url, path);
|
|
97
|
+
{
|
|
98
|
+
const fromEnv = env.BETTER_AUTH_URL || env.NEXT_PUBLIC_BETTER_AUTH_URL || env.PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_AUTH_URL || (env.BASE_URL !== "/" ? env.BASE_URL : void 0);
|
|
99
|
+
if (fromEnv) return withPath(fromEnv, path);
|
|
100
|
+
}
|
|
101
|
+
if (typeof window !== "undefined" && window.location) return withPath(window.location.origin, path);
|
|
102
|
+
}
|
|
103
|
+
const PROTO_POLLUTION_PATTERNS = {
|
|
104
|
+
proto: /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/,
|
|
105
|
+
constructor: /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/,
|
|
106
|
+
protoShort: /"__proto__"\s*:/,
|
|
107
|
+
constructorShort: /"constructor"\s*:/
|
|
108
|
+
};
|
|
109
|
+
const JSON_SIGNATURE = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/;
|
|
110
|
+
const SPECIAL_VALUES = {
|
|
111
|
+
true: true,
|
|
112
|
+
false: false,
|
|
113
|
+
null: null,
|
|
114
|
+
undefined: void 0,
|
|
115
|
+
nan: NaN,
|
|
116
|
+
infinity: Number.POSITIVE_INFINITY,
|
|
117
|
+
"-infinity": Number.NEGATIVE_INFINITY
|
|
118
|
+
};
|
|
119
|
+
const ISO_DATE_REGEX = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,7}))?(?:Z|([+-])(\d{2}):(\d{2}))$/;
|
|
120
|
+
function isValidDate(date) {
|
|
121
|
+
return date instanceof Date && !isNaN(date.getTime());
|
|
122
|
+
}
|
|
123
|
+
function parseISODate(value) {
|
|
124
|
+
const match = ISO_DATE_REGEX.exec(value);
|
|
125
|
+
if (!match) return null;
|
|
126
|
+
const [, year, month, day, hour, minute, second, ms, offsetSign, offsetHour, offsetMinute] = match;
|
|
127
|
+
let date = new Date(Date.UTC(parseInt(year, 10), parseInt(month, 10) - 1, parseInt(day, 10), parseInt(hour, 10), parseInt(minute, 10), parseInt(second, 10), ms ? parseInt(ms.padEnd(3, "0"), 10) : 0));
|
|
128
|
+
if (offsetSign) {
|
|
129
|
+
const offset = (parseInt(offsetHour, 10) * 60 + parseInt(offsetMinute, 10)) * (offsetSign === "+" ? -1 : 1);
|
|
130
|
+
date.setUTCMinutes(date.getUTCMinutes() + offset);
|
|
131
|
+
}
|
|
132
|
+
return isValidDate(date) ? date : null;
|
|
133
|
+
}
|
|
134
|
+
function betterJSONParse(value, options = {}) {
|
|
135
|
+
const { strict = false, warnings = false, reviver, parseDates = true } = options;
|
|
136
|
+
if (typeof value !== "string") return value;
|
|
137
|
+
const trimmed = value.trim();
|
|
138
|
+
if (trimmed.length > 0 && trimmed[0] === '"' && trimmed.endsWith('"') && !trimmed.slice(1, -1).includes('"')) return trimmed.slice(1, -1);
|
|
139
|
+
const lowerValue = trimmed.toLowerCase();
|
|
140
|
+
if (lowerValue.length <= 9 && lowerValue in SPECIAL_VALUES) return SPECIAL_VALUES[lowerValue];
|
|
141
|
+
if (!JSON_SIGNATURE.test(trimmed)) {
|
|
142
|
+
if (strict) throw new SyntaxError("[better-json] Invalid JSON");
|
|
143
|
+
return value;
|
|
144
|
+
}
|
|
145
|
+
if (Object.entries(PROTO_POLLUTION_PATTERNS).some(([key, pattern]) => {
|
|
146
|
+
const matches = pattern.test(trimmed);
|
|
147
|
+
if (matches && warnings) console.warn(`[better-json] Detected potential prototype pollution attempt using ${key} pattern`);
|
|
148
|
+
return matches;
|
|
149
|
+
}) && strict) throw new Error("[better-json] Potential prototype pollution attempt detected");
|
|
150
|
+
try {
|
|
151
|
+
const secureReviver = (key, value$1) => {
|
|
152
|
+
if (key === "__proto__" || key === "constructor" && value$1 && typeof value$1 === "object" && "prototype" in value$1) {
|
|
153
|
+
if (warnings) console.warn(`[better-json] Dropping "${key}" key to prevent prototype pollution`);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (parseDates && typeof value$1 === "string") {
|
|
157
|
+
const date = parseISODate(value$1);
|
|
158
|
+
if (date) return date;
|
|
159
|
+
}
|
|
160
|
+
return reviver ? reviver(key, value$1) : value$1;
|
|
161
|
+
};
|
|
162
|
+
return JSON.parse(trimmed, secureReviver);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (strict) throw error;
|
|
165
|
+
return value;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function parseJSON(value, options = { strict: true }) {
|
|
169
|
+
return betterJSONParse(value, options);
|
|
170
|
+
}
|
|
171
|
+
let clean = Symbol("clean");
|
|
172
|
+
let listenerQueue = [];
|
|
173
|
+
let lqIndex = 0;
|
|
174
|
+
const QUEUE_ITEMS_PER_LISTENER = 4;
|
|
175
|
+
const atom = /* @__NO_SIDE_EFFECTS__ */ (initialValue) => {
|
|
176
|
+
let listeners = [];
|
|
177
|
+
let $atom = {
|
|
178
|
+
get() {
|
|
179
|
+
if (!$atom.lc) {
|
|
180
|
+
$atom.listen(() => {
|
|
181
|
+
})();
|
|
182
|
+
}
|
|
183
|
+
return $atom.value;
|
|
184
|
+
},
|
|
185
|
+
lc: 0,
|
|
186
|
+
listen(listener) {
|
|
187
|
+
$atom.lc = listeners.push(listener);
|
|
188
|
+
return () => {
|
|
189
|
+
for (let i = lqIndex + QUEUE_ITEMS_PER_LISTENER; i < listenerQueue.length; ) {
|
|
190
|
+
if (listenerQueue[i] === listener) {
|
|
191
|
+
listenerQueue.splice(i, QUEUE_ITEMS_PER_LISTENER);
|
|
192
|
+
} else {
|
|
193
|
+
i += QUEUE_ITEMS_PER_LISTENER;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
let index = listeners.indexOf(listener);
|
|
197
|
+
if (~index) {
|
|
198
|
+
listeners.splice(index, 1);
|
|
199
|
+
if (!--$atom.lc) $atom.off();
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
notify(oldValue, changedKey) {
|
|
204
|
+
let runListenerQueue = !listenerQueue.length;
|
|
205
|
+
for (let listener of listeners) {
|
|
206
|
+
listenerQueue.push(listener, $atom.value, oldValue, changedKey);
|
|
207
|
+
}
|
|
208
|
+
if (runListenerQueue) {
|
|
209
|
+
for (lqIndex = 0; lqIndex < listenerQueue.length; lqIndex += QUEUE_ITEMS_PER_LISTENER) {
|
|
210
|
+
listenerQueue[lqIndex](
|
|
211
|
+
listenerQueue[lqIndex + 1],
|
|
212
|
+
listenerQueue[lqIndex + 2],
|
|
213
|
+
listenerQueue[lqIndex + 3]
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
listenerQueue.length = 0;
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
/* It will be called on last listener unsubscribing.
|
|
220
|
+
We will redefine it in onMount and onStop. */
|
|
221
|
+
off() {
|
|
222
|
+
},
|
|
223
|
+
set(newValue) {
|
|
224
|
+
let oldValue = $atom.value;
|
|
225
|
+
if (oldValue !== newValue) {
|
|
226
|
+
$atom.value = newValue;
|
|
227
|
+
$atom.notify(oldValue);
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
subscribe(listener) {
|
|
231
|
+
let unbind = $atom.listen(listener);
|
|
232
|
+
listener($atom.value);
|
|
233
|
+
return unbind;
|
|
234
|
+
},
|
|
235
|
+
value: initialValue
|
|
236
|
+
};
|
|
237
|
+
if (process.env.NODE_ENV !== "production") {
|
|
238
|
+
$atom[clean] = () => {
|
|
239
|
+
listeners = [];
|
|
240
|
+
$atom.lc = 0;
|
|
241
|
+
$atom.off();
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
return $atom;
|
|
245
|
+
};
|
|
246
|
+
const MOUNT = 5;
|
|
247
|
+
const UNMOUNT = 6;
|
|
248
|
+
const REVERT_MUTATION = 10;
|
|
249
|
+
let on = (object, listener, eventKey, mutateStore) => {
|
|
250
|
+
object.events = object.events || {};
|
|
251
|
+
if (!object.events[eventKey + REVERT_MUTATION]) {
|
|
252
|
+
object.events[eventKey + REVERT_MUTATION] = mutateStore((eventProps) => {
|
|
253
|
+
object.events[eventKey].reduceRight((event, l) => (l(event), event), {
|
|
254
|
+
shared: {},
|
|
255
|
+
...eventProps
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
object.events[eventKey] = object.events[eventKey] || [];
|
|
260
|
+
object.events[eventKey].push(listener);
|
|
261
|
+
return () => {
|
|
262
|
+
let currentListeners = object.events[eventKey];
|
|
263
|
+
let index = currentListeners.indexOf(listener);
|
|
264
|
+
currentListeners.splice(index, 1);
|
|
265
|
+
if (!currentListeners.length) {
|
|
266
|
+
delete object.events[eventKey];
|
|
267
|
+
object.events[eventKey + REVERT_MUTATION]();
|
|
268
|
+
delete object.events[eventKey + REVERT_MUTATION];
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
let STORE_UNMOUNT_DELAY = 1e3;
|
|
273
|
+
let onMount = ($store, initialize) => {
|
|
274
|
+
let listener = (payload) => {
|
|
275
|
+
let destroy = initialize(payload);
|
|
276
|
+
if (destroy) $store.events[UNMOUNT].push(destroy);
|
|
277
|
+
};
|
|
278
|
+
return on($store, listener, MOUNT, (runListeners) => {
|
|
279
|
+
let originListen = $store.listen;
|
|
280
|
+
$store.listen = (...args) => {
|
|
281
|
+
if (!$store.lc && !$store.active) {
|
|
282
|
+
$store.active = true;
|
|
283
|
+
runListeners();
|
|
284
|
+
}
|
|
285
|
+
return originListen(...args);
|
|
286
|
+
};
|
|
287
|
+
let originOff = $store.off;
|
|
288
|
+
$store.events[UNMOUNT] = [];
|
|
289
|
+
$store.off = () => {
|
|
290
|
+
originOff();
|
|
291
|
+
setTimeout(() => {
|
|
292
|
+
if ($store.active && !$store.lc) {
|
|
293
|
+
$store.active = false;
|
|
294
|
+
for (let destroy of $store.events[UNMOUNT]) destroy();
|
|
295
|
+
$store.events[UNMOUNT] = [];
|
|
296
|
+
}
|
|
297
|
+
}, STORE_UNMOUNT_DELAY);
|
|
298
|
+
};
|
|
299
|
+
if (process.env.NODE_ENV !== "production") {
|
|
300
|
+
let originClean = $store[clean];
|
|
301
|
+
$store[clean] = () => {
|
|
302
|
+
for (let destroy of $store.events[UNMOUNT]) destroy();
|
|
303
|
+
$store.events[UNMOUNT] = [];
|
|
304
|
+
$store.active = false;
|
|
305
|
+
originClean();
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
return () => {
|
|
309
|
+
$store.listen = originListen;
|
|
310
|
+
$store.off = originOff;
|
|
311
|
+
};
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
function listenKeys($store, keys, listener) {
|
|
315
|
+
let keysSet = new Set(keys).add(void 0);
|
|
316
|
+
return $store.listen((value, oldValue, changed) => {
|
|
317
|
+
if (keysSet.has(changed)) {
|
|
318
|
+
listener(value, oldValue, changed);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
var __defProp = Object.defineProperty;
|
|
323
|
+
var __defProps = Object.defineProperties;
|
|
324
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
325
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
326
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
327
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
328
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
329
|
+
var __spreadValues = (a, b) => {
|
|
330
|
+
for (var prop in b || (b = {}))
|
|
331
|
+
if (__hasOwnProp.call(b, prop))
|
|
332
|
+
__defNormalProp(a, prop, b[prop]);
|
|
333
|
+
if (__getOwnPropSymbols)
|
|
334
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
335
|
+
if (__propIsEnum.call(b, prop))
|
|
336
|
+
__defNormalProp(a, prop, b[prop]);
|
|
337
|
+
}
|
|
338
|
+
return a;
|
|
339
|
+
};
|
|
340
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
341
|
+
var BetterFetchError = class extends Error {
|
|
342
|
+
constructor(status, statusText, error) {
|
|
343
|
+
super(statusText || status.toString(), {
|
|
344
|
+
cause: error
|
|
345
|
+
});
|
|
346
|
+
this.status = status;
|
|
347
|
+
this.statusText = statusText;
|
|
348
|
+
this.error = error;
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
var initializePlugins = async (url, options) => {
|
|
352
|
+
var _a, _b, _c, _d, _e, _f;
|
|
353
|
+
let opts = options || {};
|
|
354
|
+
const hooks = {
|
|
355
|
+
onRequest: [options == null ? void 0 : options.onRequest],
|
|
356
|
+
onResponse: [options == null ? void 0 : options.onResponse],
|
|
357
|
+
onSuccess: [options == null ? void 0 : options.onSuccess],
|
|
358
|
+
onError: [options == null ? void 0 : options.onError],
|
|
359
|
+
onRetry: [options == null ? void 0 : options.onRetry]
|
|
360
|
+
};
|
|
361
|
+
if (!options || !(options == null ? void 0 : options.plugins)) {
|
|
362
|
+
return {
|
|
363
|
+
url,
|
|
364
|
+
options: opts,
|
|
365
|
+
hooks
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
for (const plugin of (options == null ? void 0 : options.plugins) || []) {
|
|
369
|
+
if (plugin.init) {
|
|
370
|
+
const pluginRes = await ((_a = plugin.init) == null ? void 0 : _a.call(plugin, url.toString(), options));
|
|
371
|
+
opts = pluginRes.options || opts;
|
|
372
|
+
url = pluginRes.url;
|
|
373
|
+
}
|
|
374
|
+
hooks.onRequest.push((_b = plugin.hooks) == null ? void 0 : _b.onRequest);
|
|
375
|
+
hooks.onResponse.push((_c = plugin.hooks) == null ? void 0 : _c.onResponse);
|
|
376
|
+
hooks.onSuccess.push((_d = plugin.hooks) == null ? void 0 : _d.onSuccess);
|
|
377
|
+
hooks.onError.push((_e = plugin.hooks) == null ? void 0 : _e.onError);
|
|
378
|
+
hooks.onRetry.push((_f = plugin.hooks) == null ? void 0 : _f.onRetry);
|
|
379
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
url,
|
|
382
|
+
options: opts,
|
|
383
|
+
hooks
|
|
384
|
+
};
|
|
385
|
+
};
|
|
386
|
+
var LinearRetryStrategy = class {
|
|
387
|
+
constructor(options) {
|
|
388
|
+
this.options = options;
|
|
389
|
+
}
|
|
390
|
+
shouldAttemptRetry(attempt, response) {
|
|
391
|
+
if (this.options.shouldRetry) {
|
|
392
|
+
return Promise.resolve(
|
|
393
|
+
attempt < this.options.attempts && this.options.shouldRetry(response)
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
return Promise.resolve(attempt < this.options.attempts);
|
|
397
|
+
}
|
|
398
|
+
getDelay() {
|
|
399
|
+
return this.options.delay;
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
var ExponentialRetryStrategy = class {
|
|
403
|
+
constructor(options) {
|
|
404
|
+
this.options = options;
|
|
405
|
+
}
|
|
406
|
+
shouldAttemptRetry(attempt, response) {
|
|
407
|
+
if (this.options.shouldRetry) {
|
|
408
|
+
return Promise.resolve(
|
|
409
|
+
attempt < this.options.attempts && this.options.shouldRetry(response)
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
return Promise.resolve(attempt < this.options.attempts);
|
|
413
|
+
}
|
|
414
|
+
getDelay(attempt) {
|
|
415
|
+
const delay = Math.min(
|
|
416
|
+
this.options.maxDelay,
|
|
417
|
+
this.options.baseDelay * 2 ** attempt
|
|
418
|
+
);
|
|
419
|
+
return delay;
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
function createRetryStrategy(options) {
|
|
423
|
+
if (typeof options === "number") {
|
|
424
|
+
return new LinearRetryStrategy({
|
|
425
|
+
type: "linear",
|
|
426
|
+
attempts: options,
|
|
427
|
+
delay: 1e3
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
switch (options.type) {
|
|
431
|
+
case "linear":
|
|
432
|
+
return new LinearRetryStrategy(options);
|
|
433
|
+
case "exponential":
|
|
434
|
+
return new ExponentialRetryStrategy(options);
|
|
435
|
+
default:
|
|
436
|
+
throw new Error("Invalid retry strategy");
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
var getAuthHeader = async (options) => {
|
|
440
|
+
const headers = {};
|
|
441
|
+
const getValue = async (value) => typeof value === "function" ? await value() : value;
|
|
442
|
+
if (options == null ? void 0 : options.auth) {
|
|
443
|
+
if (options.auth.type === "Bearer") {
|
|
444
|
+
const token = await getValue(options.auth.token);
|
|
445
|
+
if (!token) {
|
|
446
|
+
return headers;
|
|
447
|
+
}
|
|
448
|
+
headers["authorization"] = `Bearer ${token}`;
|
|
449
|
+
} else if (options.auth.type === "Basic") {
|
|
450
|
+
const username = getValue(options.auth.username);
|
|
451
|
+
const password = getValue(options.auth.password);
|
|
452
|
+
if (!username || !password) {
|
|
453
|
+
return headers;
|
|
454
|
+
}
|
|
455
|
+
headers["authorization"] = `Basic ${btoa(`${username}:${password}`)}`;
|
|
456
|
+
} else if (options.auth.type === "Custom") {
|
|
457
|
+
const value = getValue(options.auth.value);
|
|
458
|
+
if (!value) {
|
|
459
|
+
return headers;
|
|
460
|
+
}
|
|
461
|
+
headers["authorization"] = `${getValue(options.auth.prefix)} ${value}`;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return headers;
|
|
465
|
+
};
|
|
466
|
+
var JSON_RE = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i;
|
|
467
|
+
function detectResponseType(request) {
|
|
468
|
+
const _contentType = request.headers.get("content-type");
|
|
469
|
+
const textTypes = /* @__PURE__ */ new Set([
|
|
470
|
+
"image/svg",
|
|
471
|
+
"application/xml",
|
|
472
|
+
"application/xhtml",
|
|
473
|
+
"application/html"
|
|
474
|
+
]);
|
|
475
|
+
if (!_contentType) {
|
|
476
|
+
return "json";
|
|
477
|
+
}
|
|
478
|
+
const contentType = _contentType.split(";").shift() || "";
|
|
479
|
+
if (JSON_RE.test(contentType)) {
|
|
480
|
+
return "json";
|
|
481
|
+
}
|
|
482
|
+
if (textTypes.has(contentType) || contentType.startsWith("text/")) {
|
|
483
|
+
return "text";
|
|
484
|
+
}
|
|
485
|
+
return "blob";
|
|
486
|
+
}
|
|
487
|
+
function isJSONParsable(value) {
|
|
488
|
+
try {
|
|
489
|
+
JSON.parse(value);
|
|
490
|
+
return true;
|
|
491
|
+
} catch (error) {
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function isJSONSerializable(value) {
|
|
496
|
+
if (value === void 0) {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
const t = typeof value;
|
|
500
|
+
if (t === "string" || t === "number" || t === "boolean" || t === null) {
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
if (t !== "object") {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
if (Array.isArray(value)) {
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
if (value.buffer) {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
return value.constructor && value.constructor.name === "Object" || typeof value.toJSON === "function";
|
|
513
|
+
}
|
|
514
|
+
function jsonParse(text) {
|
|
515
|
+
try {
|
|
516
|
+
return JSON.parse(text);
|
|
517
|
+
} catch (error) {
|
|
518
|
+
return text;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function isFunction(value) {
|
|
522
|
+
return typeof value === "function";
|
|
523
|
+
}
|
|
524
|
+
function getFetch(options) {
|
|
525
|
+
if (options == null ? void 0 : options.customFetchImpl) {
|
|
526
|
+
return options.customFetchImpl;
|
|
527
|
+
}
|
|
528
|
+
if (typeof globalThis !== "undefined" && isFunction(globalThis.fetch)) {
|
|
529
|
+
return globalThis.fetch;
|
|
530
|
+
}
|
|
531
|
+
if (typeof window !== "undefined" && isFunction(window.fetch)) {
|
|
532
|
+
return window.fetch;
|
|
533
|
+
}
|
|
534
|
+
throw new Error("No fetch implementation found");
|
|
535
|
+
}
|
|
536
|
+
async function getHeaders(opts) {
|
|
537
|
+
const headers = new Headers(opts == null ? void 0 : opts.headers);
|
|
538
|
+
const authHeader = await getAuthHeader(opts);
|
|
539
|
+
for (const [key, value] of Object.entries(authHeader || {})) {
|
|
540
|
+
headers.set(key, value);
|
|
541
|
+
}
|
|
542
|
+
if (!headers.has("content-type")) {
|
|
543
|
+
const t = detectContentType(opts == null ? void 0 : opts.body);
|
|
544
|
+
if (t) {
|
|
545
|
+
headers.set("content-type", t);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return headers;
|
|
549
|
+
}
|
|
550
|
+
function detectContentType(body) {
|
|
551
|
+
if (isJSONSerializable(body)) {
|
|
552
|
+
return "application/json";
|
|
553
|
+
}
|
|
554
|
+
return null;
|
|
555
|
+
}
|
|
556
|
+
function getBody(options) {
|
|
557
|
+
if (!(options == null ? void 0 : options.body)) {
|
|
558
|
+
return null;
|
|
559
|
+
}
|
|
560
|
+
const headers = new Headers(options == null ? void 0 : options.headers);
|
|
561
|
+
if (isJSONSerializable(options.body) && !headers.has("content-type")) {
|
|
562
|
+
for (const [key, value] of Object.entries(options == null ? void 0 : options.body)) {
|
|
563
|
+
if (value instanceof Date) {
|
|
564
|
+
options.body[key] = value.toISOString();
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return JSON.stringify(options.body);
|
|
568
|
+
}
|
|
569
|
+
return options.body;
|
|
570
|
+
}
|
|
571
|
+
function getMethod$1(url, options) {
|
|
572
|
+
var _a;
|
|
573
|
+
if (options == null ? void 0 : options.method) {
|
|
574
|
+
return options.method.toUpperCase();
|
|
575
|
+
}
|
|
576
|
+
if (url.startsWith("@")) {
|
|
577
|
+
const pMethod = (_a = url.split("@")[1]) == null ? void 0 : _a.split("/")[0];
|
|
578
|
+
if (!methods.includes(pMethod)) {
|
|
579
|
+
return (options == null ? void 0 : options.body) ? "POST" : "GET";
|
|
580
|
+
}
|
|
581
|
+
return pMethod.toUpperCase();
|
|
582
|
+
}
|
|
583
|
+
return (options == null ? void 0 : options.body) ? "POST" : "GET";
|
|
584
|
+
}
|
|
585
|
+
function getTimeout(options, controller) {
|
|
586
|
+
let abortTimeout;
|
|
587
|
+
if (!(options == null ? void 0 : options.signal) && (options == null ? void 0 : options.timeout)) {
|
|
588
|
+
abortTimeout = setTimeout(() => controller == null ? void 0 : controller.abort(), options == null ? void 0 : options.timeout);
|
|
589
|
+
}
|
|
590
|
+
return {
|
|
591
|
+
abortTimeout,
|
|
592
|
+
clearTimeout: () => {
|
|
593
|
+
if (abortTimeout) {
|
|
594
|
+
clearTimeout(abortTimeout);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
var ValidationError = class _ValidationError extends Error {
|
|
600
|
+
constructor(issues, message) {
|
|
601
|
+
super(message || JSON.stringify(issues, null, 2));
|
|
602
|
+
this.issues = issues;
|
|
603
|
+
Object.setPrototypeOf(this, _ValidationError.prototype);
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
async function parseStandardSchema(schema, input) {
|
|
607
|
+
let result = await schema["~standard"].validate(input);
|
|
608
|
+
if (result.issues) {
|
|
609
|
+
throw new ValidationError(result.issues);
|
|
610
|
+
}
|
|
611
|
+
return result.value;
|
|
612
|
+
}
|
|
613
|
+
var methods = ["get", "post", "put", "patch", "delete"];
|
|
614
|
+
var applySchemaPlugin = (config) => ({
|
|
615
|
+
id: "apply-schema",
|
|
616
|
+
name: "Apply Schema",
|
|
617
|
+
version: "1.0.0",
|
|
618
|
+
async init(url, options) {
|
|
619
|
+
var _a, _b, _c, _d;
|
|
620
|
+
const schema = ((_b = (_a = config.plugins) == null ? void 0 : _a.find(
|
|
621
|
+
(plugin) => {
|
|
622
|
+
var _a2;
|
|
623
|
+
return ((_a2 = plugin.schema) == null ? void 0 : _a2.config) ? url.startsWith(plugin.schema.config.baseURL || "") || url.startsWith(plugin.schema.config.prefix || "") : false;
|
|
624
|
+
}
|
|
625
|
+
)) == null ? void 0 : _b.schema) || config.schema;
|
|
626
|
+
if (schema) {
|
|
627
|
+
let urlKey = url;
|
|
628
|
+
if ((_c = schema.config) == null ? void 0 : _c.prefix) {
|
|
629
|
+
if (urlKey.startsWith(schema.config.prefix)) {
|
|
630
|
+
urlKey = urlKey.replace(schema.config.prefix, "");
|
|
631
|
+
if (schema.config.baseURL) {
|
|
632
|
+
url = url.replace(schema.config.prefix, schema.config.baseURL);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
if ((_d = schema.config) == null ? void 0 : _d.baseURL) {
|
|
637
|
+
if (urlKey.startsWith(schema.config.baseURL)) {
|
|
638
|
+
urlKey = urlKey.replace(schema.config.baseURL, "");
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
const keySchema = schema.schema[urlKey];
|
|
642
|
+
if (keySchema) {
|
|
643
|
+
let opts = __spreadProps(__spreadValues({}, options), {
|
|
644
|
+
method: keySchema.method,
|
|
645
|
+
output: keySchema.output
|
|
646
|
+
});
|
|
647
|
+
if (!(options == null ? void 0 : options.disableValidation)) {
|
|
648
|
+
opts = __spreadProps(__spreadValues({}, opts), {
|
|
649
|
+
body: keySchema.input ? await parseStandardSchema(keySchema.input, options == null ? void 0 : options.body) : options == null ? void 0 : options.body,
|
|
650
|
+
params: keySchema.params ? await parseStandardSchema(keySchema.params, options == null ? void 0 : options.params) : options == null ? void 0 : options.params,
|
|
651
|
+
query: keySchema.query ? await parseStandardSchema(keySchema.query, options == null ? void 0 : options.query) : options == null ? void 0 : options.query
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
return {
|
|
655
|
+
url,
|
|
656
|
+
options: opts
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return {
|
|
661
|
+
url,
|
|
662
|
+
options
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
var createFetch = (config) => {
|
|
667
|
+
async function $fetch(url, options) {
|
|
668
|
+
const opts = __spreadProps(__spreadValues(__spreadValues({}, config), options), {
|
|
669
|
+
plugins: [...(config == null ? void 0 : config.plugins) || [], applySchemaPlugin(config || {})]
|
|
670
|
+
});
|
|
671
|
+
if (config == null ? void 0 : config.catchAllError) {
|
|
672
|
+
try {
|
|
673
|
+
return await betterFetch(url, opts);
|
|
674
|
+
} catch (error) {
|
|
675
|
+
return {
|
|
676
|
+
data: null,
|
|
677
|
+
error: {
|
|
678
|
+
status: 500,
|
|
679
|
+
statusText: "Fetch Error",
|
|
680
|
+
message: "Fetch related error. Captured by catchAllError option. See error property for more details.",
|
|
681
|
+
error
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return await betterFetch(url, opts);
|
|
687
|
+
}
|
|
688
|
+
return $fetch;
|
|
689
|
+
};
|
|
690
|
+
function getURL2(url, option) {
|
|
691
|
+
let { baseURL, params, query } = option || {
|
|
692
|
+
query: {},
|
|
693
|
+
params: {},
|
|
694
|
+
baseURL: ""
|
|
695
|
+
};
|
|
696
|
+
let basePath = url.startsWith("http") ? url.split("/").slice(0, 3).join("/") : baseURL || "";
|
|
697
|
+
if (url.startsWith("@")) {
|
|
698
|
+
const m = url.toString().split("@")[1].split("/")[0];
|
|
699
|
+
if (methods.includes(m)) {
|
|
700
|
+
url = url.replace(`@${m}/`, "/");
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
if (!basePath.endsWith("/")) basePath += "/";
|
|
704
|
+
let [path, urlQuery] = url.replace(basePath, "").split("?");
|
|
705
|
+
const queryParams = new URLSearchParams(urlQuery);
|
|
706
|
+
for (const [key, value] of Object.entries(query || {})) {
|
|
707
|
+
if (value == null) continue;
|
|
708
|
+
queryParams.set(key, String(value));
|
|
709
|
+
}
|
|
710
|
+
if (params) {
|
|
711
|
+
if (Array.isArray(params)) {
|
|
712
|
+
const paramPaths = path.split("/").filter((p) => p.startsWith(":"));
|
|
713
|
+
for (const [index, key] of paramPaths.entries()) {
|
|
714
|
+
const value = params[index];
|
|
715
|
+
path = path.replace(key, value);
|
|
716
|
+
}
|
|
717
|
+
} else {
|
|
718
|
+
for (const [key, value] of Object.entries(params)) {
|
|
719
|
+
path = path.replace(`:${key}`, String(value));
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
path = path.split("/").map(encodeURIComponent).join("/");
|
|
724
|
+
if (path.startsWith("/")) path = path.slice(1);
|
|
725
|
+
let queryParamString = queryParams.toString();
|
|
726
|
+
queryParamString = queryParamString.length > 0 ? `?${queryParamString}`.replace(/\+/g, "%20") : "";
|
|
727
|
+
if (!basePath.startsWith("http")) {
|
|
728
|
+
return `${basePath}${path}${queryParamString}`;
|
|
729
|
+
}
|
|
730
|
+
const _url = new URL(`${path}${queryParamString}`, basePath);
|
|
731
|
+
return _url;
|
|
732
|
+
}
|
|
733
|
+
var betterFetch = async (url, options) => {
|
|
734
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
735
|
+
const {
|
|
736
|
+
hooks,
|
|
737
|
+
url: __url,
|
|
738
|
+
options: opts
|
|
739
|
+
} = await initializePlugins(url, options);
|
|
740
|
+
const fetch2 = getFetch(opts);
|
|
741
|
+
const controller = new AbortController();
|
|
742
|
+
const signal = (_a = opts.signal) != null ? _a : controller.signal;
|
|
743
|
+
const _url = getURL2(__url, opts);
|
|
744
|
+
const body = getBody(opts);
|
|
745
|
+
const headers = await getHeaders(opts);
|
|
746
|
+
const method = getMethod$1(__url, opts);
|
|
747
|
+
let context = __spreadProps(__spreadValues({}, opts), {
|
|
748
|
+
url: _url,
|
|
749
|
+
headers,
|
|
750
|
+
body,
|
|
751
|
+
method,
|
|
752
|
+
signal
|
|
753
|
+
});
|
|
754
|
+
for (const onRequest of hooks.onRequest) {
|
|
755
|
+
if (onRequest) {
|
|
756
|
+
const res = await onRequest(context);
|
|
757
|
+
if (res instanceof Object) {
|
|
758
|
+
context = res;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
if ("pipeTo" in context && typeof context.pipeTo === "function" || typeof ((_b = options == null ? void 0 : options.body) == null ? void 0 : _b.pipe) === "function") {
|
|
763
|
+
if (!("duplex" in context)) {
|
|
764
|
+
context.duplex = "half";
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
const { clearTimeout: clearTimeout2 } = getTimeout(opts, controller);
|
|
768
|
+
let response = await fetch2(context.url, context);
|
|
769
|
+
clearTimeout2();
|
|
770
|
+
const responseContext = {
|
|
771
|
+
response,
|
|
772
|
+
request: context
|
|
773
|
+
};
|
|
774
|
+
for (const onResponse of hooks.onResponse) {
|
|
775
|
+
if (onResponse) {
|
|
776
|
+
const r = await onResponse(__spreadProps(__spreadValues({}, responseContext), {
|
|
777
|
+
response: ((_c = options == null ? void 0 : options.hookOptions) == null ? void 0 : _c.cloneResponse) ? response.clone() : response
|
|
778
|
+
}));
|
|
779
|
+
if (r instanceof Response) {
|
|
780
|
+
response = r;
|
|
781
|
+
} else if (r instanceof Object) {
|
|
782
|
+
response = r.response;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
if (response.ok) {
|
|
787
|
+
const hasBody = context.method !== "HEAD";
|
|
788
|
+
if (!hasBody) {
|
|
789
|
+
return {
|
|
790
|
+
data: "",
|
|
791
|
+
error: null
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
const responseType = detectResponseType(response);
|
|
795
|
+
const successContext = {
|
|
796
|
+
data: "",
|
|
797
|
+
response,
|
|
798
|
+
request: context
|
|
799
|
+
};
|
|
800
|
+
if (responseType === "json" || responseType === "text") {
|
|
801
|
+
const text = await response.text();
|
|
802
|
+
const parser2 = (_d = context.jsonParser) != null ? _d : jsonParse;
|
|
803
|
+
const data = await parser2(text);
|
|
804
|
+
successContext.data = data;
|
|
805
|
+
} else {
|
|
806
|
+
successContext.data = await response[responseType]();
|
|
807
|
+
}
|
|
808
|
+
if (context == null ? void 0 : context.output) {
|
|
809
|
+
if (context.output && !context.disableValidation) {
|
|
810
|
+
successContext.data = await parseStandardSchema(
|
|
811
|
+
context.output,
|
|
812
|
+
successContext.data
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
for (const onSuccess of hooks.onSuccess) {
|
|
817
|
+
if (onSuccess) {
|
|
818
|
+
await onSuccess(__spreadProps(__spreadValues({}, successContext), {
|
|
819
|
+
response: ((_e = options == null ? void 0 : options.hookOptions) == null ? void 0 : _e.cloneResponse) ? response.clone() : response
|
|
820
|
+
}));
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
if (options == null ? void 0 : options.throw) {
|
|
824
|
+
return successContext.data;
|
|
825
|
+
}
|
|
826
|
+
return {
|
|
827
|
+
data: successContext.data,
|
|
828
|
+
error: null
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
const parser = (_f = options == null ? void 0 : options.jsonParser) != null ? _f : jsonParse;
|
|
832
|
+
const responseText = await response.text();
|
|
833
|
+
const isJSONResponse = isJSONParsable(responseText);
|
|
834
|
+
const errorObject = isJSONResponse ? await parser(responseText) : null;
|
|
835
|
+
const errorContext = {
|
|
836
|
+
response,
|
|
837
|
+
responseText,
|
|
838
|
+
request: context,
|
|
839
|
+
error: __spreadProps(__spreadValues({}, errorObject), {
|
|
840
|
+
status: response.status,
|
|
841
|
+
statusText: response.statusText
|
|
842
|
+
})
|
|
843
|
+
};
|
|
844
|
+
for (const onError of hooks.onError) {
|
|
845
|
+
if (onError) {
|
|
846
|
+
await onError(__spreadProps(__spreadValues({}, errorContext), {
|
|
847
|
+
response: ((_g = options == null ? void 0 : options.hookOptions) == null ? void 0 : _g.cloneResponse) ? response.clone() : response
|
|
848
|
+
}));
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
if (options == null ? void 0 : options.retry) {
|
|
852
|
+
const retryStrategy = createRetryStrategy(options.retry);
|
|
853
|
+
const _retryAttempt = (_h = options.retryAttempt) != null ? _h : 0;
|
|
854
|
+
if (await retryStrategy.shouldAttemptRetry(_retryAttempt, response)) {
|
|
855
|
+
for (const onRetry of hooks.onRetry) {
|
|
856
|
+
if (onRetry) {
|
|
857
|
+
await onRetry(responseContext);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
const delay = retryStrategy.getDelay(_retryAttempt);
|
|
861
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
862
|
+
return await betterFetch(url, __spreadProps(__spreadValues({}, options), {
|
|
863
|
+
retryAttempt: _retryAttempt + 1
|
|
864
|
+
}));
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
if (options == null ? void 0 : options.throw) {
|
|
868
|
+
throw new BetterFetchError(
|
|
869
|
+
response.status,
|
|
870
|
+
response.statusText,
|
|
871
|
+
isJSONResponse ? errorObject : responseText
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
return {
|
|
875
|
+
data: null,
|
|
876
|
+
error: __spreadProps(__spreadValues({}, errorObject), {
|
|
877
|
+
status: response.status,
|
|
878
|
+
statusText: response.statusText
|
|
879
|
+
})
|
|
880
|
+
};
|
|
881
|
+
};
|
|
882
|
+
const kBroadcastChannel = Symbol.for("better-auth:broadcast-channel");
|
|
883
|
+
const now$1 = () => Math.floor(Date.now() / 1e3);
|
|
884
|
+
var WindowBroadcastChannel = class {
|
|
885
|
+
listeners = /* @__PURE__ */ new Set();
|
|
886
|
+
name;
|
|
887
|
+
constructor(name = "better-auth.message") {
|
|
888
|
+
this.name = name;
|
|
889
|
+
}
|
|
890
|
+
subscribe(listener) {
|
|
891
|
+
this.listeners.add(listener);
|
|
892
|
+
return () => {
|
|
893
|
+
this.listeners.delete(listener);
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
post(message) {
|
|
897
|
+
if (typeof window === "undefined") return;
|
|
898
|
+
try {
|
|
899
|
+
localStorage.setItem(this.name, JSON.stringify({
|
|
900
|
+
...message,
|
|
901
|
+
timestamp: now$1()
|
|
902
|
+
}));
|
|
903
|
+
} catch {
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
setup() {
|
|
907
|
+
if (typeof window === "undefined" || typeof window.addEventListener === "undefined") return () => {
|
|
908
|
+
};
|
|
909
|
+
const handler = (event) => {
|
|
910
|
+
if (event.key !== this.name) return;
|
|
911
|
+
const message = JSON.parse(event.newValue ?? "{}");
|
|
912
|
+
if (message?.event !== "session" || !message?.data) return;
|
|
913
|
+
this.listeners.forEach((listener) => listener(message));
|
|
914
|
+
};
|
|
915
|
+
window.addEventListener("storage", handler);
|
|
916
|
+
return () => {
|
|
917
|
+
window.removeEventListener("storage", handler);
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
function getGlobalBroadcastChannel(name = "better-auth.message") {
|
|
922
|
+
if (!globalThis[kBroadcastChannel]) globalThis[kBroadcastChannel] = new WindowBroadcastChannel(name);
|
|
923
|
+
return globalThis[kBroadcastChannel];
|
|
924
|
+
}
|
|
925
|
+
const kFocusManager = Symbol.for("better-auth:focus-manager");
|
|
926
|
+
var WindowFocusManager = class {
|
|
927
|
+
listeners = /* @__PURE__ */ new Set();
|
|
928
|
+
subscribe(listener) {
|
|
929
|
+
this.listeners.add(listener);
|
|
930
|
+
return () => {
|
|
931
|
+
this.listeners.delete(listener);
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
setFocused(focused) {
|
|
935
|
+
this.listeners.forEach((listener) => listener(focused));
|
|
936
|
+
}
|
|
937
|
+
setup() {
|
|
938
|
+
if (typeof window === "undefined" || typeof document === "undefined" || typeof window.addEventListener === "undefined") return () => {
|
|
939
|
+
};
|
|
940
|
+
const onFocus = () => this.setFocused(true);
|
|
941
|
+
const visibilityHandler = () => {
|
|
942
|
+
if (document.visibilityState === "visible") this.setFocused(true);
|
|
943
|
+
};
|
|
944
|
+
window.addEventListener("focus", onFocus, false);
|
|
945
|
+
document.addEventListener("visibilitychange", visibilityHandler, false);
|
|
946
|
+
return () => {
|
|
947
|
+
window.removeEventListener("focus", onFocus, false);
|
|
948
|
+
document.removeEventListener("visibilitychange", visibilityHandler, false);
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
function getGlobalFocusManager() {
|
|
953
|
+
if (!globalThis[kFocusManager]) globalThis[kFocusManager] = new WindowFocusManager();
|
|
954
|
+
return globalThis[kFocusManager];
|
|
955
|
+
}
|
|
956
|
+
const kOnlineManager = Symbol.for("better-auth:online-manager");
|
|
957
|
+
var WindowOnlineManager = class {
|
|
958
|
+
listeners = /* @__PURE__ */ new Set();
|
|
959
|
+
isOnline = typeof navigator !== "undefined" ? navigator.onLine : true;
|
|
960
|
+
subscribe(listener) {
|
|
961
|
+
this.listeners.add(listener);
|
|
962
|
+
return () => {
|
|
963
|
+
this.listeners.delete(listener);
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
setOnline(online) {
|
|
967
|
+
this.isOnline = online;
|
|
968
|
+
this.listeners.forEach((listener) => listener(online));
|
|
969
|
+
}
|
|
970
|
+
setup() {
|
|
971
|
+
if (typeof window === "undefined" || typeof window.addEventListener === "undefined") return () => {
|
|
972
|
+
};
|
|
973
|
+
const onOnline = () => this.setOnline(true);
|
|
974
|
+
const onOffline = () => this.setOnline(false);
|
|
975
|
+
window.addEventListener("online", onOnline, false);
|
|
976
|
+
window.addEventListener("offline", onOffline, false);
|
|
977
|
+
return () => {
|
|
978
|
+
window.removeEventListener("online", onOnline, false);
|
|
979
|
+
window.removeEventListener("offline", onOffline, false);
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
function getGlobalOnlineManager() {
|
|
984
|
+
if (!globalThis[kOnlineManager]) globalThis[kOnlineManager] = new WindowOnlineManager();
|
|
985
|
+
return globalThis[kOnlineManager];
|
|
986
|
+
}
|
|
987
|
+
const isServer = () => typeof window === "undefined";
|
|
988
|
+
const useAuthQuery = (initializedAtom, path, $fetch, options) => {
|
|
989
|
+
const value = /* @__PURE__ */ atom({
|
|
990
|
+
data: null,
|
|
991
|
+
error: null,
|
|
992
|
+
isPending: true,
|
|
993
|
+
isRefetching: false,
|
|
994
|
+
refetch: (queryParams) => fn(queryParams)
|
|
995
|
+
});
|
|
996
|
+
const fn = async (queryParams) => {
|
|
997
|
+
return new Promise((resolve) => {
|
|
998
|
+
const opts = typeof options === "function" ? options({
|
|
999
|
+
data: value.get().data,
|
|
1000
|
+
error: value.get().error,
|
|
1001
|
+
isPending: value.get().isPending
|
|
1002
|
+
}) : options;
|
|
1003
|
+
$fetch(path, {
|
|
1004
|
+
...opts,
|
|
1005
|
+
query: {
|
|
1006
|
+
...opts?.query,
|
|
1007
|
+
...queryParams?.query
|
|
1008
|
+
},
|
|
1009
|
+
async onSuccess(context) {
|
|
1010
|
+
value.set({
|
|
1011
|
+
data: context.data,
|
|
1012
|
+
error: null,
|
|
1013
|
+
isPending: false,
|
|
1014
|
+
isRefetching: false,
|
|
1015
|
+
refetch: value.value.refetch
|
|
1016
|
+
});
|
|
1017
|
+
await opts?.onSuccess?.(context);
|
|
1018
|
+
},
|
|
1019
|
+
async onError(context) {
|
|
1020
|
+
const { request } = context;
|
|
1021
|
+
const retryAttempts = typeof request.retry === "number" ? request.retry : request.retry?.attempts;
|
|
1022
|
+
const retryAttempt = request.retryAttempt || 0;
|
|
1023
|
+
if (retryAttempts && retryAttempt < retryAttempts) return;
|
|
1024
|
+
value.set({
|
|
1025
|
+
error: context.error,
|
|
1026
|
+
data: null,
|
|
1027
|
+
isPending: false,
|
|
1028
|
+
isRefetching: false,
|
|
1029
|
+
refetch: value.value.refetch
|
|
1030
|
+
});
|
|
1031
|
+
await opts?.onError?.(context);
|
|
1032
|
+
},
|
|
1033
|
+
async onRequest(context) {
|
|
1034
|
+
const currentValue = value.get();
|
|
1035
|
+
value.set({
|
|
1036
|
+
isPending: currentValue.data === null,
|
|
1037
|
+
data: currentValue.data,
|
|
1038
|
+
error: null,
|
|
1039
|
+
isRefetching: true,
|
|
1040
|
+
refetch: value.value.refetch
|
|
1041
|
+
});
|
|
1042
|
+
await opts?.onRequest?.(context);
|
|
1043
|
+
}
|
|
1044
|
+
}).catch((error) => {
|
|
1045
|
+
value.set({
|
|
1046
|
+
error,
|
|
1047
|
+
data: null,
|
|
1048
|
+
isPending: false,
|
|
1049
|
+
isRefetching: false,
|
|
1050
|
+
refetch: value.value.refetch
|
|
1051
|
+
});
|
|
1052
|
+
}).finally(() => {
|
|
1053
|
+
resolve(void 0);
|
|
1054
|
+
});
|
|
1055
|
+
});
|
|
1056
|
+
};
|
|
1057
|
+
initializedAtom = Array.isArray(initializedAtom) ? initializedAtom : [initializedAtom];
|
|
1058
|
+
let isMounted = false;
|
|
1059
|
+
for (const initAtom of initializedAtom) initAtom.subscribe(async () => {
|
|
1060
|
+
if (isServer()) return;
|
|
1061
|
+
if (isMounted) await fn();
|
|
1062
|
+
else onMount(value, () => {
|
|
1063
|
+
const timeoutId = setTimeout(async () => {
|
|
1064
|
+
if (!isMounted) {
|
|
1065
|
+
await fn();
|
|
1066
|
+
isMounted = true;
|
|
1067
|
+
}
|
|
1068
|
+
}, 0);
|
|
1069
|
+
return () => {
|
|
1070
|
+
value.off();
|
|
1071
|
+
initAtom.off();
|
|
1072
|
+
clearTimeout(timeoutId);
|
|
1073
|
+
};
|
|
1074
|
+
});
|
|
1075
|
+
});
|
|
1076
|
+
return value;
|
|
1077
|
+
};
|
|
1078
|
+
const now = () => Math.floor(Date.now() / 1e3);
|
|
1079
|
+
const FOCUS_REFETCH_RATE_LIMIT_SECONDS = 5;
|
|
1080
|
+
function createSessionRefreshManager(opts) {
|
|
1081
|
+
const { sessionAtom, sessionSignal, $fetch, options = {} } = opts;
|
|
1082
|
+
const refetchInterval = options.sessionOptions?.refetchInterval ?? 0;
|
|
1083
|
+
const refetchOnWindowFocus = options.sessionOptions?.refetchOnWindowFocus ?? true;
|
|
1084
|
+
const refetchWhenOffline = options.sessionOptions?.refetchWhenOffline ?? false;
|
|
1085
|
+
const state = {
|
|
1086
|
+
lastSync: 0,
|
|
1087
|
+
lastSessionRequest: 0,
|
|
1088
|
+
cachedSession: void 0
|
|
1089
|
+
};
|
|
1090
|
+
const shouldRefetch = () => {
|
|
1091
|
+
return refetchWhenOffline || getGlobalOnlineManager().isOnline;
|
|
1092
|
+
};
|
|
1093
|
+
const triggerRefetch = (event) => {
|
|
1094
|
+
if (!shouldRefetch()) return;
|
|
1095
|
+
if (event?.event === "storage") {
|
|
1096
|
+
state.lastSync = now();
|
|
1097
|
+
sessionSignal.set(!sessionSignal.get());
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
const currentSession = sessionAtom.get();
|
|
1101
|
+
if (event?.event === "poll") {
|
|
1102
|
+
state.lastSessionRequest = now();
|
|
1103
|
+
$fetch("/get-session").then((res) => {
|
|
1104
|
+
sessionAtom.set({
|
|
1105
|
+
...currentSession,
|
|
1106
|
+
data: res.data,
|
|
1107
|
+
error: res.error || null
|
|
1108
|
+
});
|
|
1109
|
+
state.lastSync = now();
|
|
1110
|
+
sessionSignal.set(!sessionSignal.get());
|
|
1111
|
+
}).catch(() => {
|
|
1112
|
+
});
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
if (event?.event === "visibilitychange") {
|
|
1116
|
+
if (now() - state.lastSessionRequest < FOCUS_REFETCH_RATE_LIMIT_SECONDS && currentSession?.data !== null && currentSession?.data !== void 0) return;
|
|
1117
|
+
}
|
|
1118
|
+
if (currentSession?.data === null || currentSession?.data === void 0 || event?.event === "visibilitychange") {
|
|
1119
|
+
if (event?.event === "visibilitychange") state.lastSessionRequest = now();
|
|
1120
|
+
state.lastSync = now();
|
|
1121
|
+
sessionSignal.set(!sessionSignal.get());
|
|
1122
|
+
}
|
|
1123
|
+
};
|
|
1124
|
+
const broadcastSessionUpdate = (trigger) => {
|
|
1125
|
+
getGlobalBroadcastChannel().post({
|
|
1126
|
+
event: "session",
|
|
1127
|
+
data: { trigger },
|
|
1128
|
+
clientId: Math.random().toString(36).substring(7)
|
|
1129
|
+
});
|
|
1130
|
+
};
|
|
1131
|
+
const setupPolling = () => {
|
|
1132
|
+
if (refetchInterval && refetchInterval > 0) state.pollInterval = setInterval(() => {
|
|
1133
|
+
if (sessionAtom.get()?.data) triggerRefetch({ event: "poll" });
|
|
1134
|
+
}, refetchInterval * 1e3);
|
|
1135
|
+
};
|
|
1136
|
+
const setupBroadcast = () => {
|
|
1137
|
+
state.unsubscribeBroadcast = getGlobalBroadcastChannel().subscribe(() => {
|
|
1138
|
+
triggerRefetch({ event: "storage" });
|
|
1139
|
+
});
|
|
1140
|
+
};
|
|
1141
|
+
const setupFocusRefetch = () => {
|
|
1142
|
+
if (!refetchOnWindowFocus) return;
|
|
1143
|
+
state.unsubscribeFocus = getGlobalFocusManager().subscribe(() => {
|
|
1144
|
+
triggerRefetch({ event: "visibilitychange" });
|
|
1145
|
+
});
|
|
1146
|
+
};
|
|
1147
|
+
const setupOnlineRefetch = () => {
|
|
1148
|
+
state.unsubscribeOnline = getGlobalOnlineManager().subscribe((online) => {
|
|
1149
|
+
if (online) triggerRefetch({ event: "visibilitychange" });
|
|
1150
|
+
});
|
|
1151
|
+
};
|
|
1152
|
+
const init = () => {
|
|
1153
|
+
setupPolling();
|
|
1154
|
+
setupBroadcast();
|
|
1155
|
+
setupFocusRefetch();
|
|
1156
|
+
setupOnlineRefetch();
|
|
1157
|
+
getGlobalBroadcastChannel().setup();
|
|
1158
|
+
getGlobalFocusManager().setup();
|
|
1159
|
+
getGlobalOnlineManager().setup();
|
|
1160
|
+
};
|
|
1161
|
+
const cleanup = () => {
|
|
1162
|
+
if (state.pollInterval) {
|
|
1163
|
+
clearInterval(state.pollInterval);
|
|
1164
|
+
state.pollInterval = void 0;
|
|
1165
|
+
}
|
|
1166
|
+
if (state.unsubscribeBroadcast) {
|
|
1167
|
+
state.unsubscribeBroadcast();
|
|
1168
|
+
state.unsubscribeBroadcast = void 0;
|
|
1169
|
+
}
|
|
1170
|
+
if (state.unsubscribeFocus) {
|
|
1171
|
+
state.unsubscribeFocus();
|
|
1172
|
+
state.unsubscribeFocus = void 0;
|
|
1173
|
+
}
|
|
1174
|
+
if (state.unsubscribeOnline) {
|
|
1175
|
+
state.unsubscribeOnline();
|
|
1176
|
+
state.unsubscribeOnline = void 0;
|
|
1177
|
+
}
|
|
1178
|
+
state.lastSync = 0;
|
|
1179
|
+
state.lastSessionRequest = 0;
|
|
1180
|
+
state.cachedSession = void 0;
|
|
1181
|
+
};
|
|
1182
|
+
return {
|
|
1183
|
+
init,
|
|
1184
|
+
cleanup,
|
|
1185
|
+
triggerRefetch,
|
|
1186
|
+
broadcastSessionUpdate
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
const redirectPlugin = {
|
|
1190
|
+
id: "redirect",
|
|
1191
|
+
name: "Redirect",
|
|
1192
|
+
hooks: { onSuccess(context) {
|
|
1193
|
+
if (context.data?.url && context.data?.redirect) {
|
|
1194
|
+
if (typeof window !== "undefined" && window.location) {
|
|
1195
|
+
if (window.location) try {
|
|
1196
|
+
window.location.href = context.data.url;
|
|
1197
|
+
} catch {
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
} }
|
|
1202
|
+
};
|
|
1203
|
+
const userAgentPlugin = {
|
|
1204
|
+
id: "user-agent",
|
|
1205
|
+
name: "UserAgent",
|
|
1206
|
+
hooks: { onRequest(context) {
|
|
1207
|
+
context.headers.append("user-agent", "better-auth");
|
|
1208
|
+
} }
|
|
1209
|
+
};
|
|
1210
|
+
function getSessionAtom($fetch, options) {
|
|
1211
|
+
const $signal = /* @__PURE__ */ atom(false);
|
|
1212
|
+
const session = useAuthQuery($signal, "/get-session", $fetch, { method: "GET" });
|
|
1213
|
+
onMount(session, () => {
|
|
1214
|
+
const refreshManager = createSessionRefreshManager({
|
|
1215
|
+
sessionAtom: session,
|
|
1216
|
+
sessionSignal: $signal,
|
|
1217
|
+
$fetch,
|
|
1218
|
+
options
|
|
1219
|
+
});
|
|
1220
|
+
refreshManager.init();
|
|
1221
|
+
return () => {
|
|
1222
|
+
refreshManager.cleanup();
|
|
1223
|
+
};
|
|
1224
|
+
});
|
|
1225
|
+
return {
|
|
1226
|
+
session,
|
|
1227
|
+
$sessionSignal: $signal
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
const getClientConfig = (options, loadEnv) => {
|
|
1231
|
+
const isCredentialsSupported = "credentials" in Request.prototype;
|
|
1232
|
+
const baseURL = getBaseURL(options?.baseURL, options?.basePath) ?? "/api/auth";
|
|
1233
|
+
const pluginsFetchPlugins = options?.plugins?.flatMap((plugin) => plugin.fetchPlugins).filter((pl) => pl !== void 0) || [];
|
|
1234
|
+
const lifeCyclePlugin = {
|
|
1235
|
+
id: "lifecycle-hooks",
|
|
1236
|
+
name: "lifecycle-hooks",
|
|
1237
|
+
hooks: {
|
|
1238
|
+
onSuccess: options?.fetchOptions?.onSuccess,
|
|
1239
|
+
onError: options?.fetchOptions?.onError,
|
|
1240
|
+
onRequest: options?.fetchOptions?.onRequest,
|
|
1241
|
+
onResponse: options?.fetchOptions?.onResponse
|
|
1242
|
+
}
|
|
1243
|
+
};
|
|
1244
|
+
const { onSuccess, onError, onRequest, onResponse, ...restOfFetchOptions } = options?.fetchOptions || {};
|
|
1245
|
+
const $fetch = createFetch({
|
|
1246
|
+
baseURL,
|
|
1247
|
+
...isCredentialsSupported ? { credentials: "include" } : {},
|
|
1248
|
+
method: "GET",
|
|
1249
|
+
jsonParser(text) {
|
|
1250
|
+
if (!text) return null;
|
|
1251
|
+
return parseJSON(text, { strict: false });
|
|
1252
|
+
},
|
|
1253
|
+
customFetchImpl: fetch,
|
|
1254
|
+
...restOfFetchOptions,
|
|
1255
|
+
plugins: [
|
|
1256
|
+
lifeCyclePlugin,
|
|
1257
|
+
...restOfFetchOptions.plugins || [],
|
|
1258
|
+
...options?.disableDefaultFetchPlugins ? [] : [userAgentPlugin, redirectPlugin],
|
|
1259
|
+
...pluginsFetchPlugins
|
|
1260
|
+
]
|
|
1261
|
+
});
|
|
1262
|
+
const { $sessionSignal, session } = getSessionAtom($fetch, options);
|
|
1263
|
+
const plugins = options?.plugins || [];
|
|
1264
|
+
let pluginsActions = {};
|
|
1265
|
+
let pluginsAtoms = {
|
|
1266
|
+
$sessionSignal,
|
|
1267
|
+
session
|
|
1268
|
+
};
|
|
1269
|
+
let pluginPathMethods = {
|
|
1270
|
+
"/sign-out": "POST",
|
|
1271
|
+
"/revoke-sessions": "POST",
|
|
1272
|
+
"/revoke-other-sessions": "POST",
|
|
1273
|
+
"/delete-user": "POST"
|
|
1274
|
+
};
|
|
1275
|
+
const atomListeners = [{
|
|
1276
|
+
signal: "$sessionSignal",
|
|
1277
|
+
matcher(path) {
|
|
1278
|
+
return path === "/sign-out" || path === "/update-user" || path === "/sign-up/email" || path === "/sign-in/email" || path === "/delete-user" || path === "/verify-email" || path === "/revoke-sessions" || path === "/revoke-session" || path === "/change-email";
|
|
1279
|
+
}
|
|
1280
|
+
}];
|
|
1281
|
+
for (const plugin of plugins) {
|
|
1282
|
+
if (plugin.getAtoms) Object.assign(pluginsAtoms, plugin.getAtoms?.($fetch));
|
|
1283
|
+
if (plugin.pathMethods) Object.assign(pluginPathMethods, plugin.pathMethods);
|
|
1284
|
+
if (plugin.atomListeners) atomListeners.push(...plugin.atomListeners);
|
|
1285
|
+
}
|
|
1286
|
+
const $store = {
|
|
1287
|
+
notify: (signal) => {
|
|
1288
|
+
pluginsAtoms[signal].set(!pluginsAtoms[signal].get());
|
|
1289
|
+
},
|
|
1290
|
+
listen: (signal, listener) => {
|
|
1291
|
+
pluginsAtoms[signal].subscribe(listener);
|
|
1292
|
+
},
|
|
1293
|
+
atoms: pluginsAtoms
|
|
1294
|
+
};
|
|
1295
|
+
for (const plugin of plugins) if (plugin.getActions) Object.assign(pluginsActions, plugin.getActions?.($fetch, $store, options));
|
|
1296
|
+
return {
|
|
1297
|
+
get baseURL() {
|
|
1298
|
+
return baseURL;
|
|
1299
|
+
},
|
|
1300
|
+
pluginsActions,
|
|
1301
|
+
pluginsAtoms,
|
|
1302
|
+
pluginPathMethods,
|
|
1303
|
+
atomListeners,
|
|
1304
|
+
$fetch,
|
|
1305
|
+
$store
|
|
1306
|
+
};
|
|
1307
|
+
};
|
|
1308
|
+
function isAtom(value) {
|
|
1309
|
+
return typeof value === "object" && value !== null && "get" in value && typeof value.get === "function" && "lc" in value && typeof value.lc === "number";
|
|
1310
|
+
}
|
|
1311
|
+
function getMethod(path, knownPathMethods, args) {
|
|
1312
|
+
const method = knownPathMethods[path];
|
|
1313
|
+
const { fetchOptions, query, ...body } = args || {};
|
|
1314
|
+
if (method) return method;
|
|
1315
|
+
if (fetchOptions?.method) return fetchOptions.method;
|
|
1316
|
+
if (body && Object.keys(body).length > 0) return "POST";
|
|
1317
|
+
return "GET";
|
|
1318
|
+
}
|
|
1319
|
+
function createDynamicPathProxy(routes, client, knownPathMethods, atoms, atomListeners) {
|
|
1320
|
+
function createProxy(path = []) {
|
|
1321
|
+
return new Proxy(function() {
|
|
1322
|
+
}, {
|
|
1323
|
+
get(_, prop) {
|
|
1324
|
+
if (typeof prop !== "string") return;
|
|
1325
|
+
if (prop === "then" || prop === "catch" || prop === "finally") return;
|
|
1326
|
+
const fullPath = [...path, prop];
|
|
1327
|
+
let current = routes;
|
|
1328
|
+
for (const segment of fullPath) if (current && typeof current === "object" && segment in current) current = current[segment];
|
|
1329
|
+
else {
|
|
1330
|
+
current = void 0;
|
|
1331
|
+
break;
|
|
1332
|
+
}
|
|
1333
|
+
if (typeof current === "function") return current;
|
|
1334
|
+
if (isAtom(current)) return current;
|
|
1335
|
+
return createProxy(fullPath);
|
|
1336
|
+
},
|
|
1337
|
+
apply: async (_, __, args) => {
|
|
1338
|
+
const routePath = "/" + path.map((segment) => segment.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)).join("/");
|
|
1339
|
+
const arg = args[0] || {};
|
|
1340
|
+
const fetchOptions = args[1] || {};
|
|
1341
|
+
const { query, fetchOptions: argFetchOptions, ...body } = arg;
|
|
1342
|
+
const options = {
|
|
1343
|
+
...fetchOptions,
|
|
1344
|
+
...argFetchOptions
|
|
1345
|
+
};
|
|
1346
|
+
const method = getMethod(routePath, knownPathMethods, arg);
|
|
1347
|
+
return await client(routePath, {
|
|
1348
|
+
...options,
|
|
1349
|
+
body: method === "GET" ? void 0 : {
|
|
1350
|
+
...body,
|
|
1351
|
+
...options?.body || {}
|
|
1352
|
+
},
|
|
1353
|
+
query: query || options?.query,
|
|
1354
|
+
method,
|
|
1355
|
+
async onSuccess(context) {
|
|
1356
|
+
await options?.onSuccess?.(context);
|
|
1357
|
+
if (!atomListeners || options.disableSignal) return;
|
|
1358
|
+
const matches = atomListeners.filter((s) => s.matcher(routePath));
|
|
1359
|
+
if (!matches.length) return;
|
|
1360
|
+
for (const match of matches) {
|
|
1361
|
+
const signal = atoms[match.signal];
|
|
1362
|
+
if (!signal) return;
|
|
1363
|
+
const val = signal.get();
|
|
1364
|
+
setTimeout(() => {
|
|
1365
|
+
signal.set(!val);
|
|
1366
|
+
}, 10);
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
return createProxy();
|
|
1374
|
+
}
|
|
1375
|
+
function useStore(store, options = {}) {
|
|
1376
|
+
let snapshotRef = useRef(store.get());
|
|
1377
|
+
const { keys, deps = [store, keys] } = options;
|
|
1378
|
+
let subscribe = useCallback((onChange) => {
|
|
1379
|
+
const emitChange = (value) => {
|
|
1380
|
+
if (snapshotRef.current === value) return;
|
|
1381
|
+
snapshotRef.current = value;
|
|
1382
|
+
onChange();
|
|
1383
|
+
};
|
|
1384
|
+
emitChange(store.value);
|
|
1385
|
+
if (keys?.length) return listenKeys(store, keys, emitChange);
|
|
1386
|
+
return store.listen(emitChange);
|
|
1387
|
+
}, deps);
|
|
1388
|
+
let get = () => snapshotRef.current;
|
|
1389
|
+
return useSyncExternalStore(subscribe, get, get);
|
|
1390
|
+
}
|
|
1391
|
+
function getAtomKey(str) {
|
|
1392
|
+
return `use${capitalizeFirstLetter(str)}`;
|
|
1393
|
+
}
|
|
1394
|
+
function capitalizeFirstLetter(str) {
|
|
1395
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
1396
|
+
}
|
|
1397
|
+
function createAuthClient(options) {
|
|
1398
|
+
const { pluginPathMethods, pluginsActions, pluginsAtoms, $fetch, $store, atomListeners } = getClientConfig(options);
|
|
1399
|
+
let resolvedHooks = {};
|
|
1400
|
+
for (const [key, value] of Object.entries(pluginsAtoms)) resolvedHooks[getAtomKey(key)] = () => useStore(value);
|
|
1401
|
+
return createDynamicPathProxy({
|
|
1402
|
+
...pluginsActions,
|
|
1403
|
+
...resolvedHooks,
|
|
1404
|
+
$fetch,
|
|
1405
|
+
$store
|
|
1406
|
+
}, $fetch, pluginPathMethods, pluginsAtoms, atomListeners);
|
|
1407
|
+
}
|
|
1408
|
+
const authClient = createAuthClient({
|
|
1409
|
+
baseURL: clientEnv.VITE_API_URL
|
|
1410
|
+
});
|
|
1411
|
+
export {
|
|
1412
|
+
WISTRON_PRIMARY_COLOR as W,
|
|
1413
|
+
authClient as a,
|
|
1414
|
+
WISTRON_SECONDARY_COLOR as b,
|
|
1415
|
+
useGetTheme as u
|
|
1416
|
+
};
|
|
1417
|
+
//# sourceMappingURL=auth-client-B6cIXYDV.js.map
|