@spoosh/plugin-prefetch 0.1.3 → 0.2.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 CHANGED
@@ -28,14 +28,14 @@ const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
28
28
  const { useRead, useWrite, prefetch } = createReactSpoosh(spoosh);
29
29
 
30
30
  // Basic prefetch
31
- await prefetch((api) => api.posts.$get());
31
+ await prefetch((api) => api("posts").GET());
32
32
 
33
33
  // Prefetch with query options
34
- await prefetch((api) => api.posts.$get({ query: { page: 1, limit: 10 } }));
34
+ await prefetch((api) => api("posts").GET({ query: { page: 1, limit: 10 } }));
35
35
 
36
36
  // Prefetch with plugin options (staleTime, retries, etc.)
37
37
  await prefetch(
38
- (api) => api.users(userId).$get(),
38
+ (api) => api("users/:id").GET({ params: { id: userId } }),
39
39
  {
40
40
  staleTime: 60000,
41
41
  retries: 3,
@@ -45,7 +45,7 @@ await prefetch(
45
45
  // Prefetch on hover
46
46
  <Link
47
47
  href="/posts/1"
48
- onMouseEnter={() => prefetch((api) => api.posts(1).$get())}
48
+ onMouseEnter={() => prefetch((api) => api("posts/:id").GET({ params: { id: 1 } }))}
49
49
  >
50
50
  View Post
51
51
  </Link>
@@ -77,9 +77,9 @@ Multiple calls to prefetch the same data will return the same promise, avoiding
77
77
 
78
78
  ```typescript
79
79
  // These will only make ONE network request
80
- prefetch((api) => api.posts.$get());
81
- prefetch((api) => api.posts.$get());
82
- prefetch((api) => api.posts.$get());
80
+ prefetch((api) => api("posts").GET());
81
+ prefetch((api) => api("posts").GET());
82
+ prefetch((api) => api("posts").GET());
83
83
  ```
84
84
 
85
85
  ### Memory Leak Prevention
package/dist/index.d.mts CHANGED
@@ -52,13 +52,13 @@ declare module "@spoosh/core" {
52
52
  * const { prefetch } = createReactSpoosh(client);
53
53
  *
54
54
  * // Basic prefetch
55
- * await prefetch((api) => api.posts.$get());
55
+ * await prefetch((api) => api("posts").GET());
56
56
  *
57
57
  * // Prefetch with query options
58
- * await prefetch((api) => api.posts.$get({ query: { page: 1, limit: 10 } }));
58
+ * await prefetch((api) => api("posts").GET({ query: { page: 1, limit: 10 } }));
59
59
  *
60
60
  * // Prefetch with plugin options (staleTime, retries, etc.)
61
- * await prefetch((api) => api.users(userId).$get(), {
61
+ * await prefetch((api) => api("users/:id").GET({ params: { id: userId } }), {
62
62
  * staleTime: 60000,
63
63
  * retries: 3,
64
64
  * });
@@ -66,7 +66,7 @@ declare module "@spoosh/core" {
66
66
  * // Prefetch on hover
67
67
  * <Link
68
68
  * href="/posts/1"
69
- * onMouseEnter={() => prefetch((api) => api.posts(1).$get())}
69
+ * onMouseEnter={() => prefetch((api) => api("posts/:id").GET({ params: { id: 1 } }))}
70
70
  * >
71
71
  * View Post
72
72
  * </Link>
package/dist/index.d.ts CHANGED
@@ -52,13 +52,13 @@ declare module "@spoosh/core" {
52
52
  * const { prefetch } = createReactSpoosh(client);
53
53
  *
54
54
  * // Basic prefetch
55
- * await prefetch((api) => api.posts.$get());
55
+ * await prefetch((api) => api("posts").GET());
56
56
  *
57
57
  * // Prefetch with query options
58
- * await prefetch((api) => api.posts.$get({ query: { page: 1, limit: 10 } }));
58
+ * await prefetch((api) => api("posts").GET({ query: { page: 1, limit: 10 } }));
59
59
  *
60
60
  * // Prefetch with plugin options (staleTime, retries, etc.)
61
- * await prefetch((api) => api.users(userId).$get(), {
61
+ * await prefetch((api) => api("users/:id").GET({ params: { id: userId } }), {
62
62
  * staleTime: 60000,
63
63
  * retries: 3,
64
64
  * });
@@ -66,7 +66,7 @@ declare module "@spoosh/core" {
66
66
  * // Prefetch on hover
67
67
  * <Link
68
68
  * href="/posts/1"
69
- * onMouseEnter={() => prefetch((api) => api.posts(1).$get())}
69
+ * onMouseEnter={() => prefetch((api) => api("posts/:id").GET({ params: { id: 1 } }))}
70
70
  * >
71
71
  * View Post
72
72
  * </Link>
package/dist/index.js CHANGED
@@ -58,7 +58,7 @@ function prefetchPlugin(config = {}) {
58
58
  const { api, stateManager, eventEmitter, pluginExecutor } = context;
59
59
  const prefetch = async (selector, options = {}) => {
60
60
  const { tags, additionalTags } = options;
61
- let callPath = [];
61
+ let callPath = "";
62
62
  let callMethod = "";
63
63
  let callOptions = void 0;
64
64
  const selectorProxy = (0, import_core.createSelectorProxy)((result) => {
@@ -74,16 +74,17 @@ function prefetchPlugin(config = {}) {
74
74
  selector(selectorProxy);
75
75
  if (!callMethod) {
76
76
  throw new Error(
77
- "prefetch requires selecting a $get method. Example: prefetch((api) => api.posts.$get())"
77
+ 'prefetch requires selecting a GET method. Example: prefetch((api) => api("posts").GET())'
78
78
  );
79
79
  }
80
- const resolvedPath = (0, import_core.resolvePath)(callPath, void 0);
80
+ const pathSegments = callPath.split("/").filter(Boolean);
81
+ const resolvedPath = (0, import_core.resolvePath)(pathSegments, void 0);
81
82
  const resolvedTags = (0, import_core.resolveTags)(
82
83
  { tags, additionalTags },
83
84
  resolvedPath
84
85
  );
85
86
  const queryKey = stateManager.createQueryKey({
86
- path: callPath,
87
+ path: pathSegments,
87
88
  method: callMethod,
88
89
  options: callOptions
89
90
  });
@@ -91,7 +92,7 @@ function prefetchPlugin(config = {}) {
91
92
  const abortController = new AbortController();
92
93
  const pluginContext = pluginExecutor.createContext({
93
94
  operationType: "read",
94
- path: callPath,
95
+ path: pathSegments,
95
96
  method: callMethod,
96
97
  queryKey,
97
98
  tags: resolvedTags,
@@ -120,11 +121,8 @@ function prefetchPlugin(config = {}) {
120
121
  }
121
122
  };
122
123
  try {
123
- let current = api;
124
- for (const segment of resolvedPath) {
125
- current = current[segment];
126
- }
127
- const method = current[callMethod];
124
+ const pathMethods = api(callPath);
125
+ const method = pathMethods[callMethod];
128
126
  const mergedOptions = {
129
127
  ...callOptions,
130
128
  ...pluginContext.requestOptions
package/dist/index.mjs CHANGED
@@ -37,7 +37,7 @@ function prefetchPlugin(config = {}) {
37
37
  const { api, stateManager, eventEmitter, pluginExecutor } = context;
38
38
  const prefetch = async (selector, options = {}) => {
39
39
  const { tags, additionalTags } = options;
40
- let callPath = [];
40
+ let callPath = "";
41
41
  let callMethod = "";
42
42
  let callOptions = void 0;
43
43
  const selectorProxy = createSelectorProxy((result) => {
@@ -53,16 +53,17 @@ function prefetchPlugin(config = {}) {
53
53
  selector(selectorProxy);
54
54
  if (!callMethod) {
55
55
  throw new Error(
56
- "prefetch requires selecting a $get method. Example: prefetch((api) => api.posts.$get())"
56
+ 'prefetch requires selecting a GET method. Example: prefetch((api) => api("posts").GET())'
57
57
  );
58
58
  }
59
- const resolvedPath = resolvePath(callPath, void 0);
59
+ const pathSegments = callPath.split("/").filter(Boolean);
60
+ const resolvedPath = resolvePath(pathSegments, void 0);
60
61
  const resolvedTags = resolveTags(
61
62
  { tags, additionalTags },
62
63
  resolvedPath
63
64
  );
64
65
  const queryKey = stateManager.createQueryKey({
65
- path: callPath,
66
+ path: pathSegments,
66
67
  method: callMethod,
67
68
  options: callOptions
68
69
  });
@@ -70,7 +71,7 @@ function prefetchPlugin(config = {}) {
70
71
  const abortController = new AbortController();
71
72
  const pluginContext = pluginExecutor.createContext({
72
73
  operationType: "read",
73
- path: callPath,
74
+ path: pathSegments,
74
75
  method: callMethod,
75
76
  queryKey,
76
77
  tags: resolvedTags,
@@ -99,11 +100,8 @@ function prefetchPlugin(config = {}) {
99
100
  }
100
101
  };
101
102
  try {
102
- let current = api;
103
- for (const segment of resolvedPath) {
104
- current = current[segment];
105
- }
106
- const method = current[callMethod];
103
+ const pathMethods = api(callPath);
104
+ const method = pathMethods[callMethod];
107
105
  const mergedOptions = {
108
106
  ...callOptions,
109
107
  ...pluginContext.requestOptions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-prefetch",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Prefetch plugin for Spoosh - preload data before it's needed",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -11,7 +11,7 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/nxnom/spoosh/issues"
13
13
  },
14
- "homepage": "https://spoosh.dev/docs/plugins/prefetch",
14
+ "homepage": "https://spoosh.dev/react/docs/plugins/prefetch",
15
15
  "publishConfig": {
16
16
  "access": "public"
17
17
  },
@@ -33,11 +33,11 @@
33
33
  }
34
34
  },
35
35
  "peerDependencies": {
36
- "@spoosh/core": ">=0.4.0"
36
+ "@spoosh/core": ">=0.6.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@spoosh/core": "0.4.0",
40
- "@spoosh/test-utils": "0.1.4"
39
+ "@spoosh/core": "0.6.0",
40
+ "@spoosh/test-utils": "0.1.5"
41
41
  },
42
42
  "scripts": {
43
43
  "dev": "tsup --watch",