@farm.js/core 0.1.0-beta.1 → 0.1.0-beta.4

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 (254) hide show
  1. package/README.md +1 -1
  2. package/dist/agent-runtime.d.mts +2 -1
  3. package/dist/agent-runtime.d.ts +2 -1
  4. package/dist/api/route-manager.cjs +410 -15
  5. package/dist/api/route-manager.cjs.map +1 -1
  6. package/dist/api/route-manager.d.mts +4 -2
  7. package/dist/api/route-manager.d.ts +4 -2
  8. package/dist/api/route-manager.mjs +9 -8
  9. package/dist/api/runtime.cjs +748 -9
  10. package/dist/api/runtime.cjs.map +1 -1
  11. package/dist/api/runtime.d.mts +3 -16
  12. package/dist/api/runtime.d.ts +3 -16
  13. package/dist/api/runtime.mjs +8 -1
  14. package/dist/api.cjs +1243 -342
  15. package/dist/api.cjs.map +1 -1
  16. package/dist/api.d.mts +5 -147
  17. package/dist/api.d.ts +5 -147
  18. package/dist/api.mjs +51 -21
  19. package/dist/{app-CFRNJXP1.d.ts → app-BjN-da13.d.ts} +38 -3
  20. package/dist/{app-BiX4qb-d.d.mts → app-CakAr8Be.d.mts} +38 -3
  21. package/dist/app-markdown.cjs +1 -1
  22. package/dist/app-markdown.cjs.map +1 -1
  23. package/dist/app-markdown.d.mts +2 -1
  24. package/dist/app-markdown.d.ts +2 -1
  25. package/dist/app-markdown.mjs +1 -1
  26. package/dist/cache.cjs +414 -12
  27. package/dist/cache.cjs.map +1 -1
  28. package/dist/cache.d.mts +120 -6
  29. package/dist/cache.d.ts +120 -6
  30. package/dist/cache.mjs +11 -3
  31. package/dist/{chunk-Y7A27276.mjs → chunk-2CUKEMVR.mjs} +349 -15
  32. package/dist/chunk-2CUKEMVR.mjs.map +1 -0
  33. package/dist/{chunk-QF4PHCMA.mjs → chunk-2ED5Q2GH.mjs} +3 -3
  34. package/dist/chunk-2ED5Q2GH.mjs.map +1 -0
  35. package/dist/{chunk-GZP5W4K3.mjs → chunk-32W4QEJP.mjs} +1 -1
  36. package/dist/chunk-32W4QEJP.mjs.map +1 -0
  37. package/dist/{chunk-TRORP7E4.mjs → chunk-3U3ZUQAQ.mjs} +2 -2
  38. package/dist/{chunk-25D7G34K.mjs → chunk-65A7QIKI.mjs} +5 -5
  39. package/dist/{chunk-25D7G34K.mjs.map → chunk-65A7QIKI.mjs.map} +1 -1
  40. package/dist/{chunk-G3B5BRDY.mjs → chunk-AJWRO5WE.mjs} +539 -61
  41. package/dist/chunk-AJWRO5WE.mjs.map +1 -0
  42. package/dist/chunk-ALCLXXTY.mjs +187 -0
  43. package/dist/chunk-ALCLXXTY.mjs.map +1 -0
  44. package/dist/{chunk-CHFHNEMO.mjs → chunk-AOBXKNS5.mjs} +627 -84
  45. package/dist/chunk-AOBXKNS5.mjs.map +1 -0
  46. package/dist/{chunk-B6N4ZB2H.mjs → chunk-C2ROET2V.mjs} +3 -3
  47. package/dist/{chunk-JN3UU635.mjs → chunk-EJ7T4TPB.mjs} +2 -2
  48. package/dist/chunk-EJ7T4TPB.mjs.map +1 -0
  49. package/dist/{chunk-ZJQNATYV.mjs → chunk-FRPWCMMG.mjs} +3 -3
  50. package/dist/chunk-FRPWCMMG.mjs.map +1 -0
  51. package/dist/{chunk-QM6VV26B.mjs → chunk-GQVGIUSN.mjs} +3 -3
  52. package/dist/{chunk-QM6VV26B.mjs.map → chunk-GQVGIUSN.mjs.map} +1 -1
  53. package/dist/{chunk-UREJA4GZ.mjs → chunk-GXASWWJU.mjs} +1 -1
  54. package/dist/{chunk-UREJA4GZ.mjs.map → chunk-GXASWWJU.mjs.map} +1 -1
  55. package/dist/{chunk-3QJO55ZF.mjs → chunk-HNIQFZKB.mjs} +5 -3
  56. package/dist/{chunk-3QJO55ZF.mjs.map → chunk-HNIQFZKB.mjs.map} +1 -1
  57. package/dist/{chunk-PRNYWD7K.mjs → chunk-JDEOUK5V.mjs} +67 -239
  58. package/dist/chunk-JDEOUK5V.mjs.map +1 -0
  59. package/dist/{chunk-O3HADRW6.mjs → chunk-MIUW47JX.mjs} +2 -2
  60. package/dist/{chunk-O3HADRW6.mjs.map → chunk-MIUW47JX.mjs.map} +1 -1
  61. package/dist/{chunk-UHE2TTLK.mjs → chunk-NM3CRAAL.mjs} +9 -8
  62. package/dist/chunk-NM3CRAAL.mjs.map +1 -0
  63. package/dist/chunk-PFE6GMNE.mjs +603 -0
  64. package/dist/chunk-PFE6GMNE.mjs.map +1 -0
  65. package/dist/{chunk-S2XHZS7W.mjs → chunk-QGQX7SFC.mjs} +2 -2
  66. package/dist/chunk-QGQX7SFC.mjs.map +1 -0
  67. package/dist/{chunk-RKI6TN3A.mjs → chunk-QM746KUO.mjs} +9 -2
  68. package/dist/chunk-QM746KUO.mjs.map +1 -0
  69. package/dist/chunk-RBRLLZL5.mjs +81 -0
  70. package/dist/chunk-RBRLLZL5.mjs.map +1 -0
  71. package/dist/{chunk-HXP3ZSPC.mjs → chunk-SADGIRS2.mjs} +3 -3
  72. package/dist/{chunk-6XDUHOEM.mjs → chunk-TQAEEJB4.mjs} +3 -3
  73. package/dist/{chunk-AGS7UJ52.mjs → chunk-UB5UOSHB.mjs} +24 -12
  74. package/dist/chunk-UB5UOSHB.mjs.map +1 -0
  75. package/dist/{chunk-SXZTEK3V.mjs → chunk-UXUAWNO5.mjs} +2 -2
  76. package/dist/chunk-V3XURYJO.mjs +756 -0
  77. package/dist/chunk-V3XURYJO.mjs.map +1 -0
  78. package/dist/{chunk-TU4YJICB.mjs → chunk-VVKWFZAH.mjs} +41 -16
  79. package/dist/chunk-VVKWFZAH.mjs.map +1 -0
  80. package/dist/{chunk-JLWMCADZ.mjs → chunk-VY2UYN5R.mjs} +24 -4
  81. package/dist/chunk-VY2UYN5R.mjs.map +1 -0
  82. package/dist/{chunk-QFQRHRZS.mjs → chunk-W2CRQFF6.mjs} +151 -23
  83. package/dist/chunk-W2CRQFF6.mjs.map +1 -0
  84. package/dist/chunk-W4UIVMVI.mjs +10 -0
  85. package/dist/chunk-W4UIVMVI.mjs.map +1 -0
  86. package/dist/{chunk-Y2OJJD7R.mjs → chunk-XIVV5IWN.mjs} +7 -9
  87. package/dist/chunk-XIVV5IWN.mjs.map +1 -0
  88. package/dist/{chunk-OYXO65LI.mjs → chunk-YW7OBE6Q.mjs} +4 -4
  89. package/dist/chunk-YW7OBE6Q.mjs.map +1 -0
  90. package/dist/{chunk-YHU7SS6L.mjs → chunk-Z5CQMP54.mjs} +11 -11
  91. package/dist/{chunk-SOU3Q3ZA.mjs → chunk-ZZ57EH5Z.mjs} +1 -1
  92. package/dist/chunk-ZZ57EH5Z.mjs.map +1 -0
  93. package/dist/{client-D_yUjVhe.d.mts → client-BT1mFQas.d.mts} +55 -10
  94. package/dist/{client-Dkc03WP8.d.ts → client-Nn0q4eO4.d.ts} +55 -10
  95. package/dist/{client-plugins-BZnpeTGA.d.ts → client-plugins-Cw4qUGsw.d.ts} +7 -0
  96. package/dist/{client-plugins-g4bzuCmo.d.mts → client-plugins-DQQ1adXM.d.mts} +7 -0
  97. package/dist/client-plugins.d.mts +2 -1
  98. package/dist/client-plugins.d.ts +2 -1
  99. package/dist/client.cjs +922 -91
  100. package/dist/client.cjs.map +1 -1
  101. package/dist/client.d.mts +121 -3
  102. package/dist/client.d.ts +121 -3
  103. package/dist/client.mjs +23 -16
  104. package/dist/config.d.mts +2 -1
  105. package/dist/config.d.ts +2 -1
  106. package/dist/cron.cjs.map +1 -1
  107. package/dist/cron.mjs +1 -1
  108. package/dist/{devtools-AE4RVXCM.mjs → devtools-LVC3VB5P.mjs} +10 -10
  109. package/dist/{devtools-client-6WPY2AQU.mjs → devtools-client-3XRTBND5.mjs} +2 -11
  110. package/dist/devtools-client-3XRTBND5.mjs.map +1 -0
  111. package/dist/docs.cjs +14 -2
  112. package/dist/docs.cjs.map +1 -1
  113. package/dist/docs.mjs +10 -10
  114. package/dist/i18n/server.cjs +2 -2
  115. package/dist/i18n/server.cjs.map +1 -1
  116. package/dist/i18n/server.mjs +1 -1
  117. package/dist/index.cjs +2852 -463
  118. package/dist/index.cjs.map +1 -1
  119. package/dist/index.d.mts +13 -12
  120. package/dist/index.d.ts +13 -12
  121. package/dist/index.mjs +152 -109
  122. package/dist/index.mjs.map +1 -1
  123. package/dist/integrations.d.mts +2 -1
  124. package/dist/integrations.d.ts +2 -1
  125. package/dist/internal/build-runtime.cjs +1616 -149
  126. package/dist/internal/build-runtime.cjs.map +1 -1
  127. package/dist/internal/build-runtime.d.mts +4 -3
  128. package/dist/internal/build-runtime.d.ts +4 -3
  129. package/dist/internal/build-runtime.mjs +27 -25
  130. package/dist/internal/client-runtime.cjs.map +1 -1
  131. package/dist/internal/client-runtime.mjs +1 -1
  132. package/dist/internal/config-runtime.cjs +13 -2
  133. package/dist/internal/config-runtime.cjs.map +1 -1
  134. package/dist/internal/config-runtime.d.mts +3 -2
  135. package/dist/internal/config-runtime.d.ts +3 -2
  136. package/dist/internal/config-runtime.mjs +9 -9
  137. package/dist/internal/production-runtime.cjs +285 -9
  138. package/dist/internal/production-runtime.cjs.map +1 -1
  139. package/dist/internal/production-runtime.d.mts +4 -4
  140. package/dist/internal/production-runtime.d.ts +4 -4
  141. package/dist/internal/production-runtime.mjs +10 -8
  142. package/dist/{metadata-DAXRaOHx.d.ts → metadata-BAPg_Nmo.d.ts} +1 -1
  143. package/dist/{metadata-C9NZxMDW.d.mts → metadata-_6i9oFG3.d.mts} +1 -1
  144. package/dist/middleware.cjs +2 -2
  145. package/dist/middleware.cjs.map +1 -1
  146. package/dist/middleware.d.mts +4 -3
  147. package/dist/middleware.d.ts +4 -3
  148. package/dist/middleware.mjs +3 -3
  149. package/dist/navigation.cjs +40 -15
  150. package/dist/navigation.cjs.map +1 -1
  151. package/dist/navigation.mjs +3 -3
  152. package/dist/{nitro-KME62MWG.mjs → nitro-QO5LV6QK.mjs} +11 -13
  153. package/dist/{nitro-KME62MWG.mjs.map → nitro-QO5LV6QK.mjs.map} +1 -1
  154. package/dist/plugin.cjs +1032 -92
  155. package/dist/plugin.cjs.map +1 -1
  156. package/dist/plugin.d.mts +2 -1
  157. package/dist/plugin.d.ts +2 -1
  158. package/dist/plugin.mjs +23 -21
  159. package/dist/{production-runtime-DRkCHUvJ.d.ts → production-runtime-BoJDc5mc.d.ts} +1 -1
  160. package/dist/{production-runtime-flrjp_nC.d.mts → production-runtime-DPfgMV9Y.d.mts} +1 -1
  161. package/dist/routes.cjs +276 -6
  162. package/dist/routes.cjs.map +1 -1
  163. package/dist/routes.d.mts +1 -1
  164. package/dist/routes.d.ts +1 -1
  165. package/dist/routes.mjs +4 -4
  166. package/dist/runtime-9nQjVM7X.d.mts +206 -0
  167. package/dist/runtime-DgeS2skB.d.ts +206 -0
  168. package/dist/server-action-security.cjs +28 -10
  169. package/dist/server-action-security.cjs.map +1 -1
  170. package/dist/server-action-security.d.mts +2 -1
  171. package/dist/server-action-security.d.ts +2 -1
  172. package/dist/server-action-security.mjs +2 -2
  173. package/dist/server-fn-client.cjs.map +1 -1
  174. package/dist/server-fn-client.d.mts +1 -0
  175. package/dist/server-fn-client.d.ts +1 -0
  176. package/dist/server-fn-client.mjs +1 -1
  177. package/dist/server-fn.cjs +785 -3
  178. package/dist/server-fn.cjs.map +1 -1
  179. package/dist/server-fn.d.mts +10 -1
  180. package/dist/server-fn.d.ts +10 -1
  181. package/dist/server-fn.mjs +6 -3
  182. package/dist/server-plugins.d.mts +3 -2
  183. package/dist/server-plugins.d.ts +3 -2
  184. package/dist/server-query-client.cjs +278 -7
  185. package/dist/server-query-client.cjs.map +1 -1
  186. package/dist/server-query-client.mjs +4 -4
  187. package/dist/server-query.cjs +381 -10
  188. package/dist/server-query.cjs.map +1 -1
  189. package/dist/server-query.mjs +5 -5
  190. package/dist/server.cjs +1233 -118
  191. package/dist/server.cjs.map +1 -1
  192. package/dist/server.d.mts +8 -7
  193. package/dist/server.d.ts +8 -7
  194. package/dist/server.mjs +53 -33
  195. package/dist/server.mjs.map +1 -1
  196. package/dist/testing.cjs +436 -25
  197. package/dist/testing.cjs.map +1 -1
  198. package/dist/testing.d.mts +4 -3
  199. package/dist/testing.d.ts +4 -3
  200. package/dist/testing.mjs +19 -18
  201. package/dist/testing.mjs.map +1 -1
  202. package/dist/transport-BmbGTXA5.d.mts +49 -0
  203. package/dist/transport-BmbGTXA5.d.ts +49 -0
  204. package/dist/{utils-DOXXivjL.d.mts → utils-CHXlMDkg.d.mts} +1 -1
  205. package/dist/{utils-DA1yo54_.d.ts → utils-Drsyz1Vj.d.ts} +1 -1
  206. package/dist/version.cjs +36 -0
  207. package/dist/version.cjs.map +1 -0
  208. package/dist/version.d.mts +3 -0
  209. package/dist/version.d.ts +3 -0
  210. package/dist/version.mjs +8 -0
  211. package/dist/version.mjs.map +1 -0
  212. package/dist/vite.cjs +1039 -93
  213. package/dist/vite.cjs.map +1 -1
  214. package/dist/vite.d.mts +2 -1
  215. package/dist/vite.d.ts +2 -1
  216. package/dist/vite.mjs +23 -21
  217. package/dist/workflows.cjs +6 -8
  218. package/dist/workflows.cjs.map +1 -1
  219. package/dist/workflows.mjs +1 -1
  220. package/package.json +12 -5
  221. package/types/api.d.ts +44 -0
  222. package/types/client.d.ts +220 -16
  223. package/dist/chunk-4PTK6KT4.mjs +0 -364
  224. package/dist/chunk-4PTK6KT4.mjs.map +0 -1
  225. package/dist/chunk-AGS7UJ52.mjs.map +0 -1
  226. package/dist/chunk-CHFHNEMO.mjs.map +0 -1
  227. package/dist/chunk-EVGBEZDF.mjs +0 -215
  228. package/dist/chunk-EVGBEZDF.mjs.map +0 -1
  229. package/dist/chunk-G3B5BRDY.mjs.map +0 -1
  230. package/dist/chunk-GZP5W4K3.mjs.map +0 -1
  231. package/dist/chunk-JLWMCADZ.mjs.map +0 -1
  232. package/dist/chunk-JN3UU635.mjs.map +0 -1
  233. package/dist/chunk-OYXO65LI.mjs.map +0 -1
  234. package/dist/chunk-PRNYWD7K.mjs.map +0 -1
  235. package/dist/chunk-QF4PHCMA.mjs.map +0 -1
  236. package/dist/chunk-QFQRHRZS.mjs.map +0 -1
  237. package/dist/chunk-RKI6TN3A.mjs.map +0 -1
  238. package/dist/chunk-S2XHZS7W.mjs.map +0 -1
  239. package/dist/chunk-SOU3Q3ZA.mjs.map +0 -1
  240. package/dist/chunk-TU4YJICB.mjs.map +0 -1
  241. package/dist/chunk-TXCFGOHW.mjs +0 -42
  242. package/dist/chunk-TXCFGOHW.mjs.map +0 -1
  243. package/dist/chunk-UHE2TTLK.mjs.map +0 -1
  244. package/dist/chunk-Y2OJJD7R.mjs.map +0 -1
  245. package/dist/chunk-Y7A27276.mjs.map +0 -1
  246. package/dist/chunk-ZJQNATYV.mjs.map +0 -1
  247. package/dist/devtools-client-6WPY2AQU.mjs.map +0 -1
  248. /package/dist/{chunk-TRORP7E4.mjs.map → chunk-3U3ZUQAQ.mjs.map} +0 -0
  249. /package/dist/{chunk-B6N4ZB2H.mjs.map → chunk-C2ROET2V.mjs.map} +0 -0
  250. /package/dist/{chunk-HXP3ZSPC.mjs.map → chunk-SADGIRS2.mjs.map} +0 -0
  251. /package/dist/{chunk-6XDUHOEM.mjs.map → chunk-TQAEEJB4.mjs.map} +0 -0
  252. /package/dist/{chunk-SXZTEK3V.mjs.map → chunk-UXUAWNO5.mjs.map} +0 -0
  253. /package/dist/{chunk-YHU7SS6L.mjs.map → chunk-Z5CQMP54.mjs.map} +0 -0
  254. /package/dist/{devtools-AE4RVXCM.mjs.map → devtools-LVC3VB5P.mjs.map} +0 -0
package/types/client.d.ts CHANGED
@@ -8,11 +8,15 @@
8
8
 
9
9
  import type {
10
10
  AnchorHTMLAttributes,
11
+ ComponentType,
12
+ FormEvent,
13
+ FormHTMLAttributes,
11
14
  ReactElement,
12
15
  ReactNode,
13
16
  RefObject,
14
17
  RefAttributes,
15
18
  } from "react";
19
+ import type { DefinedCacheKey, InferCacheKeyData, RouteDataCacheKey } from "@farm.js/core/cache";
16
20
 
17
21
  declare module "@farm.js/core/client" {
18
22
  export interface MiddlewareProps {
@@ -378,6 +382,36 @@ declare module "@farm.js/core/client" {
378
382
  key: CacheKey<TData>;
379
383
  };
380
384
 
385
+ export interface FarmAPIStream<TItem> extends AsyncIterable<TItem> {
386
+ readonly response: Response;
387
+ cancel(reason?: unknown): Promise<void>;
388
+ }
389
+
390
+ export class APIClientError<
391
+ TCode extends string = string,
392
+ TData = unknown,
393
+ TStatus extends number = number,
394
+ > extends Error {
395
+ readonly code: TCode;
396
+ readonly data: TData;
397
+ readonly status: TStatus;
398
+ readonly response?: Response;
399
+
400
+ constructor(
401
+ code: TCode,
402
+ data: TData,
403
+ options: {
404
+ status: TStatus;
405
+ message: string;
406
+ response?: Response;
407
+ },
408
+ );
409
+ }
410
+
411
+ export type APIClientSystemError =
412
+ | APIClientError<"http_error", unknown, number>
413
+ | APIClientError<"network_error", unknown, 0>;
414
+
381
415
  export type RequestEvent = {
382
416
  requestId: string;
383
417
  method: StatusEvent["method"];
@@ -406,7 +440,7 @@ declare module "@farm.js/core/client" {
406
440
  export type CachePolicy = "cache-first" | "network-only" | "stale-while-revalidate";
407
441
 
408
442
  export type CacheOptions = {
409
- key?: string;
443
+ key?: RouteDataCacheKey;
410
444
  policy?: CachePolicy;
411
445
  staleTime?: number;
412
446
  gcTime?: number;
@@ -419,9 +453,9 @@ declare module "@farm.js/core/client" {
419
453
  };
420
454
 
421
455
  export type InvalidateTarget =
422
- | string
456
+ | RouteDataCacheKey
423
457
  | {
424
- key: string;
458
+ key: RouteDataCacheKey;
425
459
  }
426
460
  | {
427
461
  path: string;
@@ -439,7 +473,7 @@ declare module "@farm.js/core/client" {
439
473
 
440
474
  export type OptimisticUpdate =
441
475
  | [CallableRouteRef<any>, unknown, (prev: any) => any]
442
- | [CacheKey<any> | string, (prev: any) => any];
476
+ | [CacheKey<any> | DefinedCacheKey<any> | string, (prev: any) => any];
443
477
 
444
478
  export type OptimisticOptions<TUpdates extends readonly unknown[] = readonly OptimisticUpdate[]> =
445
479
  {
@@ -452,7 +486,7 @@ declare module "@farm.js/core/client" {
452
486
  TError = unknown,
453
487
  TUpdates extends readonly unknown[] = readonly OptimisticUpdate[],
454
488
  > = {
455
- key?: CacheKey<TData> | string;
489
+ key?: CacheKey<TData> | RouteDataCacheKey;
456
490
  cache?: CacheOptions;
457
491
  retry?: RetryOptions;
458
492
  invalidate?: InvalidateOptions;
@@ -496,11 +530,13 @@ declare module "@farm.js/core/client" {
496
530
  ]
497
531
  : never
498
532
  : TUpdate extends readonly [infer TKey, (prev: any) => any]
499
- ? TKey extends CacheKey<infer TData>
500
- ? [TKey, (prev: TData | undefined) => TData]
501
- : TKey extends string
502
- ? [TKey, (prev: unknown) => unknown]
503
- : never
533
+ ? TKey extends DefinedCacheKey<any, RouteDataCacheKey>
534
+ ? [TKey, (prev: InferCacheKeyData<TKey> | undefined) => InferCacheKeyData<TKey>]
535
+ : TKey extends CacheKey<infer TData>
536
+ ? [TKey, (prev: TData | undefined) => TData]
537
+ : TKey extends string
538
+ ? [TKey, (prev: unknown) => unknown]
539
+ : never
504
540
  : never;
505
541
 
506
542
  type NormalizeOptimisticUpdates<TUpdates extends readonly unknown[]> = {
@@ -514,8 +550,10 @@ declare module "@farm.js/core/client" {
514
550
  type TypedEndpointLike = {
515
551
  __types: {
516
552
  body: any;
553
+ inputBody?: any;
517
554
  query: any;
518
555
  response: any;
556
+ errors?: any;
519
557
  };
520
558
  };
521
559
 
@@ -554,13 +592,26 @@ declare module "@farm.js/core/client" {
554
592
  type HasRequiredKeys<T> = RequiredKeys<T> extends never ? false : true;
555
593
 
556
594
  // Type utilities to extract endpoint input/output types
595
+ type InferEndpointBody<T> = T extends {
596
+ __types: {
597
+ inputBody: infer TInputBody;
598
+ };
599
+ }
600
+ ? TInputBody
601
+ : T extends {
602
+ __types: {
603
+ body: infer TBody;
604
+ };
605
+ }
606
+ ? TBody
607
+ : never;
608
+
557
609
  type InferEndpointInput<T> = T extends {
558
610
  __types: {
559
- body: infer TBody;
560
611
  query: infer TQuery;
561
612
  };
562
613
  }
563
- ? SimplifyEndpointInput<BodyInputProp<TBody> & QueryInputProp<TQuery>>
614
+ ? SimplifyEndpointInput<BodyInputProp<InferEndpointBody<T>> & QueryInputProp<TQuery>>
564
615
  : {};
565
616
 
566
617
  type InferEndpointOutput<T> = T extends {
@@ -568,24 +619,177 @@ declare module "@farm.js/core/client" {
568
619
  response: infer R;
569
620
  };
570
621
  }
571
- ? R
622
+ ? R extends { readonly __farmStreamItem: infer TItem }
623
+ ? FarmAPIStream<TItem>
624
+ : R
572
625
  : any;
573
626
 
627
+ type InferEndpointError<T> = T extends {
628
+ __types: {
629
+ errors: infer TErrors;
630
+ };
631
+ }
632
+ ? keyof TErrors extends never
633
+ ? Error
634
+ :
635
+ | {
636
+ [TCode in keyof TErrors]: TErrors[TCode] extends {
637
+ data: infer TData;
638
+ status: infer TStatus extends number;
639
+ }
640
+ ? APIClientError<TCode & string, TData, TStatus>
641
+ : never;
642
+ }[keyof TErrors]
643
+ | APIClientSystemError
644
+ : Error;
645
+
574
646
  type EndpointMethod<T = any> = (<
575
647
  TUpdates extends readonly unknown[] = readonly OptimisticUpdate[],
576
648
  >(
577
649
  ...args: HasRequiredKeys<InferEndpointInput<T>> extends true
578
650
  ? [
579
651
  options: InferEndpointInput<T>,
580
- clientOptions?: ClientOptions<InferEndpointOutput<T>, Error, TUpdates>,
652
+ clientOptions?: ClientOptions<InferEndpointOutput<T>, InferEndpointError<T>, TUpdates>,
581
653
  ]
582
654
  : [
583
655
  options?: InferEndpointInput<T>,
584
- clientOptions?: ClientOptions<InferEndpointOutput<T>, Error, TUpdates>,
656
+ clientOptions?: ClientOptions<InferEndpointOutput<T>, InferEndpointError<T>, TUpdates>,
585
657
  ]
586
- ) => Promise<APIResult<InferEndpointOutput<T>, Error>>) &
658
+ ) => Promise<APIResult<InferEndpointOutput<T>, InferEndpointError<T>>>) &
587
659
  RouteRef<InferEndpointOutput<T>, InferEndpointInput<T>>;
588
660
 
661
+ export type MutationStatus = "idle" | "pending" | "success" | "error";
662
+
663
+ export type AnyMutationTarget = (...args: any[]) => Promise<any>;
664
+
665
+ export type InferMutationVariables<TTarget extends AnyMutationTarget> =
666
+ Parameters<TTarget> extends [] ? undefined : Parameters<TTarget>[0];
667
+
668
+ export type InferMutationData<TTarget extends AnyMutationTarget> = TTarget extends {
669
+ readonly __farmRouteData: infer TData;
670
+ }
671
+ ? TData
672
+ : Awaited<ReturnType<TTarget>>;
673
+
674
+ export type InferMutationError<TTarget extends AnyMutationTarget> = TTarget extends (
675
+ ...args: any[]
676
+ ) => Promise<APIResult<any, infer TError>>
677
+ ? TError
678
+ : Error;
679
+
680
+ export type MutationOptimisticContext<TVariables, TData> = {
681
+ variables: TVariables | undefined;
682
+ current: TData | null;
683
+ };
684
+
685
+ export type UseMutationOptions<TVariables, TData, TError = Error> = {
686
+ initialData?: TData | null;
687
+ resetOnMutate?: boolean;
688
+ optimistic?: (
689
+ context: MutationOptimisticContext<TVariables, TData>,
690
+ ) => TData | null | undefined;
691
+ rollbackOnError?: boolean;
692
+ request?: ClientOptions<TData, TError>;
693
+ onSuccess?: (data: TData, variables: TVariables | undefined) => void;
694
+ onError?: (error: TError, variables: TVariables | undefined) => void;
695
+ onSettled?: (
696
+ data: TData | null,
697
+ error: TError | null,
698
+ variables: TVariables | undefined,
699
+ ) => void;
700
+ };
701
+
702
+ export type MutationAsync<TTarget extends AnyMutationTarget, TData = InferMutationData<TTarget>> =
703
+ [] extends Parameters<TTarget>
704
+ ? (variables?: InferMutationVariables<TTarget>) => Promise<TData>
705
+ : (variables: InferMutationVariables<TTarget>) => Promise<TData>;
706
+
707
+ export type MutationTrigger<TTarget extends AnyMutationTarget> =
708
+ [] extends Parameters<TTarget>
709
+ ? (variables?: InferMutationVariables<TTarget>) => void
710
+ : (variables: InferMutationVariables<TTarget>) => void;
711
+
712
+ export type UseMutationReturn<
713
+ TTarget extends AnyMutationTarget,
714
+ TData = InferMutationData<TTarget>,
715
+ TError = InferMutationError<TTarget>,
716
+ > = {
717
+ pending: boolean;
718
+ status: MutationStatus;
719
+ data: TData | null;
720
+ error: TError | null;
721
+ variables: InferMutationVariables<TTarget> | undefined;
722
+ mutate: MutationTrigger<TTarget>;
723
+ mutateAsync: MutationAsync<TTarget, TData>;
724
+ reset: () => void;
725
+ };
726
+
727
+ export function useMutation<TTarget extends AnyMutationTarget>(
728
+ target: TTarget,
729
+ options?: UseMutationOptions<
730
+ InferMutationVariables<TTarget>,
731
+ InferMutationData<TTarget>,
732
+ InferMutationError<TTarget>
733
+ >,
734
+ ): UseMutationReturn<TTarget>;
735
+
736
+ export type FetcherState = "idle" | "submitting";
737
+
738
+ export type FetcherFormDataContext = {
739
+ form: HTMLFormElement | null;
740
+ submitter: HTMLElement | null;
741
+ };
742
+
743
+ export type UseFetcherOptions<TTarget extends AnyMutationTarget> = UseMutationOptions<
744
+ InferMutationVariables<TTarget>,
745
+ InferMutationData<TTarget>,
746
+ InferMutationError<TTarget>
747
+ > & {
748
+ mapFormData?: (
749
+ formData: FormData,
750
+ context: FetcherFormDataContext,
751
+ ) => InferMutationVariables<TTarget>;
752
+ };
753
+
754
+ export type FetcherFormProps = Omit<
755
+ FormHTMLAttributes<HTMLFormElement>,
756
+ "action" | "onSubmit"
757
+ > & {
758
+ action?: string | ((formData: FormData) => void | Promise<void>);
759
+ onSubmit?: (event: FormEvent<HTMLFormElement>) => void;
760
+ };
761
+
762
+ type FetcherInput<TTarget extends AnyMutationTarget> = InferMutationVariables<TTarget> | FormData;
763
+
764
+ export type FetcherSubmitAsync<TTarget extends AnyMutationTarget> =
765
+ [] extends Parameters<TTarget>
766
+ ? (input?: FetcherInput<TTarget>) => Promise<InferMutationData<TTarget>>
767
+ : (input: FetcherInput<TTarget>) => Promise<InferMutationData<TTarget>>;
768
+
769
+ export type FetcherSubmit<TTarget extends AnyMutationTarget> =
770
+ [] extends Parameters<TTarget>
771
+ ? (input?: FetcherInput<TTarget>) => void
772
+ : (input: FetcherInput<TTarget>) => void;
773
+
774
+ export type UseFetcherReturn<TTarget extends AnyMutationTarget> = {
775
+ state: FetcherState;
776
+ status: MutationStatus;
777
+ pending: boolean;
778
+ data: InferMutationData<TTarget> | null;
779
+ error: InferMutationError<TTarget> | null;
780
+ variables: InferMutationVariables<TTarget> | undefined;
781
+ formData: FormData | null;
782
+ submit: FetcherSubmit<TTarget>;
783
+ submitAsync: FetcherSubmitAsync<TTarget>;
784
+ Form: ComponentType<FetcherFormProps>;
785
+ reset: () => void;
786
+ };
787
+
788
+ export function useFetcher<TTarget extends AnyMutationTarget>(
789
+ target: TTarget,
790
+ options?: UseFetcherOptions<TTarget>,
791
+ ): UseFetcherReturn<TTarget>;
792
+
589
793
  type RouterToClient<T> = {
590
794
  [K in keyof T]: T[K] extends TypedEndpointLike
591
795
  ? EndpointMethod<T[K]>
@@ -1,364 +0,0 @@
1
- import {
2
- notifyFarmCacheInvalidation
3
- } from "./chunk-TXCFGOHW.mjs";
4
- import {
5
- getActiveFarmI18nSnapshot
6
- } from "./chunk-WZ5QFROO.mjs";
7
- import {
8
- emitFarmEvent
9
- } from "./chunk-7ZY5AIJO.mjs";
10
- import {
11
- __name
12
- } from "./chunk-CGILA3WO.mjs";
13
-
14
- // src/cache.ts
15
- var _FarmDataCache = class _FarmDataCache {
16
- constructor() {
17
- this.entries = /* @__PURE__ */ new Map();
18
- this.inflight = /* @__PURE__ */ new Map();
19
- this.invalidatedTagVersions = /* @__PURE__ */ new Map();
20
- this.version = 0;
21
- }
22
- get size() {
23
- return this.entries.size;
24
- }
25
- get(key, options = {}) {
26
- return this.getEntry(key, options)?.value;
27
- }
28
- getEntry(key, options = {}) {
29
- const entry = this.entries.get(key);
30
- if (!entry) {
31
- emitFarmEvent({ type: "cache.miss", key });
32
- return void 0;
33
- }
34
- const stale = this.isStale(entry, options.now);
35
- if (stale) {
36
- emitFarmEvent({
37
- type: "cache.stale",
38
- key,
39
- tags: Array.from(entry.tags),
40
- revalidate: entry.revalidate
41
- });
42
- }
43
- if (!options.allowStale && stale) {
44
- emitFarmEvent({ type: "cache.miss", key, reason: "stale" });
45
- return void 0;
46
- }
47
- emitFarmEvent({
48
- type: "cache.hit",
49
- key,
50
- tags: Array.from(entry.tags),
51
- revalidate: entry.revalidate,
52
- stale
53
- });
54
- return this.toPublicEntry(entry);
55
- }
56
- set(key, value, options = {}) {
57
- const tags = /* @__PURE__ */ new Set();
58
- for (const tag of options.tags ?? []) {
59
- tags.add(normalizeCacheTag(tag));
60
- }
61
- for (const routePath of options.paths ?? []) {
62
- tags.add(createPathCacheTag(routePath));
63
- }
64
- const entry = {
65
- key,
66
- value,
67
- tags,
68
- createdAt: options.createdAt ?? Date.now(),
69
- createdVersion: ++this.version,
70
- revalidate: normalizeRevalidate(options.revalidate)
71
- };
72
- this.entries.set(key, entry);
73
- emitFarmEvent({
74
- type: "cache.set",
75
- key,
76
- tags: Array.from(tags),
77
- revalidate: entry.revalidate
78
- });
79
- return this.toPublicEntry(entry);
80
- }
81
- delete(key) {
82
- const deleted = this.entries.delete(key);
83
- emitFarmEvent({ type: "cache.delete", key, deleted });
84
- return deleted;
85
- }
86
- clear() {
87
- const count = this.entries.size;
88
- this.entries.clear();
89
- this.inflight.clear();
90
- this.invalidatedTagVersions.clear();
91
- this.version = 0;
92
- emitFarmEvent({ type: "cache.clear", count });
93
- }
94
- isStale(entry, now = Date.now()) {
95
- if (typeof entry.revalidate === "number" && entry.revalidate > 0 && now - entry.createdAt >= entry.revalidate * 1e3) {
96
- return true;
97
- }
98
- for (const tag of entry.tags) {
99
- const invalidatedVersion = this.invalidatedTagVersions.get(normalizeCacheTag(tag));
100
- if (typeof invalidatedVersion === "number" && typeof entry.createdVersion === "number" && invalidatedVersion > entry.createdVersion) {
101
- return true;
102
- }
103
- }
104
- return false;
105
- }
106
- revalidateTag(tag, options = {}) {
107
- const normalized = normalizeCacheTag(tag);
108
- const count = this.invalidateTag(normalized);
109
- emitFarmEvent(
110
- options.source === "updateTag" ? { type: "cache.updateTag", tag: normalized, count } : { type: "cache.revalidateTag", tag: normalized, profile: options.profile, count }
111
- );
112
- return count;
113
- }
114
- revalidatePath(routePath) {
115
- const normalizedPath = normalizeRevalidatePath(routePath);
116
- const pathTag = createPathCacheTag(normalizedPath);
117
- const pprCount = this.countEntriesForTags([pathTag, "ppr"]);
118
- const count = this.invalidateTag(pathTag);
119
- emitFarmEvent({ type: "cache.revalidatePath", path: normalizedPath, count });
120
- if (pprCount > 0) {
121
- emitFarmEvent({
122
- type: "ppr.shell.invalidated",
123
- route: normalizedPath,
124
- reason: "revalidatePath",
125
- count: pprCount
126
- });
127
- }
128
- return count;
129
- }
130
- async getOrSet(key, producer, options = {}) {
131
- const cached = this.getEntry(key);
132
- if (cached) {
133
- return cached.value;
134
- }
135
- const inflight = this.inflight.get(key);
136
- if (inflight) {
137
- emitFarmEvent({ type: "cache.dedupe", key });
138
- return inflight;
139
- }
140
- const promise = Promise.resolve().then(producer).then((value) => {
141
- this.set(key, value, options);
142
- return value;
143
- }).catch((error) => {
144
- emitFarmEvent({ type: "cache.error", key, operation: "set", error });
145
- throw error;
146
- }).finally(() => {
147
- this.inflight.delete(key);
148
- });
149
- this.inflight.set(key, promise);
150
- return promise;
151
- }
152
- countEntriesForTag(tag) {
153
- let count = 0;
154
- for (const entry of this.entries.values()) {
155
- if (entry.tags.has(tag)) {
156
- count++;
157
- }
158
- }
159
- return count;
160
- }
161
- countEntriesForTags(tags) {
162
- let count = 0;
163
- for (const entry of this.entries.values()) {
164
- if (tags.every((tag) => entry.tags.has(tag))) {
165
- count++;
166
- }
167
- }
168
- return count;
169
- }
170
- invalidateTag(normalizedTag) {
171
- this.invalidatedTagVersions.set(normalizedTag, ++this.version);
172
- return this.countEntriesForTag(normalizedTag);
173
- }
174
- toPublicEntry(entry) {
175
- return {
176
- key: entry.key,
177
- value: entry.value,
178
- tags: Array.from(entry.tags),
179
- createdAt: entry.createdAt,
180
- createdVersion: entry.createdVersion,
181
- revalidate: entry.revalidate
182
- };
183
- }
184
- };
185
- __name(_FarmDataCache, "FarmDataCache");
186
- var FarmDataCache = _FarmDataCache;
187
- var FARM_DATA_CACHE_SYMBOL = /* @__PURE__ */ Symbol.for("farm.dataCache");
188
- var farmDataCacheGlobal = globalThis;
189
- var sharedFarmDataCache = farmDataCacheGlobal[FARM_DATA_CACHE_SYMBOL] ?? (farmDataCacheGlobal[FARM_DATA_CACHE_SYMBOL] = new FarmDataCache());
190
- var functionIds = /* @__PURE__ */ new WeakMap();
191
- var nextFunctionId = 0;
192
- function getFarmDataCache() {
193
- return sharedFarmDataCache;
194
- }
195
- __name(getFarmDataCache, "getFarmDataCache");
196
- function unstable_cache(fn, keyParts = [], options = {}) {
197
- return async (...args) => {
198
- const locale = getActiveFarmI18nSnapshot()?.locale;
199
- const key = createFarmCacheKey([
200
- "unstable_cache",
201
- getFunctionCacheIdentity(fn),
202
- locale ? ["locale", locale] : null,
203
- keyParts,
204
- args
205
- ]);
206
- return getFarmDataCache().getOrSet(key, () => fn(...args), options);
207
- };
208
- }
209
- __name(unstable_cache, "unstable_cache");
210
- function revalidateTag(_tag, _profile) {
211
- getFarmDataCache().revalidateTag(_tag, {
212
- source: "revalidateTag",
213
- profile: _profile
214
- });
215
- }
216
- __name(revalidateTag, "revalidateTag");
217
- function updateTag(tag) {
218
- getFarmDataCache().revalidateTag(tag, { source: "updateTag" });
219
- }
220
- __name(updateTag, "updateTag");
221
- function revalidatePath(routePath) {
222
- getFarmDataCache().revalidatePath(routePath);
223
- }
224
- __name(revalidatePath, "revalidatePath");
225
- function invalidate(key) {
226
- const clientKey = createRouteDataCacheKey(key);
227
- getFarmDataCache().revalidateTag(createRouteDataCacheTag(key), { source: "updateTag" });
228
- notifyFarmCacheInvalidation(clientKey);
229
- }
230
- __name(invalidate, "invalidate");
231
- function invalidateRouteData(key) {
232
- invalidate(key);
233
- }
234
- __name(invalidateRouteData, "invalidateRouteData");
235
- function createPathCacheTag(routePath) {
236
- return `path:${normalizeRevalidatePath(routePath)}`;
237
- }
238
- __name(createPathCacheTag, "createPathCacheTag");
239
- function createRouteDataCacheTag(key) {
240
- return `route-data:${createRouteDataCacheKey(key)}`;
241
- }
242
- __name(createRouteDataCacheTag, "createRouteDataCacheTag");
243
- function createRouteDataCacheKey(key) {
244
- return createFarmCacheKey(Array.isArray(key) ? key : [key]);
245
- }
246
- __name(createRouteDataCacheKey, "createRouteDataCacheKey");
247
- function normalizeRevalidatePath(routePath) {
248
- if (typeof routePath !== "string") {
249
- throw new TypeError("revalidatePath expects a path string.");
250
- }
251
- let normalized = routePath.trim();
252
- if (!normalized) {
253
- throw new Error("revalidatePath expects a non-empty path.");
254
- }
255
- try {
256
- if (/^https?:\/\//.test(normalized)) {
257
- normalized = new URL(normalized).pathname;
258
- }
259
- } catch {
260
- }
261
- normalized = normalized.split("?")[0] ?? normalized;
262
- normalized = normalized.startsWith("/") ? normalized : `/${normalized}`;
263
- normalized = normalized.replace(/\/{2,}/g, "/");
264
- if (normalized.length > 1) {
265
- normalized = normalized.replace(/\/+$/, "");
266
- }
267
- return normalized || "/";
268
- }
269
- __name(normalizeRevalidatePath, "normalizeRevalidatePath");
270
- function createFarmCacheKey(parts) {
271
- return stableSerialize(parts);
272
- }
273
- __name(createFarmCacheKey, "createFarmCacheKey");
274
- function normalizeRevalidate(revalidate) {
275
- if (revalidate === false || revalidate === void 0) {
276
- return revalidate;
277
- }
278
- if (!Number.isFinite(revalidate) || revalidate <= 0) {
279
- return void 0;
280
- }
281
- return revalidate;
282
- }
283
- __name(normalizeRevalidate, "normalizeRevalidate");
284
- function normalizeCacheTag(tag) {
285
- if (typeof tag !== "string") {
286
- throw new TypeError("Cache tags must be strings.");
287
- }
288
- const normalized = tag.trim();
289
- if (!normalized) {
290
- throw new Error("Cache tags cannot be empty.");
291
- }
292
- return normalized;
293
- }
294
- __name(normalizeCacheTag, "normalizeCacheTag");
295
- function getFunctionCacheIdentity(fn) {
296
- if (fn.name) {
297
- return `name:${fn.name}`;
298
- }
299
- let id = functionIds.get(fn);
300
- if (!id) {
301
- id = ++nextFunctionId;
302
- functionIds.set(fn, id);
303
- }
304
- return `anonymous:${id}`;
305
- }
306
- __name(getFunctionCacheIdentity, "getFunctionCacheIdentity");
307
- function stableSerialize(value, seen = /* @__PURE__ */ new WeakSet()) {
308
- if (value === null) return "null";
309
- if (value === void 0) return "undefined";
310
- const valueType = typeof value;
311
- if (valueType === "string") return JSON.stringify(value);
312
- if (valueType === "number" || valueType === "boolean" || valueType === "bigint") {
313
- return `${valueType}:${String(value)}`;
314
- }
315
- if (valueType === "symbol") {
316
- return `symbol:${String(value)}`;
317
- }
318
- if (valueType === "function") {
319
- return `function:${getFunctionCacheIdentity(value)}`;
320
- }
321
- if (value instanceof Date) {
322
- return `date:${value.toISOString()}`;
323
- }
324
- if (value instanceof URL) {
325
- return `url:${value.toString()}`;
326
- }
327
- if (value instanceof RegExp) {
328
- return `regexp:${value.toString()}`;
329
- }
330
- if (Array.isArray(value)) {
331
- return `[${value.map((item) => stableSerialize(item, seen)).join(",")}]`;
332
- }
333
- if (value && typeof value === "object") {
334
- if (seen.has(value)) {
335
- return "[Circular]";
336
- }
337
- seen.add(value);
338
- const entries = Object.entries(value).sort(
339
- ([a], [b]) => a.localeCompare(b)
340
- );
341
- const serialized = entries.map(([key, item]) => `${JSON.stringify(key)}:${stableSerialize(item, seen)}`).join(",");
342
- seen.delete(value);
343
- return `{${serialized}}`;
344
- }
345
- return String(value);
346
- }
347
- __name(stableSerialize, "stableSerialize");
348
-
349
- export {
350
- FarmDataCache,
351
- getFarmDataCache,
352
- unstable_cache,
353
- revalidateTag,
354
- updateTag,
355
- revalidatePath,
356
- invalidate,
357
- invalidateRouteData,
358
- createPathCacheTag,
359
- createRouteDataCacheTag,
360
- createRouteDataCacheKey,
361
- normalizeRevalidatePath,
362
- createFarmCacheKey
363
- };
364
- //# sourceMappingURL=chunk-4PTK6KT4.mjs.map