@mono-lit/utility 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +54 -0
- package/README.md +157 -3
- package/dist/app-roots-CvJnmXcs.js +128 -0
- package/dist/app-roots-DK41xMWj.d.ts +90 -0
- package/dist/config-node-UzJrXg0Z.js +76 -0
- package/dist/config-node.d.ts +923 -0
- package/dist/config-node.js +7 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +3 -0
- package/dist/create-config-B8IhX4ht.d.ts +735 -0
- package/dist/create-config-DdL3Fh6T.js +357 -0
- package/dist/fetching.d.ts +244 -0
- package/dist/fetching.js +1015 -0
- package/dist/generate-CJ7MtcDd.js +291 -0
- package/dist/index-BAy6tbOG.d.ts +371 -0
- package/dist/index-DYlzhHq7.d.ts +514 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +941 -0
- package/dist/merge-file-CLc3wxY4.d.ts +99 -0
- package/dist/merge-file-CtyKTu5L.js +133 -0
- package/dist/mock-db-4tTWrF-P.js +1282 -0
- package/dist/mock-db.d.ts +2 -0
- package/dist/mock-db.js +4 -0
- package/dist/mono-alias-DNDm-jB_.js +508 -0
- package/dist/mono-clone.mjs +958 -0
- package/dist/mono-db.d.ts +1 -0
- package/dist/mono-db.js +158 -0
- package/dist/mono-env.mjs +231 -0
- package/dist/mono-git.mjs +798 -0
- package/dist/mono-link-YlOQ2PVS.js +488 -0
- package/dist/mono-prepare.d.ts +1 -0
- package/dist/mono-prepare.js +28 -0
- package/dist/mono-skills.d.ts +1 -0
- package/dist/mono-skills.js +837 -0
- package/dist/mono-tsconfig-D2qRL9_R.js +204 -0
- package/dist/mono.mjs +34 -0
- package/dist/nuxt-layers-DP1m7f1-.js +176 -0
- package/dist/nuxt-layers.d.ts +36 -0
- package/dist/nuxt-layers.js +3 -0
- package/dist/nuxt-runtime.d.ts +17 -0
- package/dist/nuxt-runtime.js +102 -0
- package/dist/nuxt.d.ts +390 -0
- package/dist/nuxt.js +737 -0
- package/dist/runtime-vs7U9Ed4.js +6054 -0
- package/dist/runtime.d.ts +2518 -0
- package/dist/runtime.js +4 -0
- package/dist/session-Bcig-rPi.js +1721 -0
- package/dist/types-B6FdB7LJ.d.ts +57 -0
- package/dist/universal-BR0O7Qwk.d.ts +59 -0
- package/dist/universal-DIVEzCvN.js +600 -0
- package/dist/virtual-mono-apps.d.ts +16 -0
- package/dist/vite.d.ts +943 -0
- package/dist/vite.js +1132 -0
- package/package.json +141 -3
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import { jwtDecode } from "jwt-decode";
|
|
2
|
+
|
|
3
|
+
//#region src/token/cookie-core.ts
|
|
4
|
+
/** Regex matching a split chunk cookie: `<encName>_split_<idx>=`. */
|
|
5
|
+
function chunkPattern(name) {
|
|
6
|
+
return new RegExp(`^${encodeURIComponent(name)}_split_(\\d+)=`);
|
|
7
|
+
}
|
|
8
|
+
/** From raw "encName=encValue" entries, return this name's chunk values, index-ordered + decoded. */
|
|
9
|
+
function parseChunks(rawEntries, name) {
|
|
10
|
+
const pattern = chunkPattern(name);
|
|
11
|
+
const hits = [];
|
|
12
|
+
for (const c of rawEntries) {
|
|
13
|
+
const m = c.match(pattern);
|
|
14
|
+
if (m) hits.push({
|
|
15
|
+
idx: Number(m[1]),
|
|
16
|
+
raw: c
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
hits.sort((a, b) => a.idx - b.idx);
|
|
20
|
+
let combined = "";
|
|
21
|
+
for (const { raw } of hits) {
|
|
22
|
+
const eqPos = raw.indexOf("=");
|
|
23
|
+
if (eqPos >= 0) combined += decodeURIComponent(raw.substring(eqPos + 1));
|
|
24
|
+
}
|
|
25
|
+
return combined;
|
|
26
|
+
}
|
|
27
|
+
/** Indexes of this name's existing chunks (for cleanup before a rewrite). */
|
|
28
|
+
function chunkIndexes(rawEntries, name) {
|
|
29
|
+
const pattern = chunkPattern(name);
|
|
30
|
+
const idx = [];
|
|
31
|
+
for (const c of rawEntries) {
|
|
32
|
+
const m = c.match(pattern);
|
|
33
|
+
if (m) idx.push(Number(m[1]));
|
|
34
|
+
}
|
|
35
|
+
return idx.sort((a, b) => a - b);
|
|
36
|
+
}
|
|
37
|
+
/** Slice a value into ~`every`-char parts. */
|
|
38
|
+
function splitValue(value, every) {
|
|
39
|
+
const parts = [];
|
|
40
|
+
let i = 0;
|
|
41
|
+
while (i * every < value.length) {
|
|
42
|
+
parts.push(value.slice(i * every, (i + 1) * every));
|
|
43
|
+
i++;
|
|
44
|
+
}
|
|
45
|
+
return parts;
|
|
46
|
+
}
|
|
47
|
+
/** Compute an expiry Date from `days`/`milis` (returns undefined when neither given). */
|
|
48
|
+
function computeExpiry(days, milis) {
|
|
49
|
+
if (!days && !milis) return void 0;
|
|
50
|
+
const expires = /* @__PURE__ */ new Date();
|
|
51
|
+
if (milis) expires.setTime(expires.getTime() + milis);
|
|
52
|
+
if (days) expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1e3);
|
|
53
|
+
return expires;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Build a `{ get, add, remove }` cookie util over any synchronous `CookieStore`.
|
|
57
|
+
* Carries the exact original semantics: split handling, days/milis expiry, and a
|
|
58
|
+
* write-verify (re-read after write) before reporting success.
|
|
59
|
+
*/
|
|
60
|
+
function createCookie(store) {
|
|
61
|
+
const get = (name, split = false) => {
|
|
62
|
+
if (split) {
|
|
63
|
+
const combined = parseChunks(store.readAll(), name);
|
|
64
|
+
if (combined) return combined;
|
|
65
|
+
}
|
|
66
|
+
return store.read(name);
|
|
67
|
+
};
|
|
68
|
+
const add = ({ name, value, days, milis, split = false, splitEvery = 2e3 }) => {
|
|
69
|
+
if (!days && !milis) return null;
|
|
70
|
+
try {
|
|
71
|
+
const expires = computeExpiry(days, milis);
|
|
72
|
+
if (split) {
|
|
73
|
+
for (const idx of chunkIndexes(store.readAll(), name)) store.delete(`${name}_split_${idx}`);
|
|
74
|
+
store.delete(name);
|
|
75
|
+
const parts = splitValue(value, splitEvery);
|
|
76
|
+
let ok = true;
|
|
77
|
+
parts.forEach((part, i) => {
|
|
78
|
+
store.write(`${name}_split_${i}`, part, expires);
|
|
79
|
+
ok &&= store.read(`${name}_split_${i}`) === part;
|
|
80
|
+
});
|
|
81
|
+
if (ok) return value || get(name, true);
|
|
82
|
+
} else {
|
|
83
|
+
store.write(name, value, expires);
|
|
84
|
+
if (store.read(name) === value) return value || get(name);
|
|
85
|
+
}
|
|
86
|
+
} catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const remove = (name, split = false) => {
|
|
91
|
+
if (split) {
|
|
92
|
+
for (const idx of chunkIndexes(store.readAll(), name)) store.delete(`${name}_split_${idx}`);
|
|
93
|
+
store.delete(name);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (get(name) !== null) {
|
|
97
|
+
store.delete(name);
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
add,
|
|
104
|
+
get,
|
|
105
|
+
remove
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/token/token-core.ts
|
|
111
|
+
/** Pure: read `a.b.c` off an object. */
|
|
112
|
+
function getByPath(obj, path) {
|
|
113
|
+
if (path) return path?.split(".").reduce((acc, key) => acc?.[key], obj);
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Build the token util ({ get, add, decode, validate, replace, fetch, wrap }) over
|
|
118
|
+
* injected cookie/jwt/fetch dependencies. Behavior matches the original `useMyToken`.
|
|
119
|
+
*/
|
|
120
|
+
function createToken(deps, options) {
|
|
121
|
+
const { cookie, isJwt, decode, fetch } = deps;
|
|
122
|
+
const get = (name, split) => {
|
|
123
|
+
const token = cookie.get(String(name || options?.name), Boolean(split) || Boolean(options?.splitCookie));
|
|
124
|
+
if (token) return token;
|
|
125
|
+
return null;
|
|
126
|
+
};
|
|
127
|
+
const add = ({ name, value, days, milis, splitCookie }) => {
|
|
128
|
+
return cookie.add({
|
|
129
|
+
name: String(name || options?.name),
|
|
130
|
+
value: String(value || options?.value),
|
|
131
|
+
days: Number(days || options?.days),
|
|
132
|
+
milis: Number(milis || options?.milis),
|
|
133
|
+
split: Boolean(splitCookie || options?.splitCookie)
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
const decodeToken = (name, split) => {
|
|
137
|
+
const token = cookie.get(String(name || options?.name), split || Boolean(options?.splitCookie));
|
|
138
|
+
if (token && isJwt(token)) return decode(token);
|
|
139
|
+
return null;
|
|
140
|
+
};
|
|
141
|
+
const validate = (name, split) => {
|
|
142
|
+
const getToken = get(String(name || options?.name), Boolean(split) || Boolean(options?.splitCookie));
|
|
143
|
+
if (getToken && !["", "undefined"].includes(String(getToken))) {
|
|
144
|
+
const decoded = decodeToken(String(name || options?.name), Boolean(split) || Boolean(options?.splitCookie));
|
|
145
|
+
if (decoded) {
|
|
146
|
+
const currentTime = Math.floor(Date.now() / 1e3);
|
|
147
|
+
if (decoded.exp && Number(decoded.exp) > currentTime) return true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
};
|
|
152
|
+
const replace = ({ name, value, days, milis, splitCookie }) => {
|
|
153
|
+
const addCookie = cookie.add({
|
|
154
|
+
name: String(name || options?.name),
|
|
155
|
+
value: String(value || options?.value),
|
|
156
|
+
days: Number(days || options?.days),
|
|
157
|
+
milis: Number(milis || options?.milis),
|
|
158
|
+
split: Boolean(splitCookie) || Boolean(options?.splitCookie)
|
|
159
|
+
});
|
|
160
|
+
return Boolean(addCookie);
|
|
161
|
+
};
|
|
162
|
+
const fetchToken = async ({ fetchParams, name, path, splitCookie } = {}) => {
|
|
163
|
+
if (!validate(String(name || options?.name))) {
|
|
164
|
+
const { all } = await fetch(String(fetchParams?.url || options?.fetchParams?.url), {
|
|
165
|
+
...fetchParams?.options,
|
|
166
|
+
...options?.fetchParams?.options
|
|
167
|
+
});
|
|
168
|
+
if (all) return {
|
|
169
|
+
response: all,
|
|
170
|
+
cookie: add({
|
|
171
|
+
name: String(name || options?.name) || getByPath(all, String(path?.name || options?.path?.name)),
|
|
172
|
+
value: getByPath(all, String(path?.value || options?.path?.value)),
|
|
173
|
+
milis: getByPath(all, String(path?.milis || options?.path?.milis)),
|
|
174
|
+
days: getByPath(all, String(path?.days || options?.path?.days)),
|
|
175
|
+
splitCookie: Boolean(splitCookie) || Boolean(options?.splitCookie)
|
|
176
|
+
})
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
response: null,
|
|
181
|
+
cookie: null
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
const wrap = async ({ name, fetchParams, path, splitCookie } = {}, callback) => {
|
|
185
|
+
await fetchToken({
|
|
186
|
+
fetchParams: {
|
|
187
|
+
...fetchParams,
|
|
188
|
+
...options?.fetchParams
|
|
189
|
+
},
|
|
190
|
+
name: String(name || options?.name),
|
|
191
|
+
path: {
|
|
192
|
+
...path,
|
|
193
|
+
...options?.path
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
const getStoredToken = cookie.get(String(name || options?.name), Boolean(splitCookie) || Boolean(options?.splitCookie));
|
|
197
|
+
if (getStoredToken && callback) return callback(getStoredToken);
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
decode: decodeToken,
|
|
201
|
+
add,
|
|
202
|
+
get,
|
|
203
|
+
replace,
|
|
204
|
+
fetch: fetchToken,
|
|
205
|
+
validate,
|
|
206
|
+
wrap
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/token/jwt.ts
|
|
212
|
+
/** Pure: is this string shaped like a JWT? */
|
|
213
|
+
const isJwt = (jwt) => /^([A-Za-z0-9-_]+)\.([A-Za-z0-9-_]+)\.([A-Za-z0-9-_.+/=]+)$/.test(jwt);
|
|
214
|
+
/** Pure: decode a JWT string into its payload, or null when not a JWT. */
|
|
215
|
+
function decodeJwt(token) {
|
|
216
|
+
if (token && isJwt(token)) return jwtDecode(token);
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
const useMyJwt = () => {
|
|
220
|
+
/**
|
|
221
|
+
* Jwt decode untuk mengambil payload informasi user.
|
|
222
|
+
* @param cookie nama cookie jwt yang ingin didecode (read via client document.cookie)
|
|
223
|
+
*/
|
|
224
|
+
const cookieDecode = ({ cookie, token, splitCookie }) => {
|
|
225
|
+
if (cookie) {
|
|
226
|
+
const decoded = decodeJwt(useMyCookie().get(cookie, Boolean(splitCookie)));
|
|
227
|
+
if (decoded) return decoded;
|
|
228
|
+
}
|
|
229
|
+
return decodeJwt(token);
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
isJwt,
|
|
233
|
+
cookieDecode
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/token/fetch.ts
|
|
239
|
+
async function useMyFetch(url, options) {
|
|
240
|
+
let response = null;
|
|
241
|
+
try {
|
|
242
|
+
response = await fetch(options?.baseUrl + url, {
|
|
243
|
+
...options,
|
|
244
|
+
headers: {
|
|
245
|
+
...options?.headers,
|
|
246
|
+
"Content-Type": "application/json",
|
|
247
|
+
...options?.token && { Authorization: `Bearer ${options.token}` }
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
if (!response.ok) {
|
|
251
|
+
if (response.status === 401) {
|
|
252
|
+
if (options?.unauthCall) options.unauthCall();
|
|
253
|
+
}
|
|
254
|
+
const errorResponse = await response.json();
|
|
255
|
+
const error = errorResponse?.Message || errorResponse?.message || errorResponse?.Error || errorResponse || "Terjadi kesalahan!.";
|
|
256
|
+
const errorReturn = typeof error === "string" ? error : JSON.stringify(error);
|
|
257
|
+
const respon = {
|
|
258
|
+
message: errorReturn,
|
|
259
|
+
statusCode: response.status,
|
|
260
|
+
data: null,
|
|
261
|
+
all: null
|
|
262
|
+
};
|
|
263
|
+
if (options.callback) options.callback({
|
|
264
|
+
message: errorReturn,
|
|
265
|
+
statusCode: response.status,
|
|
266
|
+
data: null,
|
|
267
|
+
all: null
|
|
268
|
+
});
|
|
269
|
+
return respon;
|
|
270
|
+
} else {
|
|
271
|
+
const raw = await response.text();
|
|
272
|
+
let responseData;
|
|
273
|
+
try {
|
|
274
|
+
responseData = raw === "" ? null : JSON.parse(raw);
|
|
275
|
+
} catch {
|
|
276
|
+
responseData = raw;
|
|
277
|
+
}
|
|
278
|
+
const respon = {
|
|
279
|
+
data: responseData?.data,
|
|
280
|
+
statusCode: response.status,
|
|
281
|
+
message: responseData?.message || null,
|
|
282
|
+
all: responseData
|
|
283
|
+
};
|
|
284
|
+
if (options.callback) options.callback(respon);
|
|
285
|
+
return respon;
|
|
286
|
+
}
|
|
287
|
+
} catch (error) {
|
|
288
|
+
const respon = {
|
|
289
|
+
message: error?.message || "Tejadi kesalahan!.",
|
|
290
|
+
statusCode: 500,
|
|
291
|
+
data: null,
|
|
292
|
+
all: null
|
|
293
|
+
};
|
|
294
|
+
if (options.callback) options.callback(respon);
|
|
295
|
+
return {
|
|
296
|
+
message: error?.message || "Tejadi kesalahan!.",
|
|
297
|
+
statusCode: 500,
|
|
298
|
+
data: null,
|
|
299
|
+
all: null
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/token/client.ts
|
|
306
|
+
/** Synchronous `CookieStore` over `document.cookie` (original encoding preserved). */
|
|
307
|
+
const documentCookieStore = {
|
|
308
|
+
read(name) {
|
|
309
|
+
const nameEQ = encodeURIComponent(name) + "=";
|
|
310
|
+
const cookies = document.cookie.split(";").map((s) => s.trim());
|
|
311
|
+
for (const cookie of cookies) if (cookie.startsWith(nameEQ)) return decodeURIComponent(cookie.substring(nameEQ.length));
|
|
312
|
+
return null;
|
|
313
|
+
},
|
|
314
|
+
readAll() {
|
|
315
|
+
return document.cookie.split(";").map((s) => s.trim()).filter(Boolean);
|
|
316
|
+
},
|
|
317
|
+
write(name, value, expires) {
|
|
318
|
+
const parts = [
|
|
319
|
+
`${encodeURIComponent(name)}=${encodeURIComponent(value)}`,
|
|
320
|
+
expires ? `expires=${expires.toUTCString()}` : "",
|
|
321
|
+
"path=/"
|
|
322
|
+
].filter(Boolean);
|
|
323
|
+
document.cookie = parts.join("; ");
|
|
324
|
+
},
|
|
325
|
+
delete(name) {
|
|
326
|
+
document.cookie = `${encodeURIComponent(name)}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
/** Client cookie util — `document.cookie` backed (unchanged public shape). */
|
|
330
|
+
const useMyCookie = () => createCookie(documentCookieStore);
|
|
331
|
+
/** Client token util — `document.cookie` backed (unchanged public shape). */
|
|
332
|
+
const useMyToken = (options) => createToken({
|
|
333
|
+
cookie: useMyCookie(),
|
|
334
|
+
isJwt,
|
|
335
|
+
decode: decodeJwt,
|
|
336
|
+
fetch: useMyFetch
|
|
337
|
+
}, options);
|
|
338
|
+
|
|
339
|
+
//#endregion
|
|
340
|
+
//#region src/token/storage.ts
|
|
341
|
+
const useMyStorage = (options) => {
|
|
342
|
+
const storage = !options?.type || options.type == "local" ? localStorage : sessionStorage;
|
|
343
|
+
const get = (name) => {
|
|
344
|
+
const optionName = name || options?.name;
|
|
345
|
+
if (typeof optionName === "string") return storage.getItem(optionName);
|
|
346
|
+
if (Array.isArray(optionName)) {
|
|
347
|
+
const mapStorage = optionName?.map((e) => {
|
|
348
|
+
return {
|
|
349
|
+
name: e,
|
|
350
|
+
value: storage.getItem(e)
|
|
351
|
+
};
|
|
352
|
+
}).filter((e) => e.value);
|
|
353
|
+
if (mapStorage.length > 0) return mapStorage;
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
if (optionName instanceof RegExp) {
|
|
357
|
+
var result = [];
|
|
358
|
+
for (let i = 0; i < storage.length; i++) {
|
|
359
|
+
const key = storage.key(i);
|
|
360
|
+
if (key && optionName.test(key)) result.push({
|
|
361
|
+
name: key,
|
|
362
|
+
value: storage.getItem(key) ?? void 0
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
const filterResult = result.filter((e) => e.value);
|
|
366
|
+
if (filterResult.length > 0) return filterResult;
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
return null;
|
|
370
|
+
};
|
|
371
|
+
const change = ({ name, value }) => {
|
|
372
|
+
const optionName = name || options?.name;
|
|
373
|
+
const optionValue = value || options?.value;
|
|
374
|
+
if (typeof optionName !== "string" || typeof optionValue === "undefined") return null;
|
|
375
|
+
const existing = storage.getItem(optionName);
|
|
376
|
+
if (!existing) return null;
|
|
377
|
+
try {
|
|
378
|
+
const parsed = JSON.parse(existing);
|
|
379
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed) && typeof optionValue === "object" && !Array.isArray(optionValue)) {
|
|
380
|
+
const merged = {
|
|
381
|
+
...parsed,
|
|
382
|
+
...optionValue
|
|
383
|
+
};
|
|
384
|
+
const stringified = JSON.stringify(merged);
|
|
385
|
+
storage.setItem(optionName, stringified);
|
|
386
|
+
return stringified;
|
|
387
|
+
}
|
|
388
|
+
const stringified = typeof optionValue === "string" ? optionValue : JSON.stringify(optionValue);
|
|
389
|
+
storage.setItem(optionName, stringified);
|
|
390
|
+
return stringified;
|
|
391
|
+
} catch {
|
|
392
|
+
const stringified = typeof optionValue === "string" ? optionValue : JSON.stringify(optionValue);
|
|
393
|
+
storage.setItem(optionName, stringified);
|
|
394
|
+
return stringified;
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
const add = ({ name, value, items }) => {
|
|
398
|
+
const optionName = name || options?.name;
|
|
399
|
+
const optionValue = value || options?.value;
|
|
400
|
+
const optionItems = items || options?.items;
|
|
401
|
+
if (!optionItems && typeof optionName === "string" && optionValue) {
|
|
402
|
+
const stringified = typeof optionValue === "string" ? optionValue : JSON.stringify(optionValue);
|
|
403
|
+
storage.setItem(optionName, stringified);
|
|
404
|
+
return stringified;
|
|
405
|
+
}
|
|
406
|
+
if (optionItems) {
|
|
407
|
+
const filterItems = optionItems.filter((item) => typeof item?.name === "string" && item.value);
|
|
408
|
+
var stringifiedd = "";
|
|
409
|
+
filterItems.forEach((item) => {
|
|
410
|
+
const stringified = typeof item.value === "string" ? item.value : JSON.stringify(item.value);
|
|
411
|
+
stringifiedd += stringified;
|
|
412
|
+
storage.setItem(String(item?.name), stringified);
|
|
413
|
+
});
|
|
414
|
+
return stringifiedd;
|
|
415
|
+
}
|
|
416
|
+
return null;
|
|
417
|
+
};
|
|
418
|
+
const remove = (name) => {
|
|
419
|
+
const optionName = name || options?.name;
|
|
420
|
+
if (typeof optionName === "string") {
|
|
421
|
+
storage.removeItem(optionName);
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
if (Array.isArray(optionName)) {
|
|
425
|
+
optionName.forEach((key) => storage.removeItem(key));
|
|
426
|
+
return true;
|
|
427
|
+
}
|
|
428
|
+
if (optionName instanceof RegExp) {
|
|
429
|
+
const toRemove = [];
|
|
430
|
+
for (let i = 0; i < storage.length; i++) {
|
|
431
|
+
const key = storage.key(i);
|
|
432
|
+
if (key && optionName.test(key)) toRemove.push(key);
|
|
433
|
+
}
|
|
434
|
+
toRemove.forEach((key) => storage.removeItem(key));
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
return false;
|
|
438
|
+
};
|
|
439
|
+
const pull = (name) => {
|
|
440
|
+
const value = get(name);
|
|
441
|
+
if (value) {
|
|
442
|
+
remove(name);
|
|
443
|
+
return value;
|
|
444
|
+
}
|
|
445
|
+
return null;
|
|
446
|
+
};
|
|
447
|
+
const redirect = (url, { name, value, items }) => {
|
|
448
|
+
if (add({
|
|
449
|
+
name,
|
|
450
|
+
value,
|
|
451
|
+
items
|
|
452
|
+
})) window.location.href = url;
|
|
453
|
+
};
|
|
454
|
+
return {
|
|
455
|
+
get,
|
|
456
|
+
add,
|
|
457
|
+
remove,
|
|
458
|
+
change,
|
|
459
|
+
pull,
|
|
460
|
+
redirect
|
|
461
|
+
};
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
//#endregion
|
|
465
|
+
//#region src/composables/universal.ts
|
|
466
|
+
const isClient = () => typeof document !== "undefined";
|
|
467
|
+
let eventResolver;
|
|
468
|
+
/**
|
|
469
|
+
* Wire how the SSR cookie utils find the current request event. Call once from a
|
|
470
|
+
* Nuxt plugin: `setMonoEventResolver(() => useRequestEvent())`. Without it (and
|
|
471
|
+
* without an explicit `event` argument) the server utils degrade to empty reads.
|
|
472
|
+
*/
|
|
473
|
+
function setMonoEventResolver(fn) {
|
|
474
|
+
eventResolver = fn;
|
|
475
|
+
}
|
|
476
|
+
function resolveEvent(explicit) {
|
|
477
|
+
return explicit ?? eventResolver?.();
|
|
478
|
+
}
|
|
479
|
+
/** Per-request write overlay: makes read-after-write work; `null` = tombstone. */
|
|
480
|
+
const overlay = /* @__PURE__ */ new WeakMap();
|
|
481
|
+
function overlayFor(event) {
|
|
482
|
+
let m = overlay.get(event);
|
|
483
|
+
if (!m) {
|
|
484
|
+
m = /* @__PURE__ */ new Map();
|
|
485
|
+
overlay.set(event, m);
|
|
486
|
+
}
|
|
487
|
+
return m;
|
|
488
|
+
}
|
|
489
|
+
function reqHeaderCookie(event) {
|
|
490
|
+
return event?.node?.req?.headers?.cookie ?? event?.req?.headers?.cookie ?? "";
|
|
491
|
+
}
|
|
492
|
+
function parseReqCookies(event) {
|
|
493
|
+
const out = {};
|
|
494
|
+
const header = reqHeaderCookie(event);
|
|
495
|
+
if (!header) return out;
|
|
496
|
+
for (const pair of header.split(";")) {
|
|
497
|
+
const eq = pair.indexOf("=");
|
|
498
|
+
if (eq < 0) continue;
|
|
499
|
+
const k = decodeURIComponent(pair.slice(0, eq).trim());
|
|
500
|
+
const v = decodeURIComponent(pair.slice(eq + 1).trim());
|
|
501
|
+
if (k) out[k] = v;
|
|
502
|
+
}
|
|
503
|
+
return out;
|
|
504
|
+
}
|
|
505
|
+
/** Combined view: request cookies overlaid with this request's writes/tombstones. */
|
|
506
|
+
function combinedCookies(event) {
|
|
507
|
+
const map = new Map(Object.entries(parseReqCookies(event)));
|
|
508
|
+
const ov = overlayFor(event);
|
|
509
|
+
for (const [k, v] of ov) if (v == null) map.delete(k);
|
|
510
|
+
else map.set(k, v);
|
|
511
|
+
return map;
|
|
512
|
+
}
|
|
513
|
+
function serializeCookie(name, value, expires) {
|
|
514
|
+
return [
|
|
515
|
+
`${encodeURIComponent(name)}=${encodeURIComponent(value)}`,
|
|
516
|
+
expires ? `Expires=${expires.toUTCString()}` : "",
|
|
517
|
+
"Path=/"
|
|
518
|
+
].filter(Boolean).join("; ");
|
|
519
|
+
}
|
|
520
|
+
function appendSetCookie(event, serialized) {
|
|
521
|
+
const res = event?.node?.res ?? event?.res;
|
|
522
|
+
if (!res || typeof res.setHeader !== "function") return;
|
|
523
|
+
const prev = res.getHeader?.("set-cookie");
|
|
524
|
+
const arr = prev == null ? [] : Array.isArray(prev) ? prev.slice() : [prev];
|
|
525
|
+
arr.push(serialized);
|
|
526
|
+
res.setHeader("set-cookie", arr);
|
|
527
|
+
}
|
|
528
|
+
/** A synchronous `CookieStore` backed by the h3 request event (+ overlay). */
|
|
529
|
+
function eventCookieStore(event) {
|
|
530
|
+
if (!event) return {
|
|
531
|
+
read: () => null,
|
|
532
|
+
readAll: () => [],
|
|
533
|
+
write: () => {},
|
|
534
|
+
delete: () => {}
|
|
535
|
+
};
|
|
536
|
+
return {
|
|
537
|
+
read(name) {
|
|
538
|
+
const ov = overlayFor(event);
|
|
539
|
+
if (ov.has(name)) return ov.get(name) ?? null;
|
|
540
|
+
return parseReqCookies(event)[name] ?? null;
|
|
541
|
+
},
|
|
542
|
+
readAll() {
|
|
543
|
+
return [...combinedCookies(event)].map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
|
|
544
|
+
},
|
|
545
|
+
write(name, value, expires) {
|
|
546
|
+
appendSetCookie(event, serializeCookie(name, value, expires));
|
|
547
|
+
overlayFor(event).set(name, value);
|
|
548
|
+
},
|
|
549
|
+
delete(name) {
|
|
550
|
+
appendSetCookie(event, serializeCookie(name, "", /* @__PURE__ */ new Date(0)));
|
|
551
|
+
overlayFor(event).set(name, null);
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
/** Cookie util. Client: document.cookie. Server: the h3 request event (sync). */
|
|
556
|
+
function monoCookie(event) {
|
|
557
|
+
if (isClient()) return useMyCookie();
|
|
558
|
+
return createCookie(eventCookieStore(resolveEvent(event)));
|
|
559
|
+
}
|
|
560
|
+
/** Token util. Client: document.cookie. Server: the h3 request event (sync). */
|
|
561
|
+
function monoToken(options, event) {
|
|
562
|
+
if (isClient()) return useMyToken(options);
|
|
563
|
+
return createToken({
|
|
564
|
+
cookie: monoCookie(event),
|
|
565
|
+
isJwt,
|
|
566
|
+
decode: decodeJwt,
|
|
567
|
+
fetch: useMyFetch
|
|
568
|
+
}, options);
|
|
569
|
+
}
|
|
570
|
+
/** JWT util. `cookieDecode({ cookie })` reads via the isomorphic cookie util. */
|
|
571
|
+
function monoJwt(event) {
|
|
572
|
+
if (isClient()) return useMyJwt();
|
|
573
|
+
const cookieDecode = ({ cookie, token, splitCookie }) => {
|
|
574
|
+
if (cookie) {
|
|
575
|
+
const tok = monoCookie(event).get(cookie, Boolean(splitCookie));
|
|
576
|
+
const decoded = decodeJwt(tok);
|
|
577
|
+
if (decoded) return decoded;
|
|
578
|
+
}
|
|
579
|
+
return decodeJwt(token);
|
|
580
|
+
};
|
|
581
|
+
return {
|
|
582
|
+
isJwt,
|
|
583
|
+
cookieDecode
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
/** Storage util. Client: localStorage/sessionStorage. Server: safe no-op. */
|
|
587
|
+
function monoStorage(options) {
|
|
588
|
+
if (isClient()) return useMyStorage(options);
|
|
589
|
+
return {
|
|
590
|
+
get: () => null,
|
|
591
|
+
add: (_) => null,
|
|
592
|
+
remove: (_) => false,
|
|
593
|
+
change: (_) => null,
|
|
594
|
+
pull: (_) => null,
|
|
595
|
+
redirect: () => {}
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
//#endregion
|
|
600
|
+
export { setMonoEventResolver as a, useMyToken as c, useMyJwt as d, monoToken as i, useMyFetch as l, monoJwt as n, useMyStorage as o, monoStorage as r, useMyCookie as s, monoCookie as t, decodeJwt as u };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `virtual:mono-apps` — provided by @mono-lit/utility's Vite plugin (`monoLith()` or
|
|
3
|
+
* `monoRepo().vite()` / `mono.plugin`): one lazy import per federated app's
|
|
4
|
+
* `mono.config`, keyed by app name, whether the app is a `.mono/apps` clone or
|
|
5
|
+
* an `apps[].path` sibling.
|
|
6
|
+
*
|
|
7
|
+
* Reference it from the app's `env.d.ts`:
|
|
8
|
+
* /// <reference types="@mono-lit/utility/virtual-mono-apps" />
|
|
9
|
+
*/
|
|
10
|
+
declare module 'virtual:mono-apps' {
|
|
11
|
+
const apps: Record<
|
|
12
|
+
string,
|
|
13
|
+
() => Promise<{ default: import('@mono-lit/utility/config').MonoConfig }>
|
|
14
|
+
>
|
|
15
|
+
export default apps
|
|
16
|
+
}
|