@trpc/server 10.43.0 → 11.0.0-next.91

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 (164) hide show
  1. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  2. package/dist/adapters/aws-lambda/index.js +6 -4
  3. package/dist/adapters/aws-lambda/index.mjs +6 -4
  4. package/dist/adapters/aws-lambda/utils.d.ts +3 -1
  5. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  6. package/dist/adapters/express.js +4 -3
  7. package/dist/adapters/express.mjs +4 -3
  8. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  9. package/dist/adapters/fastify/index.js +8 -5
  10. package/dist/adapters/fastify/index.mjs +8 -5
  11. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  12. package/dist/adapters/fetch/index.js +6 -4
  13. package/dist/adapters/fetch/index.mjs +6 -4
  14. package/dist/adapters/fetch/types.d.ts +2 -1
  15. package/dist/adapters/fetch/types.d.ts.map +1 -1
  16. package/dist/adapters/next.js +4 -3
  17. package/dist/adapters/next.mjs +4 -3
  18. package/dist/adapters/node-http/index.js +4 -3
  19. package/dist/adapters/node-http/index.mjs +4 -3
  20. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  21. package/dist/adapters/node-http/types.d.ts +2 -1
  22. package/dist/adapters/node-http/types.d.ts.map +1 -1
  23. package/dist/adapters/standalone.d.ts +1 -6
  24. package/dist/adapters/standalone.d.ts.map +1 -1
  25. package/dist/adapters/standalone.js +8 -19
  26. package/dist/adapters/standalone.mjs +8 -19
  27. package/dist/adapters/ws.d.ts +25 -4
  28. package/dist/adapters/ws.d.ts.map +1 -1
  29. package/dist/adapters/ws.js +67 -14
  30. package/dist/adapters/ws.mjs +58 -6
  31. package/dist/bundle-analysis.json +910 -0
  32. package/dist/config-5de7eae5.js +10 -0
  33. package/dist/config-60fc2667.js +7 -0
  34. package/dist/config-ab4c1c6b.mjs +5 -0
  35. package/dist/core/index.d.ts +1 -1
  36. package/dist/core/index.d.ts.map +1 -1
  37. package/dist/core/initTRPC.d.ts +1 -1
  38. package/dist/core/internals/builderTypes.d.ts +11 -0
  39. package/dist/core/internals/builderTypes.d.ts.map +1 -0
  40. package/dist/core/internals/getParseFn.d.ts +0 -5
  41. package/dist/core/internals/getParseFn.d.ts.map +1 -1
  42. package/dist/core/internals/mergeRouters.d.ts +0 -3
  43. package/dist/core/internals/mergeRouters.d.ts.map +1 -1
  44. package/dist/core/internals/procedureBuilder.d.ts +24 -27
  45. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  46. package/dist/core/internals/utils.d.ts +7 -8
  47. package/dist/core/internals/utils.d.ts.map +1 -1
  48. package/dist/core/middleware.d.ts +16 -15
  49. package/dist/core/middleware.d.ts.map +1 -1
  50. package/dist/core/procedure.d.ts +41 -42
  51. package/dist/core/procedure.d.ts.map +1 -1
  52. package/dist/core/router.d.ts +9 -56
  53. package/dist/core/router.d.ts.map +1 -1
  54. package/dist/core/types.d.ts +7 -12
  55. package/dist/core/types.d.ts.map +1 -1
  56. package/dist/http/index.js +3 -2
  57. package/dist/http/index.mjs +3 -2
  58. package/dist/http/resolveHTTPResponse.d.ts +3 -3
  59. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  60. package/dist/http/types.d.ts +22 -1
  61. package/dist/http/types.d.ts.map +1 -1
  62. package/dist/index.d.ts +10 -2
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +24 -811
  65. package/dist/index.mjs +15 -802
  66. package/dist/{nodeHTTPRequestHandler-cf01c646.mjs → nodeHTTPRequestHandler-0986c5a1.mjs} +8 -3
  67. package/dist/{nodeHTTPRequestHandler-b7d358fe.js → nodeHTTPRequestHandler-18f3370e.js} +8 -3
  68. package/dist/{nodeHTTPRequestHandler-9781faee.js → nodeHTTPRequestHandler-388f5fce.js} +8 -3
  69. package/dist/{resolveHTTPResponse-68c8befb.mjs → resolveHTTPResponse-55c648bd.mjs} +14 -12
  70. package/dist/{resolveHTTPResponse-79011e44.js → resolveHTTPResponse-5def1b0c.js} +15 -13
  71. package/dist/{resolveHTTPResponse-051b3a40.js → resolveHTTPResponse-8d474952.js} +12 -12
  72. package/dist/{config-3ab6b85e.js → router-3c22fe56.js} +9 -46
  73. package/dist/{config-4c0f8e88.mjs → router-5274e533.mjs} +10 -45
  74. package/dist/{config-f356f2fd.js → router-a1635eab.js} +9 -44
  75. package/dist/rpc/envelopes.d.ts +1 -5
  76. package/dist/rpc/envelopes.d.ts.map +1 -1
  77. package/dist/rpc/index.d.ts +0 -1
  78. package/dist/rpc/index.d.ts.map +1 -1
  79. package/dist/rpc/index.js +0 -2
  80. package/dist/rpc/index.mjs +0 -1
  81. package/dist/shared/getErrorShape.d.ts +1 -1
  82. package/dist/shared/getErrorShape.d.ts.map +1 -1
  83. package/dist/shared/index.d.ts +1 -0
  84. package/dist/shared/index.d.ts.map +1 -1
  85. package/dist/shared/jsonify.d.ts +3 -2
  86. package/dist/shared/jsonify.d.ts.map +1 -1
  87. package/dist/shared/transformTRPCResponse.d.ts +1 -1
  88. package/dist/shared/transformTRPCResponse.d.ts.map +1 -1
  89. package/dist/shared/types.d.ts +6 -0
  90. package/dist/shared/types.d.ts.map +1 -0
  91. package/dist/types.d.ts +5 -0
  92. package/dist/types.d.ts.map +1 -1
  93. package/dist/unstableInternalsExport-10cc2bae.mjs +353 -0
  94. package/dist/unstableInternalsExport-a064a7dd.js +349 -0
  95. package/dist/unstableInternalsExport-bf8496ea.js +363 -0
  96. package/dist/unstableInternalsExport.d.ts +11 -0
  97. package/dist/unstableInternalsExport.d.ts.map +1 -0
  98. package/dist/unstableInternalsExport.js +16 -0
  99. package/dist/unstableInternalsExport.mjs +5 -0
  100. package/package.json +9 -3
  101. package/src/adapters/aws-lambda/index.ts +11 -7
  102. package/src/adapters/aws-lambda/utils.ts +3 -0
  103. package/src/adapters/fastify/fastifyRequestHandler.ts +9 -5
  104. package/src/adapters/fetch/fetchRequestHandler.ts +9 -3
  105. package/src/adapters/fetch/types.ts +2 -1
  106. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +15 -4
  107. package/src/adapters/node-http/types.ts +2 -1
  108. package/src/adapters/standalone.ts +4 -22
  109. package/src/adapters/ws.ts +110 -14
  110. package/src/core/index.ts +1 -0
  111. package/src/core/internals/builderTypes.ts +11 -0
  112. package/src/core/internals/getParseFn.ts +0 -13
  113. package/src/core/internals/mergeRouters.ts +0 -4
  114. package/src/core/internals/procedureBuilder.ts +50 -70
  115. package/src/core/internals/utils.ts +11 -8
  116. package/src/core/middleware.ts +37 -31
  117. package/src/core/procedure.ts +46 -54
  118. package/src/core/router.ts +19 -117
  119. package/src/core/types.ts +11 -20
  120. package/src/http/resolveHTTPResponse.ts +17 -14
  121. package/src/http/types.ts +24 -1
  122. package/src/index.ts +16 -2
  123. package/src/rpc/envelopes.ts +1 -6
  124. package/src/rpc/index.ts +0 -1
  125. package/src/shared/getErrorShape.ts +2 -1
  126. package/src/shared/index.ts +2 -0
  127. package/src/shared/jsonify.ts +9 -5
  128. package/src/shared/transformTRPCResponse.ts +1 -1
  129. package/src/shared/types.ts +9 -0
  130. package/src/types.ts +8 -0
  131. package/src/unstableInternalsExport.ts +10 -0
  132. package/unstableInternalsExport/index.d.ts +1 -0
  133. package/unstableInternalsExport/index.js +1 -0
  134. package/adapters/zodFileSchema/index.d.ts +0 -1
  135. package/adapters/zodFileSchema/index.js +0 -1
  136. package/dist/adapters/lambda/index.d.ts +0 -12
  137. package/dist/adapters/lambda/index.d.ts.map +0 -1
  138. package/dist/deprecated/internals/middlewares.d.ts +0 -52
  139. package/dist/deprecated/internals/middlewares.d.ts.map +0 -1
  140. package/dist/deprecated/internals/procedure.d.ts +0 -96
  141. package/dist/deprecated/internals/procedure.d.ts.map +0 -1
  142. package/dist/deprecated/interop.d.ts +0 -44
  143. package/dist/deprecated/interop.d.ts.map +0 -1
  144. package/dist/deprecated/router.d.ts +0 -204
  145. package/dist/deprecated/router.d.ts.map +0 -1
  146. package/dist/internals.d.ts +0 -10
  147. package/dist/internals.d.ts.map +0 -1
  148. package/dist/parseTRPCMessage-1377f305.js +0 -56
  149. package/dist/parseTRPCMessage-95955211.js +0 -63
  150. package/dist/parseTRPCMessage-a0f17853.mjs +0 -54
  151. package/dist/rpc/parseTRPCMessage.d.ts +0 -5
  152. package/dist/rpc/parseTRPCMessage.d.ts.map +0 -1
  153. package/dist/subscription.d.ts +0 -13
  154. package/dist/subscription.d.ts.map +0 -1
  155. package/dist/subscription.js +0 -38
  156. package/dist/subscription.mjs +0 -34
  157. package/src/adapters/lambda/index.ts +0 -18
  158. package/src/deprecated/internals/middlewares.ts +0 -61
  159. package/src/deprecated/internals/procedure.ts +0 -411
  160. package/src/deprecated/interop.ts +0 -232
  161. package/src/deprecated/router.ts +0 -945
  162. package/src/internals.ts +0 -12
  163. package/src/rpc/parseTRPCMessage.ts +0 -84
  164. package/src/subscription.ts +0 -43
@@ -0,0 +1,11 @@
1
+ /**
2
+ * These types have to be exported so users can generate their own types definitions files
3
+ *
4
+ * @remark Do not `import` anything here as it will be unreliable between minor versions of tRPC
5
+ */
6
+ export { mergeRouters } from './core/internals/mergeRouters';
7
+ export * from './core/internals/procedureBuilder';
8
+ export * from './core/internals/utils';
9
+ export type { MiddlewareFunction, MiddlewareBuilder } from './core/middleware';
10
+ export * from './core/procedure';
11
+ //# sourceMappingURL=unstableInternalsExport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unstableInternalsExport.d.ts","sourceRoot":"","sources":["../src/unstableInternalsExport.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,cAAc,mCAAmC,CAAC;AAClD,cAAc,wBAAwB,CAAC;AACvC,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC/E,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var unstableInternalsExport = require('./unstableInternalsExport-bf8496ea.js');
6
+ require('./router-3c22fe56.js');
7
+ require('./TRPCError-ae7b67e8.js');
8
+ require('./index-784ff647.js');
9
+ require('./codes-87f6824b.js');
10
+
11
+
12
+
13
+ exports.createBuilder = unstableInternalsExport.createBuilder;
14
+ exports.mergeRouters = unstableInternalsExport.mergeRouters;
15
+ exports.middlewareMarker = unstableInternalsExport.middlewareMarker;
16
+ exports.unsetMarker = unstableInternalsExport.unsetMarker;
@@ -0,0 +1,5 @@
1
+ export { c as createBuilder, m as mergeRouters, f as middlewareMarker, g as unsetMarker } from './unstableInternalsExport-10cc2bae.mjs';
2
+ import './router-5274e533.mjs';
3
+ import './TRPCError-816ff32e.mjs';
4
+ import './index-f91d720c.mjs';
5
+ import './codes-c924c3db.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "10.43.0",
3
+ "version": "11.0.0-next.91+ca021e61",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -118,6 +118,11 @@
118
118
  "import": "./dist/shared/index.mjs",
119
119
  "require": "./dist/shared/index.js",
120
120
  "default": "./dist/shared/index.js"
121
+ },
122
+ "./unstableInternalsExport": {
123
+ "import": "./dist/unstableInternalsExport.mjs",
124
+ "require": "./dist/unstableInternalsExport.js",
125
+ "default": "./dist/unstableInternalsExport.js"
121
126
  }
122
127
  },
123
128
  "files": [
@@ -131,6 +136,7 @@
131
136
  "observable",
132
137
  "subscription",
133
138
  "shared",
139
+ "unstableInternalsExport",
134
140
  "!**/*.test.*"
135
141
  ],
136
142
  "publishConfig": {
@@ -138,7 +144,7 @@
138
144
  },
139
145
  "devDependencies": {
140
146
  "@fastify/websocket": "^7.1.2",
141
- "@tanstack/react-query": "^4.18.0",
147
+ "@tanstack/react-query": "^5.0.0",
142
148
  "@types/aws-lambda": "^8.10.97",
143
149
  "@types/express": "^4.17.17",
144
150
  "@types/hash-sum": "^1.0.0",
@@ -173,5 +179,5 @@
173
179
  "funding": [
174
180
  "https://trpc.io/sponsor"
175
181
  ],
176
- "gitHead": "0d505fd7415f537bd3e263971686b13a78584167"
182
+ "gitHead": "ca021e61ba8e6eb6e1715f124489cda75a00b5aa"
177
183
  }
@@ -6,9 +6,13 @@ import type {
6
6
  Context as APIGWContext,
7
7
  } from 'aws-lambda';
8
8
  import { TRPCError } from '../..';
9
- import { AnyRouter, inferRouterContext } from '../../core';
10
- import { HTTPRequest, resolveHTTPResponse } from '../../http';
11
- import { HTTPResponse } from '../../http/internals/types';
9
+ import { AnyRouter } from '../../core';
10
+ import {
11
+ HTTPRequest,
12
+ ResolveHTTPRequestOptionsContextFn,
13
+ resolveHTTPResponse,
14
+ } from '../../http';
15
+ import type { HTTPResponse } from '../../http/internals/types';
12
16
  import {
13
17
  APIGatewayEvent,
14
18
  APIGatewayResult,
@@ -92,10 +96,10 @@ export function awsLambdaRequestHandler<
92
96
  return async (event, context) => {
93
97
  const req = lambdaEventToHTTPRequest(event);
94
98
  const path = getPath(event);
95
- const createContext = async function _createContext(): Promise<
96
- inferRouterContext<TRouter>
97
- > {
98
- return await opts.createContext?.({ event, context });
99
+ const createContext: ResolveHTTPRequestOptionsContextFn<TRouter> = async (
100
+ innerOpts,
101
+ ) => {
102
+ return await opts.createContext?.({ event, context, ...innerOpts });
99
103
  };
100
104
 
101
105
  const response = await resolveHTTPResponse({
@@ -7,6 +7,7 @@ import type {
7
7
  } from 'aws-lambda';
8
8
  import type { AnyRouter, inferRouterContext } from '../../core';
9
9
  import { TRPCError } from '../../error/TRPCError';
10
+ import { TRPCRequestInfo } from '../../http';
10
11
  import type { HTTPHeaders, ResponseMetaFn } from '../../http/internals/types';
11
12
  import { OnErrorFunction } from '../../internals/types';
12
13
 
@@ -18,6 +19,7 @@ export type APIGatewayResult =
18
19
  export type CreateAWSLambdaContextOptions<TEvent extends APIGatewayEvent> = {
19
20
  event: TEvent;
20
21
  context: APIGWContext;
22
+ info: TRPCRequestInfo;
21
23
  };
22
24
  export type AWSLambdaCreateContextFn<
23
25
  TRouter extends AnyRouter,
@@ -25,6 +27,7 @@ export type AWSLambdaCreateContextFn<
25
27
  > = ({
26
28
  event,
27
29
  context,
30
+ info,
28
31
  }: CreateAWSLambdaContextOptions<TEvent>) =>
29
32
  | inferRouterContext<TRouter>
30
33
  | Promise<inferRouterContext<TRouter>>;
@@ -1,10 +1,11 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { FastifyReply, FastifyRequest } from 'fastify';
3
- import { AnyRouter, inferRouterContext } from '../../core';
3
+ import { AnyRouter } from '../../core';
4
4
  import {
5
5
  getBatchStreamFormatter,
6
6
  HTTPBaseHandlerOptions,
7
7
  HTTPRequest,
8
+ ResolveHTTPRequestOptionsContextFn,
8
9
  } from '../../http';
9
10
  import { HTTPResponse, ResponseChunk } from '../../http/internals/types';
10
11
  import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
@@ -32,10 +33,13 @@ export async function fastifyRequestHandler<
32
33
  TRequest extends FastifyRequest,
33
34
  TResponse extends FastifyReply,
34
35
  >(opts: FastifyRequestHandlerOptions<TRouter, TRequest, TResponse>) {
35
- const createContext = async function _createContext(): Promise<
36
- inferRouterContext<TRouter>
37
- > {
38
- return opts.createContext?.(opts);
36
+ const createContext: ResolveHTTPRequestOptionsContextFn<TRouter> = async (
37
+ innerOpts,
38
+ ) => {
39
+ return await opts.createContext?.({
40
+ ...opts,
41
+ ...innerOpts,
42
+ });
39
43
  };
40
44
 
41
45
  const query = opts.req.query
@@ -1,5 +1,9 @@
1
1
  import { AnyRouter } from '../../core';
2
- import { getBatchStreamFormatter, HTTPRequest } from '../../http';
2
+ import {
3
+ getBatchStreamFormatter,
4
+ HTTPRequest,
5
+ ResolveHTTPRequestOptionsContextFn,
6
+ } from '../../http';
3
7
  import { HTTPResponse, ResponseChunk } from '../../http/internals/types';
4
8
  import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
5
9
  import { FetchHandlerOptions } from './types';
@@ -15,8 +19,10 @@ export async function fetchRequestHandler<TRouter extends AnyRouter>(
15
19
  ): Promise<Response> {
16
20
  const resHeaders = new Headers();
17
21
 
18
- const createContext = async () => {
19
- return opts.createContext?.({ req: opts.req, resHeaders });
22
+ const createContext: ResolveHTTPRequestOptionsContextFn<TRouter> = async (
23
+ innerOpts,
24
+ ) => {
25
+ return opts.createContext?.({ req: opts.req, resHeaders, ...innerOpts });
20
26
  };
21
27
 
22
28
  const url = new URL(opts.req.url);
@@ -1,9 +1,10 @@
1
1
  import { AnyRouter, inferRouterContext } from '../../core';
2
- import { HTTPBaseHandlerOptions } from '../../http';
2
+ import { HTTPBaseHandlerOptions, TRPCRequestInfo } from '../../http';
3
3
 
4
4
  export type FetchCreateContextFnOptions = {
5
5
  req: Request;
6
6
  resHeaders: Headers;
7
+ info: TRPCRequestInfo;
7
8
  };
8
9
 
9
10
  export type FetchCreateContextFn<TRouter extends AnyRouter> = (
@@ -1,7 +1,10 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
  import { AnyRouter } from '../../core';
3
- import { inferRouterContext } from '../../core/types';
4
- import { getBatchStreamFormatter, HTTPRequest } from '../../http';
3
+ import {
4
+ getBatchStreamFormatter,
5
+ HTTPRequest,
6
+ ResolveHTTPRequestOptionsContextFn,
7
+ } from '../../http';
5
8
  import { HTTPResponse, ResponseChunk } from '../../http/internals/types';
6
9
  import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
7
10
  import { nodeHTTPJSONContentTypeHandler } from './content-type/json';
@@ -24,8 +27,16 @@ export async function nodeHTTPRequestHandler<
24
27
  return handleViaMiddleware(opts.req, opts.res, async (err) => {
25
28
  if (err) throw err;
26
29
 
27
- const createContext = async (): Promise<inferRouterContext<TRouter>> => {
28
- return await opts.createContext?.(opts);
30
+ //
31
+ // Build tRPC dependencies
32
+
33
+ const createContext: ResolveHTTPRequestOptionsContextFn<TRouter> = async (
34
+ innerOpts,
35
+ ) => {
36
+ return await opts.createContext?.({
37
+ ...opts,
38
+ ...innerOpts,
39
+ });
29
40
  };
30
41
 
31
42
  const query = opts.req.query
@@ -1,6 +1,6 @@
1
1
  import { IncomingMessage, ServerResponse } from 'http';
2
2
  import { AnyRouter, inferRouterContext } from '../../core';
3
- import { HTTPBaseHandlerOptions } from '../../http';
3
+ import { HTTPBaseHandlerOptions, TRPCRequestInfo } from '../../http';
4
4
  import { MaybePromise } from '../../types';
5
5
  import { NodeHTTPContentTypeHandler } from './internals/contentType';
6
6
 
@@ -93,6 +93,7 @@ export type NodeHTTPRequestHandlerOptions<
93
93
  export type NodeHTTPCreateContextFnOptions<TRequest, TResponse> = {
94
94
  req: TRequest;
95
95
  res: TResponse;
96
+ info: TRPCRequestInfo;
96
97
  };
97
98
  export type NodeHTTPCreateContextFn<
98
99
  TRouter extends AnyRouter,
@@ -19,14 +19,9 @@ export function createHTTPHandler<TRouter extends AnyRouter>(
19
19
  opts: CreateHTTPHandlerOptions<TRouter>,
20
20
  ) {
21
21
  return async (req: http.IncomingMessage, res: http.ServerResponse) => {
22
- // if no hostname, set a dummy one
23
- const href = req.url!.startsWith('/')
24
- ? `http://127.0.0.1${req.url}`
25
- : req.url!;
26
-
27
- // get procedure path and remove the leading slash
28
- // /procedure -> procedure
29
- const path = new URL(href).pathname.slice(1);
22
+ // Get procedure path and remove the leading slash, `/procedure -> procedure`
23
+ // Use dummy hostname if one is not provided.
24
+ const path = new URL(req.url!, 'http://127.0.0.1').pathname.slice(1);
30
25
 
31
26
  await nodeHTTPRequestHandler({
32
27
  ...opts,
@@ -41,18 +36,5 @@ export function createHTTPServer<TRouter extends AnyRouter>(
41
36
  opts: CreateHTTPHandlerOptions<TRouter>,
42
37
  ) {
43
38
  const handler = createHTTPHandler(opts);
44
- const server = http.createServer((req, res) => handler(req, res));
45
-
46
- return {
47
- server,
48
- listen: (port?: number, hostname?: string) => {
49
- server.listen(port, hostname);
50
- const actualPort =
51
- port === 0 ? ((server.address() as any).port as number) : port;
52
-
53
- return {
54
- port: actualPort,
55
- };
56
- },
57
- };
39
+ return http.createServer((req, res) => handler(req, res));
58
40
  }
@@ -1,22 +1,111 @@
1
1
  import { IncomingMessage } from 'http';
2
2
  import ws from 'ws';
3
- import { AnyRouter, callProcedure, inferRouterContext } from '../core';
3
+ import {
4
+ AnyRouter,
5
+ callProcedure,
6
+ inferRouterContext,
7
+ ProcedureType,
8
+ } from '../core';
4
9
  import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
5
10
  import { BaseHandlerOptions } from '../internals/types';
6
11
  import { isObservable, Unsubscribable } from '../observable';
7
12
  import {
8
13
  JSONRPC2,
9
- parseTRPCMessage,
10
14
  TRPCClientOutgoingMessage,
11
15
  TRPCReconnectNotification,
12
16
  TRPCResponseMessage,
13
17
  } from '../rpc';
14
18
  import { getErrorShape } from '../shared/getErrorShape';
15
19
  import { transformTRPCResponse } from '../shared/transformTRPCResponse';
16
- import {
17
- NodeHTTPCreateContextFnOptions,
18
- NodeHTTPCreateContextOption,
19
- } from './node-http';
20
+ import { CombinedDataTransformer } from '../transformer';
21
+ import { MaybePromise } from '../types';
22
+ import { NodeHTTPCreateContextFnOptions } from './node-http';
23
+
24
+ /* istanbul ignore next -- @preserve */
25
+ function assertIsObject(obj: unknown): asserts obj is Record<string, unknown> {
26
+ if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
27
+ throw new Error('Not an object');
28
+ }
29
+ }
30
+ /* istanbul ignore next -- @preserve */
31
+ function assertIsProcedureType(obj: unknown): asserts obj is ProcedureType {
32
+ if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
33
+ throw new Error('Invalid procedure type');
34
+ }
35
+ }
36
+ /* istanbul ignore next -- @preserve */
37
+ function assertIsRequestId(
38
+ obj: unknown,
39
+ ): asserts obj is number | string | null {
40
+ if (
41
+ obj !== null &&
42
+ typeof obj === 'number' &&
43
+ isNaN(obj) &&
44
+ typeof obj !== 'string'
45
+ ) {
46
+ throw new Error('Invalid request id');
47
+ }
48
+ }
49
+ /* istanbul ignore next -- @preserve */
50
+ function assertIsString(obj: unknown): asserts obj is string {
51
+ if (typeof obj !== 'string') {
52
+ throw new Error('Invalid string');
53
+ }
54
+ }
55
+ /* istanbul ignore next -- @preserve */
56
+ function assertIsJSONRPC2OrUndefined(
57
+ obj: unknown,
58
+ ): asserts obj is '2.0' | undefined {
59
+ if (typeof obj !== 'undefined' && obj !== '2.0') {
60
+ throw new Error('Must be JSONRPC 2.0');
61
+ }
62
+ }
63
+ export function parseMessage(
64
+ obj: unknown,
65
+ transformer: CombinedDataTransformer,
66
+ ): TRPCClientOutgoingMessage {
67
+ assertIsObject(obj);
68
+ const { method, params, id, jsonrpc } = obj;
69
+ assertIsRequestId(id);
70
+ assertIsJSONRPC2OrUndefined(jsonrpc);
71
+ if (method === 'subscription.stop') {
72
+ return {
73
+ id,
74
+ jsonrpc,
75
+ method,
76
+ };
77
+ }
78
+ assertIsProcedureType(method);
79
+ assertIsObject(params);
80
+
81
+ const { input: rawInput, path } = params;
82
+ assertIsString(path);
83
+ const input = transformer.input.deserialize(rawInput);
84
+ return {
85
+ id,
86
+ jsonrpc,
87
+ method,
88
+ params: {
89
+ input,
90
+ path,
91
+ },
92
+ };
93
+ }
94
+
95
+ /**
96
+ * @public
97
+ */
98
+ export type CreateWSSContextFnOptions = Omit<
99
+ NodeHTTPCreateContextFnOptions<IncomingMessage, ws>,
100
+ 'info'
101
+ >;
102
+
103
+ /**
104
+ * @public
105
+ */
106
+ export type CreateWSSContextFn<TRouter extends AnyRouter> = (
107
+ opts: CreateWSSContextFnOptions,
108
+ ) => MaybePromise<inferRouterContext<TRouter>>;
20
109
 
21
110
  /**
22
111
  * Web socket server handler
@@ -25,16 +114,23 @@ export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<
25
114
  TRouter,
26
115
  IncomingMessage
27
116
  > &
28
- NodeHTTPCreateContextOption<TRouter, IncomingMessage, ws> & {
117
+ (object extends inferRouterContext<TRouter>
118
+ ? {
119
+ /**
120
+ * @link https://trpc.io/docs/context
121
+ **/
122
+ createContext?: CreateWSSContextFn<TRouter>;
123
+ }
124
+ : {
125
+ /**
126
+ * @link https://trpc.io/docs/context
127
+ **/
128
+ createContext: CreateWSSContextFn<TRouter>;
129
+ }) & {
29
130
  wss: ws.Server;
30
131
  process?: NodeJS.Process;
31
132
  };
32
133
 
33
- export type CreateWSSContextFnOptions = NodeHTTPCreateContextFnOptions<
34
- IncomingMessage,
35
- ws
36
- >;
37
-
38
134
  export function applyWSSHandler<TRouter extends AnyRouter>(
39
135
  opts: WSSHandlerOptions<TRouter>,
40
136
  ) {
@@ -95,7 +191,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
95
191
  const result = await callProcedure({
96
192
  procedures: router._def.procedures,
97
193
  path,
98
- rawInput: input,
194
+ getRawInput: async () => input,
99
195
  ctx,
100
196
  type,
101
197
  });
@@ -208,7 +304,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
208
304
  const msgJSON: unknown = JSON.parse(message.toString());
209
305
  const msgs: unknown[] = Array.isArray(msgJSON) ? msgJSON : [msgJSON];
210
306
  const promises = msgs
211
- .map((raw) => parseTRPCMessage(raw, transformer))
307
+ .map((raw) => parseMessage(raw, transformer))
212
308
  .map(handleRequest);
213
309
  await Promise.all(promises);
214
310
  } catch (cause) {
package/src/core/index.ts CHANGED
@@ -12,6 +12,7 @@ export type {
12
12
  AnyQueryProcedure,
13
13
  AnyMutationProcedure,
14
14
  AnySubscriptionProcedure,
15
+ AnyProcedureParams,
15
16
  ProcedureParams,
16
17
  ProcedureArgs,
17
18
  ProcedureOptions,
@@ -0,0 +1,11 @@
1
+ import { AnyRootConfig } from './config';
2
+
3
+ export type AnyProcedureBuilderParams = {
4
+ _config: AnyRootConfig;
5
+ _meta: unknown;
6
+ _ctx_out: unknown;
7
+ _input_in: unknown;
8
+ _input_out: unknown;
9
+ _output_in: unknown;
10
+ _output_out: unknown;
11
+ };
@@ -41,16 +41,3 @@ export function getParseFn<TType>(procedureParser: Parser): ParseFn<TType> {
41
41
 
42
42
  throw new Error('Could not find a validator fn');
43
43
  }
44
-
45
- /**
46
- * @deprecated only for backwards compat
47
- * @internal
48
- */
49
- export function getParseFnOrPassThrough<TType>(
50
- procedureParser: Parser | undefined,
51
- ): ParseFn<TType> {
52
- if (!procedureParser) {
53
- return (v) => v as TType;
54
- }
55
- return getParseFn(procedureParser);
56
- }
@@ -30,10 +30,6 @@ export type MergeRouters<
30
30
  router: true;
31
31
  procedures: Head['_def']['procedures'] & TRouterDef['procedures'];
32
32
  record: Head['_def']['record'] & TRouterDef['record'];
33
- queries: Head['_def']['queries'] & TRouterDef['queries'];
34
- mutations: Head['_def']['mutations'] & TRouterDef['mutations'];
35
- subscriptions: Head['_def']['subscriptions'] &
36
- TRouterDef['subscriptions'];
37
33
  }
38
34
  >
39
35
  : Router<TRouterDef> & TRouterDef['record'];