@trpc/server 11.0.0-alpha-tmp-export-from-main.221 → 11.0.0-alpha-tmp-export-from-main.222

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 (136) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +68 -0
  2. package/dist/adapters/aws-lambda/index.d.ts +54 -6
  3. package/dist/adapters/aws-lambda/index.js +110 -20
  4. package/dist/adapters/aws-lambda/index.mjs +94 -4
  5. package/dist/adapters/express.d.mts +19 -0
  6. package/dist/adapters/express.d.ts +9 -6
  7. package/dist/adapters/express.js +3 -3
  8. package/dist/adapters/express.mjs +1 -1
  9. package/dist/adapters/fastify/index.d.mts +33 -0
  10. package/dist/adapters/fastify/index.d.ts +33 -3
  11. package/dist/adapters/fastify/index.js +124 -5
  12. package/dist/adapters/fastify/index.mjs +125 -2
  13. package/dist/adapters/fetch/index.d.mts +51 -0
  14. package/dist/adapters/fetch/index.d.ts +51 -3
  15. package/dist/adapters/fetch/index.js +115 -4
  16. package/dist/adapters/fetch/index.mjs +116 -1
  17. package/dist/adapters/next.d.mts +21 -0
  18. package/dist/adapters/next.d.ts +11 -7
  19. package/dist/adapters/next.js +6 -6
  20. package/dist/adapters/next.mjs +2 -2
  21. package/dist/adapters/node-http/content-type/form-data/index.d.mts +219 -0
  22. package/dist/adapters/node-http/content-type/form-data/index.d.ts +201 -10
  23. package/dist/adapters/node-http/content-type/form-data/index.js +662 -23
  24. package/dist/adapters/node-http/content-type/form-data/index.mjs +646 -10
  25. package/dist/adapters/node-http/{types.d.ts → content-type/json/index.d.mts} +21 -15
  26. package/dist/adapters/node-http/content-type/json/index.d.ts +89 -2
  27. package/dist/adapters/node-http/content-type/json/index.js +48 -7
  28. package/dist/adapters/node-http/content-type/json/index.mjs +44 -3
  29. package/dist/adapters/node-http/index.d.mts +100 -0
  30. package/dist/adapters/node-http/index.d.ts +100 -3
  31. package/dist/adapters/node-http/index.js +106 -4
  32. package/dist/adapters/node-http/index.mjs +107 -1
  33. package/dist/adapters/standalone.d.mts +21 -0
  34. package/dist/adapters/standalone.d.ts +11 -9
  35. package/dist/adapters/standalone.js +9 -5
  36. package/dist/adapters/standalone.mjs +2 -2
  37. package/dist/adapters/ws.d.mts +36 -0
  38. package/dist/adapters/ws.d.ts +12 -13
  39. package/dist/adapters/ws.js +30 -28
  40. package/dist/adapters/ws.mjs +12 -10
  41. package/dist/http.d.mts +1 -0
  42. package/dist/http.d.ts +1 -7
  43. package/dist/http.js +7 -7
  44. package/dist/http.mjs +1 -1
  45. package/dist/index.d.mts +9 -0
  46. package/dist/index.d.ts +5 -65
  47. package/dist/index.js +809 -46
  48. package/dist/index.mjs +798 -1
  49. package/dist/observable.d.mts +1 -0
  50. package/dist/observable.d.ts +1 -3
  51. package/dist/observable.js +8 -8
  52. package/dist/observable.mjs +1 -1
  53. package/dist/rpc.d.mts +1 -0
  54. package/dist/rpc.d.ts +1 -3
  55. package/dist/rpc.js +5 -5
  56. package/dist/rpc.mjs +1 -1
  57. package/dist/shared.d.mts +1 -0
  58. package/dist/shared.d.ts +1 -26
  59. package/dist/shared.js +4 -4
  60. package/dist/shared.mjs +1 -1
  61. package/dist/unstableDontImportMe.d.mts +1 -0
  62. package/dist/unstableDontImportMe.d.ts +1 -10
  63. package/dist/unstableDontImportMe.js +1549 -142
  64. package/dist/unstableDontImportMe.mjs +1514 -1
  65. package/package.json +6 -4
  66. package/dist/@trpc-server/http.d.ts +0 -2
  67. package/dist/@trpc-server/http.d.ts.map +0 -1
  68. package/dist/adapters/aws-lambda/index.d.ts.map +0 -1
  69. package/dist/adapters/aws-lambda/utils.d.ts +0 -48
  70. package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
  71. package/dist/adapters/aws-lambda/utils.js +0 -100
  72. package/dist/adapters/aws-lambda/utils.mjs +0 -93
  73. package/dist/adapters/express.d.ts.map +0 -1
  74. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +0 -13
  75. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +0 -1
  76. package/dist/adapters/fastify/fastifyRequestHandler.js +0 -81
  77. package/dist/adapters/fastify/fastifyRequestHandler.mjs +0 -79
  78. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +0 -22
  79. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +0 -1
  80. package/dist/adapters/fastify/fastifyTRPCPlugin.js +0 -51
  81. package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +0 -49
  82. package/dist/adapters/fastify/index.d.ts.map +0 -1
  83. package/dist/adapters/fetch/fetchRequestHandler.d.ts +0 -18
  84. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +0 -1
  85. package/dist/adapters/fetch/fetchRequestHandler.js +0 -118
  86. package/dist/adapters/fetch/fetchRequestHandler.mjs +0 -116
  87. package/dist/adapters/fetch/index.d.ts.map +0 -1
  88. package/dist/adapters/fetch/types.d.ts +0 -31
  89. package/dist/adapters/fetch/types.d.ts.map +0 -1
  90. package/dist/adapters/next.d.ts.map +0 -1
  91. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -70
  92. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
  93. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
  94. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
  95. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
  96. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
  97. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
  98. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
  99. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
  100. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
  101. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
  102. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
  103. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
  104. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
  105. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
  106. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
  107. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
  108. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
  109. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
  110. package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -42
  111. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -40
  112. package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
  113. package/dist/adapters/node-http/index.d.ts.map +0 -1
  114. package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
  115. package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
  116. package/dist/adapters/node-http/internals/contentType.js +0 -8
  117. package/dist/adapters/node-http/internals/contentType.mjs +0 -6
  118. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +0 -14
  119. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +0 -1
  120. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +0 -109
  121. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +0 -107
  122. package/dist/adapters/node-http/types.d.ts.map +0 -1
  123. package/dist/adapters/standalone.d.ts.map +0 -1
  124. package/dist/adapters/ws.d.ts.map +0 -1
  125. package/dist/http.d.ts.map +0 -1
  126. package/dist/index.d.ts.map +0 -1
  127. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.js +0 -203
  128. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.mjs +0 -201
  129. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.js +0 -167
  130. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.mjs +0 -163
  131. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.js +0 -35
  132. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.mjs +0 -30
  133. package/dist/observable.d.ts.map +0 -1
  134. package/dist/rpc.d.ts.map +0 -1
  135. package/dist/shared.d.ts.map +0 -1
  136. package/dist/unstableDontImportMe.d.ts.map +0 -1
@@ -1,9 +1,128 @@
1
- 'use strict';
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
- var fastifyRequestHandler = require('./fastifyRequestHandler.js');
4
- var fastifyTRPCPlugin = require('./fastifyTRPCPlugin.js');
3
+ var node_stream = require('node:stream');
4
+ var http = require('@trpc/server/http');
5
+ var ws = require('@trpc/server/adapters/ws');
5
6
 
7
+ async function fastifyRequestHandler(opts) {
8
+ const createContext = async (innerOpts)=>{
9
+ return await opts.createContext?.({
10
+ ...opts,
11
+ ...innerOpts
12
+ });
13
+ };
14
+ const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
15
+ const req = {
16
+ query,
17
+ method: opts.req.method,
18
+ headers: opts.req.headers,
19
+ body: opts.req.body ?? 'null'
20
+ };
21
+ let resolve;
22
+ const promise = new Promise((r)=>resolve = r);
23
+ let isStream = false;
24
+ let stream;
25
+ let formatter;
26
+ const unstable_onHead = (head, isStreaming)=>{
27
+ if (!opts.res.statusCode || opts.res.statusCode === 200) {
28
+ opts.res.statusCode = head.status;
29
+ }
30
+ for (const [key, value] of Object.entries(head.headers ?? {})){
31
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
32
+ continue;
33
+ }
34
+ void opts.res.header(key, value);
35
+ }
36
+ if (isStreaming) {
37
+ void opts.res.header('Transfer-Encoding', 'chunked');
38
+ void opts.res.header('Vary', opts.res.hasHeader('Vary') ? 'trpc-batch-mode, ' + opts.res.getHeader('Vary') : 'trpc-batch-mode');
39
+ stream = new node_stream.Readable();
40
+ stream._read = ()=>{}; // eslint-disable-line @typescript-eslint/no-empty-function -- https://github.com/fastify/fastify/issues/805#issuecomment-369172154
41
+ resolve(opts.res.send(stream));
42
+ isStream = true;
43
+ formatter = http.getBatchStreamFormatter();
44
+ }
45
+ };
46
+ const unstable_onChunk = ([index, string])=>{
47
+ if (index === -1) {
48
+ // full response, no streaming
49
+ resolve(opts.res.send(string));
50
+ } else {
51
+ stream.push(formatter(index, string));
52
+ }
53
+ };
54
+ http.resolveHTTPResponse({
55
+ req,
56
+ createContext,
57
+ path: opts.path,
58
+ router: opts.router,
59
+ batching: opts.batching,
60
+ responseMeta: opts.responseMeta,
61
+ onError (o) {
62
+ opts?.onError?.({
63
+ ...o,
64
+ req: opts.req
65
+ });
66
+ },
67
+ unstable_onHead,
68
+ unstable_onChunk
69
+ }).then(()=>{
70
+ if (isStream) {
71
+ stream.push(formatter.end());
72
+ stream.push(null); // https://github.com/fastify/fastify/issues/805#issuecomment-369172154
73
+ }
74
+ }).catch(()=>{
75
+ if (isStream) {
76
+ stream.push(null);
77
+ }
78
+ });
79
+ return promise;
80
+ }
6
81
 
82
+ /**
83
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
84
+ *
85
+ * Do **not** import from `@trpc/core`
86
+ * @example
87
+ * ```ts
88
+ * import type { AnyTRPCRouter } from '@trpc/server'
89
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
90
+ * ```
91
+ */ /// <reference types="@fastify/websocket" />
92
+ function fastifyTRPCPlugin(fastify, opts, done) {
93
+ fastify.removeContentTypeParser('application/json');
94
+ fastify.addContentTypeParser('application/json', {
95
+ parseAs: 'string'
96
+ }, function(_, body, _done) {
97
+ _done(null, body);
98
+ });
99
+ let prefix = opts.prefix ?? '';
100
+ // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
101
+ // @ts-expect-error property 'default' does not exists on type ...
102
+ if (typeof fastifyTRPCPlugin.default !== 'function') {
103
+ prefix = ''; // handled by fastify internally
104
+ }
105
+ fastify.all(`${prefix}/:path`, async (req, res)=>{
106
+ const path = req.params.path;
107
+ await fastifyRequestHandler({
108
+ ...opts.trpcOptions,
109
+ req,
110
+ res,
111
+ path
112
+ });
113
+ });
114
+ if (opts.useWSS) {
115
+ ws.applyWSSHandler({
116
+ ...opts.trpcOptions,
117
+ wss: fastify.websocketServer
118
+ });
119
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
120
+ fastify.get(prefix ?? '/', {
121
+ websocket: true
122
+ }, ()=>{});
123
+ }
124
+ done();
125
+ }
7
126
 
8
- exports.fastifyRequestHandler = fastifyRequestHandler.fastifyRequestHandler;
9
- exports.fastifyTRPCPlugin = fastifyTRPCPlugin.fastifyTRPCPlugin;
127
+ exports.fastifyRequestHandler = fastifyRequestHandler;
128
+ exports.fastifyTRPCPlugin = fastifyTRPCPlugin;
@@ -1,2 +1,125 @@
1
- export { fastifyRequestHandler } from './fastifyRequestHandler.mjs';
2
- export { fastifyTRPCPlugin } from './fastifyTRPCPlugin.mjs';
1
+ import { Readable } from 'node:stream';
2
+ import { resolveHTTPResponse, getBatchStreamFormatter } from '@trpc/server/http';
3
+ import { applyWSSHandler } from '@trpc/server/adapters/ws';
4
+
5
+ async function fastifyRequestHandler(opts) {
6
+ const createContext = async (innerOpts)=>{
7
+ return await opts.createContext?.({
8
+ ...opts,
9
+ ...innerOpts
10
+ });
11
+ };
12
+ const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
13
+ const req = {
14
+ query,
15
+ method: opts.req.method,
16
+ headers: opts.req.headers,
17
+ body: opts.req.body ?? 'null'
18
+ };
19
+ let resolve;
20
+ const promise = new Promise((r)=>resolve = r);
21
+ let isStream = false;
22
+ let stream;
23
+ let formatter;
24
+ const unstable_onHead = (head, isStreaming)=>{
25
+ if (!opts.res.statusCode || opts.res.statusCode === 200) {
26
+ opts.res.statusCode = head.status;
27
+ }
28
+ for (const [key, value] of Object.entries(head.headers ?? {})){
29
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
30
+ continue;
31
+ }
32
+ void opts.res.header(key, value);
33
+ }
34
+ if (isStreaming) {
35
+ void opts.res.header('Transfer-Encoding', 'chunked');
36
+ void opts.res.header('Vary', opts.res.hasHeader('Vary') ? 'trpc-batch-mode, ' + opts.res.getHeader('Vary') : 'trpc-batch-mode');
37
+ stream = new Readable();
38
+ stream._read = ()=>{}; // eslint-disable-line @typescript-eslint/no-empty-function -- https://github.com/fastify/fastify/issues/805#issuecomment-369172154
39
+ resolve(opts.res.send(stream));
40
+ isStream = true;
41
+ formatter = getBatchStreamFormatter();
42
+ }
43
+ };
44
+ const unstable_onChunk = ([index, string])=>{
45
+ if (index === -1) {
46
+ // full response, no streaming
47
+ resolve(opts.res.send(string));
48
+ } else {
49
+ stream.push(formatter(index, string));
50
+ }
51
+ };
52
+ resolveHTTPResponse({
53
+ req,
54
+ createContext,
55
+ path: opts.path,
56
+ router: opts.router,
57
+ batching: opts.batching,
58
+ responseMeta: opts.responseMeta,
59
+ onError (o) {
60
+ opts?.onError?.({
61
+ ...o,
62
+ req: opts.req
63
+ });
64
+ },
65
+ unstable_onHead,
66
+ unstable_onChunk
67
+ }).then(()=>{
68
+ if (isStream) {
69
+ stream.push(formatter.end());
70
+ stream.push(null); // https://github.com/fastify/fastify/issues/805#issuecomment-369172154
71
+ }
72
+ }).catch(()=>{
73
+ if (isStream) {
74
+ stream.push(null);
75
+ }
76
+ });
77
+ return promise;
78
+ }
79
+
80
+ /**
81
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
82
+ *
83
+ * Do **not** import from `@trpc/core`
84
+ * @example
85
+ * ```ts
86
+ * import type { AnyTRPCRouter } from '@trpc/server'
87
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
88
+ * ```
89
+ */ /// <reference types="@fastify/websocket" />
90
+ function fastifyTRPCPlugin(fastify, opts, done) {
91
+ fastify.removeContentTypeParser('application/json');
92
+ fastify.addContentTypeParser('application/json', {
93
+ parseAs: 'string'
94
+ }, function(_, body, _done) {
95
+ _done(null, body);
96
+ });
97
+ let prefix = opts.prefix ?? '';
98
+ // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
99
+ // @ts-expect-error property 'default' does not exists on type ...
100
+ if (typeof fastifyTRPCPlugin.default !== 'function') {
101
+ prefix = ''; // handled by fastify internally
102
+ }
103
+ fastify.all(`${prefix}/:path`, async (req, res)=>{
104
+ const path = req.params.path;
105
+ await fastifyRequestHandler({
106
+ ...opts.trpcOptions,
107
+ req,
108
+ res,
109
+ path
110
+ });
111
+ });
112
+ if (opts.useWSS) {
113
+ applyWSSHandler({
114
+ ...opts.trpcOptions,
115
+ wss: fastify.websocketServer
116
+ });
117
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
118
+ fastify.get(prefix ?? '/', {
119
+ websocket: true
120
+ }, ()=>{});
121
+ }
122
+ done();
123
+ }
124
+
125
+ export { fastifyRequestHandler, fastifyTRPCPlugin };
@@ -0,0 +1,51 @@
1
+ import { AnyRouter, inferRouterContext } from '@trpc/server';
2
+ import { TRPCRequestInfo, HTTPBaseHandlerOptions } from '@trpc/core';
3
+
4
+ /**
5
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
6
+ *
7
+ * Do **not** import from `@trpc/core`
8
+ * @example
9
+ * ```ts
10
+ * import type { AnyTRPCRouter } from '@trpc/server'
11
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
12
+ * ```
13
+ */
14
+
15
+ type FetchCreateContextFnOptions = {
16
+ req: Request;
17
+ resHeaders: Headers;
18
+ info: TRPCRequestInfo;
19
+ };
20
+ type FetchCreateContextFn<TRouter extends AnyRouter> = (opts: FetchCreateContextFnOptions) => inferRouterContext<TRouter> | Promise<inferRouterContext<TRouter>>;
21
+ type FetchCreateContextOption<TRouter extends AnyRouter> = unknown extends inferRouterContext<TRouter> ? {
22
+ /**
23
+ * @link https://trpc.io/docs/v11/context
24
+ **/
25
+ createContext?: FetchCreateContextFn<TRouter>;
26
+ } : {
27
+ /**
28
+ * @link https://trpc.io/docs/v11/context
29
+ **/
30
+ createContext: FetchCreateContextFn<TRouter>;
31
+ };
32
+ type FetchHandlerOptions<TRouter extends AnyRouter> = FetchCreateContextOption<TRouter> & HTTPBaseHandlerOptions<TRouter, Request>;
33
+
34
+ /**
35
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
36
+ *
37
+ * Do **not** import from `@trpc/core`
38
+ * @example
39
+ * ```ts
40
+ * import type { AnyTRPCRouter } from '@trpc/server'
41
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
42
+ * ```
43
+ */
44
+
45
+ type FetchHandlerRequestOptions<TRouter extends AnyRouter> = FetchHandlerOptions<TRouter> & {
46
+ req: Request;
47
+ endpoint: string;
48
+ };
49
+ declare function fetchRequestHandler<TRouter extends AnyRouter>(opts: FetchHandlerRequestOptions<TRouter>): Promise<Response>;
50
+
51
+ export { type FetchCreateContextFn, type FetchCreateContextFnOptions, type FetchCreateContextOption, type FetchHandlerOptions, type FetchHandlerRequestOptions, fetchRequestHandler };
@@ -1,3 +1,51 @@
1
- export * from './fetchRequestHandler';
2
- export * from './types';
3
- //# sourceMappingURL=index.d.ts.map
1
+ import { AnyRouter, inferRouterContext } from '@trpc/server';
2
+ import { TRPCRequestInfo, HTTPBaseHandlerOptions } from '@trpc/core';
3
+
4
+ /**
5
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
6
+ *
7
+ * Do **not** import from `@trpc/core`
8
+ * @example
9
+ * ```ts
10
+ * import type { AnyTRPCRouter } from '@trpc/server'
11
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
12
+ * ```
13
+ */
14
+
15
+ type FetchCreateContextFnOptions = {
16
+ req: Request;
17
+ resHeaders: Headers;
18
+ info: TRPCRequestInfo;
19
+ };
20
+ type FetchCreateContextFn<TRouter extends AnyRouter> = (opts: FetchCreateContextFnOptions) => inferRouterContext<TRouter> | Promise<inferRouterContext<TRouter>>;
21
+ type FetchCreateContextOption<TRouter extends AnyRouter> = unknown extends inferRouterContext<TRouter> ? {
22
+ /**
23
+ * @link https://trpc.io/docs/v11/context
24
+ **/
25
+ createContext?: FetchCreateContextFn<TRouter>;
26
+ } : {
27
+ /**
28
+ * @link https://trpc.io/docs/v11/context
29
+ **/
30
+ createContext: FetchCreateContextFn<TRouter>;
31
+ };
32
+ type FetchHandlerOptions<TRouter extends AnyRouter> = FetchCreateContextOption<TRouter> & HTTPBaseHandlerOptions<TRouter, Request>;
33
+
34
+ /**
35
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
36
+ *
37
+ * Do **not** import from `@trpc/core`
38
+ * @example
39
+ * ```ts
40
+ * import type { AnyTRPCRouter } from '@trpc/server'
41
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
42
+ * ```
43
+ */
44
+
45
+ type FetchHandlerRequestOptions<TRouter extends AnyRouter> = FetchHandlerOptions<TRouter> & {
46
+ req: Request;
47
+ endpoint: string;
48
+ };
49
+ declare function fetchRequestHandler<TRouter extends AnyRouter>(opts: FetchHandlerRequestOptions<TRouter>): Promise<Response>;
50
+
51
+ export { type FetchCreateContextFn, type FetchCreateContextFnOptions, type FetchCreateContextOption, type FetchHandlerOptions, type FetchHandlerRequestOptions, fetchRequestHandler };
@@ -1,7 +1,118 @@
1
- 'use strict';
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
- var fetchRequestHandler = require('./fetchRequestHandler.js');
3
+ var http = require('@trpc/server/http');
4
4
 
5
+ /**
6
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
7
+ *
8
+ * Do **not** import from `@trpc/core`
9
+ * @example
10
+ * ```ts
11
+ * import type { AnyTRPCRouter } from '@trpc/server'
12
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
13
+ * ```
14
+ */ // @trpc/server
15
+ const trimSlashes = (path)=>{
16
+ path = path.startsWith('/') ? path.slice(1) : path;
17
+ path = path.endsWith('/') ? path.slice(0, -1) : path;
18
+ return path;
19
+ };
20
+ async function fetchRequestHandler(opts) {
21
+ const resHeaders = new Headers();
22
+ const createContext = async (innerOpts)=>{
23
+ return opts.createContext?.({
24
+ req: opts.req,
25
+ resHeaders,
26
+ ...innerOpts
27
+ });
28
+ };
29
+ const url = new URL(opts.req.url);
30
+ const pathname = trimSlashes(url.pathname);
31
+ const endpoint = trimSlashes(opts.endpoint);
32
+ const path = trimSlashes(pathname.slice(endpoint.length));
33
+ const req = {
34
+ query: url.searchParams,
35
+ method: opts.req.method,
36
+ headers: Object.fromEntries(opts.req.headers),
37
+ body: opts.req.headers.get('content-type')?.startsWith('application/json') ? await opts.req.text() : ''
38
+ };
39
+ let resolve;
40
+ const promise = new Promise((r)=>resolve = r);
41
+ let status = 200;
42
+ let isStream = false;
43
+ let controller;
44
+ let encoder;
45
+ let formatter;
46
+ const unstable_onHead = (head, isStreaming)=>{
47
+ for (const [key, value] of Object.entries(head.headers ?? {})){
48
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
49
+ continue;
50
+ }
51
+ if (typeof value === 'string') {
52
+ resHeaders.set(key, value);
53
+ continue;
54
+ }
55
+ for (const v of value){
56
+ resHeaders.append(key, v);
57
+ }
58
+ }
59
+ status = head.status;
60
+ if (isStreaming) {
61
+ resHeaders.set('Transfer-Encoding', 'chunked');
62
+ resHeaders.append('Vary', 'trpc-batch-mode');
63
+ const stream = new ReadableStream({
64
+ start (c) {
65
+ controller = c;
66
+ }
67
+ });
68
+ const response = new Response(stream, {
69
+ status,
70
+ headers: resHeaders
71
+ });
72
+ resolve(response);
73
+ encoder = new TextEncoder();
74
+ formatter = http.getBatchStreamFormatter();
75
+ isStream = true;
76
+ }
77
+ };
78
+ const unstable_onChunk = ([index, string])=>{
79
+ if (index === -1) {
80
+ // full response, no streaming
81
+ const response = new Response(string || null, {
82
+ status,
83
+ headers: resHeaders
84
+ });
85
+ resolve(response);
86
+ } else {
87
+ controller.enqueue(encoder.encode(formatter(index, string)));
88
+ }
89
+ };
90
+ http.resolveHTTPResponse({
91
+ req,
92
+ createContext,
93
+ path,
94
+ router: opts.router,
95
+ batching: opts.batching,
96
+ responseMeta: opts.responseMeta,
97
+ onError (o) {
98
+ opts?.onError?.({
99
+ ...o,
100
+ req: opts.req
101
+ });
102
+ },
103
+ unstable_onHead,
104
+ unstable_onChunk
105
+ }).then(()=>{
106
+ if (isStream) {
107
+ controller.enqueue(encoder.encode(formatter.end()));
108
+ controller.close();
109
+ }
110
+ }).catch(()=>{
111
+ if (isStream) {
112
+ controller.close();
113
+ }
114
+ });
115
+ return promise;
116
+ }
5
117
 
6
-
7
- exports.fetchRequestHandler = fetchRequestHandler.fetchRequestHandler;
118
+ exports.fetchRequestHandler = fetchRequestHandler;
@@ -1 +1,116 @@
1
- export { fetchRequestHandler } from './fetchRequestHandler.mjs';
1
+ import { resolveHTTPResponse, getBatchStreamFormatter } from '@trpc/server/http';
2
+
3
+ /**
4
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
5
+ *
6
+ * Do **not** import from `@trpc/core`
7
+ * @example
8
+ * ```ts
9
+ * import type { AnyTRPCRouter } from '@trpc/server'
10
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
11
+ * ```
12
+ */ // @trpc/server
13
+ const trimSlashes = (path)=>{
14
+ path = path.startsWith('/') ? path.slice(1) : path;
15
+ path = path.endsWith('/') ? path.slice(0, -1) : path;
16
+ return path;
17
+ };
18
+ async function fetchRequestHandler(opts) {
19
+ const resHeaders = new Headers();
20
+ const createContext = async (innerOpts)=>{
21
+ return opts.createContext?.({
22
+ req: opts.req,
23
+ resHeaders,
24
+ ...innerOpts
25
+ });
26
+ };
27
+ const url = new URL(opts.req.url);
28
+ const pathname = trimSlashes(url.pathname);
29
+ const endpoint = trimSlashes(opts.endpoint);
30
+ const path = trimSlashes(pathname.slice(endpoint.length));
31
+ const req = {
32
+ query: url.searchParams,
33
+ method: opts.req.method,
34
+ headers: Object.fromEntries(opts.req.headers),
35
+ body: opts.req.headers.get('content-type')?.startsWith('application/json') ? await opts.req.text() : ''
36
+ };
37
+ let resolve;
38
+ const promise = new Promise((r)=>resolve = r);
39
+ let status = 200;
40
+ let isStream = false;
41
+ let controller;
42
+ let encoder;
43
+ let formatter;
44
+ const unstable_onHead = (head, isStreaming)=>{
45
+ for (const [key, value] of Object.entries(head.headers ?? {})){
46
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
47
+ continue;
48
+ }
49
+ if (typeof value === 'string') {
50
+ resHeaders.set(key, value);
51
+ continue;
52
+ }
53
+ for (const v of value){
54
+ resHeaders.append(key, v);
55
+ }
56
+ }
57
+ status = head.status;
58
+ if (isStreaming) {
59
+ resHeaders.set('Transfer-Encoding', 'chunked');
60
+ resHeaders.append('Vary', 'trpc-batch-mode');
61
+ const stream = new ReadableStream({
62
+ start (c) {
63
+ controller = c;
64
+ }
65
+ });
66
+ const response = new Response(stream, {
67
+ status,
68
+ headers: resHeaders
69
+ });
70
+ resolve(response);
71
+ encoder = new TextEncoder();
72
+ formatter = getBatchStreamFormatter();
73
+ isStream = true;
74
+ }
75
+ };
76
+ const unstable_onChunk = ([index, string])=>{
77
+ if (index === -1) {
78
+ // full response, no streaming
79
+ const response = new Response(string || null, {
80
+ status,
81
+ headers: resHeaders
82
+ });
83
+ resolve(response);
84
+ } else {
85
+ controller.enqueue(encoder.encode(formatter(index, string)));
86
+ }
87
+ };
88
+ resolveHTTPResponse({
89
+ req,
90
+ createContext,
91
+ path,
92
+ router: opts.router,
93
+ batching: opts.batching,
94
+ responseMeta: opts.responseMeta,
95
+ onError (o) {
96
+ opts?.onError?.({
97
+ ...o,
98
+ req: opts.req
99
+ });
100
+ },
101
+ unstable_onHead,
102
+ unstable_onChunk
103
+ }).then(()=>{
104
+ if (isStream) {
105
+ controller.enqueue(encoder.encode(formatter.end()));
106
+ controller.close();
107
+ }
108
+ }).catch(()=>{
109
+ if (isStream) {
110
+ controller.close();
111
+ }
112
+ });
113
+ return promise;
114
+ }
115
+
116
+ export { fetchRequestHandler };
@@ -0,0 +1,21 @@
1
+ import { NextApiRequest, NextApiResponse, NextApiHandler } from 'next';
2
+ export { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
3
+ import { AnyRouter } from '@trpc/server';
4
+ import { NodeHTTPCreateContextFnOptions, NodeHTTPHandlerOptions } from '@trpc/server/adapters/node-http';
5
+
6
+ /**
7
+ * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
8
+ *
9
+ * Do **not** import from `@trpc/core`
10
+ * @example
11
+ * ```ts
12
+ * import type { AnyTRPCRouter } from '@trpc/server'
13
+ * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
14
+ * ```
15
+ */
16
+
17
+ type CreateNextContextOptions = NodeHTTPCreateContextFnOptions<NextApiRequest, NextApiResponse>;
18
+
19
+ declare function createNextApiHandler<TRouter extends AnyRouter>(opts: NodeHTTPHandlerOptions<TRouter, NextApiRequest, NextApiResponse>): NextApiHandler;
20
+
21
+ export { type CreateNextContextOptions, createNextApiHandler };
@@ -1,3 +1,8 @@
1
+ import { NextApiRequest, NextApiResponse, NextApiHandler } from 'next';
2
+ export { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
3
+ import { AnyRouter } from '@trpc/server';
4
+ import { NodeHTTPCreateContextFnOptions, NodeHTTPHandlerOptions } from '@trpc/server/adapters/node-http';
5
+
1
6
  /**
2
7
  * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
3
8
  *
@@ -8,10 +13,9 @@
8
13
  * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
9
14
  * ```
10
15
  */
11
- import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
12
- import type { AnyRouter } from '..';
13
- import type { NodeHTTPCreateContextFnOptions, NodeHTTPHandlerOptions } from './node-http';
14
- export type CreateNextContextOptions = NodeHTTPCreateContextFnOptions<NextApiRequest, NextApiResponse>;
15
- export type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
16
- export declare function createNextApiHandler<TRouter extends AnyRouter>(opts: NodeHTTPHandlerOptions<TRouter, NextApiRequest, NextApiResponse>): NextApiHandler;
17
- //# sourceMappingURL=next.d.ts.map
16
+
17
+ type CreateNextContextOptions = NodeHTTPCreateContextFnOptions<NextApiRequest, NextApiResponse>;
18
+
19
+ declare function createNextApiHandler<TRouter extends AnyRouter>(opts: NodeHTTPHandlerOptions<TRouter, NextApiRequest, NextApiResponse>): NextApiHandler;
20
+
21
+ export { type CreateNextContextOptions, createNextApiHandler };