mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-27 10:20:39 +01:00
immediately execute mutations
This commit is contained in:
@@ -11,14 +11,13 @@ 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.todo.addTodo.mutate({
|
const x = await $client.todo.addTodo.mutate({
|
||||||
id: Date.now(),
|
id: Date.now(),
|
||||||
userId: 69,
|
userId: 69,
|
||||||
title,
|
title,
|
||||||
completed: false,
|
completed: false,
|
||||||
})
|
})
|
||||||
await result.execute()
|
console.log(x.data.value)
|
||||||
console.log('Todo: ', result.data.value)
|
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|||||||
@@ -11,8 +11,8 @@
|
|||||||
"import": "./dist/index.mjs"
|
"import": "./dist/index.mjs"
|
||||||
},
|
},
|
||||||
"./client": {
|
"./client": {
|
||||||
"require": "./dist/client.js",
|
"require": "./dist/client/index.js",
|
||||||
"import": "./dist/client.mjs"
|
"import": "./dist/client/index.mjs"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
@@ -37,7 +37,6 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"h3": "^0.8.5",
|
"h3": "^0.8.5",
|
||||||
"ohash": "^0.1.5",
|
"ohash": "^0.1.5",
|
||||||
"ohmyfetch": "^0.4.20",
|
|
||||||
"ufo": "^0.8.6"
|
"ufo": "^0.8.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import type { CreateTRPCClientOptions, inferRouterProxyClient } from '@trpc/client'
|
import type { CreateTRPCClientOptions, inferRouterProxyClient } from '@trpc/client'
|
||||||
import { createTRPCProxyClient } from '@trpc/client'
|
import { createTRPCProxyClient } from '@trpc/client'
|
||||||
import { FetchError } from 'ohmyfetch'
|
|
||||||
import type {
|
import type {
|
||||||
AnyRouter,
|
AnyRouter,
|
||||||
} from '@trpc/server'
|
} from '@trpc/server'
|
||||||
@@ -37,13 +36,6 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: strin
|
|||||||
|
|
||||||
const queryKey = getQueryKey(path, input)
|
const queryKey = getQueryKey(path, input)
|
||||||
|
|
||||||
if (lastArg === 'mutate') {
|
|
||||||
return useAsyncDataWithError(queryKey, () => (client as any)[path][lastArg](input), {
|
|
||||||
...asyncDataOptions as Record<string, any>,
|
|
||||||
immediate: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return useAsyncDataWithError(queryKey, () => (client as any)[path][lastArg](input), asyncDataOptions)
|
return useAsyncDataWithError(queryKey, () => (client as any)[path][lastArg](input), asyncDataOptions)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -79,17 +71,3 @@ export function createTRPCNuxtProxyClient<TRouter extends AnyRouter>(opts: Creat
|
|||||||
|
|
||||||
return decoratedClient
|
return decoratedClient
|
||||||
}
|
}
|
||||||
|
|
||||||
export function customFetch(input: RequestInfo | URL, options?: RequestInit) {
|
|
||||||
return globalThis.$fetch.raw(input.toString(), options)
|
|
||||||
.catch((e) => {
|
|
||||||
if (e instanceof FetchError && e.response)
|
|
||||||
return e.response
|
|
||||||
|
|
||||||
throw e
|
|
||||||
})
|
|
||||||
.then(response => ({
|
|
||||||
...response,
|
|
||||||
json: () => Promise.resolve(response._data),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user