@trpc/next 10.26.0 → 10.27.1

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.
@@ -3,8 +3,41 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var client = require('@trpc/client');
6
+ var server = require('@trpc/server');
6
7
  var shared = require('@trpc/server/shared');
7
8
  var React = require('react');
9
+ var shared$1 = require('../shared-2ca37369.js');
10
+
11
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */ function set(obj, path, value) {
12
+ if (typeof path === 'string') {
13
+ path = path.split(/[\.\[\]]/).filter(Boolean);
14
+ }
15
+ if (path.length > 1) {
16
+ const p = path.shift();
17
+ const isArrayIndex = /^\d+$/.test(path[0]);
18
+ obj[p] = obj[p] || (isArrayIndex ? [] : {});
19
+ set(obj[p], path, value);
20
+ return;
21
+ }
22
+ const p1 = path[0];
23
+ if (obj[p1] === undefined) {
24
+ obj[p1] = value;
25
+ } else if (Array.isArray(obj[p1])) {
26
+ obj[p1].push(value);
27
+ } else {
28
+ obj[p1] = [
29
+ obj[p1],
30
+ value
31
+ ];
32
+ }
33
+ }
34
+ function formDataToObject(formData) {
35
+ const obj = {};
36
+ for (const [key, value] of formData.entries()){
37
+ set(obj, key, value);
38
+ }
39
+ return obj;
40
+ }
8
41
 
9
42
  /// <reference types="next" />
10
43
  // ts-prune-ignore-next
@@ -24,5 +57,60 @@ function experimental_createTRPCNextAppDirServer(opts) {
24
57
  return client$1[procedureType](fullPath, ...callOpts.args);
25
58
  });
26
59
  }
60
+ function experimental_createServerActionHandler(t, opts) {
61
+ const config = t._config;
62
+ const { normalizeFormData =true , createContext } = opts;
63
+ const transformer = config.transformer;
64
+ // TODO allow this to take a `TRouter` in addition to a `AnyProcedure`
65
+ return function createServerAction(proc) {
66
+ return async function actionHandler(rawInput) {
67
+ const ctx = undefined;
68
+ try {
69
+ const ctx1 = await createContext();
70
+ if (normalizeFormData && shared$1.isFormData(rawInput)) {
71
+ // Normalizes formdata so we can use `z.object({})` etc on the server
72
+ try {
73
+ rawInput = formDataToObject(rawInput);
74
+ } catch {
75
+ throw new server.TRPCError({
76
+ code: 'INTERNAL_SERVER_ERROR',
77
+ message: 'Failed to convert FormData to an object'
78
+ });
79
+ }
80
+ } else if (rawInput && !shared$1.isFormData(rawInput)) {
81
+ rawInput = transformer.input.deserialize(rawInput);
82
+ }
83
+ const data = await proc({
84
+ input: undefined,
85
+ ctx: ctx1,
86
+ path: 'serverAction',
87
+ rawInput,
88
+ type: proc._type
89
+ });
90
+ const transformedJSON = shared.transformTRPCResponse(config, {
91
+ result: {
92
+ data
93
+ }
94
+ });
95
+ return transformedJSON;
96
+ } catch (cause) {
97
+ const error = server.getTRPCErrorFromUnknown(cause);
98
+ const shape = shared.getErrorShape({
99
+ config,
100
+ ctx,
101
+ error,
102
+ input: rawInput,
103
+ path: 'serverAction',
104
+ type: proc._type
105
+ });
106
+ // TODO: send the right HTTP header?!
107
+ return shared.transformTRPCResponse(t._config, {
108
+ error: shape
109
+ });
110
+ }
111
+ };
112
+ };
113
+ }
27
114
 
115
+ exports.experimental_createServerActionHandler = experimental_createServerActionHandler;
28
116
  exports.experimental_createTRPCNextAppDirServer = experimental_createTRPCNextAppDirServer;
@@ -1,6 +1,39 @@
1
1
  import { createTRPCUntypedClient, clientCallTypeToProcedureType } from '@trpc/client';
2
- import { createRecursiveProxy } from '@trpc/server/shared';
2
+ import { TRPCError, getTRPCErrorFromUnknown } from '@trpc/server';
3
+ import { createRecursiveProxy, transformTRPCResponse, getErrorShape } from '@trpc/server/shared';
3
4
  import { cache } from 'react';
5
+ import { i as isFormData } from '../shared-59b269d5.mjs';
6
+
7
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */ function set(obj, path, value) {
8
+ if (typeof path === 'string') {
9
+ path = path.split(/[\.\[\]]/).filter(Boolean);
10
+ }
11
+ if (path.length > 1) {
12
+ const p = path.shift();
13
+ const isArrayIndex = /^\d+$/.test(path[0]);
14
+ obj[p] = obj[p] || (isArrayIndex ? [] : {});
15
+ set(obj[p], path, value);
16
+ return;
17
+ }
18
+ const p1 = path[0];
19
+ if (obj[p1] === undefined) {
20
+ obj[p1] = value;
21
+ } else if (Array.isArray(obj[p1])) {
22
+ obj[p1].push(value);
23
+ } else {
24
+ obj[p1] = [
25
+ obj[p1],
26
+ value
27
+ ];
28
+ }
29
+ }
30
+ function formDataToObject(formData) {
31
+ const obj = {};
32
+ for (const [key, value] of formData.entries()){
33
+ set(obj, key, value);
34
+ }
35
+ return obj;
36
+ }
4
37
 
5
38
  /// <reference types="next" />
6
39
  // ts-prune-ignore-next
@@ -20,5 +53,59 @@ function experimental_createTRPCNextAppDirServer(opts) {
20
53
  return client[procedureType](fullPath, ...callOpts.args);
21
54
  });
22
55
  }
56
+ function experimental_createServerActionHandler(t, opts) {
57
+ const config = t._config;
58
+ const { normalizeFormData =true , createContext } = opts;
59
+ const transformer = config.transformer;
60
+ // TODO allow this to take a `TRouter` in addition to a `AnyProcedure`
61
+ return function createServerAction(proc) {
62
+ return async function actionHandler(rawInput) {
63
+ const ctx = undefined;
64
+ try {
65
+ const ctx1 = await createContext();
66
+ if (normalizeFormData && isFormData(rawInput)) {
67
+ // Normalizes formdata so we can use `z.object({})` etc on the server
68
+ try {
69
+ rawInput = formDataToObject(rawInput);
70
+ } catch {
71
+ throw new TRPCError({
72
+ code: 'INTERNAL_SERVER_ERROR',
73
+ message: 'Failed to convert FormData to an object'
74
+ });
75
+ }
76
+ } else if (rawInput && !isFormData(rawInput)) {
77
+ rawInput = transformer.input.deserialize(rawInput);
78
+ }
79
+ const data = await proc({
80
+ input: undefined,
81
+ ctx: ctx1,
82
+ path: 'serverAction',
83
+ rawInput,
84
+ type: proc._type
85
+ });
86
+ const transformedJSON = transformTRPCResponse(config, {
87
+ result: {
88
+ data
89
+ }
90
+ });
91
+ return transformedJSON;
92
+ } catch (cause) {
93
+ const error = getTRPCErrorFromUnknown(cause);
94
+ const shape = getErrorShape({
95
+ config,
96
+ ctx,
97
+ error,
98
+ input: rawInput,
99
+ path: 'serverAction',
100
+ type: proc._type
101
+ });
102
+ // TODO: send the right HTTP header?!
103
+ return transformTRPCResponse(t._config, {
104
+ error: shape
105
+ });
106
+ }
107
+ };
108
+ };
109
+ }
23
110
 
24
- export { experimental_createTRPCNextAppDirServer };
111
+ export { experimental_createServerActionHandler, experimental_createTRPCNextAppDirServer };
@@ -1,5 +1,5 @@
1
1
  import { CreateTRPCClientOptions, Resolver, TRPCUntypedClient } from '@trpc/client';
2
- import { AnyQueryProcedure, AnyRouter, Filter, ProtectedIntersection, ThenArg } from '@trpc/server';
2
+ import { AnyProcedure, AnyQueryProcedure, AnyRouter, Filter, ProtectedIntersection, ThenArg, inferHandlerInput } from '@trpc/server';
3
3
  /**
4
4
  * @internal
5
5
  */
@@ -23,5 +23,25 @@ export declare type CreateTRPCNextAppRouter<TRouter extends AnyRouter> = Protect
23
23
  export interface CreateTRPCNextAppRouterOptions<TRouter extends AnyRouter> {
24
24
  config: () => CreateTRPCClientOptions<TRouter>;
25
25
  }
26
+ /**
27
+ * @internal
28
+ */
29
+ export declare function isFormData(value: unknown): value is FormData;
30
+ /**
31
+ * @internal
32
+ */
33
+ export interface ActionHandlerDef {
34
+ input?: any;
35
+ output?: any;
36
+ errorShape: any;
37
+ }
38
+ /**
39
+ * @internal
40
+ */
41
+ export declare type inferActionDef<TProc extends AnyProcedure> = {
42
+ input: inferHandlerInput<TProc>[0];
43
+ output: TProc['_def']['_output_out'];
44
+ errorShape: TProc['_def']['_config']['$types']['errorShape'];
45
+ };
26
46
  export {};
27
47
  //# sourceMappingURL=shared.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/app-dir/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,QAAQ,EACR,iBAAiB,EAClB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,iBAAiB,EACjB,SAAS,EACT,M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pc/server/shared');
4
+
5
+ /**
6
+ * @internal
7
+ */ function isFormData(value) {
8
+ if (typeof FormData === 'undefined') {
9
+ // FormData is not supported
10
+ return false;
11
+ }
12
+ return value instanceof FormData;
13
+ }
14
+
15
+ exports.isFormData = isFormData;
@@ -0,0 +1,13 @@
1
+ import '@trpc/server/shared';
2
+
3
+ /**
4
+ * @internal
5
+ */ function isFormData(value) {
6
+ if (typeof FormData === 'undefined') {
7
+ // FormData is not supported
8
+ return false;
9
+ }
10
+ return value instanceof FormData;
11
+ }
12
+
13
+ export { isFormData as i };
@@ -0,0 +1,14 @@
1
+ import '@trpc/server/shared';
2
+
3
+ /**
4
+ * @internal
5
+ */
6
+ function isFormData(value) {
7
+ if (typeof FormData === 'undefined') {
8
+ // FormData is not supported
9
+ return false;
10
+ }
11
+ return value instanceof FormData;
12
+ }
13
+
14
+ export { isFormData as i };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/next",
3
- "version": "10.26.0",
3
+ "version": "10.27.1",
4
4
  "description": "The tRPC Next.js library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -55,9 +55,9 @@
55
55
  ],
56
56
  "peerDependencies": {
57
57
  "@tanstack/react-query": "^4.18.0",
58
- "@trpc/client": "10.26.0",
59
- "@trpc/react-query": "10.26.0",
60
- "@trpc/server": "10.26.0",
58
+ "@trpc/client": "10.27.1",
59
+ "@trpc/react-query": "10.27.1",
60
+ "@trpc/server": "10.27.1",
61
61
  "next": "*",
62
62
  "react": ">=16.8.0",
63
63
  "react-dom": ">=16.8.0"
@@ -67,16 +67,16 @@
67
67
  },
68
68
  "devDependencies": {
69
69
  "@tanstack/react-query": "^4.18.0",
70
- "@trpc/client": "10.26.0",
71
- "@trpc/react-query": "10.26.0",
72
- "@trpc/server": "10.26.0",
70
+ "@trpc/client": "10.27.1",
71
+ "@trpc/react-query": "10.27.1",
72
+ "@trpc/server": "10.27.1",
73
73
  "@types/express": "^4.17.17",
74
74
  "@types/node": "^18.7.20",
75
75
  "@types/react": "^18.2.6",
76
76
  "@types/react-dom": "^18.2.4",
77
77
  "eslint": "^8.40.0",
78
78
  "express": "^4.17.1",
79
- "next": "^13.3.4",
79
+ "next": "^13.4.3",
80
80
  "react": "^18.2.0",
81
81
  "react-dom": "^18.2.0",
82
82
  "rollup": "^2.79.1",
@@ -86,5 +86,5 @@
86
86
  "publishConfig": {
87
87
  "access": "public"
88
88
  },
89
- "gitHead": "877c5b499676e21e7ebdf79eacde300ffc03325d"
89
+ "gitHead": "dffd2e72cc40f70156adebbb6a2d1e1df39d9808"
90
90
  }