trpc-nuxt-module 4.4.0 → 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.
Files changed (98) hide show
  1. package/README.md +21 -13
  2. package/dist/index.d.ts +9 -0
  3. package/dist/index.js +84 -0
  4. package/dist/models/ExportReference.d.ts +4 -0
  5. package/dist/models/ModuleOptions.d.ts +8 -0
  6. package/dist/models/TrpcServerFetch.d.ts +2 -0
  7. package/dist/models/WebSocketOptions.d.ts +6 -0
  8. package/dist/runtime/client/createTRPCNuxtClient.d.ts +4 -0
  9. package/dist/runtime/client/createTRPCNuxtClient.js +28 -0
  10. package/dist/runtime/client/getMutationKey.d.ts +2 -0
  11. package/dist/runtime/client/getMutationKey.js +4 -0
  12. package/dist/runtime/client/getQueryKey.d.ts +2 -0
  13. package/dist/runtime/client/getQueryKey.js +4 -0
  14. package/dist/runtime/client/httpBatchLink.d.ts +3 -0
  15. package/dist/runtime/client/httpBatchLink.js +8 -0
  16. package/dist/runtime/client/httpLink.d.ts +3 -0
  17. package/dist/runtime/client/httpLink.js +8 -0
  18. package/dist/runtime/client/models/ConnectionStateSubscriptionStatus.d.ts +5 -0
  19. package/dist/runtime/client/models/ConnectionStateSubscriptionStatus.js +8 -0
  20. package/dist/runtime/client/models/DecoratedMutation.d.ts +9 -0
  21. package/dist/runtime/client/models/DecoratedMutation.js +1 -0
  22. package/dist/runtime/client/models/DecoratedProcedure.d.ts +6 -0
  23. package/dist/runtime/client/models/DecoratedProcedure.js +1 -0
  24. package/dist/runtime/client/models/DecoratedQuery.d.ts +8 -0
  25. package/dist/runtime/client/models/DecoratedQuery.js +1 -0
  26. package/dist/runtime/client/models/DecoratedRouterRecord.d.ts +9 -0
  27. package/dist/runtime/client/models/DecoratedRouterRecord.js +1 -0
  28. package/dist/runtime/client/models/DecoratedSubscription.d.ts +8 -0
  29. package/dist/runtime/client/models/DecoratedSubscription.js +1 -0
  30. package/dist/runtime/client/models/KeysOf.d.ts +1 -0
  31. package/dist/runtime/client/models/KeysOf.js +1 -0
  32. package/dist/runtime/client/models/PickFrom.d.ts +1 -0
  33. package/dist/runtime/client/models/PickFrom.js +1 -0
  34. package/dist/runtime/client/models/ProcedureNode.d.ts +4 -0
  35. package/dist/runtime/client/models/ProcedureNode.js +1 -0
  36. package/dist/runtime/client/models/ProcedurePath.d.ts +3 -0
  37. package/dist/runtime/client/models/ProcedurePath.js +1 -0
  38. package/dist/runtime/client/models/QueryAsyncData.d.ts +6 -0
  39. package/dist/runtime/client/models/QueryAsyncData.js +1 -0
  40. package/dist/runtime/client/models/QueryError.d.ts +3 -0
  41. package/dist/runtime/client/models/QueryError.js +1 -0
  42. package/dist/runtime/client/models/SubscriptionStatus.d.ts +7 -0
  43. package/dist/runtime/client/models/SubscriptionStatus.js +8 -0
  44. package/dist/runtime/client/models/TRPCNuxtClient.d.ts +3 -0
  45. package/dist/runtime/client/models/TRPCNuxtClient.js +1 -0
  46. package/dist/runtime/client/models/UseMutationOptions.d.ts +8 -0
  47. package/dist/runtime/client/models/UseMutationOptions.js +1 -0
  48. package/dist/runtime/client/models/UseMutationReturn.d.ts +7 -0
  49. package/dist/runtime/client/models/UseMutationReturn.js +1 -0
  50. package/dist/runtime/client/models/UseQuery.d.ts +6 -0
  51. package/dist/runtime/client/models/UseQuery.js +1 -0
  52. package/dist/runtime/client/models/UseQueryOptions.d.ts +9 -0
  53. package/dist/runtime/client/models/UseQueryOptions.js +1 -0
  54. package/dist/runtime/client/models/UseSubscriptionOptions.d.ts +5 -0
  55. package/dist/runtime/client/models/UseSubscriptionOptions.js +1 -0
  56. package/dist/runtime/client/models/UseSubscriptionReturn.d.ts +8 -0
  57. package/dist/runtime/client/models/UseSubscriptionReturn.js +1 -0
  58. package/dist/runtime/client/services/getLinkFetch.d.ts +2 -0
  59. package/dist/runtime/client/services/getLinkFetch.js +24 -0
  60. package/dist/runtime/client/services/getProcedureKey.d.ts +1 -0
  61. package/dist/runtime/client/services/getProcedureKey.js +4 -0
  62. package/dist/runtime/client/services/mergeSignals.d.ts +1 -0
  63. package/dist/runtime/client/services/mergeSignals.js +3 -0
  64. package/dist/runtime/client/services/useProcedureMutation.d.ts +6 -0
  65. package/dist/runtime/client/services/useProcedureMutation.js +28 -0
  66. package/dist/runtime/client/services/useProcedureQuery.d.ts +7 -0
  67. package/dist/runtime/client/services/useProcedureQuery.js +13 -0
  68. package/dist/runtime/client/services/useProcedureSubscription.d.ts +6 -0
  69. package/dist/runtime/client/services/useProcedureSubscription.js +68 -0
  70. package/dist/runtime/client/services/useTrpcServerFetch.d.ts +2 -0
  71. package/dist/runtime/client/services/useTrpcServerFetch.js +7 -0
  72. package/dist/runtime/constants.d.ts +4 -0
  73. package/dist/runtime/constants.js +6 -0
  74. package/dist/runtime/server/createTRPCEventHandler.d.ts +4 -0
  75. package/dist/runtime/server/createTRPCEventHandler.js +30 -0
  76. package/dist/runtime/server/createTRPCWebSocketHandler.d.ts +4 -0
  77. package/dist/runtime/server/createTRPCWebSocketHandler.js +65 -0
  78. package/dist/runtime/server/models/PeerWebSocketAdapter.d.ts +6 -0
  79. package/dist/runtime/server/models/PeerWebSocketAdapter.js +15 -0
  80. package/dist/runtime/server/models/TRPCEventHandlerOptions.d.ts +9 -0
  81. package/dist/runtime/server/models/TRPCEventHandlerOptions.js +1 -0
  82. package/dist/runtime/server/models/TRPCWebSocketConnection.d.ts +5 -0
  83. package/dist/runtime/server/models/TRPCWebSocketConnection.js +1 -0
  84. package/dist/runtime/server/models/TRPCWebSocketHandler.d.ts +5 -0
  85. package/dist/runtime/server/models/TRPCWebSocketHandler.js +1 -0
  86. package/dist/runtime/server/models/TRPCWebSocketHandlerOptions.d.ts +11 -0
  87. package/dist/runtime/server/models/TRPCWebSocketHandlerOptions.js +1 -0
  88. package/dist/runtime/server/models/WebSocketClientAdapter.d.ts +16 -0
  89. package/dist/runtime/server/models/WebSocketClientAdapter.js +27 -0
  90. package/dist/runtime/server/models/WebSocketServerAdapter.d.ts +5 -0
  91. package/dist/runtime/server/models/WebSocketServerAdapter.js +15 -0
  92. package/dist/runtime/server/services/toIncomingMessage.d.ts +3 -0
  93. package/dist/runtime/server/services/toIncomingMessage.js +11 -0
  94. package/dist/services/getEventHandlerTemplate.js +9 -0
  95. package/dist/services/getImportStatement.js +3 -0
  96. package/dist/services/getWebSocketHandlerTemplate.js +22 -0
  97. package/dist/services/getWebSocketHooksTypeTemplate.js +17 -0
  98. package/package.json +9 -8
package/README.md CHANGED
@@ -5,7 +5,11 @@
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): a Nuxt module that registers your router's HTTP handler — and, when you ask for it, a WebSocket handler over Nitro's own WebSockets — fetches through the request event during server rendering so a call never leaves the process, and decorates the typed client with `useQuery`, `useLazyQuery`, `useMutation` and `useSubscription`, each a composable over Nuxt's own `useAsyncData`.
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`.
9
13
 
10
14
  Design notes live in [the specs](https://github.com/Esposter/Esposter/tree/main/openspec/specs/trpc-nuxt-module).
11
15
 
@@ -19,11 +23,13 @@ Design notes live in [the specs](https://github.com/Esposter/Esposter/tree/main/
19
23
 
20
24
  ## <a name="getting-started">🚀 Getting Started</a>
21
25
 
26
+ 1. Install the packages.
27
+
22
28
  ```bash
23
29
  pnpm i trpc-nuxt-module @trpc/server @trpc/client
24
30
  ```
25
31
 
26
- Register the module with the file and export name of your router and your context factory:
32
+ 2. Register the module in `nuxt.config.ts`. Give the file and the export name of your router and of your context factory.
27
33
 
28
34
  ```ts
29
35
  // nuxt.config.ts
@@ -39,6 +45,8 @@ export default defineNuxtConfig({
39
45
  });
40
46
  ```
41
47
 
48
+ 3. Define the router in the file that the `router` option names.
49
+
42
50
  ```ts
43
51
  // server/trpc/router.ts
44
52
  import { initTRPC } from "@trpc/server";
@@ -51,7 +59,7 @@ export const appRouter = t.router({ greeting: t.procedure.input(z.string()).quer
51
59
  export type AppRouter = typeof appRouter;
52
60
  ```
53
61
 
54
- Provide a client from a plugin — `createTRPCNuxtClient` and the links are auto-imported:
62
+ 4. Create a plugin from this code. `createTRPCNuxtClient` and the links are auto-imported. `useNuxtApp()` returns `$trpc`.
55
63
 
56
64
  ```ts
57
65
  // app/plugins/trpc.ts
@@ -63,7 +71,7 @@ export default defineNuxtPlugin(() => {
63
71
  });
64
72
  ```
65
73
 
66
- And call a procedure from a page:
74
+ 5. Create a page from this code. The page shows `Hello world`.
67
75
 
68
76
  ```vue
69
77
  <script setup lang="ts">
@@ -79,16 +87,16 @@ const { data } = await $trpc.greeting.useQuery(name);
79
87
 
80
88
  ## <a name="key-exports">🧩 Key Exports</a>
81
89
 
82
- | Export | Description |
83
- | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- |
84
- | `createTRPCNuxtClient` | The typed client, every procedure decorated with `useQuery`, `useLazyQuery`, `useMutation` or `useSubscription` |
85
- | `getMutationKey`, `getQueryKey` | The `useAsyncData` keys a procedure's data is cached under, for `useNuxtData` and `refreshNuxtData` |
86
- | `httpBatchLink`, `httpLink` | tRPC's links, sending through the request event during server rendering |
87
- | `trpc-nuxt-module` | The module — `router`, `createContext`, `endpoint` and `webSocket` options |
88
- | `trpc-nuxt-module/runtime/server/createTRPCEventHandler` | The HTTP handler over tRPC's fetch adapter, for a Nitro app without Nuxt |
89
- | `trpc-nuxt-module/runtime/server/createTRPCWebSocketHandler` | The WebSocket handler over tRPC's `applyWSSHandler` and crossws, for a Nitro app without Nuxt |
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. |
90
98
 
91
- A WebSocket connection's open and close call the `trpc-nuxt-module:webSocket:open` and `trpc-nuxt-module:webSocket:close` Nitro runtime hooks with its context, typed off your router.
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.
92
100
 
93
101
  ## <a name="license">⚖️ License</a>
94
102
 
@@ -0,0 +1,9 @@
1
+ import { ModuleOptions } from "./models/ModuleOptions.js";
2
+ import { NuxtModule } from "@nuxt/schema";
3
+ declare module "@nuxt/schema" {
4
+ interface NuxtConfig {
5
+ trpc?: Partial<ModuleOptions>;
6
+ }
7
+ }
8
+ declare const trpcNuxtModule: NuxtModule<ModuleOptions>;
9
+ export { type ModuleOptions, trpcNuxtModule as default };
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ import { DEFAULT_ENDPOINT, MODULE_NAME } from "./runtime/constants.js";
2
+ import { getEventHandlerTemplate } from "./services/getEventHandlerTemplate.js";
3
+ import { getWebSocketHandlerTemplate } from "./services/getWebSocketHandlerTemplate.js";
4
+ import { getWebSocketHooksTypeTemplate } from "./services/getWebSocketHooksTypeTemplate.js";
5
+ import { addImports, addServerHandler, addServerTemplate, addTypeTemplate, defineNuxtModule, resolvePath } from "@nuxt/kit";
6
+ // src/module.ts
7
+ const CLIENT_IMPORTS = [
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
+ }
28
+ ];
29
+ const EVENT_HANDLER_FILENAME = `#${MODULE_NAME}/eventHandler.mjs`;
30
+ const WEBSOCKET_HANDLER_FILENAME = `#${MODULE_NAME}/webSocketHandler.mjs`;
31
+ const trpcNuxtModule = defineNuxtModule({
32
+ defaults: { endpoint: DEFAULT_ENDPOINT },
33
+ meta: {
34
+ compatibility: { nuxt: ">=5.0.0-0" },
35
+ configKey: "trpc",
36
+ name: MODULE_NAME
37
+ },
38
+ async setup({ createContext, endpoint, router, webSocket }, nuxt) {
39
+ const resolvedRouter = {
40
+ ...router,
41
+ from: await resolvePath(router.from)
42
+ };
43
+ const resolvedCreateContext = createContext && {
44
+ ...createContext,
45
+ from: await resolvePath(createContext.from)
46
+ };
47
+ const routedEndpoint = `${nuxt.options.app.baseURL.replace(/\/$/u, "")}${endpoint}`;
48
+ addServerTemplate({
49
+ filename: EVENT_HANDLER_FILENAME,
50
+ getContents: () => getEventHandlerTemplate(resolvedRouter, routedEndpoint, resolvedCreateContext)
51
+ });
52
+ addServerHandler({
53
+ handler: EVENT_HANDLER_FILENAME,
54
+ route: `${endpoint}/**`
55
+ });
56
+ if (webSocket) {
57
+ nuxt.options.nitro.features = {
58
+ ...nuxt.options.nitro.features,
59
+ websocket: true
60
+ };
61
+ addServerTemplate({
62
+ filename: WEBSOCKET_HANDLER_FILENAME,
63
+ getContents: () => getWebSocketHandlerTemplate(resolvedRouter, webSocket, resolvedCreateContext)
64
+ });
65
+ addServerHandler({
66
+ handler: WEBSOCKET_HANDLER_FILENAME,
67
+ lazy: true,
68
+ route: webSocket.endpoint
69
+ });
70
+ addTypeTemplate({
71
+ filename: `types/${MODULE_NAME}.d.ts`,
72
+ getContents: () => getWebSocketHooksTypeTemplate({
73
+ ...resolvedRouter,
74
+ from: resolvedRouter.from.replace(/\.[cm]?ts$/u, "")
75
+ })
76
+ }, {
77
+ nitro: true,
78
+ nuxt: true
79
+ });
80
+ }
81
+ addImports([...CLIENT_IMPORTS]);
82
+ }
83
+ });
84
+ export { trpcNuxtModule as default };
@@ -0,0 +1,4 @@
1
+ export interface ExportReference {
2
+ from: string;
3
+ name: string;
4
+ }
@@ -0,0 +1,8 @@
1
+ import { ExportReference } from "./ExportReference.js";
2
+ import { WebSocketOptions } from "./WebSocketOptions.js";
3
+ export interface ModuleOptions {
4
+ createContext?: ExportReference;
5
+ endpoint: string;
6
+ router: ExportReference;
7
+ webSocket?: WebSocketOptions;
8
+ }
@@ -0,0 +1,2 @@
1
+ import { serverFetch } from "nitro/app";
2
+ export type TrpcServerFetch = typeof serverFetch;
@@ -0,0 +1,6 @@
1
+ import { AnyTRPCRouter } from "@trpc/server";
2
+ import { WSSHandlerOptions } from "@trpc/server/adapters/ws";
3
+ export interface WebSocketOptions {
4
+ endpoint: string;
5
+ keepAlive?: WSSHandlerOptions<AnyTRPCRouter>["keepAlive"];
6
+ }
@@ -0,0 +1,4 @@
1
+ import { TRPCNuxtClient } from "./models/TRPCNuxtClient.js";
2
+ import { CreateTRPCClientOptions } from "@trpc/client";
3
+ import { AnyTRPCRouter } from "@trpc/server";
4
+ export declare const createTRPCNuxtClient: <TRouter extends AnyTRPCRouter>(options: CreateTRPCClientOptions<TRouter>) => TRPCNuxtClient<TRouter>;
@@ -0,0 +1,28 @@
1
+ import { useProcedureMutation } from "./services/useProcedureMutation.js";
2
+ import { useProcedureQuery } from "./services/useProcedureQuery.js";
3
+ import { useProcedureSubscription } from "./services/useProcedureSubscription.js";
4
+ import { createTRPCUntypedClient } from "@trpc/client";
5
+ import { createTRPCRecursiveProxy } from "@trpc/server";
6
+ // src/runtime/client/createTRPCNuxtClient.ts
7
+ const createTRPCNuxtClient = (options) => {
8
+ const client = createTRPCUntypedClient(options);
9
+ return createTRPCRecursiveProxy(({ args: [first, second], path }) => {
10
+ const procedurePath = path.slice(0, -1);
11
+ const procedure = procedurePath.join(".");
12
+ switch (path.at(-1)) {
13
+ case "_def": return { path: procedurePath };
14
+ case "mutate": return client.mutation(procedure, first, second);
15
+ case "query": return client.query(procedure, first, second);
16
+ case "subscribe": return client.subscription(procedure, first, second);
17
+ case "useLazyQuery": return useProcedureQuery(client, procedure, first, {
18
+ ...second,
19
+ lazy: true
20
+ });
21
+ case "useMutation": return useProcedureMutation(client, procedure, first);
22
+ case "useQuery": return useProcedureQuery(client, procedure, first, second);
23
+ case "useSubscription": return useProcedureSubscription(client, procedure, first, second);
24
+ default: throw new TypeError(`"${path.join(".")}" is not a procedure call`);
25
+ }
26
+ });
27
+ };
28
+ export { createTRPCNuxtClient };
@@ -0,0 +1,2 @@
1
+ import { ProcedureNode } from "./models/ProcedureNode.js";
2
+ export declare const getMutationKey: (procedure: ProcedureNode) => string;
@@ -0,0 +1,4 @@
1
+ import { getProcedureKey } from "./services/getProcedureKey.js";
2
+ // src/runtime/client/getMutationKey.ts
3
+ const getMutationKey = (procedure) => getProcedureKey(procedure._def().path.join("."));
4
+ export { getMutationKey };
@@ -0,0 +1,2 @@
1
+ import { ProcedureNode } from "./models/ProcedureNode.js";
2
+ export declare const getQueryKey: (procedure: ProcedureNode, input?: unknown) => string;
@@ -0,0 +1,4 @@
1
+ import { getProcedureKey } from "./services/getProcedureKey.js";
2
+ // src/runtime/client/getQueryKey.ts
3
+ const getQueryKey = (procedure, input) => getProcedureKey(procedure._def().path.join("."), input);
4
+ export { getQueryKey };
@@ -0,0 +1,3 @@
1
+ import { HTTPBatchLinkOptions, TRPCLink } from "@trpc/client";
2
+ import { AnyTRPCRouter } from "@trpc/server";
3
+ export declare const httpBatchLink: <TRouter extends AnyTRPCRouter>(options: HTTPBatchLinkOptions<TRouter["_def"]["_config"]["$types"]>) => TRPCLink<TRouter>;
@@ -0,0 +1,8 @@
1
+ import { getLinkFetch } from "./services/getLinkFetch.js";
2
+ import { httpBatchLink as httpBatchLink$1 } from "@trpc/client";
3
+ // src/runtime/client/httpBatchLink.ts
4
+ const httpBatchLink = (options) => httpBatchLink$1({
5
+ fetch: getLinkFetch(),
6
+ ...options
7
+ });
8
+ export { httpBatchLink };
@@ -0,0 +1,3 @@
1
+ import { HTTPLinkOptions, TRPCLink } from "@trpc/client";
2
+ import { AnyTRPCRouter } from "@trpc/server";
3
+ export declare const httpLink: <TRouter extends AnyTRPCRouter>(options: HTTPLinkOptions<TRouter["_def"]["_config"]["$types"]>) => TRPCLink<TRouter>;
@@ -0,0 +1,8 @@
1
+ import { getLinkFetch } from "./services/getLinkFetch.js";
2
+ import { httpLink as httpLink$1 } from "@trpc/client";
3
+ // src/runtime/client/httpLink.ts
4
+ const httpLink = (options) => httpLink$1({
5
+ fetch: getLinkFetch(),
6
+ ...options
7
+ });
8
+ export { httpLink };
@@ -0,0 +1,5 @@
1
+ export declare enum ConnectionStateSubscriptionStatus {
2
+ Connecting = "connecting",
3
+ Idle = "idle",
4
+ Pending = "pending"
5
+ }
@@ -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 };
@@ -0,0 +1,9 @@
1
+ import { KeysOf } from "./KeysOf.js";
2
+ import { ProcedureNode } from "./ProcedureNode.js";
3
+ import { UseMutationOptions } from "./UseMutationOptions.js";
4
+ import { UseMutationReturn } from "./UseMutationReturn.js";
5
+ import { Resolver, TRPCResolverDef } from "@trpc/client";
6
+ export interface DecoratedMutation<TDefinition extends TRPCResolverDef> extends ProcedureNode {
7
+ mutate: Resolver<TDefinition>;
8
+ useMutation: <TData = TDefinition["output"], TPickKeys extends KeysOf<TData> = KeysOf<TData>, TDefault = undefined>(options?: UseMutationOptions<TDefinition, TData, TPickKeys, TDefault>) => UseMutationReturn<TDefinition, TData, TPickKeys, TDefault>;
9
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { DecoratedMutation } from "./DecoratedMutation.js";
2
+ import { DecoratedQuery } from "./DecoratedQuery.js";
3
+ import { DecoratedSubscription } from "./DecoratedSubscription.js";
4
+ import { TRPCResolverDef } from "@trpc/client";
5
+ import { TRPCProcedureType } from "@trpc/server";
6
+ export type DecoratedProcedure<TType extends TRPCProcedureType, TDefinition extends TRPCResolverDef> = TType extends "query" ? DecoratedQuery<TDefinition> : TType extends "mutation" ? DecoratedMutation<TDefinition> : DecoratedSubscription<TDefinition>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { ProcedureNode } from "./ProcedureNode.js";
2
+ import { UseQuery } from "./UseQuery.js";
3
+ import { Resolver, TRPCResolverDef } from "@trpc/client";
4
+ export interface DecoratedQuery<TDefinition extends TRPCResolverDef> extends ProcedureNode {
5
+ query: Resolver<TDefinition>;
6
+ useLazyQuery: UseQuery<TDefinition>;
7
+ useQuery: UseQuery<TDefinition>;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { DecoratedProcedure } from "./DecoratedProcedure.js";
2
+ import { ProcedureNode } from "./ProcedureNode.js";
3
+ import { AnyTRPCProcedure, AnyTRPCRootTypes, TRPCRouterRecord, inferProcedureInput, inferTransformedProcedureOutput } from "@trpc/server";
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
+ errorShape: TRoot["errorShape"];
6
+ input: inferProcedureInput<TRecord[TKey]>;
7
+ output: inferTransformedProcedureOutput<TRoot, TRecord[TKey]>;
8
+ transformer: TRoot["transformer"];
9
+ }> : TRecord[TKey] extends TRPCRouterRecord ? DecoratedRouterRecord<TRoot, TRecord[TKey]> : never; };
@@ -0,0 +1,8 @@
1
+ import { UseSubscriptionOptions } from "./UseSubscriptionOptions.js";
2
+ import { UseSubscriptionReturn } from "./UseSubscriptionReturn.js";
3
+ import { MaybeRefOrGetter } from "vue";
4
+ import { SubscriptionResolver, TRPCClientError, TRPCResolverDef } from "@trpc/client";
5
+ export interface DecoratedSubscription<TDefinition extends TRPCResolverDef> {
6
+ subscribe: SubscriptionResolver<TDefinition>;
7
+ useSubscription: (input: MaybeRefOrGetter<TDefinition["input"]>, options?: UseSubscriptionOptions<TDefinition>) => UseSubscriptionReturn<Parameters<NonNullable<UseSubscriptionOptions<TDefinition>["onData"]>>[0], TRPCClientError<TDefinition>>;
8
+ }
@@ -0,0 +1 @@
1
+ export type KeysOf<T> = (T extends T ? (keyof T extends string ? keyof T : never) : never)[];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export type PickFrom<T, TKeys extends string[]> = T extends unknown[] ? T : T extends Record<string, unknown> ? keyof T extends TKeys[number] ? T : TKeys[number] extends never ? T : Pick<T, TKeys[number]> : T;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { ProcedurePath } from "./ProcedurePath.js";
2
+ export interface ProcedureNode {
3
+ readonly _def: () => ProcedurePath;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export interface ProcedurePath {
2
+ path: string[];
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { KeysOf } from "./KeysOf.js";
2
+ import { PickFrom } from "./PickFrom.js";
3
+ import { QueryError } from "./QueryError.js";
4
+ import { AsyncData } from "nuxt/app";
5
+ import { TRPCResolverDef } from "@trpc/client";
6
+ export type QueryAsyncData<TDefinition extends TRPCResolverDef, TData, TPickKeys extends KeysOf<TData>, TDefault> = AsyncData<PickFrom<TData, TPickKeys> | TDefault, QueryError<TDefinition> | undefined>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { NuxtError } from "nuxt/app";
2
+ import { TRPCClientErrorLike, TRPCResolverDef } from "@trpc/client";
3
+ export type QueryError<TDefinition extends TRPCResolverDef> = NuxtError<TRPCClientErrorLike<TDefinition>["data"]>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export declare const SubscriptionStatus: {
2
+ readonly Connecting: "connecting";
3
+ readonly Error: "error";
4
+ readonly Idle: "idle";
5
+ readonly Pending: "pending";
6
+ };
7
+ export type SubscriptionStatus = (typeof SubscriptionStatus)[keyof typeof SubscriptionStatus];
@@ -0,0 +1,8 @@
1
+ // src/runtime/client/models/SubscriptionStatus.ts
2
+ const SubscriptionStatus = {
3
+ Connecting: "connecting",
4
+ Error: "error",
5
+ Idle: "idle",
6
+ Pending: "pending"
7
+ };
8
+ export { SubscriptionStatus };
@@ -0,0 +1,3 @@
1
+ import { DecoratedRouterRecord } from "./DecoratedRouterRecord.js";
2
+ import { AnyTRPCRouter } from "@trpc/server";
3
+ export type TRPCNuxtClient<TRouter extends AnyTRPCRouter> = DecoratedRouterRecord<TRouter["_def"]["_config"]["$types"], TRouter["_def"]["record"]>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { KeysOf } from "./KeysOf.js";
2
+ import { AsyncDataOptions } from "nuxt/app";
3
+ import { TRPCRequestOptions, TRPCResolverDef } from "@trpc/client";
4
+ import { Except } from "type-fest";
5
+ export type UseMutationOptions<TDefinition extends TRPCResolverDef, TData = TDefinition["output"], TPickKeys extends KeysOf<TData> = KeysOf<TData>, TDefault = undefined> = Except<AsyncDataOptions<TDefinition["output"], TData, TPickKeys, TDefault>, "enabled" | "immediate" | "lazy" | "server" | "watch"> & {
6
+ mutationKey?: string;
7
+ trpc?: TRPCRequestOptions;
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import { KeysOf } from "./KeysOf.js";
2
+ import { PickFrom } from "./PickFrom.js";
3
+ import { QueryAsyncData } from "./QueryAsyncData.js";
4
+ import { TRPCResolverDef } from "@trpc/client";
5
+ export type UseMutationReturn<TDefinition extends TRPCResolverDef, TData, TPickKeys extends KeysOf<TData>, TDefault> = QueryAsyncData<TDefinition, TData, TPickKeys, TDefault> & {
6
+ mutate: (input: TDefinition["input"]) => Promise<PickFrom<TData, TPickKeys> | TDefault>;
7
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { KeysOf } from "./KeysOf.js";
2
+ import { QueryAsyncData } from "./QueryAsyncData.js";
3
+ import { UseQueryOptions } from "./UseQueryOptions.js";
4
+ import { MaybeRefOrGetter } from "vue";
5
+ import { TRPCResolverDef } from "@trpc/client";
6
+ export type UseQuery<TDefinition extends TRPCResolverDef> = <TData = TDefinition["output"], TPickKeys extends KeysOf<TData> = KeysOf<TData>, TDefault = undefined>(input: MaybeRefOrGetter<TDefinition["input"]>, options?: UseQueryOptions<TDefinition, TData, TPickKeys, TDefault>) => QueryAsyncData<TDefinition, TData, TPickKeys, TDefault>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { KeysOf } from "./KeysOf.js";
2
+ import { AsyncDataOptions } from "nuxt/app";
3
+ import { TRPCRequestOptions, TRPCResolverDef } from "@trpc/client";
4
+ import { Except } from "type-fest";
5
+ export type UseQueryOptions<TDefinition extends TRPCResolverDef, TData = TDefinition["output"], TPickKeys extends KeysOf<TData> = KeysOf<TData>, TDefault = undefined> = Except<AsyncDataOptions<TDefinition["output"], TData, TPickKeys, TDefault>, "watch"> & {
6
+ queryKey?: string;
7
+ trpc?: TRPCRequestOptions;
8
+ watch?: false | NonNullable<AsyncDataOptions<TDefinition["output"], TData, TPickKeys, TDefault>["watch"]>;
9
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { MaybeRefOrGetter } from "vue";
2
+ import { SubscriptionResolver, TRPCResolverDef } from "@trpc/client";
3
+ export type UseSubscriptionOptions<TDefinition extends TRPCResolverDef> = Parameters<SubscriptionResolver<TDefinition>>[1] & {
4
+ enabled?: MaybeRefOrGetter<boolean>;
5
+ };
@@ -0,0 +1,8 @@
1
+ import { SubscriptionStatus } from "./SubscriptionStatus.js";
2
+ import { ShallowRef } from "vue";
3
+ export interface UseSubscriptionReturn<TData, TError> {
4
+ data: ShallowRef<TData | undefined>;
5
+ error: ShallowRef<TError | undefined>;
6
+ reset: () => void;
7
+ status: ShallowRef<SubscriptionStatus>;
8
+ }
@@ -0,0 +1,2 @@
1
+ import { TRPCFetch } from "@trpc/client";
2
+ export declare const getLinkFetch: () => TRPCFetch;
@@ -0,0 +1,24 @@
1
+ import { useTrpcServerFetch } from "./useTrpcServerFetch.js";
2
+ import { useRequestEvent } from "nuxt/app";
3
+ import { checkIsServer } from "@esposter/shared";
4
+ import { fetchWithEvent, getProxyRequestHeaders } from "nitro/h3";
5
+ // src/runtime/client/services/getLinkFetch.ts
6
+ const getLinkFetch = () => {
7
+ if (checkIsServer()) {
8
+ const event = useRequestEvent();
9
+ if (event?.app) return (url, options) => fetchWithEvent(event, url, options);
10
+ if (event) {
11
+ const serverFetchInProcess = useTrpcServerFetch();
12
+ return (url, options) => {
13
+ const headers = new Headers(getProxyRequestHeaders(event, { host: true }));
14
+ for (const [name, value] of new Headers(options?.headers)) headers.set(name, value);
15
+ return serverFetchInProcess(new URL(url, event.url), {
16
+ ...options,
17
+ headers
18
+ }, event.context);
19
+ };
20
+ }
21
+ }
22
+ return (input, init) => fetch(new URL(input, window.location.href), init);
23
+ };
24
+ export { getLinkFetch };
@@ -0,0 +1 @@
1
+ export declare const getProcedureKey: (path: string, input?: unknown) => string;
@@ -0,0 +1,4 @@
1
+ import { hash } from "ohash";
2
+ // src/runtime/client/services/getProcedureKey.ts
3
+ const getProcedureKey = (path, input) => input === void 0 ? path : `${path}-${hash(input)}`;
4
+ export { getProcedureKey };
@@ -0,0 +1 @@
1
+ export declare const mergeSignals: (signal: AbortSignal, callerSignal?: AbortSignal) => AbortSignal;
@@ -0,0 +1,3 @@
1
+ // src/runtime/client/services/mergeSignals.ts
2
+ const mergeSignals = (signal, callerSignal) => callerSignal ? AbortSignal.any([signal, callerSignal]) : signal;
3
+ export { mergeSignals };
@@ -0,0 +1,6 @@
1
+ import { KeysOf } from "../models/KeysOf.js";
2
+ import { UseMutationOptions } from "../models/UseMutationOptions.js";
3
+ import { UseMutationReturn } from "../models/UseMutationReturn.js";
4
+ import { TRPCResolverDef, TRPCUntypedClient } from "@trpc/client";
5
+ import { AnyTRPCRouter } from "@trpc/server";
6
+ export declare const useProcedureMutation: (client: TRPCUntypedClient<AnyTRPCRouter>, path: string, { mutationKey, trpc, ...options }?: UseMutationOptions<TRPCResolverDef>) => UseMutationReturn<TRPCResolverDef, unknown, KeysOf<unknown>, undefined>;
@@ -0,0 +1,28 @@
1
+ import { getProcedureKey } from "./getProcedureKey.js";
2
+ import { createError, useAsyncData } from "nuxt/app";
3
+ // src/runtime/client/services/useProcedureMutation.ts
4
+ const useProcedureMutation = (client, path, { mutationKey = getProcedureKey(path), trpc, ...options } = {}) => {
5
+ let request = Promise.resolve();
6
+ const asyncData = useAsyncData(mutationKey, () => request, {
7
+ ...options,
8
+ immediate: false,
9
+ server: false
10
+ });
11
+ return Object.assign(asyncData, { mutate: async (input) => {
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;
18
+ await asyncData.execute();
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;
26
+ } });
27
+ };
28
+ export { useProcedureMutation };