@trpc/next 10.0.0-alpha.8 → 10.0.0-proxy-alpha.53

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.mjs CHANGED
@@ -1,242 +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
- import { createReactQueryHooks, createTRPCClient } from '@trpc/react';
1
+ import { QueryClient, QueryClientProvider, Hydrate, dehydrate } from '@tanstack/react-query';
2
+ import { createReactQueryHooks, createTRPCClient, createReactQueryHooksProxy } from '@trpc/react';
7
3
  import React, { useState, createElement } from 'react';
8
- import { QueryClient, QueryClientProvider, Hydrate, dehydrate } from 'react-query';
9
4
  import ssrPrepass from 'react-ssr-prepass';
10
5
 
11
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
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
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
14
-
15
- function transformQueryOrMutationCacheErrors(result) {
16
- var error = result.state.error;
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
- if (error instanceof Error && error.name === 'TRPCClientError') {
19
- var newError = {
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 result;
21
+ return extends_.apply(this, arguments);
32
22
  }
33
23
 
34
- function withTRPC(opts) {
35
- var getClientConfig = opts.config;
36
- return function (AppOrPage) {
37
- var trpc = createReactQueryHooks(opts);
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
- var config = getClientConfig({});
48
- var queryClient = new QueryClient(config.queryClientConfig);
49
- var trpcClient = trpc.createClient(config);
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
- queryClient: queryClient,
52
- trpcClient: trpcClient,
53
- isPrepass: false,
54
- ssrContext: null
37
+ ...result,
38
+ state: {
39
+ ...result.state,
40
+ error: newError
41
+ }
55
42
  };
56
- }),
57
- _useState2 = _slicedToArray(_useState, 1),
58
- _useState2$ = _useState2[0],
59
- queryClient = _useState2$.queryClient,
60
- trpcClient = _useState2$.trpcClient,
61
- isPrepass = _useState2$.isPrepass,
62
- ssrContext = _useState2$.ssrContext;
63
-
64
- var hydratedState = trpc.useDehydratedState((_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
65
- return /*#__PURE__*/React.createElement(trpc.Provider, {
66
- client: trpcClient,
67
- queryClient: queryClient,
68
- isPrepass: isPrepass,
69
- ssrContext: ssrContext
70
- }, /*#__PURE__*/React.createElement(QueryClientProvider, {
71
- client: queryClient
72
- }, /*#__PURE__*/React.createElement(Hydrate, {
73
- state: hydratedState
74
- }, /*#__PURE__*/React.createElement(AppOrPage, props))));
75
- };
76
-
77
- if (AppOrPage.getInitialProps || opts.ssr) {
78
- WithTRPC.getInitialProps = /*#__PURE__*/function () {
79
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(appOrPageCtx) {
80
- var _opts$responseMeta;
81
-
82
- var AppTree, isApp, ctx, pageProps, _originalProps$pagePr, originalProps, originalPageProps, getAppTreeProps, config, trpcClient, queryClient, trpcProp, prepassProps, dehydratedCache, dehydratedCacheWithErrors, appTreeProps, meta, _i, _Object$entries, _Object$entries$_i, key, value, _ctx$res;
83
-
84
- return _regeneratorRuntime.wrap(function _callee$(_context) {
85
- while (1) {
86
- switch (_context.prev = _context.next) {
87
- case 0:
88
- AppTree = appOrPageCtx.AppTree; // Determine if we are wrapping an App component or a Page component.
89
-
90
- isApp = !!appOrPageCtx.Component;
91
- ctx = isApp ? appOrPageCtx.ctx : appOrPageCtx; // Run the wrapped component's getInitialProps function.
92
-
93
- pageProps = {};
94
-
95
- if (!AppOrPage.getInitialProps) {
96
- _context.next = 10;
97
- break;
98
- }
99
-
100
- _context.next = 7;
101
- return AppOrPage.getInitialProps(appOrPageCtx);
102
-
103
- case 7:
104
- originalProps = _context.sent;
105
- originalPageProps = isApp ? (_originalProps$pagePr = originalProps.pageProps) !== null && _originalProps$pagePr !== void 0 ? _originalProps$pagePr : {} : originalProps;
106
- pageProps = _objectSpread(_objectSpread({}, originalPageProps), pageProps);
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
- if (!(typeof window !== 'undefined' || !opts.ssr)) {
116
- _context.next = 13;
117
- break;
118
- }
119
-
120
- return _context.abrupt("return", getAppTreeProps(pageProps));
121
-
122
- case 13:
123
- config = getClientConfig({
124
- ctx: ctx
125
- });
126
- trpcClient = createTRPCClient(config);
127
- queryClient = new QueryClient(config.queryClientConfig);
128
- trpcProp = {
129
- config: config,
130
- trpcClient: trpcClient,
131
- queryClient: queryClient,
132
- isPrepass: true,
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,
108
+ ssrState: 'prepass',
133
109
  ssrContext: ctx
134
- };
135
- prepassProps = {
136
- pageProps: pageProps,
110
+ };
111
+ const prepassProps = {
112
+ pageProps,
137
113
  trpc: trpcProp
138
- }; // Run the prepass step on AppTree. This will run all trpc queries on the server.
139
- // multiple prepass ensures that we can do batching on the server
140
-
141
- case 18:
142
-
143
- _context.next = 21;
144
- return ssrPrepass( /*#__PURE__*/createElement(AppTree, prepassProps));
145
-
146
- case 21:
147
- if (queryClient.isFetching()) {
148
- _context.next = 23;
149
- break;
150
- }
151
-
152
- return _context.abrupt("break", 27);
153
-
154
- case 23:
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
- case 25:
166
- _context.next = 18;
167
- break;
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
- }); // since error instances can't be serialized, let's make them into `TRPCClientErrorLike`-objects
176
-
177
- dehydratedCacheWithErrors = _objectSpread(_objectSpread({}, dehydratedCache), {}, {
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
- }); // dehydrate query client's state and add it to the props
181
-
182
- pageProps.trpcState = opts.transformer ? opts.transformer.serialize(dehydratedCacheWithErrors) : dehydratedCacheWithErrors;
183
- appTreeProps = getAppTreeProps(pageProps);
184
- meta = ((_opts$responseMeta = opts.responseMeta) === null || _opts$responseMeta === void 0 ? void 0 : _opts$responseMeta.call(opts, {
185
- ctx: ctx,
186
- clientErrors: [].concat(_toConsumableArray(dehydratedCache.queries), _toConsumableArray(dehydratedCache.mutations)).map(function (v) {
187
- return v.state.error;
188
- }).flatMap(function (err) {
189
- return err instanceof Error && err.name === 'TRPCClientError' ? [err] : [];
190
- })
191
- })) || {};
192
-
193
- for (_i = 0, _Object$entries = Object.entries(meta.headers || {}); _i < _Object$entries.length; _i++) {
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
- (_ctx$res = ctx.res) === null || _ctx$res === void 0 ? void 0 : _ctx$res.setHeader(key, value);
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
- return _context.abrupt("return", appTreeProps);
206
-
207
- case 35:
208
- case "end":
209
- return _context.stop();
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
- var hooks = createReactQueryHooks(opts);
229
-
230
- var _withTRPC = withTRPC(opts);
231
-
232
- return {
233
- useContext: hooks.useContext,
234
- useInfiniteQuery: hooks.useInfiniteQuery,
235
- useMutation: hooks.useMutation,
236
- useQuery: hooks.useQuery,
237
- useSubscription: hooks.useSubscription,
238
- withTRPC: _withTRPC
239
- };
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
+ };
240
189
  }
241
190
 
242
191
  export { setupTRPC, withTRPC };