@solid-primitives/storage
Advanced tools
Comparing version 1.3.3 to 1.3.4
103
dist/dev.js
@@ -6,3 +6,3 @@ import { createSignal, onMount, onCleanup, createEffect } from 'solid-js'; | ||
const [error, setError] = createSignal(); | ||
const handleError = (props == null ? void 0 : props.throw) ? (err, fallback) => { | ||
const handleError = props?.throw ? (err, fallback) => { | ||
setError(err instanceof Error ? err : new Error(fallback)); | ||
@@ -13,4 +13,4 @@ throw err; | ||
}; | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const signals = /* @__PURE__ */ new Map(); | ||
@@ -41,4 +41,4 @@ const store = new Proxy( | ||
); | ||
if (value !== null && (props == null ? void 0 : props.deserializer)) { | ||
return props.deserializer(value, key, props == null ? void 0 : props.options); | ||
if (value !== null && props?.deserializer) { | ||
return props.deserializer(value, key, props?.options); | ||
} | ||
@@ -50,6 +50,6 @@ return value; | ||
const setter = (key, value, options) => { | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer( | ||
const filteredValue = props?.serializer ? props.serializer( | ||
value, | ||
key, | ||
options ?? (props == null ? void 0 : props.options) | ||
options ?? props?.options | ||
) : value; | ||
@@ -75,5 +75,4 @@ const apiKey = `${prefix}${key}`; | ||
const clear = () => apis.forEach((api) => { | ||
var _a; | ||
try { | ||
(_a = api == null ? void 0 : api.clear) == null ? void 0 : _a.call(api); | ||
api?.clear?.(); | ||
} catch (err) { | ||
@@ -87,3 +86,3 @@ handleError(err, `Error clearing ${api.name}`); | ||
if (!result.hasOwnProperty(key)) { | ||
const filteredValue = value && (props == null ? void 0 : props.deserializer) ? props.deserializer(value, key, props == null ? void 0 : props.options) : value; | ||
const filteredValue = value && props?.deserializer ? props.deserializer(value, key, props?.options) : value; | ||
if (filteredValue) { | ||
@@ -120,5 +119,4 @@ result[key] = filteredValue; | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
var _a; | ||
let changed = false; | ||
@@ -138,3 +136,3 @@ apis.forEach((api) => { | ||
}); | ||
changed && ev.key && ((_a = signals.get(ev.key)) == null ? void 0 : _a[1]()); | ||
changed && ev.key && signals.get(ev.key)?.[1](); | ||
}; | ||
@@ -145,10 +143,4 @@ if ("addEventListener" in globalThis) { | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -169,3 +161,3 @@ }); | ||
const [error, setError] = createSignal(); | ||
const handleError = (props == null ? void 0 : props.throw) ? (err, fallback) => { | ||
const handleError = props?.throw ? (err, fallback) => { | ||
setError(err instanceof Error ? err : new Error(fallback)); | ||
@@ -176,4 +168,4 @@ throw err; | ||
}; | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : []; | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : []; | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const signals = /* @__PURE__ */ new Map(); | ||
@@ -200,6 +192,6 @@ const store = new Proxy({}, { | ||
return value.then( | ||
(value2) => value2 && (props == null ? void 0 : props.deserializer) ? props.deserializer(value2, key, props == null ? void 0 : props.options) : value2 | ||
(value2) => value2 && props?.deserializer ? props.deserializer(value2, key, props?.options) : value2 | ||
); | ||
} | ||
return value !== null && (props == null ? void 0 : props.deserializer) ? Promise.resolve(props.deserializer(value, key, props == null ? void 0 : props.options)) : Promise.resolve(value); | ||
return value !== null && props?.deserializer ? Promise.resolve(props.deserializer(value, key, props?.options)) : Promise.resolve(value); | ||
}, null); | ||
@@ -209,3 +201,3 @@ } | ||
const setter = (key, value, options) => { | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer(value, key, options ?? (props == null ? void 0 : props.options)) : value; | ||
const filteredValue = props?.serializer ? props.serializer(value, key, options ?? props?.options) : value; | ||
return Promise.all( | ||
@@ -217,3 +209,3 @@ apis.map((api) => { | ||
filteredValue, | ||
options ?? (props == null ? void 0 : props.options) | ||
options ?? props?.options | ||
); | ||
@@ -261,3 +253,3 @@ } catch (err) { | ||
if (!result.hasOwnProperty(key)) { | ||
const filteredValue = value && (props == null ? void 0 : props.deserializer) ? props.deserializer(value, key, props == null ? void 0 : props.options) : value; | ||
const filteredValue = value && props?.deserializer ? props.deserializer(value, key, props?.options) : value; | ||
if (filteredValue) { | ||
@@ -293,5 +285,4 @@ result[key] = filteredValue; | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
var _a; | ||
let changed = false; | ||
@@ -308,3 +299,3 @@ apis.forEach(async (api) => { | ||
}); | ||
changed && ev.key && ((_a = signals.get(ev.key)) == null ? void 0 : _a[1]()); | ||
changed && ev.key && signals.get(ev.key)?.[1](); | ||
}; | ||
@@ -315,10 +306,4 @@ if ("addEventListener" in globalThis) { | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -339,4 +324,4 @@ }); | ||
const [error, setError] = createSignal(); | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const read = () => apis.reduce((result, api) => { | ||
@@ -353,8 +338,8 @@ if (result !== null || !api) { | ||
); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
} | ||
} | ||
if (value !== null && (props == null ? void 0 : props.deserializer)) { | ||
return props.deserializer(value + "", key, props == null ? void 0 : props.options); | ||
if (value !== null && props?.deserializer) { | ||
return props.deserializer(value + "", key, props?.options); | ||
} | ||
@@ -366,3 +351,3 @@ return value; | ||
const value = accessor(); | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer(value, key, props == null ? void 0 : props.options) : value + ""; | ||
const filteredValue = props?.serializer ? props.serializer(value, key, props?.options) : value + ""; | ||
const apiKey = `${prefix}${key}`; | ||
@@ -374,3 +359,3 @@ try { | ||
apis.forEach( | ||
(api) => api.getItem(apiKey) !== filteredValue && api.setItem(apiKey, filteredValue, props == null ? void 0 : props.options) | ||
(api) => api.getItem(apiKey) !== filteredValue && api.setItem(apiKey, filteredValue, props?.options) | ||
); | ||
@@ -382,3 +367,3 @@ } | ||
); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
@@ -392,3 +377,3 @@ } | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
@@ -405,3 +390,3 @@ let changed = false; | ||
setError(err instanceof Error ? err : new Error("Error synching api after event")); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
@@ -416,10 +401,4 @@ } | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -463,8 +442,4 @@ }); | ||
_cookies: [globalThis.document, "cookie"], | ||
getItem: (key) => { | ||
var _a; | ||
return ((_a = cookieStorage._cookies[0][cookieStorage._cookies[1]].match("(^|;)\\s*" + key + "\\s*=\\s*([^;]+)")) == null ? void 0 : _a.pop()) ?? null; | ||
}, | ||
getItem: (key) => cookieStorage._cookies[0][cookieStorage._cookies[1]].match("(^|;)\\s*" + key + "\\s*=\\s*([^;]+)")?.pop() ?? null, | ||
setItem: (key, value, options) => { | ||
var _a; | ||
const oldValue = cookieStorage.getItem(key); | ||
@@ -478,3 +453,3 @@ cookieStorage._cookies[0][cookieStorage._cookies[1]] = `${key}=${value}${serializeCookieOptions( | ||
newValue: value, | ||
url: (_a = globalThis.document) == null ? void 0 : _a.URL, | ||
url: globalThis.document?.URL, | ||
storageArea: cookieStorage | ||
@@ -481,0 +456,0 @@ }); |
import { Accessor, Setter } from 'solid-js'; | ||
declare type StorageWithOptions<O> = { | ||
type StorageWithOptions<O> = { | ||
clear: () => void; | ||
@@ -15,5 +15,5 @@ getItem: (key: string, options?: O) => string | null; | ||
}; | ||
declare type StorageDeserializer<T, O> = (value: string, key: string, options?: O) => T; | ||
declare type StorageSerializer<T, O> = (value: T, key: string, options?: O) => string; | ||
declare type AnyStorageProps<A, O, T> = { | ||
type StorageDeserializer<T, O> = (value: string, key: string, options?: O) => T; | ||
type StorageSerializer<T, O> = (value: T, key: string, options?: O) => string; | ||
type AnyStorageProps<A, O, T> = { | ||
/** a Storage-like API, e.g. localStorage */ | ||
@@ -34,7 +34,7 @@ api?: A | A[]; | ||
}; | ||
declare type StringStorageProps<A, O, T = string> = AnyStorageProps<A, O, T>; | ||
declare type StorageProps<T, A, O> = T extends string ? StringStorageProps<A, O> : AnyStorageProps<A, O, T>; | ||
declare type StorageObject<T> = Record<string, T>; | ||
declare type StorageSetter<T, O> = (key: string, value: T, options?: O) => void; | ||
declare type StorageActions<T> = { | ||
type StringStorageProps<A, O, T = string> = AnyStorageProps<A, O, T>; | ||
type StorageProps<T, A, O> = T extends string ? StringStorageProps<A, O> : AnyStorageProps<A, O, T>; | ||
type StorageObject<T> = Record<string, T>; | ||
type StorageSetter<T, O> = (key: string, value: T, options?: O) => void; | ||
type StorageActions<T> = { | ||
remove: (key: string) => void; | ||
@@ -47,3 +47,3 @@ clear: () => void; | ||
}; | ||
declare type AsyncStorage = { | ||
type AsyncStorage = { | ||
clear?: () => Promise<void> | void; | ||
@@ -58,3 +58,3 @@ getItem: (key: string) => Promise<string | null> | string | null; | ||
}; | ||
declare type AsyncStorageWithOptions<O> = { | ||
type AsyncStorageWithOptions<O> = { | ||
clear?: () => Promise<void> | void; | ||
@@ -69,5 +69,5 @@ getItem: (key: string, options?: O) => Promise<string | null> | string | null; | ||
}; | ||
declare type AsyncStorageObject<T> = Record<string, Promise<T | null>>; | ||
declare type AsyncStorageSetter<T, O> = (key: string, value: T, options?: O) => Promise<void> | void; | ||
declare type AsyncStorageActions<T> = { | ||
type AsyncStorageObject<T> = Record<string, Promise<T | null>>; | ||
type AsyncStorageSetter<T, O> = (key: string, value: T, options?: O) => Promise<void> | void; | ||
type AsyncStorageActions<T> = { | ||
remove: (key: string) => Promise<void> | void; | ||
@@ -80,3 +80,3 @@ clear: () => Promise<void> | void; | ||
}; | ||
declare type StorageSignalProps<T, A, O> = StorageProps<T, A, O> & { | ||
type StorageSignalProps<T, A, O> = StorageProps<T, A, O> & { | ||
/** signal equality checker */ | ||
@@ -181,3 +181,3 @@ equals?: false | ((prev: T, next: T) => boolean); | ||
declare type CookieOptions = { | ||
type CookieOptions = { | ||
domain?: string; | ||
@@ -184,0 +184,0 @@ expires?: Date | number | String; |
@@ -6,3 +6,3 @@ import { createSignal, onMount, onCleanup, createEffect } from 'solid-js'; | ||
const [error, setError] = createSignal(); | ||
const handleError = (props == null ? void 0 : props.throw) ? (err, fallback) => { | ||
const handleError = props?.throw ? (err, fallback) => { | ||
setError(err instanceof Error ? err : new Error(fallback)); | ||
@@ -13,4 +13,4 @@ throw err; | ||
}; | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const signals = /* @__PURE__ */ new Map(); | ||
@@ -41,4 +41,4 @@ const store = new Proxy( | ||
); | ||
if (value !== null && (props == null ? void 0 : props.deserializer)) { | ||
return props.deserializer(value, key, props == null ? void 0 : props.options); | ||
if (value !== null && props?.deserializer) { | ||
return props.deserializer(value, key, props?.options); | ||
} | ||
@@ -50,6 +50,6 @@ return value; | ||
const setter = (key, value, options) => { | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer( | ||
const filteredValue = props?.serializer ? props.serializer( | ||
value, | ||
key, | ||
options ?? (props == null ? void 0 : props.options) | ||
options ?? props?.options | ||
) : value; | ||
@@ -75,5 +75,4 @@ const apiKey = `${prefix}${key}`; | ||
const clear = () => apis.forEach((api) => { | ||
var _a; | ||
try { | ||
(_a = api == null ? void 0 : api.clear) == null ? void 0 : _a.call(api); | ||
api?.clear?.(); | ||
} catch (err) { | ||
@@ -87,3 +86,3 @@ handleError(err, `Error clearing ${api.name}`); | ||
if (!result.hasOwnProperty(key)) { | ||
const filteredValue = value && (props == null ? void 0 : props.deserializer) ? props.deserializer(value, key, props == null ? void 0 : props.options) : value; | ||
const filteredValue = value && props?.deserializer ? props.deserializer(value, key, props?.options) : value; | ||
if (filteredValue) { | ||
@@ -120,5 +119,4 @@ result[key] = filteredValue; | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
var _a; | ||
let changed = false; | ||
@@ -138,3 +136,3 @@ apis.forEach((api) => { | ||
}); | ||
changed && ev.key && ((_a = signals.get(ev.key)) == null ? void 0 : _a[1]()); | ||
changed && ev.key && signals.get(ev.key)?.[1](); | ||
}; | ||
@@ -145,10 +143,4 @@ if ("addEventListener" in globalThis) { | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -169,3 +161,3 @@ }); | ||
const [error, setError] = createSignal(); | ||
const handleError = (props == null ? void 0 : props.throw) ? (err, fallback) => { | ||
const handleError = props?.throw ? (err, fallback) => { | ||
setError(err instanceof Error ? err : new Error(fallback)); | ||
@@ -176,4 +168,4 @@ throw err; | ||
}; | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : []; | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : []; | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const signals = /* @__PURE__ */ new Map(); | ||
@@ -200,6 +192,6 @@ const store = new Proxy({}, { | ||
return value.then( | ||
(value2) => value2 && (props == null ? void 0 : props.deserializer) ? props.deserializer(value2, key, props == null ? void 0 : props.options) : value2 | ||
(value2) => value2 && props?.deserializer ? props.deserializer(value2, key, props?.options) : value2 | ||
); | ||
} | ||
return value !== null && (props == null ? void 0 : props.deserializer) ? Promise.resolve(props.deserializer(value, key, props == null ? void 0 : props.options)) : Promise.resolve(value); | ||
return value !== null && props?.deserializer ? Promise.resolve(props.deserializer(value, key, props?.options)) : Promise.resolve(value); | ||
}, null); | ||
@@ -209,3 +201,3 @@ } | ||
const setter = (key, value, options) => { | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer(value, key, options ?? (props == null ? void 0 : props.options)) : value; | ||
const filteredValue = props?.serializer ? props.serializer(value, key, options ?? props?.options) : value; | ||
return Promise.all( | ||
@@ -217,3 +209,3 @@ apis.map((api) => { | ||
filteredValue, | ||
options ?? (props == null ? void 0 : props.options) | ||
options ?? props?.options | ||
); | ||
@@ -261,3 +253,3 @@ } catch (err) { | ||
if (!result.hasOwnProperty(key)) { | ||
const filteredValue = value && (props == null ? void 0 : props.deserializer) ? props.deserializer(value, key, props == null ? void 0 : props.options) : value; | ||
const filteredValue = value && props?.deserializer ? props.deserializer(value, key, props?.options) : value; | ||
if (filteredValue) { | ||
@@ -293,5 +285,4 @@ result[key] = filteredValue; | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
var _a; | ||
let changed = false; | ||
@@ -308,3 +299,3 @@ apis.forEach(async (api) => { | ||
}); | ||
changed && ev.key && ((_a = signals.get(ev.key)) == null ? void 0 : _a[1]()); | ||
changed && ev.key && signals.get(ev.key)?.[1](); | ||
}; | ||
@@ -315,10 +306,4 @@ if ("addEventListener" in globalThis) { | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -339,4 +324,4 @@ }); | ||
const [error, setError] = createSignal(); | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const read = () => apis.reduce((result, api) => { | ||
@@ -353,8 +338,8 @@ if (result !== null || !api) { | ||
); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
} | ||
} | ||
if (value !== null && (props == null ? void 0 : props.deserializer)) { | ||
return props.deserializer(value + "", key, props == null ? void 0 : props.options); | ||
if (value !== null && props?.deserializer) { | ||
return props.deserializer(value + "", key, props?.options); | ||
} | ||
@@ -366,3 +351,3 @@ return value; | ||
const value = accessor(); | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer(value, key, props == null ? void 0 : props.options) : value + ""; | ||
const filteredValue = props?.serializer ? props.serializer(value, key, props?.options) : value + ""; | ||
const apiKey = `${prefix}${key}`; | ||
@@ -374,3 +359,3 @@ try { | ||
apis.forEach( | ||
(api) => api.getItem(apiKey) !== filteredValue && api.setItem(apiKey, filteredValue, props == null ? void 0 : props.options) | ||
(api) => api.getItem(apiKey) !== filteredValue && api.setItem(apiKey, filteredValue, props?.options) | ||
); | ||
@@ -382,3 +367,3 @@ } | ||
); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
@@ -392,3 +377,3 @@ } | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
@@ -405,3 +390,3 @@ let changed = false; | ||
setError(err instanceof Error ? err : new Error("Error synching api after event")); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
@@ -416,10 +401,4 @@ } | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -463,8 +442,4 @@ }); | ||
_cookies: [globalThis.document, "cookie"], | ||
getItem: (key) => { | ||
var _a; | ||
return ((_a = cookieStorage._cookies[0][cookieStorage._cookies[1]].match("(^|;)\\s*" + key + "\\s*=\\s*([^;]+)")) == null ? void 0 : _a.pop()) ?? null; | ||
}, | ||
getItem: (key) => cookieStorage._cookies[0][cookieStorage._cookies[1]].match("(^|;)\\s*" + key + "\\s*=\\s*([^;]+)")?.pop() ?? null, | ||
setItem: (key, value, options) => { | ||
var _a; | ||
const oldValue = cookieStorage.getItem(key); | ||
@@ -478,3 +453,3 @@ cookieStorage._cookies[0][cookieStorage._cookies[1]] = `${key}=${value}${serializeCookieOptions( | ||
newValue: value, | ||
url: (_a = globalThis.document) == null ? void 0 : _a.URL, | ||
url: globalThis.document?.URL, | ||
storageArea: cookieStorage | ||
@@ -481,0 +456,0 @@ }); |
@@ -6,3 +6,3 @@ import { createSignal, onMount, onCleanup, createEffect } from 'solid-js'; | ||
const [error, setError] = createSignal(); | ||
const handleError = (props == null ? void 0 : props.throw) ? (err, fallback) => { | ||
const handleError = props?.throw ? (err, fallback) => { | ||
setError(err instanceof Error ? err : new Error(fallback)); | ||
@@ -13,4 +13,4 @@ throw err; | ||
}; | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const signals = /* @__PURE__ */ new Map(); | ||
@@ -41,4 +41,4 @@ const store = new Proxy( | ||
); | ||
if (value !== null && (props == null ? void 0 : props.deserializer)) { | ||
return props.deserializer(value, key, props == null ? void 0 : props.options); | ||
if (value !== null && props?.deserializer) { | ||
return props.deserializer(value, key, props?.options); | ||
} | ||
@@ -50,6 +50,6 @@ return value; | ||
const setter = (key, value, options) => { | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer( | ||
const filteredValue = props?.serializer ? props.serializer( | ||
value, | ||
key, | ||
options ?? (props == null ? void 0 : props.options) | ||
options ?? props?.options | ||
) : value; | ||
@@ -75,5 +75,4 @@ const apiKey = `${prefix}${key}`; | ||
const clear = () => apis.forEach((api) => { | ||
var _a; | ||
try { | ||
(_a = api == null ? void 0 : api.clear) == null ? void 0 : _a.call(api); | ||
api?.clear?.(); | ||
} catch (err) { | ||
@@ -87,3 +86,3 @@ handleError(err, `Error clearing ${api.name}`); | ||
if (!result.hasOwnProperty(key)) { | ||
const filteredValue = value && (props == null ? void 0 : props.deserializer) ? props.deserializer(value, key, props == null ? void 0 : props.options) : value; | ||
const filteredValue = value && props?.deserializer ? props.deserializer(value, key, props?.options) : value; | ||
if (filteredValue) { | ||
@@ -120,5 +119,4 @@ result[key] = filteredValue; | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
var _a; | ||
let changed = false; | ||
@@ -138,3 +136,3 @@ apis.forEach((api) => { | ||
}); | ||
changed && ev.key && ((_a = signals.get(ev.key)) == null ? void 0 : _a[1]()); | ||
changed && ev.key && signals.get(ev.key)?.[1](); | ||
}; | ||
@@ -145,10 +143,4 @@ if ("addEventListener" in globalThis) { | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -169,3 +161,3 @@ }); | ||
const [error, setError] = createSignal(); | ||
const handleError = (props == null ? void 0 : props.throw) ? (err, fallback) => { | ||
const handleError = props?.throw ? (err, fallback) => { | ||
setError(err instanceof Error ? err : new Error(fallback)); | ||
@@ -176,4 +168,4 @@ throw err; | ||
}; | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : []; | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : []; | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const signals = /* @__PURE__ */ new Map(); | ||
@@ -200,6 +192,6 @@ const store = new Proxy({}, { | ||
return value.then( | ||
(value2) => value2 && (props == null ? void 0 : props.deserializer) ? props.deserializer(value2, key, props == null ? void 0 : props.options) : value2 | ||
(value2) => value2 && props?.deserializer ? props.deserializer(value2, key, props?.options) : value2 | ||
); | ||
} | ||
return value !== null && (props == null ? void 0 : props.deserializer) ? Promise.resolve(props.deserializer(value, key, props == null ? void 0 : props.options)) : Promise.resolve(value); | ||
return value !== null && props?.deserializer ? Promise.resolve(props.deserializer(value, key, props?.options)) : Promise.resolve(value); | ||
}, null); | ||
@@ -209,3 +201,3 @@ } | ||
const setter = (key, value, options) => { | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer(value, key, options ?? (props == null ? void 0 : props.options)) : value; | ||
const filteredValue = props?.serializer ? props.serializer(value, key, options ?? props?.options) : value; | ||
return Promise.all( | ||
@@ -217,3 +209,3 @@ apis.map((api) => { | ||
filteredValue, | ||
options ?? (props == null ? void 0 : props.options) | ||
options ?? props?.options | ||
); | ||
@@ -261,3 +253,3 @@ } catch (err) { | ||
if (!result.hasOwnProperty(key)) { | ||
const filteredValue = value && (props == null ? void 0 : props.deserializer) ? props.deserializer(value, key, props == null ? void 0 : props.options) : value; | ||
const filteredValue = value && props?.deserializer ? props.deserializer(value, key, props?.options) : value; | ||
if (filteredValue) { | ||
@@ -293,5 +285,4 @@ result[key] = filteredValue; | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
var _a; | ||
let changed = false; | ||
@@ -308,3 +299,3 @@ apis.forEach(async (api) => { | ||
}); | ||
changed && ev.key && ((_a = signals.get(ev.key)) == null ? void 0 : _a[1]()); | ||
changed && ev.key && signals.get(ev.key)?.[1](); | ||
}; | ||
@@ -315,10 +306,4 @@ if ("addEventListener" in globalThis) { | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -339,4 +324,4 @@ }); | ||
const [error, setError] = createSignal(); | ||
const apis = (props == null ? void 0 : props.api) ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = (props == null ? void 0 : props.prefix) ? `${props.prefix}.` : ""; | ||
const apis = props?.api ? Array.isArray(props.api) ? props.api : [props.api] : [globalThis.localStorage].filter(Boolean); | ||
const prefix = props?.prefix ? `${props.prefix}.` : ""; | ||
const read = () => apis.reduce((result, api) => { | ||
@@ -353,8 +338,8 @@ if (result !== null || !api) { | ||
); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
} | ||
} | ||
if (value !== null && (props == null ? void 0 : props.deserializer)) { | ||
return props.deserializer(value + "", key, props == null ? void 0 : props.options); | ||
if (value !== null && props?.deserializer) { | ||
return props.deserializer(value + "", key, props?.options); | ||
} | ||
@@ -366,3 +351,3 @@ return value; | ||
const value = accessor(); | ||
const filteredValue = (props == null ? void 0 : props.serializer) ? props.serializer(value, key, props == null ? void 0 : props.options) : value + ""; | ||
const filteredValue = props?.serializer ? props.serializer(value, key, props?.options) : value + ""; | ||
const apiKey = `${prefix}${key}`; | ||
@@ -374,3 +359,3 @@ try { | ||
apis.forEach( | ||
(api) => api.getItem(apiKey) !== filteredValue && api.setItem(apiKey, filteredValue, props == null ? void 0 : props.options) | ||
(api) => api.getItem(apiKey) !== filteredValue && api.setItem(apiKey, filteredValue, props?.options) | ||
); | ||
@@ -382,3 +367,3 @@ } | ||
); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
@@ -392,3 +377,3 @@ } | ||
}; | ||
(props == null ? void 0 : props.sync) !== false && onMount(() => { | ||
props?.sync !== false && onMount(() => { | ||
const listener = (ev) => { | ||
@@ -405,3 +390,3 @@ let changed = false; | ||
setError(err instanceof Error ? err : new Error("Error synching api after event")); | ||
if (props == null ? void 0 : props.throw) { | ||
if (props?.throw) { | ||
throw err; | ||
@@ -416,10 +401,4 @@ } | ||
} else { | ||
apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.addEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
}); | ||
onCleanup(() => apis.forEach((api) => { | ||
var _a; | ||
return (_a = api.removeEventListener) == null ? void 0 : _a.call(api, "storage", listener); | ||
})); | ||
apis.forEach((api) => api.addEventListener?.("storage", listener)); | ||
onCleanup(() => apis.forEach((api) => api.removeEventListener?.("storage", listener))); | ||
} | ||
@@ -463,8 +442,4 @@ }); | ||
_cookies: [globalThis.document, "cookie"], | ||
getItem: (key) => { | ||
var _a; | ||
return ((_a = cookieStorage._cookies[0][cookieStorage._cookies[1]].match("(^|;)\\s*" + key + "\\s*=\\s*([^;]+)")) == null ? void 0 : _a.pop()) ?? null; | ||
}, | ||
getItem: (key) => cookieStorage._cookies[0][cookieStorage._cookies[1]].match("(^|;)\\s*" + key + "\\s*=\\s*([^;]+)")?.pop() ?? null, | ||
setItem: (key, value, options) => { | ||
var _a; | ||
const oldValue = cookieStorage.getItem(key); | ||
@@ -478,3 +453,3 @@ cookieStorage._cookies[0][cookieStorage._cookies[1]] = `${key}=${value}${serializeCookieOptions( | ||
newValue: value, | ||
url: (_a = globalThis.document) == null ? void 0 : _a.URL, | ||
url: globalThis.document?.URL, | ||
storageArea: cookieStorage | ||
@@ -481,0 +456,0 @@ }); |
{ | ||
"name": "@solid-primitives/storage", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "Primitive that provides reactive wrappers for storage access", | ||
@@ -34,4 +34,4 @@ "author": "Alex Lohr <alex.lohr@logmein.com>", | ||
"browser": { | ||
"./dist/server.cjs": "./dist/index.cjs", | ||
"./dist/server.js": "./dist/index.js" | ||
"./dist/server.js": "./dist/index.js", | ||
"./dist/server.cjs": "./dist/index.cjs" | ||
}, | ||
@@ -41,3 +41,6 @@ "types": "./dist/index.d.ts", | ||
"worker": { | ||
"import": "./dist/server.js", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/server.js" | ||
}, | ||
"require": "./dist/server.cjs" | ||
@@ -47,21 +50,39 @@ }, | ||
"development": { | ||
"import": "./dist/dev.js", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/dev.js" | ||
}, | ||
"require": "./dist/dev.cjs" | ||
}, | ||
"import": "./dist/index.js", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"require": "./dist/index.cjs" | ||
}, | ||
"deno": { | ||
"import": "./dist/server.js", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/server.js" | ||
}, | ||
"require": "./dist/server.cjs" | ||
}, | ||
"node": { | ||
"import": "./dist/server.js", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/server.js" | ||
}, | ||
"require": "./dist/server.cjs" | ||
}, | ||
"development": { | ||
"import": "./dist/dev.js", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/dev.js" | ||
}, | ||
"require": "./dist/dev.cjs" | ||
}, | ||
"import": "./dist/index.js", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"require": "./dist/index.cjs" | ||
@@ -76,8 +97,6 @@ }, | ||
], | ||
"devDependencies": { | ||
"solid-js": "^1.5.0" | ||
}, | ||
"peerDependencies": { | ||
"solid-js": "^1.5.0" | ||
"solid-js": "^1.6.0" | ||
}, | ||
"typesVersions": {}, | ||
"scripts": { | ||
@@ -84,0 +103,0 @@ "build": "jiti ../../scripts/build.ts --ssr --dev", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
0
106109
2978