@sveltebase/utils 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/async.svelte.d.ts +18 -0
- package/dist/async.svelte.d.ts.map +1 -0
- package/dist/async.svelte.js +63 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +72 -0
- package/package.json +36 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type TryCatchReturn = {
|
|
2
|
+
success: string;
|
|
3
|
+
error?: never;
|
|
4
|
+
} | {
|
|
5
|
+
error: string;
|
|
6
|
+
success?: never;
|
|
7
|
+
} | null | void;
|
|
8
|
+
export declare function createAsync<T extends (...args: any[]) => Promise<TryCatchReturn> | Promise<void>>(asyncFn: T): {
|
|
9
|
+
/**
|
|
10
|
+
* Check loading state.
|
|
11
|
+
* Pass a key for specific actions, or call without args for global actions.
|
|
12
|
+
*/
|
|
13
|
+
isLoading(key?: string): boolean;
|
|
14
|
+
readonly error: Error | null;
|
|
15
|
+
run: (...args: Parameters<T>) => Promise<TryCatchReturn>;
|
|
16
|
+
runWithKey: (key: string, ...args: Parameters<T>) => Promise<TryCatchReturn>;
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=async.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async.svelte.d.ts","sourceRoot":"","sources":["../src/async.svelte.ts"],"names":[],"mappings":"AAIA,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;AAIT,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"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DEV } from "esm-env";
|
|
2
|
+
import { toast } from "svelte-sonner";
|
|
3
|
+
import { SvelteMap } from "svelte/reactivity";
|
|
4
|
+
const GLOBAL_KEY = "__global__";
|
|
5
|
+
export function createAsync(asyncFn) {
|
|
6
|
+
const loadingStates = $state(new SvelteMap());
|
|
7
|
+
let error = $state(null);
|
|
8
|
+
async function execute(id, args) {
|
|
9
|
+
try {
|
|
10
|
+
loadingStates.set(id, true);
|
|
11
|
+
error = null;
|
|
12
|
+
const response = await asyncFn(...args);
|
|
13
|
+
if (response?.success) {
|
|
14
|
+
toast.success(response.success);
|
|
15
|
+
}
|
|
16
|
+
else if (response?.error) {
|
|
17
|
+
toast.error(response.error);
|
|
18
|
+
}
|
|
19
|
+
return response;
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
23
|
+
error = e;
|
|
24
|
+
if (DEV) {
|
|
25
|
+
toast.error(e.name, { description: e.message });
|
|
26
|
+
console.error("[Dev Error]:", e);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
toast.error("Something went wrong");
|
|
30
|
+
}
|
|
31
|
+
throw e;
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
loadingStates.set(id, false);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Execute using the global loading key.
|
|
39
|
+
*/
|
|
40
|
+
async function run(...args) {
|
|
41
|
+
return execute(GLOBAL_KEY, args);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Execute using an explicit loading key.
|
|
45
|
+
*/
|
|
46
|
+
async function runWithKey(key, ...args) {
|
|
47
|
+
return execute(key || GLOBAL_KEY, args);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
/**
|
|
51
|
+
* Check loading state.
|
|
52
|
+
* Pass a key for specific actions, or call without args for global actions.
|
|
53
|
+
*/
|
|
54
|
+
isLoading(key) {
|
|
55
|
+
return loadingStates.get(key ?? GLOBAL_KEY) ?? false;
|
|
56
|
+
},
|
|
57
|
+
get error() {
|
|
58
|
+
return error;
|
|
59
|
+
},
|
|
60
|
+
run,
|
|
61
|
+
runWithKey
|
|
62
|
+
};
|
|
63
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface CookieOptions {
|
|
2
|
+
expires?: number;
|
|
3
|
+
path?: string;
|
|
4
|
+
domain?: string;
|
|
5
|
+
secure?: boolean;
|
|
6
|
+
sameSite?: "Lax" | "Strict" | "None";
|
|
7
|
+
partitioned?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const Cookies: {
|
|
10
|
+
set(name: string, value: string, options?: CookieOptions): void;
|
|
11
|
+
get(name: string): string | null;
|
|
12
|
+
remove(name: string, options?: Pick<CookieOptions, "path" | "domain">): void;
|
|
13
|
+
};
|
|
14
|
+
export declare function timestamps<T extends boolean>(updateOnly: T): T extends true ? {
|
|
15
|
+
updatedAt: number;
|
|
16
|
+
} : {
|
|
17
|
+
createdAt: number;
|
|
18
|
+
updatedAt: number;
|
|
19
|
+
};
|
|
20
|
+
export type TryCatchReturn = {
|
|
21
|
+
success: string;
|
|
22
|
+
error?: never;
|
|
23
|
+
} | {
|
|
24
|
+
error: string;
|
|
25
|
+
success?: never;
|
|
26
|
+
} | null | void;
|
|
27
|
+
export declare function tryCatch(task: () => Promise<TryCatchReturn> | TryCatchReturn): Promise<void>;
|
|
28
|
+
export declare const wait: (ms: number) => Promise<unknown>;
|
|
29
|
+
export { createAsync } from "./async.svelte.js";
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
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@
|
|
|
1
|
+
import { DEV } from "esm-env";
|
|
2
|
+
import { toast } from "svelte-sonner";
|
|
3
|
+
export const Cookies = {
|
|
4
|
+
set(name, value, options = {}) {
|
|
5
|
+
const defaults = {
|
|
6
|
+
path: "/",
|
|
7
|
+
sameSite: "Lax",
|
|
8
|
+
secure: window.location.protocol === "https:"
|
|
9
|
+
};
|
|
10
|
+
const settings = { ...defaults, ...options };
|
|
11
|
+
// 1. Encode name and value
|
|
12
|
+
let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
13
|
+
// 2. Expiration (Capped at 400 days by modern browsers)
|
|
14
|
+
if (settings.expires) {
|
|
15
|
+
const maxAge = settings.expires * 24 * 60 * 60;
|
|
16
|
+
cookieString += `; max-age=${maxAge}`;
|
|
17
|
+
}
|
|
18
|
+
// 3. Attributes
|
|
19
|
+
cookieString += `; path=${settings.path}`;
|
|
20
|
+
if (settings.domain) {
|
|
21
|
+
cookieString += `; domain=${settings.domain}`;
|
|
22
|
+
}
|
|
23
|
+
if (settings.sameSite) {
|
|
24
|
+
cookieString += `; samesite=${settings.sameSite}`;
|
|
25
|
+
// If SameSite is 'None', Secure MUST be true
|
|
26
|
+
if (settings.sameSite === "None")
|
|
27
|
+
settings.secure = true;
|
|
28
|
+
}
|
|
29
|
+
if (settings.secure) {
|
|
30
|
+
cookieString += "; secure";
|
|
31
|
+
}
|
|
32
|
+
if (settings.partitioned) {
|
|
33
|
+
cookieString += "; partitioned";
|
|
34
|
+
}
|
|
35
|
+
document.cookie = cookieString;
|
|
36
|
+
},
|
|
37
|
+
get(name) {
|
|
38
|
+
const match = document.cookie.match(new RegExp("(^| )" + encodeURIComponent(name) + "=([^;]+)"));
|
|
39
|
+
return match ? decodeURIComponent(match[2]) : null;
|
|
40
|
+
},
|
|
41
|
+
remove(name, options = {}) {
|
|
42
|
+
this.set(name, "", { ...options, expires: -1 });
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
export function timestamps(updateOnly) {
|
|
46
|
+
const date = Date.now();
|
|
47
|
+
return (updateOnly ? { updatedAt: date } : { createdAt: date, updatedAt: date });
|
|
48
|
+
}
|
|
49
|
+
export async function tryCatch(task) {
|
|
50
|
+
try {
|
|
51
|
+
const response = await task();
|
|
52
|
+
if (response?.success) {
|
|
53
|
+
toast.success(response.success);
|
|
54
|
+
}
|
|
55
|
+
else if (response?.error) {
|
|
56
|
+
toast.error(response.error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
// Handle unexpected runtime crashes
|
|
61
|
+
if (DEV) {
|
|
62
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
63
|
+
toast.error(error.name, { description: error.message });
|
|
64
|
+
console.error("[Dev Error]:", error);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
toast.error("Something went wrong");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
72
|
+
export { createAsync } from "./async.svelte.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sveltebase/utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"svelte": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"svelte": "./dist/index.js",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"svelte": "^5.0.0",
|
|
22
|
+
"svelte-sonner": "^1.0.5"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"esm-env": "^1.2.2"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"svelte-sonner": "^1.0.5"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "svelte-package --input ./src --output ./dist",
|
|
32
|
+
"check": "tsc --project tsconfig.json --noEmit",
|
|
33
|
+
"lint": "bun run build && publint --strict --pack npm",
|
|
34
|
+
"clean": "rm -rf dist"
|
|
35
|
+
}
|
|
36
|
+
}
|