mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-25 17:30:33 +01:00
Compare commits
13 Commits
v0.4.0-bet
...
v0.4.0-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebb6637238 | ||
|
|
0c83ade918 | ||
|
|
97d9b3ddf8 | ||
|
|
eaa61f3700 | ||
|
|
a5a9f430d5 | ||
|
|
94f59c396a | ||
|
|
fe4c10ae25 | ||
|
|
7a97127353 | ||
|
|
0c4a43ec19 | ||
|
|
da6d738406 | ||
|
|
70c0b35c48 | ||
|
|
da52b350ba | ||
|
|
b45c613ce7 |
1
docs/README.md
Normal file
1
docs/README.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
docs
|
||||||
5
docs/app.config.ts
Normal file
5
docs/app.config.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export default defineAppConfig({
|
||||||
|
docus: {
|
||||||
|
title: 'tRPC Nuxt',
|
||||||
|
},
|
||||||
|
})
|
||||||
90
docs/content/1.get-started/1.installation.md
Normal file
90
docs/content/1.get-started/1.installation.md
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
---
|
||||||
|
title: Installation
|
||||||
|
description: tRPC-Nuxt provides first class integration with tRPC.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
|
||||||
|
## 1. Add to existing Nuxt project
|
||||||
|
|
||||||
|
::code-group
|
||||||
|
|
||||||
|
```bash [pnpm]
|
||||||
|
pnpm add @trpc/server@next @trpc/client@next trpc-nuxt@next zod
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash [npm]
|
||||||
|
npm install @trpc/server@next @trpc/client@next trpc-nuxt@next zod
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash [yarn]
|
||||||
|
yarn add @trpc/server@next @trpc/client@next trpc-nuxt@next zod
|
||||||
|
```
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
#### Why @trpc/server?
|
||||||
|
|
||||||
|
For implementing tRPC endpoints and routers.
|
||||||
|
|
||||||
|
#### Why @trpc/client?
|
||||||
|
|
||||||
|
For making typesafe API calls from your client.
|
||||||
|
|
||||||
|
#### Why zod?
|
||||||
|
|
||||||
|
Most examples use [Zod](https://github.com/colinhacks/zod) for input validation and tRPC.io highly recommends it, though it isn't required.
|
||||||
|
|
||||||
|
## 2. Enable strict mode
|
||||||
|
|
||||||
|
If you want to use Zod for input validation, make sure you have enabled strict mode:
|
||||||
|
|
||||||
|
::code-group
|
||||||
|
|
||||||
|
```json [tsconfig.json]
|
||||||
|
{
|
||||||
|
"extends": "./.nuxt/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"strict": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [nuxt.config.ts]
|
||||||
|
export default defineNuxtConfig({
|
||||||
|
typescript: {
|
||||||
|
strict: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
If strict mode is too much, at least enable `strictNullChecks`:
|
||||||
|
|
||||||
|
::code-group
|
||||||
|
|
||||||
|
```json [tsconfig.json]
|
||||||
|
{
|
||||||
|
"extends": "./.nuxt/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"strictNullChecks": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [nuxt.config.ts]
|
||||||
|
export default defineNuxtConfig({
|
||||||
|
typescript: {
|
||||||
|
tsConfig: {
|
||||||
|
strictNullChecks: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
Now that you've installed the required dependencies, you are ready to start building your application.
|
||||||
140
docs/content/1.get-started/2.usage.md
Normal file
140
docs/content/1.get-started/2.usage.md
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
---
|
||||||
|
title: Usage
|
||||||
|
description: tRPC-Nuxt provides first class integration with tRPC.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
|
||||||
|
## Recommended file structure
|
||||||
|
|
||||||
|
Recommended but not enforced file structure. This is what you get when starting from [the examples](../main/example-apps.md).
|
||||||
|
|
||||||
|
```graphql
|
||||||
|
.
|
||||||
|
├── server
|
||||||
|
│ ├── api
|
||||||
|
│ │ └── trpc
|
||||||
|
│ │ └── [trpc].ts # <-- tRPC HTTP handler
|
||||||
|
│ │ └── [..]
|
||||||
|
│ ├── trpc
|
||||||
|
│ │ ├── routers
|
||||||
|
│ │ │ ├── index.ts # <-- main app router
|
||||||
|
│ │ │ ├── todo.ts # <-- sub routers
|
||||||
|
│ │ │ └── [..]
|
||||||
|
│ │ ├── context.ts # <-- create app context
|
||||||
|
│ │ └── trpc.ts # <-- procedure helpers
|
||||||
|
├── plugins
|
||||||
|
│ ├── client.ts # <-- tRPC Client as a plugin
|
||||||
|
└── [..]
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1. Create a tRPC router
|
||||||
|
|
||||||
|
Initialize your tRPC backend using the `initTRPC` function and create your first router.
|
||||||
|
|
||||||
|
::code-group
|
||||||
|
|
||||||
|
```ts [server/trpc/trpc.ts]
|
||||||
|
import { TRPCError, initTRPC } from '@trpc/server'
|
||||||
|
|
||||||
|
// Avoid exporting the entire t-object since it's not very
|
||||||
|
// descriptive and can be confusing to newcomers used to t
|
||||||
|
// meaning translation in i18n libraries.
|
||||||
|
const t = initTRPC.create()
|
||||||
|
|
||||||
|
// Base router and procedure helpers
|
||||||
|
export const router = t.router
|
||||||
|
export const publicProcedure = t.procedure
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [server/trpc/routers/index.ts]
|
||||||
|
import { z } from 'zod'
|
||||||
|
import { publicProcedure, router } from '..'
|
||||||
|
|
||||||
|
export const appRouter = router({
|
||||||
|
hello: publicProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
text: z.string().nullish(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.query(({ input }) => {
|
||||||
|
return {
|
||||||
|
greeting: `hello ${input?.text ?? 'world'}`,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
// export type definition of API
|
||||||
|
export type AppRouter = typeof appRouter
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts [server/api/trpc/[trpc].ts]
|
||||||
|
import { createNuxtApiHandler } from 'trpc-nuxt'
|
||||||
|
import { appRouter } from '../../trpc/routers'
|
||||||
|
|
||||||
|
// export API handler
|
||||||
|
export default createNuxtApiHandler({
|
||||||
|
router: appRouter,
|
||||||
|
createContext: () => ({}),
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
::alert{type=info}
|
||||||
|
If you need to split your router into several subrouters, you can implement them in the `server/trpc/routers` directory and import and [merge them](https://trpc.io/docs/v10/merging-routers) to a single root `appRouter`.
|
||||||
|
::
|
||||||
|
|
||||||
|
## 2. Create tRPC client plugin
|
||||||
|
|
||||||
|
Create a set of strongly-typed composables using your API's type signature.
|
||||||
|
|
||||||
|
```ts [plugins/client.ts]
|
||||||
|
import { httpBatchLink } from '@trpc/client'
|
||||||
|
import { createTRPCNuxtProxyClient } from 'trpc-nuxt/client'
|
||||||
|
import type { AppRouter } from '~~/server/trpc/routers'
|
||||||
|
|
||||||
|
export default defineNuxtPlugin(() => {
|
||||||
|
const client = createTRPCNuxtProxyClient<AppRouter>({
|
||||||
|
links: [
|
||||||
|
httpBatchLink({
|
||||||
|
/**
|
||||||
|
* 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',
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
provide: {
|
||||||
|
client,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Make API requests
|
||||||
|
|
||||||
|
```vue [pages/index.vue]
|
||||||
|
<script setup lang="ts">
|
||||||
|
const { $client } = useNuxtApp()
|
||||||
|
|
||||||
|
// query and mutate uses useAsyncData under the hood
|
||||||
|
const { data, pending, error } = await $client.hello.query({ text: 'client' })
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div v-if="pending">
|
||||||
|
Loading...
|
||||||
|
</div>
|
||||||
|
<div v-else-if="error?.data?.code">
|
||||||
|
Error: {{ error.data.code }}
|
||||||
|
</div>
|
||||||
|
<div v-else>
|
||||||
|
<p>{{ hello.data?.greeting }}</p>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
```
|
||||||
6
docs/content/2.examples/1.basic.md
Normal file
6
docs/content/2.examples/1.basic.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
title: Basic
|
||||||
|
description: tRPC-Nuxt provides first class integration with tRPC.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Basic Example
|
||||||
6
docs/content/2.examples/2.multiple-routers.md
Normal file
6
docs/content/2.examples/2.multiple-routers.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
title: Multiple Routers
|
||||||
|
description: tRPC-Nuxt provides first class integration with tRPC.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Multi Routers
|
||||||
31
docs/content/index.md
Normal file
31
docs/content/index.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
---
|
||||||
|
title: "tRPC Nuxt"
|
||||||
|
description: "A supa simple wrapper arousnd supabase-js to enable usage and integration within Nuxt."
|
||||||
|
navigation: false
|
||||||
|
layout: page
|
||||||
|
---
|
||||||
|
|
||||||
|
::block-hero
|
||||||
|
---
|
||||||
|
cta:
|
||||||
|
- Get Started
|
||||||
|
- /get-started/installation
|
||||||
|
secondary:
|
||||||
|
- Star on GitHub ->
|
||||||
|
- https://github.com/wobsoriano/trpc-nuxt
|
||||||
|
snippet: npm install trpc-nuxt@next
|
||||||
|
---
|
||||||
|
|
||||||
|
#title
|
||||||
|
tRPC [Nuxt]{.text-primary-500}
|
||||||
|
|
||||||
|
#description
|
||||||
|
End-to-end typesafe APIs in Nuxt applications.
|
||||||
|
|
||||||
|
#extra
|
||||||
|
::list
|
||||||
|
- Automatic typesafety
|
||||||
|
- Snappy DX
|
||||||
|
- Autocompletion
|
||||||
|
::
|
||||||
|
::
|
||||||
16
docs/nuxt.config.ts
Normal file
16
docs/nuxt.config.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
export default defineNuxtConfig({
|
||||||
|
app: {
|
||||||
|
pageTransition: false,
|
||||||
|
layoutTransition: false,
|
||||||
|
},
|
||||||
|
extends: '@nuxt-themes/docus',
|
||||||
|
build: {
|
||||||
|
transpile: [/content-edge/],
|
||||||
|
},
|
||||||
|
nitro: {
|
||||||
|
prerender: {
|
||||||
|
crawlLinks: true,
|
||||||
|
routes: ['/'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
17
docs/package.json
Normal file
17
docs/package.json
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "docs",
|
||||||
|
"description": "Docs for TRPC-Nuxt",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nuxi dev",
|
||||||
|
"build": "nuxi build",
|
||||||
|
"generate": "nuxi build",
|
||||||
|
"preview": "nuxi preview"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@docus/github": "npm:@docus/github-edge@latest",
|
||||||
|
"@nuxt-themes/docus": "npm:@nuxt-themes/docus-edge@0.1.0-2a7c428",
|
||||||
|
"@nuxt-themes/website": "0.1.7",
|
||||||
|
"nuxt": "^3.0.0-rc.12",
|
||||||
|
"vue-plausible": "^1.3.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
25
docs/tokens.config.ts
Normal file
25
docs/tokens.config.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { defineTheme } from 'pinceau'
|
||||||
|
|
||||||
|
export default defineTheme({
|
||||||
|
title: 'asddsasda 3s',
|
||||||
|
cover: {
|
||||||
|
src: 'https://res.cloudinary.com/nuxt/image/upload/v1650870623/nuxt3-rc-social_z6qh3m.png',
|
||||||
|
alt: 'Nuxt 3 cover image',
|
||||||
|
},
|
||||||
|
aside: {
|
||||||
|
level: 1,
|
||||||
|
},
|
||||||
|
// colors: {
|
||||||
|
// primary: {
|
||||||
|
// 100: '#77b0db',
|
||||||
|
// 200: '#589ed3',
|
||||||
|
// 300: '#4e98d0',
|
||||||
|
// 400: '#398ccb',
|
||||||
|
// 500: '#398ccb',
|
||||||
|
// 600: '#2BAB71',
|
||||||
|
// 700: '#317eb9',
|
||||||
|
// 800: '#2e77af',
|
||||||
|
// 900: '#266290',
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
})
|
||||||
3
docs/tsconfig.json
Normal file
3
docs/tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"extends": "./.nuxt/tsconfig.json"
|
||||||
|
}
|
||||||
48
package.json
48
package.json
@@ -1,15 +1,55 @@
|
|||||||
{
|
{
|
||||||
"name": "trpc-nuxt-workspace",
|
"name": "trpc-nuxt",
|
||||||
"packageManager": "pnpm@7.5.0",
|
"version": "0.4.0-beta.4",
|
||||||
|
"packageManager": "pnpm@7.14.1",
|
||||||
|
"license": "MIT",
|
||||||
|
"sideEffects": false,
|
||||||
|
"exports": {
|
||||||
|
"./package.json": "./package.json",
|
||||||
|
".": {
|
||||||
|
"require": "./dist/index.js",
|
||||||
|
"import": "./dist/index.mjs"
|
||||||
|
},
|
||||||
|
"./client": {
|
||||||
|
"require": "./dist/client/index.js",
|
||||||
|
"import": "./dist/client/index.mjs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"files": [
|
||||||
|
"dist",
|
||||||
|
"client.d.ts"
|
||||||
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"prepublishOnly": "nr build",
|
||||||
|
"build": "tsup",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"lint:fix": "eslint . --fix"
|
"lint:fix": "eslint . --fix",
|
||||||
|
"release": "bumpp && npm publish"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@trpc/client": "^10.0.0-proxy-beta.21",
|
||||||
|
"@trpc/server": "^10.0.0-proxy-beta.21",
|
||||||
|
"nuxt": "^3.0.0-rc.12"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"h3": "^0.8.5",
|
||||||
|
"ohash": "^0.1.5",
|
||||||
|
"ufo": "^0.8.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@antfu/eslint-config": "^0.27.0",
|
"@antfu/eslint-config": "^0.27.0",
|
||||||
"@antfu/ni": "^0.18.3",
|
"@antfu/ni": "^0.18.3",
|
||||||
|
"@trpc/client": "10.0.0-rc.2",
|
||||||
|
"@trpc/server": "10.0.0-rc.2",
|
||||||
|
"bumpp": "^8.2.1",
|
||||||
"eslint": "^8.25.0",
|
"eslint": "^8.25.0",
|
||||||
"pnpm": "^7.5.0"
|
"nuxt": "3.0.0-rc.12",
|
||||||
|
"pnpm": "^7.14.1",
|
||||||
|
"tsup": "6.0.1",
|
||||||
|
"typescript": "^4.7.4"
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"extends": "@antfu",
|
"extends": "@antfu",
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "trpc-nuxt",
|
|
||||||
"version": "0.4.0-beta.2",
|
|
||||||
"packageManager": "pnpm@7.5.0",
|
|
||||||
"license": "MIT",
|
|
||||||
"sideEffects": false,
|
|
||||||
"exports": {
|
|
||||||
"./package.json": "./package.json",
|
|
||||||
".": {
|
|
||||||
"require": "./dist/index.js",
|
|
||||||
"import": "./dist/index.mjs"
|
|
||||||
},
|
|
||||||
"./client": {
|
|
||||||
"require": "./dist/client.js",
|
|
||||||
"import": "./dist/client.mjs"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"main": "./dist/index.js",
|
|
||||||
"module": "./dist/index.mjs",
|
|
||||||
"types": "./dist/index.d.ts",
|
|
||||||
"files": [
|
|
||||||
"dist",
|
|
||||||
"client.d.ts"
|
|
||||||
],
|
|
||||||
"scripts": {
|
|
||||||
"prepublishOnly": "nr build",
|
|
||||||
"build": "tsup",
|
|
||||||
"lint": "eslint .",
|
|
||||||
"lint:fix": "eslint . --fix",
|
|
||||||
"release": "bumpp --commit --push --tag && npm publish"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"@trpc/client": "^10.0.0-proxy-beta.21",
|
|
||||||
"@trpc/server": "^10.0.0-proxy-beta.21",
|
|
||||||
"nuxt": "^3.0.0-rc.12"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"h3": "^0.8.5",
|
|
||||||
"ohash": "^0.1.5",
|
|
||||||
"ohmyfetch": "^0.4.20",
|
|
||||||
"ufo": "^0.8.6"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@trpc/client": "10.0.0-rc.1",
|
|
||||||
"@trpc/server": "10.0.0-rc.1",
|
|
||||||
"bumpp": "^8.2.1",
|
|
||||||
"nuxt": "3.0.0-rc.12",
|
|
||||||
"tsup": "6.0.1",
|
|
||||||
"typescript": "^4.7.4"
|
|
||||||
},
|
|
||||||
"eslintConfig": {
|
|
||||||
"extends": "@antfu",
|
|
||||||
"rules": {
|
|
||||||
"no-console": "warn"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../../tsconfig.json"
|
|
||||||
}
|
|
||||||
@@ -9,6 +9,8 @@
|
|||||||
"postinstall": "nuxt prepare"
|
"postinstall": "nuxt prepare"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@trpc/client": "10.0.0-rc.2",
|
||||||
|
"@trpc/server": "10.0.0-rc.2",
|
||||||
"superjson": "^1.11.0",
|
"superjson": "^1.11.0",
|
||||||
"trpc-nuxt": "workspace:*",
|
"trpc-nuxt": "workspace:*",
|
||||||
"zod": "^3.19.1"
|
"zod": "^3.19.1"
|
||||||
@@ -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)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { router } from '..'
|
import { router } from '../trpc'
|
||||||
import { todoRouter } from './todo'
|
import { todoRouter } from './todo'
|
||||||
import { userRouter } from './user'
|
import { userRouter } from './user'
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { publicProcedure, router } from '..'
|
import { publicProcedure, router } from '../trpc'
|
||||||
|
|
||||||
const baseURL = 'https://jsonplaceholder.typicode.com'
|
const baseURL = 'https://jsonplaceholder.typicode.com'
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { publicProcedure, router } from '..'
|
import { publicProcedure, router } from '../trpc'
|
||||||
|
|
||||||
const baseURL = 'https://jsonplaceholder.typicode.com'
|
const baseURL = 'https://jsonplaceholder.typicode.com'
|
||||||
|
|
||||||
3365
pnpm-lock.yaml
generated
3365
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,2 +1,3 @@
|
|||||||
packages:
|
packages:
|
||||||
- packages/*
|
- playground
|
||||||
|
- docs
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
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'
|
||||||
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
||||||
import { hash } from 'ohash'
|
import { hash } from 'ohash'
|
||||||
import type { DecoratedProcedureRecord } from './types'
|
import type { DecoratedProcedureRecord } from './types'
|
||||||
|
// @ts-ignore: nuxt internal
|
||||||
|
import { useAsyncData, useState } from '#app'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the key used for `useAsyncData` call
|
* Calculates the key used for `useAsyncData` call
|
||||||
@@ -28,7 +29,7 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: strin
|
|||||||
const pathCopy = [name, ...opts.path]
|
const pathCopy = [name, ...opts.path]
|
||||||
|
|
||||||
// The last arg is for instance `.mutate` or `.query()`
|
// The last arg is for instance `.mutate` or `.query()`
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
||||||
const lastArg = pathCopy.pop()!
|
const lastArg = pathCopy.pop()!
|
||||||
|
|
||||||
const path = pathCopy.join('.')
|
const path = pathCopy.join('.')
|
||||||
@@ -37,13 +38,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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -52,9 +46,7 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: strin
|
|||||||
* Custom useAsyncData to add server error to client
|
* Custom useAsyncData to add server error to client
|
||||||
*/
|
*/
|
||||||
async function useAsyncDataWithError(queryKey: string, cb: any, asyncDataOptions: any) {
|
async function useAsyncDataWithError(queryKey: string, cb: any, asyncDataOptions: any) {
|
||||||
// @ts-ignore: nuxt internal
|
|
||||||
const serverError = useState(`error-${queryKey}`, () => null)
|
const serverError = useState(`error-${queryKey}`, () => null)
|
||||||
// @ts-ignore: nuxt internal
|
|
||||||
const { error, data, ...rest } = await useAsyncData(queryKey, cb, asyncDataOptions)
|
const { error, data, ...rest } = await useAsyncData(queryKey, cb, asyncDataOptions)
|
||||||
|
|
||||||
if (error.value && !serverError.value)
|
if (error.value && !serverError.value)
|
||||||
@@ -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),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"outDir": "dist",
|
"resolveJsonModule": true,
|
||||||
"resolveJsonModule": true
|
"skipDefaultLibCheck": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user