@trpc/next 10.0.0-alpha.44 → 10.0.0-alpha.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +161 -220
- package/dist/index.mjs +161 -215
- package/package.json +10 -14
- package/dist/index.ts.js +0 -1
package/dist/index.js
CHANGED
|
@@ -2,11 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
|
|
6
|
-
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
|
|
7
|
-
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
8
|
-
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
9
|
-
var _regeneratorRuntime = require('@babel/runtime/regenerator');
|
|
10
5
|
var reactQuery = require('@tanstack/react-query');
|
|
11
6
|
var react = require('@trpc/react');
|
|
12
7
|
var React = require('react');
|
|
@@ -14,246 +9,192 @@ var ssrPrepass = require('react-ssr-prepass');
|
|
|
14
9
|
|
|
15
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
11
|
|
|
17
|
-
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
|
|
18
|
-
var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
|
|
19
|
-
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
|
|
20
|
-
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
21
|
-
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
|
|
22
12
|
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
23
13
|
var ssrPrepass__default = /*#__PURE__*/_interopDefaultLegacy(ssrPrepass);
|
|
24
14
|
|
|
25
|
-
function
|
|
15
|
+
function extends_() {
|
|
16
|
+
extends_ = Object.assign || function (target) {
|
|
17
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
18
|
+
var source = arguments[i];
|
|
26
19
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
for (var key in source) {
|
|
21
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
22
|
+
target[key] = source[key];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
31
26
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
message: error.message,
|
|
35
|
-
data: error.data,
|
|
36
|
-
shape: error.shape
|
|
37
|
-
};
|
|
38
|
-
return _objectSpread(_objectSpread({}, result), {}, {
|
|
39
|
-
state: _objectSpread(_objectSpread({}, result.state), {}, {
|
|
40
|
-
error: newError
|
|
41
|
-
})
|
|
42
|
-
});
|
|
43
|
-
}
|
|
27
|
+
return target;
|
|
28
|
+
};
|
|
44
29
|
|
|
45
|
-
return
|
|
30
|
+
return extends_.apply(this, arguments);
|
|
46
31
|
}
|
|
47
32
|
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
var trpc = react.createReactQueryHooks();
|
|
52
|
-
|
|
53
|
-
var WithTRPC = function WithTRPC(props) {
|
|
54
|
-
var _props$pageProps;
|
|
55
|
-
|
|
56
|
-
var _useState = React.useState(function () {
|
|
57
|
-
if (props.trpc) {
|
|
58
|
-
return props.trpc;
|
|
59
|
-
}
|
|
33
|
+
function _extends() {
|
|
34
|
+
return extends_.apply(this, arguments);
|
|
35
|
+
}
|
|
60
36
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
37
|
+
function transformQueryOrMutationCacheErrors(result) {
|
|
38
|
+
const error = result.state.error;
|
|
39
|
+
if (error instanceof Error && error.name === 'TRPCClientError') {
|
|
40
|
+
const newError = {
|
|
41
|
+
message: error.message,
|
|
42
|
+
data: error.data,
|
|
43
|
+
shape: error.shape
|
|
44
|
+
};
|
|
64
45
|
return {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
46
|
+
...result,
|
|
47
|
+
state: {
|
|
48
|
+
...result.state,
|
|
49
|
+
error: newError
|
|
50
|
+
}
|
|
69
51
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
case 10:
|
|
123
|
-
getAppTreeProps = function getAppTreeProps(props) {
|
|
124
|
-
return isApp ? {
|
|
125
|
-
pageProps: props
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
function withTRPC(opts) {
|
|
56
|
+
const { config: getClientConfig } = opts;
|
|
57
|
+
return (AppOrPage)=>{
|
|
58
|
+
const trpc = react.createReactQueryHooks();
|
|
59
|
+
const WithTRPC = (props)=>{
|
|
60
|
+
const [{ queryClient , trpcClient , ssrState , ssrContext }] = React.useState(()=>{
|
|
61
|
+
if (props.trpc) {
|
|
62
|
+
return props.trpc;
|
|
63
|
+
}
|
|
64
|
+
const config = getClientConfig({});
|
|
65
|
+
const queryClient = new reactQuery.QueryClient(config.queryClientConfig);
|
|
66
|
+
const trpcClient = trpc.createClient(config);
|
|
67
|
+
return {
|
|
68
|
+
queryClient,
|
|
69
|
+
trpcClient,
|
|
70
|
+
ssrState: opts.ssr ? 'mounting' : false,
|
|
71
|
+
ssrContext: null
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
const hydratedState = trpc.useDehydratedState(trpcClient, props.pageProps?.trpcState);
|
|
75
|
+
return /*#__PURE__*/ React__default["default"].createElement(trpc.Provider, {
|
|
76
|
+
client: trpcClient,
|
|
77
|
+
queryClient: queryClient,
|
|
78
|
+
ssrState: ssrState,
|
|
79
|
+
ssrContext: ssrContext
|
|
80
|
+
}, /*#__PURE__*/ React__default["default"].createElement(reactQuery.QueryClientProvider, {
|
|
81
|
+
client: queryClient
|
|
82
|
+
}, /*#__PURE__*/ React__default["default"].createElement(reactQuery.Hydrate, {
|
|
83
|
+
state: hydratedState
|
|
84
|
+
}, /*#__PURE__*/ React__default["default"].createElement(AppOrPage, _extends({}, props)))));
|
|
85
|
+
};
|
|
86
|
+
if (AppOrPage.getInitialProps || opts.ssr) {
|
|
87
|
+
WithTRPC.getInitialProps = async (appOrPageCtx)=>{
|
|
88
|
+
const AppTree = appOrPageCtx.AppTree;
|
|
89
|
+
// Determine if we are wrapping an App component or a Page component.
|
|
90
|
+
const isApp = !!appOrPageCtx.Component;
|
|
91
|
+
const ctx = isApp ? appOrPageCtx.ctx : appOrPageCtx;
|
|
92
|
+
// Run the wrapped component's getInitialProps function.
|
|
93
|
+
let pageProps = {};
|
|
94
|
+
if (AppOrPage.getInitialProps) {
|
|
95
|
+
const originalProps = await AppOrPage.getInitialProps(appOrPageCtx);
|
|
96
|
+
const originalPageProps = isApp ? originalProps.pageProps ?? {} : originalProps;
|
|
97
|
+
pageProps = {
|
|
98
|
+
...originalPageProps,
|
|
99
|
+
...pageProps
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const getAppTreeProps = (props)=>isApp ? {
|
|
103
|
+
pageProps: props
|
|
126
104
|
} : props;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
});
|
|
140
|
-
trpcClient = react.createTRPCClient(config);
|
|
141
|
-
queryClient = new reactQuery.QueryClient(config.queryClientConfig);
|
|
142
|
-
trpcProp = {
|
|
143
|
-
config: config,
|
|
144
|
-
trpcClient: trpcClient,
|
|
145
|
-
queryClient: queryClient,
|
|
105
|
+
if (typeof window !== 'undefined' || !opts.ssr) {
|
|
106
|
+
return getAppTreeProps(pageProps);
|
|
107
|
+
}
|
|
108
|
+
const config = getClientConfig({
|
|
109
|
+
ctx
|
|
110
|
+
});
|
|
111
|
+
const trpcClient = react.createTRPCClient(config);
|
|
112
|
+
const queryClient = new reactQuery.QueryClient(config.queryClientConfig);
|
|
113
|
+
const trpcProp = {
|
|
114
|
+
config,
|
|
115
|
+
trpcClient,
|
|
116
|
+
queryClient,
|
|
146
117
|
ssrState: 'prepass',
|
|
147
118
|
ssrContext: ctx
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
pageProps
|
|
119
|
+
};
|
|
120
|
+
const prepassProps = {
|
|
121
|
+
pageProps,
|
|
151
122
|
trpc: trpcProp
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
_context.next = 25;
|
|
170
|
-
return new Promise(function (resolve) {
|
|
171
|
-
var unsub = queryClient.getQueryCache().subscribe(function (event) {
|
|
172
|
-
if ((event === null || event === void 0 ? void 0 : event.query.getObserversCount()) === 0) {
|
|
173
|
-
resolve();
|
|
174
|
-
unsub();
|
|
175
|
-
}
|
|
123
|
+
};
|
|
124
|
+
// Run the prepass step on AppTree. This will run all trpc queries on the server.
|
|
125
|
+
// multiple prepass ensures that we can do batching on the server
|
|
126
|
+
while(true){
|
|
127
|
+
// render full tree
|
|
128
|
+
await ssrPrepass__default["default"](/*#__PURE__*/ React.createElement(AppTree, prepassProps));
|
|
129
|
+
if (!queryClient.isFetching()) {
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
// wait until the query cache has settled it's promises
|
|
133
|
+
await new Promise((resolve)=>{
|
|
134
|
+
const unsub = queryClient.getQueryCache().subscribe((event)=>{
|
|
135
|
+
if (event?.query.getObserversCount() === 0) {
|
|
136
|
+
resolve();
|
|
137
|
+
unsub();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
176
140
|
});
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
case 27:
|
|
184
|
-
dehydratedCache = reactQuery.dehydrate(queryClient, {
|
|
185
|
-
shouldDehydrateQuery: function shouldDehydrateQuery() {
|
|
186
|
-
// makes sure errors are also dehydrated
|
|
187
|
-
return true;
|
|
141
|
+
}
|
|
142
|
+
const dehydratedCache = reactQuery.dehydrate(queryClient, {
|
|
143
|
+
shouldDehydrateQuery () {
|
|
144
|
+
// makes sure errors are also dehydrated
|
|
145
|
+
return true;
|
|
188
146
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
147
|
+
});
|
|
148
|
+
// since error instances can't be serialized, let's make them into `TRPCClientErrorLike`-objects
|
|
149
|
+
const dehydratedCacheWithErrors = {
|
|
150
|
+
...dehydratedCache,
|
|
192
151
|
queries: dehydratedCache.queries.map(transformQueryOrMutationCacheErrors),
|
|
193
152
|
mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
ctx
|
|
200
|
-
clientErrors: [
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
_Object$entries$_i = _slicedToArray__default["default"](_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
|
|
209
|
-
|
|
153
|
+
};
|
|
154
|
+
// dehydrate query client's state and add it to the props
|
|
155
|
+
pageProps.trpcState = trpcClient.runtime.transformer.serialize(dehydratedCacheWithErrors);
|
|
156
|
+
const appTreeProps = getAppTreeProps(pageProps);
|
|
157
|
+
const meta = opts.responseMeta?.({
|
|
158
|
+
ctx,
|
|
159
|
+
clientErrors: [
|
|
160
|
+
...dehydratedCache.queries,
|
|
161
|
+
...dehydratedCache.mutations,
|
|
162
|
+
].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
|
|
163
|
+
err
|
|
164
|
+
] : [])
|
|
165
|
+
}) || {};
|
|
166
|
+
for (const [key, value] of Object.entries(meta.headers || {})){
|
|
210
167
|
if (typeof value === 'string') {
|
|
211
|
-
|
|
168
|
+
ctx.res?.setHeader(key, value);
|
|
212
169
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (meta.status && ctx.res) {
|
|
170
|
+
}
|
|
171
|
+
if (meta.status && ctx.res) {
|
|
216
172
|
ctx.res.statusCode = meta.status;
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
}, _callee);
|
|
227
|
-
}));
|
|
228
|
-
|
|
229
|
-
return function (_x) {
|
|
230
|
-
return _ref.apply(this, arguments);
|
|
231
|
-
};
|
|
232
|
-
}();
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
var displayName = AppOrPage.displayName || AppOrPage.name || 'Component';
|
|
236
|
-
WithTRPC.displayName = "withTRPC(".concat(displayName, ")");
|
|
237
|
-
return WithTRPC;
|
|
238
|
-
};
|
|
173
|
+
}
|
|
174
|
+
return appTreeProps;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
const displayName = AppOrPage.displayName || AppOrPage.name || 'Component';
|
|
178
|
+
WithTRPC.displayName = `withTRPC(${displayName})`;
|
|
179
|
+
return WithTRPC;
|
|
180
|
+
};
|
|
239
181
|
}
|
|
240
182
|
|
|
241
183
|
function setupTRPC(opts) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
};
|
|
184
|
+
const hooks = react.createReactQueryHooks();
|
|
185
|
+
const proxy = react.createReactQueryHooksProxy(hooks);
|
|
186
|
+
// TODO: maybe set TSSRContext to `never` when using `WithTRPCNoSSROptions`
|
|
187
|
+
const _withTRPC = withTRPC(opts);
|
|
188
|
+
return {
|
|
189
|
+
proxy,
|
|
190
|
+
useContext: hooks.useContext,
|
|
191
|
+
useInfiniteQuery: hooks.useInfiniteQuery,
|
|
192
|
+
useMutation: hooks.useMutation,
|
|
193
|
+
useQuery: hooks.useQuery,
|
|
194
|
+
useSubscription: hooks.useSubscription,
|
|
195
|
+
withTRPC: _withTRPC,
|
|
196
|
+
queries: hooks.queries
|
|
197
|
+
};
|
|
257
198
|
}
|
|
258
199
|
|
|
259
200
|
exports.setupTRPC = setupTRPC;
|
package/dist/index.mjs
CHANGED
|
@@ -1,245 +1,191 @@
|
|
|
1
|
-
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
2
|
-
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
|
|
3
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
4
|
-
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
5
|
-
import _regeneratorRuntime from '@babel/runtime/regenerator';
|
|
6
1
|
import { QueryClient, QueryClientProvider, Hydrate, dehydrate } from '@tanstack/react-query';
|
|
7
2
|
import { createReactQueryHooks, createTRPCClient, createReactQueryHooksProxy } from '@trpc/react';
|
|
8
3
|
import React, { useState, createElement } from 'react';
|
|
9
4
|
import ssrPrepass from 'react-ssr-prepass';
|
|
10
5
|
|
|
11
|
-
function
|
|
6
|
+
function extends_() {
|
|
7
|
+
extends_ = Object.assign || function (target) {
|
|
8
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
9
|
+
var source = arguments[i];
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
for (var key in source) {
|
|
12
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
13
|
+
target[key] = source[key];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
message: error.message,
|
|
21
|
-
data: error.data,
|
|
22
|
-
shape: error.shape
|
|
23
|
-
};
|
|
24
|
-
return _objectSpread(_objectSpread({}, result), {}, {
|
|
25
|
-
state: _objectSpread(_objectSpread({}, result.state), {}, {
|
|
26
|
-
error: newError
|
|
27
|
-
})
|
|
28
|
-
});
|
|
29
|
-
}
|
|
18
|
+
return target;
|
|
19
|
+
};
|
|
30
20
|
|
|
31
|
-
return
|
|
21
|
+
return extends_.apply(this, arguments);
|
|
32
22
|
}
|
|
33
23
|
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var trpc = createReactQueryHooks();
|
|
38
|
-
|
|
39
|
-
var WithTRPC = function WithTRPC(props) {
|
|
40
|
-
var _props$pageProps;
|
|
41
|
-
|
|
42
|
-
var _useState = useState(function () {
|
|
43
|
-
if (props.trpc) {
|
|
44
|
-
return props.trpc;
|
|
45
|
-
}
|
|
24
|
+
function _extends() {
|
|
25
|
+
return extends_.apply(this, arguments);
|
|
26
|
+
}
|
|
46
27
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
28
|
+
function transformQueryOrMutationCacheErrors(result) {
|
|
29
|
+
const error = result.state.error;
|
|
30
|
+
if (error instanceof Error && error.name === 'TRPCClientError') {
|
|
31
|
+
const newError = {
|
|
32
|
+
message: error.message,
|
|
33
|
+
data: error.data,
|
|
34
|
+
shape: error.shape
|
|
35
|
+
};
|
|
50
36
|
return {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
37
|
+
...result,
|
|
38
|
+
state: {
|
|
39
|
+
...result.state,
|
|
40
|
+
error: newError
|
|
41
|
+
}
|
|
55
42
|
};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
case 10:
|
|
109
|
-
getAppTreeProps = function getAppTreeProps(props) {
|
|
110
|
-
return isApp ? {
|
|
111
|
-
pageProps: props
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
function withTRPC(opts) {
|
|
47
|
+
const { config: getClientConfig } = opts;
|
|
48
|
+
return (AppOrPage)=>{
|
|
49
|
+
const trpc = createReactQueryHooks();
|
|
50
|
+
const WithTRPC = (props)=>{
|
|
51
|
+
const [{ queryClient , trpcClient , ssrState , ssrContext }] = useState(()=>{
|
|
52
|
+
if (props.trpc) {
|
|
53
|
+
return props.trpc;
|
|
54
|
+
}
|
|
55
|
+
const config = getClientConfig({});
|
|
56
|
+
const queryClient = new QueryClient(config.queryClientConfig);
|
|
57
|
+
const trpcClient = trpc.createClient(config);
|
|
58
|
+
return {
|
|
59
|
+
queryClient,
|
|
60
|
+
trpcClient,
|
|
61
|
+
ssrState: opts.ssr ? 'mounting' : false,
|
|
62
|
+
ssrContext: null
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
const hydratedState = trpc.useDehydratedState(trpcClient, props.pageProps?.trpcState);
|
|
66
|
+
return /*#__PURE__*/ React.createElement(trpc.Provider, {
|
|
67
|
+
client: trpcClient,
|
|
68
|
+
queryClient: queryClient,
|
|
69
|
+
ssrState: ssrState,
|
|
70
|
+
ssrContext: ssrContext
|
|
71
|
+
}, /*#__PURE__*/ React.createElement(QueryClientProvider, {
|
|
72
|
+
client: queryClient
|
|
73
|
+
}, /*#__PURE__*/ React.createElement(Hydrate, {
|
|
74
|
+
state: hydratedState
|
|
75
|
+
}, /*#__PURE__*/ React.createElement(AppOrPage, _extends({}, props)))));
|
|
76
|
+
};
|
|
77
|
+
if (AppOrPage.getInitialProps || opts.ssr) {
|
|
78
|
+
WithTRPC.getInitialProps = async (appOrPageCtx)=>{
|
|
79
|
+
const AppTree = appOrPageCtx.AppTree;
|
|
80
|
+
// Determine if we are wrapping an App component or a Page component.
|
|
81
|
+
const isApp = !!appOrPageCtx.Component;
|
|
82
|
+
const ctx = isApp ? appOrPageCtx.ctx : appOrPageCtx;
|
|
83
|
+
// Run the wrapped component's getInitialProps function.
|
|
84
|
+
let pageProps = {};
|
|
85
|
+
if (AppOrPage.getInitialProps) {
|
|
86
|
+
const originalProps = await AppOrPage.getInitialProps(appOrPageCtx);
|
|
87
|
+
const originalPageProps = isApp ? originalProps.pageProps ?? {} : originalProps;
|
|
88
|
+
pageProps = {
|
|
89
|
+
...originalPageProps,
|
|
90
|
+
...pageProps
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const getAppTreeProps = (props)=>isApp ? {
|
|
94
|
+
pageProps: props
|
|
112
95
|
} : props;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
});
|
|
126
|
-
trpcClient = createTRPCClient(config);
|
|
127
|
-
queryClient = new QueryClient(config.queryClientConfig);
|
|
128
|
-
trpcProp = {
|
|
129
|
-
config: config,
|
|
130
|
-
trpcClient: trpcClient,
|
|
131
|
-
queryClient: queryClient,
|
|
96
|
+
if (typeof window !== 'undefined' || !opts.ssr) {
|
|
97
|
+
return getAppTreeProps(pageProps);
|
|
98
|
+
}
|
|
99
|
+
const config = getClientConfig({
|
|
100
|
+
ctx
|
|
101
|
+
});
|
|
102
|
+
const trpcClient = createTRPCClient(config);
|
|
103
|
+
const queryClient = new QueryClient(config.queryClientConfig);
|
|
104
|
+
const trpcProp = {
|
|
105
|
+
config,
|
|
106
|
+
trpcClient,
|
|
107
|
+
queryClient,
|
|
132
108
|
ssrState: 'prepass',
|
|
133
109
|
ssrContext: ctx
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
pageProps
|
|
110
|
+
};
|
|
111
|
+
const prepassProps = {
|
|
112
|
+
pageProps,
|
|
137
113
|
trpc: trpcProp
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
_context.next = 25;
|
|
156
|
-
return new Promise(function (resolve) {
|
|
157
|
-
var unsub = queryClient.getQueryCache().subscribe(function (event) {
|
|
158
|
-
if ((event === null || event === void 0 ? void 0 : event.query.getObserversCount()) === 0) {
|
|
159
|
-
resolve();
|
|
160
|
-
unsub();
|
|
161
|
-
}
|
|
114
|
+
};
|
|
115
|
+
// Run the prepass step on AppTree. This will run all trpc queries on the server.
|
|
116
|
+
// multiple prepass ensures that we can do batching on the server
|
|
117
|
+
while(true){
|
|
118
|
+
// render full tree
|
|
119
|
+
await ssrPrepass(/*#__PURE__*/ createElement(AppTree, prepassProps));
|
|
120
|
+
if (!queryClient.isFetching()) {
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
// wait until the query cache has settled it's promises
|
|
124
|
+
await new Promise((resolve)=>{
|
|
125
|
+
const unsub = queryClient.getQueryCache().subscribe((event)=>{
|
|
126
|
+
if (event?.query.getObserversCount() === 0) {
|
|
127
|
+
resolve();
|
|
128
|
+
unsub();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
162
131
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
case 27:
|
|
170
|
-
dehydratedCache = dehydrate(queryClient, {
|
|
171
|
-
shouldDehydrateQuery: function shouldDehydrateQuery() {
|
|
172
|
-
// makes sure errors are also dehydrated
|
|
173
|
-
return true;
|
|
132
|
+
}
|
|
133
|
+
const dehydratedCache = dehydrate(queryClient, {
|
|
134
|
+
shouldDehydrateQuery () {
|
|
135
|
+
// makes sure errors are also dehydrated
|
|
136
|
+
return true;
|
|
174
137
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
138
|
+
});
|
|
139
|
+
// since error instances can't be serialized, let's make them into `TRPCClientErrorLike`-objects
|
|
140
|
+
const dehydratedCacheWithErrors = {
|
|
141
|
+
...dehydratedCache,
|
|
178
142
|
queries: dehydratedCache.queries.map(transformQueryOrMutationCacheErrors),
|
|
179
143
|
mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
ctx
|
|
186
|
-
clientErrors: [
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
_Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
|
|
195
|
-
|
|
144
|
+
};
|
|
145
|
+
// dehydrate query client's state and add it to the props
|
|
146
|
+
pageProps.trpcState = trpcClient.runtime.transformer.serialize(dehydratedCacheWithErrors);
|
|
147
|
+
const appTreeProps = getAppTreeProps(pageProps);
|
|
148
|
+
const meta = opts.responseMeta?.({
|
|
149
|
+
ctx,
|
|
150
|
+
clientErrors: [
|
|
151
|
+
...dehydratedCache.queries,
|
|
152
|
+
...dehydratedCache.mutations,
|
|
153
|
+
].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
|
|
154
|
+
err
|
|
155
|
+
] : [])
|
|
156
|
+
}) || {};
|
|
157
|
+
for (const [key, value] of Object.entries(meta.headers || {})){
|
|
196
158
|
if (typeof value === 'string') {
|
|
197
|
-
|
|
159
|
+
ctx.res?.setHeader(key, value);
|
|
198
160
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if (meta.status && ctx.res) {
|
|
161
|
+
}
|
|
162
|
+
if (meta.status && ctx.res) {
|
|
202
163
|
ctx.res.statusCode = meta.status;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
}, _callee);
|
|
213
|
-
}));
|
|
214
|
-
|
|
215
|
-
return function (_x) {
|
|
216
|
-
return _ref.apply(this, arguments);
|
|
217
|
-
};
|
|
218
|
-
}();
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
var displayName = AppOrPage.displayName || AppOrPage.name || 'Component';
|
|
222
|
-
WithTRPC.displayName = "withTRPC(".concat(displayName, ")");
|
|
223
|
-
return WithTRPC;
|
|
224
|
-
};
|
|
164
|
+
}
|
|
165
|
+
return appTreeProps;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const displayName = AppOrPage.displayName || AppOrPage.name || 'Component';
|
|
169
|
+
WithTRPC.displayName = `withTRPC(${displayName})`;
|
|
170
|
+
return WithTRPC;
|
|
171
|
+
};
|
|
225
172
|
}
|
|
226
173
|
|
|
227
174
|
function setupTRPC(opts) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
};
|
|
175
|
+
const hooks = createReactQueryHooks();
|
|
176
|
+
const proxy = createReactQueryHooksProxy(hooks);
|
|
177
|
+
// TODO: maybe set TSSRContext to `never` when using `WithTRPCNoSSROptions`
|
|
178
|
+
const _withTRPC = withTRPC(opts);
|
|
179
|
+
return {
|
|
180
|
+
proxy,
|
|
181
|
+
useContext: hooks.useContext,
|
|
182
|
+
useInfiniteQuery: hooks.useInfiniteQuery,
|
|
183
|
+
useMutation: hooks.useMutation,
|
|
184
|
+
useQuery: hooks.useQuery,
|
|
185
|
+
useSubscription: hooks.useSubscription,
|
|
186
|
+
withTRPC: _withTRPC,
|
|
187
|
+
queries: hooks.queries
|
|
188
|
+
};
|
|
243
189
|
}
|
|
244
190
|
|
|
245
191
|
export { setupTRPC, withTRPC };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/next",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.47",
|
|
4
4
|
"description": "tRPC Next lib",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,9 +10,6 @@
|
|
|
10
10
|
"url": "git+https://github.com/trpc/trpc.git",
|
|
11
11
|
"directory": "packages/next"
|
|
12
12
|
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"build": "rollup -c"
|
|
15
|
-
},
|
|
16
13
|
"main": "dist/index.js",
|
|
17
14
|
"module": "dist/index.mjs",
|
|
18
15
|
"typings": "dist/index.d.ts",
|
|
@@ -24,9 +21,9 @@
|
|
|
24
21
|
}
|
|
25
22
|
},
|
|
26
23
|
"files": [
|
|
27
|
-
"README.md",
|
|
28
24
|
"dist",
|
|
29
|
-
"src"
|
|
25
|
+
"src",
|
|
26
|
+
"README.md"
|
|
30
27
|
],
|
|
31
28
|
"eslintConfig": {
|
|
32
29
|
"rules": {
|
|
@@ -39,22 +36,21 @@
|
|
|
39
36
|
},
|
|
40
37
|
"peerDependencies": {
|
|
41
38
|
"@tanstack/react-query": "^4.0.10",
|
|
42
|
-
"@trpc/client": "10.0.0-alpha.
|
|
43
|
-
"@trpc/react": "10.0.0-alpha.
|
|
44
|
-
"@trpc/server": "10.0.0-alpha.
|
|
39
|
+
"@trpc/client": "10.0.0-alpha.47",
|
|
40
|
+
"@trpc/react": "10.0.0-alpha.47",
|
|
41
|
+
"@trpc/server": "10.0.0-alpha.47",
|
|
45
42
|
"next": "*",
|
|
46
43
|
"react": ">=16.8.0",
|
|
47
44
|
"react-dom": ">=16.8.0"
|
|
48
45
|
},
|
|
49
46
|
"dependencies": {
|
|
50
|
-
"@babel/runtime": "^7.9.0",
|
|
51
47
|
"react-ssr-prepass": "^1.5.0"
|
|
52
48
|
},
|
|
53
49
|
"devDependencies": {
|
|
54
50
|
"@tanstack/react-query": "^4.0.10",
|
|
55
|
-
"@trpc/client": "10.0.0-alpha.
|
|
56
|
-
"@trpc/react": "10.0.0-alpha.
|
|
57
|
-
"@trpc/server": "10.0.0-alpha.
|
|
51
|
+
"@trpc/client": "10.0.0-alpha.47",
|
|
52
|
+
"@trpc/react": "10.0.0-alpha.47",
|
|
53
|
+
"@trpc/server": "10.0.0-alpha.47",
|
|
58
54
|
"@types/express": "^4.17.12",
|
|
59
55
|
"express": "^4.17.1",
|
|
60
56
|
"next": "^12.1.6",
|
|
@@ -65,5 +61,5 @@
|
|
|
65
61
|
"publishConfig": {
|
|
66
62
|
"access": "public"
|
|
67
63
|
},
|
|
68
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "c7cfb976538a35e529053add9a6cbda8b94e521d"
|
|
69
65
|
}
|
package/dist/index.ts.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|