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.
- package/README.md +21 -13
- package/dist/index.d.ts +9 -0
- package/dist/index.js +84 -0
- package/dist/models/ExportReference.d.ts +4 -0
- package/dist/models/ModuleOptions.d.ts +8 -0
- package/dist/models/TrpcServerFetch.d.ts +2 -0
- package/dist/models/WebSocketOptions.d.ts +6 -0
- package/dist/runtime/client/createTRPCNuxtClient.d.ts +4 -0
- package/dist/runtime/client/createTRPCNuxtClient.js +28 -0
- package/dist/runtime/client/getMutationKey.d.ts +2 -0
- package/dist/runtime/client/getMutationKey.js +4 -0
- package/dist/runtime/client/getQueryKey.d.ts +2 -0
- package/dist/runtime/client/getQueryKey.js +4 -0
- package/dist/runtime/client/httpBatchLink.d.ts +3 -0
- package/dist/runtime/client/httpBatchLink.js +8 -0
- package/dist/runtime/client/httpLink.d.ts +3 -0
- package/dist/runtime/client/httpLink.js +8 -0
- package/dist/runtime/client/models/ConnectionStateSubscriptionStatus.d.ts +5 -0
- package/dist/runtime/client/models/ConnectionStateSubscriptionStatus.js +8 -0
- package/dist/runtime/client/models/DecoratedMutation.d.ts +9 -0
- package/dist/runtime/client/models/DecoratedMutation.js +1 -0
- package/dist/runtime/client/models/DecoratedProcedure.d.ts +6 -0
- package/dist/runtime/client/models/DecoratedProcedure.js +1 -0
- package/dist/runtime/client/models/DecoratedQuery.d.ts +8 -0
- package/dist/runtime/client/models/DecoratedQuery.js +1 -0
- package/dist/runtime/client/models/DecoratedRouterRecord.d.ts +9 -0
- package/dist/runtime/client/models/DecoratedRouterRecord.js +1 -0
- package/dist/runtime/client/models/DecoratedSubscription.d.ts +8 -0
- package/dist/runtime/client/models/DecoratedSubscription.js +1 -0
- package/dist/runtime/client/models/KeysOf.d.ts +1 -0
- package/dist/runtime/client/models/KeysOf.js +1 -0
- package/dist/runtime/client/models/PickFrom.d.ts +1 -0
- package/dist/runtime/client/models/PickFrom.js +1 -0
- package/dist/runtime/client/models/ProcedureNode.d.ts +4 -0
- package/dist/runtime/client/models/ProcedureNode.js +1 -0
- package/dist/runtime/client/models/ProcedurePath.d.ts +3 -0
- package/dist/runtime/client/models/ProcedurePath.js +1 -0
- package/dist/runtime/client/models/QueryAsyncData.d.ts +6 -0
- package/dist/runtime/client/models/QueryAsyncData.js +1 -0
- package/dist/runtime/client/models/QueryError.d.ts +3 -0
- package/dist/runtime/client/models/QueryError.js +1 -0
- package/dist/runtime/client/models/SubscriptionStatus.d.ts +7 -0
- package/dist/runtime/client/models/SubscriptionStatus.js +8 -0
- package/dist/runtime/client/models/TRPCNuxtClient.d.ts +3 -0
- package/dist/runtime/client/models/TRPCNuxtClient.js +1 -0
- package/dist/runtime/client/models/UseMutationOptions.d.ts +8 -0
- package/dist/runtime/client/models/UseMutationOptions.js +1 -0
- package/dist/runtime/client/models/UseMutationReturn.d.ts +7 -0
- package/dist/runtime/client/models/UseMutationReturn.js +1 -0
- package/dist/runtime/client/models/UseQuery.d.ts +6 -0
- package/dist/runtime/client/models/UseQuery.js +1 -0
- package/dist/runtime/client/models/UseQueryOptions.d.ts +9 -0
- package/dist/runtime/client/models/UseQueryOptions.js +1 -0
- package/dist/runtime/client/models/UseSubscriptionOptions.d.ts +5 -0
- package/dist/runtime/client/models/UseSubscriptionOptions.js +1 -0
- package/dist/runtime/client/models/UseSubscriptionReturn.d.ts +8 -0
- package/dist/runtime/client/models/UseSubscriptionReturn.js +1 -0
- package/dist/runtime/client/services/getLinkFetch.d.ts +2 -0
- package/dist/runtime/client/services/getLinkFetch.js +24 -0
- package/dist/runtime/client/services/getProcedureKey.d.ts +1 -0
- package/dist/runtime/client/services/getProcedureKey.js +4 -0
- package/dist/runtime/client/services/mergeSignals.d.ts +1 -0
- package/dist/runtime/client/services/mergeSignals.js +3 -0
- package/dist/runtime/client/services/useProcedureMutation.d.ts +6 -0
- package/dist/runtime/client/services/useProcedureMutation.js +28 -0
- package/dist/runtime/client/services/useProcedureQuery.d.ts +7 -0
- package/dist/runtime/client/services/useProcedureQuery.js +13 -0
- package/dist/runtime/client/services/useProcedureSubscription.d.ts +6 -0
- package/dist/runtime/client/services/useProcedureSubscription.js +68 -0
- package/dist/runtime/client/services/useTrpcServerFetch.d.ts +2 -0
- package/dist/runtime/client/services/useTrpcServerFetch.js +7 -0
- package/dist/runtime/constants.d.ts +4 -0
- package/dist/runtime/constants.js +6 -0
- package/dist/runtime/server/createTRPCEventHandler.d.ts +4 -0
- package/dist/runtime/server/createTRPCEventHandler.js +30 -0
- package/dist/runtime/server/createTRPCWebSocketHandler.d.ts +4 -0
- package/dist/runtime/server/createTRPCWebSocketHandler.js +65 -0
- package/dist/runtime/server/models/PeerWebSocketAdapter.d.ts +6 -0
- package/dist/runtime/server/models/PeerWebSocketAdapter.js +15 -0
- package/dist/runtime/server/models/TRPCEventHandlerOptions.d.ts +9 -0
- package/dist/runtime/server/models/TRPCEventHandlerOptions.js +1 -0
- package/dist/runtime/server/models/TRPCWebSocketConnection.d.ts +5 -0
- package/dist/runtime/server/models/TRPCWebSocketConnection.js +1 -0
- package/dist/runtime/server/models/TRPCWebSocketHandler.d.ts +5 -0
- package/dist/runtime/server/models/TRPCWebSocketHandler.js +1 -0
- package/dist/runtime/server/models/TRPCWebSocketHandlerOptions.d.ts +11 -0
- package/dist/runtime/server/models/TRPCWebSocketHandlerOptions.js +1 -0
- 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 +5 -0
- package/dist/runtime/server/models/WebSocketServerAdapter.js +15 -0
- package/dist/runtime/server/services/toIncomingMessage.d.ts +3 -0
- package/dist/runtime/server/services/toIncomingMessage.js +11 -0
- package/dist/services/getEventHandlerTemplate.js +9 -0
- package/dist/services/getImportStatement.js +3 -0
- package/dist/services/getWebSocketHandlerTemplate.js +22 -0
- package/dist/services/getWebSocketHooksTypeTemplate.js +17 -0
- 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)
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
85
|
-
| `getMutationKey`, `getQueryKey` | The `useAsyncData` keys a procedure's data
|
|
86
|
-
| `httpBatchLink`, `httpLink` | tRPC's links
|
|
87
|
-
| `trpc-nuxt-module` | The module
|
|
88
|
-
| `trpc-nuxt-module/runtime/server/createTRPCEventHandler` | The HTTP handler over tRPC's fetch adapter
|
|
89
|
-
| `trpc-nuxt-module/runtime/server/createTRPCWebSocketHandler` | The WebSocket handler over tRPC's `applyWSSHandler` and crossws
|
|
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
|
|
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
|
|
package/dist/index.d.ts
ADDED
|
@@ -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,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,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,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,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 @@
|
|
|
1
|
+
export {};
|
|
@@ -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 {};
|
|
@@ -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 @@
|
|
|
1
|
+
export {};
|
|
@@ -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 @@
|
|
|
1
|
+
export {};
|
|
@@ -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 @@
|
|
|
1
|
+
export {};
|
|
@@ -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 @@
|
|
|
1
|
+
export {};
|
|
@@ -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 @@
|
|
|
1
|
+
export declare const mergeSignals: (signal: AbortSignal, callerSignal?: AbortSignal) => AbortSignal;
|
|
@@ -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 };
|