mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-25 17:30:33 +01:00
fix publish issue
This commit is contained in:
7
package/playground/.gitignore
vendored
Normal file
7
package/playground/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
node_modules
|
||||
*.log*
|
||||
.nuxt
|
||||
.nitro
|
||||
.cache
|
||||
.output
|
||||
.env
|
||||
31
package/playground/README.md
Normal file
31
package/playground/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# dev playground
|
||||
|
||||
## Setup
|
||||
|
||||
Make sure to install the dependencies:
|
||||
|
||||
```bash
|
||||
pnpm install
|
||||
```
|
||||
|
||||
## Development Server
|
||||
|
||||
Start the development server on http://localhost:3000
|
||||
|
||||
```bash
|
||||
pnpm dev
|
||||
```
|
||||
|
||||
## Production
|
||||
|
||||
Build the application for production:
|
||||
|
||||
```bash
|
||||
pnpm build
|
||||
```
|
||||
|
||||
Locally preview production build:
|
||||
|
||||
```bash
|
||||
pnpm preview
|
||||
```
|
||||
18
package/playground/app.vue
Normal file
18
package/playground/app.vue
Normal file
@@ -0,0 +1,18 @@
|
||||
<script setup lang="ts">
|
||||
const { data, error } = await useAsyncQuery(['getUser', { username: 'jcena' }], {
|
||||
lazy: true,
|
||||
})
|
||||
|
||||
const client = useClient()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<div v-if="data">
|
||||
{{ JSON.stringify(data, null, 2) }}
|
||||
</div>
|
||||
<div v-else-if="error">
|
||||
asdx {{ JSON.stringify(error, null, 2) }}
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
13
package/playground/nuxt.config.ts
Normal file
13
package/playground/nuxt.config.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { defineNuxtConfig } from 'nuxt'
|
||||
import Module from '..'
|
||||
|
||||
// https://v3.nuxtjs.org/api/configuration/nuxt.config
|
||||
export default defineNuxtConfig({
|
||||
modules: [Module],
|
||||
runtimeConfig: {
|
||||
baseURL: 'http://localhost:3000',
|
||||
},
|
||||
typescript: {
|
||||
strict: true,
|
||||
},
|
||||
})
|
||||
4
package/playground/package.json
Normal file
4
package/playground/package.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "playground",
|
||||
"private": true
|
||||
}
|
||||
82
package/playground/server/trpc/index.ts
Normal file
82
package/playground/server/trpc/index.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
// ~/server/trpc/index.ts
|
||||
import { ZodError, z } from 'zod'
|
||||
import * as trpc from '@trpc/server'
|
||||
import type { inferAsyncReturnType } from '@trpc/server'
|
||||
import type { CompatibilityEvent } from 'h3'
|
||||
import type { ResponseMetaFnPayload } from 'trpc-nuxt/api'
|
||||
// import superjson from 'superjson'
|
||||
|
||||
const fakeUsers = [
|
||||
{ id: 1, username: 'jcena' },
|
||||
{ id: 2, username: 'dbatista' },
|
||||
{ id: 3, username: 'jbiden' },
|
||||
]
|
||||
|
||||
export const router = trpc
|
||||
.router<inferAsyncReturnType<typeof createContext>>()
|
||||
.formatError(({ shape, error }) => {
|
||||
return {
|
||||
...shape,
|
||||
data: {
|
||||
...shape.data,
|
||||
zodError:
|
||||
error.code === 'BAD_REQUEST'
|
||||
&& error.cause instanceof ZodError
|
||||
? error.cause.flatten()
|
||||
: null,
|
||||
},
|
||||
}
|
||||
})
|
||||
.query('getUsers', {
|
||||
resolve() {
|
||||
return fakeUsers
|
||||
},
|
||||
})
|
||||
.query('getUser', {
|
||||
// validate input with Zod
|
||||
input: z.object({
|
||||
username: z.string().min(5),
|
||||
}),
|
||||
resolve(req) {
|
||||
return fakeUsers.find(i => i.username === req.input.username) ?? null
|
||||
},
|
||||
})
|
||||
.mutation('createUser', {
|
||||
input: z.object({ username: z.string().min(5) }),
|
||||
resolve(req) {
|
||||
const newUser = {
|
||||
id: fakeUsers.length + 1,
|
||||
username: req.input.username,
|
||||
}
|
||||
fakeUsers.push(newUser)
|
||||
return newUser
|
||||
},
|
||||
})
|
||||
|
||||
export const createContext = (event: CompatibilityEvent) => {
|
||||
event.res.setHeader('x-ssr', 1)
|
||||
return {}
|
||||
}
|
||||
|
||||
export const responseMeta = (opts: ResponseMetaFnPayload<any>) => {
|
||||
// const nuxtApp = useNuxtApp()
|
||||
// const client = useClient()
|
||||
// console.log(opts)
|
||||
|
||||
// if (nuxtApp.nuxtState) {
|
||||
// nuxtApp.nuxtState.trpc = client.runtime.transformer.serialize({
|
||||
// ctx: opts.ctx,
|
||||
// errors: opts.errors,
|
||||
// })
|
||||
// }
|
||||
// else {
|
||||
// nuxtApp.nuxtState = {
|
||||
// trpc: client.runtime.transformer.serialize({
|
||||
// ctx: opts.ctx,
|
||||
// errors: opts.errors,
|
||||
// }),
|
||||
// }
|
||||
// }
|
||||
|
||||
return {}
|
||||
}
|
||||
Reference in New Issue
Block a user