mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-26 18:00:37 +01:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
285487e9bf | ||
|
|
2cfa64fcc6 |
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "trpc-nuxt",
|
"name": "trpc-nuxt",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.1.14",
|
"version": "0.1.15",
|
||||||
"packageManager": "pnpm@7.1.1",
|
"packageManager": "pnpm@7.1.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/module.cjs",
|
"main": "./dist/module.cjs",
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const client = useClient()
|
const { $client } = useNuxtApp()
|
||||||
const { data: todos, pending, error, refresh } = await useAsyncQuery(['getTodos'])
|
const { data: todos, pending, error, refresh } = await useAsyncQuery(['getTodos'])
|
||||||
|
|
||||||
const addTodo = async () => {
|
const addTodo = async () => {
|
||||||
const title = Math.random().toString(36).slice(2, 7)
|
const title = Math.random().toString(36).slice(2, 7)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await client.mutation('addTodo', {
|
const result = await $client.mutation('addTodo', {
|
||||||
id: Date.now(),
|
id: Date.now(),
|
||||||
userId: 69,
|
userId: 69,
|
||||||
title,
|
title,
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ export async function createContext(event: CompatibilityEvent) {
|
|||||||
// Will be available as `ctx` in all your resolvers
|
// Will be available as `ctx` in all your resolvers
|
||||||
|
|
||||||
// This is just an example of something you'd might want to do in your ctx fn
|
// This is just an example of something you'd might want to do in your ctx fn
|
||||||
// const x = useCookies(event)
|
const x = useCookies(event)
|
||||||
console.log('Headers', event.req.headers)
|
console.log(x)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ import * as trpc from '@trpc/client'
|
|||||||
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
|
import { defineNuxtPlugin, useRequestHeaders, useRuntimeConfig } from '#app'
|
||||||
import type { router } from '~/server/trpc'
|
import type { router } from '~/server/trpc'
|
||||||
|
|
||||||
type AppRouter = typeof router
|
declare type AppRouter = typeof router
|
||||||
|
|
||||||
const config = useRuntimeConfig().public.trpc
|
|
||||||
const client = trpc.createTRPCClient<AppRouter>({
|
|
||||||
url: `${config.baseURL}${config.trpcURL}`,
|
|
||||||
headers: useRequestHeaders(),
|
|
||||||
})
|
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
|
const config = useRuntimeConfig().public.trpc
|
||||||
|
const client = trpc.createTRPCClient<AppRouter>({
|
||||||
|
url: `${config.baseURL}${config.trpcURL}`,
|
||||||
|
headers: useRequestHeaders(),
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
provide: {
|
provide: {
|
||||||
client,
|
client,
|
||||||
|
|||||||
Reference in New Issue
Block a user