trpc-nuxt-module 4.3.3 → 4.5.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/README.md +25 -30
- package/dist/index.js +21 -9
- package/dist/runtime/client/models/ConnectionStateSubscriptionStatus.d.ts +5 -0
- package/dist/runtime/client/models/ConnectionStateSubscriptionStatus.js +8 -0
- package/dist/runtime/client/models/DecoratedRouterRecord.d.ts +1 -1
- package/dist/runtime/client/services/useProcedureMutation.js +16 -10
- package/dist/runtime/client/services/useProcedureSubscription.js +4 -3
- package/dist/runtime/server/createTRPCWebSocketHandler.js +15 -12
- package/dist/runtime/server/models/PeerWebSocketAdapter.d.ts +5 -16
- package/dist/runtime/server/models/PeerWebSocketAdapter.js +9 -30
- package/dist/runtime/server/models/WebSocketClientAdapter.d.ts +16 -0
- package/dist/runtime/server/models/WebSocketClientAdapter.js +27 -0
- package/dist/runtime/server/models/WebSocketServerAdapter.d.ts +1 -2
- package/dist/runtime/server/models/WebSocketServerAdapter.js +7 -5
- package/dist/services/getEventHandlerTemplate.js +1 -2
- package/dist/services/getWebSocketHandlerTemplate.js +1 -1
- package/package.json +13 -35
package/README.md
CHANGED
|
@@ -5,23 +5,31 @@
|
|
|
5
5
|
[![NPM downloads][badge-npm-downloads]][url-npm]
|
|
6
6
|
[![NPM Unpacked Size (with version)][badge-npm-unpacked-size]][url-npm]
|
|
7
7
|
|
|
8
|
-
tRPC for [Nuxt](https://nuxt.com).
|
|
8
|
+
tRPC for [Nuxt](https://nuxt.com). This module does the following:
|
|
9
|
+
|
|
10
|
+
- It registers the router's HTTP handler, and a WebSocket handler over Nitro's own WebSockets when you enable it.
|
|
11
|
+
- During server rendering, it fetches through the request event, so a call never leaves the process.
|
|
12
|
+
- It decorates the typed client with `useQuery`, `useLazyQuery`, `useMutation` and `useSubscription`. Each is a composable over Nuxt's own `useAsyncData`.
|
|
13
|
+
|
|
14
|
+
Design notes live in [the specs](https://github.com/Esposter/Esposter/tree/main/openspec/specs/trpc-nuxt-module).
|
|
9
15
|
|
|
10
16
|
## Table of Contents
|
|
11
17
|
|
|
12
18
|
- 🚀 [Getting Started](#getting-started)
|
|
13
|
-
-
|
|
19
|
+
- 🧩 [Key Exports](#key-exports)
|
|
14
20
|
- ⚖️ [License](#license)
|
|
15
21
|
|
|
16
22
|
---
|
|
17
23
|
|
|
18
24
|
## <a name="getting-started">🚀 Getting Started</a>
|
|
19
25
|
|
|
26
|
+
1. Install the packages.
|
|
27
|
+
|
|
20
28
|
```bash
|
|
21
29
|
pnpm i trpc-nuxt-module @trpc/server @trpc/client
|
|
22
30
|
```
|
|
23
31
|
|
|
24
|
-
Register the module
|
|
32
|
+
2. Register the module in `nuxt.config.ts`. Give the file and the export name of your router and of your context factory.
|
|
25
33
|
|
|
26
34
|
```ts
|
|
27
35
|
// nuxt.config.ts
|
|
@@ -37,6 +45,8 @@ export default defineNuxtConfig({
|
|
|
37
45
|
});
|
|
38
46
|
```
|
|
39
47
|
|
|
48
|
+
3. Define the router in the file that the `router` option names.
|
|
49
|
+
|
|
40
50
|
```ts
|
|
41
51
|
// server/trpc/router.ts
|
|
42
52
|
import { initTRPC } from "@trpc/server";
|
|
@@ -49,7 +59,7 @@ export const appRouter = t.router({ greeting: t.procedure.input(z.string()).quer
|
|
|
49
59
|
export type AppRouter = typeof appRouter;
|
|
50
60
|
```
|
|
51
61
|
|
|
52
|
-
|
|
62
|
+
4. Create a plugin from this code. `createTRPCNuxtClient` and the links are auto-imported. `useNuxtApp()` returns `$trpc`.
|
|
53
63
|
|
|
54
64
|
```ts
|
|
55
65
|
// app/plugins/trpc.ts
|
|
@@ -61,7 +71,7 @@ export default defineNuxtPlugin(() => {
|
|
|
61
71
|
});
|
|
62
72
|
```
|
|
63
73
|
|
|
64
|
-
|
|
74
|
+
5. Create a page from this code. The page shows `Hello world`.
|
|
65
75
|
|
|
66
76
|
```vue
|
|
67
77
|
<script setup lang="ts">
|
|
@@ -75,33 +85,18 @@ const { data } = await $trpc.greeting.useQuery(name);
|
|
|
75
85
|
</template>
|
|
76
86
|
```
|
|
77
87
|
|
|
78
|
-
## <a name="
|
|
79
|
-
|
|
80
|
-
We highly recommend you take a look at the [documentation](https://github.com/Esposter/Esposter/tree/main/openspec/specs/trpc-nuxt-module) to level up. It covers the design and the upstream `trpc-nuxt` issues this module answers.
|
|
81
|
-
|
|
82
|
-
### Key exports
|
|
88
|
+
## <a name="key-exports">🧩 Key Exports</a>
|
|
83
89
|
|
|
84
|
-
| Export
|
|
85
|
-
|
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
| `
|
|
90
|
-
| `trpc-nuxt-module/runtime/server/createTRPCEventHandler`
|
|
90
|
+
| Export | Description |
|
|
91
|
+
| ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------- |
|
|
92
|
+
| `createTRPCNuxtClient` | The typed client. Every procedure is decorated with `useQuery`, `useLazyQuery`, `useMutation` or `useSubscription`. |
|
|
93
|
+
| `getMutationKey`, `getQueryKey` | The `useAsyncData` keys that cache a procedure's data. Used by `useNuxtData` and `refreshNuxtData`. |
|
|
94
|
+
| `httpBatchLink`, `httpLink` | tRPC's links. They send through the request event during server rendering. |
|
|
95
|
+
| `trpc-nuxt-module` | The module. Its options are `router`, `createContext`, `endpoint` and `webSocket`. |
|
|
96
|
+
| `trpc-nuxt-module/runtime/server/createTRPCEventHandler` | The HTTP handler over tRPC's fetch adapter. For a Nitro app without Nuxt. |
|
|
97
|
+
| `trpc-nuxt-module/runtime/server/createTRPCWebSocketHandler` | The WebSocket handler over tRPC's `applyWSSHandler` and crossws. For a Nitro app without Nuxt. |
|
|
91
98
|
|
|
92
|
-
A WebSocket connection
|
|
93
|
-
|
|
94
|
-
### Commands
|
|
95
|
-
|
|
96
|
-
Run from `packages/trpc-nuxt-module/`:
|
|
97
|
-
|
|
98
|
-
```bash
|
|
99
|
-
pnpm build # compile to dist/
|
|
100
|
-
pnpm test # vitest watch mode (coverage is run from the repo root)
|
|
101
|
-
pnpm bench # the event handler against tRPC's own fetch adapter
|
|
102
|
-
pnpm lint:fix # auto-fix lint
|
|
103
|
-
pnpm typecheck # type check
|
|
104
|
-
```
|
|
99
|
+
A WebSocket connection calls the `trpc-nuxt-module:webSocket:open` Nitro runtime hook when it opens. It calls the `trpc-nuxt-module:webSocket:close` hook when it closes. Both hooks receive the connection's context, typed from your router.
|
|
105
100
|
|
|
106
101
|
## <a name="license">⚖️ License</a>
|
|
107
102
|
|
package/dist/index.js
CHANGED
|
@@ -5,11 +5,26 @@ import { getWebSocketHooksTypeTemplate } from "./services/getWebSocketHooksTypeT
|
|
|
5
5
|
import { addImports, addServerHandler, addServerTemplate, addTypeTemplate, defineNuxtModule, resolvePath } from "@nuxt/kit";
|
|
6
6
|
// src/module.ts
|
|
7
7
|
const CLIENT_IMPORTS = [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
{
|
|
9
|
+
from: "trpc-nuxt-module/runtime/client/createTRPCNuxtClient",
|
|
10
|
+
name: "createTRPCNuxtClient"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
from: "trpc-nuxt-module/runtime/client/getMutationKey",
|
|
14
|
+
name: "getMutationKey"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
from: "trpc-nuxt-module/runtime/client/getQueryKey",
|
|
18
|
+
name: "getQueryKey"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
from: "trpc-nuxt-module/runtime/client/httpBatchLink",
|
|
22
|
+
name: "httpBatchLink"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
from: "trpc-nuxt-module/runtime/client/httpLink",
|
|
26
|
+
name: "httpLink"
|
|
27
|
+
}
|
|
13
28
|
];
|
|
14
29
|
const EVENT_HANDLER_FILENAME = `#${MODULE_NAME}/eventHandler.mjs`;
|
|
15
30
|
const WEBSOCKET_HANDLER_FILENAME = `#${MODULE_NAME}/webSocketHandler.mjs`;
|
|
@@ -63,10 +78,7 @@ const trpcNuxtModule = defineNuxtModule({
|
|
|
63
78
|
nuxt: true
|
|
64
79
|
});
|
|
65
80
|
}
|
|
66
|
-
addImports(CLIENT_IMPORTS
|
|
67
|
-
from: `${MODULE_NAME}/runtime/client/${name}`,
|
|
68
|
-
name
|
|
69
|
-
})));
|
|
81
|
+
addImports([...CLIENT_IMPORTS]);
|
|
70
82
|
}
|
|
71
83
|
});
|
|
72
84
|
export { trpcNuxtModule as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// src/runtime/client/models/ConnectionStateSubscriptionStatus.ts
|
|
2
|
+
let ConnectionStateSubscriptionStatus = /* @__PURE__ */ function(ConnectionStateSubscriptionStatus) {
|
|
3
|
+
ConnectionStateSubscriptionStatus["Connecting"] = "connecting";
|
|
4
|
+
ConnectionStateSubscriptionStatus["Idle"] = "idle";
|
|
5
|
+
ConnectionStateSubscriptionStatus["Pending"] = "pending";
|
|
6
|
+
return ConnectionStateSubscriptionStatus;
|
|
7
|
+
}({});
|
|
8
|
+
export { ConnectionStateSubscriptionStatus };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ProcedureNode } from "./ProcedureNode.js";
|
|
2
1
|
import { DecoratedProcedure } from "./DecoratedProcedure.js";
|
|
2
|
+
import { ProcedureNode } from "./ProcedureNode.js";
|
|
3
3
|
import { AnyTRPCProcedure, AnyTRPCRootTypes, TRPCRouterRecord, inferProcedureInput, inferTransformedProcedureOutput } from "@trpc/server";
|
|
4
4
|
export type DecoratedRouterRecord<TRoot extends AnyTRPCRootTypes, TRecord extends TRPCRouterRecord> = ProcedureNode & { readonly [TKey in keyof TRecord]: TRecord[TKey] extends AnyTRPCProcedure ? DecoratedProcedure<TRecord[TKey]["_def"]["type"], {
|
|
5
5
|
errorShape: TRoot["errorShape"];
|
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
import { getProcedureKey } from "./getProcedureKey.js";
|
|
2
|
-
import {
|
|
3
|
-
import { useAsyncData } from "nuxt/app";
|
|
2
|
+
import { createError, useAsyncData } from "nuxt/app";
|
|
4
3
|
// src/runtime/client/services/useProcedureMutation.ts
|
|
5
4
|
const useProcedureMutation = (client, path, { mutationKey = getProcedureKey(path), trpc, ...options } = {}) => {
|
|
6
|
-
let
|
|
7
|
-
const asyncData = useAsyncData(mutationKey, (
|
|
8
|
-
...trpc,
|
|
9
|
-
signal: mergeSignals(signal, trpc?.signal)
|
|
10
|
-
}), {
|
|
5
|
+
let request = Promise.resolve();
|
|
6
|
+
const asyncData = useAsyncData(mutationKey, () => request, {
|
|
11
7
|
...options,
|
|
12
8
|
immediate: false,
|
|
13
9
|
server: false
|
|
14
10
|
});
|
|
15
11
|
return Object.assign(asyncData, { mutate: async (input) => {
|
|
16
|
-
|
|
12
|
+
const signals = [trpc?.signal, options.timeout === void 0 ? void 0 : AbortSignal.timeout(options.timeout)];
|
|
13
|
+
const ownRequest = client.mutation(path, input, {
|
|
14
|
+
...trpc,
|
|
15
|
+
signal: AbortSignal.any(signals.filter((signal) => signal !== void 0))
|
|
16
|
+
});
|
|
17
|
+
request = ownRequest;
|
|
17
18
|
await asyncData.execute();
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
const [outcome] = await Promise.allSettled([ownRequest]);
|
|
20
|
+
if (outcome.status === "rejected") throw createError(outcome.reason);
|
|
21
|
+
const transformed = options.transform ? await options.transform(outcome.value) : outcome.value;
|
|
22
|
+
if (!options.pick) return transformed;
|
|
23
|
+
const picked = {};
|
|
24
|
+
for (const key of options.pick) picked[key] = transformed[key];
|
|
25
|
+
return picked;
|
|
20
26
|
} });
|
|
21
27
|
};
|
|
22
28
|
export { useProcedureMutation };
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import "../models/ConnectionStateSubscriptionStatus.js";
|
|
1
2
|
import { SubscriptionStatus } from "../models/SubscriptionStatus.js";
|
|
2
3
|
import { getCurrentScope, onScopeDispose, shallowRef, toValue, watch } from "vue";
|
|
3
4
|
import { checkIsServer } from "@esposter/shared";
|
|
4
5
|
// src/runtime/client/services/useProcedureSubscription.ts
|
|
5
6
|
const ConnectionStateSubscriptionStatusMap = {
|
|
6
|
-
connecting: SubscriptionStatus.Connecting,
|
|
7
|
-
idle: SubscriptionStatus.Idle,
|
|
8
|
-
pending: SubscriptionStatus.Pending
|
|
7
|
+
["connecting"]: SubscriptionStatus.Connecting,
|
|
8
|
+
["idle"]: SubscriptionStatus.Idle,
|
|
9
|
+
["pending"]: SubscriptionStatus.Pending
|
|
9
10
|
};
|
|
10
11
|
const useProcedureSubscription = (client, path, input, { enabled = true, ...options } = {}) => {
|
|
11
12
|
const data = shallowRef();
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PeerWebSocketAdapter } from "./models/PeerWebSocketAdapter.js";
|
|
2
2
|
import { WebSocketServerAdapter } from "./models/WebSocketServerAdapter.js";
|
|
3
3
|
import { toIncomingMessage } from "./services/toIncomingMessage.js";
|
|
4
|
+
import { getResultAsync, noop } from "@esposter/shared";
|
|
4
5
|
import { applyWSSHandler } from "@trpc/server/adapters/ws";
|
|
5
6
|
// src/runtime/server/createTRPCWebSocketHandler.ts
|
|
6
7
|
const createTRPCWebSocketHandler = ({ createContext, onClose, onOpen, ...options }) => {
|
|
@@ -18,22 +19,24 @@ const createTRPCWebSocketHandler = ({ createContext, onClose, onOpen, ...options
|
|
|
18
19
|
const peerConnection = peerConnections.get(peer.id);
|
|
19
20
|
if (!peerConnection) return;
|
|
20
21
|
peerConnections.delete(peer.id);
|
|
21
|
-
peerConnection.client
|
|
22
|
-
await onClose?.({
|
|
23
|
-
context
|
|
22
|
+
PeerWebSocketAdapter.receiveClose(peerConnection.client, code, reason);
|
|
23
|
+
await peerConnection.context.match((context) => getResultAsync(() => Promise.resolve(onClose?.({
|
|
24
|
+
context,
|
|
24
25
|
peer
|
|
25
|
-
});
|
|
26
|
+
}))).match(noop, console.error), noop);
|
|
26
27
|
},
|
|
27
28
|
error: (peer, error) => {
|
|
28
|
-
peerConnections.get(peer.id)
|
|
29
|
+
const peerConnection = peerConnections.get(peer.id);
|
|
30
|
+
if (peerConnection) PeerWebSocketAdapter.receiveError(peerConnection.client, error);
|
|
29
31
|
},
|
|
30
32
|
message: (peer, message) => {
|
|
31
|
-
peerConnections.get(peer.id)
|
|
33
|
+
const peerConnection = peerConnections.get(peer.id);
|
|
34
|
+
if (peerConnection) PeerWebSocketAdapter.receiveMessage(peerConnection.client, message.text());
|
|
32
35
|
},
|
|
33
36
|
open: async (peer) => {
|
|
34
37
|
const client = new PeerWebSocketAdapter(peer);
|
|
35
38
|
const request = toIncomingMessage(peer);
|
|
36
|
-
const context = Promise.resolve(createContext({
|
|
39
|
+
const context = getResultAsync(() => Promise.resolve(createContext({
|
|
37
40
|
info: {
|
|
38
41
|
accept: null,
|
|
39
42
|
calls: [],
|
|
@@ -45,16 +48,16 @@ const createTRPCWebSocketHandler = ({ createContext, onClose, onOpen, ...options
|
|
|
45
48
|
},
|
|
46
49
|
req: request,
|
|
47
50
|
res: client
|
|
48
|
-
}));
|
|
51
|
+
})));
|
|
49
52
|
peerConnections.set(peer.id, {
|
|
50
53
|
client,
|
|
51
54
|
context
|
|
52
55
|
});
|
|
53
|
-
webSocketServer.
|
|
54
|
-
await onOpen?.({
|
|
55
|
-
context:
|
|
56
|
+
webSocketServer.emit("connection", client, request);
|
|
57
|
+
await context.andThen((value) => getResultAsync(() => Promise.resolve(onOpen?.({
|
|
58
|
+
context: value,
|
|
56
59
|
peer
|
|
57
|
-
});
|
|
60
|
+
})))).match(noop, console.error);
|
|
58
61
|
}
|
|
59
62
|
}
|
|
60
63
|
};
|
|
@@ -1,17 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
readonly CLOSING: number;
|
|
7
|
-
readonly CONNECTING: number;
|
|
8
|
-
readonly OPEN: number;
|
|
9
|
-
readyState: number;
|
|
10
|
-
constructor(peer: Peer);
|
|
11
|
-
close(code?: number, reason?: string): void;
|
|
12
|
-
receiveClose(code?: number, reason?: string): void;
|
|
13
|
-
receiveError(error: unknown): void;
|
|
14
|
-
receiveMessage(message: string): void;
|
|
15
|
-
send(data: string): void;
|
|
16
|
-
terminate(): void;
|
|
1
|
+
import { WebSocketClientAdapter } from "./WebSocketClientAdapter.js";
|
|
2
|
+
export declare class PeerWebSocketAdapter extends WebSocketClientAdapter {
|
|
3
|
+
static receiveClose(client: PeerWebSocketAdapter, code?: number, reason?: string): void;
|
|
4
|
+
static receiveError(client: PeerWebSocketAdapter, error: unknown): void;
|
|
5
|
+
static receiveMessage(client: PeerWebSocketAdapter, message: string): void;
|
|
17
6
|
}
|
|
@@ -1,36 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { WebSocketClientAdapter } from "./WebSocketClientAdapter.js";
|
|
2
2
|
// src/runtime/server/models/PeerWebSocketAdapter.ts
|
|
3
|
-
var PeerWebSocketAdapter = class extends
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
OPEN = WebSocket.OPEN;
|
|
8
|
-
readyState = WebSocket.OPEN;
|
|
9
|
-
#peer;
|
|
10
|
-
constructor(peer) {
|
|
11
|
-
super();
|
|
12
|
-
this.#peer = peer;
|
|
3
|
+
var PeerWebSocketAdapter = class extends WebSocketClientAdapter {
|
|
4
|
+
static receiveClose(client, code, reason) {
|
|
5
|
+
client.readyState = WebSocket.CLOSED;
|
|
6
|
+
client.emit("close", code, reason);
|
|
13
7
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.#peer.close(code, reason);
|
|
8
|
+
static receiveError(client, error) {
|
|
9
|
+
client.emit("error", error);
|
|
17
10
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
this.emit("close", code, reason);
|
|
21
|
-
}
|
|
22
|
-
receiveError(error) {
|
|
23
|
-
this.emit("error", error);
|
|
24
|
-
}
|
|
25
|
-
receiveMessage(message) {
|
|
26
|
-
this.emit("message", Buffer.from(message), false);
|
|
27
|
-
}
|
|
28
|
-
send(data) {
|
|
29
|
-
this.#peer.send(data);
|
|
30
|
-
}
|
|
31
|
-
terminate() {
|
|
32
|
-
this.readyState = WebSocket.CLOSING;
|
|
33
|
-
this.#peer.terminate();
|
|
11
|
+
static receiveMessage(client, message) {
|
|
12
|
+
client.emit("message", Buffer.from(message), false);
|
|
34
13
|
}
|
|
35
14
|
};
|
|
36
15
|
export { PeerWebSocketAdapter };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
export declare class WebSocketClientAdapter extends EventEmitter {
|
|
3
|
+
readonly close: (code?: number, reason?: string) => void;
|
|
4
|
+
readonly CLOSED: number;
|
|
5
|
+
readonly CLOSING: number;
|
|
6
|
+
readonly CONNECTING: number;
|
|
7
|
+
readonly OPEN: number;
|
|
8
|
+
readyState: number;
|
|
9
|
+
readonly send: (data: string) => void;
|
|
10
|
+
readonly terminate: () => void;
|
|
11
|
+
constructor(transport: {
|
|
12
|
+
close: (code?: number, reason?: string) => void;
|
|
13
|
+
send: (data: string) => void;
|
|
14
|
+
terminate: () => void;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
// src/runtime/server/models/WebSocketClientAdapter.ts
|
|
3
|
+
var WebSocketClientAdapter = class extends EventEmitter {
|
|
4
|
+
close;
|
|
5
|
+
CLOSED = WebSocket.CLOSED;
|
|
6
|
+
CLOSING = WebSocket.CLOSING;
|
|
7
|
+
CONNECTING = WebSocket.CONNECTING;
|
|
8
|
+
OPEN = WebSocket.OPEN;
|
|
9
|
+
readyState = WebSocket.OPEN;
|
|
10
|
+
send;
|
|
11
|
+
terminate;
|
|
12
|
+
constructor(transport) {
|
|
13
|
+
super();
|
|
14
|
+
this.close = (code, reason) => {
|
|
15
|
+
this.readyState = WebSocket.CLOSING;
|
|
16
|
+
transport.close(code, reason);
|
|
17
|
+
};
|
|
18
|
+
this.send = (data) => {
|
|
19
|
+
transport.send(data);
|
|
20
|
+
};
|
|
21
|
+
this.terminate = () => {
|
|
22
|
+
this.readyState = WebSocket.CLOSING;
|
|
23
|
+
transport.terminate();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export { WebSocketClientAdapter };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
|
-
import { IncomingMessage } from "node:http";
|
|
3
2
|
export declare class WebSocketServerAdapter<TClient extends EventEmitter> extends EventEmitter {
|
|
4
3
|
readonly clients: Set<TClient>;
|
|
5
|
-
|
|
4
|
+
constructor();
|
|
6
5
|
}
|
|
@@ -2,12 +2,14 @@ import { EventEmitter } from "node:events";
|
|
|
2
2
|
// src/runtime/server/models/WebSocketServerAdapter.ts
|
|
3
3
|
var WebSocketServerAdapter = class extends EventEmitter {
|
|
4
4
|
clients = /* @__PURE__ */ new Set();
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this.clients.
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
this.on("connection", (client) => {
|
|
8
|
+
this.clients.add(client);
|
|
9
|
+
client.once("close", () => {
|
|
10
|
+
this.clients.delete(client);
|
|
11
|
+
});
|
|
9
12
|
});
|
|
10
|
-
this.emit("connection", client, request);
|
|
11
13
|
}
|
|
12
14
|
};
|
|
13
15
|
export { WebSocketServerAdapter };
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { MODULE_NAME } from "../runtime/constants.js";
|
|
2
1
|
import { getImportStatement } from "./getImportStatement.js";
|
|
3
2
|
// src/services/getEventHandlerTemplate.ts
|
|
4
3
|
const getEventHandlerTemplate = (router, endpoint, createContext) => [
|
|
5
|
-
`import { createTRPCEventHandler } from "
|
|
4
|
+
`import { createTRPCEventHandler } from "trpc-nuxt-module/runtime/server/createTRPCEventHandler";`,
|
|
6
5
|
getImportStatement(router, "router"),
|
|
7
6
|
...createContext ? [getImportStatement(createContext, "createContext")] : [],
|
|
8
7
|
`export default createTRPCEventHandler({ ${createContext ? "createContext, " : ""}endpoint: ${JSON.stringify(endpoint)}, router });`
|
|
@@ -5,7 +5,7 @@ const getWebSocketHandlerTemplate = (router, { keepAlive }, createContext) => [
|
|
|
5
5
|
`import { useNitroHooks } from "nitro/app";`,
|
|
6
6
|
`import { defineWebSocketHandler } from "nitro/h3";`,
|
|
7
7
|
`import { WEBSOCKET_CLOSE_HOOK, WEBSOCKET_OPEN_HOOK } from "${MODULE_NAME}/runtime/constants";`,
|
|
8
|
-
`import { createTRPCWebSocketHandler } from "
|
|
8
|
+
`import { createTRPCWebSocketHandler } from "trpc-nuxt-module/runtime/server/createTRPCWebSocketHandler";`,
|
|
9
9
|
getImportStatement(router, "router"),
|
|
10
10
|
createContext ? getImportStatement(createContext, "createContext") : "const createContext = () => ({});",
|
|
11
11
|
`const nitroHooks = useNitroHooks();`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trpc-nuxt-module",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0",
|
|
4
4
|
"description": "tRPC for Nuxt — a Nuxt module that registers your router's HTTP and WebSocket handlers, fetches through the request event during SSR, and decorates the typed client with useQuery, useMutation and useSubscription over useAsyncData.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nitro",
|
|
@@ -38,33 +38,11 @@
|
|
|
38
38
|
"./runtime/client/getQueryKey": "./dist/runtime/client/getQueryKey.js",
|
|
39
39
|
"./runtime/client/httpBatchLink": "./dist/runtime/client/httpBatchLink.js",
|
|
40
40
|
"./runtime/client/httpLink": "./dist/runtime/client/httpLink.js",
|
|
41
|
-
"./runtime/client/models/DecoratedMutation": "./dist/runtime/client/models/DecoratedMutation.js",
|
|
42
|
-
"./runtime/client/models/DecoratedProcedure": "./dist/runtime/client/models/DecoratedProcedure.js",
|
|
43
|
-
"./runtime/client/models/DecoratedQuery": "./dist/runtime/client/models/DecoratedQuery.js",
|
|
44
|
-
"./runtime/client/models/DecoratedRouterRecord": "./dist/runtime/client/models/DecoratedRouterRecord.js",
|
|
45
|
-
"./runtime/client/models/DecoratedSubscription": "./dist/runtime/client/models/DecoratedSubscription.js",
|
|
46
|
-
"./runtime/client/models/KeysOf": "./dist/runtime/client/models/KeysOf.js",
|
|
47
|
-
"./runtime/client/models/PickFrom": "./dist/runtime/client/models/PickFrom.js",
|
|
48
|
-
"./runtime/client/models/ProcedureNode": "./dist/runtime/client/models/ProcedureNode.js",
|
|
49
|
-
"./runtime/client/models/ProcedurePath": "./dist/runtime/client/models/ProcedurePath.js",
|
|
50
|
-
"./runtime/client/models/QueryAsyncData": "./dist/runtime/client/models/QueryAsyncData.js",
|
|
51
|
-
"./runtime/client/models/QueryError": "./dist/runtime/client/models/QueryError.js",
|
|
52
|
-
"./runtime/client/models/SubscriptionStatus": "./dist/runtime/client/models/SubscriptionStatus.js",
|
|
53
|
-
"./runtime/client/models/TRPCNuxtClient": "./dist/runtime/client/models/TRPCNuxtClient.js",
|
|
54
|
-
"./runtime/client/models/UseMutationOptions": "./dist/runtime/client/models/UseMutationOptions.js",
|
|
55
|
-
"./runtime/client/models/UseMutationReturn": "./dist/runtime/client/models/UseMutationReturn.js",
|
|
56
|
-
"./runtime/client/models/UseQuery": "./dist/runtime/client/models/UseQuery.js",
|
|
57
|
-
"./runtime/client/models/UseQueryOptions": "./dist/runtime/client/models/UseQueryOptions.js",
|
|
58
|
-
"./runtime/client/models/UseSubscriptionOptions": "./dist/runtime/client/models/UseSubscriptionOptions.js",
|
|
59
|
-
"./runtime/client/models/UseSubscriptionReturn": "./dist/runtime/client/models/UseSubscriptionReturn.js",
|
|
60
41
|
"./runtime/constants": "./dist/runtime/constants.js",
|
|
61
42
|
"./runtime/server/createTRPCEventHandler": "./dist/runtime/server/createTRPCEventHandler.js",
|
|
62
43
|
"./runtime/server/createTRPCWebSocketHandler": "./dist/runtime/server/createTRPCWebSocketHandler.js",
|
|
63
|
-
"./runtime/server/models/PeerWebSocketAdapter": "./dist/runtime/server/models/PeerWebSocketAdapter.js",
|
|
64
|
-
"./runtime/server/models/TRPCEventHandlerOptions": "./dist/runtime/server/models/TRPCEventHandlerOptions.js",
|
|
65
44
|
"./runtime/server/models/TRPCWebSocketConnection": "./dist/runtime/server/models/TRPCWebSocketConnection.js",
|
|
66
|
-
"./runtime/server/models/
|
|
67
|
-
"./runtime/server/models/TRPCWebSocketHandlerOptions": "./dist/runtime/server/models/TRPCWebSocketHandlerOptions.js",
|
|
45
|
+
"./runtime/server/models/WebSocketClientAdapter": "./dist/runtime/server/models/WebSocketClientAdapter.js",
|
|
68
46
|
"./runtime/server/models/WebSocketServerAdapter": "./dist/runtime/server/models/WebSocketServerAdapter.js",
|
|
69
47
|
"./package.json": "./package.json"
|
|
70
48
|
},
|
|
@@ -77,33 +55,33 @@
|
|
|
77
55
|
"lint": "eslint .",
|
|
78
56
|
"lint:fix": "eslint --fix .",
|
|
79
57
|
"test": "vitest",
|
|
80
|
-
"typecheck": "
|
|
58
|
+
"typecheck": "tsrs --fast"
|
|
81
59
|
},
|
|
82
60
|
"dependencies": {
|
|
83
|
-
"@esposter/shared": "4.
|
|
61
|
+
"@esposter/shared": "4.5.0",
|
|
84
62
|
"@nuxt/kit": "npm:@nuxt/kit-nightly@5x",
|
|
85
63
|
"@nuxt/schema": "npm:@nuxt/schema-nightly@5x",
|
|
86
64
|
"ohash": "^2.0.12",
|
|
87
65
|
"type-fest": "^5.10.0"
|
|
88
66
|
},
|
|
89
67
|
"devDependencies": {
|
|
90
|
-
"@esposter/configuration": "4.
|
|
91
|
-
"@esposter/shared-node": "4.
|
|
68
|
+
"@esposter/configuration": "4.5.0",
|
|
69
|
+
"@esposter/shared-node": "4.5.0",
|
|
92
70
|
"@nuxt/nitro-server": "npm:@nuxt/nitro-server-nightly@5x",
|
|
93
71
|
"@nuxt/test-utils": "^4.3.4",
|
|
94
|
-
"@types/node": "^26.6.
|
|
72
|
+
"@types/node": "^26.6.6",
|
|
95
73
|
"happy-dom": "^21.0.4",
|
|
96
74
|
"superjson": "^2.2.6",
|
|
97
|
-
"tsdown": "^0.23.
|
|
75
|
+
"tsdown": "^0.23.5",
|
|
98
76
|
"zod": "^4.6.7"
|
|
99
77
|
},
|
|
100
78
|
"peerDependencies": {
|
|
101
|
-
"@trpc/client": "^11.
|
|
102
|
-
"@trpc/server": "^11.
|
|
103
|
-
"crossws": "^0.4.
|
|
104
|
-
"nitro": "npm:nitro-nightly@
|
|
79
|
+
"@trpc/client": "^11.24.0",
|
|
80
|
+
"@trpc/server": "^11.24.0",
|
|
81
|
+
"crossws": "^0.4.17",
|
|
82
|
+
"nitro": "npm:nitro-nightly@latest",
|
|
105
83
|
"nuxt": "npm:nuxt-nightly@5x",
|
|
106
84
|
"vue": "^3.5.44"
|
|
107
85
|
},
|
|
108
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "e30c001f3b4b15693e75fb53092d62a97ade4d2b"
|
|
109
87
|
}
|