@trpc/next 10.0.0-alpha.4 → 10.0.0-alpha.40

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/README.md CHANGED
@@ -1,3 +1,86 @@
1
+ <p align="center">
2
+ <a href="https://trpc.io/"><img src="../../www/static/img/logo-text.svg" alt="tRPC" height="130"/></a>
3
+ </p>
4
+
5
+ <p align="center">
6
+ <strong>End-to-end typesafe APIs made easy</strong>
7
+ </p>
8
+
9
+ <p align="center">
10
+ <!-- TODO: replace with new version GIF -->
11
+ <img src="https://storage.googleapis.com/trpc/trpcgif.gif" alt="Demo" />
12
+ </p>
13
+
1
14
  # `@trpc/next`
2
15
 
3
- https://trpc.io
16
+ > Connect a tRPC router to Next.js.
17
+
18
+ ## Documentation
19
+
20
+ Full documentation for `@trpc/next` can be found [here](https://trpc.io/docs/nextjs)
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ # npm
26
+ npm install @trpc/next @trpc/react react-query
27
+
28
+ # Yarn
29
+ yarn add @trpc/next @trpc/react react-query
30
+
31
+ # pnpm
32
+ pnpm add @trpc/next @trpc/react react-query
33
+ ```
34
+
35
+ ## Basic Example
36
+
37
+ Setup tRPC in `utils/trpc.ts`.
38
+
39
+ ```ts
40
+ import { setupTRPC } from '@trpc/next';
41
+ // Import the router type from your server file
42
+ import type { AppRouter } from '../pages/api/[trpc].ts';
43
+
44
+ export const trpc = setupTRPC<AppRouter>({
45
+ config() {
46
+ return {
47
+ url: 'http://localhost:3000/api/trpc',
48
+ };
49
+ },
50
+ ssr: true,
51
+ });
52
+ ```
53
+
54
+ Hook up tRPC inside `_app.tsx`.
55
+
56
+ ```ts
57
+ import { trpc } from '~/utils/trpc';
58
+
59
+ const App = ({ Component, pageProps }) => {
60
+ return <Component {...pageProps} />;
61
+ };
62
+
63
+ export default trpc.withTRPC(App);
64
+ ```
65
+
66
+ Now you can query your API in any component.
67
+
68
+ ```ts
69
+ import { trpc } from '~/utils/trpc';
70
+
71
+ export function Hello() {
72
+ const { data, error, status } = trpc.proxy.greeting.useQuery({
73
+ name: 'tRPC',
74
+ });
75
+
76
+ if (error) {
77
+ return <p>{error.message}</p>;
78
+ }
79
+
80
+ if (status !== 'success') {
81
+ return <p>Loading...</p>;
82
+ }
83
+
84
+ return <div>{data && <p>{data.greeting}</p>}</div>;
85
+ }
86
+ ```
package/dist/index.js CHANGED
@@ -48,7 +48,7 @@ function transformQueryOrMutationCacheErrors(result) {
48
48
  function withTRPC(opts) {
49
49
  var getClientConfig = opts.config;
50
50
  return function (AppOrPage) {
51
- var trpc = react.createReactQueryHooks(opts);
51
+ var trpc = react.createReactQueryHooks();
52
52
 
53
53
  var WithTRPC = function WithTRPC(props) {
54
54
  var _props$pageProps;
@@ -64,7 +64,7 @@ function withTRPC(opts) {
64
64
  return {
65
65
  queryClient: queryClient,
66
66
  trpcClient: trpcClient,
67
- isPrepass: false,
67
+ ssrState: opts.ssr ? 'mounting' : false,
68
68
  ssrContext: null
69
69
  };
70
70
  }),
@@ -72,14 +72,14 @@ function withTRPC(opts) {
72
72
  _useState2$ = _useState2[0],
73
73
  queryClient = _useState2$.queryClient,
74
74
  trpcClient = _useState2$.trpcClient,
75
- isPrepass = _useState2$.isPrepass,
75
+ ssrState = _useState2$.ssrState,
76
76
  ssrContext = _useState2$.ssrContext;
77
77
 
78
- var hydratedState = trpc.useDehydratedState((_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
78
+ var hydratedState = trpc.useDehydratedState(trpcClient, (_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
79
79
  return /*#__PURE__*/React__default["default"].createElement(trpc.Provider, {
80
80
  client: trpcClient,
81
81
  queryClient: queryClient,
82
- isPrepass: isPrepass,
82
+ ssrState: ssrState,
83
83
  ssrContext: ssrContext
84
84
  }, /*#__PURE__*/React__default["default"].createElement(reactQuery.QueryClientProvider, {
85
85
  client: queryClient
@@ -91,7 +91,7 @@ function withTRPC(opts) {
91
91
  if (AppOrPage.getInitialProps || opts.ssr) {
92
92
  WithTRPC.getInitialProps = /*#__PURE__*/function () {
93
93
  var _ref = _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(appOrPageCtx) {
94
- var _opts$responseMeta, _opts$responseMeta2;
94
+ var _opts$responseMeta;
95
95
 
96
96
  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;
97
97
 
@@ -143,7 +143,7 @@ function withTRPC(opts) {
143
143
  config: config,
144
144
  trpcClient: trpcClient,
145
145
  queryClient: queryClient,
146
- isPrepass: true,
146
+ ssrState: 'prepass',
147
147
  ssrContext: ctx
148
148
  };
149
149
  prepassProps = {
@@ -193,18 +193,18 @@ function withTRPC(opts) {
193
193
  mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
194
194
  }); // dehydrate query client's state and add it to the props
195
195
 
196
- pageProps.trpcState = opts.transformer ? opts.transformer.serialize(dehydratedCacheWithErrors) : dehydratedCacheWithErrors;
196
+ pageProps.trpcState = trpcClient.runtime.transformer.serialize(dehydratedCacheWithErrors);
197
197
  appTreeProps = getAppTreeProps(pageProps);
198
- meta = (_opts$responseMeta = (_opts$responseMeta2 = opts.responseMeta) === null || _opts$responseMeta2 === void 0 ? void 0 : _opts$responseMeta2.call(opts, {
198
+ meta = ((_opts$responseMeta = opts.responseMeta) === null || _opts$responseMeta === void 0 ? void 0 : _opts$responseMeta.call(opts, {
199
199
  ctx: ctx,
200
200
  clientErrors: [].concat(_toConsumableArray__default["default"](dehydratedCache.queries), _toConsumableArray__default["default"](dehydratedCache.mutations)).map(function (v) {
201
201
  return v.state.error;
202
202
  }).flatMap(function (err) {
203
203
  return err instanceof Error && err.name === 'TRPCClientError' ? [err] : [];
204
204
  })
205
- })) !== null && _opts$responseMeta !== void 0 ? _opts$responseMeta : {};
205
+ })) || {};
206
206
 
207
- for (_i = 0, _Object$entries = Object.entries(meta); _i < _Object$entries.length; _i++) {
207
+ for (_i = 0, _Object$entries = Object.entries(meta.headers || {}); _i < _Object$entries.length; _i++) {
208
208
  _Object$entries$_i = _slicedToArray__default["default"](_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
209
209
 
210
210
  if (typeof value === 'string') {
@@ -239,17 +239,20 @@ function withTRPC(opts) {
239
239
  }
240
240
 
241
241
  function setupTRPC(opts) {
242
- var hooks = react.createReactQueryHooks(opts);
242
+ var hooks = react.createReactQueryHooks();
243
+ var proxy = react.createReactQueryHooksProxy(hooks); // TODO: maybe set TSSRContext to `never` when using `WithTRPCNoSSROptions`
243
244
 
244
245
  var _withTRPC = withTRPC(opts);
245
246
 
246
247
  return {
248
+ proxy: proxy,
247
249
  useContext: hooks.useContext,
248
250
  useInfiniteQuery: hooks.useInfiniteQuery,
249
251
  useMutation: hooks.useMutation,
250
252
  useQuery: hooks.useQuery,
251
253
  useSubscription: hooks.useSubscription,
252
- withTRPC: _withTRPC
254
+ withTRPC: _withTRPC,
255
+ queries: hooks.queries
253
256
  };
254
257
  }
255
258
 
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
3
3
  import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
4
4
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
5
5
  import _regeneratorRuntime from '@babel/runtime/regenerator';
6
- import { createReactQueryHooks, createTRPCClient } from '@trpc/react';
6
+ import { createReactQueryHooks, createTRPCClient, createReactQueryHooksProxy } from '@trpc/react';
7
7
  import React, { useState, createElement } from 'react';
8
8
  import { QueryClient, QueryClientProvider, Hydrate, dehydrate } from 'react-query';
9
9
  import ssrPrepass from 'react-ssr-prepass';
@@ -34,7 +34,7 @@ function transformQueryOrMutationCacheErrors(result) {
34
34
  function withTRPC(opts) {
35
35
  var getClientConfig = opts.config;
36
36
  return function (AppOrPage) {
37
- var trpc = createReactQueryHooks(opts);
37
+ var trpc = createReactQueryHooks();
38
38
 
39
39
  var WithTRPC = function WithTRPC(props) {
40
40
  var _props$pageProps;
@@ -50,7 +50,7 @@ function withTRPC(opts) {
50
50
  return {
51
51
  queryClient: queryClient,
52
52
  trpcClient: trpcClient,
53
- isPrepass: false,
53
+ ssrState: opts.ssr ? 'mounting' : false,
54
54
  ssrContext: null
55
55
  };
56
56
  }),
@@ -58,14 +58,14 @@ function withTRPC(opts) {
58
58
  _useState2$ = _useState2[0],
59
59
  queryClient = _useState2$.queryClient,
60
60
  trpcClient = _useState2$.trpcClient,
61
- isPrepass = _useState2$.isPrepass,
61
+ ssrState = _useState2$.ssrState,
62
62
  ssrContext = _useState2$.ssrContext;
63
63
 
64
- var hydratedState = trpc.useDehydratedState((_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
64
+ var hydratedState = trpc.useDehydratedState(trpcClient, (_props$pageProps = props.pageProps) === null || _props$pageProps === void 0 ? void 0 : _props$pageProps.trpcState);
65
65
  return /*#__PURE__*/React.createElement(trpc.Provider, {
66
66
  client: trpcClient,
67
67
  queryClient: queryClient,
68
- isPrepass: isPrepass,
68
+ ssrState: ssrState,
69
69
  ssrContext: ssrContext
70
70
  }, /*#__PURE__*/React.createElement(QueryClientProvider, {
71
71
  client: queryClient
@@ -77,7 +77,7 @@ function withTRPC(opts) {
77
77
  if (AppOrPage.getInitialProps || opts.ssr) {
78
78
  WithTRPC.getInitialProps = /*#__PURE__*/function () {
79
79
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(appOrPageCtx) {
80
- var _opts$responseMeta, _opts$responseMeta2;
80
+ var _opts$responseMeta;
81
81
 
82
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
83
 
@@ -129,7 +129,7 @@ function withTRPC(opts) {
129
129
  config: config,
130
130
  trpcClient: trpcClient,
131
131
  queryClient: queryClient,
132
- isPrepass: true,
132
+ ssrState: 'prepass',
133
133
  ssrContext: ctx
134
134
  };
135
135
  prepassProps = {
@@ -179,18 +179,18 @@ function withTRPC(opts) {
179
179
  mutations: dehydratedCache.mutations.map(transformQueryOrMutationCacheErrors)
180
180
  }); // dehydrate query client's state and add it to the props
181
181
 
182
- pageProps.trpcState = opts.transformer ? opts.transformer.serialize(dehydratedCacheWithErrors) : dehydratedCacheWithErrors;
182
+ pageProps.trpcState = trpcClient.runtime.transformer.serialize(dehydratedCacheWithErrors);
183
183
  appTreeProps = getAppTreeProps(pageProps);
184
- meta = (_opts$responseMeta = (_opts$responseMeta2 = opts.responseMeta) === null || _opts$responseMeta2 === void 0 ? void 0 : _opts$responseMeta2.call(opts, {
184
+ meta = ((_opts$responseMeta = opts.responseMeta) === null || _opts$responseMeta === void 0 ? void 0 : _opts$responseMeta.call(opts, {
185
185
  ctx: ctx,
186
186
  clientErrors: [].concat(_toConsumableArray(dehydratedCache.queries), _toConsumableArray(dehydratedCache.mutations)).map(function (v) {
187
187
  return v.state.error;
188
188
  }).flatMap(function (err) {
189
189
  return err instanceof Error && err.name === 'TRPCClientError' ? [err] : [];
190
190
  })
191
- })) !== null && _opts$responseMeta !== void 0 ? _opts$responseMeta : {};
191
+ })) || {};
192
192
 
193
- for (_i = 0, _Object$entries = Object.entries(meta); _i < _Object$entries.length; _i++) {
193
+ for (_i = 0, _Object$entries = Object.entries(meta.headers || {}); _i < _Object$entries.length; _i++) {
194
194
  _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1];
195
195
 
196
196
  if (typeof value === 'string') {
@@ -225,17 +225,20 @@ function withTRPC(opts) {
225
225
  }
226
226
 
227
227
  function setupTRPC(opts) {
228
- var hooks = createReactQueryHooks(opts);
228
+ var hooks = createReactQueryHooks();
229
+ var proxy = createReactQueryHooksProxy(hooks); // TODO: maybe set TSSRContext to `never` when using `WithTRPCNoSSROptions`
229
230
 
230
231
  var _withTRPC = withTRPC(opts);
231
232
 
232
233
  return {
234
+ proxy: proxy,
233
235
  useContext: hooks.useContext,
234
236
  useInfiniteQuery: hooks.useInfiniteQuery,
235
237
  useMutation: hooks.useMutation,
236
238
  useQuery: hooks.useQuery,
237
239
  useSubscription: hooks.useSubscription,
238
- withTRPC: _withTRPC
240
+ withTRPC: _withTRPC,
241
+ queries: hooks.queries
239
242
  };
240
243
  }
241
244