@trpc/server 10.13.2 → 10.14.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) <year> <author>
3
+ Copyright (c) 2023 Alex Johansson
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ export * from '../../dist/adapters/zodFileSchema';
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/adapters/zodFileSchema');
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-40bc6a18.js');
5
+ var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-7ebe1e44.js');
6
6
  require('../resolveHTTPResponse-cbfdc43d.js');
7
7
  require('../config-bd759464.js');
8
8
  require('../TRPCError-4cbab0d0.js');
@@ -1,4 +1,4 @@
1
- import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-4314fa32.mjs';
1
+ import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-8c27f394.mjs';
2
2
  import '../resolveHTTPResponse-8e9dd846.mjs';
3
3
  import '../config-fb5f49a1.mjs';
4
4
  import '../TRPCError-e7333eae.mjs';
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var TRPCError = require('../TRPCError-4cbab0d0.js');
6
- var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-40bc6a18.js');
6
+ var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-7ebe1e44.js');
7
7
  require('../resolveHTTPResponse-cbfdc43d.js');
8
8
  require('../config-bd759464.js');
9
9
  require('../codes-e0ea0f4c.js');
@@ -1,5 +1,5 @@
1
1
  import { T as TRPCError } from '../TRPCError-e7333eae.mjs';
2
- import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-4314fa32.mjs';
2
+ import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-8c27f394.mjs';
3
3
  import '../resolveHTTPResponse-8e9dd846.mjs';
4
4
  import '../config-fb5f49a1.mjs';
5
5
  import '../codes-52c11119.mjs';
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var nodeHTTPRequestHandler = require('../../nodeHTTPRequestHandler-40bc6a18.js');
5
+ var nodeHTTPRequestHandler = require('../../nodeHTTPRequestHandler-7ebe1e44.js');
6
6
  require('../../resolveHTTPResponse-cbfdc43d.js');
7
7
  require('../../config-bd759464.js');
8
8
  require('../../TRPCError-4cbab0d0.js');
@@ -1,4 +1,4 @@
1
- export { n as nodeHTTPRequestHandler } from '../../nodeHTTPRequestHandler-4314fa32.mjs';
1
+ export { n as nodeHTTPRequestHandler } from '../../nodeHTTPRequestHandler-8c27f394.mjs';
2
2
  import '../../resolveHTTPResponse-8e9dd846.mjs';
3
3
  import '../../config-fb5f49a1.mjs';
4
4
  import '../../TRPCError-e7333eae.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"nodeHTTPRequestHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/nodeHTTPRequestHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAKvC,OAAO,EACL,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB,aAAK,6BAA6B,CAChC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,IAChC;IACF,GAAG,EAAE,QAAQ,CAAC;IACd,GAAG,EAAE,SAAS,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,sBAAsB,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAEzD,wBAAsB,sBAAsB,CAC1C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,EAClC,IAAI,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,iBA8ClE"}
1
+ {"version":3,"file":"nodeHTTPRequestHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/nodeHTTPRequestHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAKvC,OAAO,EACL,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB,aAAK,6BAA6B,CAChC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,IAChC;IACF,GAAG,EAAE,QAAQ,CAAC;IACd,GAAG,EAAE,SAAS,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,sBAAsB,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAEzD,wBAAsB,sBAAsB,CAC1C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,EAClC,IAAI,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,iBAqElE"}
@@ -22,7 +22,30 @@ export declare type NodeHTTPCreateContextOption<TRouter extends AnyRouter, TRequ
22
22
  **/
23
23
  createContext: NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>;
24
24
  };
25
+ /**
26
+ * @internal
27
+ */
28
+ interface ConnectMiddleware<TRequest extends NodeHTTPRequest = NodeHTTPRequest, TResponse extends NodeHTTPResponse = NodeHTTPResponse> {
29
+ (req: TRequest, res: TResponse, next: (err?: any) => any): void;
30
+ }
25
31
  export declare type NodeHTTPHandlerOptions<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse> = HTTPBaseHandlerOptions<TRouter, TRequest> & {
32
+ /**
33
+ * By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
34
+ *
35
+ * This can be used to handle them manually or via the `cors` npm package: https://www.npmjs.com/package/cors
36
+ *
37
+ * ```ts
38
+ * import cors from 'cors'
39
+ *
40
+ * nodeHTTPRequestHandler({
41
+ * cors: cors()
42
+ * })
43
+ * ```
44
+ *
45
+ * You can also use it for other needs which a connect/node.js compatible middleware can solve,
46
+ * though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
47
+ */
48
+ middleware?: ConnectMiddleware;
26
49
  maxBodySize?: number;
27
50
  } & NodeHTTPCreateContextOption<TRouter, TRequest, TResponse>;
28
51
  export declare type NodeHTTPCreateContextFnOptions<TRequest, TResponse> = {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/types.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,UAAU,QAAQ;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,QAAQ,GAAG,QAAQ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CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GAAG,2BAA2B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAE9D,oBAAY,8BAA8B,CAAC,QAAQ,EAAE,SAAS,IAAI;IAChE,GAAG,EAAE,QAAQ,CAAC;IACd,GAAG,EAAE,SAAS,CAAC;CAChB,CAAC;AACF,oBAAY,uBAAuB,CACjC,OAAO,SAAS,SAAS,EACzB,QAAQ,EACR,SAAS,IACP,CACF,IAAI,EAAE,8BAA8B,CAAC,QAAQ,EAAE,SAAS,CAAC,KACtD,YAAY,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC"}
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var http = require('node:http');
6
- var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-40bc6a18.js');
6
+ var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-7ebe1e44.js');
7
7
  require('../resolveHTTPResponse-cbfdc43d.js');
8
8
  require('../config-bd759464.js');
9
9
  require('../TRPCError-4cbab0d0.js');
@@ -1,5 +1,5 @@
1
1
  import http from 'node:http';
2
- import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-4314fa32.mjs';
2
+ import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-8c27f394.mjs';
3
3
  import '../resolveHTTPResponse-8e9dd846.mjs';
4
4
  import '../config-fb5f49a1.mjs';
5
5
  import '../TRPCError-e7333eae.mjs';
@@ -1,4 +1,4 @@
1
- export type { AnyRouter, ProcedureRecord, ProcedureRouterRecord, CreateRouterInner, } from './router';
1
+ export type { AnyRouter, ProcedureRecord, ProcedureRouterRecord, CreateRouterInner, Router, } from './router';
2
2
  export { callProcedure } from './router';
3
3
  export type { Procedure, AnyProcedure, AnyQueryProcedure, AnyMutationProcedure, AnySubscriptionProcedure, ProcedureParams, ProcedureArgs, ProcedureOptions, } from './procedure';
4
4
  export type { inferParser } from './parser';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,SAAS,EACT,eAAe,EACf,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,YAAY,EACV,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,EACxB,eAAe,EACf,aAAa,EACb,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAE7E,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,SAAS,EACT,eAAe,EACf,qBAAqB,EACrB,iBAAiB,EACjB,MAAM,GACP,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,YAAY,EACV,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,EACxB,eAAe,EACf,aAAa,EACb,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAE7E,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,cAAc,SAAS,CAAC"}
@@ -0,0 +1,90 @@
1
+ import { r as resolveHTTPResponse } from './resolveHTTPResponse-39258d19.js';
2
+ import { T as TRPCError } from './TRPCError-ed16b3b7.js';
3
+
4
+ async function getPostBody(opts) {
5
+ const { req, maxBodySize = Infinity } = opts;
6
+ return new Promise((resolve) => {
7
+ if ('body' in req) {
8
+ resolve({ ok: true, data: req.body });
9
+ return;
10
+ }
11
+ let body = '';
12
+ let hasBody = false;
13
+ req.on('data', function (data) {
14
+ body += data;
15
+ hasBody = true;
16
+ if (body.length > maxBodySize) {
17
+ resolve({
18
+ ok: false,
19
+ error: new TRPCError({ code: 'PAYLOAD_TOO_LARGE' }),
20
+ });
21
+ req.socket.destroy();
22
+ }
23
+ });
24
+ req.on('end', () => {
25
+ resolve({
26
+ ok: true,
27
+ data: hasBody ? body : undefined,
28
+ });
29
+ });
30
+ });
31
+ }
32
+
33
+ async function nodeHTTPRequestHandler(opts) {
34
+ const handleViaMiddleware = opts.middleware ??
35
+ ((_req, _res, next) => {
36
+ return next();
37
+ });
38
+ return handleViaMiddleware(opts.req, opts.res, async (err) => {
39
+ if (err) {
40
+ throw err;
41
+ }
42
+ //
43
+ // Build tRPC dependencies
44
+ async function createContext() {
45
+ return await opts.createContext?.(opts);
46
+ }
47
+ const bodyResult = await getPostBody(opts);
48
+ const query = opts.req.query
49
+ ? new URLSearchParams(opts.req.query)
50
+ : new URLSearchParams(opts.req.url.split('?')[1]);
51
+ const req = {
52
+ method: opts.req.method,
53
+ headers: opts.req.headers,
54
+ query,
55
+ body: bodyResult.ok ? bodyResult.data : undefined,
56
+ };
57
+ //
58
+ // Invoke tRPC
59
+ const result = await resolveHTTPResponse({
60
+ batching: opts.batching,
61
+ responseMeta: opts.responseMeta,
62
+ path: opts.path,
63
+ createContext,
64
+ router: opts.router,
65
+ req,
66
+ error: bodyResult.ok ? null : bodyResult.error,
67
+ onError(o) {
68
+ opts?.onError?.({
69
+ ...o,
70
+ req: opts.req,
71
+ });
72
+ },
73
+ });
74
+ //
75
+ // Handle result
76
+ const { res } = opts;
77
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
78
+ res.statusCode = result.status;
79
+ }
80
+ for (const [key, value] of Object.entries(result.headers ?? {})) {
81
+ if (typeof value === 'undefined') {
82
+ continue;
83
+ }
84
+ res.setHeader(key, value);
85
+ }
86
+ res.end(result.body);
87
+ });
88
+ }
89
+
90
+ export { nodeHTTPRequestHandler as n };
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+
3
+ var resolveHTTPResponse = require('./resolveHTTPResponse-cbfdc43d.js');
4
+ var TRPCError = require('./TRPCError-4cbab0d0.js');
5
+
6
+ async function getPostBody(opts) {
7
+ const { req , maxBodySize =Infinity } = opts;
8
+ return new Promise((resolve)=>{
9
+ if ('body' in req) {
10
+ resolve({
11
+ ok: true,
12
+ data: req.body
13
+ });
14
+ return;
15
+ }
16
+ let body = '';
17
+ let hasBody = false;
18
+ req.on('data', function(data) {
19
+ body += data;
20
+ hasBody = true;
21
+ if (body.length > maxBodySize) {
22
+ resolve({
23
+ ok: false,
24
+ error: new TRPCError.TRPCError({
25
+ code: 'PAYLOAD_TOO_LARGE'
26
+ })
27
+ });
28
+ req.socket.destroy();
29
+ }
30
+ });
31
+ req.on('end', ()=>{
32
+ resolve({
33
+ ok: true,
34
+ data: hasBody ? body : undefined
35
+ });
36
+ });
37
+ });
38
+ }
39
+
40
+ async function nodeHTTPRequestHandler(opts) {
41
+ const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>{
42
+ return next();
43
+ });
44
+ return handleViaMiddleware(opts.req, opts.res, async (err)=>{
45
+ if (err) {
46
+ throw err;
47
+ }
48
+ //
49
+ // Build tRPC dependencies
50
+ async function createContext() {
51
+ return await opts.createContext?.(opts);
52
+ }
53
+ const bodyResult = await getPostBody(opts);
54
+ const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
55
+ const req = {
56
+ method: opts.req.method,
57
+ headers: opts.req.headers,
58
+ query,
59
+ body: bodyResult.ok ? bodyResult.data : undefined
60
+ };
61
+ //
62
+ // Invoke tRPC
63
+ const result = await resolveHTTPResponse.resolveHTTPResponse({
64
+ batching: opts.batching,
65
+ responseMeta: opts.responseMeta,
66
+ path: opts.path,
67
+ createContext,
68
+ router: opts.router,
69
+ req,
70
+ error: bodyResult.ok ? null : bodyResult.error,
71
+ onError (o) {
72
+ opts?.onError?.({
73
+ ...o,
74
+ req: opts.req
75
+ });
76
+ }
77
+ });
78
+ //
79
+ // Handle result
80
+ const { res } = opts;
81
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
82
+ res.statusCode = result.status;
83
+ }
84
+ for (const [key, value] of Object.entries(result.headers ?? {})){
85
+ if (typeof value === 'undefined') {
86
+ continue;
87
+ }
88
+ res.setHeader(key, value);
89
+ }
90
+ res.end(result.body);
91
+ });
92
+ }
93
+
94
+ exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler;
@@ -0,0 +1,92 @@
1
+ import { r as resolveHTTPResponse } from './resolveHTTPResponse-8e9dd846.mjs';
2
+ import { T as TRPCError } from './TRPCError-e7333eae.mjs';
3
+
4
+ async function getPostBody(opts) {
5
+ const { req , maxBodySize =Infinity } = opts;
6
+ return new Promise((resolve)=>{
7
+ if ('body' in req) {
8
+ resolve({
9
+ ok: true,
10
+ data: req.body
11
+ });
12
+ return;
13
+ }
14
+ let body = '';
15
+ let hasBody = false;
16
+ req.on('data', function(data) {
17
+ body += data;
18
+ hasBody = true;
19
+ if (body.length > maxBodySize) {
20
+ resolve({
21
+ ok: false,
22
+ error: new TRPCError({
23
+ code: 'PAYLOAD_TOO_LARGE'
24
+ })
25
+ });
26
+ req.socket.destroy();
27
+ }
28
+ });
29
+ req.on('end', ()=>{
30
+ resolve({
31
+ ok: true,
32
+ data: hasBody ? body : undefined
33
+ });
34
+ });
35
+ });
36
+ }
37
+
38
+ async function nodeHTTPRequestHandler(opts) {
39
+ const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>{
40
+ return next();
41
+ });
42
+ return handleViaMiddleware(opts.req, opts.res, async (err)=>{
43
+ if (err) {
44
+ throw err;
45
+ }
46
+ //
47
+ // Build tRPC dependencies
48
+ async function createContext() {
49
+ return await opts.createContext?.(opts);
50
+ }
51
+ const bodyResult = await getPostBody(opts);
52
+ const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
53
+ const req = {
54
+ method: opts.req.method,
55
+ headers: opts.req.headers,
56
+ query,
57
+ body: bodyResult.ok ? bodyResult.data : undefined
58
+ };
59
+ //
60
+ // Invoke tRPC
61
+ const result = await resolveHTTPResponse({
62
+ batching: opts.batching,
63
+ responseMeta: opts.responseMeta,
64
+ path: opts.path,
65
+ createContext,
66
+ router: opts.router,
67
+ req,
68
+ error: bodyResult.ok ? null : bodyResult.error,
69
+ onError (o) {
70
+ opts?.onError?.({
71
+ ...o,
72
+ req: opts.req
73
+ });
74
+ }
75
+ });
76
+ //
77
+ // Handle result
78
+ const { res } = opts;
79
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
80
+ res.statusCode = result.status;
81
+ }
82
+ for (const [key, value] of Object.entries(result.headers ?? {})){
83
+ if (typeof value === 'undefined') {
84
+ continue;
85
+ }
86
+ res.setHeader(key, value);
87
+ }
88
+ res.end(result.body);
89
+ });
90
+ }
91
+
92
+ export { nodeHTTPRequestHandler as n };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "10.13.2",
3
+ "version": "10.14.0",
4
4
  "description": "tRPC Server",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -156,5 +156,5 @@
156
156
  "yup": "^1.0.0",
157
157
  "zod": "^3.0.0"
158
158
  },
159
- "gitHead": "5f4fb9641e1f43125f977b3f84268ac7e5884715"
159
+ "gitHead": "452b679c1ba5ad52dc5fd8a1779cdb98544cdd30"
160
160
  }
@@ -25,49 +25,72 @@ export async function nodeHTTPRequestHandler<
25
25
  TRequest extends NodeHTTPRequest,
26
26
  TResponse extends NodeHTTPResponse,
27
27
  >(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>) {
28
- const createContext = async function _createContext(): Promise<
29
- inferRouterContext<TRouter>
30
- > {
31
- return await opts.createContext?.(opts);
32
- };
33
- const { path, router } = opts;
28
+ const handleViaMiddleware =
29
+ opts.middleware ??
30
+ ((_req, _res, next) => {
31
+ return next();
32
+ });
34
33
 
35
- const bodyResult = await getPostBody(opts);
34
+ return handleViaMiddleware(opts.req, opts.res, async (err) => {
35
+ if (err) {
36
+ throw err;
37
+ }
36
38
 
37
- const query = opts.req.query
38
- ? new URLSearchParams(opts.req.query as any)
39
- : new URLSearchParams(opts.req.url!.split('?')[1]);
40
- const req: HTTPRequest = {
41
- method: opts.req.method!,
42
- headers: opts.req.headers,
43
- query,
44
- body: bodyResult.ok ? bodyResult.data : undefined,
45
- };
46
- const result = await resolveHTTPResponse({
47
- batching: opts.batching,
48
- responseMeta: opts.responseMeta,
49
- path,
50
- createContext,
51
- router,
52
- req,
53
- error: bodyResult.ok ? null : bodyResult.error,
54
- onError(o) {
55
- opts?.onError?.({
56
- ...o,
57
- req: opts.req,
58
- });
59
- },
60
- });
39
+ //
40
+ // Build tRPC dependencies
61
41
 
62
- const { res } = opts;
63
- if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
64
- res.statusCode = result.status;
65
- }
66
- for (const [key, value] of Object.entries(result.headers ?? {})) {
67
- if (typeof value === 'undefined') {
68
- continue;
42
+ async function createContext(): Promise<inferRouterContext<TRouter>> {
43
+ return await opts.createContext?.(opts);
69
44
  }
70
- res.setHeader(key, value);
71
- }
72
- res.end(result.body);
45
+
46
+ const bodyResult = await getPostBody(opts);
47
+
48
+ const query = opts.req.query
49
+ ? new URLSearchParams(opts.req.query as any)
50
+ : new URLSearchParams(opts.req.url!.split('?')[1]);
51
+
52
+ const req: HTTPRequest = {
53
+ method: opts.req.method!,
54
+ headers: opts.req.headers,
55
+ query,
56
+ body: bodyResult.ok ? bodyResult.data : undefined,
57
+ };
58
+
59
+ //
60
+ // Invoke tRPC
61
+
62
+ const result = await resolveHTTPResponse({
63
+ batching: opts.batching,
64
+ responseMeta: opts.responseMeta,
65
+ path: opts.path,
66
+ createContext,
67
+ router: opts.router,
68
+ req,
69
+ error: bodyResult.ok ? null : bodyResult.error,
70
+ onError(o) {
71
+ opts?.onError?.({
72
+ ...o,
73
+ req: opts.req,
74
+ });
75
+ },
76
+ });
77
+
78
+ //
79
+ // Handle result
80
+
81
+ const { res } = opts;
82
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
83
+ res.statusCode = result.status;
84
+ }
85
+
86
+ for (const [key, value] of Object.entries(result.headers ?? {})) {
87
+ if (typeof value === 'undefined') {
88
+ continue;
89
+ }
90
+
91
+ res.setHeader(key, value);
92
+ }
93
+
94
+ res.end(result.body);
95
+ });
73
96
  }
@@ -31,11 +31,38 @@ export type NodeHTTPCreateContextOption<
31
31
  createContext: NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>;
32
32
  };
33
33
 
34
+ /**
35
+ * @internal
36
+ */
37
+ interface ConnectMiddleware<
38
+ TRequest extends NodeHTTPRequest = NodeHTTPRequest,
39
+ TResponse extends NodeHTTPResponse = NodeHTTPResponse,
40
+ > {
41
+ (req: TRequest, res: TResponse, next: (err?: any) => any): void;
42
+ }
43
+
34
44
  export type NodeHTTPHandlerOptions<
35
45
  TRouter extends AnyRouter,
36
46
  TRequest extends NodeHTTPRequest,
37
47
  TResponse extends NodeHTTPResponse,
38
48
  > = HTTPBaseHandlerOptions<TRouter, TRequest> & {
49
+ /**
50
+ * By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
51
+ *
52
+ * This can be used to handle them manually or via the `cors` npm package: https://www.npmjs.com/package/cors
53
+ *
54
+ * ```ts
55
+ * import cors from 'cors'
56
+ *
57
+ * nodeHTTPRequestHandler({
58
+ * cors: cors()
59
+ * })
60
+ * ```
61
+ *
62
+ * You can also use it for other needs which a connect/node.js compatible middleware can solve,
63
+ * though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
64
+ */
65
+ middleware?: ConnectMiddleware;
39
66
  maxBodySize?: number;
40
67
  } & NodeHTTPCreateContextOption<TRouter, TRequest, TResponse>;
41
68
 
package/src/core/index.ts CHANGED
@@ -3,6 +3,7 @@ export type {
3
3
  ProcedureRecord,
4
4
  ProcedureRouterRecord,
5
5
  CreateRouterInner,
6
+ Router,
6
7
  } from './router';
7
8
  export { callProcedure } from './router';
8
9
  export type {
@@ -1,81 +0,0 @@
1
- 'use strict';
2
-
3
- var resolveHTTPResponse = require('./resolveHTTPResponse-cbfdc43d.js');
4
- var TRPCError = require('./TRPCError-4cbab0d0.js');
5
-
6
- async function getPostBody(opts) {
7
- const { req , maxBodySize =Infinity } = opts;
8
- return new Promise((resolve)=>{
9
- if ('body' in req) {
10
- resolve({
11
- ok: true,
12
- data: req.body
13
- });
14
- return;
15
- }
16
- let body = '';
17
- let hasBody = false;
18
- req.on('data', function(data) {
19
- body += data;
20
- hasBody = true;
21
- if (body.length > maxBodySize) {
22
- resolve({
23
- ok: false,
24
- error: new TRPCError.TRPCError({
25
- code: 'PAYLOAD_TOO_LARGE'
26
- })
27
- });
28
- req.socket.destroy();
29
- }
30
- });
31
- req.on('end', ()=>{
32
- resolve({
33
- ok: true,
34
- data: hasBody ? body : undefined
35
- });
36
- });
37
- });
38
- }
39
-
40
- async function nodeHTTPRequestHandler(opts) {
41
- const createContext = async function _createContext() {
42
- return await opts.createContext?.(opts);
43
- };
44
- const { path , router } = opts;
45
- const bodyResult = await getPostBody(opts);
46
- const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
47
- const req = {
48
- method: opts.req.method,
49
- headers: opts.req.headers,
50
- query,
51
- body: bodyResult.ok ? bodyResult.data : undefined
52
- };
53
- const result = await resolveHTTPResponse.resolveHTTPResponse({
54
- batching: opts.batching,
55
- responseMeta: opts.responseMeta,
56
- path,
57
- createContext,
58
- router,
59
- req,
60
- error: bodyResult.ok ? null : bodyResult.error,
61
- onError (o) {
62
- opts?.onError?.({
63
- ...o,
64
- req: opts.req
65
- });
66
- }
67
- });
68
- const { res } = opts;
69
- if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
70
- res.statusCode = result.status;
71
- }
72
- for (const [key, value] of Object.entries(result.headers ?? {})){
73
- if (typeof value === 'undefined') {
74
- continue;
75
- }
76
- res.setHeader(key, value);
77
- }
78
- res.end(result.body);
79
- }
80
-
81
- exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler;
@@ -1,79 +0,0 @@
1
- import { r as resolveHTTPResponse } from './resolveHTTPResponse-8e9dd846.mjs';
2
- import { T as TRPCError } from './TRPCError-e7333eae.mjs';
3
-
4
- async function getPostBody(opts) {
5
- const { req , maxBodySize =Infinity } = opts;
6
- return new Promise((resolve)=>{
7
- if ('body' in req) {
8
- resolve({
9
- ok: true,
10
- data: req.body
11
- });
12
- return;
13
- }
14
- let body = '';
15
- let hasBody = false;
16
- req.on('data', function(data) {
17
- body += data;
18
- hasBody = true;
19
- if (body.length > maxBodySize) {
20
- resolve({
21
- ok: false,
22
- error: new TRPCError({
23
- code: 'PAYLOAD_TOO_LARGE'
24
- })
25
- });
26
- req.socket.destroy();
27
- }
28
- });
29
- req.on('end', ()=>{
30
- resolve({
31
- ok: true,
32
- data: hasBody ? body : undefined
33
- });
34
- });
35
- });
36
- }
37
-
38
- async function nodeHTTPRequestHandler(opts) {
39
- const createContext = async function _createContext() {
40
- return await opts.createContext?.(opts);
41
- };
42
- const { path , router } = opts;
43
- const bodyResult = await getPostBody(opts);
44
- const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
45
- const req = {
46
- method: opts.req.method,
47
- headers: opts.req.headers,
48
- query,
49
- body: bodyResult.ok ? bodyResult.data : undefined
50
- };
51
- const result = await resolveHTTPResponse({
52
- batching: opts.batching,
53
- responseMeta: opts.responseMeta,
54
- path,
55
- createContext,
56
- router,
57
- req,
58
- error: bodyResult.ok ? null : bodyResult.error,
59
- onError (o) {
60
- opts?.onError?.({
61
- ...o,
62
- req: opts.req
63
- });
64
- }
65
- });
66
- const { res } = opts;
67
- if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
68
- res.statusCode = result.status;
69
- }
70
- for (const [key, value] of Object.entries(result.headers ?? {})){
71
- if (typeof value === 'undefined') {
72
- continue;
73
- }
74
- res.setHeader(key, value);
75
- }
76
- res.end(result.body);
77
- }
78
-
79
- export { nodeHTTPRequestHandler as n };
@@ -1,76 +0,0 @@
1
- import { r as resolveHTTPResponse } from './resolveHTTPResponse-39258d19.js';
2
- import { T as TRPCError } from './TRPCError-ed16b3b7.js';
3
-
4
- async function getPostBody(opts) {
5
- const { req, maxBodySize = Infinity } = opts;
6
- return new Promise((resolve) => {
7
- if ('body' in req) {
8
- resolve({ ok: true, data: req.body });
9
- return;
10
- }
11
- let body = '';
12
- let hasBody = false;
13
- req.on('data', function (data) {
14
- body += data;
15
- hasBody = true;
16
- if (body.length > maxBodySize) {
17
- resolve({
18
- ok: false,
19
- error: new TRPCError({ code: 'PAYLOAD_TOO_LARGE' }),
20
- });
21
- req.socket.destroy();
22
- }
23
- });
24
- req.on('end', () => {
25
- resolve({
26
- ok: true,
27
- data: hasBody ? body : undefined,
28
- });
29
- });
30
- });
31
- }
32
-
33
- async function nodeHTTPRequestHandler(opts) {
34
- const createContext = async function _createContext() {
35
- return await opts.createContext?.(opts);
36
- };
37
- const { path, router } = opts;
38
- const bodyResult = await getPostBody(opts);
39
- const query = opts.req.query
40
- ? new URLSearchParams(opts.req.query)
41
- : new URLSearchParams(opts.req.url.split('?')[1]);
42
- const req = {
43
- method: opts.req.method,
44
- headers: opts.req.headers,
45
- query,
46
- body: bodyResult.ok ? bodyResult.data : undefined,
47
- };
48
- const result = await resolveHTTPResponse({
49
- batching: opts.batching,
50
- responseMeta: opts.responseMeta,
51
- path,
52
- createContext,
53
- router,
54
- req,
55
- error: bodyResult.ok ? null : bodyResult.error,
56
- onError(o) {
57
- opts?.onError?.({
58
- ...o,
59
- req: opts.req,
60
- });
61
- },
62
- });
63
- const { res } = opts;
64
- if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
65
- res.statusCode = result.status;
66
- }
67
- for (const [key, value] of Object.entries(result.headers ?? {})) {
68
- if (typeof value === 'undefined') {
69
- continue;
70
- }
71
- res.setHeader(key, value);
72
- }
73
- res.end(result.body);
74
- }
75
-
76
- export { nodeHTTPRequestHandler as n };