{"id":19356885,"url":"https://github.com/alexfigliolia/typed-storage","last_synced_at":"2026-02-07T23:32:13.531Z","repository":{"id":257814183,"uuid":"869238325","full_name":"alexfigliolia/typed-storage","owner":"alexfigliolia","description":"A type-safe wrapper around the browser's LocalStorage and SessionStorage API's","archived":false,"fork":false,"pushed_at":"2024-10-08T00:54:17.000Z","size":50,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-10T07:45:03.777Z","etag":null,"topics":["local-storage","session-storage","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@figliolia/typed-storage","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexfigliolia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-08T00:44:30.000Z","updated_at":"2024-10-08T02:34:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"dab54921-faf7-41b3-a334-855543ac330b","html_url":"https://github.com/alexfigliolia/typed-storage","commit_stats":null,"previous_names":["alexfigliolia/typed-storage"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alexfigliolia/typed-storage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfigliolia%2Ftyped-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfigliolia%2Ftyped-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfigliolia%2Ftyped-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfigliolia%2Ftyped-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexfigliolia","download_url":"https://codeload.github.com/alexfigliolia/typed-storage/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfigliolia%2Ftyped-storage/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29212586,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T23:14:30.912Z","status":"ssl_error","status_checked_at":"2026-02-07T23:14:17.253Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["local-storage","session-storage","typescript"],"created_at":"2024-11-10T07:05:45.776Z","updated_at":"2026-02-07T23:32:13.511Z","avatar_url":"https://github.com/alexfigliolia.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Typed Storage\nA type-safe wrapper around the browser's `LocalStorage` and `SessionStorage` API's. The motivation behind this package is two fold:\n1. When working with large teams, ensuring that key-names are consistent and follow certain conventions can be combersome\n2. Ensuring that only values of a certain type are stored for each key can require can be difficult because the API's require string-values when storing data\n\nThe `TypedStorage` API allows you define a schema for your data and restricts storing data to only that which matches your key's corresponding value type.\n\n## Installation\n```bash\nnpm i @figliolia/typed-storage\n# or \nyarn add @figliolia/typed-storage\n```\n\n## Basic Usage\n\n### Defining Your API\n```typescript\nimport { TypedStorage } from \"@figliolia/typed-storage\";\n\nexport interface Schema {\n  JWT: string;\n  userId: number;\n  shoppingCart: { item: string, price: number }[];\n}\n\nconst LocalStorage = new TypedStorage\u003cSchema\u003e(localStorage);\n// or \nconst SessionStorage = new TypedStorage\u003cSchema\u003e(sessionStorage);\n```\n\n### Using Your API\nWhen using your `TypedStorage` instances, your data-type is preserved when setting and getting:\n\n```typescript\nimport { LocalStorage } from \"./path/to/myLocalStorage\";\n\nLocalStorage.setItem(\"shoppingCart\", [\n  {item: \"Bananas\", price: 3.00 },\n  {item: \"Apples\", price: 2.50 },\n  // Stringified under the hood\n]);\n\nconst cart = LocalStorage.getItem(\"shoppingCart\"); // parsed under the hood\n// { item: string, price: number }[] | null\n\n\nconst cart = LocalStorage.getItem(\"cart\"); // mispelled key\n// Fails typescript validation!\n```\n\n### Supported Data Types\nThe `TypedStorage` API supports storing any data-type that is JSON-valid. For keys with values of type `object` or `array`, `JSON.parse()` will be used to deserialize your data upon retreival. \n\nFor keys with values of type `string` or `number`, best effort parsing is used to determine the correct type on retrieval. For example if the value being retrieved can be safely converted to an integer, float, or `BigInt`, it will be. Otherwise the value will be returned as a string. \n\nFor instances where your key-value pair requires a customized serialization or deserialization technique, you can instantiate your `TypedStorage` along with your deserializer mapped to your key:\n\n```typescript\nimport { TypedStorage } from \"@figliolia/typed-storage\";\n\ninterface Schema {\n  meyKey: Map\u003cstring, number\u003e; // (invalid JSON)\n}\n\nconst MyStorage = new TypedStorage\u003cSchema\u003e(localStorage, {\n  meyKey: {\n    // Convert map to object and stringify it for storage\n    serialize: (map) =\u003e {\n      const obj: Record\u003cstring, number\u003e = {};\n      for(const [key, value] of map) {\n        obj[key] = value;\n      }\n      return JSON.stringify(map);\n    },\n    // Convert stored object back into a Map for runtime usage\n    deserialize: (map) =\u003e {\n      const obj = JSON.parse(map);\n      const result = new Map();\n      for(const key in obj) {\n        result.set(key, obj[key]);\n      }\n      return result;\n    } \n  }\n});\n```\n\nSerializers can also be used in instances where you wish to handle numeric values as strings\n```typescript\nimport { TypedStorage } from \"@figliolia/typed-storage\";\n\ninterface Schema {\n  floatValue: string; // Handle float as strings\n}\n\nconst MyStorage = new TypedStorage\u003cSchema\u003e(localStorage, {\n  floatValue: {\n    // Prevent float-like string from being returned as a number\n    deserialize: (floatValue) =\u003e floatValue;\n  }\n})\n```\nBecause `TypedStorage` parses using a best-effort interpretation of the value, if your string contains only numeric characters, it'll be parsed as a number regardless of your schema definition.\n\nIf you run into a case such as this and you wish to preserve string-types for numeric values, provide a `serialize` method for that key that simply returns the value as is.\n\n## Advanced Usage\nThis library also provides an enhancement to the `TypedStorage` API called `LiveStorage`. It works identically to `TypedStorage` with the exception that `LiveStorage` allows you to synchronize your application logic with the data you store:\n\n```typescript\nimport { LiveStorage } from \"@figliolia/typed-storage\";\n\nexport interface Schema {\n  JWT: string;\n  userId: number;\n  shoppingCart: { item: string, price: number }[];\n}\n\nconst LocalStorage = new TypedStorage\u003cSchema\u003e(localStorage);\n// or \nconst SessionStorage = new TypedStorage\u003cSchema\u003e(sessionStorage);\n\n// Let's update our UI whenever our `shoppingCart` changes\n\nconst currency = new Intl.NumberFormat('en-us', {\n  style: 'currency',\n  currency: 'USD'\n});\n\nconst checkoutUI = document.getElementById(\"checkout\");\n\nLocalStorage.on(\"shoppingCart\", cart =\u003e {\n  if(cart === null) {\n    // the key was deleted\n    checkoutUI.textContent = currency.format(0.00);\n    return;\n  }\n\n  // Update your checkout UI's total cost $\n  const newPrice = cart.reduce((acc, next) =\u003e {\n    acc += next.price;\n    return acc;\n  }, 0);\n  checkoutUI.textContent = currency.format(newPrice);\n});\n```\nIn the example above, we'll update our `checkout UI` whenever the user's shopping cart is updated.\n\n### When is LiveStorage the better idea?\nUse `LiveStorage` in areas where your business logic depends heavily on reads/writes to storage. If your application is performing read/writes in logic that spans multiple features or modules, you may find the that `LiveStorage` API allows you to write more centralized logic in otherwise complex scenarios.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexfigliolia%2Ftyped-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexfigliolia%2Ftyped-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexfigliolia%2Ftyped-storage/lists"}