Merge pull request #51 from cawa-93/patch-1

docs: Replace regular `fetch` with a `$fetch` from nuxt
This commit is contained in:
Robert Soriano
2022-12-12 20:42:12 -08:00
committed by GitHub

View File

@@ -112,11 +112,31 @@ export default defineNuxtPlugin(() => {
const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: '/api/trpc',
/**
* If you want to use SSR, you need to use the server's full URL
* @link https://trpc.io/docs/ssr
**/
url: 'http://localhost:3000/api/trpc',
* Replace regular `fetch` with a `$fetch` from nuxt
*
* During server-side rendering, calling $fetch to fetch your internal API routes
* will directly call the relevant function (emulating the request),
* saving an additional API call.
*
* @see https://nuxt.com/docs/api/utils/dollarfetch
*/
fetch: (input, options) =>
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),
})),
}),
],
})