@ribbon-studios/js-utils 3.0.1 → 3.1.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 +18 -0
- package/dist/__tests__/storage.spec.d.ts +1 -0
- package/dist/index.cjs +30 -3
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +30 -3
- package/dist/storage.d.ts +18 -0
- package/package.json +18 -18
package/README.md
CHANGED
|
@@ -30,6 +30,8 @@ Collection of generic javascript utilities curated by the Ribbon Studios Team~
|
|
|
30
30
|
- [`rfetch.interceptors.request`](#rfetchinterceptorsrequest)
|
|
31
31
|
- [`rfetch.interceptors.reject`](#rfetchinterceptorsreject)
|
|
32
32
|
- [`rfetch.delimiters`](#rfetchdelimiters)
|
|
33
|
+
- [Storage](#storage)
|
|
34
|
+
- [`RibbonStorage`](#ribbonstorage)
|
|
33
35
|
|
|
34
36
|
## Promises
|
|
35
37
|
|
|
@@ -284,6 +286,22 @@ try {
|
|
|
284
286
|
}
|
|
285
287
|
```
|
|
286
288
|
|
|
289
|
+
## Storage
|
|
290
|
+
|
|
291
|
+
### `RibbonStorage`
|
|
292
|
+
|
|
293
|
+
A small wrapper around local / session storage that automatically stringifies and parses responses.
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
import { RibbonStorage } from '@ribbon-studios/js-utils';
|
|
297
|
+
|
|
298
|
+
RibbonStorage.local.set('hello', 'world'); // Sets 'hello' key in localStorage to '"world"'
|
|
299
|
+
RibbonStorage.local.get('hello'); // 'world'
|
|
300
|
+
|
|
301
|
+
RibbonStorage.session.set('hello', 'world'); // Sets 'hello' key in sessionStorage to '"world"'
|
|
302
|
+
RibbonStorage.session.get('hello'); // 'world'
|
|
303
|
+
```
|
|
304
|
+
|
|
287
305
|
[_**Want to Contribute?**_](/CONTRIBUTING.md)
|
|
288
306
|
|
|
289
307
|
[npm-version-image]: https://img.shields.io/npm/v/@ribbon-studios/js-utils.svg
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -43,7 +43,7 @@ async function retry(fn, n) {
|
|
|
43
43
|
}
|
|
44
44
|
class RibbonFetchError extends Error {
|
|
45
45
|
constructor({ status, content }) {
|
|
46
|
-
super(content);
|
|
46
|
+
super(content?.toString());
|
|
47
47
|
this.status = status;
|
|
48
48
|
this.content = content;
|
|
49
49
|
}
|
|
@@ -59,7 +59,6 @@ const fetchInterceptors = {
|
|
|
59
59
|
};
|
|
60
60
|
let delimiter = 1;
|
|
61
61
|
async function rfetch(url, { params, body, ...options } = {}) {
|
|
62
|
-
var _a, _b;
|
|
63
62
|
const requestInit = {
|
|
64
63
|
method: "GET",
|
|
65
64
|
...options
|
|
@@ -105,7 +104,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
105
104
|
Promise.resolve(requestInit)
|
|
106
105
|
)
|
|
107
106
|
);
|
|
108
|
-
const content =
|
|
107
|
+
const content = response.headers.get("Content-Type")?.toLowerCase()?.includes("json") ? await response.json() : await response.text();
|
|
109
108
|
if (response.ok) {
|
|
110
109
|
if (response.status === 204) return void 0;
|
|
111
110
|
return content;
|
|
@@ -203,8 +202,36 @@ rfetch.delete = (url, options) => {
|
|
|
203
202
|
is2.error = error;
|
|
204
203
|
})(rfetch2.is || (rfetch2.is = {}));
|
|
205
204
|
})(rfetch || (rfetch = {}));
|
|
205
|
+
const _RibbonStorage = class _RibbonStorage {
|
|
206
|
+
static get(storage, key, defaultValue = null) {
|
|
207
|
+
try {
|
|
208
|
+
const value = storage.getItem(key);
|
|
209
|
+
if (!value) return defaultValue;
|
|
210
|
+
return JSON.parse(value);
|
|
211
|
+
} catch {
|
|
212
|
+
return defaultValue;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
static set(storage, key, value) {
|
|
216
|
+
if (value === void 0 || value === null) {
|
|
217
|
+
storage.removeItem(key);
|
|
218
|
+
} else {
|
|
219
|
+
storage.setItem(key, JSON.stringify(value));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
_RibbonStorage.local = {
|
|
224
|
+
get: _RibbonStorage.get.bind(Storage, localStorage),
|
|
225
|
+
set: _RibbonStorage.set.bind(Storage, localStorage)
|
|
226
|
+
};
|
|
227
|
+
_RibbonStorage.session = {
|
|
228
|
+
get: _RibbonStorage.get.bind(Storage, sessionStorage),
|
|
229
|
+
set: _RibbonStorage.set.bind(Storage, sessionStorage)
|
|
230
|
+
};
|
|
231
|
+
let RibbonStorage = _RibbonStorage;
|
|
206
232
|
exports.DelimiterType = DelimiterType;
|
|
207
233
|
exports.RibbonFetchError = RibbonFetchError;
|
|
234
|
+
exports.RibbonStorage = RibbonStorage;
|
|
208
235
|
exports.assert = assert;
|
|
209
236
|
exports.delay = delay;
|
|
210
237
|
exports.never = never;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -41,7 +41,7 @@ async function retry(fn, n) {
|
|
|
41
41
|
}
|
|
42
42
|
class RibbonFetchError extends Error {
|
|
43
43
|
constructor({ status, content }) {
|
|
44
|
-
super(content);
|
|
44
|
+
super(content?.toString());
|
|
45
45
|
this.status = status;
|
|
46
46
|
this.content = content;
|
|
47
47
|
}
|
|
@@ -57,7 +57,6 @@ const fetchInterceptors = {
|
|
|
57
57
|
};
|
|
58
58
|
let delimiter = 1;
|
|
59
59
|
async function rfetch(url, { params, body, ...options } = {}) {
|
|
60
|
-
var _a, _b;
|
|
61
60
|
const requestInit = {
|
|
62
61
|
method: "GET",
|
|
63
62
|
...options
|
|
@@ -103,7 +102,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
103
102
|
Promise.resolve(requestInit)
|
|
104
103
|
)
|
|
105
104
|
);
|
|
106
|
-
const content =
|
|
105
|
+
const content = response.headers.get("Content-Type")?.toLowerCase()?.includes("json") ? await response.json() : await response.text();
|
|
107
106
|
if (response.ok) {
|
|
108
107
|
if (response.status === 204) return void 0;
|
|
109
108
|
return content;
|
|
@@ -201,9 +200,37 @@ rfetch.delete = (url, options) => {
|
|
|
201
200
|
is2.error = error;
|
|
202
201
|
})(rfetch2.is || (rfetch2.is = {}));
|
|
203
202
|
})(rfetch || (rfetch = {}));
|
|
203
|
+
const _RibbonStorage = class _RibbonStorage {
|
|
204
|
+
static get(storage, key, defaultValue = null) {
|
|
205
|
+
try {
|
|
206
|
+
const value = storage.getItem(key);
|
|
207
|
+
if (!value) return defaultValue;
|
|
208
|
+
return JSON.parse(value);
|
|
209
|
+
} catch {
|
|
210
|
+
return defaultValue;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
static set(storage, key, value) {
|
|
214
|
+
if (value === void 0 || value === null) {
|
|
215
|
+
storage.removeItem(key);
|
|
216
|
+
} else {
|
|
217
|
+
storage.setItem(key, JSON.stringify(value));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
_RibbonStorage.local = {
|
|
222
|
+
get: _RibbonStorage.get.bind(Storage, localStorage),
|
|
223
|
+
set: _RibbonStorage.set.bind(Storage, localStorage)
|
|
224
|
+
};
|
|
225
|
+
_RibbonStorage.session = {
|
|
226
|
+
get: _RibbonStorage.get.bind(Storage, sessionStorage),
|
|
227
|
+
set: _RibbonStorage.set.bind(Storage, sessionStorage)
|
|
228
|
+
};
|
|
229
|
+
let RibbonStorage = _RibbonStorage;
|
|
204
230
|
export {
|
|
205
231
|
DelimiterType,
|
|
206
232
|
RibbonFetchError,
|
|
233
|
+
RibbonStorage,
|
|
207
234
|
assert,
|
|
208
235
|
delay,
|
|
209
236
|
never,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class RibbonStorage {
|
|
2
|
+
static get<T>(storage: globalThis.Storage, key: string, defaultValue?: T): T;
|
|
3
|
+
static set<T>(storage: globalThis.Storage, key: string, value: T): void;
|
|
4
|
+
/**
|
|
5
|
+
* Shorthands for getting / setting from {@link localStorage}
|
|
6
|
+
*/
|
|
7
|
+
static local: {
|
|
8
|
+
get: any;
|
|
9
|
+
set: any;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Shorthands for getting / setting from {@link sessionStorage}
|
|
13
|
+
*/
|
|
14
|
+
static session: {
|
|
15
|
+
get: any;
|
|
16
|
+
set: any;
|
|
17
|
+
};
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ribbon-studios/js-utils",
|
|
3
|
-
"version": "3.
|
|
4
|
-
"description": "Collection of generic javascript utilities curated by the
|
|
3
|
+
"version": "3.1.1",
|
|
4
|
+
"description": "Collection of generic javascript utilities curated by the Ribbon Studios Team~",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "src/*.ts",
|
|
7
7
|
"main": "./dist/index.cjs",
|
|
@@ -28,23 +28,23 @@
|
|
|
28
28
|
"build": "rm -rf dist && vite build"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@eslint/js": "^
|
|
32
|
-
"@types/chance": "^1.1.
|
|
33
|
-
"@types/node": "^22.
|
|
34
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
35
|
-
"@typescript-eslint/parser": "^8.
|
|
36
|
-
"@vitest/coverage-v8": "^
|
|
37
|
-
"ajv": "^8.
|
|
38
|
-
"chance": "^1.1.
|
|
39
|
-
"eslint-plugin-unused-imports": "^4.1
|
|
40
|
-
"happy-dom": "^
|
|
41
|
-
"jiti": "^2.
|
|
42
|
-
"typescript": "^5.
|
|
43
|
-
"typescript-eslint": "^8.
|
|
44
|
-
"vite": "^
|
|
45
|
-
"vite-plugin-dts": "^4.5.
|
|
31
|
+
"@eslint/js": "^10.0.1",
|
|
32
|
+
"@types/chance": "^1.1.7",
|
|
33
|
+
"@types/node": "^22.19.15",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^8.56.1",
|
|
35
|
+
"@typescript-eslint/parser": "^8.56.1",
|
|
36
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
37
|
+
"ajv": "^8.18.0",
|
|
38
|
+
"chance": "^1.1.13",
|
|
39
|
+
"eslint-plugin-unused-imports": "^4.4.1",
|
|
40
|
+
"happy-dom": "^20.8.3",
|
|
41
|
+
"jiti": "^2.6.1",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"typescript-eslint": "^8.56.1",
|
|
44
|
+
"vite": "^7.3.1",
|
|
45
|
+
"vite-plugin-dts": "^4.5.4",
|
|
46
46
|
"vite-plugin-lib-types": "^3.1.2",
|
|
47
|
-
"vitest": "^
|
|
47
|
+
"vitest": "^4.0.18",
|
|
48
48
|
"vitest-dom": "^0.1.1"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|