@ribbon-studios/js-utils 3.0.0 → 3.1.0
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 +2 -3
- package/dist/index.js +2 -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();
|
|
46
|
+
super(content);
|
|
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;
|
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();
|
|
44
|
+
super(content);
|
|
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;
|
|
@@ -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.0",
|
|
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": {
|