mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-23 16:30:41 +01:00
Compare commits
58 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c2df9e2bd | ||
|
|
5cf1acfa7e | ||
|
|
f67f7fc5f4 | ||
|
|
e11edc59eb | ||
|
|
e522a59a4c | ||
|
|
1d7be4642d | ||
|
|
3552017e4f | ||
|
|
de690a7914 | ||
|
|
d2666650de | ||
|
|
e3d35c6b04 | ||
|
|
b569afde50 | ||
|
|
d4f3942fff | ||
|
|
68003e9c3e | ||
|
|
b27938f108 | ||
|
|
9e443ac559 | ||
|
|
8ba002407a | ||
|
|
f061b0531f | ||
|
|
f08b724df7 | ||
|
|
4085d5fe26 | ||
|
|
13713343ce | ||
|
|
d6f11bb301 | ||
|
|
c5c1f1cdba | ||
|
|
0681dd554d | ||
|
|
e7a76694af | ||
|
|
da75c18ebe | ||
|
|
6289575900 | ||
|
|
ac212672e4 | ||
|
|
b8e2a83e26 | ||
|
|
fd98b0d414 | ||
|
|
20ab235856 | ||
|
|
7205c356f8 | ||
|
|
e14827e0c7 | ||
|
|
eea6406d5a | ||
|
|
2d3409a25b | ||
|
|
7a71a6e128 | ||
|
|
d8e89297cd | ||
|
|
6768205318 | ||
|
|
c20a092a31 | ||
|
|
64df8fb7ad | ||
|
|
d5567e5826 | ||
|
|
6b898e836c | ||
|
|
fd38c4f983 | ||
|
|
48d4c6342f | ||
|
|
297c3d52f4 | ||
|
|
0f9653d1a4 | ||
|
|
85998101c7 | ||
|
|
d6cb770154 | ||
|
|
c222008524 | ||
|
|
9cd21eb300 | ||
|
|
b4e83dbb0b | ||
|
|
0fa63c86ba | ||
|
|
7371dc4c10 | ||
|
|
77ab5f0dc6 | ||
|
|
ba15d15d67 | ||
|
|
7999039088 | ||
|
|
dc15500074 | ||
|
|
99a83562aa | ||
|
|
e04344d508 |
11
README.md
11
README.md
@@ -2,6 +2,17 @@
|
|||||||
|
|
||||||
End-to-end typesafe APIs with [tRPC.io](https://trpc.io/) in Nuxt applications.
|
End-to-end typesafe APIs with [tRPC.io](https://trpc.io/) in Nuxt applications.
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<figure>
|
||||||
|
<img src="https://i.imgur.com/3AZlBZH.gif" alt="Demo" />
|
||||||
|
<figcaption>
|
||||||
|
<p align="center">
|
||||||
|
The client above is <strong>not</strong> importing any code from the server, only its type declarations.
|
||||||
|
</p>
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
</p>
|
||||||
|
|
||||||
Docs: https://trpc-nuxt.vercel.app
|
Docs: https://trpc-nuxt.vercel.app
|
||||||
|
|
||||||
For version 3 of this module (tRPC v9, auto-imports, auto handlers), [go here](https://github.com/wobsoriano/trpc-nuxt/tree/v3).
|
For version 3 of this module (tRPC v9, auto-imports, auto handlers), [go here](https://github.com/wobsoriano/trpc-nuxt/tree/v3).
|
||||||
|
|||||||
1
client.d.ts
vendored
1
client.d.ts
vendored
@@ -1 +0,0 @@
|
|||||||
export * from './dist/client/index'
|
|
||||||
@@ -1,5 +1,25 @@
|
|||||||
export default defineAppConfig({
|
export default defineAppConfig({
|
||||||
docus: {
|
docus: {
|
||||||
title: 'tRPC Nuxt'
|
title: 'tRPC Nuxt',
|
||||||
|
image: 'https://og-image.vercel.app/tRPC-Nuxt',
|
||||||
|
alt: 'tRPC-Nuxt cover',
|
||||||
|
url: 'https://trpc-nuxt.vercel.app',
|
||||||
|
debug: false,
|
||||||
|
socials: {
|
||||||
|
github: 'wobsoriano/trpc-nuxt'
|
||||||
|
},
|
||||||
|
aside: {
|
||||||
|
level: 1
|
||||||
|
},
|
||||||
|
footer: {
|
||||||
|
credits: true,
|
||||||
|
icons: [
|
||||||
|
{
|
||||||
|
label: 'NuxtJS',
|
||||||
|
href: 'https://nuxtjs.org',
|
||||||
|
component: 'IconNuxt'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,20 +5,18 @@ description: tRPC-Nuxt provides first class integration with tRPC.
|
|||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
## 1. Add to existing Nuxt project
|
|
||||||
|
|
||||||
::code-group
|
::code-group
|
||||||
|
|
||||||
```bash [pnpm]
|
```bash [pnpm]
|
||||||
pnpm add @trpc/server@next @trpc/client@next trpc-nuxt@beta zod
|
pnpm add @trpc/server@next @trpc/client@next trpc-nuxt zod
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash [npm]
|
```bash [npm]
|
||||||
npm install @trpc/server@next @trpc/client@next trpc-nuxt@beta zod
|
npm install @trpc/server@next @trpc/client@next trpc-nuxt zod
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash [yarn]
|
```bash [yarn]
|
||||||
yarn add @trpc/server@next @trpc/client@next trpc-nuxt@beta zod
|
yarn add @trpc/server@next @trpc/client@next trpc-nuxt zod
|
||||||
```
|
```
|
||||||
|
|
||||||
::
|
::
|
||||||
@@ -35,18 +33,6 @@ For making typesafe API calls from your client.
|
|||||||
|
|
||||||
Most examples use [Zod](https://github.com/colinhacks/zod) for input validation and tRPC.io highly recommends it, though it isn't required.
|
Most examples use [Zod](https://github.com/colinhacks/zod) for input validation and tRPC.io highly recommends it, though it isn't required.
|
||||||
|
|
||||||
## 2. Install module
|
|
||||||
|
|
||||||
This will transpile `trpc-nuxt` and exclude `trpc-nuxt/client` from optimized dependencies by Vite.
|
|
||||||
|
|
||||||
```ts [nuxt.config.ts]
|
|
||||||
export default defineNuxtConfig({
|
|
||||||
modules: ['trpc-nuxt/module']
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
## Next Steps
|
## Next Steps
|
||||||
|
|
||||||
Now that you've installed the required dependencies, you are ready to start building your application.
|
Now that you've installed the required dependencies, you are ready to start building your application.
|
||||||
|
|||||||
@@ -88,15 +88,14 @@ If you need to split your router into several subrouters, you can implement them
|
|||||||
|
|
||||||
## 2. Create tRPC client plugin
|
## 2. Create tRPC client plugin
|
||||||
|
|
||||||
Create a set of strongly-typed composables using your API's type signature.
|
Create a strongly-typed plugin using your API's type signature.
|
||||||
|
|
||||||
```ts [plugins/client.ts]
|
```ts [plugins/client.ts]
|
||||||
import { httpBatchLink } from '@trpc/client'
|
import { httpBatchLink, createTRPCProxyClient } from '@trpc/client'
|
||||||
import { createTRPCNuxtProxyClient } from 'trpc-nuxt/client'
|
|
||||||
import type { AppRouter } from '@/server/trpc/routers'
|
import type { AppRouter } from '@/server/trpc/routers'
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
const client = createTRPCNuxtProxyClient<AppRouter>({
|
const client = createTRPCProxyClient<AppRouter>({
|
||||||
links: [
|
links: [
|
||||||
httpBatchLink({
|
httpBatchLink({
|
||||||
/**
|
/**
|
||||||
@@ -122,19 +121,10 @@ export default defineNuxtPlugin(() => {
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { $client } = useNuxtApp()
|
const { $client } = useNuxtApp()
|
||||||
|
|
||||||
// query and mutate uses useAsyncData under the hood
|
const data = await $client.hello.query({ text: 'client' })
|
||||||
const { data, pending, error } = await $client.hello.query({ text: 'client' })
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div v-if="pending">
|
<p>{{data?.greeting }}</p>
|
||||||
Loading...
|
|
||||||
</div>
|
|
||||||
<div v-else-if="error?.data?.code">
|
|
||||||
Error: {{ error.data.code }}
|
|
||||||
</div>
|
|
||||||
<div v-else>
|
|
||||||
<p>{{ hello.data?.greeting }}</p>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
|
|||||||
40
docs/content/1.get-started/3.tips/1.composables.md
Normal file
40
docs/content/1.get-started/3.tips/1.composables.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
---
|
||||||
|
title: Composables
|
||||||
|
---
|
||||||
|
|
||||||
|
# Composables
|
||||||
|
|
||||||
|
It is often useful to wrap functionality of your `@trpc/client` api within other functions. For this purpose, it's necessary to be able to infer input types and output types generated by your `@trpc/server` router.
|
||||||
|
|
||||||
|
## Inference Helpers
|
||||||
|
|
||||||
|
`@trpc/server` exports the following helper types to assist with inferring these types from the `AppRouter` exported by your `@trpc/server` router:
|
||||||
|
|
||||||
|
- `inferRouterInputs<TRouter>`
|
||||||
|
- `inferRouterOutputs<TRouter>`
|
||||||
|
|
||||||
|
Let's assume we have this example query wrapped within Nuxt's [useAsyncData](https://v3.nuxtjs.org/api/composables/use-async-data/):
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const { data, error } = await useAsyncData(() => $client.todo.getTodos.query())
|
||||||
|
```
|
||||||
|
|
||||||
|
We can wrap this in a composable and also set the client error types:
|
||||||
|
|
||||||
|
```ts [composables/useGetTodos.ts]
|
||||||
|
import { TRPCClientError } from '@trpc/client'
|
||||||
|
import type { inferRouterOutputs } from '@trpc/server'
|
||||||
|
import type { AppRouter } from '@/server/trpc/routers'
|
||||||
|
|
||||||
|
type RouterOutput = inferRouterOutputs<AppRouter>
|
||||||
|
type GetTodosOutput = RouterOutput['todo']['getTodos']
|
||||||
|
|
||||||
|
type ErrorOutput = TRPCClientError<AppRouter>
|
||||||
|
|
||||||
|
export default function useGetTodos() {
|
||||||
|
const { $client } = useNuxtApp()
|
||||||
|
return useAsyncData<GetTodosOutput, ErrorOutput>(() => $client.todo.getTodos.query())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Now, we have a fully-typed composable.
|
||||||
40
docs/content/1.get-started/3.tips/2.headers.md
Normal file
40
docs/content/1.get-started/3.tips/2.headers.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
---
|
||||||
|
title: Headers
|
||||||
|
---
|
||||||
|
|
||||||
|
# Headers
|
||||||
|
|
||||||
|
We can use the built-in [useRequestHeaders](https://v3.nuxtjs.org/api/composables/use-request-headers/) to set outgoing request headers:
|
||||||
|
|
||||||
|
```ts [plugins/client.ts]
|
||||||
|
export default defineNuxtPlugin(() => {
|
||||||
|
const headers = useRequestHeaders()
|
||||||
|
|
||||||
|
const client = createTRPCProxyClient<AppRouter>({
|
||||||
|
links: [
|
||||||
|
httpBatchLink({
|
||||||
|
// headers need to be a function so it gets called dynamically
|
||||||
|
// every HTTP request
|
||||||
|
headers() {
|
||||||
|
// You can add more custom headers here
|
||||||
|
return headers
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
provide: {
|
||||||
|
client,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [server/trpc/context.ts]
|
||||||
|
export function createContext (event: H3Event) {
|
||||||
|
console.log('cookies', parseCookies(event))
|
||||||
|
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
```
|
||||||
103
docs/content/1.get-started/3.tips/3.authorization.md
Normal file
103
docs/content/1.get-started/3.tips/3.authorization.md
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
---
|
||||||
|
title: Authorization
|
||||||
|
---
|
||||||
|
|
||||||
|
# Authorization
|
||||||
|
|
||||||
|
The `createContext` function is called for each incoming request so here you can add contextual information about the calling user from the request object.
|
||||||
|
|
||||||
|
::alert{type="warning"}
|
||||||
|
Before you can access request headers in any context or middleware, you need to set the outgoing request headers. See [here](/get-started/tips/headers).
|
||||||
|
::
|
||||||
|
|
||||||
|
## Create context from request headers
|
||||||
|
|
||||||
|
```ts [server/trpc/context.ts]
|
||||||
|
import { inferAsyncReturnType } from '@trpc/server'
|
||||||
|
import { decodeAndVerifyJwtToken } from './somewhere/in/your/app/utils'
|
||||||
|
|
||||||
|
export async function createContext(event: H3Event) {
|
||||||
|
// Create your context based on the request object
|
||||||
|
// Will be available as `ctx` in all your resolvers
|
||||||
|
|
||||||
|
// This is just an example of something you might want to do in your ctx fn
|
||||||
|
const authorization = getRequestHeader(event, 'authorization')
|
||||||
|
async function getUserFromHeader() {
|
||||||
|
if (authorization) {
|
||||||
|
const user = await decodeAndVerifyJwtToken(authorization.split(' ')[1])
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
const user = await getUserFromHeader()
|
||||||
|
|
||||||
|
return {
|
||||||
|
user,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type Context = inferAsyncReturnType<typeof createContext>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Option 1: Authorize using resolver
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { TRPCError, initTRPC } from '@trpc/server'
|
||||||
|
import type { Context } from '../context'
|
||||||
|
|
||||||
|
export const t = initTRPC.context<Context>().create()
|
||||||
|
|
||||||
|
const appRouter = t.router({
|
||||||
|
// open for anyone
|
||||||
|
hello: t.procedure
|
||||||
|
.input(z.string().nullish())
|
||||||
|
.query(({ input, ctx }) => `hello ${input ?? ctx.user?.name ?? 'world'}`),
|
||||||
|
// checked in resolver
|
||||||
|
secret: t.procedure.query(({ ctx }) => {
|
||||||
|
if (!ctx.user) {
|
||||||
|
throw new TRPCError({ code: 'UNAUTHORIZED' })
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
secret: 'sauce',
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Option 2: Authorize using middleware
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { TRPCError, initTRPC } from '@trpc/server'
|
||||||
|
|
||||||
|
export const t = initTRPC.context<Context>().create()
|
||||||
|
|
||||||
|
const isAuthed = t.middleware(({ next, ctx }) => {
|
||||||
|
if (!ctx.user?.isAdmin) {
|
||||||
|
throw new TRPCError({ code: 'UNAUTHORIZED' })
|
||||||
|
}
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
user: ctx.user,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// you can reuse this for any procedure
|
||||||
|
export const protectedProcedure = t.procedure.use(isAuthed)
|
||||||
|
|
||||||
|
t.router({
|
||||||
|
// this is accessible for everyone
|
||||||
|
hello: t.procedure
|
||||||
|
.input(z.string().nullish())
|
||||||
|
.query(({ input, ctx }) => `hello ${input ?? ctx.user?.name ?? 'world'}`),
|
||||||
|
admin: t.router({
|
||||||
|
// this is accessible only to admins
|
||||||
|
secret: protectedProcedure.query(({ ctx }) => {
|
||||||
|
return {
|
||||||
|
secret: 'sauce',
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
This page is entirely based on [authorization docs](https://trpc.io/docs/v10/authorization) of tRPC with a minimal change made to work with Nuxt.
|
||||||
84
docs/content/1.get-started/3.tips/4.server-side-calls.md
Normal file
84
docs/content/1.get-started/3.tips/4.server-side-calls.md
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
---
|
||||||
|
title: Server Side Calls
|
||||||
|
---
|
||||||
|
|
||||||
|
# Server Side Calls
|
||||||
|
|
||||||
|
You may need to call your procedure(s) directly from the server, `createCaller()` function returns you an instance of `RouterCaller` able to execute queries and mutations.
|
||||||
|
|
||||||
|
## Input query example
|
||||||
|
|
||||||
|
We create the router with a input query and then we call the asynchronous `greeting` procedure to get the result.
|
||||||
|
|
||||||
|
::code-group
|
||||||
|
|
||||||
|
```ts [server/trpc/trpc.ts]
|
||||||
|
import { initTRPC } from '@trpc/server'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
const t = initTRPC.create()
|
||||||
|
|
||||||
|
export const router = t.router({
|
||||||
|
// Create procedure at path 'greeting'
|
||||||
|
greeting: t.procedure
|
||||||
|
.input(z.object({ name: z.string() }))
|
||||||
|
.query(({ input }) => `Hello ${input.name}`),
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [server/api/greeting.ts]
|
||||||
|
import { router } from '@/server/trpc/trpc'
|
||||||
|
|
||||||
|
export default eventHandler(async (event) => {
|
||||||
|
const { name } = getQuery(event)
|
||||||
|
const caller = router.createCaller({})
|
||||||
|
|
||||||
|
const greeting = await caller.greeting({ name })
|
||||||
|
|
||||||
|
return {
|
||||||
|
greeting
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
## Mutation example
|
||||||
|
|
||||||
|
We create the router with a mutation and then we call the asynchronous `post` procedure to get the result.
|
||||||
|
|
||||||
|
::code-group
|
||||||
|
|
||||||
|
```ts [server/trpc/trpc.ts]
|
||||||
|
import { initTRPC } from '@trpc/server'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
const posts = ['One', 'Two', 'Three']
|
||||||
|
|
||||||
|
const t = initTRPC.create()
|
||||||
|
export const router = t.router({
|
||||||
|
post: t.router({
|
||||||
|
add: t.procedure.input(z.string()).mutation(({ input }) => {
|
||||||
|
posts.push(input)
|
||||||
|
return posts
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [server/api/post.ts]
|
||||||
|
import { router } from '@/server/trpc/trpc'
|
||||||
|
|
||||||
|
export default eventHandler(async (event) => {
|
||||||
|
const body = await getBody(event)
|
||||||
|
const caller = router.createCaller({})
|
||||||
|
|
||||||
|
const post = await caller.post.add(body.post)
|
||||||
|
|
||||||
|
return {
|
||||||
|
post
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
::
|
||||||
24
docs/content/1.get-started/3.tips/5.aborting-procedures.md
Normal file
24
docs/content/1.get-started/3.tips/5.aborting-procedures.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
---
|
||||||
|
title: Aborting Procedures
|
||||||
|
---
|
||||||
|
|
||||||
|
# Aborting Procedures
|
||||||
|
|
||||||
|
tRPC adheres to the industry standard when it comes to aborting procedures. All you have to do is pass an `AbortSignal` to the query-options and then call its parent `AbortController`'s `abort` method.
|
||||||
|
|
||||||
|
```ts [composables/useGetTodo.ts]
|
||||||
|
export default function useGetTodo(id: number) {
|
||||||
|
const { $client } = useNuxtApp()
|
||||||
|
const ac = new AbortController()
|
||||||
|
|
||||||
|
onScopeDispose(() => {
|
||||||
|
ac.abort()
|
||||||
|
})
|
||||||
|
|
||||||
|
return useAsyncData(() => {
|
||||||
|
return $client.todo.getTodo.query(id, {
|
||||||
|
signal: ac.signal
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
4
docs/content/1.get-started/index.md
Normal file
4
docs/content/1.get-started/index.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
navigation: false
|
||||||
|
redirect: /get-started/installation
|
||||||
|
---
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: "tRPC Nuxt"
|
title: "tRPC Nuxt"
|
||||||
description: "A supa simple wrapper arousnd supabase-js to enable usage and integration within Nuxt."
|
description: "End-to-end typesafe APIs in Nuxt applications."
|
||||||
navigation: false
|
navigation: false
|
||||||
layout: page
|
layout: page
|
||||||
---
|
---
|
||||||
@@ -13,7 +13,7 @@ cta:
|
|||||||
secondary:
|
secondary:
|
||||||
- Star on GitHub ->
|
- Star on GitHub ->
|
||||||
- https://github.com/wobsoriano/trpc-nuxt
|
- https://github.com/wobsoriano/trpc-nuxt
|
||||||
snippet: npm install trpc-nuxt@beta
|
snippet: npm install trpc-nuxt
|
||||||
---
|
---
|
||||||
|
|
||||||
#title
|
#title
|
||||||
@@ -26,6 +26,6 @@ End-to-end typesafe APIs in Nuxt applications.
|
|||||||
::list
|
::list
|
||||||
- Automatic typesafety
|
- Automatic typesafety
|
||||||
- Snappy DX
|
- Snappy DX
|
||||||
- Autocompletion
|
- Autocompletion on the client, for inputs, outputs and errors
|
||||||
::
|
::
|
||||||
::
|
::
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ export default defineNuxtConfig({
|
|||||||
modules: ['@nuxtlabs/github-module'],
|
modules: ['@nuxtlabs/github-module'],
|
||||||
extends: process.env.DOCUS_THEME_PATH || '@nuxt-themes/docus',
|
extends: process.env.DOCUS_THEME_PATH || '@nuxt-themes/docus',
|
||||||
github: {
|
github: {
|
||||||
owner: 'nuxt',
|
owner: 'wobsoriano',
|
||||||
repo: 'content',
|
repo: 'trpc-nuxt',
|
||||||
branch: 'main'
|
branch: 'next'
|
||||||
},
|
},
|
||||||
colorMode: {
|
colorMode: {
|
||||||
preference: 'dark'
|
preference: 'dark'
|
||||||
@@ -17,10 +17,6 @@ export default defineNuxtConfig({
|
|||||||
transpile: [/content-edge/, /github-module/]
|
transpile: [/content-edge/, /github-module/]
|
||||||
},
|
},
|
||||||
nitro: {
|
nitro: {
|
||||||
prerender: {
|
|
||||||
crawlLinks: true,
|
|
||||||
routes: ['/']
|
|
||||||
},
|
|
||||||
preset: 'vercel'
|
preset: 'vercel'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,12 +8,10 @@
|
|||||||
"preview": "nuxi preview"
|
"preview": "nuxi preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"nuxt": "^3.0.0-rc.13",
|
"nuxt": "^3.0.0"
|
||||||
"@nuxtjs/tailwindcss": "^6.1.3"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxt-themes/docus": "npm:@nuxt-themes/docus-edge@latest",
|
"@nuxt-themes/docus": "^0.3.1",
|
||||||
"@nuxtlabs/github-module": "npm:@nuxtlabs/github-module-edge@latest"
|
"@nuxtlabs/github-module": "^1.5.3"
|
||||||
},
|
}
|
||||||
"packageManager": "yarn@1.22.19"
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,6 @@
|
|||||||
import { defineTheme } from 'pinceau'
|
import { defineTheme } from 'pinceau'
|
||||||
|
|
||||||
export default defineTheme({
|
export default defineTheme({
|
||||||
title: 'tRPC-Nuxt',
|
|
||||||
cover: {
|
|
||||||
src: 'https://og-image.vercel.app/tRPC-Nuxt',
|
|
||||||
alt: 'tRPC-Nuxt conver'
|
|
||||||
},
|
|
||||||
aside: {
|
|
||||||
level: 1
|
|
||||||
},
|
|
||||||
colors: {
|
colors: {
|
||||||
primary: {
|
primary: {
|
||||||
50: '#BFEDFC',
|
50: '#BFEDFC',
|
||||||
|
|||||||
17
module.mjs
17
module.mjs
@@ -1,17 +0,0 @@
|
|||||||
import { defineNuxtModule } from '@nuxt/kit'
|
|
||||||
|
|
||||||
export default defineNuxtModule({
|
|
||||||
meta: {
|
|
||||||
name: 'trpc-nuxt',
|
|
||||||
configKey: 'trpc',
|
|
||||||
compatibility: {
|
|
||||||
nuxt: '^3.0.0-rc.13'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
setup (_moduleOptions, nuxt) {
|
|
||||||
nuxt.options.build.transpile.push('trpc-nuxt')
|
|
||||||
nuxt.options.vite.optimizeDeps = nuxt.options.vite.optimizeDeps || {}
|
|
||||||
nuxt.options.vite.optimizeDeps.exclude = nuxt.options.vite.optimizeDeps.exclude || []
|
|
||||||
nuxt.options.vite.optimizeDeps.exclude.push('trpc-nuxt/client')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
42
package.json
42
package.json
@@ -1,33 +1,24 @@
|
|||||||
{
|
{
|
||||||
"name": "trpc-nuxt",
|
"name": "trpc-nuxt",
|
||||||
|
"description": "End-to-end typesafe APIs in Nuxt applications.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.4.0",
|
"version": "0.4.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"sideEffects": false,
|
"sideEffects": false,
|
||||||
"exports": {
|
"exports": {
|
||||||
"./package.json": "./package.json",
|
|
||||||
".": {
|
".": {
|
||||||
"require": "./dist/index.cjs",
|
"require": "./dist/index.cjs",
|
||||||
"import": "./dist/index.mjs"
|
"import": "./dist/index.mjs"
|
||||||
},
|
|
||||||
"./client": {
|
|
||||||
"require": "./dist/client/index.cjs",
|
|
||||||
"import": "./dist/client/index.mjs"
|
|
||||||
},
|
|
||||||
"./module": {
|
|
||||||
"import": "./module.mjs"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"main": "./dist/index.mjs",
|
"main": "./dist/index.mjs",
|
||||||
"module": "./dist/index.mjs",
|
"module": "./dist/index.mjs",
|
||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist"
|
||||||
"client.d.ts",
|
|
||||||
"module.mjs"
|
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "concurrently \"pnpm build --watch\" \"pnpm --filter playground dev\"",
|
"dev": "concurrently \"pnpm build -- --watch\" \"pnpm --filter playground dev\"",
|
||||||
"dev:prepare": "pnpm build && nuxt prepare playground",
|
"dev:prepare": "pnpm build && nuxt prepare playground",
|
||||||
"prepublishOnly": "pnpm build",
|
"prepublishOnly": "pnpm build",
|
||||||
"build": "tsup",
|
"build": "tsup",
|
||||||
@@ -36,32 +27,25 @@
|
|||||||
"release": "bumpp && npm publish"
|
"release": "bumpp && npm publish"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@trpc/client": "^10.0.0-proxy-beta.21",
|
"@trpc/client": "^10.0.0",
|
||||||
"@trpc/server": "^10.0.0-proxy-beta.21"
|
"@trpc/server": "^10.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"h3": "^0.8.6",
|
"h3": "^1.0.1",
|
||||||
"nanoid": "^4.0.0",
|
"nanoid": "^4.0.0",
|
||||||
"nuxt": "^3.0.0-rc.13",
|
"ohash": "^1.0.0",
|
||||||
"ohash": "^0.1.5",
|
"ufo": "^1.0.0"
|
||||||
"ufo": "^0.8.6"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxt/kit": "3.0.0-rc.13",
|
|
||||||
"@nuxtjs/eslint-config-typescript": "^11.0.0",
|
"@nuxtjs/eslint-config-typescript": "^11.0.0",
|
||||||
"@trpc/client": "10.0.0-rc.7",
|
"@trpc/client": "^10.1.0",
|
||||||
"@trpc/server": "10.0.0-rc.7",
|
"@trpc/server": "^10.1.0",
|
||||||
"bumpp": "^8.2.1",
|
"bumpp": "^8.2.1",
|
||||||
"concurrently": "^7.5.0",
|
"concurrently": "^7.5.0",
|
||||||
"eslint": "^8.25.0",
|
"eslint": "^8.25.0",
|
||||||
"tsup": "6.4.0",
|
"tsup": "6.4.0",
|
||||||
"typescript": "^4.7.4"
|
"typescript": "^4.7.4"
|
||||||
},
|
},
|
||||||
"pnpm": {
|
|
||||||
"patchedDependencies": {
|
|
||||||
"nuxt@3.0.0-rc.13": "patches/nuxt@3.0.0-rc.13.patch"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"extends": [
|
"extends": [
|
||||||
"@nuxtjs/eslint-config-typescript"
|
"@nuxtjs/eslint-config-typescript"
|
||||||
@@ -77,6 +61,8 @@
|
|||||||
"eslintIgnore": [
|
"eslintIgnore": [
|
||||||
"*.json",
|
"*.json",
|
||||||
"node_modules",
|
"node_modules",
|
||||||
"*.md"
|
"*.md",
|
||||||
|
"dist",
|
||||||
|
".output"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
diff --git a/dist/head/runtime/lib/vueuse-head.plugin.mjs b/dist/head/runtime/lib/vueuse-head.plugin.mjs
|
|
||||||
index a3278e56986d8e8efb099c502c82d9661a36d846..148a8879ba433072cecd15995cfd5b1b69941124 100644
|
|
||||||
--- a/dist/head/runtime/lib/vueuse-head.plugin.mjs
|
|
||||||
+++ b/dist/head/runtime/lib/vueuse-head.plugin.mjs
|
|
||||||
@@ -4,7 +4,7 @@ import { defineNuxtPlugin, useRouter } from "#app";
|
|
||||||
import { appHead } from "#build/nuxt.config.mjs";
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
|
||||||
const head = createHead();
|
|
||||||
- head.addEntry(appHead, { resolved: true });
|
|
||||||
+ head.addHeadObjs(appHead, { resolved: true });
|
|
||||||
nuxtApp.vueApp.use(head);
|
|
||||||
if (process.client) {
|
|
||||||
let pauseDOMUpdates = true;
|
|
||||||
@@ -25,7 +25,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
||||||
}
|
|
||||||
nuxtApp._useHead = (_meta, options) => {
|
|
||||||
if (process.server) {
|
|
||||||
- head.addEntry(_meta, options);
|
|
||||||
+ head.addHeadObjs(_meta, options);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const cleanUp = head.addReactiveEntry(_meta, options);
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
// https://v3.nuxtjs.org/api/configuration/nuxt.config
|
// https://v3.nuxtjs.org/api/configuration/nuxt.config
|
||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
modules: ['trpc-nuxt/module']
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,13 +9,13 @@
|
|||||||
"postinstall": "nuxt prepare"
|
"postinstall": "nuxt prepare"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@trpc/client": "10.0.0-rc.7",
|
"@trpc/client": "^10.1.0",
|
||||||
"@trpc/server": "10.0.0-rc.7",
|
"@trpc/server": "^10.1.0",
|
||||||
"superjson": "^1.11.0",
|
"superjson": "^1.11.0",
|
||||||
"trpc-nuxt": "workspace:*",
|
"trpc-nuxt": "workspace:*",
|
||||||
"zod": "^3.19.1"
|
"zod": "^3.19.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nuxt": "^3.0.0-rc.13"
|
"nuxt": "^3.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { $client } = useNuxtApp()
|
import { TRPCClientError } from '@trpc/client';
|
||||||
// const headers = useClientHeaders()
|
import type { inferRouterOutputs } from '@trpc/server';
|
||||||
|
import type { AppRouter } from '~~/server/trpc/routers';
|
||||||
|
|
||||||
// const addHeader = () => {
|
const { $client } = useNuxtApp()
|
||||||
// headers.value.authorization = 'Bearer abcdefghijklmnop'
|
|
||||||
// console.log(headers.value)
|
|
||||||
// }
|
|
||||||
|
|
||||||
const addTodo = async () => {
|
const addTodo = async () => {
|
||||||
const title = Math.random().toString(36).slice(2, 7)
|
const title = Math.random().toString(36).slice(2, 7)
|
||||||
@@ -17,17 +15,16 @@ const addTodo = async () => {
|
|||||||
title,
|
title,
|
||||||
completed: false
|
completed: false
|
||||||
})
|
})
|
||||||
console.log(x.data.value)
|
console.log(x)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: todos, pending, error, refresh } = await $client.todo.getTodos.query(undefined, {
|
type RouterOutput = inferRouterOutputs<AppRouter>;
|
||||||
trpc: {
|
type ErrorOutput = TRPCClientError<AppRouter>
|
||||||
abortOnUnmount: true
|
|
||||||
}
|
const { data: todos, pending, error, refresh } = await useAsyncData<RouterOutput['todo']['getTodos'], ErrorOutput>(() => $client.todo.getTodos.query())
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const { $client } = useNuxtApp()
|
const { $client } = useNuxtApp()
|
||||||
const { data: todo, pending, error } = await $client.todo.getTodo.query(Number(route.params.id))
|
const { data: todo, pending, error } = await useAsyncData(() => $client.todo.getTodo.query(Number(route.params.id)))
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { httpBatchLink, loggerLink } from '@trpc/client'
|
import { createTRPCProxyClient, httpBatchLink, loggerLink } from '@trpc/client'
|
||||||
import { createTRPCNuxtProxyClient } from 'trpc-nuxt/client'
|
|
||||||
import superjson from 'superjson'
|
import superjson from 'superjson'
|
||||||
import type { AppRouter } from '~~/server/trpc/routers'
|
import type { AppRouter } from '~~/server/trpc/routers'
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
const client = createTRPCNuxtProxyClient<AppRouter>({
|
const headers = useRequestHeaders()
|
||||||
|
const client = createTRPCProxyClient<AppRouter>({
|
||||||
transformer: superjson,
|
transformer: superjson,
|
||||||
links: [
|
links: [
|
||||||
// adds pretty logs to your console in development and logs errors in production
|
// adds pretty logs to your console in development and logs errors in production
|
||||||
@@ -14,7 +14,10 @@ export default defineNuxtPlugin(() => {
|
|||||||
(opts.direction === 'down' && opts.result instanceof Error)
|
(opts.direction === 'down' && opts.result instanceof Error)
|
||||||
}),
|
}),
|
||||||
httpBatchLink({
|
httpBatchLink({
|
||||||
url: 'http://localhost:3000/api/trpc'
|
url: 'http://localhost:3000/api/trpc',
|
||||||
|
headers () {
|
||||||
|
return headers
|
||||||
|
}
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ export type Context = inferAsyncReturnType<typeof createContext>
|
|||||||
* @link https://trpc.io/docs/context
|
* @link https://trpc.io/docs/context
|
||||||
*/
|
*/
|
||||||
export function createContext (
|
export function createContext (
|
||||||
opts: H3Event
|
event: H3Event
|
||||||
) {
|
) {
|
||||||
// for API-response caching see https://trpc.io/docs/caching
|
// for API-response caching see https://trpc.io/docs/caching
|
||||||
|
console.log('cookies', parseCookies(event))
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|||||||
2137
pnpm-lock.yaml
generated
2137
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,66 +0,0 @@
|
|||||||
import type { CreateTRPCClientOptions, inferRouterProxyClient } from '@trpc/client'
|
|
||||||
import { createTRPCProxyClient } from '@trpc/client'
|
|
||||||
import type {
|
|
||||||
AnyRouter
|
|
||||||
} from '@trpc/server'
|
|
||||||
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
|
||||||
import { hash } from 'ohash'
|
|
||||||
import { nanoid } from 'nanoid'
|
|
||||||
import type { DecoratedProcedureRecord } from './types'
|
|
||||||
// @ts-expect-error: Nuxt auto-imports
|
|
||||||
import { getCurrentInstance, onScopeDispose, useAsyncData } from '#imports'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the key used for `useAsyncData` call
|
|
||||||
*/
|
|
||||||
export function getQueryKey (
|
|
||||||
path: string,
|
|
||||||
input: unknown
|
|
||||||
): string {
|
|
||||||
return input === undefined ? path : `${path}-${hash(input || '')}`
|
|
||||||
}
|
|
||||||
|
|
||||||
function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: string, client: inferRouterProxyClient<TRouter>) {
|
|
||||||
return createRecursiveProxy((opts) => {
|
|
||||||
const args = opts.args
|
|
||||||
|
|
||||||
const pathCopy = [name, ...opts.path]
|
|
||||||
|
|
||||||
// The last arg is for instance `.mutate` or `.query()`
|
|
||||||
const lastArg = pathCopy.pop()!
|
|
||||||
|
|
||||||
const path = pathCopy.join('.')
|
|
||||||
|
|
||||||
const [input, otherOptions] = args
|
|
||||||
|
|
||||||
const queryKey = lastArg === 'mutate' ? nanoid() : getQueryKey(path, input)
|
|
||||||
|
|
||||||
const { trpc, ...asyncDataOptions } = otherOptions || {} as any
|
|
||||||
|
|
||||||
let controller: AbortController
|
|
||||||
|
|
||||||
if (trpc?.abortOnUnmount) {
|
|
||||||
if (getCurrentInstance()) {
|
|
||||||
onScopeDispose(() => {
|
|
||||||
controller?.abort?.()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
|
|
||||||
}
|
|
||||||
|
|
||||||
return useAsyncData(queryKey, () => (client as any)[path][lastArg](input, {
|
|
||||||
signal: controller?.signal,
|
|
||||||
...trpc
|
|
||||||
}), asyncDataOptions)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createTRPCNuxtProxyClient<TRouter extends AnyRouter> (opts: CreateTRPCClientOptions<TRouter>) {
|
|
||||||
const client = createTRPCProxyClient(opts)
|
|
||||||
|
|
||||||
const decoratedClient = createFlatProxy((key) => {
|
|
||||||
return createNuxtProxyDecoration(key, client)
|
|
||||||
}) as DecoratedProcedureRecord<TRouter['_def']['record'], TRouter>
|
|
||||||
|
|
||||||
return decoratedClient
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
import type { TRPCClientErrorLike, TRPCRequestOptions } from '@trpc/client'
|
|
||||||
import type { TRPCSubscriptionObserver } from '@trpc/client/dist/internals/TRPCClient'
|
|
||||||
import type {
|
|
||||||
AnyMutationProcedure,
|
|
||||||
AnyProcedure,
|
|
||||||
AnyQueryProcedure,
|
|
||||||
AnyRouter,
|
|
||||||
AnySubscriptionProcedure,
|
|
||||||
ProcedureArgs,
|
|
||||||
ProcedureRouterRecord,
|
|
||||||
inferProcedureInput,
|
|
||||||
inferProcedureOutput
|
|
||||||
} from '@trpc/server'
|
|
||||||
import type { Unsubscribable, inferObservableValue } from '@trpc/server/observable'
|
|
||||||
import type {
|
|
||||||
AsyncData,
|
|
||||||
AsyncDataOptions,
|
|
||||||
KeyOfRes,
|
|
||||||
PickFrom,
|
|
||||||
_Transform
|
|
||||||
} from 'nuxt/dist/app/composables/asyncData'
|
|
||||||
|
|
||||||
// Modified @trpc/client and @trpc/react-query types
|
|
||||||
// https://github.com/trpc/trpc/blob/next/packages/client/src/createTRPCClientProxy.ts
|
|
||||||
// https://github.com/trpc/trpc/blob/next/packages/react-query/src/createTRPCReact.tsx
|
|
||||||
|
|
||||||
interface TRPCOptions extends TRPCRequestOptions {
|
|
||||||
abortOnUnmount?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
type SubscriptionResolver<
|
|
||||||
TProcedure extends AnyProcedure,
|
|
||||||
TRouter extends AnyRouter,
|
|
||||||
> = (
|
|
||||||
...args: [
|
|
||||||
input: ProcedureArgs<TProcedure['_def']>[0],
|
|
||||||
opts: ProcedureArgs<TProcedure['_def']>[1] &
|
|
||||||
Partial<
|
|
||||||
TRPCSubscriptionObserver<
|
|
||||||
inferObservableValue<inferProcedureOutput<TProcedure>>,
|
|
||||||
TRPCClientErrorLike<TRouter>
|
|
||||||
>
|
|
||||||
>,
|
|
||||||
]
|
|
||||||
) => Unsubscribable
|
|
||||||
|
|
||||||
type DecorateProcedure<
|
|
||||||
TProcedure extends AnyProcedure,
|
|
||||||
TRouter extends AnyRouter,
|
|
||||||
> = TProcedure extends AnyQueryProcedure
|
|
||||||
? {
|
|
||||||
query: <
|
|
||||||
TData = inferProcedureOutput<TProcedure>,
|
|
||||||
Transform extends _Transform<TData> = _Transform<TData, TData>,
|
|
||||||
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
|
|
||||||
>(
|
|
||||||
input: inferProcedureInput<TProcedure>,
|
|
||||||
opts?: AsyncDataOptions<TData, Transform, PickKeys> & { trpc: TRPCOptions },
|
|
||||||
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>>
|
|
||||||
} : TProcedure extends AnyMutationProcedure ? {
|
|
||||||
mutate: <
|
|
||||||
TData = inferProcedureOutput<TProcedure>,
|
|
||||||
Transform extends _Transform<TData> = _Transform<TData, TData>,
|
|
||||||
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
|
|
||||||
>(
|
|
||||||
input: inferProcedureInput<TProcedure>,
|
|
||||||
opts?: AsyncDataOptions<TData, Transform, PickKeys> & { trpc: TRPCOptions },
|
|
||||||
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>>
|
|
||||||
} : TProcedure extends AnySubscriptionProcedure ? {
|
|
||||||
subscribe: SubscriptionResolver<TProcedure, TRouter>
|
|
||||||
} : never
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export type DecoratedProcedureRecord<
|
|
||||||
TProcedures extends ProcedureRouterRecord,
|
|
||||||
TRouter extends AnyRouter,
|
|
||||||
> = {
|
|
||||||
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
|
|
||||||
? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record'], TRouter>
|
|
||||||
: TProcedures[TKey] extends AnyProcedure
|
|
||||||
? DecorateProcedure<TProcedures[TKey], TRouter>
|
|
||||||
: never;
|
|
||||||
}
|
|
||||||
127
src/index.ts
127
src/index.ts
@@ -1 +1,126 @@
|
|||||||
export * from './server'
|
import type { ResponseMeta } from '@trpc/server/http'
|
||||||
|
import { resolveHTTPResponse } from '@trpc/server/http'
|
||||||
|
import type {
|
||||||
|
AnyRouter,
|
||||||
|
ProcedureType,
|
||||||
|
inferRouterContext,
|
||||||
|
inferRouterError
|
||||||
|
} from '@trpc/server'
|
||||||
|
import {
|
||||||
|
TRPCError
|
||||||
|
} from '@trpc/server'
|
||||||
|
import { createURL } from 'ufo'
|
||||||
|
import type { H3Event } from 'h3'
|
||||||
|
import { createError, defineEventHandler, isMethod, readBody } from 'h3'
|
||||||
|
import type { TRPCResponse } from '@trpc/server/rpc'
|
||||||
|
|
||||||
|
type MaybePromise<T> = T | Promise<T>
|
||||||
|
|
||||||
|
export type CreateContextFn<TRouter extends AnyRouter> = (event: H3Event) => MaybePromise<inferRouterContext<TRouter>>
|
||||||
|
|
||||||
|
export interface ResponseMetaFnPayload<TRouter extends AnyRouter> {
|
||||||
|
data: TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
||||||
|
ctx?: inferRouterContext<TRouter>
|
||||||
|
paths?: string[]
|
||||||
|
type: ProcedureType | 'unknown'
|
||||||
|
errors: TRPCError[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: ResponseMetaFnPayload<TRouter>) => ResponseMeta
|
||||||
|
|
||||||
|
export interface OnErrorPayload<TRouter extends AnyRouter> {
|
||||||
|
error: TRPCError
|
||||||
|
type: ProcedureType | 'unknown'
|
||||||
|
path: string | undefined
|
||||||
|
req: H3Event['node']['req']
|
||||||
|
input: unknown
|
||||||
|
ctx: undefined | inferRouterContext<TRouter>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type OnErrorFn<TRouter extends AnyRouter> = (opts: OnErrorPayload<TRouter>) => void
|
||||||
|
|
||||||
|
export interface ResolveHTTPRequestOptions<TRouter extends AnyRouter> {
|
||||||
|
router: TRouter
|
||||||
|
createContext?: CreateContextFn<TRouter>
|
||||||
|
responseMeta?: ResponseMetaFn<TRouter>
|
||||||
|
onError?: OnErrorFn<TRouter>
|
||||||
|
batching?: {
|
||||||
|
enabled: boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPath (event: H3Event): string | null {
|
||||||
|
if (typeof event.context.params.trpc === 'string') { return event.context.params.trpc }
|
||||||
|
|
||||||
|
if (Array.isArray(event.context.params.trpc)) { return event.context.params.trpc.join('/') }
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createNuxtApiHandler<TRouter extends AnyRouter> ({
|
||||||
|
router,
|
||||||
|
createContext,
|
||||||
|
responseMeta,
|
||||||
|
onError,
|
||||||
|
batching
|
||||||
|
}: ResolveHTTPRequestOptions<TRouter>) {
|
||||||
|
return defineEventHandler(async (event) => {
|
||||||
|
const {
|
||||||
|
req,
|
||||||
|
res
|
||||||
|
} = event.node
|
||||||
|
|
||||||
|
const $url = createURL(req.url!)
|
||||||
|
|
||||||
|
const path = getPath(event)
|
||||||
|
|
||||||
|
if (path === null) {
|
||||||
|
const error = router.getErrorShape({
|
||||||
|
error: new TRPCError({
|
||||||
|
message:
|
||||||
|
'Param "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
||||||
|
code: 'INTERNAL_SERVER_ERROR'
|
||||||
|
}),
|
||||||
|
type: 'unknown',
|
||||||
|
ctx: undefined,
|
||||||
|
path: undefined,
|
||||||
|
input: undefined
|
||||||
|
})
|
||||||
|
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
statusMessage: JSON.stringify(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const httpResponse = await resolveHTTPResponse({
|
||||||
|
batching,
|
||||||
|
router,
|
||||||
|
req: {
|
||||||
|
method: req.method!,
|
||||||
|
headers: req.headers,
|
||||||
|
body: isMethod(event, 'GET') ? null : await readBody(event),
|
||||||
|
query: $url.searchParams
|
||||||
|
},
|
||||||
|
path,
|
||||||
|
createContext: async () => await createContext?.(event),
|
||||||
|
responseMeta,
|
||||||
|
onError: (o) => {
|
||||||
|
onError?.({
|
||||||
|
...o,
|
||||||
|
req
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const { status, headers, body } = httpResponse
|
||||||
|
|
||||||
|
res.statusCode = status
|
||||||
|
|
||||||
|
headers && Object.keys(headers).forEach((key) => {
|
||||||
|
res.setHeader(key, headers[key]!)
|
||||||
|
})
|
||||||
|
|
||||||
|
return body
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
126
src/server.ts
126
src/server.ts
@@ -1,126 +0,0 @@
|
|||||||
import type { ResponseMeta } from '@trpc/server/http'
|
|
||||||
import { resolveHTTPResponse } from '@trpc/server/http'
|
|
||||||
import type {
|
|
||||||
AnyRouter,
|
|
||||||
ProcedureType,
|
|
||||||
inferRouterContext,
|
|
||||||
inferRouterError
|
|
||||||
} from '@trpc/server'
|
|
||||||
import {
|
|
||||||
TRPCError
|
|
||||||
} from '@trpc/server'
|
|
||||||
import { createURL } from 'ufo'
|
|
||||||
import type { H3Event } from 'h3'
|
|
||||||
import { createError, defineEventHandler, isMethod, readBody } from 'h3'
|
|
||||||
import type { TRPCResponse } from '@trpc/server/rpc'
|
|
||||||
|
|
||||||
type MaybePromise<T> = T | Promise<T>
|
|
||||||
|
|
||||||
export type CreateContextFn<TRouter extends AnyRouter> = (event: H3Event) => MaybePromise<inferRouterContext<TRouter>>
|
|
||||||
|
|
||||||
export interface ResponseMetaFnPayload<TRouter extends AnyRouter> {
|
|
||||||
data: TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
|
||||||
ctx?: inferRouterContext<TRouter>
|
|
||||||
paths?: string[]
|
|
||||||
type: ProcedureType | 'unknown'
|
|
||||||
errors: TRPCError[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: ResponseMetaFnPayload<TRouter>) => ResponseMeta
|
|
||||||
|
|
||||||
export interface OnErrorPayload<TRouter extends AnyRouter> {
|
|
||||||
error: TRPCError
|
|
||||||
type: ProcedureType | 'unknown'
|
|
||||||
path: string | undefined
|
|
||||||
req: H3Event['req']
|
|
||||||
input: unknown
|
|
||||||
ctx: undefined | inferRouterContext<TRouter>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type OnErrorFn<TRouter extends AnyRouter> = (opts: OnErrorPayload<TRouter>) => void
|
|
||||||
|
|
||||||
export interface ResolveHTTPRequestOptions<TRouter extends AnyRouter> {
|
|
||||||
router: TRouter
|
|
||||||
createContext?: CreateContextFn<TRouter>
|
|
||||||
responseMeta?: ResponseMetaFn<TRouter>
|
|
||||||
onError?: OnErrorFn<TRouter>
|
|
||||||
batching?: {
|
|
||||||
enabled: boolean
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPath (event: H3Event): string | null {
|
|
||||||
if (typeof event.context.params.trpc === 'string') { return event.context.params.trpc }
|
|
||||||
|
|
||||||
if (Array.isArray(event.context.params.trpc)) { return event.context.params.trpc.join('/') }
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createNuxtApiHandler<TRouter extends AnyRouter> ({
|
|
||||||
router,
|
|
||||||
createContext,
|
|
||||||
responseMeta,
|
|
||||||
onError,
|
|
||||||
batching
|
|
||||||
}: ResolveHTTPRequestOptions<TRouter>) {
|
|
||||||
return defineEventHandler(async (event) => {
|
|
||||||
const {
|
|
||||||
req,
|
|
||||||
res
|
|
||||||
} = event
|
|
||||||
|
|
||||||
const $url = createURL(req.url!)
|
|
||||||
|
|
||||||
const path = getPath(event)
|
|
||||||
|
|
||||||
if (path === null) {
|
|
||||||
const error = router.getErrorShape({
|
|
||||||
error: new TRPCError({
|
|
||||||
message:
|
|
||||||
'Param "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
|
||||||
code: 'INTERNAL_SERVER_ERROR'
|
|
||||||
}),
|
|
||||||
type: 'unknown',
|
|
||||||
ctx: undefined,
|
|
||||||
path: undefined,
|
|
||||||
input: undefined
|
|
||||||
})
|
|
||||||
|
|
||||||
throw createError({
|
|
||||||
statusCode: 500,
|
|
||||||
statusMessage: JSON.stringify(error)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const httpResponse = await resolveHTTPResponse({
|
|
||||||
batching,
|
|
||||||
router,
|
|
||||||
req: {
|
|
||||||
method: req.method!,
|
|
||||||
headers: req.headers,
|
|
||||||
body: isMethod(event, 'GET') ? null : await readBody(event),
|
|
||||||
query: $url.searchParams
|
|
||||||
},
|
|
||||||
path,
|
|
||||||
createContext: async () => await createContext?.(event),
|
|
||||||
responseMeta,
|
|
||||||
onError: (o) => {
|
|
||||||
onError?.({
|
|
||||||
...o,
|
|
||||||
req
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const { status, headers, body } = httpResponse
|
|
||||||
|
|
||||||
res.statusCode = status
|
|
||||||
|
|
||||||
headers && Object.keys(headers).forEach((key) => {
|
|
||||||
res.setHeader(key, headers[key]!)
|
|
||||||
})
|
|
||||||
|
|
||||||
return body
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
import { defineConfig } from 'tsup'
|
import { defineConfig } from 'tsup'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
entry: ['src/index.ts', 'src/client/index.ts'],
|
entry: ['src/index.ts'],
|
||||||
format: ['cjs', 'esm'],
|
format: ['cjs', 'esm'],
|
||||||
splitting: false,
|
splitting: false,
|
||||||
clean: true,
|
clean: true,
|
||||||
external: ['#app', '#imports'],
|
external: ['#app', '#imports'],
|
||||||
dts: true,
|
dts: true,
|
||||||
outExtension({ format }) {
|
outExtension ({ format }) {
|
||||||
return {
|
return {
|
||||||
js: format === 'esm' ? '.mjs' : `.${format}`,
|
js: format === 'esm' ? '.mjs' : `.${format}`
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user