@trpc/next 11.3.1 → 11.3.2-canary.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 (104) hide show
  1. package/app-dir/client/package.json +1 -0
  2. package/app-dir/links/nextCache/package.json +1 -0
  3. package/app-dir/links/nextHttp/package.json +1 -0
  4. package/app-dir/server/package.json +1 -0
  5. package/dist/app-dir/client.cjs +115 -0
  6. package/dist/app-dir/client.d.cts +50 -0
  7. package/dist/app-dir/client.d.cts.map +1 -0
  8. package/dist/app-dir/client.d.mts +50 -0
  9. package/dist/app-dir/client.d.mts.map +1 -0
  10. package/dist/app-dir/client.mjs +109 -40
  11. package/dist/app-dir/client.mjs.map +1 -0
  12. package/dist/app-dir/links/nextCache.cjs +48 -0
  13. package/dist/app-dir/links/nextCache.d.cts +15 -0
  14. package/dist/app-dir/links/nextCache.d.cts.map +1 -0
  15. package/dist/app-dir/links/nextCache.d.mts +15 -0
  16. package/dist/app-dir/links/nextCache.d.mts.map +1 -0
  17. package/dist/app-dir/links/nextCache.mjs +43 -55
  18. package/dist/app-dir/links/nextCache.mjs.map +1 -0
  19. package/dist/app-dir/links/nextHttp.cjs +35 -0
  20. package/dist/app-dir/links/nextHttp.d.cts +18 -0
  21. package/dist/app-dir/links/nextHttp.d.cts.map +1 -0
  22. package/dist/app-dir/links/nextHttp.d.mts +18 -0
  23. package/dist/app-dir/links/nextHttp.d.mts.map +1 -0
  24. package/dist/app-dir/links/nextHttp.mjs +30 -32
  25. package/dist/app-dir/links/nextHttp.mjs.map +1 -0
  26. package/dist/app-dir/server.cjs +96 -0
  27. package/dist/app-dir/server.d.cts +2 -0
  28. package/dist/app-dir/server.d.mts +2 -0
  29. package/dist/app-dir/server.mjs +85 -109
  30. package/dist/app-dir/server.mjs.map +1 -0
  31. package/dist/chunk-CUT6urMc.cjs +30 -0
  32. package/dist/index.cjs +95 -0
  33. package/dist/index.d.cts +35 -0
  34. package/dist/index.d.cts.map +1 -0
  35. package/dist/index.d.mts +35 -0
  36. package/dist/index.d.mts.map +1 -0
  37. package/dist/index.mjs +94 -2
  38. package/dist/index.mjs.map +1 -0
  39. package/dist/server.d-BXv5jwlB.d.mts +90 -0
  40. package/dist/server.d-BXv5jwlB.d.mts.map +1 -0
  41. package/dist/server.d-DLnegS7F.d.cts +90 -0
  42. package/dist/server.d-DLnegS7F.d.cts.map +1 -0
  43. package/dist/shared-C9SS-IYg.cjs +29 -0
  44. package/dist/shared-FEQznZ-6.mjs +18 -0
  45. package/dist/shared-FEQznZ-6.mjs.map +1 -0
  46. package/dist/ssrPrepass.cjs +104 -0
  47. package/dist/ssrPrepass.d.cts +9 -0
  48. package/dist/ssrPrepass.d.cts.map +1 -0
  49. package/dist/ssrPrepass.d.mts +9 -0
  50. package/dist/ssrPrepass.d.mts.map +1 -0
  51. package/dist/ssrPrepass.mjs +98 -131
  52. package/dist/ssrPrepass.mjs.map +1 -0
  53. package/dist/withTRPC.d-BY6FsDWi.d.mts +54 -0
  54. package/dist/withTRPC.d-BY6FsDWi.d.mts.map +1 -0
  55. package/dist/withTRPC.d-DctYN4Yz.d.cts +54 -0
  56. package/dist/withTRPC.d-DctYN4Yz.d.cts.map +1 -0
  57. package/package.json +63 -34
  58. package/ssrPrepass/package.json +1 -0
  59. package/app-dir/client/index.d.ts +0 -1
  60. package/app-dir/client/index.js +0 -1
  61. package/app-dir/links/nextCache/index.d.ts +0 -1
  62. package/app-dir/links/nextCache/index.js +0 -1
  63. package/app-dir/links/nextHttp/index.d.ts +0 -1
  64. package/app-dir/links/nextHttp/index.js +0 -1
  65. package/app-dir/server/index.d.ts +0 -1
  66. package/app-dir/server/index.js +0 -1
  67. package/dist/app-dir/client.d.ts +0 -6
  68. package/dist/app-dir/client.d.ts.map +0 -1
  69. package/dist/app-dir/client.js +0 -48
  70. package/dist/app-dir/create-action-hook.d.ts +0 -50
  71. package/dist/app-dir/create-action-hook.d.ts.map +0 -1
  72. package/dist/app-dir/create-action-hook.js +0 -108
  73. package/dist/app-dir/create-action-hook.mjs +0 -105
  74. package/dist/app-dir/links/nextCache.d.ts +0 -12
  75. package/dist/app-dir/links/nextCache.d.ts.map +0 -1
  76. package/dist/app-dir/links/nextCache.js +0 -62
  77. package/dist/app-dir/links/nextHttp.d.ts +0 -15
  78. package/dist/app-dir/links/nextHttp.d.ts.map +0 -1
  79. package/dist/app-dir/links/nextHttp.js +0 -39
  80. package/dist/app-dir/server.d.ts +0 -30
  81. package/dist/app-dir/server.d.ts.map +0 -1
  82. package/dist/app-dir/server.js +0 -122
  83. package/dist/app-dir/shared.d.ts +0 -55
  84. package/dist/app-dir/shared.d.ts.map +0 -1
  85. package/dist/app-dir/shared.js +0 -20
  86. package/dist/app-dir/shared.mjs +0 -17
  87. package/dist/app-dir/types.d.ts +0 -31
  88. package/dist/app-dir/types.d.ts.map +0 -1
  89. package/dist/createTRPCNext.d.ts +0 -29
  90. package/dist/createTRPCNext.d.ts.map +0 -1
  91. package/dist/createTRPCNext.js +0 -39
  92. package/dist/createTRPCNext.mjs +0 -37
  93. package/dist/index.d.ts +0 -3
  94. package/dist/index.d.ts.map +0 -1
  95. package/dist/index.js +0 -9
  96. package/dist/ssrPrepass.d.ts +0 -3
  97. package/dist/ssrPrepass.d.ts.map +0 -1
  98. package/dist/ssrPrepass.js +0 -139
  99. package/dist/withTRPC.d.ts +0 -54
  100. package/dist/withTRPC.d.ts.map +0 -1
  101. package/dist/withTRPC.js +0 -84
  102. package/dist/withTRPC.mjs +0 -82
  103. package/ssrPrepass/index.d.ts +0 -1
  104. package/ssrPrepass/index.js +0 -1
@@ -1,17 +0,0 @@
1
- import '@trpc/client';
2
- import '@trpc/server/unstable-core-do-not-import';
3
-
4
- /**
5
- * @internal
6
- */ function generateCacheTag(procedurePath, input) {
7
- return input ? `${procedurePath}?input=${JSON.stringify(input)}` : procedurePath;
8
- }
9
- function isFormData(value) {
10
- if (typeof FormData === 'undefined') {
11
- // FormData is not supported
12
- return false;
13
- }
14
- return value instanceof FormData;
15
- }
16
-
17
- export { generateCacheTag, isFormData };
@@ -1,31 +0,0 @@
1
- import type { Resolver } from '@trpc/client';
2
- import type { AnyProcedure, AnyRootTypes, inferProcedureInput, inferTransformedProcedureOutput, ProcedureType, RouterRecord } from '@trpc/server/unstable-core-do-not-import';
3
- type ResolverDef = {
4
- input: any;
5
- output: any;
6
- transformer: boolean;
7
- errorShape: any;
8
- };
9
- export type DecorateProcedureServer<TType extends ProcedureType, TDef extends ResolverDef> = TType extends 'query' ? {
10
- query: Resolver<TDef>;
11
- revalidate: (input?: TDef['input']) => Promise<{
12
- revalidated: false;
13
- error: string;
14
- } | {
15
- revalidated: true;
16
- }>;
17
- } : TType extends 'mutation' ? {
18
- mutate: Resolver<TDef>;
19
- } : TType extends 'subscription' ? {
20
- subscribe: Resolver<TDef>;
21
- } : never;
22
- export type NextAppDirDecorateRouterRecord<TRoot extends AnyRootTypes, TRecord extends RouterRecord> = {
23
- [TKey in keyof TRecord]: TRecord[TKey] extends infer $Value ? $Value extends AnyProcedure ? DecorateProcedureServer<$Value['_def']['type'], {
24
- input: inferProcedureInput<$Value>;
25
- output: inferTransformedProcedureOutput<TRoot, $Value>;
26
- errorShape: TRoot['errorShape'];
27
- transformer: TRoot['transformer'];
28
- }> : $Value extends RouterRecord ? NextAppDirDecorateRouterRecord<TRoot, $Value> : never : never;
29
- };
30
- export {};
31
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/app-dir/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,mBAAmB,EACnB,+BAA+B,EAC/B,aAAa,EACb,YAAY,EACb,MAAM,0CAA0C,CAAC;AAElD,KAAK,WAAW,GAAG;IACjB,KAAK,EAAE,GAAG,CAAC;IACX,MAAM,EAAE,GAAG,CAAC;IAC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ecorateRouterRecord, TRPCUseQueries, TRPCUseSuspenseQueries } from '@trpc/react-query/shared';
2
- import type { AnyRouter, ProtectedIntersection } from '@trpc/server/unstable-core-do-not-import';
3
- import type { NextPageContext } from 'next/types';
4
- import type { WithTRPCNoSSROptions, WithTRPCSSROptions } from './withTRPC';
5
- import { withTRPC } from './withTRPC';
6
- /**
7
- * @internal
8
- */
9
- export interface CreateTRPCNextBase<TRouter extends AnyRouter, TSSRContext extends NextPageContext> {
10
- /**
11
- * @deprecated renamed to `useUtils` and will be removed in a future tRPC version
12
- *
13
- * @see https://trpc.io/docs/v11/client/react/useUtils
14
- */
15
- useContext(): CreateReactUtils<TRouter, TSSRContext>;
16
- /**
17
- * @see https://trpc.io/docs/v11/client/react/useUtils
18
- */
19
- useUtils(): CreateReactUtils<TRouter, TSSRContext>;
20
- withTRPC: ReturnType<typeof withTRPC<TRouter, TSSRContext>>;
21
- useQueries: TRPCUseQueries<TRouter>;
22
- useSuspenseQueries: TRPCUseSuspenseQueries<TRouter>;
23
- }
24
- /**
25
- * @internal
26
- */
27
- export type CreateTRPCNext<TRouter extends AnyRouter, TSSRContext extends NextPageContext> = ProtectedIntersection<CreateTRPCNextBase<TRouter, TSSRContext>, DecorateRouterRecord<TRouter['_def']['_config']['$types'], TRouter['_def']['record']>>;
28
- export declare function createTRPCNext<TRouter extends AnyRouter, TSSRContext extends NextPageContext = NextPageContext>(opts: WithTRPCNoSSROptions<TRouter> | WithTRPCSSROptions<TRouter>): CreateTRPCNext<TRouter, TSSRContext>;
29
- //# sourceMappingURL=createTRPCNext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createTRPCNext.d.ts","sourceRoot":"","sources":["../src/createTRPCNext.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,gBAAgB,EAChB,oBAAoB,EACpB,cAAc,EACd,sBAAsB,EACvB,MAAM,0BAA0B,CAAC;AAMlC,OAAO,KAAK,EACV,SAAS,EACT,qBAAqB,EACtB,MAAM,0C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-import');
5
- var React = require('react');
6
- var withTRPC = require('./withTRPC.js');
7
-
8
- /* istanbul ignore file -- @preserve */ // We're testing this through E2E-testing
9
- function createTRPCNext(opts) {
10
- const hooks = shared.createRootHooks(opts);
11
- // TODO: maybe set TSSRContext to `never` when using `WithTRPCNoSSROptions`
12
- const _withTRPC = withTRPC.withTRPC(opts);
13
- const proxy = shared.createReactDecoration(hooks);
14
- return unstableCoreDoNotImport.createFlatProxy((key)=>{
15
- if (key === 'useContext' || key === 'useUtils') {
16
- return ()=>{
17
- const context = hooks.useUtils();
18
- // create a stable reference of the utils context
19
- return React.useMemo(()=>{
20
- return shared.createReactQueryUtils(context);
21
- }, [
22
- context
23
- ]);
24
- };
25
- }
26
- if (key === 'useQueries') {
27
- return hooks.useQueries;
28
- }
29
- if (key === 'useSuspenseQueries') {
30
- return hooks.useSuspenseQueries;
31
- }
32
- if (key === 'withTRPC') {
33
- return _withTRPC;
34
- }
35
- return proxy[key];
36
- });
37
- }
38
-
39
- exports.createTRPCNext = createTRPCNext;
@@ -1,37 +0,0 @@
1
- import { createRootHooks, createReactDecoration, createReactQueryUtils } from '@trpc/react-query/shared';
2
- import { createFlatProxy } from '@trpc/server/unstable-core-do-not-import';
3
- import { useMemo } from 'react';
4
- import { withTRPC } from './withTRPC.mjs';
5
-
6
- /* istanbul ignore file -- @preserve */ // We're testing this through E2E-testing
7
- function createTRPCNext(opts) {
8
- const hooks = createRootHooks(opts);
9
- // TODO: maybe set TSSRContext to `never` when using `WithTRPCNoSSROptions`
10
- const _withTRPC = withTRPC(opts);
11
- const proxy = createReactDecoration(hooks);
12
- return createFlatProxy((key)=>{
13
- if (key === 'useContext' || key === 'useUtils') {
14
- return ()=>{
15
- const context = hooks.useUtils();
16
- // create a stable reference of the utils context
17
- return useMemo(()=>{
18
- return createReactQueryUtils(context);
19
- }, [
20
- context
21
- ]);
22
- };
23
- }
24
- if (key === 'useQueries') {
25
- return hooks.useQueries;
26
- }
27
- if (key === 'useSuspenseQueries') {
28
- return hooks.useSuspenseQueries;
29
- }
30
- if (key === 'withTRPC') {
31
- return _withTRPC;
32
- }
33
- return proxy[key];
34
- });
35
- }
36
-
37
- export { createTRPCNext };
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './withTRPC';
2
- export * from './createTRPCNext';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC"}
package/dist/index.js DELETED
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- var withTRPC = require('./withTRPC.js');
4
- var createTRPCNext = require('./createTRPCNext.js');
5
-
6
-
7
-
8
- exports.withTRPC = withTRPC.withTRPC;
9
- exports.createTRPCNext = createTRPCNext.createTRPCNext;
@@ -1,3 +0,0 @@
1
- import type { TRPCPrepassHelper } from './withTRPC';
2
- export declare const ssrPrepass: TRPCPrepassHelper;
3
- //# sourceMappingURL=ssrPrepass.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ssrPrepass.d.ts","sourceRoot":"","sources":["../src/ssrPrepass.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,iBAAiB,EAAoB,MAAM,YAAY,CAAC;AAyBtE,eAAO,MAAM,UAAU,EAAE,iBA0IxB,CAAC"}
@@ -1,139 +0,0 @@
1
- 'use strict';
2
-
3
- var reactQuery = require('@tanstack/react-query');
4
- var client = require('@trpc/client');
5
- var unstableInternals = require('@trpc/client/unstable-internals');
6
- var shared = require('@trpc/react-query/shared');
7
- var React = require('react');
8
-
9
- function transformQueryOrMutationCacheErrors(result) {
10
- const error = result.state.error;
11
- if (error instanceof Error && error.name === 'TRPCClientError') {
12
- const newError = {
13
- message: error.message,
14
- data: error.data,
15
- shape: error.shape
16
- };
17
- return {
18
- ...result,
19
- state: {
20
- ...result.state,
21
- error: newError
22
- }
23
- };
24
- }
25
- return result;
26
- }
27
- const ssrPrepass = (opts)=>{
28
- const { parent, WithTRPC, AppOrPage } = opts;
29
- const transformer = unstableInternals.getTransformer(parent.transformer);
30
- WithTRPC.getInitialProps = async (appOrPageCtx)=>{
31
- const shouldSsr = async ()=>{
32
- if (typeof window !== 'undefined') {
33
- return false;
34
- }
35
- if (typeof parent.ssr === 'function') {
36
- try {
37
- return await parent.ssr({
38
- ctx: appOrPageCtx.ctx
39
- });
40
- } catch {
41
- return false;
42
- }
43
- }
44
- return parent.ssr;
45
- };
46
- const ssrEnabled = await shouldSsr();
47
- const AppTree = appOrPageCtx.AppTree;
48
- // Determine if we are wrapping an App component or a Page component.
49
- const isApp = !!appOrPageCtx.Component;
50
- const ctx = isApp ? appOrPageCtx.ctx : appOrPageCtx;
51
- // Run the wrapped component's getInitialProps function.
52
- let pageProps = {};
53
- if (AppOrPage.getInitialProps) {
54
- const originalProps = await AppOrPage.getInitialProps(appOrPageCtx);
55
- const originalPageProps = isApp ? originalProps.pageProps ?? {} : originalProps;
56
- pageProps = {
57
- ...originalPageProps,
58
- ...pageProps
59
- };
60
- }
61
- const getAppTreeProps = (props)=>isApp ? {
62
- pageProps: props
63
- } : props;
64
- if (typeof window !== 'undefined' || !ssrEnabled) {
65
- return getAppTreeProps(pageProps);
66
- }
67
- const config = parent.config({
68
- ctx
69
- });
70
- const trpcClient = client.createTRPCUntypedClient(config);
71
- const queryClient = shared.getQueryClient(config);
72
- const trpcProp = {
73
- config,
74
- trpcClient,
75
- queryClient,
76
- ssrState: 'prepass',
77
- ssrContext: ctx
78
- };
79
- const prepassProps = {
80
- pageProps,
81
- trpc: trpcProp
82
- };
83
- const reactDomServer = await import('react-dom/server');
84
- // Run the prepass step on AppTree. This will run all trpc queries on the server.
85
- // multiple prepass ensures that we can do batching on the server
86
- while(true){
87
- // render full tree
88
- reactDomServer.renderToString(React.createElement(AppTree, prepassProps));
89
- if (!queryClient.isFetching()) {
90
- break;
91
- }
92
- // wait until the query cache has settled it's promises
93
- await new Promise((resolve)=>{
94
- const unsub = queryClient.getQueryCache().subscribe((event)=>{
95
- if (event?.query.getObserversCount() === 0) {
96
- resolve();
97
- unsub();
98
- }
99
- });
100
- });
101
- }
102
- const dehydratedCache = reactQuery.dehydrate(queryClient, {
103
- shouldDehydrateQuery (query) {
104
- // filter out queries that are marked as trpc: { ssr: false } or are not enabled, but make sure errors are dehydrated
105
- const isExcludedFromSSr = query.state.fetchStatus === 'idle' && query.state.status === 'pending';
106
- return !isExcludedFromSSr;
107
- }
108
- });
109
- // since error instances can't be serialized, let's make them into `TRPCClientErrorLike`-objects
110
- const dehydratedCacheWithErrors = {
111
- ...dehydratedCache,
112
- queries: dehydratedCache.queries.map(transformQueryOrMutationCacheErrors),
113
- mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
114
- };
115
- // dehydrate query client's state and add it to the props
116
- pageProps['trpcState'] = transformer.input.serialize(dehydratedCacheWithErrors);
117
- const appTreeProps = getAppTreeProps(pageProps);
118
- const meta = parent.responseMeta?.({
119
- ctx,
120
- clientErrors: [
121
- ...dehydratedCache.queries,
122
- ...dehydratedCache.mutations
123
- ].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
124
- err
125
- ] : [])
126
- }) ?? {};
127
- for (const [key, value] of Object.entries(meta.headers ?? {})){
128
- if (typeof value === 'string') {
129
- ctx.res?.setHeader(key, value);
130
- }
131
- }
132
- if (meta.status && ctx.res) {
133
- ctx.res.statusCode = meta.status;
134
- }
135
- return appTreeProps;
136
- };
137
- };
138
-
139
- exports.ssrPrepass = ssrPrepass;
@@ -1,54 +0,0 @@
1
- /**
2
- * Heavily based on urql's ssr
3
- * https://github.com/FormidableLabs/urql/blob/main/packages/next-urql/src/with-urql-client.ts
4
- */
5
- import type { QueryClient } from '@tanstack/react-query';
6
- import type { CreateTRPCClientOptions, TRPCClient, TRPCClientError, TRPCUntypedClient } from '@trpc/client';
7
- import { type TransformerOptions } from '@trpc/client/unstable-internals';
8
- import type { CreateTRPCReactOptions, CreateTRPCReactQueryClientConfig } from '@trpc/react-query/shared';
9
- import type { AnyRouter, inferClientTypes, ResponseMeta } from '@trpc/server/unstable-core-do-not-import';
10
- import type { NextComponentType, NextPageContext } from 'next/dist/shared/lib/utils';
11
- export type WithTRPCConfig<TRouter extends AnyRouter> = CreateTRPCClientOptions<TRouter> & CreateTRPCReactQueryClientConfig & {
12
- abortOnUnmount?: boolean;
13
- };
14
- type WithTRPCOptions<TRouter extends AnyRouter> = CreateTRPCReactOptions<TRouter> & {
15
- config: (info: {
16
- ctx?: NextPageContext;
17
- }) => WithTRPCConfig<TRouter>;
18
- } & TransformerOptions<inferClientTypes<TRouter>>;
19
- export type TRPCPrepassHelper = (opts: {
20
- parent: WithTRPCSSROptions<AnyRouter>;
21
- WithTRPC: NextComponentType<any, any, any>;
22
- AppOrPage: NextComponentType<any, any, any>;
23
- }) => void;
24
- export type WithTRPCSSROptions<TRouter extends AnyRouter> = WithTRPCOptions<TRouter> & {
25
- /**
26
- * If you enable this, you also need to add a `ssrPrepass`-prop
27
- * @see https://trpc.io/docs/client/nextjs/ssr
28
- */
29
- ssr: true | ((opts: {
30
- ctx: NextPageContext;
31
- }) => boolean | Promise<boolean>);
32
- responseMeta?: (opts: {
33
- ctx: NextPageContext;
34
- clientErrors: TRPCClientError<TRouter>[];
35
- }) => ResponseMeta;
36
- /**
37
- * use `import { ssrPrepass } from '@trpc/next/ssrPrepass'`
38
- * @see https://trpc.io/docs/client/nextjs/ssr
39
- */
40
- ssrPrepass: TRPCPrepassHelper;
41
- };
42
- export type WithTRPCNoSSROptions<TRouter extends AnyRouter> = WithTRPCOptions<TRouter> & {
43
- ssr?: false;
44
- };
45
- export type TRPCPrepassProps<TRouter extends AnyRouter, TSSRContext extends NextPageContext = NextPageContext> = {
46
- config: WithTRPCConfig<TRouter>;
47
- queryClient: QueryClient;
48
- trpcClient: TRPCUntypedClient<TRouter> | TRPCClient<TRouter>;
49
- ssrState: 'prepass';
50
- ssrContext: TSSRContext;
51
- };
52
- export declare function withTRPC<TRouter extends AnyRouter, TSSRContext extends NextPageContext = NextPageContext>(opts: WithTRPCNoSSROptions<TRouter> | WithTRPCSSROptions<TRouter>): (AppOrPage: NextComponentType<any, any, any>) => NextComponentType;
53
- export {};
54
- //# sourceMappingURL=withTRPC.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"withTRPC.d.ts","sourceRoot":"","sources":["../src/withTRPC.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAmB,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAE1E,OAAO,KAAK,EACV,uBAAuB,EACvB,UAAU,EACV,eAAe,EACf,iBAAiB,EAClB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EACV,sBAAsB,EACtB,gCAAgC,EACjC,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EACV,SAAS,EAET,gBAAgB,EAChB,YAAY,EACb,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EAGV,iBAAiB,EACjB,eAAe,EAChB,MAAM,4BAA4B,CAAC;AAIpC,MAAM,MAAM,cAAc,CAAC,OAAO,SAAS,SAAS,IAClD,uBAAuB,CAAC,OAAO,CAAC,GAC9B,gCAAgC,GAAG;IACjC,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AAEN,KAAK,eAAe,CAAC,OAAO,SAAS,SAAS,IAC5C,sBAAsB,CAAC,OAAO,CAAC,GAAG;IAChC,MAAM,EAAE,CAAC,IAAI,EAAE;QAAE,GAAG,CAAC,EAAE,eAAe,CAAA;KAAE,KAAK,cAAc,CAAC,OAAO,CAAC,CAAC;CACtE,GAAG,kBAAkB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AAEpD,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE;IACrC,MA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,oBAAoB,CAAC,OAAO,CAAC,GAAG,kBAAkB,CAAC,OAAO,CAAC,IAOzD,WAAW,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,KAAG,iBAAiB,CA+FxE"}
package/dist/withTRPC.js DELETED
@@ -1,84 +0,0 @@
1
- 'use strict';
2
-
3
- var reactQuery = require('@tanstack/react-query');
4
- var unstableInternals = require('@trpc/client/unstable-internals');
5
- var shared = require('@trpc/react-query/shared');
6
- var React = require('react');
7
-
8
- function withTRPC(opts) {
9
- const { config: getClientConfig } = opts;
10
- const transformer = unstableInternals.getTransformer(opts.transformer);
11
- return (AppOrPage)=>{
12
- const trpc = shared.createRootHooks(opts);
13
- const WithTRPC = (props)=>{
14
- const [prepassProps] = React.useState(()=>{
15
- if (props.trpc) {
16
- return props.trpc;
17
- }
18
- const config = getClientConfig({});
19
- const queryClient = shared.getQueryClient(config);
20
- const trpcClient = trpc.createClient(config);
21
- return {
22
- abortOnUnmount: config.abortOnUnmount,
23
- queryClient,
24
- trpcClient,
25
- ssrState: opts.ssr ? 'mounting' : false,
26
- ssrContext: null
27
- };
28
- });
29
- const { queryClient, trpcClient, ssrState, ssrContext } = prepassProps;
30
- // allow normal components to be wrapped, not just app/pages
31
- const trpcState = props.pageProps?.trpcState;
32
- const hydratedState = React.useMemo(()=>{
33
- if (!trpcState) {
34
- return trpcState;
35
- }
36
- return transformer.input.deserialize(trpcState);
37
- }, [
38
- trpcState
39
- ]);
40
- return /*#__PURE__*/ React.createElement(trpc.Provider, {
41
- abortOnUnmount: prepassProps.abortOnUnmount ?? false,
42
- client: trpcClient,
43
- queryClient: queryClient,
44
- ssrState: ssrState,
45
- ssrContext: ssrContext
46
- }, /*#__PURE__*/ React.createElement(reactQuery.QueryClientProvider, {
47
- client: queryClient
48
- }, /*#__PURE__*/ React.createElement(reactQuery.HydrationBoundary, {
49
- state: hydratedState
50
- }, /*#__PURE__*/ React.createElement(AppOrPage, props))));
51
- };
52
- if (opts.ssr) {
53
- opts.ssrPrepass({
54
- parent: opts,
55
- AppOrPage,
56
- WithTRPC
57
- });
58
- } else if (AppOrPage.getInitialProps) {
59
- // Allow combining `getServerSideProps` and `getInitialProps`
60
- WithTRPC.getInitialProps = async (appOrPageCtx)=>{
61
- // Determine if we are wrapping an App component or a Page component.
62
- const isApp = !!appOrPageCtx.Component;
63
- // Run the wrapped component's getInitialProps function.
64
- let pageProps = {};
65
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66
- const originalProps = await AppOrPage.getInitialProps(appOrPageCtx);
67
- const originalPageProps = isApp ? originalProps.pageProps ?? {} : originalProps;
68
- pageProps = {
69
- ...originalPageProps,
70
- ...pageProps
71
- };
72
- const getAppTreeProps = (props)=>isApp ? {
73
- pageProps: props
74
- } : props;
75
- return getAppTreeProps(pageProps);
76
- };
77
- }
78
- const displayName = AppOrPage.displayName ?? AppOrPage.name ?? 'Component';
79
- WithTRPC.displayName = `withTRPC(${displayName})`;
80
- return WithTRPC;
81
- };
82
- }
83
-
84
- exports.withTRPC = withTRPC;
package/dist/withTRPC.mjs DELETED
@@ -1,82 +0,0 @@
1
- import { QueryClientProvider, HydrationBoundary } from '@tanstack/react-query';
2
- import { getTransformer } from '@trpc/client/unstable-internals';
3
- import { createRootHooks, getQueryClient } from '@trpc/react-query/shared';
4
- import React, { useState } from 'react';
5
-
6
- function withTRPC(opts) {
7
- const { config: getClientConfig } = opts;
8
- const transformer = getTransformer(opts.transformer);
9
- return (AppOrPage)=>{
10
- const trpc = createRootHooks(opts);
11
- const WithTRPC = (props)=>{
12
- const [prepassProps] = useState(()=>{
13
- if (props.trpc) {
14
- return props.trpc;
15
- }
16
- const config = getClientConfig({});
17
- const queryClient = getQueryClient(config);
18
- const trpcClient = trpc.createClient(config);
19
- return {
20
- abortOnUnmount: config.abortOnUnmount,
21
- queryClient,
22
- trpcClient,
23
- ssrState: opts.ssr ? 'mounting' : false,
24
- ssrContext: null
25
- };
26
- });
27
- const { queryClient, trpcClient, ssrState, ssrContext } = prepassProps;
28
- // allow normal components to be wrapped, not just app/pages
29
- const trpcState = props.pageProps?.trpcState;
30
- const hydratedState = React.useMemo(()=>{
31
- if (!trpcState) {
32
- return trpcState;
33
- }
34
- return transformer.input.deserialize(trpcState);
35
- }, [
36
- trpcState
37
- ]);
38
- return /*#__PURE__*/ React.createElement(trpc.Provider, {
39
- abortOnUnmount: prepassProps.abortOnUnmount ?? false,
40
- client: trpcClient,
41
- queryClient: queryClient,
42
- ssrState: ssrState,
43
- ssrContext: ssrContext
44
- }, /*#__PURE__*/ React.createElement(QueryClientProvider, {
45
- client: queryClient
46
- }, /*#__PURE__*/ React.createElement(HydrationBoundary, {
47
- state: hydratedState
48
- }, /*#__PURE__*/ React.createElement(AppOrPage, props))));
49
- };
50
- if (opts.ssr) {
51
- opts.ssrPrepass({
52
- parent: opts,
53
- AppOrPage,
54
- WithTRPC
55
- });
56
- } else if (AppOrPage.getInitialProps) {
57
- // Allow combining `getServerSideProps` and `getInitialProps`
58
- WithTRPC.getInitialProps = async (appOrPageCtx)=>{
59
- // Determine if we are wrapping an App component or a Page component.
60
- const isApp = !!appOrPageCtx.Component;
61
- // Run the wrapped component's getInitialProps function.
62
- let pageProps = {};
63
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
64
- const originalProps = await AppOrPage.getInitialProps(appOrPageCtx);
65
- const originalPageProps = isApp ? originalProps.pageProps ?? {} : originalProps;
66
- pageProps = {
67
- ...originalPageProps,
68
- ...pageProps
69
- };
70
- const getAppTreeProps = (props)=>isApp ? {
71
- pageProps: props
72
- } : props;
73
- return getAppTreeProps(pageProps);
74
- };
75
- }
76
- const displayName = AppOrPage.displayName ?? AppOrPage.name ?? 'Component';
77
- WithTRPC.displayName = `withTRPC(${displayName})`;
78
- return WithTRPC;
79
- };
80
- }
81
-
82
- export { withTRPC };
@@ -1 +0,0 @@
1
- export * from '../dist/ssrPrepass';
@@ -1 +0,0 @@
1
- module.exports = require('../dist/ssrPrepass');