{"_id":"@alehuo/type-safe-storage","_rev":"2-364b40b11dfa1c9fad22dab83a28ccd2","name":"@alehuo/type-safe-storage","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@alehuo/type-safe-storage","version":"1.0.0","keywords":[],"author":{"name":"Aleksi Huotala","email":"aleksi.huotala@reaktor.com"},"license":"MIT","_id":"@alehuo/type-safe-storage@1.0.0","maintainers":[{"name":"alehuo","email":"alehuo@pm.me"}],"dist":{"shasum":"709e48958f58fc63a0a44d16f7d8ecd08eb6dadd","tarball":"https://registry.npmjs.org/@alehuo/type-safe-storage/-/type-safe-storage-1.0.0.tgz","fileCount":8,"integrity":"sha512-raMID8ahcqg3RkOotS3C3uukET1thxoCvDOfAzaVlMEG5LU10AdRcmQil+/GaY8PpN+D/q8P7slTQZr7HPFW2w==","signatures":[{"sig":"MEYCIQD08E2ZgaynKHP2E+I5ZNNY/phZuZEduRitCgwe4QNZ/QIhAPPGOxFbnRdptoCpyVebcyCiRsU6vrYcRFlft367JrnK","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":7311,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgThVwCRA9TVsSAnZWagAA2acP/A2Z0X2/vfeLwShZ6S9z\nmkxBfZTRh8aAyYRm/HGN5XMxb/TEL8vskhPtfqdtR0UWH0khVcvQF0E5pMUF\n2cbaMocxbhn5UciTGUjeWuFZ3lsvResgI6ZDTWBm8pfTY517KVfx1KSXaHvr\nOA5ST3MK8fr42Q/04d/o/0Cuy7UahubPV0dXaXyvLzW3pzuziuVg2URw0oaL\nStCCyBq+6zHQxp9wCJzUboZQPx+pKpPUVKhBEgapSeSqXigU1NAb/iAs/9rU\njVQzTsnAT0Fx+P9C+0Hxg3plxSr8gtTOk07LL6QfmEE7NOpIe3rVDL37V5Y+\nVTFBg6v2CWDd3XT5XH9m3bxQ03cBIKnrdiVvYpJbIDVpT0dH8hkW9HnZ99+9\nTFClZRWr8W6ADwOXnDrIJm5hhjbMiD5CA6hTrpNY7WODbNgXFKK7Kk2qWW16\nlUpvhPoeIauCwFI87l2aRPD9PqCRneeUngHHS7X+bCeXqyWLg+ThKjVJjENN\n1Qv/lp30HnZ7S856QqKMhbmiOUciKvrNqHnxWAFjZI/8obr/rDr7vu+jt3KE\nvShV75mCEosP/m9HX21lB1kF8HRYZhoGwQ082UltufSGzmqoTQE/X0432+Qv\n4FSkBXguQkiALMkhy3i8nyVf5St3ewLFtlZpAhB6TDKj41BJ2wERb6uqQa2h\nFmk2\r\n=d6ZI\r\n-----END PGP SIGNATURE-----\r\n"},"main":"dist/index.js","gitHead":"ff9754bc6545b1a8dc28d8e8531f4ef402288305","scripts":{"test":"jest","build":"tsc"},"typings":"dist/index.d.ts","_npmUser":{"name":"alehuo","email":"alehuo@pm.me"},"_npmVersion":"7.6.0","description":"## Introduction","directories":{},"_nodeVersion":"15.11.0","dependencies":{"fp-ts":"^2.9.5","io-ts":"^2.2.14"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^26.6.3","ts-node":"^9.1.1","webpack":"^5.23.0","typescript":"^4.1.5","@types/jest":"^26.0.20","@babel/preset-env":"^7.12.17","jest-localstorage-mock":"^2.4.6","@babel/preset-typescript":"^7.12.17"},"_npmOperationalInternal":{"tmp":"tmp/type-safe-storage_1.0.0_1615730031871_0.7512332032585971","host":"s3://npm-registry-packages"},"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info."}},"time":{"created":"2021-03-14T13:53:51.833Z","modified":"2025-01-25T07:16:34.807Z","1.0.0":"2021-03-14T13:53:52.045Z"},"author":{"name":"Aleksi Huotala","email":"aleksi.huotala@reaktor.com"},"license":"MIT","keywords":[],"description":"## Introduction","maintainers":[{"name":"alehuo","email":"alehuo@pm.me"}],"readme":"# type-safe-storage\n\n## Introduction\n\nThis library wraps the browser's localStorage and sessionStorage functionality to provide run-time type-checking of the values that are set and get from the sessionStorage and localStorage object.\n\nThis library adds a new parameter to the `setItem()` and `getItem()` functions which is used to pass an `io-ts` codec. The codec is used to validate the data coming from and going to the browser's local storage.\n\n## Installation\n\n```\nnpm install --save @alehuo/type-safe-storage fp-ts@2.9.5 io-ts@2.2.14\n```\n\n## Implemented methods\n\n- ```typescript\n    setItem(key, value, codec)\n    ```\n- ```typescript\n    getItem(key, codec)\n    ```\n- ```typescript\n    removeItem()\n    ```\n- ```typescript\n    clear()\n    ```\n- ```typescript\n    length // getter\n    ```\n\n## Example\n\n### setItem & getItem\n\n```typescript\nimport { typeSafeLocalStorage as localStorage } from \"@alehuo/type-safe-storage\";\nimport * as t from \"io-ts\";\nimport { isRight } from \"fp-ts/lib/either\";\n\nconst codec = t.type({\n  id: t.number,\n  value: t.string,\n});\n\nconst res = localStorage.setItem(\n  \"key\",\n  {\n    id: 1,\n    value: \"First\",\n  },\n  codec\n);\n\n// setItem returns either Left<\"ERROR\"> or Right<\"Ok\"> depending if the value passes the validation\n\nif (isRight(res)) {\n  console.log(\"Success!\");\n  const retrieved = localStorage.getItem(\"key\", codec);\n  // Returns null for non-existent storage value\n  // Returns instance of Left<t.Errors> if the decoding fails\n  // Returns Right<{ id: number, value: string }> for a successfull decoding\n  if (retrieved === null) {\n    console.error(\"Value does not exist!\");\n  }\n  if (isLeft(retrieved)) {\n    console.error(\"Failed to decode value!\");\n  } else {\n    console.log(`The value is: \"${retrieved.right.value}\"`);\n    // Output: The value is: \"First\"\n  }\n}\n```\n","readmeFilename":"README.md"}