@ribbon-studios/js-utils 1.2.0 → 1.3.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/CONTRIBUTING.md +1 -1
- package/README.md +15 -2
- package/dist/index.cjs +14 -4
- package/dist/index.js +14 -4
- package/dist/promises/__tests__/retry.spec.d.ts +1 -0
- package/dist/promises/index.d.ts +1 -0
- package/dist/promises/retry.d.ts +8 -0
- package/package.json +18 -15
package/CONTRIBUTING.md
CHANGED
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ Collection of generic javascript utilities curated by the Rainbow Cafe~
|
|
|
19
19
|
- [`assert`](#assert)
|
|
20
20
|
- [`assert.defined`](#assertdefined)
|
|
21
21
|
- [`never`](#never)
|
|
22
|
+
- [`retry`](#retry)
|
|
22
23
|
- [Fetch](#fetch)
|
|
23
24
|
- [`rfetch`](#rfetch)
|
|
24
25
|
- [`rfetch.get`](#rfetchget)
|
|
@@ -103,6 +104,18 @@ const promise = never(); // Returns a promise that never resolves
|
|
|
103
104
|
const promise = never(Promise.resolve('hello')); // Returns a promise that never resolves
|
|
104
105
|
```
|
|
105
106
|
|
|
107
|
+
### `retry`
|
|
108
|
+
|
|
109
|
+
Retries a function `n` times until it resolves successfully.
|
|
110
|
+
This can be useful for requests that tend to be flaky.
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import { retry } from '@ribbon-studios/js-utils';
|
|
114
|
+
|
|
115
|
+
// Returns a promise that resolves when the request is successful or fails after its exceeded that maximum attempts.
|
|
116
|
+
const promise = retry(() => getMaps(), 5);
|
|
117
|
+
```
|
|
118
|
+
|
|
106
119
|
## Fetch
|
|
107
120
|
|
|
108
121
|
### `rfetch`
|
|
@@ -202,7 +215,7 @@ await rfetch.remove<MyExpectedResponse>('https://ribbonstudios.com');
|
|
|
202
215
|
[coveralls-url]: https://coveralls.io/github/ribbon-studios/js-utils?branch=main
|
|
203
216
|
[code-style-image]: https://img.shields.io/badge/code%20style-prettier-ff69b4.svg
|
|
204
217
|
[code-style-url]: https://prettier.io
|
|
205
|
-
[maintainability-image]: https://img.shields.io/codeclimate/maintainability/ribbon-studios/
|
|
206
|
-
[maintainability-url]: https://codeclimate.com/github/ribbon-studios/
|
|
218
|
+
[maintainability-image]: https://img.shields.io/codeclimate/maintainability/ribbon-studios/js-utils
|
|
219
|
+
[maintainability-url]: https://codeclimate.com/github/ribbon-studios/js-utils/maintainability
|
|
207
220
|
[semantic-release-url]: https://github.com/semantic-release/semantic-release
|
|
208
221
|
[semantic-release-image]: https://img.shields.io/badge/%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079
|
package/dist/index.cjs
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
async function assert(p, predicate, message) {
|
|
4
4
|
const value = await p;
|
|
5
|
-
if (predicate(value))
|
|
6
|
-
return value;
|
|
5
|
+
if (predicate(value)) return value;
|
|
7
6
|
throw new Error(message ?? "Value does not satisfy predicate");
|
|
8
7
|
}
|
|
9
8
|
((assert2) => {
|
|
@@ -27,11 +26,21 @@ async function delay(promise, ms) {
|
|
|
27
26
|
delay2.fallback = fallback;
|
|
28
27
|
})(delay || (delay = {}));
|
|
29
28
|
async function never(p) {
|
|
30
|
-
if (p)
|
|
31
|
-
console.warn(`Promise is being called via "never", please ensure this doesn't get deployed!`, p);
|
|
29
|
+
if (p) console.warn(`Promise is being called via "never", please ensure this doesn't get deployed!`, p);
|
|
32
30
|
return new Promise(() => {
|
|
33
31
|
});
|
|
34
32
|
}
|
|
33
|
+
async function retry(fn, n) {
|
|
34
|
+
let attempts = 0;
|
|
35
|
+
while (true) {
|
|
36
|
+
try {
|
|
37
|
+
return await fn();
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (++attempts < n) continue;
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
35
44
|
async function rfetch(url, options) {
|
|
36
45
|
var _a, _b;
|
|
37
46
|
const { params, headers, body, ...internalOptions } = {
|
|
@@ -112,4 +121,5 @@ async function rfetch(url, options) {
|
|
|
112
121
|
exports.assert = assert;
|
|
113
122
|
exports.delay = delay;
|
|
114
123
|
exports.never = never;
|
|
124
|
+
exports.retry = retry;
|
|
115
125
|
exports.rfetch = rfetch;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
async function assert(p, predicate, message) {
|
|
2
2
|
const value = await p;
|
|
3
|
-
if (predicate(value))
|
|
4
|
-
return value;
|
|
3
|
+
if (predicate(value)) return value;
|
|
5
4
|
throw new Error(message ?? "Value does not satisfy predicate");
|
|
6
5
|
}
|
|
7
6
|
((assert2) => {
|
|
@@ -25,11 +24,21 @@ async function delay(promise, ms) {
|
|
|
25
24
|
delay2.fallback = fallback;
|
|
26
25
|
})(delay || (delay = {}));
|
|
27
26
|
async function never(p) {
|
|
28
|
-
if (p)
|
|
29
|
-
console.warn(`Promise is being called via "never", please ensure this doesn't get deployed!`, p);
|
|
27
|
+
if (p) console.warn(`Promise is being called via "never", please ensure this doesn't get deployed!`, p);
|
|
30
28
|
return new Promise(() => {
|
|
31
29
|
});
|
|
32
30
|
}
|
|
31
|
+
async function retry(fn, n) {
|
|
32
|
+
let attempts = 0;
|
|
33
|
+
while (true) {
|
|
34
|
+
try {
|
|
35
|
+
return await fn();
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (++attempts < n) continue;
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
33
42
|
async function rfetch(url, options) {
|
|
34
43
|
var _a, _b;
|
|
35
44
|
const { params, headers, body, ...internalOptions } = {
|
|
@@ -111,5 +120,6 @@ export {
|
|
|
111
120
|
assert,
|
|
112
121
|
delay,
|
|
113
122
|
never,
|
|
123
|
+
retry,
|
|
114
124
|
rfetch
|
|
115
125
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/promises/index.d.ts
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempts a request {@link n} times until it resolves.
|
|
3
|
+
*
|
|
4
|
+
* @param fn The function to invoke.
|
|
5
|
+
* @param n The maximum number of attempts
|
|
6
|
+
* @returns A resolved promise if it succeeds or the rejected promise if it exceeds {@link n}
|
|
7
|
+
*/
|
|
8
|
+
export declare function retry<T>(fn: () => Promise<T>, n: number): Promise<T>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ribbon-studios/js-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "src/*.ts",
|
|
@@ -28,20 +28,23 @@
|
|
|
28
28
|
"build": "rm -rf dist && vite build"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
33
|
-
"@
|
|
34
|
-
"@
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
31
|
+
"@eslint/js": "^9.22.0",
|
|
32
|
+
"@types/chance": "^1.1.6",
|
|
33
|
+
"@types/node": "^22.13.10",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^8.26.0",
|
|
35
|
+
"@typescript-eslint/parser": "^8.26.0",
|
|
36
|
+
"@vitest/coverage-v8": "^3.0.8",
|
|
37
|
+
"ajv": "^8.17.1",
|
|
38
|
+
"chance": "^1.1.12",
|
|
39
|
+
"eslint-plugin-unused-imports": "^4.1.4",
|
|
40
|
+
"happy-dom": "^17.4.1",
|
|
41
|
+
"jiti": "^2.4.2",
|
|
42
|
+
"typescript": "^5.8.2",
|
|
43
|
+
"typescript-eslint": "^8.26.0",
|
|
44
|
+
"vite": "^6.2.1",
|
|
45
|
+
"vite-plugin-dts": "^4.5.3",
|
|
46
|
+
"vite-plugin-lib-types": "^3.1.2",
|
|
47
|
+
"vitest": "^3.0.8",
|
|
45
48
|
"vitest-dom": "^0.1.1"
|
|
46
49
|
},
|
|
47
50
|
"publishConfig": {
|