@ribbon-studios/js-utils 1.2.0 → 1.3.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/CONTRIBUTING.md CHANGED
@@ -5,7 +5,7 @@ Then this is the perfect place for you!
5
5
 
6
6
  ## Prerequisites
7
7
 
8
- - NodeJS 18
8
+ - NodeJS 22
9
9
 
10
10
  ## Setting Up Locally
11
11
 
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/refreshly
206
- [maintainability-url]: https://codeclimate.com/github/ribbon-studios/refreshly/maintainability
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,21 +26,29 @@ 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
- const { params, headers, body, ...internalOptions } = {
38
- method: "GET",
39
- headers: {},
40
- ...options
46
+ const requestInit = {
47
+ method: (options == null ? void 0 : options.method) ?? "GET"
41
48
  };
42
49
  const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
43
- if (params) {
44
- for (const [key, values] of Object.entries(params)) {
50
+ if (options == null ? void 0 : options.params) {
51
+ for (const [key, values] of Object.entries(options.params)) {
45
52
  if (Array.isArray(values)) {
46
53
  for (const value of values) {
47
54
  internalURL.searchParams.append(key, value.toString());
@@ -51,18 +58,28 @@ async function rfetch(url, options) {
51
58
  }
52
59
  }
53
60
  }
54
- let internalBody = void 0;
55
- if (body) {
56
- internalBody = body instanceof FormData ? body : JSON.stringify(body);
61
+ if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
62
+ if (options.body instanceof FormData) {
63
+ requestInit.body = options.body;
64
+ requestInit.headers = {
65
+ "Content-Type": "application/x-www-form-urlencoded",
66
+ ...requestInit.headers
67
+ };
68
+ } else if (typeof options.body === "string") {
69
+ requestInit.body = options.body;
70
+ requestInit.headers = {
71
+ "Content-Type": "application/json",
72
+ ...requestInit.headers
73
+ };
74
+ } else {
75
+ requestInit.body = JSON.stringify(options.body);
76
+ requestInit.headers = {
77
+ "Content-Type": "application/json",
78
+ ...requestInit.headers
79
+ };
80
+ }
57
81
  }
58
- const response = await fetch(internalURL, {
59
- ...internalOptions,
60
- headers: {
61
- "Content-Type": internalBody instanceof FormData ? "application/x-www-form-urlencoded" : "application/json",
62
- ...headers
63
- },
64
- body: internalBody
65
- });
82
+ const response = await fetch(internalURL, requestInit);
66
83
  const content = ((_b = (_a = response.headers.get("Content-Type")) == null ? void 0 : _a.toLowerCase()) == null ? void 0 : _b.includes("json")) ? await response.json() : await response.text();
67
84
  if (response.ok) {
68
85
  return content;
@@ -112,4 +129,5 @@ async function rfetch(url, options) {
112
129
  exports.assert = assert;
113
130
  exports.delay = delay;
114
131
  exports.never = never;
132
+ exports.retry = retry;
115
133
  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,21 +24,29 @@ 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
- const { params, headers, body, ...internalOptions } = {
36
- method: "GET",
37
- headers: {},
38
- ...options
44
+ const requestInit = {
45
+ method: (options == null ? void 0 : options.method) ?? "GET"
39
46
  };
40
47
  const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
41
- if (params) {
42
- for (const [key, values] of Object.entries(params)) {
48
+ if (options == null ? void 0 : options.params) {
49
+ for (const [key, values] of Object.entries(options.params)) {
43
50
  if (Array.isArray(values)) {
44
51
  for (const value of values) {
45
52
  internalURL.searchParams.append(key, value.toString());
@@ -49,18 +56,28 @@ async function rfetch(url, options) {
49
56
  }
50
57
  }
51
58
  }
52
- let internalBody = void 0;
53
- if (body) {
54
- internalBody = body instanceof FormData ? body : JSON.stringify(body);
59
+ if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
60
+ if (options.body instanceof FormData) {
61
+ requestInit.body = options.body;
62
+ requestInit.headers = {
63
+ "Content-Type": "application/x-www-form-urlencoded",
64
+ ...requestInit.headers
65
+ };
66
+ } else if (typeof options.body === "string") {
67
+ requestInit.body = options.body;
68
+ requestInit.headers = {
69
+ "Content-Type": "application/json",
70
+ ...requestInit.headers
71
+ };
72
+ } else {
73
+ requestInit.body = JSON.stringify(options.body);
74
+ requestInit.headers = {
75
+ "Content-Type": "application/json",
76
+ ...requestInit.headers
77
+ };
78
+ }
55
79
  }
56
- const response = await fetch(internalURL, {
57
- ...internalOptions,
58
- headers: {
59
- "Content-Type": internalBody instanceof FormData ? "application/x-www-form-urlencoded" : "application/json",
60
- ...headers
61
- },
62
- body: internalBody
63
- });
80
+ const response = await fetch(internalURL, requestInit);
64
81
  const content = ((_b = (_a = response.headers.get("Content-Type")) == null ? void 0 : _a.toLowerCase()) == null ? void 0 : _b.includes("json")) ? await response.json() : await response.text();
65
82
  if (response.ok) {
66
83
  return content;
@@ -111,5 +128,6 @@ export {
111
128
  assert,
112
129
  delay,
113
130
  never,
131
+ retry,
114
132
  rfetch
115
133
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
1
  export * from './assert';
2
2
  export * from './delay';
3
3
  export * from './never';
4
+ export * from './retry';
@@ -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,13 +1,13 @@
1
1
  {
2
2
  "name": "@ribbon-studios/js-utils",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",
7
7
  "main": "./dist/index.cjs",
8
8
  "module": "./dist/index.module.js",
9
9
  "unpkg": "./dist/index.umd.js",
10
- "types": "dist/index.d.ts",
10
+ "types": "./dist/index.d.ts",
11
11
  "exports": {
12
12
  ".": {
13
13
  "import": {
@@ -28,20 +28,23 @@
28
28
  "build": "rm -rf dist && vite build"
29
29
  },
30
30
  "devDependencies": {
31
- "@types/node": "^20.11.20",
32
- "@typescript-eslint/eslint-plugin": "^7.0.2",
33
- "@typescript-eslint/parser": "^7.0.2",
34
- "@vitest/coverage-v8": "^1.3.1",
35
- "chance": "^1.1.11",
36
- "eslint": "^8.57.0",
37
- "eslint-config-prettier": "^9.1.0",
38
- "eslint-plugin-unused-imports": "^3.1.0",
39
- "happy-dom": "^13.5.0",
40
- "typescript": "^5.3.3",
41
- "vite": "^5.1.4",
42
- "vite-plugin-dts": "^3.7.3",
43
- "vite-plugin-lib-types": "^3.0.9",
44
- "vitest": "^1.3.1",
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": {