@hyper-fetch/react 8.1.1 → 8.2.2

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.
Files changed (131) hide show
  1. package/README.md +202 -52
  2. package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +1 -1
  3. package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +1 -1
  4. package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +3 -3
  5. package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
  6. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +55 -55
  7. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +8 -9
  8. package/__tests__/features/provider/provider.hydration.spec.tsx +3 -4
  9. package/__tests__/features/provider/use-config-provider.base.spec.tsx +1 -2
  10. package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +1 -1
  11. package/__tests__/features/use-cache/use-cache.base.spec.ts +6 -6
  12. package/__tests__/features/use-cache/use-cache.typescript.spec.ts +1 -2
  13. package/__tests__/features/use-emitter/use-emitter.base.spec.ts +1 -1
  14. package/__tests__/features/use-fetch/use-fetch.base.spec.ts +13 -9
  15. package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +1 -1
  16. package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +1 -1
  17. package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +1 -1
  18. package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +1 -1
  19. package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +2 -2
  20. package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +1 -1
  21. package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +1 -1
  22. package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +1 -1
  23. package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +2 -2
  24. package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +3 -2
  25. package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +3 -3
  26. package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +47 -17
  27. package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +4 -4
  28. package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +1 -2
  29. package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +2 -2
  30. package/__tests__/features/use-fetch/use-fetch.window.spec.ts +1 -1
  31. package/__tests__/features/use-listener/use-listener.base.spec.ts +1 -1
  32. package/__tests__/features/use-queue/use-queue.actions.spec.ts +3 -3
  33. package/__tests__/features/use-queue/use-queue.base.spec.ts +13 -13
  34. package/__tests__/features/use-queue/use-queue.resolve.spec.ts +6 -6
  35. package/__tests__/features/use-stream/use-stream.base.spec.ts +1 -1
  36. package/__tests__/features/use-submit/use-submit.base.spec.ts +2 -2
  37. package/__tests__/features/use-submit/use-submit.cancel.spec.ts +1 -1
  38. package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +1 -1
  39. package/__tests__/features/use-submit/use-submit.debounce.spec.ts +15 -15
  40. package/__tests__/features/use-submit/use-submit.methods.spec.ts +1 -1
  41. package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +1 -1
  42. package/__tests__/features/use-submit/use-submit.queue.spec.ts +1 -1
  43. package/__tests__/features/use-submit/use-submit.retry.spec.ts +2 -2
  44. package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +1 -1
  45. package/__tests__/features/use-submit/use-submit.submit.spec.ts +1 -1
  46. package/__tests__/features/use-submit/use-submit.typescript.spec.ts +2 -3
  47. package/__tests__/shared/isolation.tests.ts +1 -1
  48. package/__tests__/shared/response.tests.ts +2 -2
  49. package/__tests__/tsconfig.json +11 -17
  50. package/__tests__/utils/cache.utils.ts +6 -6
  51. package/__tests__/utils/use-app-manager.utils.ts +0 -1
  52. package/__tests__/utils/use-cache.utils.ts +0 -1
  53. package/__tests__/utils/use-emitter.utils.ts +1 -2
  54. package/__tests__/utils/use-fetch.utils.ts +0 -1
  55. package/__tests__/utils/use-listener.utils.ts +1 -2
  56. package/__tests__/utils/use-queue.utils.ts +4 -5
  57. package/__tests__/utils/use-request-events.utils.ts +0 -1
  58. package/__tests__/utils/use-socket-state.utils.ts +1 -2
  59. package/__tests__/utils/use-submit.utils.ts +0 -1
  60. package/__tests__/utils/use-tracked-state.utils.ts +0 -1
  61. package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
  62. package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
  63. package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts.map +1 -1
  64. package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts.map +1 -1
  65. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
  66. package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +1 -0
  67. package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts.map +1 -1
  68. package/dist/hooks/use-cache/use-cache.hooks.d.ts +1 -0
  69. package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
  70. package/dist/hooks/use-cache/use-cache.types.d.ts +2 -2
  71. package/dist/hooks/use-cache/use-cache.types.d.ts.map +1 -1
  72. package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +1 -0
  73. package/dist/hooks/use-emitter/use-emitter.hooks.d.ts.map +1 -1
  74. package/dist/hooks/use-emitter/use-emitter.types.d.ts +1 -0
  75. package/dist/hooks/use-emitter/use-emitter.types.d.ts.map +1 -1
  76. package/dist/hooks/use-fetch/use-fetch.constants.d.ts.map +1 -1
  77. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +2 -1
  78. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
  79. package/dist/hooks/use-fetch/use-fetch.types.d.ts.map +1 -1
  80. package/dist/hooks/use-listener/use-listener.hooks.d.ts +1 -0
  81. package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
  82. package/dist/hooks/use-listener/use-listener.types.d.ts +1 -0
  83. package/dist/hooks/use-listener/use-listener.types.d.ts.map +1 -1
  84. package/dist/hooks/use-queue/use-queue.constants.d.ts.map +1 -1
  85. package/dist/hooks/use-queue/use-queue.hooks.d.ts +2 -1
  86. package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
  87. package/dist/hooks/use-queue/use-queue.types.d.ts +6 -0
  88. package/dist/hooks/use-queue/use-queue.types.d.ts.map +1 -1
  89. package/dist/hooks/use-submit/use-submit.hooks.d.ts +2 -1
  90. package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
  91. package/dist/hooks/use-submit/use-submit.types.d.ts +1 -1
  92. package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -1
  93. package/dist/index.mjs +30 -22
  94. package/dist/index.mjs.map +1 -1
  95. package/dist/provider/provider.types.d.ts +4 -4
  96. package/dist/provider/provider.types.d.ts.map +1 -1
  97. package/dist/utils/bounce.utils.d.ts.map +1 -1
  98. package/dist/utils/deep-equal.utils.d.ts +2 -2
  99. package/dist/utils/deep-equal.utils.d.ts.map +1 -1
  100. package/dist/utils/tracked-proxy.utils.d.ts +1 -1
  101. package/dist/utils/tracked-proxy.utils.d.ts.map +1 -1
  102. package/package.json +8 -13
  103. package/src/helpers/use-request-events/use-request-events.hooks.ts +4 -5
  104. package/src/helpers/use-request-events/use-request-events.types.ts +0 -1
  105. package/src/helpers/use-socket-state/use-socket-state.hooks.ts +5 -6
  106. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +11 -11
  107. package/src/helpers/use-tracked-state/use-tracked-state.types.ts +0 -1
  108. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +12 -13
  109. package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
  110. package/src/hooks/use-cache/use-cache.hooks.ts +4 -4
  111. package/src/hooks/use-cache/use-cache.types.ts +2 -3
  112. package/src/hooks/use-emitter/use-emitter.hooks.ts +4 -4
  113. package/src/hooks/use-emitter/use-emitter.types.ts +1 -0
  114. package/src/hooks/use-fetch/use-fetch.constants.ts +0 -1
  115. package/src/hooks/use-fetch/use-fetch.hooks.ts +14 -10
  116. package/src/hooks/use-fetch/use-fetch.types.ts +0 -1
  117. package/src/hooks/use-listener/use-listener.hooks.ts +3 -3
  118. package/src/hooks/use-listener/use-listener.types.ts +1 -0
  119. package/src/hooks/use-queue/use-queue.constants.ts +0 -1
  120. package/src/hooks/use-queue/use-queue.hooks.ts +3 -3
  121. package/src/hooks/use-queue/use-queue.types.ts +6 -0
  122. package/src/hooks/use-stream/use-stream.hooks.ts +4 -4
  123. package/src/hooks/use-submit/use-submit.hooks.ts +7 -7
  124. package/src/hooks/use-submit/use-submit.types.ts +1 -2
  125. package/src/provider/provider.types.ts +4 -5
  126. package/src/utils/bounce.utils.ts +1 -0
  127. package/src/utils/deep-equal.utils.ts +28 -12
  128. package/src/utils/tracked-proxy.utils.ts +1 -1
  129. package/tsconfig.json +11 -2
  130. package/vite.config.ts +14 -20
  131. package/.eslintrc.cjs +0 -12
@@ -1,3 +1,5 @@
1
+ import { useDidMount } from "@better-hooks/lifecycle";
2
+ import { useDebounce, useThrottle } from "@better-hooks/performance";
1
3
  import type {
2
4
  ExtractAdapterResolvedType,
3
5
  RequestInstance,
@@ -9,19 +11,17 @@ import type {
9
11
  ExtractAdapterType,
10
12
  } from "@hyper-fetch/core";
11
13
  import { sendRequest } from "@hyper-fetch/core";
12
- import { useDidMount } from "@better-hooks/lifecycle";
13
- import { useDebounce, useThrottle } from "@better-hooks/performance";
14
- import { useMemo, useRef } from "react";
15
-
16
- import type { UseSubmitOptionsType, UseSubmitReturnType } from "hooks/use-submit";
17
- import { useSubmitDefaultOptions } from "hooks/use-submit";
18
14
  import type { UseTrackedStateType } from "helpers";
19
15
  import { useTrackedState, useRequestEvents } from "helpers";
16
+ import type { UseSubmitOptionsType, UseSubmitReturnType } from "hooks/use-submit";
17
+ import { useSubmitDefaultOptions } from "hooks/use-submit";
20
18
  import { useProvider } from "provider";
19
+ import { useMemo, useRef } from "react";
21
20
  import { createTrackedProxy, getBounceData } from "utils";
22
21
 
23
22
  /**
24
- * This hooks aims to mutate data on the server.
23
+ * This hook aims to mutate data on the server. Unlike useFetch, it does not fire automatically -
24
+ * call the returned `submit` function to trigger the request (e.g., on button click).
25
25
  * @param request
26
26
  * @param options
27
27
  * @returns
@@ -1,6 +1,4 @@
1
1
  import type { ExtractAdapterResolvedType, NullableType, RequestInstance, RequestSendType } from "@hyper-fetch/core";
2
-
3
- import type { isEqual } from "utils";
4
2
  import type {
5
3
  OnProgressCallbackType,
6
4
  OnStartCallbackType,
@@ -10,6 +8,7 @@ import type {
10
8
  UseTrackedStateType,
11
9
  UseTrackedStateActions,
12
10
  } from "helpers";
11
+ import type { isEqual } from "utils";
13
12
 
14
13
  export type UseSubmitOptionsType<T extends RequestInstance> = {
15
14
  /**
@@ -1,12 +1,11 @@
1
1
  import type { RequestInstance } from "@hyper-fetch/core";
2
- import type React from "react";
3
-
4
- import type { UseSubmitOptionsType } from "hooks/use-submit";
5
2
  import type { UseCacheOptionsType } from "hooks/use-cache";
6
- import type { UseQueueOptionsType } from "hooks/use-queue";
7
- import type { UseListenerOptionsType } from "hooks/use-listener";
8
3
  import type { UseEmitterOptionsType } from "hooks/use-emitter";
9
4
  import type { UseFetchOptionsType } from "hooks/use-fetch";
5
+ import type { UseListenerOptionsType } from "hooks/use-listener";
6
+ import type { UseQueueOptionsType } from "hooks/use-queue";
7
+ import type { UseSubmitOptionsType } from "hooks/use-submit";
8
+ import type React from "react";
10
9
 
11
10
  export type ProviderOptionsType = {
12
11
  useFetchConfig?: Partial<UseFetchOptionsType<RequestInstance>>;
@@ -4,6 +4,7 @@ export const getBounceData = (bounceData: {
4
4
  }): { reset: () => void; active: boolean } => {
5
5
  return {
6
6
  ...bounceData,
7
+ // oxlint-disable-next-line unicorn/no-useless-spread
7
8
  ...{ throttle: undefined, debounce: undefined },
8
9
  };
9
10
  };
@@ -3,10 +3,14 @@
3
3
  * @param value any object or primitive
4
4
  * @returns true when value is empty
5
5
  */
6
- export const isEmpty = (value: unknown): boolean => {
6
+ export const isEmpty = (value?: unknown): boolean => {
7
7
  const valueType = Object.prototype.toString.call(value);
8
- if (Array.isArray(value)) return !value.length;
9
- if (typeof value === "object" && value !== null && valueType === "[object Object]") return !Object.keys(value).length;
8
+ if (Array.isArray(value)) {
9
+ return value.length === 0;
10
+ }
11
+ if (typeof value === "object" && value !== null && valueType === "[object Object]") {
12
+ return Object.keys(value).length === 0;
13
+ }
10
14
  return false;
11
15
  };
12
16
 
@@ -16,9 +20,11 @@ export const isEmpty = (value: unknown): boolean => {
16
20
  * @param secondValue unknown
17
21
  * @returns true when elements are equal
18
22
  */
19
- export const isEqual = (firstValue: unknown, secondValue: unknown): boolean => {
23
+ export const isEqual = (firstValue?: unknown, secondValue?: unknown): boolean => {
20
24
  // Nulls (objects), and other primitives
21
- if (firstValue === secondValue) return true;
25
+ if (firstValue === secondValue) {
26
+ return true;
27
+ }
22
28
 
23
29
  try {
24
30
  const firstValueType = Object.prototype.toString.call(firstValue);
@@ -31,24 +37,34 @@ export const isEqual = (firstValue: unknown, secondValue: unknown): boolean => {
31
37
  const isTypeValue = (type: unknown) => firstValueType === type && secondValueType === type;
32
38
 
33
39
  // Compared types are different
34
- if (firstValueType !== secondValueType) return false;
40
+ if (firstValueType !== secondValueType) {
41
+ return false;
42
+ }
35
43
 
36
44
  // NaN
37
- if (isType("number") && Number.isNaN(firstValue) && Number.isNaN(secondValue)) return true;
45
+ if (isType("number") && Number.isNaN(firstValue) && Number.isNaN(secondValue)) {
46
+ return true;
47
+ }
38
48
 
39
49
  // Empty Array or Object
40
- if (isEmpty(firstValue) && isEmpty(secondValue)) return true;
50
+ if (isEmpty(firstValue) && isEmpty(secondValue)) {
51
+ return true;
52
+ }
41
53
 
42
54
  // Array
43
55
  if (Array.isArray(firstValue) && Array.isArray(secondValue)) {
44
- if (firstValue.length !== secondValue.length) return false;
56
+ if (firstValue.length !== secondValue.length) {
57
+ return false;
58
+ }
45
59
 
46
60
  return !firstValue.some((element, i) => !isEqual(element, secondValue[i]));
47
61
  }
48
62
 
49
63
  // Object
50
64
  if (isType("object") && isTypeValue("[object Object]")) {
51
- if (Object.keys(firstValue as object).length !== Object.keys(secondValue as object).length) return false;
65
+ if (Object.keys(firstValue as object).length !== Object.keys(secondValue as object).length) {
66
+ return false;
67
+ }
52
68
 
53
69
  return !Object.entries(firstValue as object).some(
54
70
  ([key, value]) => !isEqual(value, (secondValue as Record<string, unknown>)[key]),
@@ -62,8 +78,8 @@ export const isEqual = (firstValue: unknown, secondValue: unknown): boolean => {
62
78
 
63
79
  // undefined, string, number, bool
64
80
  return firstValue === secondValue;
65
- } catch (err) {
66
- console.error(err);
81
+ } catch (error) {
82
+ console.error(error);
67
83
  return false;
68
84
  }
69
85
  };
@@ -5,7 +5,7 @@
5
5
  */
6
6
  export const createTrackedProxy = <T extends Record<string, unknown>, K extends string>(
7
7
  target: T,
8
- trackedKeys: ReadonlyArray<K>,
8
+ trackedKeys: readonly K[],
9
9
  setRenderKey: (key: K) => void,
10
10
  ): T => {
11
11
  return new Proxy(target, {
package/tsconfig.json CHANGED
@@ -2,8 +2,17 @@
2
2
  "extends": "../../tsconfig.json",
3
3
  "compilerOptions": {
4
4
  "strict": true,
5
- "baseUrl": "./src",
6
- "jsx": "react-jsx"
5
+ "jsx": "react-jsx",
6
+ "paths": {
7
+ "helpers": ["./src/helpers"],
8
+ "helpers/*": ["./src/helpers/*"],
9
+ "hooks": ["./src/hooks"],
10
+ "hooks/*": ["./src/hooks/*"],
11
+ "provider": ["./src/provider"],
12
+ "provider/*": ["./src/provider/*"],
13
+ "utils": ["./src/utils"],
14
+ "utils/*": ["./src/utils/*"]
15
+ }
7
16
  },
8
17
  "exclude": ["node_modules", "dist", "vite.config.ts", "__tests__"]
9
18
  }
package/vite.config.ts CHANGED
@@ -1,45 +1,39 @@
1
1
  /// <reference types="vitest/config" />
2
+ import path from "path";
2
3
  import { defineConfig } from "vite";
3
4
  import dts from "vite-plugin-dts";
4
5
  import tsconfigPaths from "vite-tsconfig-paths";
5
- import path from "path";
6
- import { getDtsCompilerOptionsForPackage } from "../../scripts/vite-dts-internal-paths";
6
+
7
7
  import { getRollupExternalsFromPackageJson } from "../../scripts/vite-lib-externals-from-package";
8
8
 
9
9
  export default defineConfig({
10
10
  build: {
11
11
  lib: {
12
12
  entry: "src/index.ts",
13
- formats: ["es"],
14
13
  fileName: "index",
14
+ formats: ["es"],
15
15
  },
16
- sourcemap: true,
17
16
  minify: false,
18
17
  rollupOptions: {
19
18
  external: getRollupExternalsFromPackageJson(__dirname),
20
19
  },
20
+ sourcemap: true,
21
21
  },
22
- plugins: [
23
- dts({
24
- entryRoot: "src",
25
- compilerOptions: getDtsCompilerOptionsForPackage(__dirname),
26
- }),
27
- tsconfigPaths(),
28
- ],
22
+ plugins: [tsconfigPaths(), dts({ entryRoot: "src" })],
29
23
  test: {
30
- globals: true,
31
- environment: "jsdom",
32
- setupFiles: ["./__tests__/vitest.setup.ts"],
33
- include: ["__tests__/**/*.spec.{ts,tsx}", "src/**/*.spec.{ts,tsx}"],
34
- coverage: {
35
- provider: "v8",
36
- include: ["src/**/*.{ts,tsx}"],
37
- exclude: ["**/*.spec.*", "**/types.*", "**/constants.*", "**/index.ts"],
38
- },
39
24
  alias: {
40
25
  "@hyper-fetch/core": path.resolve(__dirname, "../core/src/index.ts"),
41
26
  "@hyper-fetch/sockets": path.resolve(__dirname, "../sockets/src/index.ts"),
42
27
  "@hyper-fetch/testing": path.resolve(__dirname, "../testing/src/index.ts"),
43
28
  },
29
+ coverage: {
30
+ exclude: ["**/*.spec.*", "**/types.*", "**/constants.*", "**/index.ts"],
31
+ include: ["src/**/*.{ts,tsx}"],
32
+ provider: "v8",
33
+ },
34
+ environment: "jsdom",
35
+ globals: true,
36
+ include: ["__tests__/**/*.spec.{ts,tsx}", "src/**/*.spec.{ts,tsx}"],
37
+ setupFiles: ["./__tests__/vitest.setup.ts"],
44
38
  },
45
39
  });
package/.eslintrc.cjs DELETED
@@ -1,12 +0,0 @@
1
- module.exports = {
2
- extends: ["plugin:@nx/react", "../../.eslintrc.js"],
3
- parserOptions: {
4
- tsconfigRootDir: __dirname,
5
- project: ["./tsconfig.json", "./__tests__/tsconfig.json"],
6
- },
7
- rules: {
8
- "react/jsx-props-no-spreading": 0,
9
- "react/require-default-props": 0,
10
- },
11
- ignorePatterns: ["dist/", "coverage/", "vite.config.ts", "/*.js"],
12
- };