-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare package.json and README for publishing
- Loading branch information
Showing
2 changed files
with
50 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
# safer-web-storage | ||
# safer-web-storage | ||
|
||
Browsers that support `localStorage` and `sessionStorage` will have a property on the window object named respectively. However, for various reasons, just asserting that property exists may throw exceptions. If it does exist, that is still no guarantee that localStorage is actually available, as various browsers offer settings that disable storages. So a browser may support localStorage, but not make it available to the scripts on the page. One example of that is Safari, which in Private Browsing mode gives us an empty localStorage object with a quota of zero, effectively making it unusable. | ||
|
||
## Installation and Usage | ||
|
||
```bash | ||
npm install safer-web-storage | ||
``` | ||
|
||
```javascript | ||
import * as SafeStorage from 'safer-web-storage' | ||
|
||
const safeLocalStorage = SafeStorage.createSafeLocalStorage() | ||
const safeSessionStorage = SafeStorage.createSafeSessionStorage() | ||
|
||
safeLocalStorage.getItem('apples') | ||
safeSessionStorage.seItem('pineapples', 20) | ||
``` | ||
|
||
If either `window.sessionStorage` or its methods are not accessible, it swaps to in-memory storage. | ||
|
||
This wrapper supports all methods and properties of [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API): | ||
|
||
- `getItem` | ||
- `setItem` | ||
- `removeItem` | ||
- `key` | ||
- `clear` | ||
- `length` | ||
|
||
## API | ||
|
||
### createSafeLocalStorage(options) => Storage | ||
|
||
### createSafeSessionStorage(options) => Storage | ||
|
||
You can pass the following properties to `options`: | ||
|
||
| Name | Type | Default | Description | | ||
| -------------- | ------ | ---------------------------------------------------------------------------- | -------------------------------------------------------------------------- | | ||
| `errorMessage` | String | Looks like you've disabled \<StorageType\>. Enable it to avoid this warning. | Error message printed to the browser console when storage is not available | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
{ | ||
"name": "safer-web-storage", | ||
"version": "0.0.1", | ||
"description": "Safer web storage", | ||
"description": "Handle all exception when storage is not available due to user's browser settings. Swap to in-memory storage if sessionStorage is not available.", | ||
"keywords": [ | ||
"localStorage", | ||
"sessionStorage", | ||
"in-memory storage" | ||
], | ||
"main": "src/index.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "jest" | ||
}, | ||
"author": "Vitalii Saienko <[email protected]> (https://github.com/modestfake)", | ||
"license": "ISC", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/modestfake/safer-web-storage.git" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/modestfake/safer-web-storage/issues" | ||
}, | ||
|