@trpc/server 9.18.0 → 9.19.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.
Files changed (37) hide show
  1. package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +2 -1
  2. package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +2 -1
  3. package/adapters/express/dist/trpc-server-adapters-express.esm.js +2 -1
  4. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.d.ts +1 -0
  5. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +102 -0
  6. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.js +7 -0
  7. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +102 -0
  8. package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +97 -0
  9. package/adapters/fastify/package.json +4 -0
  10. package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +2 -1
  11. package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +2 -1
  12. package/adapters/next/dist/trpc-server-adapters-next.esm.js +2 -1
  13. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +2 -1
  14. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +2 -1
  15. package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +2 -1
  16. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +2 -1
  17. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +2 -1
  18. package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +2 -1
  19. package/dist/declarations/src/adapters/fastify/fastifyRequestHandler.d.ts +13 -0
  20. package/dist/declarations/src/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -0
  21. package/dist/declarations/src/adapters/fastify/fastifyTRPCPlugin.d.ts +12 -0
  22. package/dist/declarations/src/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -0
  23. package/dist/declarations/src/adapters/fastify/index.d.ts +3 -0
  24. package/dist/declarations/src/adapters/fastify/index.d.ts.map +1 -0
  25. package/dist/nodeHTTPRequestHandler-2cc444d8.cjs.dev.js +108 -0
  26. package/dist/nodeHTTPRequestHandler-8ddacc19.cjs.prod.js +108 -0
  27. package/dist/nodeHTTPRequestHandler-d00c1126.esm.js +106 -0
  28. package/dist/{nodeHTTPRequestHandler-b7ff366e.cjs.dev.js → resolveHTTPResponse-3867844c.cjs.dev.js} +0 -103
  29. package/dist/{nodeHTTPRequestHandler-7325b53f.cjs.prod.js → resolveHTTPResponse-562c3d62.cjs.prod.js} +0 -103
  30. package/dist/{nodeHTTPRequestHandler-2e6cc2d7.esm.js → resolveHTTPResponse-85f3f5df.esm.js} +1 -103
  31. package/dist/trpc-server.cjs.dev.js +9 -8
  32. package/dist/trpc-server.cjs.prod.js +9 -8
  33. package/dist/trpc-server.esm.js +4 -3
  34. package/package.json +8 -3
  35. package/src/adapters/fastify/fastifyRequestHandler.ts +79 -0
  36. package/src/adapters/fastify/fastifyTRPCPlugin.ts +48 -0
  37. package/src/adapters/fastify/index.ts +2 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "9.18.0",
3
+ "version": "9.19.0",
4
4
  "description": "tRPC Server",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -19,6 +19,7 @@
19
19
  "entrypoints": [
20
20
  "index.ts",
21
21
  "adapters/express.ts",
22
+ "adapters/fastify/index.ts",
22
23
  "adapters/next.ts",
23
24
  "adapters/node-http/index.ts",
24
25
  "adapters/standalone.ts",
@@ -51,21 +52,25 @@
51
52
  "@types/express": "^4.17.12",
52
53
  "@types/hash-sum": "^1.0.0",
53
54
  "@types/ws": "^8.2.0",
55
+ "abort-controller": "^3.0.0",
54
56
  "devalue": "^2.0.1",
55
57
  "express": "^4.17.1",
56
- "fastify": "^3.17.0",
58
+ "fastify": "^3.27.1",
59
+ "fastify-plugin": "^3.0.1",
60
+ "fastify-websocket": "^4.0.0",
57
61
  "hash-sum": "^2.0.0",
58
62
  "jest": "^27.1.0",
59
63
  "konn": "^0.7.0",
60
64
  "myzod": "^1.3.1",
61
65
  "next": "^12.0.1",
66
+ "node-fetch": "^2.6.1",
62
67
  "superstruct": "^0.15.2",
63
68
  "typescript": "4.4.4",
64
69
  "ws": "^8.0.0",
65
70
  "yup": "^0.32.8",
66
71
  "zod": "^3.0.0"
67
72
  },
68
- "gitHead": "e8468a50b289b0231cf1ee92d0f5195fe20c0d44",
73
+ "gitHead": "6eb1642a4a0aba9eb5fe4c1aa2648f5bdf65fcde",
69
74
  "dependencies": {
70
75
  "tslib": "^2.1.0"
71
76
  }
@@ -0,0 +1,79 @@
1
+ import { URLSearchParams } from 'url';
2
+ import { FastifyReply, FastifyRequest } from 'fastify';
3
+ import { assertNotBrowser } from '../../assertNotBrowser';
4
+ import { NodeHTTPCreateContextOption } from '../node-http';
5
+ import { AnyRouter, inferRouterContext } from '../../router';
6
+ import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
7
+ import {
8
+ HTTPBaseHandlerOptions,
9
+ HTTPRequest,
10
+ } from '../../http/internals/types';
11
+
12
+ assertNotBrowser();
13
+
14
+ export type FastifyHandlerOptions<
15
+ TRouter extends AnyRouter,
16
+ TRequest extends FastifyRequest,
17
+ TResponse extends FastifyReply,
18
+ > = HTTPBaseHandlerOptions<TRouter, TRequest> &
19
+ NodeHTTPCreateContextOption<TRouter, TRequest, TResponse>;
20
+
21
+ type FastifyRequestHandlerOptions<
22
+ TRouter extends AnyRouter,
23
+ TRequest extends FastifyRequest,
24
+ TResponse extends FastifyReply,
25
+ > = {
26
+ req: TRequest;
27
+ res: TResponse;
28
+ path: string;
29
+ } & FastifyHandlerOptions<TRouter, TRequest, TResponse>;
30
+
31
+ export async function fastifyRequestHandler<
32
+ TRouter extends AnyRouter,
33
+ TRequest extends FastifyRequest,
34
+ TResponse extends FastifyReply,
35
+ >(opts: FastifyRequestHandlerOptions<TRouter, TRequest, TResponse>) {
36
+ const createContext = async function _createContext(): Promise<
37
+ inferRouterContext<TRouter>
38
+ > {
39
+ return opts.createContext?.(opts);
40
+ };
41
+
42
+ const query = opts.req.query
43
+ ? new URLSearchParams(opts.req.query as any)
44
+ : new URLSearchParams(opts.req.url.split('?')[1]);
45
+
46
+ const req: HTTPRequest = {
47
+ query,
48
+ method: opts.req.method,
49
+ headers: opts.req.headers,
50
+ body: opts.req.body ?? 'null',
51
+ };
52
+
53
+ const result = await resolveHTTPResponse({
54
+ req,
55
+ createContext,
56
+ path: opts.path,
57
+ router: opts.router,
58
+ batching: opts.batching,
59
+ responseMeta: opts.responseMeta,
60
+ onError(o) {
61
+ opts?.onError?.({ ...o, req: opts.req });
62
+ },
63
+ });
64
+
65
+ const { res } = opts;
66
+
67
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
68
+ res.statusCode = result.status;
69
+ }
70
+
71
+ for (const [key, value] of Object.entries(result.headers ?? {})) {
72
+ if (typeof value === 'undefined') {
73
+ continue;
74
+ }
75
+ res.header(key, value);
76
+ }
77
+
78
+ res.send(result.body);
79
+ }
@@ -0,0 +1,48 @@
1
+ /// <reference types="fastify-websocket" />
2
+ import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
3
+ import { fastifyRequestHandler } from './fastifyRequestHandler';
4
+ import { NodeHTTPCreateContextFnOptions } from '../node-http';
5
+ import { applyWSSHandler, WSSHandlerOptions } from '../ws';
6
+ import { FastifyHandlerOptions } from '.';
7
+ import { AnyRouter } from '../..';
8
+
9
+ export interface FastifyTRPCPluginOptions<TRouter extends AnyRouter> {
10
+ prefix?: string;
11
+ useWSS?: boolean;
12
+ trpcOptions: FastifyHandlerOptions<TRouter, FastifyRequest, FastifyReply>;
13
+ }
14
+
15
+ export type CreateFastifyContextOptions = NodeHTTPCreateContextFnOptions<
16
+ FastifyRequest,
17
+ FastifyReply
18
+ >;
19
+
20
+ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
21
+ fastify: FastifyInstance,
22
+ opts: FastifyTRPCPluginOptions<TRouter>,
23
+ done: (err?: Error) => void,
24
+ ) {
25
+ fastify.addContentTypeParser(
26
+ 'application/json',
27
+ { parseAs: 'string' },
28
+ function (_, body, _done) {
29
+ _done(null, body);
30
+ },
31
+ );
32
+
33
+ fastify.all(`${opts.prefix ?? ''}/:path`, (req, res) => {
34
+ const path = (req.params as any).path;
35
+ fastifyRequestHandler({ ...opts.trpcOptions, req, res, path });
36
+ });
37
+
38
+ if (opts.useWSS) {
39
+ applyWSSHandler<TRouter>({
40
+ ...(opts.trpcOptions as WSSHandlerOptions<TRouter>),
41
+ wss: fastify.websocketServer,
42
+ });
43
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
44
+ fastify.get(opts.prefix ?? '/', { websocket: true }, () => {});
45
+ }
46
+
47
+ done();
48
+ }
@@ -0,0 +1,2 @@
1
+ export * from './fastifyRequestHandler';
2
+ export * from './fastifyTRPCPlugin';