immediately execute mutations

This commit is contained in:
Robert Soriano
2022-10-30 01:52:20 -07:00
parent 025ce7e028
commit b45c613ce7
3 changed files with 4 additions and 28 deletions

View File

@@ -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)

View File

@@ -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": {

View File

@@ -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),
}))
}