@sveltebase/utils 0.1.0 → 0.1.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/dist/async.svelte.d.ts.map +1 -1
- package/dist/async.svelte.js +30 -6
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +86 -12
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async.svelte.d.ts","sourceRoot":"","sources":["../src/async.svelte.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"async.svelte.d.ts","sourceRoot":"","sources":["../src/async.svelte.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,cAAc,GACtB;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,GAClC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,KAAK,CAAA;CAAE,GAClC,IAAI,GACJ,IAAI,CAAC;AAqCT,wBAAgB,WAAW,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,cAAc,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,EAC/F,OAAO,EAAE,CAAC;IAmDR;;;OAGG;oBACa,MAAM;;mBAhBI,UAAU,CAAC,CAAC,CAAC;sBAOV,MAAM,WAAW,UAAU,CAAC,CAAC,CAAC;EAkB9D"}
|
package/dist/async.svelte.js
CHANGED
|
@@ -1,7 +1,31 @@
|
|
|
1
|
-
import { DEV } from "esm-env";
|
|
2
|
-
import { toast } from "svelte-sonner";
|
|
1
|
+
import { BROWSER, DEV } from "esm-env";
|
|
3
2
|
import { SvelteMap } from "svelte/reactivity";
|
|
4
3
|
const GLOBAL_KEY = "__global__";
|
|
4
|
+
let toastModulePromise = null;
|
|
5
|
+
async function getToastModule() {
|
|
6
|
+
if (!BROWSER) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
if (!toastModulePromise) {
|
|
10
|
+
toastModulePromise = import("svelte-sonner")
|
|
11
|
+
.then((module) => module)
|
|
12
|
+
.catch((error) => {
|
|
13
|
+
if (DEV) {
|
|
14
|
+
console.error("[createAsync] Failed to load svelte-sonner:", error);
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return toastModulePromise;
|
|
20
|
+
}
|
|
21
|
+
async function toastSuccess(message) {
|
|
22
|
+
const toastModule = await getToastModule();
|
|
23
|
+
toastModule?.toast.success(message);
|
|
24
|
+
}
|
|
25
|
+
async function toastError(message, description) {
|
|
26
|
+
const toastModule = await getToastModule();
|
|
27
|
+
toastModule?.toast.error(message, description ? { description } : undefined);
|
|
28
|
+
}
|
|
5
29
|
export function createAsync(asyncFn) {
|
|
6
30
|
const loadingStates = $state(new SvelteMap());
|
|
7
31
|
let error = $state(null);
|
|
@@ -11,10 +35,10 @@ export function createAsync(asyncFn) {
|
|
|
11
35
|
error = null;
|
|
12
36
|
const response = await asyncFn(...args);
|
|
13
37
|
if (response?.success) {
|
|
14
|
-
|
|
38
|
+
await toastSuccess(response.success);
|
|
15
39
|
}
|
|
16
40
|
else if (response?.error) {
|
|
17
|
-
|
|
41
|
+
await toastError(response.error);
|
|
18
42
|
}
|
|
19
43
|
return response;
|
|
20
44
|
}
|
|
@@ -22,11 +46,11 @@ export function createAsync(asyncFn) {
|
|
|
22
46
|
const e = err instanceof Error ? err : new Error(String(err));
|
|
23
47
|
error = e;
|
|
24
48
|
if (DEV) {
|
|
25
|
-
|
|
49
|
+
await toastError(e.name, e.message);
|
|
26
50
|
console.error("[Dev Error]:", e);
|
|
27
51
|
}
|
|
28
52
|
else {
|
|
29
|
-
|
|
53
|
+
await toastError("Something went wrong");
|
|
30
54
|
}
|
|
31
55
|
throw e;
|
|
32
56
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -26,5 +26,10 @@ export type TryCatchReturn = {
|
|
|
26
26
|
} | null | void;
|
|
27
27
|
export declare function tryCatch(task: () => Promise<TryCatchReturn> | TryCatchReturn): Promise<void>;
|
|
28
28
|
export declare const wait: (ms: number) => Promise<unknown>;
|
|
29
|
-
export
|
|
29
|
+
export declare function createAsync<T extends (...args: any[]) => Promise<TryCatchReturn> | Promise<void>>(asyncFn: T): {
|
|
30
|
+
isLoading(key?: string): boolean;
|
|
31
|
+
readonly error: Error | null;
|
|
32
|
+
run: (...args: Parameters<T>) => Promise<TryCatchReturn>;
|
|
33
|
+
runWithKey: (key: string, ...args: Parameters<T>) => Promise<TryCatchReturn>;
|
|
34
|
+
};
|
|
30
35
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACrC,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACrC,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAuCD,eAAO,MAAM,OAAO;cACR,MAAM,SAAS,MAAM,YAAW,aAAa,GAAQ,IAAI;cA6CzD,MAAM,GAAG,MAAM,GAAG,IAAI;iBAYnB,MAAM,YAAW,IAAI,CAAC,aAAa,EAAE,MAAM,GAAG,QAAQ,CAAC,GAAQ,IAAI;CAGjF,CAAC;AAEF,wBAAgB,UAAU,CAAC,CAAC,SAAS,OAAO,EAC1C,UAAU,EAAE,CAAC,GACZ,CAAC,SAAS,IAAI,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAInF;AAED,MAAM,MAAM,cAAc,GACtB;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,GAClC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,KAAK,CAAA;CAAE,GAClC,IAAI,GACJ,IAAI,CAAC;AAET,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,GAAG,cAAc,iBAkBlF;AAED,eAAO,MAAM,IAAI,GAAI,IAAI,MAAM,qBAAsD,CAAC;AAItF,wBAAgB,WAAW,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,cAAc,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,EAC/F,OAAO,EAAE,CAAC;oBA6CQ,MAAM;;mBATI,UAAU,CAAC,CAAC,CAAC;sBAIV,MAAM,WAAW,UAAU,CAAC,CAAC,CAAC;EAc9D"}
|
package/dist/index.js
CHANGED
|
@@ -1,30 +1,53 @@
|
|
|
1
1
|
import { DEV } from "esm-env";
|
|
2
|
-
import {
|
|
2
|
+
import { SvelteMap } from "svelte/reactivity";
|
|
3
|
+
let toastModulePromise = null;
|
|
4
|
+
function hasBrowser() {
|
|
5
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
6
|
+
}
|
|
7
|
+
async function getToast() {
|
|
8
|
+
if (!hasBrowser()) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
if (!toastModulePromise) {
|
|
12
|
+
toastModulePromise = import("svelte-sonner")
|
|
13
|
+
.then((module) => module)
|
|
14
|
+
.catch(() => null);
|
|
15
|
+
}
|
|
16
|
+
return toastModulePromise;
|
|
17
|
+
}
|
|
18
|
+
async function toastSuccess(message, options) {
|
|
19
|
+
const toast = await getToast();
|
|
20
|
+
toast?.toast.success(message, options);
|
|
21
|
+
}
|
|
22
|
+
async function toastError(message, options) {
|
|
23
|
+
const toast = await getToast();
|
|
24
|
+
toast?.toast.error(message, options);
|
|
25
|
+
}
|
|
3
26
|
export const Cookies = {
|
|
4
27
|
set(name, value, options = {}) {
|
|
28
|
+
if (!hasBrowser()) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
5
31
|
const defaults = {
|
|
6
32
|
path: "/",
|
|
7
33
|
sameSite: "Lax",
|
|
8
34
|
secure: window.location.protocol === "https:"
|
|
9
35
|
};
|
|
10
36
|
const settings = { ...defaults, ...options };
|
|
11
|
-
// 1. Encode name and value
|
|
12
37
|
let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
13
|
-
// 2. Expiration (Capped at 400 days by modern browsers)
|
|
14
38
|
if (settings.expires) {
|
|
15
39
|
const maxAge = settings.expires * 24 * 60 * 60;
|
|
16
40
|
cookieString += `; max-age=${maxAge}`;
|
|
17
41
|
}
|
|
18
|
-
// 3. Attributes
|
|
19
42
|
cookieString += `; path=${settings.path}`;
|
|
20
43
|
if (settings.domain) {
|
|
21
44
|
cookieString += `; domain=${settings.domain}`;
|
|
22
45
|
}
|
|
23
46
|
if (settings.sameSite) {
|
|
24
47
|
cookieString += `; samesite=${settings.sameSite}`;
|
|
25
|
-
|
|
26
|
-
if (settings.sameSite === "None")
|
|
48
|
+
if (settings.sameSite === "None") {
|
|
27
49
|
settings.secure = true;
|
|
50
|
+
}
|
|
28
51
|
}
|
|
29
52
|
if (settings.secure) {
|
|
30
53
|
cookieString += "; secure";
|
|
@@ -35,6 +58,9 @@ export const Cookies = {
|
|
|
35
58
|
document.cookie = cookieString;
|
|
36
59
|
},
|
|
37
60
|
get(name) {
|
|
61
|
+
if (!hasBrowser()) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
38
64
|
const match = document.cookie.match(new RegExp("(^| )" + encodeURIComponent(name) + "=([^;]+)"));
|
|
39
65
|
return match ? decodeURIComponent(match[2]) : null;
|
|
40
66
|
},
|
|
@@ -50,23 +76,71 @@ export async function tryCatch(task) {
|
|
|
50
76
|
try {
|
|
51
77
|
const response = await task();
|
|
52
78
|
if (response?.success) {
|
|
53
|
-
|
|
79
|
+
await toastSuccess(response.success);
|
|
54
80
|
}
|
|
55
81
|
else if (response?.error) {
|
|
56
|
-
|
|
82
|
+
await toastError(response.error);
|
|
57
83
|
}
|
|
58
84
|
}
|
|
59
85
|
catch (err) {
|
|
60
|
-
// Handle unexpected runtime crashes
|
|
61
86
|
if (DEV) {
|
|
62
87
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
63
|
-
|
|
88
|
+
await toastError(error.name, { description: error.message });
|
|
64
89
|
console.error("[Dev Error]:", error);
|
|
65
90
|
}
|
|
66
91
|
else {
|
|
67
|
-
|
|
92
|
+
await toastError("Something went wrong");
|
|
68
93
|
}
|
|
69
94
|
}
|
|
70
95
|
}
|
|
71
96
|
export const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
72
|
-
|
|
97
|
+
const GLOBAL_KEY = "__global__";
|
|
98
|
+
export function createAsync(asyncFn) {
|
|
99
|
+
const loadingStates = $state(new SvelteMap());
|
|
100
|
+
let error = $state(null);
|
|
101
|
+
async function execute(id, args) {
|
|
102
|
+
try {
|
|
103
|
+
loadingStates.set(id, true);
|
|
104
|
+
error = null;
|
|
105
|
+
const response = await asyncFn(...args);
|
|
106
|
+
if (response?.success) {
|
|
107
|
+
await toastSuccess(response.success);
|
|
108
|
+
}
|
|
109
|
+
else if (response?.error) {
|
|
110
|
+
await toastError(response.error);
|
|
111
|
+
}
|
|
112
|
+
return response;
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
116
|
+
error = e;
|
|
117
|
+
if (DEV) {
|
|
118
|
+
await toastError(e.name, { description: e.message });
|
|
119
|
+
console.error("[Dev Error]:", e);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
await toastError("Something went wrong");
|
|
123
|
+
}
|
|
124
|
+
throw e;
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
loadingStates.set(id, false);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async function run(...args) {
|
|
131
|
+
return execute(GLOBAL_KEY, args);
|
|
132
|
+
}
|
|
133
|
+
async function runWithKey(key, ...args) {
|
|
134
|
+
return execute(key || GLOBAL_KEY, args);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
isLoading(key) {
|
|
138
|
+
return loadingStates.get(key ?? GLOBAL_KEY) ?? false;
|
|
139
|
+
},
|
|
140
|
+
get error() {
|
|
141
|
+
return error;
|
|
142
|
+
},
|
|
143
|
+
run,
|
|
144
|
+
runWithKey
|
|
145
|
+
};
|
|
146
|
+
}
|