{"name":"@solid-primitives/storage","version":"1.3.7-beta.0","keywords":["localStorage","sessionStorage","cookies","solid","primitives"],"author":{"name":"Alex Lohr","email":"alex.lohr@logmein.com"},"license":"MIT","_id":"@solid-primitives/storage@1.3.7-beta.0","maintainers":[{"name":"thetarnav.","email":"gthetarnav@gmail.com"},{"name":"lexlohr","email":"alex.th.kloss.77@googlemail.com"},{"name":"davedbase","email":"dave.dibiase@gmail.com"}],"contributors":[{"name":"Tom Pichaud","email":"dev.tompichaud@icloud.com"}],"homepage":"https://github.com/solidjs-community/solid-primitives/tree/main/packages/storage","bugs":{"url":"https://github.com/solidjs-community/solid-primitives/issues"},"dist":{"shasum":"7fe9ac5675f61ca523d0d9e1818b78ee9acd5067","tarball":"http://registry-npm.ywwl.com/@solid-primitives/storage/download/@solid-primitives/storage-1.3.7-beta.0.tgz","fileCount":11,"integrity":"sha512-C3usDxLARh3ORWPaWp2wkH0pNT5ZR0TVxRvPFWJKXqEtkIqe+Bjh1h8nJazblANMOtmwu4wMzhGYQBkOO437Yg==","signatures":[{"sig":"MEQCICt8MqIRPCy5GlhegQP8bGu1Ad33SWJHmm2IULi2IwoSAiBwdzKMGo7Il3eke58EisUmBurcFCul785FtEjPyJxD9A==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":121257,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkAbSRACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrD2Q//SA/TiieDrNYx2oOAWQ2IlJeDsaspl1NpVkVZBQJv1UBpoZgr\r\nKlok3B56I4rTbd/HZLbc8TMVC4bpqUjm/fEb9xqt217Rf8bkNFTTQ1yxqqav\r\nymJVRYfBmWZxVz5cOfX+2U6Kp/3eKdkBStNxWh3/oaL/tkKD/Q2QOOiyDOHO\r\nr2M+cG6eEjg0lwEiTbZmFWevhyCGjMNJs1c5lQOWeMrFO/yEgxqQ9hvqsYqW\r\ndzZ1ByzjhC1rcx6hX5gtXc+9uLAWxuPYlZ1euJXveNPey0Fcis43C/RQ9Lgw\r\nBQeITN6RGTa/h7oKit1tgm9iodsfIx5BUe3N+yKK3JnMePCA6TBVO7v9RD/T\r\nm/MvuZbid59cDGPmIyNkh7wcXG8hzyunNevcMfFqAz6KTx785GRjxCzdFHyf\r\nPcNNnX68noUY4XCpuo2Rlz7GYnbC9zDm4F6Q3COfrXiAfPscvUPPGNCrljYB\r\nC3rxN65bgMFswxJdIkhRalbyhQSwrRrkHxZfrMnHTUc00fGlTnGIixYs2Mi5\r\no9q9WSr3NHVfbp4+LNq9O2JydVuE18U2SRE4SeXDL6S3dWbssoBF1dLqujv9\r\nWYakDsD9z6Y3wXXheq2PLjwBgAmGm4kRW35LFWOL+yG2lS08AR99zC2w3xbH\r\ndFGWH+ucIS55Ig/34ZMT5wJD1jRyPm3Qq/c=\r\n=f1EU\r\n-----END PGP SIGNATURE-----\r\n","size":9674,"noattachment":false,"key":"/@solid-primitives/storage/-/@solid-primitives/storage-1.3.7-beta.0.tgz"},"main":"./dist/server.cjs","type":"module","_from":"file:solid-primitives-storage-1.3.7-beta.0.tgz","types":"./dist/index.d.ts","module":"./dist/server.js","readme":"<p>\r\n  <img width=\"100%\" src=\"https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Storage\" alt=\"Solid Primitives Storage\">\r\n</p>\r\n\r\n# @solid-primitives/storage\r\n\r\n[![turborepo](https://img.shields.io/badge/built%20with-turborepo-cc00ff.svg?style=for-the-badge&logo=turborepo)](https://turborepo.org/)\r\n[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/storage?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/storage)\r\n[![size](https://img.shields.io/npm/v/@solid-primitives/storage?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/storage)\r\n[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)\r\n\r\nCreates a primitive to reactively access both synchronous and asynchronous persistent storage APIs similar to localStorage.\r\n\r\n## Installation\r\n\r\n```\r\nnpm install @solid-primitives/storage\r\n# or\r\nyarn add @solid-primitives/storage\r\n```\r\n\r\n## How to use it\r\n\r\n### Basic Usage\r\n\r\n`createStorage` is meant to wrap any localStorage-like API to be as accessible as a [Solid Store](https://www.solidjs.com/docs/latest/api#createstore). The main differences are\r\n\r\n- that this store is persisted in whatever API is used,\r\n- that you can only use the topmost layer of the object and\r\n- that you have additional methods in an object as the third part of the returned tuple:\r\n\r\n```ts\r\nconst [store, setStore, {\r\n  remove: (key: string) => void;\r\n  clear: () => void;\r\n  toJSON: () => ({ [key: string]: string });\r\n}] = createStorage({ api: sessionStorage, prefix: 'my-app' });\r\n\r\nsetStore('key', 'value');\r\nstore.key; // 'value'\r\n```\r\n\r\nThe props object support the following parameters:\r\n\r\n`api`\r\n: an array of or a single localStorage-like storage API; default will be localStorage if it exists; an empty array or no API will not throw an error, but only ever get `null` and not actually persist anything\r\n\r\n`prefix`\r\n: a string that will be prefixed every key inside the API on set and get operations\r\n\r\n`serializer / deserializer`\r\n: a set of function to filter the input and output; the serializer takes an arbitrary object and returns a string, e.g. JSON.stringify, whereas the deserializer takes a string and returns the requested object again.\r\n\r\n`options`\r\n: for APIs that support options as third argument in the `getItem` and `setItem` method (see helper type `StorageWithOptions<O>`), you can add options they will receive on every operation.\r\n\r\n---\r\n\r\nThere are a number of convenience Methods primed with common storage APIs and our own version to use cookies:\r\n\r\n```ts\r\ncreateLocalStorage();\r\ncreateSessionStorage();\r\ncreateCookieStorage();\r\n```\r\n\r\n---\r\n\r\n### Asynchronous storage APIs\r\n\r\nIn case you have APIs that persist data on the server or via ServiceWorker in a [CookieStore](https://wicg.github.io/cookie-store/#CookieStore), you can wrap them into an `AsyncStorage` or `AsyncStorageWithOptions` API and use them with `createAsyncStorage`:\r\n\r\n```ts\r\ntype CookieStoreOptions = {\r\n  path: string;\r\n  domain: string;\r\n  expires: DOMTimeStamp;\r\n  sameSite: \"None\" | \"Lax\" | \"Strict\"\r\n}\r\nconst CookieStoreAPI: AsyncStorageWithOptions<CookieStoreOptions> = {\r\n  getItem: (key) => cookieStore.get(key),\r\n  getAll: () => cookieStore.getAll(),\r\n  setItem: (key: string, value: string, options: CookieStoreOptions = {}) => cookieStore.set({\r\n    ...options, name, value\r\n  }),\r\n  removeItem: (key) => cookieStore.delete(key),\r\n  clear: async () => {\r\n    const all = await cookieStore.getAll();\r\n    for (const key of all) {\r\n      await cookieStore.delete(key);\r\n    }\r\n  },\r\n  key: async (index: number) => {\r\n    const all = await cookieStore.getAll();\r\n    return Object.keys(all)[index];\r\n  }\r\n});\r\n\r\nconst [cookies, setCookie, {\r\n    remove: (key: string) => void;\r\n    clear: () => void;\r\n    toJSON: () => ({ [key: string]: string });\r\n}] = createAsyncStorage({ api: CookieStoreAPI, prefix: 'my-app', sync: false });\r\n\r\nawait setStore('key', 'value');\r\nawait store.key; // 'value'\r\n```\r\n\r\nIt works exactly like a synchronous storage, with the exception that you have to `await` every single return value. Once the CookieStore API becomes more prevalent, we will integrate support out of the box.\r\n\r\nIf you cannot use document.cookie, you can overwrite the entry point using the following tuple:\r\n\r\n```ts\r\nimport { cookieStorage } from '@solid-primitives/storage';\r\n\r\ncookieStorage._cookies = [object: { [name: string]: CookieProxy }, name: string];\r\n```\r\n\r\nIf you need to abstract an API yourself, you can use a getter and a setter:\r\n\r\n```ts\r\nconst CookieAbstraction = {\r\n  get cookie() { return myCookieJar.toString() }\r\n  set cookie(cookie) {\r\n    const data = {};\r\n    cookie.replace(/([^=]+)=(?:([^;]+);?)/g, (_, key, value) => { data[key] = value });\r\n    myCookieJar.set(data);\r\n  }\r\n}\r\ncookieStorage._cookies = [CookieAbstraction, 'cookie'];\r\n```\r\n\r\n---\r\n\r\n`createStorageSignal` is meant for those cases when you only need to conveniently access a single value instead of full access to the storage API:\r\n\r\n```ts\r\nconst [value, setValue] = createStorageSignal(\"value\", { api: cookieStorage });\r\n\r\nsetValue(\"value\");\r\nvalue(); // 'value'\r\n```\r\n\r\nAs a convenient additional method, you can also use `createCookieStorageSignal(key, initialValue, options)`.\r\n\r\n---\r\n\r\n### Options\r\n\r\nThe properties of your `createStorage`/`createAsyncStorage`/`createStorageSignal` props are:\r\n\r\n- `api`: the (sync or async) [Storage-like API](https://developer.mozilla.org/de/docs/Web/API/Web_Storage_API), default is localStorage\r\n- `deserializer` (optional): a deserializer or parser for the stored data\r\n- `serializer` (optional): a serializer or string converter for the stored data\r\n- `options` (optional): default options for the set-call of Storage-like API, if supported\r\n- `prefix` (optional): a prefix for the Storage keys\r\n- `sync` (optional): if set to false, [event synchronization](https://developer.mozilla.org/en-US/docs/Web/API/StorageEvent) is disabled\r\n\r\n### Tools\r\n\r\nIf you want to build your own Storage and don't want to do a .clear() method youself:\r\n\r\n```ts\r\nconst storageWithClearMethod = addClearMethod(storage_without_clear_method);\r\n```\r\n\r\n## Demo\r\n\r\nTODO\r\n\r\n## Changelog\r\n\r\nSee [CHANGELOG.md](./CHANGELOG.md)\r\n","browser":{"./dist/server.js":"./dist/index.js","./dist/server.cjs":"./dist/index.cjs"},"exports":{"deno":{"import":{"types":"./dist/index.d.ts","default":"./dist/server.js"},"require":"./dist/server.cjs"},"node":{"import":{"types":"./dist/index.d.ts","default":"./dist/server.js"},"require":"./dist/server.cjs"},"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"worker":{"import":{"types":"./dist/index.d.ts","default":"./dist/server.js"},"require":"./dist/server.cjs"},"browser":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":"./dist/index.cjs","development":{"import":{"types":"./dist/index.d.ts","default":"./dist/dev.js"},"require":"./dist/dev.cjs"}},"require":"./dist/index.cjs","development":{"import":{"types":"./dist/index.d.ts","default":"./dist/dev.js"},"require":"./dist/dev.cjs"}},"private":false,"scripts":{"test":"vitest -c ../../configs/vitest.config.ts","build":"jiti ../../scripts/build.ts --ssr --dev","test:ssr":"pnpm run test --mode ssr"},"_npmUser":{"name":"thetarnav.","email":"gthetarnav@gmail.com"},"_resolved":"C:\\Users\\gthet\\AppData\\Local\\Temp\\95d0dd38fb270786f6190871048ce5bc\\solid-primitives-storage-1.3.7-beta.0.tgz","primitive":{"list":["createStorage","createCookieStorage","createAsyncStorage","createStorageSignal","createLocalStorage","createSessionStorage"],"name":"storage","stage":3,"category":"Browser APIs"},"_integrity":"sha512-C3usDxLARh3ORWPaWp2wkH0pNT5ZR0TVxRvPFWJKXqEtkIqe+Bjh1h8nJazblANMOtmwu4wMzhGYQBkOO437Yg==","repository":{"url":"git+https://github.com/solidjs-community/solid-primitives.git","type":"git"},"_npmVersion":"8.19.2","description":"Primitive that provides reactive wrappers for storage access","directories":{},"sideEffects":false,"_nodeVersion":"18.12.1","dependencies":{"@solid-primitives/utils":"^5.4.0-beta.0"},"typesVersions":{},"_hasShrinkwrap":false,"readmeFilename":"README.md","peerDependencies":{"solid-js":"^1.6.0"},"_npmOperationalInternal":{"tmp":"tmp/storage_1.3.7-beta.0_1677833361620_0.3654266484012805","host":"s3://npm-registry-packages"},"publish_time":1677833361800,"_cnpm_publish_time":1677833361800,"dist-tags":{"beta":"1.3.7-beta.0","latest":"4.4.0","next":"5.0.0-next.8"}}