@trpc/next 9.17.1 → 10.0.0-alpha.3

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,3 @@
1
+ export * from './withTRPC';
2
+ export * from './setupNext';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
6
6
  var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
7
7
  var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
8
- var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
8
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
9
9
  var _regeneratorRuntime = require('@babel/runtime/regenerator');
10
10
  var react = require('@trpc/react');
11
11
  var React = require('react');
@@ -13,11 +13,19 @@ var reactQuery = require('react-query');
13
13
  var hydration = require('react-query/hydration');
14
14
  var ssrPrepass = require('react-ssr-prepass');
15
15
 
16
- function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
16
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
17
 
18
- var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
19
- var React__default = /*#__PURE__*/_interopDefault(React);
20
- var ssrPrepass__default = /*#__PURE__*/_interopDefault(ssrPrepass);
18
+ var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
19
+ var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
20
+ var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
21
+ var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
22
+ var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
23
+ var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
24
+ var ssrPrepass__default = /*#__PURE__*/_interopDefaultLegacy(ssrPrepass);
25
+
26
+ 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; }
27
+
28
+ 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__default["default"](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; }
21
29
 
22
30
  function transformQueryOrMutationCacheErrors(result) {
23
31
  var error = result.state.error;
@@ -41,7 +49,7 @@ function transformQueryOrMutationCacheErrors(result) {
41
49
  function withTRPC(opts) {
42
50
  var getClientConfig = opts.config;
43
51
  return function (AppOrPage) {
44
- var trpc = react.createReactQueryHooks();
52
+ var trpc = react.createReactQueryHooks(opts);
45
53
 
46
54
  var WithTRPC = function WithTRPC(props) {
47
55
  var _props$pageProps;
@@ -61,34 +69,34 @@ function withTRPC(opts) {
61
69
  ssrContext: null
62
70
  };
63
71
  }),
64
- _useState2 = _slicedToArray(_useState, 1),
72
+ _useState2 = _slicedToArray__default["default"](_useState, 1),
65
73
  _useState2$ = _useState2[0],
66
74
  queryClient = _useState2$.queryClient,
67
75
  trpcClient = _useState2$.trpcClient,
68
76
  isPrepass = _useState2$.isPrepass,
69
77
  ssrContext = _useState2$.ssrContext;
70
78
 
71
- var hydratedState = trpc.useDehydratedState(trpcClient, (_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
72
- return /*#__PURE__*/React__default['default'].createElement(trpc.Provider, {
79
+ var hydratedState = trpc.useDehydratedState((_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
80
+ return /*#__PURE__*/React__default["default"].createElement(trpc.Provider, {
73
81
  client: trpcClient,
74
82
  queryClient: queryClient,
75
83
  isPrepass: isPrepass,
76
84
  ssrContext: ssrContext
77
- }, /*#__PURE__*/React__default['default'].createElement(reactQuery.QueryClientProvider, {
85
+ }, /*#__PURE__*/React__default["default"].createElement(reactQuery.QueryClientProvider, {
78
86
  client: queryClient
79
- }, /*#__PURE__*/React__default['default'].createElement(hydration.Hydrate, {
87
+ }, /*#__PURE__*/React__default["default"].createElement(hydration.Hydrate, {
80
88
  state: hydratedState
81
- }, /*#__PURE__*/React__default['default'].createElement(AppOrPage, props))));
89
+ }, /*#__PURE__*/React__default["default"].createElement(AppOrPage, props))));
82
90
  };
83
91
 
84
92
  if (AppOrPage.getInitialProps || opts.ssr) {
85
93
  WithTRPC.getInitialProps = /*#__PURE__*/function () {
86
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee(appOrPageCtx) {
87
- var _opts$responseMeta;
94
+ var _ref = _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(appOrPageCtx) {
95
+ var _opts$responseMeta, _opts$responseMeta2;
88
96
 
89
97
  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;
90
98
 
91
- return _regeneratorRuntime__default['default'].wrap(function _callee$(_context) {
99
+ return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
92
100
  while (1) {
93
101
  switch (_context.prev = _context.next) {
94
102
  case 0:
@@ -148,7 +156,7 @@ function withTRPC(opts) {
148
156
  case 18:
149
157
 
150
158
  _context.next = 21;
151
- return ssrPrepass__default['default']( /*#__PURE__*/React.createElement(AppTree, prepassProps));
159
+ return ssrPrepass__default["default"]( /*#__PURE__*/React.createElement(AppTree, prepassProps));
152
160
 
153
161
  case 21:
154
162
  if (queryClient.isFetching()) {
@@ -186,19 +194,19 @@ function withTRPC(opts) {
186
194
  mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
187
195
  }); // dehydrate query client's state and add it to the props
188
196
 
189
- pageProps.trpcState = trpcClient.runtime.transformer.serialize(dehydratedCacheWithErrors);
197
+ pageProps.trpcState = opts.transformer ? opts.transformer.serialize(dehydratedCacheWithErrors) : dehydratedCacheWithErrors;
190
198
  appTreeProps = getAppTreeProps(pageProps);
191
- meta = ((_opts$responseMeta = opts.responseMeta) === null || _opts$responseMeta === void 0 ? void 0 : _opts$responseMeta.call(opts, {
199
+ meta = (_opts$responseMeta = (_opts$responseMeta2 = opts.responseMeta) === null || _opts$responseMeta2 === void 0 ? void 0 : _opts$responseMeta2.call(opts, {
192
200
  ctx: ctx,
193
- clientErrors: [].concat(_toConsumableArray(dehydratedCache.queries), _toConsumableArray(dehydratedCache.mutations)).map(function (v) {
201
+ clientErrors: [].concat(_toConsumableArray__default["default"](dehydratedCache.queries), _toConsumableArray__default["default"](dehydratedCache.mutations)).map(function (v) {
194
202
  return v.state.error;
195
203
  }).flatMap(function (err) {
196
204
  return err instanceof Error && err.name === 'TRPCClientError' ? [err] : [];
197
205
  })
198
- })) || {};
206
+ })) !== null && _opts$responseMeta !== void 0 ? _opts$responseMeta : {};
199
207
 
200
- for (_i = 0, _Object$entries = Object.entries(meta.headers || {}); _i < _Object$entries.length; _i++) {
201
- _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
208
+ for (_i = 0, _Object$entries = Object.entries(meta); _i < _Object$entries.length; _i++) {
209
+ _Object$entries$_i = _slicedToArray__default["default"](_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
202
210
 
203
211
  if (typeof value === 'string') {
204
212
  (_ctx$res = ctx.res) === null || _ctx$res === void 0 ? void 0 : _ctx$res.setHeader(key, value);
@@ -231,4 +239,20 @@ function withTRPC(opts) {
231
239
  };
232
240
  }
233
241
 
242
+ function setupTRPC(opts) {
243
+ var hooks = react.createReactQueryHooks(opts);
244
+
245
+ var _withTRPC = withTRPC(opts);
246
+
247
+ return {
248
+ useContext: hooks.useContext,
249
+ useInfiniteQuery: hooks.useInfiniteQuery,
250
+ useMutation: hooks.useMutation,
251
+ useQuery: hooks.useQuery,
252
+ useSubscription: hooks.useSubscription,
253
+ withTRPC: _withTRPC
254
+ };
255
+ }
256
+
257
+ exports.setupTRPC = setupTRPC;
234
258
  exports.withTRPC = withTRPC;
@@ -1,7 +1,7 @@
1
- import _toConsumableArray from '@babel/runtime/helpers/esm/toConsumableArray';
2
- import _asyncToGenerator from '@babel/runtime/helpers/esm/asyncToGenerator';
3
- import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
4
- import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
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
5
  import _regeneratorRuntime from '@babel/runtime/regenerator';
6
6
  import { createReactQueryHooks, createTRPCClient } from '@trpc/react';
7
7
  import React, { useState, createElement } from 'react';
@@ -9,6 +9,10 @@ import { QueryClient, QueryClientProvider } from 'react-query';
9
9
  import { Hydrate, dehydrate } from 'react-query/hydration';
10
10
  import ssrPrepass from 'react-ssr-prepass';
11
11
 
12
+ 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; }
13
+
14
+ 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; }
15
+
12
16
  function transformQueryOrMutationCacheErrors(result) {
13
17
  var error = result.state.error;
14
18
 
@@ -31,7 +35,7 @@ function transformQueryOrMutationCacheErrors(result) {
31
35
  function withTRPC(opts) {
32
36
  var getClientConfig = opts.config;
33
37
  return function (AppOrPage) {
34
- var trpc = createReactQueryHooks();
38
+ var trpc = createReactQueryHooks(opts);
35
39
 
36
40
  var WithTRPC = function WithTRPC(props) {
37
41
  var _props$pageProps;
@@ -58,7 +62,7 @@ function withTRPC(opts) {
58
62
  isPrepass = _useState2$.isPrepass,
59
63
  ssrContext = _useState2$.ssrContext;
60
64
 
61
- var hydratedState = trpc.useDehydratedState(trpcClient, (_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
65
+ var hydratedState = trpc.useDehydratedState((_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
62
66
  return /*#__PURE__*/React.createElement(trpc.Provider, {
63
67
  client: trpcClient,
64
68
  queryClient: queryClient,
@@ -74,7 +78,7 @@ function withTRPC(opts) {
74
78
  if (AppOrPage.getInitialProps || opts.ssr) {
75
79
  WithTRPC.getInitialProps = /*#__PURE__*/function () {
76
80
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(appOrPageCtx) {
77
- var _opts$responseMeta;
81
+ var _opts$responseMeta, _opts$responseMeta2;
78
82
 
79
83
  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;
80
84
 
@@ -176,18 +180,18 @@ function withTRPC(opts) {
176
180
  mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
177
181
  }); // dehydrate query client's state and add it to the props
178
182
 
179
- pageProps.trpcState = trpcClient.runtime.transformer.serialize(dehydratedCacheWithErrors);
183
+ pageProps.trpcState = opts.transformer ? opts.transformer.serialize(dehydratedCacheWithErrors) : dehydratedCacheWithErrors;
180
184
  appTreeProps = getAppTreeProps(pageProps);
181
- meta = ((_opts$responseMeta = opts.responseMeta) === null || _opts$responseMeta === void 0 ? void 0 : _opts$responseMeta.call(opts, {
185
+ meta = (_opts$responseMeta = (_opts$responseMeta2 = opts.responseMeta) === null || _opts$responseMeta2 === void 0 ? void 0 : _opts$responseMeta2.call(opts, {
182
186
  ctx: ctx,
183
187
  clientErrors: [].concat(_toConsumableArray(dehydratedCache.queries), _toConsumableArray(dehydratedCache.mutations)).map(function (v) {
184
188
  return v.state.error;
185
189
  }).flatMap(function (err) {
186
190
  return err instanceof Error && err.name === 'TRPCClientError' ? [err] : [];
187
191
  })
188
- })) || {};
192
+ })) !== null && _opts$responseMeta !== void 0 ? _opts$responseMeta : {};
189
193
 
190
- for (_i = 0, _Object$entries = Object.entries(meta.headers || {}); _i < _Object$entries.length; _i++) {
194
+ for (_i = 0, _Object$entries = Object.entries(meta); _i < _Object$entries.length; _i++) {
191
195
  _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
192
196
 
193
197
  if (typeof value === 'string') {
@@ -221,4 +225,19 @@ function withTRPC(opts) {
221
225
  };
222
226
  }
223
227
 
224
- export { withTRPC };
228
+ function setupTRPC(opts) {
229
+ var hooks = createReactQueryHooks(opts);
230
+
231
+ var _withTRPC = withTRPC(opts);
232
+
233
+ return {
234
+ useContext: hooks.useContext,
235
+ useInfiniteQuery: hooks.useInfiniteQuery,
236
+ useMutation: hooks.useMutation,
237
+ useQuery: hooks.useQuery,
238
+ useSubscription: hooks.useSubscription,
239
+ withTRPC: _withTRPC
240
+ };
241
+ }
242
+
243
+ export { setupTRPC, withTRPC };
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,50 @@
1
+ import { AnyRouter } from '@trpc/server';
2
+ import { WithTRPCNoSSROptions, WithTRPCSSROptions } from './withTRPC';
3
+ export declare function setupTRPC<TRouter extends AnyRouter>(opts: WithTRPCNoSSROptions<TRouter> | WithTRPCSSROptions<TRouter>): {
4
+ useContext: () => import("@trpc/react").TRPCContextState<TRouter, unknown>;
5
+ useInfiniteQuery: <TPath_3 extends { [TPath in keyof TRouter["_def"]["queries"]]: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath]> extends {
6
+ cursor?: any;
7
+ } ? TPath : never; }[keyof TRouter["_def"]["queries"]] & string>(pathAndInput: [path: TPath_3, input: Omit<{ [TPath_1 in keyof TRouter["_def"]["queries"]]: {
8
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath_1]>;
9
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["queries"][TPath_1]["call"]>>;
10
+ }; }[TPath_3]["input"], "cursor">], opts?: import("@trpc/react").UseTRPCInfiniteQueryOptions<TPath_3, Omit<{ [TPath_1 in keyof TRouter["_def"]["queries"]]: {
11
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath_1]>;
12
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["queries"][TPath_1]["call"]>>;
13
+ }; }[TPath_3]["input"], "cursor">, { [TPath_1 in keyof TRouter["_def"]["queries"]]: {
14
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath_1]>;
15
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["queries"][TPath_1]["call"]>>;
16
+ }; }[TPath_3]["output"], import("@trpc/react").TRPCClientErrorLike<TRouter>> | undefined) => import("react-query").UseInfiniteQueryResult<{ [TPath_1 in keyof TRouter["_def"]["queries"]]: {
17
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath_1]>;
18
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["queries"][TPath_1]["call"]>>;
19
+ }; }[TPath_3]["output"], import("@trpc/react").TRPCClientErrorLike<TRouter>>;
20
+ useMutation: <TPath_1 extends keyof TRouter["_def"]["mutations"] & string>(path: TPath_1 | [TPath_1], opts?: import("@trpc/react").UseTRPCMutationOptions<{ [TPath_2 in keyof TRouter["_def"]["mutations"]]: {
21
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["mutations"][TPath_2]>;
22
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["mutations"][TPath_2]["call"]>>;
23
+ }; }[TPath_1]["input"], import("@trpc/react").TRPCClientErrorLike<TRouter>, { [TPath_2 in keyof TRouter["_def"]["mutations"]]: {
24
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["mutations"][TPath_2]>;
25
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["mutations"][TPath_2]["call"]>>;
26
+ }; }[TPath_1]["output"]> | undefined) => import("react-query").UseMutationResult<{ [TPath_2 in keyof TRouter["_def"]["mutations"]]: {
27
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["mutations"][TPath_2]>;
28
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["mutations"][TPath_2]["call"]>>;
29
+ }; }[TPath_1]["output"], import("@trpc/react").TRPCClientErrorLike<TRouter>, { [TPath_2 in keyof TRouter["_def"]["mutations"]]: {
30
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["mutations"][TPath_2]>;
31
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["mutations"][TPath_2]["call"]>>;
32
+ }; }[TPath_1]["input"], unknown>;
33
+ useQuery: <TPath_4 extends keyof TRouter["_def"]["queries"] & string>(pathAndInput: [path: TPath_4, ...args: import("@trpc/server").inferHandlerInput<TRouter["_def"]["queries"][TPath_4]>], opts?: import("@trpc/react").UseTRPCQueryOptions<TPath_4, { [TPath_1 in keyof TRouter["_def"]["queries"]]: {
34
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath_1]>;
35
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["queries"][TPath_1]["call"]>>;
36
+ }; }[TPath_4]["input"], { [TPath_1 in keyof TRouter["_def"]["queries"]]: {
37
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath_1]>;
38
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["queries"][TPath_1]["call"]>>;
39
+ }; }[TPath_4]["output"], import("@trpc/react").TRPCClientErrorLike<TRouter>> | undefined) => import("react-query").UseQueryResult<{ [TPath_1 in keyof TRouter["_def"]["queries"]]: {
40
+ input: import("@trpc/server").inferProcedureInput<TRouter["_def"]["queries"][TPath_1]>;
41
+ output: import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["queries"][TPath_1]["call"]>>;
42
+ }; }[TPath_4]["output"], import("@trpc/react").TRPCClientErrorLike<TRouter>>;
43
+ useSubscription: <TPath_2 extends keyof TRouter["_def"]["subscriptions"] & string, TOutput extends ReturnType<import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["subscriptions"][TPath_2]["call"]>>["output"]>>(pathAndInput: [path: TPath_2, ...args: import("@trpc/server").inferHandlerInput<TRouter["_def"]["subscriptions"][TPath_2]>], opts: {
44
+ enabled?: boolean | undefined;
45
+ error?: ((err: import("@trpc/react").TRPCClientErrorLike<TRouter>) => void) | undefined;
46
+ next: (data: TOutput) => void;
47
+ }) => void;
48
+ withTRPC: (AppOrPage: import("next").NextComponentType<any, any, any>) => import("next").NextComponentType<import("next").NextPageContext, {}, {}>;
49
+ };
50
+ //# sourceMappingURL=setupNext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setupNext.d.ts","sourceRoot":"","sources":["../src/setupNext.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAY,MAAM,YAAY,CAAC;AAEhF,wBAAgB,SAAS,CAAC,OAAO,SAAS,SAAS,EACjD,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAAG,kBAAkB,CAAC,OAAO,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAYlE"}
@@ -1,27 +1,31 @@
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 { CreateTRPCClientOptions, TRPCClientError } from '@trpc/react';
6
- import type { AnyRouter, ResponseMeta } from '@trpc/server';
7
- import { NextComponentType, NextPageContext } from 'next/dist/shared/lib/utils';
8
- import { QueryClient } from 'react-query';
9
- declare type QueryClientConfig = ConstructorParameters<typeof QueryClient>[0];
10
- export declare type WithTRPCConfig<TRouter extends AnyRouter> = CreateTRPCClientOptions<TRouter> & {
11
- queryClientConfig?: QueryClientConfig;
12
- };
13
- export declare function withTRPC<TRouter extends AnyRouter>(opts: {
14
- config: (info: {
15
- ctx?: NextPageContext;
16
- }) => WithTRPCConfig<TRouter>;
17
- } & ({
18
- ssr?: false;
19
- } | {
20
- ssr: true;
21
- responseMeta?: (opts: {
22
- ctx: NextPageContext;
23
- clientErrors: TRPCClientError<TRouter>[];
24
- }) => ResponseMeta;
25
- })): (AppOrPage: NextComponentType<any, any, any>) => NextComponentType;
26
- export {};
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 { TRPCClientError, CreateReactQueryHooksOptions } from '@trpc/react';
6
+ import type { AnyRouter, ResponseMeta } from '@trpc/server';
7
+ import { NextComponentType, NextPageContext } from 'next/dist/shared/lib/utils';
8
+ import type { CreateTRPCClientOptions } from '@trpc/client/src/internals/TRPCClient';
9
+ import { QueryClient } from 'react-query';
10
+ declare type QueryClientConfig = ConstructorParameters<typeof QueryClient>[0];
11
+ export declare type WithTRPCConfig<TRouter extends AnyRouter> = CreateTRPCClientOptions<TRouter> & {
12
+ queryClientConfig?: QueryClientConfig;
13
+ };
14
+ interface WithTRPCOptions<TRouter extends AnyRouter> extends CreateReactQueryHooksOptions {
15
+ config: (info: {
16
+ ctx?: NextPageContext;
17
+ }) => WithTRPCConfig<TRouter>;
18
+ }
19
+ export interface WithTRPCSSROptions<TRouter extends AnyRouter> extends WithTRPCOptions<TRouter> {
20
+ ssr: true;
21
+ responseMeta?: (opts: {
22
+ ctx: NextPageContext;
23
+ clientErrors: TRPCClientError<TRouter>[];
24
+ }) => ResponseMeta;
25
+ }
26
+ export interface WithTRPCNoSSROptions<TRouter extends AnyRouter> extends WithTRPCOptions<TRouter> {
27
+ ssr?: false;
28
+ }
29
+ export declare function withTRPC<TRouter extends AnyRouter>(opts: WithTRPCNoSSROptions<TRouter> | WithTRPCSSROptions<TRouter>): (AppOrPage: NextComponentType<any, any, any>) => NextComponentType;
30
+ export {};
27
31
  //# sourceMappingURL=withTRPC.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withTRPC.d.ts","sourceRoot":"","sources":["../src/withTRPC.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAIL,eAAe,EAEf,4BAA4B,EAC7B,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,SAAS,EAAe,YAAY,EAAE,MAAM,cAAc,CAAC;AACzE,OAAO,EAGL,iBAAiB,EACjB,eAAe,EAChB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAErF,OAAO,EAAE,WAAW,EAAuB,MAAM,aAAa,CAAC;AAI/D,aAAK,iBAAiB,GAAG,qBAAqB,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AAwBtE,oBAAY,cAAc,CAAC,OAAO,SAAS,SAAS,IAClD,uBAAuB,CAAC,OAAO,CAAC,GAAG;IACjC,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;CACvC,CAAC;AAEJ,UAAU,eAAe,CAAC,OAAO,SAAS,SAAS,CACjD,SAAQ,4BAA4B;IACpC,MAAM,EAAE,CAAC,IAAI,EAAE;QAAE,GAAG,CAAC,EAAE,eAAe,CAAA;KAAE,KAAK,cAAc,CAAC,OAAO,CAAC,CAAC;CACtE;AAED,MAAM,WAAW,kBAAkB,CAAC,OAAO,SAAS,SAAS,CAC3D,SAAQ,eAAe,CAAC,OAAO,CAAC;IAChC,GAAG,EAAE,IAAI,CAAC;IACV,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE;QACpB,GAAG,EAAE,eAAe,CAAC;QACrB,YAAY,EAAE,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC;KAC1C,KAAK,YAAY,CAAC;CACpB;AACD,MAAM,WAAW,oBAAoB,CAAC,OAAO,SAAS,SAAS,CAC7D,SAAQ,eAAe,CAAC,OAAO,CAAC;IAChC,GAAG,CAAC,EAAE,KAAK,CAAC;CACb;AAED,wBAAgB,QAAQ,CAAC,OAAO,SAAS,SAAS,EAChD,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAAG,kBAAkB,CAAC,OAAO,CAAC,eAW9C,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,KAAG,iBAAiB,CAsKxE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/next",
3
- "version": "9.17.1",
3
+ "version": "10.0.0-alpha.3",
4
4
  "description": "tRPC Next lib",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -10,15 +10,19 @@
10
10
  "url": "git+https://github.com/trpc/trpc.git",
11
11
  "directory": "packages/next"
12
12
  },
13
- "preconstruct": {
14
- "entrypoints": [
15
- "index.ts"
16
- ]
13
+ "scripts": {
14
+ "build": "rollup -c"
15
+ },
16
+ "main": "dist/index.js",
17
+ "module": "dist/index.mjs",
18
+ "typings": "dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js",
23
+ "default": "./dist/index.js"
24
+ }
17
25
  },
18
- "scripts": {},
19
- "main": "dist/trpc-next.cjs.js",
20
- "module": "dist/trpc-next.esm.js",
21
- "typings": "dist/trpc-next.cjs.d.ts",
22
26
  "files": [
23
27
  "README.md",
24
28
  "dist",
@@ -30,9 +34,9 @@
30
34
  }
31
35
  },
32
36
  "peerDependencies": {
33
- "@trpc/client": "9.17.1",
34
- "@trpc/react": "9.17.1",
35
- "@trpc/server": "9.17.1",
37
+ "@trpc/client": "10.0.0-alpha.3",
38
+ "@trpc/react": "10.0.0-alpha.3",
39
+ "@trpc/server": "10.0.0-alpha.3",
36
40
  "next": "*",
37
41
  "react": ">=16.8.0",
38
42
  "react-dom": ">=16.8.0",
@@ -43,9 +47,9 @@
43
47
  "react-ssr-prepass": "^1.5.0"
44
48
  },
45
49
  "devDependencies": {
46
- "@trpc/client": "9.17.1",
47
- "@trpc/react": "9.17.1",
48
- "@trpc/server": "9.17.1",
50
+ "@trpc/client": "10.0.0-alpha.3",
51
+ "@trpc/react": "10.0.0-alpha.3",
52
+ "@trpc/server": "10.0.0-alpha.3",
49
53
  "@types/express": "^4.17.12",
50
54
  "express": "^4.17.1",
51
55
  "next": "^12.0.1",
@@ -57,5 +61,5 @@
57
61
  "publishConfig": {
58
62
  "access": "public"
59
63
  },
60
- "gitHead": "d7b9442ba2979046417e49986ddcd58d54642347"
64
+ "gitHead": "cd71a2fe5d42efc838ae3d4cebe7a9e1a36d9110"
61
65
  }
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './withTRPC';
2
+ export * from './setupNext';
@@ -0,0 +1,18 @@
1
+ import { createReactQueryHooks } from '@trpc/react';
2
+ import { AnyRouter } from '@trpc/server';
3
+ import { WithTRPCNoSSROptions, WithTRPCSSROptions, withTRPC } from './withTRPC';
4
+
5
+ export function setupTRPC<TRouter extends AnyRouter>(
6
+ opts: WithTRPCNoSSROptions<TRouter> | WithTRPCSSROptions<TRouter>,
7
+ ) {
8
+ const hooks = createReactQueryHooks<TRouter>(opts);
9
+ const _withTRPC = withTRPC(opts);
10
+ return {
11
+ useContext: hooks.useContext,
12
+ useInfiniteQuery: hooks.useInfiniteQuery,
13
+ useMutation: hooks.useMutation,
14
+ useQuery: hooks.useQuery,
15
+ useSubscription: hooks.useSubscription,
16
+ withTRPC: _withTRPC,
17
+ };
18
+ }
package/src/withTRPC.tsx CHANGED
@@ -6,10 +6,10 @@
6
6
  import {
7
7
  createReactQueryHooks,
8
8
  createTRPCClient,
9
- CreateTRPCClientOptions,
10
9
  TRPCClient,
11
10
  TRPCClientError,
12
11
  TRPCClientErrorLike,
12
+ CreateReactQueryHooksOptions,
13
13
  } from '@trpc/react';
14
14
  import type { AnyRouter, Dict, Maybe, ResponseMeta } from '@trpc/server';
15
15
  import {
@@ -18,6 +18,7 @@ import {
18
18
  NextComponentType,
19
19
  NextPageContext,
20
20
  } from 'next/dist/shared/lib/utils';
21
+ import type { CreateTRPCClientOptions } from '@trpc/client/src/internals/TRPCClient';
21
22
  import React, { createElement, useState } from 'react';
22
23
  import { QueryClient, QueryClientProvider } from 'react-query';
23
24
  import { dehydrate, DehydratedState, Hydrate } from 'react-query/hydration';
@@ -52,21 +53,26 @@ export type WithTRPCConfig<TRouter extends AnyRouter> =
52
53
  queryClientConfig?: QueryClientConfig;
53
54
  };
54
55
 
56
+ interface WithTRPCOptions<TRouter extends AnyRouter>
57
+ extends CreateReactQueryHooksOptions {
58
+ config: (info: { ctx?: NextPageContext }) => WithTRPCConfig<TRouter>;
59
+ }
60
+
61
+ export interface WithTRPCSSROptions<TRouter extends AnyRouter>
62
+ extends WithTRPCOptions<TRouter> {
63
+ ssr: true;
64
+ responseMeta?: (opts: {
65
+ ctx: NextPageContext;
66
+ clientErrors: TRPCClientError<TRouter>[];
67
+ }) => ResponseMeta;
68
+ }
69
+ export interface WithTRPCNoSSROptions<TRouter extends AnyRouter>
70
+ extends WithTRPCOptions<TRouter> {
71
+ ssr?: false;
72
+ }
73
+
55
74
  export function withTRPC<TRouter extends AnyRouter>(
56
- opts: {
57
- config: (info: { ctx?: NextPageContext }) => WithTRPCConfig<TRouter>;
58
- } & (
59
- | {
60
- ssr?: false;
61
- }
62
- | {
63
- ssr: true;
64
- responseMeta?: (opts: {
65
- ctx: NextPageContext;
66
- clientErrors: TRPCClientError<TRouter>[];
67
- }) => ResponseMeta;
68
- }
69
- ),
75
+ opts: WithTRPCNoSSROptions<TRouter> | WithTRPCSSROptions<TRouter>,
70
76
  ) {
71
77
  const { config: getClientConfig } = opts;
72
78
 
@@ -78,7 +84,7 @@ export function withTRPC<TRouter extends AnyRouter>(
78
84
  ssrContext: NextPageContext;
79
85
  };
80
86
  return (AppOrPage: NextComponentType<any, any, any>): NextComponentType => {
81
- const trpc = createReactQueryHooks<TRouter, NextPageContext>();
87
+ const trpc = createReactQueryHooks<TRouter, NextPageContext>(opts);
82
88
 
83
89
  const WithTRPC = (
84
90
  props: AppPropsType & {
@@ -102,10 +108,7 @@ export function withTRPC<TRouter extends AnyRouter>(
102
108
  },
103
109
  );
104
110
 
105
- const hydratedState = trpc.useDehydratedState(
106
- trpcClient,
107
- props.pageProps?.trpcState,
108
- );
111
+ const hydratedState = trpc.useDehydratedState(props.pageProps?.trpcState);
109
112
 
110
113
  return (
111
114
  <trpc.Provider
@@ -154,6 +157,7 @@ export function withTRPC<TRouter extends AnyRouter>(
154
157
  if (typeof window !== 'undefined' || !opts.ssr) {
155
158
  return getAppTreeProps(pageProps);
156
159
  }
160
+
157
161
  const config = getClientConfig({ ctx });
158
162
  const trpcClient = createTRPCClient(config);
159
163
  const queryClient = new QueryClient(config.queryClientConfig);
@@ -207,9 +211,9 @@ export function withTRPC<TRouter extends AnyRouter>(
207
211
  ),
208
212
  };
209
213
  // dehydrate query client's state and add it to the props
210
- pageProps.trpcState = trpcClient.runtime.transformer.serialize(
211
- dehydratedCacheWithErrors,
212
- );
214
+ pageProps.trpcState = opts.transformer
215
+ ? opts.transformer.serialize(dehydratedCacheWithErrors)
216
+ : dehydratedCacheWithErrors;
213
217
 
214
218
  const appTreeProps = getAppTreeProps(pageProps);
215
219
 
@@ -226,9 +230,9 @@ export function withTRPC<TRouter extends AnyRouter>(
226
230
  ? [err as TRPCClientError<TRouter>]
227
231
  : [],
228
232
  ),
229
- }) || {};
233
+ }) ?? {};
230
234
 
231
- for (const [key, value] of Object.entries(meta.headers || {})) {
235
+ for (const [key, value] of Object.entries(meta)) {
232
236
  if (typeof value === 'string') {
233
237
  ctx.res?.setHeader(key, value);
234
238
  }
@@ -1,2 +0,0 @@
1
- export * from './withTRPC';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"withTRPC.d.ts","sourceRoot":"../../../src","sources":["withTRPC.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAGL,uBAAuB,EAEvB,eAAe,EAEhB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,SAAS,EAAe,YAAY,EAAE,MAAM,cAAc,CAAC;AACzE,OAAO,EAGL,iBAAiB,EACjB,eAAe,EAChB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,WAAW,EAAuB,MAAM,aAAa,CAAC;AAI/D,aAAK,iBAAiB,GAAG,qBAAqB,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AAwBtE,oBAAY,cAAc,CAAC,OAAO,SAAS,SAAS,IAClD,uBAAuB,CAAC,OAAO,CAAC,GAAG;IACjC,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;CACvC,CAAC;AAEJ,wBAAgB,QAAQ,CAAC,OAAO,SAAS,SAAS,EAChD,IAAI,EAAE;IACJ,MAAM,EAAE,CAAC,IAAI,EAAE;QAAE,GAAG,CAAC,EAAE,eAAe,CAAA;KAAE,KAAK,cAAc,CAAC,OAAO,CAAC,CAAC;CACtE,GAAG,CACA;IACE,GAAG,CAAC,EAAE,KAAK,CAAC;CACb,GACD;IACE,GAAG,EAAE,IAAI,CAAC;IACV,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE;QACpB,GAAG,EAAE,eAAe,CAAC;QACrB,YAAY,EAAE,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC;KAC1C,KAAK,YAAY,CAAC;CACpB,CACJ,eAWkB,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,KAAG,iBAAiB,CAwKxE"}
@@ -1 +0,0 @@
1
- export * from "./declarations/src/index";
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./trpc-next.cjs.prod.js");
5
- } else {
6
- module.exports = require("./trpc-next.cjs.dev.js");
7
- }
@@ -1,234 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
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 _objectSpread = require('@babel/runtime/helpers/objectSpread2');
9
- var _regeneratorRuntime = require('@babel/runtime/regenerator');
10
- var react = require('@trpc/react');
11
- var React = require('react');
12
- var reactQuery = require('react-query');
13
- var hydration = require('react-query/hydration');
14
- var ssrPrepass = require('react-ssr-prepass');
15
-
16
- function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
17
-
18
- var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
19
- var React__default = /*#__PURE__*/_interopDefault(React);
20
- var ssrPrepass__default = /*#__PURE__*/_interopDefault(ssrPrepass);
21
-
22
- function transformQueryOrMutationCacheErrors(result) {
23
- var error = result.state.error;
24
-
25
- if (error instanceof Error && error.name === 'TRPCClientError') {
26
- var newError = {
27
- message: error.message,
28
- data: error.data,
29
- shape: error.shape
30
- };
31
- return _objectSpread(_objectSpread({}, result), {}, {
32
- state: _objectSpread(_objectSpread({}, result.state), {}, {
33
- error: newError
34
- })
35
- });
36
- }
37
-
38
- return result;
39
- }
40
-
41
- function withTRPC(opts) {
42
- var getClientConfig = opts.config;
43
- return function (AppOrPage) {
44
- var trpc = react.createReactQueryHooks();
45
-
46
- var WithTRPC = function WithTRPC(props) {
47
- var _props$pageProps;
48
-
49
- var _useState = React.useState(function () {
50
- if (props.trpc) {
51
- return props.trpc;
52
- }
53
-
54
- var config = getClientConfig({});
55
- var queryClient = new reactQuery.QueryClient(config.queryClientConfig);
56
- var trpcClient = trpc.createClient(config);
57
- return {
58
- queryClient: queryClient,
59
- trpcClient: trpcClient,
60
- isPrepass: false,
61
- ssrContext: null
62
- };
63
- }),
64
- _useState2 = _slicedToArray(_useState, 1),
65
- _useState2$ = _useState2[0],
66
- queryClient = _useState2$.queryClient,
67
- trpcClient = _useState2$.trpcClient,
68
- isPrepass = _useState2$.isPrepass,
69
- ssrContext = _useState2$.ssrContext;
70
-
71
- var hydratedState = trpc.useDehydratedState(trpcClient, (_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
72
- return /*#__PURE__*/React__default['default'].createElement(trpc.Provider, {
73
- client: trpcClient,
74
- queryClient: queryClient,
75
- isPrepass: isPrepass,
76
- ssrContext: ssrContext
77
- }, /*#__PURE__*/React__default['default'].createElement(reactQuery.QueryClientProvider, {
78
- client: queryClient
79
- }, /*#__PURE__*/React__default['default'].createElement(hydration.Hydrate, {
80
- state: hydratedState
81
- }, /*#__PURE__*/React__default['default'].createElement(AppOrPage, props))));
82
- };
83
-
84
- if (AppOrPage.getInitialProps || opts.ssr) {
85
- WithTRPC.getInitialProps = /*#__PURE__*/function () {
86
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee(appOrPageCtx) {
87
- var _opts$responseMeta;
88
-
89
- 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;
90
-
91
- return _regeneratorRuntime__default['default'].wrap(function _callee$(_context) {
92
- while (1) {
93
- switch (_context.prev = _context.next) {
94
- case 0:
95
- AppTree = appOrPageCtx.AppTree; // Determine if we are wrapping an App component or a Page component.
96
-
97
- isApp = !!appOrPageCtx.Component;
98
- ctx = isApp ? appOrPageCtx.ctx : appOrPageCtx; // Run the wrapped component's getInitialProps function.
99
-
100
- pageProps = {};
101
-
102
- if (!AppOrPage.getInitialProps) {
103
- _context.next = 10;
104
- break;
105
- }
106
-
107
- _context.next = 7;
108
- return AppOrPage.getInitialProps(appOrPageCtx);
109
-
110
- case 7:
111
- originalProps = _context.sent;
112
- originalPageProps = isApp ? (_originalProps$pagePr = originalProps.pageProps) !== null && _originalProps$pagePr !== void 0 ? _originalProps$pagePr : {} : originalProps;
113
- pageProps = _objectSpread(_objectSpread({}, originalPageProps), pageProps);
114
-
115
- case 10:
116
- getAppTreeProps = function getAppTreeProps(props) {
117
- return isApp ? {
118
- pageProps: props
119
- } : props;
120
- };
121
-
122
- if (!(typeof window !== 'undefined' || !opts.ssr)) {
123
- _context.next = 13;
124
- break;
125
- }
126
-
127
- return _context.abrupt("return", getAppTreeProps(pageProps));
128
-
129
- case 13:
130
- config = getClientConfig({
131
- ctx: ctx
132
- });
133
- trpcClient = react.createTRPCClient(config);
134
- queryClient = new reactQuery.QueryClient(config.queryClientConfig);
135
- trpcProp = {
136
- config: config,
137
- trpcClient: trpcClient,
138
- queryClient: queryClient,
139
- isPrepass: true,
140
- ssrContext: ctx
141
- };
142
- prepassProps = {
143
- pageProps: pageProps,
144
- trpc: trpcProp
145
- }; // Run the prepass step on AppTree. This will run all trpc queries on the server.
146
- // multiple prepass ensures that we can do batching on the server
147
-
148
- case 18:
149
-
150
- _context.next = 21;
151
- return ssrPrepass__default['default']( /*#__PURE__*/React.createElement(AppTree, prepassProps));
152
-
153
- case 21:
154
- if (queryClient.isFetching()) {
155
- _context.next = 23;
156
- break;
157
- }
158
-
159
- return _context.abrupt("break", 27);
160
-
161
- case 23:
162
- _context.next = 25;
163
- return new Promise(function (resolve) {
164
- var unsub = queryClient.getQueryCache().subscribe(function (event) {
165
- if ((event === null || event === void 0 ? void 0 : event.query.getObserversCount()) === 0) {
166
- resolve();
167
- unsub();
168
- }
169
- });
170
- });
171
-
172
- case 25:
173
- _context.next = 18;
174
- break;
175
-
176
- case 27:
177
- dehydratedCache = hydration.dehydrate(queryClient, {
178
- shouldDehydrateQuery: function shouldDehydrateQuery() {
179
- // makes sure errors are also dehydrated
180
- return true;
181
- }
182
- }); // since error instances can't be serialized, let's make them into `TRPCClientErrorLike`-objects
183
-
184
- dehydratedCacheWithErrors = _objectSpread(_objectSpread({}, dehydratedCache), {}, {
185
- queries: dehydratedCache.queries.map(transformQueryOrMutationCacheErrors),
186
- mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
187
- }); // dehydrate query client's state and add it to the props
188
-
189
- pageProps.trpcState = trpcClient.runtime.transformer.serialize(dehydratedCacheWithErrors);
190
- appTreeProps = getAppTreeProps(pageProps);
191
- meta = ((_opts$responseMeta = opts.responseMeta) === null || _opts$responseMeta === void 0 ? void 0 : _opts$responseMeta.call(opts, {
192
- ctx: ctx,
193
- clientErrors: [].concat(_toConsumableArray(dehydratedCache.queries), _toConsumableArray(dehydratedCache.mutations)).map(function (v) {
194
- return v.state.error;
195
- }).flatMap(function (err) {
196
- return err instanceof Error && err.name === 'TRPCClientError' ? [err] : [];
197
- })
198
- })) || {};
199
-
200
- for (_i = 0, _Object$entries = Object.entries(meta.headers || {}); _i < _Object$entries.length; _i++) {
201
- _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
202
-
203
- if (typeof value === 'string') {
204
- (_ctx$res = ctx.res) === null || _ctx$res === void 0 ? void 0 : _ctx$res.setHeader(key, value);
205
- }
206
- }
207
-
208
- if (meta.status && ctx.res) {
209
- ctx.res.statusCode = meta.status;
210
- }
211
-
212
- return _context.abrupt("return", appTreeProps);
213
-
214
- case 35:
215
- case "end":
216
- return _context.stop();
217
- }
218
- }
219
- }, _callee);
220
- }));
221
-
222
- return function (_x) {
223
- return _ref.apply(this, arguments);
224
- };
225
- }();
226
- }
227
-
228
- var displayName = AppOrPage.displayName || AppOrPage.name || 'Component';
229
- WithTRPC.displayName = "withTRPC(".concat(displayName, ")");
230
- return WithTRPC;
231
- };
232
- }
233
-
234
- exports.withTRPC = withTRPC;