@hefang/web-storage 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/getLocalStorage.d.ts +2 -0
- package/dist/getLocalStorage.js +4 -0
- package/dist/getSessionStorage.d.ts +2 -0
- package/dist/getSessionStorage.js +4 -0
- package/dist/getStorage.d.ts +2 -0
- package/dist/getStorage.js +9 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/setLocalStorage.d.ts +1 -0
- package/dist/setLocalStorage.js +4 -0
- package/dist/setSessionStorage.d.ts +1 -0
- package/dist/setSessionStorage.js +4 -0
- package/dist/setStorage.d.ts +1 -0
- package/dist/setStorage.js +3 -0
- package/package.json +21 -0
- package/src/getLocalStorage.ts +7 -0
- package/src/getSessionStorage.ts +7 -0
- package/src/getStorage.ts +18 -0
- package/src/index.ts +6 -0
- package/src/setLocalStorage.ts +5 -0
- package/src/setSessionStorage.ts +5 -0
- package/src/setStorage.ts +3 -0
- package/tsconfig.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @hefang/web-storage
|
|
2
|
+
|
|
3
|
+
To install dependencies:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
To run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run index.ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This project was created using `bun init` in bun v1.3.11. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function setLocalStorage(key: string, value: unknown): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function setSessionStorage<T>(key: string, value: T): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function setStorage(storage: Storage, key: string, value: unknown): void;
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hefang/web-storage",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"require": "./dist/index.js",
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"default": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "bun tsc --noEmit false",
|
|
13
|
+
"prepack": "bun run build"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/bun": "latest"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"typescript": "^5"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { getStorage } from "./getStorage";
|
|
2
|
+
|
|
3
|
+
export function getLocalStorage<T>(key: string): T | undefined;
|
|
4
|
+
export function getLocalStorage<T>(key: string, defaultValue: T): T;
|
|
5
|
+
export function getLocalStorage<T>(key: string, defaultValue?: T): T | undefined {
|
|
6
|
+
return getStorage(localStorage, key, defaultValue);
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { getStorage } from "./getStorage";
|
|
2
|
+
|
|
3
|
+
export function getSessionStorage<T>(key: string): T | undefined;
|
|
4
|
+
export function getSessionStorage<T>(key: string, defaultValue: T): T;
|
|
5
|
+
export function getSessionStorage<T>(key: string, defaultValue?: T): T | undefined {
|
|
6
|
+
return getStorage(sessionStorage, key, defaultValue);
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function getStorage<T>(storage: Storage, key: string): T | undefined;
|
|
2
|
+
export function getStorage<T>(
|
|
3
|
+
storage: Storage,
|
|
4
|
+
key: string,
|
|
5
|
+
defaultValue: T,
|
|
6
|
+
): T;
|
|
7
|
+
export function getStorage<T>(
|
|
8
|
+
storage: Storage,
|
|
9
|
+
key: string,
|
|
10
|
+
defaultValue?: T,
|
|
11
|
+
): T | undefined {
|
|
12
|
+
const value = storage.getItem(key);
|
|
13
|
+
try {
|
|
14
|
+
return value ? JSON.parse(value) : defaultValue;
|
|
15
|
+
} catch {
|
|
16
|
+
return defaultValue;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": ["ESNext", "DOM"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "Preserve",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": false,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
|
|
17
|
+
// Best practices
|
|
18
|
+
"strict": true,
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
"noUncheckedIndexedAccess": true,
|
|
22
|
+
"noImplicitOverride": true,
|
|
23
|
+
|
|
24
|
+
// Some stricter flags (disabled by default)
|
|
25
|
+
"noUnusedLocals": false,
|
|
26
|
+
"noUnusedParameters": false,
|
|
27
|
+
"noPropertyAccessFromIndexSignature": false,
|
|
28
|
+
"declaration": true,
|
|
29
|
+
"outDir": "dist"
|
|
30
|
+
}
|
|
31
|
+
}
|