@trpc/next 11.0.0-next.92 → 11.0.0-rc.329

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 (58) hide show
  1. package/README.md +5 -5
  2. package/dist/app-dir/client.d.ts +3 -3
  3. package/dist/app-dir/client.d.ts.map +1 -1
  4. package/dist/app-dir/client.js +5 -105
  5. package/dist/app-dir/client.mjs +4 -102
  6. package/dist/app-dir/create-action-hook.d.ts +18 -6
  7. package/dist/app-dir/create-action-hook.d.ts.map +1 -1
  8. package/dist/app-dir/create-action-hook.js +108 -0
  9. package/dist/app-dir/create-action-hook.mjs +105 -0
  10. package/dist/app-dir/formDataToObject.js +34 -0
  11. package/dist/app-dir/formDataToObject.mjs +32 -0
  12. package/dist/app-dir/links/nextCache.d.ts +4 -3
  13. package/dist/app-dir/links/nextCache.d.ts.map +1 -1
  14. package/dist/app-dir/links/nextCache.js +9 -10
  15. package/dist/app-dir/links/nextCache.mjs +8 -7
  16. package/dist/app-dir/links/nextHttp.d.ts +11 -5
  17. package/dist/app-dir/links/nextHttp.d.ts.map +1 -1
  18. package/dist/app-dir/links/nextHttp.js +22 -23
  19. package/dist/app-dir/links/nextHttp.mjs +22 -21
  20. package/dist/app-dir/server.d.ts +19 -12
  21. package/dist/app-dir/server.d.ts.map +1 -1
  22. package/dist/app-dir/server.js +39 -55
  23. package/dist/app-dir/server.mjs +29 -43
  24. package/dist/app-dir/shared.d.ts +19 -13
  25. package/dist/app-dir/shared.d.ts.map +1 -1
  26. package/dist/{shared-e49b9cdc.js → app-dir/shared.js} +1 -1
  27. package/dist/{shared-f6996341.mjs → app-dir/shared.mjs} +2 -2
  28. package/dist/app-dir/types.d.ts +23 -11
  29. package/dist/app-dir/types.d.ts.map +1 -1
  30. package/dist/bundle-analysis.json +56 -44
  31. package/dist/createTRPCNext.d.ts +10 -8
  32. package/dist/createTRPCNext.d.ts.map +1 -1
  33. package/dist/createTRPCNext.js +38 -0
  34. package/dist/createTRPCNext.mjs +36 -0
  35. package/dist/index.js +4 -190
  36. package/dist/index.mjs +2 -185
  37. package/dist/ssrPrepass.d.ts +3 -0
  38. package/dist/ssrPrepass.d.ts.map +1 -0
  39. package/dist/ssrPrepass.js +139 -0
  40. package/dist/ssrPrepass.mjs +137 -0
  41. package/dist/withTRPC.d.ts +41 -13
  42. package/dist/withTRPC.d.ts.map +1 -1
  43. package/dist/withTRPC.js +86 -0
  44. package/dist/withTRPC.mjs +84 -0
  45. package/package.json +36 -25
  46. package/src/app-dir/client.ts +4 -4
  47. package/src/app-dir/create-action-hook.tsx +49 -19
  48. package/src/app-dir/links/nextCache.ts +20 -8
  49. package/src/app-dir/links/nextHttp.ts +50 -30
  50. package/src/app-dir/server.ts +86 -34
  51. package/src/app-dir/shared.ts +52 -25
  52. package/src/app-dir/types.ts +41 -29
  53. package/src/createTRPCNext.tsx +25 -16
  54. package/src/ssrPrepass.ts +185 -0
  55. package/src/withTRPC.tsx +102 -180
  56. package/ssrPrepass/index.d.ts +1 -0
  57. package/ssrPrepass/index.js +1 -0
  58. package/dist/shared-642894f4.js +0 -19
package/README.md CHANGED
@@ -24,16 +24,16 @@ Full documentation for `@trpc/next` can be found [here](https://trpc.io/docs/cli
24
24
 
25
25
  ```bash
26
26
  # npm
27
- npm install @trpc/next @trpc/react-query @tanstack/react-query
27
+ npm install @trpc/next@next @trpc/react-query@next @tanstack/react-query
28
28
 
29
29
  # Yarn
30
- yarn add @trpc/next @trpc/react-query @tanstack/react-query
30
+ yarn add @trpc/next@next @trpc/react-query@next @tanstack/react-query
31
31
 
32
32
  # pnpm
33
- pnpm add @trpc/next @trpc/react-query @tanstack/react-query
33
+ pnpm add @trpc/next@next @trpc/react-query@next @tanstack/react-query
34
34
 
35
35
  # Bun
36
- bun add @trpc/next @trpc/react-query @tanstack/react-query
36
+ bun add @trpc/next@next @trpc/react-query@next @tanstack/react-query
37
37
  ```
38
38
 
39
39
  ## Basic Example
@@ -55,7 +55,7 @@ export const trpc = createTRPCNext<AppRouter>({
55
55
  ],
56
56
  };
57
57
  },
58
- ssr: true,
58
+ ssr: false,
59
59
  });
60
60
  ```
61
61
 
@@ -1,6 +1,6 @@
1
- import { CreateTRPCClient } from '@trpc/client';
2
- import { AnyRouter } from '@trpc/server';
3
- import { CreateTRPCNextAppRouterOptions } from './shared';
1
+ import type { CreateTRPCClient } from '@trpc/client';
2
+ import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';
3
+ import type { CreateTRPCNextAppRouterOptions } from './shared';
4
4
  export { experimental_createActionHook, experimental_serverActionLink, type UseTRPCActionResult, } from './create-action-hook';
5
5
  export declare function experimental_createTRPCNextAppDirClient<TRouter extends AnyRouter>(opts: CreateTRPCNextAppRouterOptions<TRouter>): CreateTRPCClient<TRouter>;
6
6
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/app-dir/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAEjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,8BAA8B,EAAE,MAAM,UAAU,CAAC;AAE1D,OAAO,EAEL,6BAA6B,EAE7B,6BAA6B,EAE7B,KAAK,mBAAmB,GACzB,MAAM,sBAAsB,CAAC;AAS9B,wBAAgB,uCAAuC,CACrD,OAAO,SAAS,SAAS,EACzB,IAAI,EAAE,8BAA8B,CAAC,OAAO,CAAC,6BAwC9C"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/app-dir/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAKrD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0CAA0C,CAAC;AAE1E,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,UAAU,CAAC;AAE/D,OAAO,EAEL,6BAA6B,EAE7B,6BAA6B,EAE7B,KAAK,mBAAmB,GACzB,MAAM,sBAAsB,CAAC;AAS9B,wBAAgB,uCAAuC,CACrD,OAAO,SAAS,SAAS,EACzB,IAAI,EAAE,8BAA8B,CAAC,OAAO,CAAC,6BAwC9C"}
@@ -1,115 +1,15 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var client = require('@trpc/client');
6
- var shared$2 = require('@trpc/server/shared');
7
- var shared$1 = require('@trpc/client/shared');
8
- var observable = require('@trpc/server/observable');
9
- var React = require('react');
10
- var shared = require('../shared-e49b9cdc.js');
11
-
12
- // ts-prune-ignore-next
13
- function experimental_serverActionLink() {
14
- return (runtime)=>({ op })=>observable.observable((observer)=>{
15
- const context = op.context;
16
- context._action(shared.isFormData(op.input) ? op.input : runtime.transformer.serialize(op.input)).then((data)=>{
17
- const transformed = shared$1.transformResult(data, runtime);
18
- if (!transformed.ok) {
19
- observer.error(client.TRPCClientError.from(transformed.error, {}));
20
- return;
21
- }
22
- observer.next({
23
- context: op.context,
24
- result: transformed.result
25
- });
26
- observer.complete();
27
- }).catch((cause)=>{
28
- observer.error(client.TRPCClientError.from(cause));
29
- });
30
- });
31
- }
32
- // ts-prune-ignore-next
33
- function experimental_createActionHook(opts) {
34
- const client$1 = client.createTRPCUntypedClient(opts);
35
- return function useAction(handler, useActionOpts) {
36
- const count = React.useRef(0);
37
- const [state, setState] = React.useState({
38
- status: 'idle'
39
- });
40
- const actionOptsRef = React.useRef(useActionOpts);
41
- actionOptsRef.current = useActionOpts;
42
- React.useEffect(()=>{
43
- return ()=>{
44
- // cleanup after unmount to prevent calling hook opts after unmount
45
- count.current = -1;
46
- actionOptsRef.current = undefined;
47
- };
48
- }, []);
49
- const mutateAsync = React.useCallback((input, requestOptions)=>{
50
- const idx = ++count.current;
51
- const context = {
52
- ...requestOptions?.context,
53
- _action (innerInput) {
54
- return handler(innerInput);
55
- }
56
- };
57
- setState({
58
- status: 'loading'
59
- });
60
- return client$1.mutation('serverAction', input, {
61
- ...requestOptions,
62
- context
63
- }).then(async (data)=>{
64
- await actionOptsRef.current?.onSuccess?.(data);
65
- if (idx !== count.current) {
66
- return;
67
- }
68
- setState({
69
- status: 'success',
70
- data: data
71
- });
72
- }).catch(async (error)=>{
73
- await actionOptsRef.current?.onError?.(error);
74
- throw error;
75
- }).catch((error)=>{
76
- if (idx !== count.current) {
77
- return;
78
- }
79
- setState({
80
- status: 'error',
81
- error: client.TRPCClientError.from(error, {})
82
- });
83
- throw error;
84
- });
85
- }, [
86
- handler
87
- ]);
88
- const mutate = React.useCallback((...args)=>{
89
- void mutateAsync(...args).catch(()=>{
90
- // ignored
91
- });
92
- }, [
93
- mutateAsync
94
- ]);
95
- return React.useMemo(()=>({
96
- ...state,
97
- mutate,
98
- mutateAsync
99
- }), [
100
- mutate,
101
- mutateAsync,
102
- state
103
- ]);
104
- };
105
- }
4
+ var unstableCoreDoNotImport = require('@trpc/server/unstable-core-do-not-import');
5
+ var createActionHook = require('./create-action-hook.js');
106
6
 
107
7
  // ts-prune-ignore-next
108
8
  function experimental_createTRPCNextAppDirClient(opts) {
109
9
  const client$1 = client.createTRPCUntypedClient(opts.config());
110
10
  // const useProxy = createUseProxy<TRouter>(client);
111
11
  const cache = new Map();
112
- return shared$2.createRecursiveProxy(({ path , args })=>{
12
+ return unstableCoreDoNotImport.createRecursiveProxy(({ path , args })=>{
113
13
  // const pathCopy = [key, ...path];
114
14
  const pathCopy = [
115
15
  ...path
@@ -143,6 +43,6 @@ function experimental_createTRPCNextAppDirClient(opts) {
143
43
  // });
144
44
  }
145
45
 
146
- exports.experimental_createActionHook = experimental_createActionHook;
46
+ exports.experimental_createActionHook = createActionHook.experimental_createActionHook;
47
+ exports.experimental_serverActionLink = createActionHook.experimental_serverActionLink;
147
48
  exports.experimental_createTRPCNextAppDirClient = experimental_createTRPCNextAppDirClient;
148
- exports.experimental_serverActionLink = experimental_serverActionLink;
@@ -1,104 +1,6 @@
1
- import { TRPCClientError, createTRPCUntypedClient, clientCallTypeToProcedureType } from '@trpc/client';
2
- import { createRecursiveProxy } from '@trpc/server/shared';
3
- import { transformResult } from '@trpc/client/shared';
4
- import { observable } from '@trpc/server/observable';
5
- import { useRef, useState, useEffect, useCallback, useMemo } from 'react';
6
- import { i as isFormData } from '../shared-f6996341.mjs';
7
-
8
- // ts-prune-ignore-next
9
- function experimental_serverActionLink() {
10
- return (runtime)=>({ op })=>observable((observer)=>{
11
- const context = op.context;
12
- context._action(isFormData(op.input) ? op.input : runtime.transformer.serialize(op.input)).then((data)=>{
13
- const transformed = transformResult(data, runtime);
14
- if (!transformed.ok) {
15
- observer.error(TRPCClientError.from(transformed.error, {}));
16
- return;
17
- }
18
- observer.next({
19
- context: op.context,
20
- result: transformed.result
21
- });
22
- observer.complete();
23
- }).catch((cause)=>{
24
- observer.error(TRPCClientError.from(cause));
25
- });
26
- });
27
- }
28
- // ts-prune-ignore-next
29
- function experimental_createActionHook(opts) {
30
- const client = createTRPCUntypedClient(opts);
31
- return function useAction(handler, useActionOpts) {
32
- const count = useRef(0);
33
- const [state, setState] = useState({
34
- status: 'idle'
35
- });
36
- const actionOptsRef = useRef(useActionOpts);
37
- actionOptsRef.current = useActionOpts;
38
- useEffect(()=>{
39
- return ()=>{
40
- // cleanup after unmount to prevent calling hook opts after unmount
41
- count.current = -1;
42
- actionOptsRef.current = undefined;
43
- };
44
- }, []);
45
- const mutateAsync = useCallback((input, requestOptions)=>{
46
- const idx = ++count.current;
47
- const context = {
48
- ...requestOptions?.context,
49
- _action (innerInput) {
50
- return handler(innerInput);
51
- }
52
- };
53
- setState({
54
- status: 'loading'
55
- });
56
- return client.mutation('serverAction', input, {
57
- ...requestOptions,
58
- context
59
- }).then(async (data)=>{
60
- await actionOptsRef.current?.onSuccess?.(data);
61
- if (idx !== count.current) {
62
- return;
63
- }
64
- setState({
65
- status: 'success',
66
- data: data
67
- });
68
- }).catch(async (error)=>{
69
- await actionOptsRef.current?.onError?.(error);
70
- throw error;
71
- }).catch((error)=>{
72
- if (idx !== count.current) {
73
- return;
74
- }
75
- setState({
76
- status: 'error',
77
- error: TRPCClientError.from(error, {})
78
- });
79
- throw error;
80
- });
81
- }, [
82
- handler
83
- ]);
84
- const mutate = useCallback((...args)=>{
85
- void mutateAsync(...args).catch(()=>{
86
- // ignored
87
- });
88
- }, [
89
- mutateAsync
90
- ]);
91
- return useMemo(()=>({
92
- ...state,
93
- mutate,
94
- mutateAsync
95
- }), [
96
- mutate,
97
- mutateAsync,
98
- state
99
- ]);
100
- };
101
- }
1
+ import { createTRPCUntypedClient, clientCallTypeToProcedureType } from '@trpc/client';
2
+ import { createRecursiveProxy } from '@trpc/server/unstable-core-do-not-import';
3
+ export { experimental_createActionHook, experimental_serverActionLink } from './create-action-hook.mjs';
102
4
 
103
5
  // ts-prune-ignore-next
104
6
  function experimental_createTRPCNextAppDirClient(opts) {
@@ -139,4 +41,4 @@ function experimental_createTRPCNextAppDirClient(opts) {
139
41
  // });
140
42
  }
141
43
 
142
- export { experimental_createActionHook, experimental_createTRPCNextAppDirClient, experimental_serverActionLink };
44
+ export { experimental_createTRPCNextAppDirClient };
@@ -1,7 +1,9 @@
1
- import { CreateTRPCClientOptions, TRPCClientError, TRPCLink } from '@trpc/client';
2
- import { AnyRouter, MaybePromise, ProcedureOptions, Simplify } from '@trpc/server';
3
- import { TRPCActionHandler } from './server';
4
- import { ActionHandlerDef } from './shared';
1
+ import type { CreateTRPCClientOptions, TRPCLink } from '@trpc/client';
2
+ import { TRPCClientError } from '@trpc/client';
3
+ import type { TransformerOptions } from '@trpc/client/unstable-internals';
4
+ import type { inferClientTypes, InferrableClientTypes, MaybePromise, ProcedureOptions, Simplify, TypeError } from '@trpc/server/unstable-core-do-not-import';
5
+ import type { TRPCActionHandler } from './server';
6
+ import type { ActionHandlerDef } from './shared';
5
7
  type MutationArgs<TDef extends ActionHandlerDef> = TDef['input'] extends void ? [input?: undefined | void, opts?: ProcedureOptions] : [input: FormData | TDef['input'], opts?: ProcedureOptions];
6
8
  interface UseTRPCActionBaseResult<TDef extends ActionHandlerDef> {
7
9
  mutate: (...args: MutationArgs<TDef>) => void;
@@ -28,11 +30,21 @@ interface UseTRPCActionLoadingResult<TDef extends ActionHandlerDef> extends UseT
28
30
  status: 'loading';
29
31
  }
30
32
  export type UseTRPCActionResult<TDef extends ActionHandlerDef> = UseTRPCActionErrorResult<TDef> | UseTRPCActionIdleResult<TDef> | UseTRPCActionLoadingResult<TDef> | UseTRPCActionSuccessResult<TDef>;
31
- export declare function experimental_serverActionLink<TRouter extends AnyRouter = AnyRouter>(): TRPCLink<TRouter>;
33
+ export declare function experimental_serverActionLink<TInferrable extends InferrableClientTypes>(...args: InferrableClientTypes extends TInferrable ? [
34
+ TypeError<'Generic parameter missing in `experimental_createActionHook<HERE>()` or experimental_serverActionLink<HERE>()'>
35
+ ] : inferClientTypes<TInferrable>['transformer'] extends true ? [
36
+ opts: TransformerOptions<{
37
+ transformer: true;
38
+ }>
39
+ ] : [
40
+ opts?: TransformerOptions<{
41
+ transformer: false;
42
+ }>
43
+ ]): TRPCLink<TInferrable>;
32
44
  interface UseTRPCActionOptions<TDef extends ActionHandlerDef> {
33
45
  onSuccess?: (result: TDef['output']) => MaybePromise<void> | void;
34
46
  onError?: (result: TRPCClientError<TDef['errorShape']>) => MaybePromise<void>;
35
47
  }
36
- export declare function experimental_createActionHook<TRouter extends AnyRouter>(opts: CreateTRPCClientOptions<TRouter>): <TDef extends ActionHandlerDef>(handler: TRPCActionHandler<TDef>, useActionOpts?: UseTRPCActionOptions<Simplify<TDef>> | undefined) => UseTRPCActionResult<TDef>;
48
+ export declare function experimental_createActionHook<TInferrable extends InferrableClientTypes>(opts: InferrableClientTypes extends TInferrable ? TypeError<'Generic parameter missing in `experimental_createActionHook<HERE>()`'> : CreateTRPCClientOptions<TInferrable>): <TDef extends ActionHandlerDef>(handler: TRPCActionHandler<TDef>, useActionOpts?: UseTRPCActionOptions<Simplify<TDef>>) => UseTRPCActionResult<TDef>;
37
49
  export {};
38
50
  //# sourceMappingURL=create-action-hook.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-action-hook.d.ts","sourceRoot":"","sources":["../../src/app-dir/create-action-hook.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EAEvB,eAAe,EACf,QAAQ,EAET,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,SAAS,EACT,YAAY,EACZ,gBAAgB,EAChB,QAAQ,EACT,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAc,MAAM,UAAU,CAAC;AAExD,KAAK,YAAY,CAAC,IAAI,SAAS,gBAAgB,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,GACzE,CAAC,KAAK,CAAC,EAAE,SAAS,GAAG,IAAI,EAAE,IAAI,CAAC,EAAE,gBAAgB,CAAC,GACnD,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,EAAE,gBAAgB,CAAC,CAAC;AAE/D,UAAU,uBAAuB,CAAC,IAAI,SAAS,gBAAgB;IAC7D,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,YAAY,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC;IAC9C,WAAW,EAAE,CAAC,GAAG,IAAI,EAAE,YAAY,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;CACvE;AAED,UAAU,0BAA0B,CAAC,IAAI,SAAS,gBAAgB,CAChE,SAAQ,uBAAuB,CAAC,IAAI,CAAC;IACrC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,MAAM,EAAE,SAAS,CAAC;CACnB;AAED,UAAU,wBAAwB,CAAC,IAAI,SAAS,gBAAgB,CAC9D,SAAQ,uBAAuB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UAAU,oBAAoB,CAAC,IAAI,SAAS,gBAAgB;IAC1D,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,YAAY,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAClE,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;CAC/E;AAED,wBAAgB,6BAA6B,CAC3C,WAAW,SAAS,qBAAqB,EAEzC,IAAI,EAAE,qBAAqB,SAAS,WAAW,GAC3C,SAAS,CAAC,sEAAsE,CAAC,GACjF,uBAAuB,CAAC,WAAW,CAAC,4CAS7B,kBAAkB,IAAI,CAAC,kBAChB,qBAAqB,SAAS,IAAI,CAAC,CAAC,+BAqFvD"}
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ var client = require('@trpc/client');
4
+ var unstableInternals = require('@trpc/client/unstable-internals');
5
+ var observable = require('@trpc/server/observable');
6
+ var unstableCoreDoNotImport = require('@trpc/server/unstable-core-do-not-import');
7
+ var React = require('react');
8
+ var shared = require('./shared.js');
9
+
10
+ // ts-prune-ignore-next
11
+ function experimental_serverActionLink(...args) {
12
+ const [opts] = args;
13
+ const transformer = unstableInternals.getTransformer(opts?.transformer);
14
+ return ()=>({ op })=>observable.observable((observer)=>{
15
+ const context = op.context;
16
+ context._action(shared.isFormData(op.input) ? op.input : transformer.input.serialize(op.input)).then((data)=>{
17
+ const transformed = unstableCoreDoNotImport.transformResult(data, transformer.output);
18
+ if (!transformed.ok) {
19
+ observer.error(client.TRPCClientError.from(transformed.error, {}));
20
+ return;
21
+ }
22
+ observer.next({
23
+ context: op.context,
24
+ result: transformed.result
25
+ });
26
+ observer.complete();
27
+ }).catch((cause)=>{
28
+ observer.error(client.TRPCClientError.from(cause));
29
+ });
30
+ });
31
+ }
32
+ // ts-prune-ignore-next
33
+ function experimental_createActionHook(opts) {
34
+ const client$1 = client.createTRPCUntypedClient(opts);
35
+ return function useAction(handler, useActionOpts) {
36
+ const count = React.useRef(0);
37
+ const [state, setState] = React.useState({
38
+ status: 'idle'
39
+ });
40
+ const actionOptsRef = React.useRef(useActionOpts);
41
+ actionOptsRef.current = useActionOpts;
42
+ React.useEffect(()=>{
43
+ return ()=>{
44
+ // cleanup after unmount to prevent calling hook opts after unmount
45
+ count.current = -1;
46
+ actionOptsRef.current = undefined;
47
+ };
48
+ }, []);
49
+ const mutateAsync = React.useCallback((input, requestOptions)=>{
50
+ const idx = ++count.current;
51
+ const context = {
52
+ ...requestOptions?.context,
53
+ _action (innerInput) {
54
+ return handler(innerInput);
55
+ }
56
+ };
57
+ setState({
58
+ status: 'loading'
59
+ });
60
+ return client$1.mutation('serverAction', input, {
61
+ ...requestOptions,
62
+ context
63
+ }).then(async (data)=>{
64
+ await actionOptsRef.current?.onSuccess?.(data);
65
+ if (idx !== count.current) {
66
+ return;
67
+ }
68
+ setState({
69
+ status: 'success',
70
+ data: data
71
+ });
72
+ }).catch(async (error)=>{
73
+ await actionOptsRef.current?.onError?.(error);
74
+ throw error;
75
+ }).catch((error)=>{
76
+ if (idx !== count.current) {
77
+ return;
78
+ }
79
+ setState({
80
+ status: 'error',
81
+ error: client.TRPCClientError.from(error, {})
82
+ });
83
+ throw error;
84
+ });
85
+ }, [
86
+ handler
87
+ ]);
88
+ const mutate = React.useCallback((...args)=>{
89
+ void mutateAsync(...args).catch(()=>{
90
+ // ignored
91
+ });
92
+ }, [
93
+ mutateAsync
94
+ ]);
95
+ return React.useMemo(()=>({
96
+ ...state,
97
+ mutate,
98
+ mutateAsync
99
+ }), [
100
+ mutate,
101
+ mutateAsync,
102
+ state
103
+ ]);
104
+ };
105
+ }
106
+
107
+ exports.experimental_createActionHook = experimental_createActionHook;
108
+ exports.experimental_serverActionLink = experimental_serverActionLink;
@@ -0,0 +1,105 @@
1
+ import { TRPCClientError, createTRPCUntypedClient } from '@trpc/client';
2
+ import { getTransformer } from '@trpc/client/unstable-internals';
3
+ import { observable } from '@trpc/server/observable';
4
+ import { transformResult } from '@trpc/server/unstable-core-do-not-import';
5
+ import { useRef, useState, useEffect, useCallback, useMemo } from 'react';
6
+ import { isFormData } from './shared.mjs';
7
+
8
+ // ts-prune-ignore-next
9
+ function experimental_serverActionLink(...args) {
10
+ const [opts] = args;
11
+ const transformer = getTransformer(opts?.transformer);
12
+ return ()=>({ op })=>observable((observer)=>{
13
+ const context = op.context;
14
+ context._action(isFormData(op.input) ? op.input : transformer.input.serialize(op.input)).then((data)=>{
15
+ const transformed = transformResult(data, transformer.output);
16
+ if (!transformed.ok) {
17
+ observer.error(TRPCClientError.from(transformed.error, {}));
18
+ return;
19
+ }
20
+ observer.next({
21
+ context: op.context,
22
+ result: transformed.result
23
+ });
24
+ observer.complete();
25
+ }).catch((cause)=>{
26
+ observer.error(TRPCClientError.from(cause));
27
+ });
28
+ });
29
+ }
30
+ // ts-prune-ignore-next
31
+ function experimental_createActionHook(opts) {
32
+ const client = createTRPCUntypedClient(opts);
33
+ return function useAction(handler, useActionOpts) {
34
+ const count = useRef(0);
35
+ const [state, setState] = useState({
36
+ status: 'idle'
37
+ });
38
+ const actionOptsRef = useRef(useActionOpts);
39
+ actionOptsRef.current = useActionOpts;
40
+ useEffect(()=>{
41
+ return ()=>{
42
+ // cleanup after unmount to prevent calling hook opts after unmount
43
+ count.current = -1;
44
+ actionOptsRef.current = undefined;
45
+ };
46
+ }, []);
47
+ const mutateAsync = useCallback((input, requestOptions)=>{
48
+ const idx = ++count.current;
49
+ const context = {
50
+ ...requestOptions?.context,
51
+ _action (innerInput) {
52
+ return handler(innerInput);
53
+ }
54
+ };
55
+ setState({
56
+ status: 'loading'
57
+ });
58
+ return client.mutation('serverAction', input, {
59
+ ...requestOptions,
60
+ context
61
+ }).then(async (data)=>{
62
+ await actionOptsRef.current?.onSuccess?.(data);
63
+ if (idx !== count.current) {
64
+ return;
65
+ }
66
+ setState({
67
+ status: 'success',
68
+ data: data
69
+ });
70
+ }).catch(async (error)=>{
71
+ await actionOptsRef.current?.onError?.(error);
72
+ throw error;
73
+ }).catch((error)=>{
74
+ if (idx !== count.current) {
75
+ return;
76
+ }
77
+ setState({
78
+ status: 'error',
79
+ error: TRPCClientError.from(error, {})
80
+ });
81
+ throw error;
82
+ });
83
+ }, [
84
+ handler
85
+ ]);
86
+ const mutate = useCallback((...args)=>{
87
+ void mutateAsync(...args).catch(()=>{
88
+ // ignored
89
+ });
90
+ }, [
91
+ mutateAsync
92
+ ]);
93
+ return useMemo(()=>({
94
+ ...state,
95
+ mutate,
96
+ mutateAsync
97
+ }), [
98
+ mutate,
99
+ mutateAsync,
100
+ state
101
+ ]);
102
+ };
103
+ }
104
+
105
+ export { experimental_createActionHook, experimental_serverActionLink };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */ function set(obj, path, value) {
4
+ if (typeof path === 'string') {
5
+ path = path.split(/[\.\[\]]/).filter(Boolean);
6
+ }
7
+ if (path.length > 1) {
8
+ const p = path.shift();
9
+ const isArrayIndex = /^\d+$/.test(path[0]);
10
+ obj[p] = obj[p] || (isArrayIndex ? [] : {});
11
+ set(obj[p], path, value);
12
+ return;
13
+ }
14
+ const p1 = path[0];
15
+ if (obj[p1] === undefined) {
16
+ obj[p1] = value;
17
+ } else if (Array.isArray(obj[p1])) {
18
+ obj[p1].push(value);
19
+ } else {
20
+ obj[p1] = [
21
+ obj[p1],
22
+ value
23
+ ];
24
+ }
25
+ }
26
+ function formDataToObject(formData) {
27
+ const obj = {};
28
+ for (const [key, value] of formData.entries()){
29
+ set(obj, key, value);
30
+ }
31
+ return obj;
32
+ }
33
+
34
+ exports.formDataToObject = formDataToObject;
@@ -0,0 +1,32 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */ function set(obj, path, value) {
2
+ if (typeof path === 'string') {
3
+ path = path.split(/[\.\[\]]/).filter(Boolean);
4
+ }
5
+ if (path.length > 1) {
6
+ const p = path.shift();
7
+ const isArrayIndex = /^\d+$/.test(path[0]);
8
+ obj[p] = obj[p] || (isArrayIndex ? [] : {});
9
+ set(obj[p], path, value);
10
+ return;
11
+ }
12
+ const p1 = path[0];
13
+ if (obj[p1] === undefined) {
14
+ obj[p1] = value;
15
+ } else if (Array.isArray(obj[p1])) {
16
+ obj[p1].push(value);
17
+ } else {
18
+ obj[p1] = [
19
+ obj[p1],
20
+ value
21
+ ];
22
+ }
23
+ }
24
+ function formDataToObject(formData) {
25
+ const obj = {};
26
+ for (const [key, value] of formData.entries()){
27
+ set(obj, key, value);
28
+ }
29
+ return obj;
30
+ }
31
+
32
+ export { formDataToObject };
@@ -1,11 +1,12 @@
1
- import { TRPCLink } from '@trpc/client';
2
- import { AnyRouter, inferRouterContext } from '@trpc/server';
1
+ import type { TRPCLink } from '@trpc/client';
2
+ import { type TransformerOptions } from '@trpc/client/unstable-internals';
3
+ import type { AnyRouter, inferClientTypes, inferRouterContext } from '@trpc/server/unstable-core-do-not-import';
3
4
  type NextCacheLinkOptions<TRouter extends AnyRouter> = {
4
5
  router: TRouter;
5
6
  createContext: () => Promise<inferRouterContext<TRouter>>;
6
7
  /** how many seconds the cache should hold before revalidating */
7
8
  revalidate?: number | false;
8
- };
9
+ } & TransformerOptions<inferClientTypes<TRouter>>;
9
10
  export declare function experimental_nextCacheLink<TRouter extends AnyRouter>(opts: NextCacheLinkOptions<TRouter>): TRPCLink<TRouter>;
10
11
  export {};
11
12
  //# sourceMappingURL=nextCache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"nextCache.d.ts","sourceRoot":"","sources":["../../../src/app-dir/links/nextCache.ts"],"names":[],"mappings":"AAEA,OAAO,EAAmB,QAAQ,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,SAAS,EAAiB,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAK5E,KAAK,oBAAoB,CAAC,OAAO,SAAS,SAAS,IAAI;IACrD,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,MAAM,OAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1D,iEAAiE;IACjE,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC7B,CAAC;AAGF,wBAAgB,0BAA0B,CAAC,OAAO,SAAS,SAAS,EAClE,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAClC,QAAQ,CAAC,OAAO,CAAC,CAwDnB"}
1
+ {"version":3,"file":"nextCache.d.ts","sourceRoot":"","sources":["../../../src/app-dir/links/nextCache.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,iCAAiC,CAAC;AAEzC,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAChB,kBAAkB,EACnB,MAAM,0CAA0C,CAAC;AAKlD,KAAK,oBAAoB,CAAC,OAAO,SAAS,SAAS,IAAI;IACrD,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,MAAM,OAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1D,iEAAiE;IACjE,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC7B,GAAG,kBAAkB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AAGlD,wBAAgB,0BAA0B,CAAC,OAAO,SAAS,SAAS,EAClE,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAClC,QAAQ,CAAC,OAAO,CAAC,CA0DnB"}