@trpc/next 11.0.0-alpha-tmp-export-from-main.213 → 11.0.0-alpha-tmp-export-from-main.217

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.
@@ -0,0 +1,176 @@
1
+ import { dehydrate, QueryClientProvider, HydrationBoundary } from '@tanstack/react-query';
2
+ import { createTRPCUntypedClient } from '@trpc/client';
3
+ import { createRootHooks, getQueryClient } from '@trpc/react-query/shared';
4
+ import React, { createElement, useState } from 'react';
5
+
6
+ function transformQueryOrMutationCacheErrors(result) {
7
+ const error = result.state.error;
8
+ if (error instanceof Error && error.name === 'TRPCClientError') {
9
+ const newError = {
10
+ message: error.message,
11
+ data: error.data,
12
+ shape: error.shape
13
+ };
14
+ return {
15
+ ...result,
16
+ state: {
17
+ ...result.state,
18
+ error: newError
19
+ }
20
+ };
21
+ }
22
+ return result;
23
+ }
24
+ function withTRPC(opts) {
25
+ const { config: getClientConfig } = opts;
26
+ return (AppOrPage)=>{
27
+ const trpc = createRootHooks(opts);
28
+ const WithTRPC = (props)=>{
29
+ const [prepassProps] = useState(()=>{
30
+ if (props.trpc) {
31
+ return props.trpc;
32
+ }
33
+ const config = getClientConfig({});
34
+ const queryClient = getQueryClient(config);
35
+ const trpcClient = trpc.createClient(config);
36
+ return {
37
+ abortOnUnmount: config.abortOnUnmount,
38
+ queryClient,
39
+ trpcClient,
40
+ ssrState: opts.ssr ? 'mounting' : false,
41
+ ssrContext: null
42
+ };
43
+ });
44
+ const { queryClient , trpcClient , ssrState , ssrContext } = prepassProps;
45
+ // allow normal components to be wrapped, not just app/pages
46
+ const hydratedState = trpc.useDehydratedState(trpcClient, props.pageProps?.trpcState);
47
+ return /*#__PURE__*/ React.createElement(trpc.Provider, {
48
+ abortOnUnmount: prepassProps.abortOnUnmount ?? false,
49
+ client: trpcClient,
50
+ queryClient: queryClient,
51
+ ssrState: ssrState,
52
+ ssrContext: ssrContext
53
+ }, /*#__PURE__*/ React.createElement(QueryClientProvider, {
54
+ client: queryClient
55
+ }, /*#__PURE__*/ React.createElement(HydrationBoundary, {
56
+ state: hydratedState
57
+ }, /*#__PURE__*/ React.createElement(AppOrPage, Object.assign({}, props)))));
58
+ };
59
+ if (AppOrPage.getInitialProps ?? opts.ssr) {
60
+ WithTRPC.getInitialProps = async (appOrPageCtx)=>{
61
+ const shouldSsr = async ()=>{
62
+ if (typeof opts.ssr === 'function') {
63
+ if (typeof window !== 'undefined') {
64
+ return false;
65
+ }
66
+ try {
67
+ return await opts.ssr({
68
+ ctx: appOrPageCtx.ctx
69
+ });
70
+ } catch (e) {
71
+ return false;
72
+ }
73
+ }
74
+ return opts.ssr;
75
+ };
76
+ const ssr = await shouldSsr();
77
+ const AppTree = appOrPageCtx.AppTree;
78
+ // Determine if we are wrapping an App component or a Page component.
79
+ const isApp = !!appOrPageCtx.Component;
80
+ const ctx = isApp ? appOrPageCtx.ctx : appOrPageCtx;
81
+ // Run the wrapped component's getInitialProps function.
82
+ let pageProps = {};
83
+ if (AppOrPage.getInitialProps) {
84
+ const originalProps = await AppOrPage.getInitialProps(appOrPageCtx);
85
+ const originalPageProps = isApp ? originalProps.pageProps ?? {} : originalProps;
86
+ pageProps = {
87
+ ...originalPageProps,
88
+ ...pageProps
89
+ };
90
+ }
91
+ const getAppTreeProps = (props)=>isApp ? {
92
+ pageProps: props
93
+ } : props;
94
+ if (typeof window !== 'undefined' || !ssr) {
95
+ return getAppTreeProps(pageProps);
96
+ }
97
+ const config = getClientConfig({
98
+ ctx
99
+ });
100
+ const trpcClient = createTRPCUntypedClient(config);
101
+ const queryClient = getQueryClient(config);
102
+ const trpcProp = {
103
+ config,
104
+ trpcClient,
105
+ queryClient,
106
+ ssrState: 'prepass',
107
+ ssrContext: ctx
108
+ };
109
+ const prepassProps = {
110
+ pageProps,
111
+ trpc: trpcProp
112
+ };
113
+ const reactDomServer = await import('react-dom/server');
114
+ // Run the prepass step on AppTree. This will run all trpc queries on the server.
115
+ // multiple prepass ensures that we can do batching on the server
116
+ while(true){
117
+ // render full tree
118
+ reactDomServer.renderToString(/*#__PURE__*/ createElement(AppTree, prepassProps));
119
+ if (!queryClient.isFetching()) {
120
+ break;
121
+ }
122
+ // wait until the query cache has settled it's promises
123
+ await new Promise((resolve)=>{
124
+ const unsub = queryClient.getQueryCache().subscribe((event)=>{
125
+ if (event?.query.getObserversCount() === 0) {
126
+ resolve();
127
+ unsub();
128
+ }
129
+ });
130
+ });
131
+ }
132
+ const dehydratedCache = dehydrate(queryClient, {
133
+ shouldDehydrateQuery (query) {
134
+ // filter out queries that are marked as trpc: { ssr: false } or are not enabled, but make sure errors are dehydrated
135
+ const isExcludedFromSSr = query.state.fetchStatus === 'idle' && query.state.status === 'pending';
136
+ return !isExcludedFromSSr;
137
+ }
138
+ });
139
+ // since error instances can't be serialized, let's make them into `TRPCClientErrorLike`-objects
140
+ const dehydratedCacheWithErrors = {
141
+ ...dehydratedCache,
142
+ queries: dehydratedCache.queries.map(transformQueryOrMutationCacheErrors),
143
+ mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
144
+ };
145
+ // dehydrate query client's state and add it to the props
146
+ pageProps['trpcState'] = trpcClient.runtime.combinedTransformer.output.serialize(dehydratedCacheWithErrors);
147
+ const appTreeProps = getAppTreeProps(pageProps);
148
+ if ('responseMeta' in opts) {
149
+ const meta = opts.responseMeta?.({
150
+ ctx,
151
+ clientErrors: [
152
+ ...dehydratedCache.queries,
153
+ ...dehydratedCache.mutations
154
+ ].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
155
+ err
156
+ ] : [])
157
+ }) ?? {};
158
+ for (const [key, value] of Object.entries(meta.headers ?? {})){
159
+ if (typeof value === 'string') {
160
+ ctx.res?.setHeader(key, value);
161
+ }
162
+ }
163
+ if (meta.status && ctx.res) {
164
+ ctx.res.statusCode = meta.status;
165
+ }
166
+ }
167
+ return appTreeProps;
168
+ };
169
+ }
170
+ const displayName = AppOrPage.displayName ?? AppOrPage.name ?? 'Component';
171
+ WithTRPC.displayName = `withTRPC(${displayName})`;
172
+ return WithTRPC;
173
+ };
174
+ }
175
+
176
+ export { withTRPC };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/next",
3
- "version": "11.0.0-alpha-tmp-export-from-main.213+855f8bc75",
3
+ "version": "11.0.0-alpha-tmp-export-from-main.217+ef6ef3e1c",
4
4
  "description": "The tRPC Next.js library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -91,12 +91,12 @@
91
91
  "!**/*.test.*"
92
92
  ],
93
93
  "dependencies": {
94
- "@trpc/core": "11.0.0-alpha-tmp-export-from-main.213+855f8bc75"
94
+ "@trpc/core": "11.0.0-alpha-tmp-export-from-main.217+ef6ef3e1c"
95
95
  },
96
96
  "peerDependencies": {
97
97
  "@tanstack/react-query": "^5.0.0",
98
- "@trpc/client": "11.0.0-alpha-tmp-export-from-main.213+855f8bc75",
99
- "@trpc/react-query": "11.0.0-alpha-tmp-export-from-main.213+855f8bc75",
98
+ "@trpc/client": "11.0.0-alpha-tmp-export-from-main.217+ef6ef3e1c",
99
+ "@trpc/react-query": "11.0.0-alpha-tmp-export-from-main.217+ef6ef3e1c",
100
100
  "next": "*",
101
101
  "react": ">=16.8.0",
102
102
  "react-dom": ">=16.8.0"
@@ -111,8 +111,8 @@
111
111
  },
112
112
  "devDependencies": {
113
113
  "@tanstack/react-query": "^5.0.0",
114
- "@trpc/client": "11.0.0-alpha-tmp-export-from-main.213+855f8bc75",
115
- "@trpc/react-query": "11.0.0-alpha-tmp-export-from-main.213+855f8bc75",
114
+ "@trpc/client": "11.0.0-alpha-tmp-export-from-main.217+ef6ef3e1c",
115
+ "@trpc/react-query": "11.0.0-alpha-tmp-export-from-main.217+ef6ef3e1c",
116
116
  "@types/express": "^4.17.17",
117
117
  "@types/node": "^20.10.0",
118
118
  "@types/react": "^18.2.33",
@@ -122,7 +122,7 @@
122
122
  "next": "^14.0.1",
123
123
  "react": "^18.2.0",
124
124
  "react-dom": "^18.2.0",
125
- "rollup": "^2.79.1",
125
+ "rollup": "^4.9.5",
126
126
  "tsx": "^4.0.0",
127
127
  "zod": "^3.0.0"
128
128
  },
@@ -132,5 +132,5 @@
132
132
  "funding": [
133
133
  "https://trpc.io/sponsor"
134
134
  ],
135
- "gitHead": "855f8bc75d6f11780eabf92376c6297f9edf6107"
135
+ "gitHead": "ef6ef3e1c3a4966d30335864d6edf9c94f096717"
136
136
  }
@@ -1,19 +0,0 @@
1
- import '@trpc/core';
2
-
3
- /**
4
- * @internal
5
- */
6
- function generateCacheTag(procedurePath, input) {
7
- return input
8
- ? `${procedurePath}?input=${JSON.stringify(input)}`
9
- : procedurePath;
10
- }
11
- function isFormData(value) {
12
- if (typeof FormData === 'undefined') {
13
- // FormData is not supported
14
- return false;
15
- }
16
- return value instanceof FormData;
17
- }
18
-
19
- export { generateCacheTag as g, isFormData as i };
File without changes