@trpc/server 11.0.0-alpha-tmp-export-from-main-nuke-core.215 → 11.0.0-alpha-tmp-export-from-main.211

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.
Files changed (207) hide show
  1. package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts +9 -0
  2. package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts.map +1 -0
  3. package/dist/@trpc-server/http.d.ts +1 -1
  4. package/dist/@trpc-server/http.d.ts.map +1 -1
  5. package/dist/@trpc-server.d.ts +3 -3
  6. package/dist/@trpc-server.d.ts.map +1 -1
  7. package/dist/adapters/aws-lambda/index.d.ts +1 -1
  8. package/dist/adapters/aws-lambda/index.js +7 -12
  9. package/dist/adapters/aws-lambda/index.mjs +3 -8
  10. package/dist/adapters/aws-lambda/utils.d.ts +1 -1
  11. package/dist/adapters/express.d.ts +1 -1
  12. package/dist/adapters/express.d.ts.map +1 -1
  13. package/dist/adapters/express.js +2 -8
  14. package/dist/adapters/express.mjs +2 -8
  15. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  16. package/dist/adapters/fastify/index.js +4 -12
  17. package/dist/adapters/fastify/index.mjs +2 -10
  18. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -1
  19. package/dist/adapters/fetch/index.js +4 -10
  20. package/dist/adapters/fetch/index.mjs +2 -8
  21. package/dist/adapters/fetch/types.d.ts +1 -1
  22. package/dist/adapters/next.d.ts +1 -1
  23. package/dist/adapters/next.js +5 -11
  24. package/dist/adapters/next.mjs +3 -9
  25. package/dist/adapters/node-http/content-type/json/index.js +3 -5
  26. package/dist/adapters/node-http/content-type/json/index.mjs +1 -3
  27. package/dist/adapters/node-http/index.js +2 -8
  28. package/dist/adapters/node-http/index.mjs +2 -8
  29. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
  30. package/dist/adapters/node-http/types.d.ts +2 -2
  31. package/dist/adapters/node-http/types.d.ts.map +1 -1
  32. package/dist/adapters/standalone.d.ts +1 -1
  33. package/dist/adapters/standalone.js +2 -8
  34. package/dist/adapters/standalone.mjs +2 -8
  35. package/dist/adapters/ws.d.ts +1 -2
  36. package/dist/adapters/ws.d.ts.map +1 -1
  37. package/dist/adapters/ws.js +26 -31
  38. package/dist/adapters/ws.mjs +1 -6
  39. package/dist/http.d.ts +6 -6
  40. package/dist/http.d.ts.map +1 -1
  41. package/dist/http.js +21 -12
  42. package/dist/http.mjs +1 -7
  43. package/dist/index.js +41 -15
  44. package/dist/index.mjs +1 -5
  45. package/dist/{nodeHTTPRequestHandler-257a3b5f.mjs → nodeHTTPRequestHandler-2d5c8791.mjs} +2 -4
  46. package/dist/{nodeHTTPRequestHandler-f4b7e374.js → nodeHTTPRequestHandler-55f05150.js} +4 -6
  47. package/dist/{nodeHTTPRequestHandler-29178135.js → nodeHTTPRequestHandler-7691fc79.js} +1 -3
  48. package/dist/observable.d.ts +2 -2
  49. package/dist/observable.d.ts.map +1 -1
  50. package/dist/observable.js +25 -9
  51. package/dist/observable.mjs +1 -3
  52. package/dist/rpc.d.ts +2 -2
  53. package/dist/rpc.d.ts.map +1 -1
  54. package/dist/rpc.js +13 -5
  55. package/dist/rpc.mjs +1 -2
  56. package/dist/shared.js +9 -4
  57. package/dist/shared.mjs +1 -2
  58. package/package.json +125 -53
  59. package/src/@trpc-core-unstable-do-not-import-this-please.ts +9 -0
  60. package/src/@trpc-server/http.ts +1 -2
  61. package/src/@trpc-server.ts +3 -3
  62. package/src/adapters/aws-lambda/index.ts +1 -1
  63. package/src/adapters/aws-lambda/utils.ts +1 -1
  64. package/src/adapters/express.ts +2 -1
  65. package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
  66. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
  67. package/src/adapters/fetch/fetchRequestHandler.ts +1 -1
  68. package/src/adapters/fetch/types.ts +1 -1
  69. package/src/adapters/next.ts +1 -1
  70. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +1 -1
  71. package/src/adapters/node-http/types.ts +3 -2
  72. package/src/adapters/standalone.ts +1 -1
  73. package/src/adapters/ws.ts +10 -9
  74. package/src/http.ts +6 -6
  75. package/src/observable.ts +2 -2
  76. package/src/rpc.ts +2 -2
  77. package/dist/TRPCError-10da405a.js +0 -61
  78. package/dist/TRPCError-14edcf88.mjs +0 -63
  79. package/dist/TRPCError-e2c8eb77.js +0 -67
  80. package/dist/batchStreamFormatter-1971db27.js +0 -32
  81. package/dist/batchStreamFormatter-378277dc.mjs +0 -30
  82. package/dist/batchStreamFormatter-4375dc8b.js +0 -31
  83. package/dist/bundle-analysis.json +0 -767
  84. package/dist/contentType-2ed02bef.mjs +0 -52
  85. package/dist/contentType-3a829011.js +0 -54
  86. package/dist/contentType-e4a2373e.js +0 -59
  87. package/dist/createProxy-0306dcaa.mjs +0 -121
  88. package/dist/createProxy-7e413449.js +0 -127
  89. package/dist/createProxy-dce0c414.js +0 -116
  90. package/dist/initTRPC-2cef03ab.js +0 -384
  91. package/dist/initTRPC-38faa18f.js +0 -371
  92. package/dist/initTRPC-fa1b3d57.mjs +0 -376
  93. package/dist/observable-2a5a96d5.js +0 -125
  94. package/dist/observable-62674916.js +0 -125
  95. package/dist/observable-6d025a37.mjs +0 -121
  96. package/dist/operators-0cc4956a.js +0 -99
  97. package/dist/operators-b08617cb.js +0 -103
  98. package/dist/operators-d0c3e794.mjs +0 -99
  99. package/dist/parseTRPCMessage-6e6c204e.js +0 -65
  100. package/dist/parseTRPCMessage-8eb0bbe9.mjs +0 -62
  101. package/dist/parseTRPCMessage-ebdfe3ca.js +0 -67
  102. package/dist/resolveHTTPResponse-833ca2b8.js +0 -265
  103. package/dist/resolveHTTPResponse-9322334c.mjs +0 -291
  104. package/dist/resolveHTTPResponse-d64c48e1.js +0 -293
  105. package/dist/rootConfig-3371c473.js +0 -89
  106. package/dist/rootConfig-571c9267.js +0 -94
  107. package/dist/rootConfig-6b04a6b8.mjs +0 -81
  108. package/dist/router-3344b52a.js +0 -262
  109. package/dist/router-919c37eb.js +0 -259
  110. package/dist/router-d6789e4b.mjs +0 -252
  111. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +0 -6
  112. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +0 -1
  113. package/dist/unstable-core-do-not-import/createProxy.d.ts +0 -19
  114. package/dist/unstable-core-do-not-import/createProxy.d.ts.map +0 -1
  115. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +0 -13
  116. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +0 -1
  117. package/dist/unstable-core-do-not-import/error/formatter.d.ts +0 -33
  118. package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +0 -1
  119. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +0 -15
  120. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +0 -1
  121. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +0 -24
  122. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +0 -1
  123. package/dist/unstable-core-do-not-import/http/contentType.d.ts +0 -29
  124. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +0 -1
  125. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +0 -5
  126. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +0 -1
  127. package/dist/unstable-core-do-not-import/http/index.d.ts +0 -7
  128. package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
  129. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +0 -51
  130. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +0 -1
  131. package/dist/unstable-core-do-not-import/http/types.d.ts +0 -96
  132. package/dist/unstable-core-do-not-import/http/types.d.ts.map +0 -1
  133. package/dist/unstable-core-do-not-import/index.d.ts +0 -35
  134. package/dist/unstable-core-do-not-import/index.d.ts.map +0 -1
  135. package/dist/unstable-core-do-not-import/index.js +0 -51
  136. package/dist/unstable-core-do-not-import/index.mjs +0 -11
  137. package/dist/unstable-core-do-not-import/initTRPC.d.ts +0 -96
  138. package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +0 -1
  139. package/dist/unstable-core-do-not-import/middleware.d.ts +0 -105
  140. package/dist/unstable-core-do-not-import/middleware.d.ts.map +0 -1
  141. package/dist/unstable-core-do-not-import/observable/index.d.ts +0 -5
  142. package/dist/unstable-core-do-not-import/observable/index.d.ts.map +0 -1
  143. package/dist/unstable-core-do-not-import/observable/observable.d.ts +0 -13
  144. package/dist/unstable-core-do-not-import/observable/observable.d.ts.map +0 -1
  145. package/dist/unstable-core-do-not-import/observable/operators.d.ts +0 -8
  146. package/dist/unstable-core-do-not-import/observable/operators.d.ts.map +0 -1
  147. package/dist/unstable-core-do-not-import/observable/types.d.ts +0 -26
  148. package/dist/unstable-core-do-not-import/observable/types.d.ts.map +0 -1
  149. package/dist/unstable-core-do-not-import/parser.d.ts +0 -30
  150. package/dist/unstable-core-do-not-import/parser.d.ts.map +0 -1
  151. package/dist/unstable-core-do-not-import/procedure.d.ts +0 -72
  152. package/dist/unstable-core-do-not-import/procedure.d.ts.map +0 -1
  153. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +0 -101
  154. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +0 -1
  155. package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -75
  156. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +0 -1
  157. package/dist/unstable-core-do-not-import/router.d.ts +0 -85
  158. package/dist/unstable-core-do-not-import/router.d.ts.map +0 -1
  159. package/dist/unstable-core-do-not-import/rpc/codes.d.ts +0 -51
  160. package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +0 -1
  161. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +0 -97
  162. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +0 -1
  163. package/dist/unstable-core-do-not-import/rpc/index.d.ts +0 -5
  164. package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +0 -1
  165. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +0 -5
  166. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +0 -1
  167. package/dist/unstable-core-do-not-import/serialize.d.ts +0 -62
  168. package/dist/unstable-core-do-not-import/serialize.d.ts.map +0 -1
  169. package/dist/unstable-core-do-not-import/transformer.d.ts +0 -107
  170. package/dist/unstable-core-do-not-import/transformer.d.ts.map +0 -1
  171. package/dist/unstable-core-do-not-import/types.d.ts +0 -98
  172. package/dist/unstable-core-do-not-import/types.d.ts.map +0 -1
  173. package/dist/unstable-core-do-not-import/utils.d.ts +0 -31
  174. package/dist/unstable-core-do-not-import/utils.d.ts.map +0 -1
  175. package/src/unstable-core-do-not-import/TRPCInferrable.ts +0 -9
  176. package/src/unstable-core-do-not-import/createProxy.ts +0 -59
  177. package/src/unstable-core-do-not-import/error/TRPCError.ts +0 -82
  178. package/src/unstable-core-do-not-import/error/formatter.ts +0 -51
  179. package/src/unstable-core-do-not-import/error/getErrorShape.ts +0 -36
  180. package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +0 -29
  181. package/src/unstable-core-do-not-import/http/contentType.ts +0 -99
  182. package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +0 -57
  183. package/src/unstable-core-do-not-import/http/index.ts +0 -23
  184. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +0 -458
  185. package/src/unstable-core-do-not-import/http/types.ts +0 -111
  186. package/src/unstable-core-do-not-import/index.ts +0 -108
  187. package/src/unstable-core-do-not-import/initTRPC.ts +0 -206
  188. package/src/unstable-core-do-not-import/middleware.ts +0 -233
  189. package/src/unstable-core-do-not-import/observable/index.ts +0 -10
  190. package/src/unstable-core-do-not-import/observable/observable.ts +0 -155
  191. package/src/unstable-core-do-not-import/observable/operators.ts +0 -119
  192. package/src/unstable-core-do-not-import/observable/types.ts +0 -76
  193. package/src/unstable-core-do-not-import/parser.ts +0 -94
  194. package/src/unstable-core-do-not-import/procedure.ts +0 -108
  195. package/src/unstable-core-do-not-import/procedureBuilder.ts +0 -458
  196. package/src/unstable-core-do-not-import/rootConfig.ts +0 -90
  197. package/src/unstable-core-do-not-import/router.ts +0 -370
  198. package/src/unstable-core-do-not-import/rpc/codes.ts +0 -44
  199. package/src/unstable-core-do-not-import/rpc/envelopes.ts +0 -136
  200. package/src/unstable-core-do-not-import/rpc/index.ts +0 -21
  201. package/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts +0 -85
  202. package/src/unstable-core-do-not-import/serialize.ts +0 -122
  203. package/src/unstable-core-do-not-import/transformer.ts +0 -202
  204. package/src/unstable-core-do-not-import/types.ts +0 -151
  205. package/src/unstable-core-do-not-import/utils.ts +0 -66
  206. package/unstable-core-do-not-import/index.d.ts +0 -1
  207. package/unstable-core-do-not-import/index.js +0 -1
@@ -1,262 +0,0 @@
1
- 'use strict';
2
-
3
- var createProxy = require('./createProxy-7e413449.js');
4
- var TRPCError = require('./TRPCError-e2c8eb77.js');
5
- var rootConfig = require('./rootConfig-3371c473.js');
6
-
7
- const defaultFormatter = ({ shape })=>{
8
- return shape;
9
- };
10
-
11
- /**
12
- * @internal
13
- */ function getDataTransformer(transformer) {
14
- if ('input' in transformer) {
15
- return transformer;
16
- }
17
- return {
18
- input: transformer,
19
- output: transformer
20
- };
21
- }
22
- /**
23
- * @internal
24
- */ const defaultTransformer = {
25
- _default: true,
26
- input: {
27
- serialize: (obj)=>obj,
28
- deserialize: (obj)=>obj
29
- },
30
- output: {
31
- serialize: (obj)=>obj,
32
- deserialize: (obj)=>obj
33
- }
34
- };
35
- function transformTRPCResponseItem(config, item) {
36
- if ('error' in item) {
37
- return {
38
- ...item,
39
- error: config.transformer.output.serialize(item.error)
40
- };
41
- }
42
- if ('data' in item.result) {
43
- return {
44
- ...item,
45
- result: {
46
- ...item.result,
47
- data: config.transformer.output.serialize(item.result.data)
48
- }
49
- };
50
- }
51
- return item;
52
- }
53
- /**
54
- * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
55
- **/ function transformTRPCResponse(config, itemOrItems) {
56
- return Array.isArray(itemOrItems) ? itemOrItems.map((item)=>transformTRPCResponseItem(config, item)) : transformTRPCResponseItem(config, itemOrItems);
57
- }
58
- // FIXME:
59
- // - the generics here are probably unnecessary
60
- // - the RPC-spec could probably be simplified to combine HTTP + WS
61
- /** @internal */ function transformResultInner(response, transformer) {
62
- if ('error' in response) {
63
- const error = transformer.deserialize(response.error);
64
- return {
65
- ok: false,
66
- error: {
67
- ...response,
68
- error
69
- }
70
- };
71
- }
72
- const result = {
73
- ...response.result,
74
- ...(!response.result.type || response.result.type === 'data') && {
75
- type: 'data',
76
- data: transformer.deserialize(response.result.data)
77
- }
78
- };
79
- return {
80
- ok: true,
81
- result
82
- };
83
- }
84
- class TransformResultError extends Error {
85
- constructor(){
86
- super('Unable to transform response from server');
87
- }
88
- }
89
- /**
90
- * Transforms and validates that the result is a valid TRPCResponse
91
- * @internal
92
- */ function transformResult(response, transformer) {
93
- let result;
94
- try {
95
- // Use the data transformers on the JSON-response
96
- result = transformResultInner(response, transformer);
97
- } catch (err) {
98
- throw new TransformResultError();
99
- }
100
- // check that output of the transformers is a valid TRPCResponse
101
- if (!result.ok && (!rootConfig.isObject(result.error.error) || typeof result.error.error['code'] !== 'number')) {
102
- throw new TransformResultError();
103
- }
104
- if (result.ok && !rootConfig.isObject(result.result)) {
105
- throw new TransformResultError();
106
- }
107
- return result;
108
- }
109
-
110
- function isRouter(procedureOrRouter) {
111
- return 'router' in procedureOrRouter._def;
112
- }
113
- const emptyRouter = {
114
- _ctx: null,
115
- _errorShape: null,
116
- _meta: null,
117
- queries: {},
118
- mutations: {},
119
- subscriptions: {},
120
- errorFormatter: defaultFormatter,
121
- transformer: defaultTransformer
122
- };
123
- /**
124
- * Reserved words that can't be used as router or procedure names
125
- */ const reservedWords = [
126
- /**
127
- * Then is a reserved word because otherwise we can't return a promise that returns a Proxy
128
- * since JS will think that `.then` is something that exists
129
- */ 'then'
130
- ];
131
- /**
132
- * @internal
133
- */ function createRouterFactory(config) {
134
- return function createRouterInner(procedures) {
135
- const reservedWordsUsed = new Set(Object.keys(procedures).filter((v)=>reservedWords.includes(v)));
136
- if (reservedWordsUsed.size > 0) {
137
- throw new Error('Reserved words used in `router({})` call: ' + Array.from(reservedWordsUsed).join(', '));
138
- }
139
- const routerProcedures = rootConfig.omitPrototype({});
140
- function recursiveGetPaths(procedures, path = '') {
141
- for (const [key, procedureOrRouter] of Object.entries(procedures ?? {})){
142
- const newPath = `${path}${key}`;
143
- if (isRouter(procedureOrRouter)) {
144
- recursiveGetPaths(procedureOrRouter._def.procedures, `${newPath}.`);
145
- continue;
146
- }
147
- if (routerProcedures[newPath]) {
148
- throw new Error(`Duplicate key: ${newPath}`);
149
- }
150
- routerProcedures[newPath] = procedureOrRouter;
151
- }
152
- }
153
- recursiveGetPaths(procedures);
154
- const _def = {
155
- _config: config,
156
- router: true,
157
- procedures: routerProcedures,
158
- ...emptyRouter,
159
- record: procedures
160
- };
161
- const router = {
162
- ...procedures,
163
- _def,
164
- createCaller (ctx) {
165
- const proxy = createProxy.createRecursiveProxy(({ path , args })=>{
166
- const fullPath = path.join('.');
167
- const procedure = _def.procedures[fullPath];
168
- return procedure({
169
- path: fullPath,
170
- getRawInput: async ()=>args[0],
171
- ctx,
172
- type: procedure._def.type
173
- });
174
- });
175
- return proxy;
176
- }
177
- };
178
- return router;
179
- };
180
- }
181
- function isProcedure(procedureOrRouter) {
182
- return !!procedureOrRouter._def.procedure;
183
- }
184
- /**
185
- * @internal
186
- */ function callProcedure(opts) {
187
- const { type , path } = opts;
188
- const proc = opts.procedures[path];
189
- if (!proc || !isProcedure(proc) || proc._def.type !== type) {
190
- throw new TRPCError.TRPCError({
191
- code: 'NOT_FOUND',
192
- message: `No "${type}"-procedure on path "${path}"`
193
- });
194
- }
195
- return proc(opts);
196
- }
197
- function createCallerFactory() {
198
- return function createCallerInner(router) {
199
- const _def = router._def;
200
- return function createCaller(maybeContext) {
201
- const proxy = createProxy.createRecursiveProxy(({ path , args })=>{
202
- const fullPath = path.join('.');
203
- const procedure = _def.procedures[fullPath];
204
- const callProc = (ctx)=>procedure({
205
- path: fullPath,
206
- getRawInput: async ()=>args[0],
207
- ctx,
208
- type: procedure._def.type
209
- });
210
- if (typeof maybeContext === 'function') {
211
- const context = maybeContext();
212
- if (context instanceof Promise) {
213
- return context.then(callProc);
214
- }
215
- return callProc(context);
216
- }
217
- return callProc(maybeContext);
218
- });
219
- return proxy;
220
- };
221
- };
222
- }
223
- function mergeRouters(...routerList) {
224
- const record = rootConfig.mergeWithoutOverrides({}, ...routerList.map((r)=>r._def.record));
225
- const errorFormatter = routerList.reduce((currentErrorFormatter, nextRouter)=>{
226
- if (nextRouter._def._config.errorFormatter && nextRouter._def._config.errorFormatter !== defaultFormatter) {
227
- if (currentErrorFormatter !== defaultFormatter && currentErrorFormatter !== nextRouter._def._config.errorFormatter) {
228
- throw new Error('You seem to have several error formatters');
229
- }
230
- return nextRouter._def._config.errorFormatter;
231
- }
232
- return currentErrorFormatter;
233
- }, defaultFormatter);
234
- const transformer = routerList.reduce((prev, current)=>{
235
- if (current._def._config.transformer && current._def._config.transformer !== defaultTransformer) {
236
- if (prev !== defaultTransformer && prev !== current._def._config.transformer) {
237
- throw new Error('You seem to have several transformers');
238
- }
239
- return current._def._config.transformer;
240
- }
241
- return prev;
242
- }, defaultTransformer);
243
- const router = createRouterFactory({
244
- errorFormatter,
245
- transformer,
246
- isDev: routerList.some((r)=>r._def._config.isDev),
247
- allowOutsideOfServer: routerList.some((r)=>r._def._config.allowOutsideOfServer),
248
- isServer: routerList.some((r)=>r._def._config.isServer),
249
- $types: routerList[0]?._def._config.$types
250
- })(record);
251
- return router;
252
- }
253
-
254
- exports.callProcedure = callProcedure;
255
- exports.createCallerFactory = createCallerFactory;
256
- exports.createRouterFactory = createRouterFactory;
257
- exports.defaultFormatter = defaultFormatter;
258
- exports.defaultTransformer = defaultTransformer;
259
- exports.getDataTransformer = getDataTransformer;
260
- exports.mergeRouters = mergeRouters;
261
- exports.transformResult = transformResult;
262
- exports.transformTRPCResponse = transformTRPCResponse;
@@ -1,259 +0,0 @@
1
- import { c as createRecursiveProxy } from './createProxy-dce0c414.js';
2
- import { T as TRPCError } from './TRPCError-10da405a.js';
3
- import { i as isObject, m as mergeWithoutOverrides, o as omitPrototype } from './rootConfig-571c9267.js';
4
-
5
- const defaultFormatter = ({ shape }) => {
6
- return shape;
7
- };
8
-
9
- /**
10
- * @internal
11
- */
12
- function getDataTransformer(transformer) {
13
- if ('input' in transformer) {
14
- return transformer;
15
- }
16
- return { input: transformer, output: transformer };
17
- }
18
- /**
19
- * @internal
20
- */
21
- const defaultTransformer = {
22
- _default: true,
23
- input: { serialize: (obj) => obj, deserialize: (obj) => obj },
24
- output: { serialize: (obj) => obj, deserialize: (obj) => obj },
25
- };
26
- function transformTRPCResponseItem(config, item) {
27
- if ('error' in item) {
28
- return {
29
- ...item,
30
- error: config.transformer.output.serialize(item.error),
31
- };
32
- }
33
- if ('data' in item.result) {
34
- return {
35
- ...item,
36
- result: {
37
- ...item.result,
38
- data: config.transformer.output.serialize(item.result.data),
39
- },
40
- };
41
- }
42
- return item;
43
- }
44
- /**
45
- * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
46
- **/
47
- function transformTRPCResponse(config, itemOrItems) {
48
- return Array.isArray(itemOrItems)
49
- ? itemOrItems.map((item) => transformTRPCResponseItem(config, item))
50
- : transformTRPCResponseItem(config, itemOrItems);
51
- }
52
- // FIXME:
53
- // - the generics here are probably unnecessary
54
- // - the RPC-spec could probably be simplified to combine HTTP + WS
55
- /** @internal */
56
- function transformResultInner(response, transformer) {
57
- if ('error' in response) {
58
- const error = transformer.deserialize(response.error);
59
- return {
60
- ok: false,
61
- error: {
62
- ...response,
63
- error,
64
- },
65
- };
66
- }
67
- const result = {
68
- ...response.result,
69
- ...((!response.result.type || response.result.type === 'data') && {
70
- type: 'data',
71
- data: transformer.deserialize(response.result.data),
72
- }),
73
- };
74
- return { ok: true, result };
75
- }
76
- class TransformResultError extends Error {
77
- constructor() {
78
- super('Unable to transform response from server');
79
- }
80
- }
81
- /**
82
- * Transforms and validates that the result is a valid TRPCResponse
83
- * @internal
84
- */
85
- function transformResult(response, transformer) {
86
- let result;
87
- try {
88
- // Use the data transformers on the JSON-response
89
- result = transformResultInner(response, transformer);
90
- }
91
- catch (err) {
92
- throw new TransformResultError();
93
- }
94
- // check that output of the transformers is a valid TRPCResponse
95
- if (!result.ok &&
96
- (!isObject(result.error.error) ||
97
- typeof result.error.error['code'] !== 'number')) {
98
- throw new TransformResultError();
99
- }
100
- if (result.ok && !isObject(result.result)) {
101
- throw new TransformResultError();
102
- }
103
- return result;
104
- }
105
-
106
- function isRouter(procedureOrRouter) {
107
- return 'router' in procedureOrRouter._def;
108
- }
109
- const emptyRouter = {
110
- _ctx: null,
111
- _errorShape: null,
112
- _meta: null,
113
- queries: {},
114
- mutations: {},
115
- subscriptions: {},
116
- errorFormatter: defaultFormatter,
117
- transformer: defaultTransformer,
118
- };
119
- /**
120
- * Reserved words that can't be used as router or procedure names
121
- */
122
- const reservedWords = [
123
- /**
124
- * Then is a reserved word because otherwise we can't return a promise that returns a Proxy
125
- * since JS will think that `.then` is something that exists
126
- */
127
- 'then',
128
- ];
129
- /**
130
- * @internal
131
- */
132
- function createRouterFactory(config) {
133
- return function createRouterInner(procedures) {
134
- const reservedWordsUsed = new Set(Object.keys(procedures).filter((v) => reservedWords.includes(v)));
135
- if (reservedWordsUsed.size > 0) {
136
- throw new Error('Reserved words used in `router({})` call: ' +
137
- Array.from(reservedWordsUsed).join(', '));
138
- }
139
- const routerProcedures = omitPrototype({});
140
- function recursiveGetPaths(procedures, path = '') {
141
- for (const [key, procedureOrRouter] of Object.entries(procedures ?? {})) {
142
- const newPath = `${path}${key}`;
143
- if (isRouter(procedureOrRouter)) {
144
- recursiveGetPaths(procedureOrRouter._def.procedures, `${newPath}.`);
145
- continue;
146
- }
147
- if (routerProcedures[newPath]) {
148
- throw new Error(`Duplicate key: ${newPath}`);
149
- }
150
- routerProcedures[newPath] = procedureOrRouter;
151
- }
152
- }
153
- recursiveGetPaths(procedures);
154
- const _def = {
155
- _config: config,
156
- router: true,
157
- procedures: routerProcedures,
158
- ...emptyRouter,
159
- record: procedures,
160
- };
161
- const router = {
162
- ...procedures,
163
- _def,
164
- createCaller(ctx) {
165
- const proxy = createRecursiveProxy(({ path, args }) => {
166
- const fullPath = path.join('.');
167
- const procedure = _def.procedures[fullPath];
168
- return procedure({
169
- path: fullPath,
170
- getRawInput: async () => args[0],
171
- ctx,
172
- type: procedure._def.type,
173
- });
174
- });
175
- return proxy;
176
- },
177
- };
178
- return router;
179
- };
180
- }
181
- function isProcedure(procedureOrRouter) {
182
- return !!procedureOrRouter._def.procedure;
183
- }
184
- /**
185
- * @internal
186
- */
187
- function callProcedure(opts) {
188
- const { type, path } = opts;
189
- const proc = opts.procedures[path];
190
- if (!proc || !isProcedure(proc) || proc._def.type !== type) {
191
- throw new TRPCError({
192
- code: 'NOT_FOUND',
193
- message: `No "${type}"-procedure on path "${path}"`,
194
- });
195
- }
196
- return proc(opts);
197
- }
198
- function createCallerFactory() {
199
- return function createCallerInner(router) {
200
- const _def = router._def;
201
- return function createCaller(maybeContext) {
202
- const proxy = createRecursiveProxy(({ path, args }) => {
203
- const fullPath = path.join('.');
204
- const procedure = _def.procedures[fullPath];
205
- const callProc = (ctx) => procedure({
206
- path: fullPath,
207
- getRawInput: async () => args[0],
208
- ctx,
209
- type: procedure._def.type,
210
- });
211
- if (typeof maybeContext === 'function') {
212
- const context = maybeContext();
213
- if (context instanceof Promise) {
214
- return context.then(callProc);
215
- }
216
- return callProc(context);
217
- }
218
- return callProc(maybeContext);
219
- });
220
- return proxy;
221
- };
222
- };
223
- }
224
- function mergeRouters(...routerList) {
225
- const record = mergeWithoutOverrides({}, ...routerList.map((r) => r._def.record));
226
- const errorFormatter = routerList.reduce((currentErrorFormatter, nextRouter) => {
227
- if (nextRouter._def._config.errorFormatter &&
228
- nextRouter._def._config.errorFormatter !== defaultFormatter) {
229
- if (currentErrorFormatter !== defaultFormatter &&
230
- currentErrorFormatter !== nextRouter._def._config.errorFormatter) {
231
- throw new Error('You seem to have several error formatters');
232
- }
233
- return nextRouter._def._config.errorFormatter;
234
- }
235
- return currentErrorFormatter;
236
- }, defaultFormatter);
237
- const transformer = routerList.reduce((prev, current) => {
238
- if (current._def._config.transformer &&
239
- current._def._config.transformer !== defaultTransformer) {
240
- if (prev !== defaultTransformer &&
241
- prev !== current._def._config.transformer) {
242
- throw new Error('You seem to have several transformers');
243
- }
244
- return current._def._config.transformer;
245
- }
246
- return prev;
247
- }, defaultTransformer);
248
- const router = createRouterFactory({
249
- errorFormatter,
250
- transformer,
251
- isDev: routerList.some((r) => r._def._config.isDev),
252
- allowOutsideOfServer: routerList.some((r) => r._def._config.allowOutsideOfServer),
253
- isServer: routerList.some((r) => r._def._config.isServer),
254
- $types: routerList[0]?._def._config.$types,
255
- })(record);
256
- return router;
257
- }
258
-
259
- export { defaultTransformer as a, createRouterFactory as b, callProcedure as c, defaultFormatter as d, createCallerFactory as e, transformResult as f, getDataTransformer as g, mergeRouters as m, transformTRPCResponse as t };