mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-29 19:30:34 +01:00
eslint fixes
This commit is contained in:
@@ -3,25 +3,25 @@ import { createTRPCNuxtProxyClient } from 'trpc-nuxt/client'
|
|||||||
import superjson from 'superjson'
|
import superjson from 'superjson'
|
||||||
import type { AppRouter } from '~~/server/trpc/routers'
|
import type { AppRouter } from '~~/server/trpc/routers'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin(() => {
|
||||||
const client = createTRPCNuxtProxyClient<AppRouter>({
|
const client = createTRPCNuxtProxyClient<AppRouter>({
|
||||||
transformer: superjson,
|
transformer: superjson,
|
||||||
links: [
|
links: [
|
||||||
// adds pretty logs to your console in development and logs errors in production
|
// adds pretty logs to your console in development and logs errors in production
|
||||||
loggerLink({
|
loggerLink({
|
||||||
enabled: opts =>
|
enabled: opts =>
|
||||||
process.env.NODE_ENV === 'development'
|
process.env.NODE_ENV === 'development' ||
|
||||||
|| (opts.direction === 'down' && opts.result instanceof Error),
|
(opts.direction === 'down' && opts.result instanceof Error)
|
||||||
}),
|
}),
|
||||||
httpBatchLink({
|
httpBatchLink({
|
||||||
url: 'http://localhost:3000/api/trpc',
|
url: 'http://localhost:3000/api/trpc'
|
||||||
}),
|
})
|
||||||
],
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
provide: {
|
provide: {
|
||||||
client,
|
client
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ export default createNuxtApiHandler({
|
|||||||
* @link https://trpc.io/docs/context
|
* @link https://trpc.io/docs/context
|
||||||
*/
|
*/
|
||||||
createContext,
|
createContext,
|
||||||
onError({ error }) {
|
onError ({ error }) {
|
||||||
if (error.code === 'INTERNAL_SERVER_ERROR') {
|
if (error.code === 'INTERNAL_SERVER_ERROR') {
|
||||||
// send to bug reporting
|
// send to bug reporting
|
||||||
console.error('Something went wrong', error)
|
console.error('Something went wrong', error)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
/**
|
/**
|
||||||
* @link https://trpc.io/docs/caching#api-response-caching
|
* @link https://trpc.io/docs/caching#api-response-caching
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ export type Context = inferAsyncReturnType<typeof createContext>
|
|||||||
* Creates context for an incoming request
|
* Creates context for an incoming request
|
||||||
* @link https://trpc.io/docs/context
|
* @link https://trpc.io/docs/context
|
||||||
*/
|
*/
|
||||||
export async function createContext(
|
export function createContext (
|
||||||
opts: H3Event,
|
opts: H3Event
|
||||||
) {
|
) {
|
||||||
// for API-response caching see https://trpc.io/docs/caching
|
// for API-response caching see https://trpc.io/docs/caching
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { userRouter } from './user'
|
|||||||
|
|
||||||
export const appRouter = router({
|
export const appRouter = router({
|
||||||
todo: todoRouter,
|
todo: todoRouter,
|
||||||
user: userRouter,
|
user: userRouter
|
||||||
})
|
})
|
||||||
|
|
||||||
export type AppRouter = typeof appRouter
|
export type AppRouter = typeof appRouter
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const TodoShape = z.object({
|
|||||||
userId: z.number(),
|
userId: z.number(),
|
||||||
id: z.number(),
|
id: z.number(),
|
||||||
title: z.string(),
|
title: z.string(),
|
||||||
completed: z.boolean(),
|
completed: z.boolean()
|
||||||
})
|
})
|
||||||
|
|
||||||
export type Todo = z.infer<typeof TodoShape>
|
export type Todo = z.infer<typeof TodoShape>
|
||||||
@@ -27,7 +27,7 @@ export const todoRouter = router({
|
|||||||
.mutation((req) => {
|
.mutation((req) => {
|
||||||
return $fetch<Todo>(`${baseURL}/todos`, {
|
return $fetch<Todo>(`${baseURL}/todos`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: req.input,
|
body: req.input
|
||||||
})
|
})
|
||||||
}),
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const UserShape = z.object({
|
|||||||
id: z.number(),
|
id: z.number(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
username: z.string(),
|
username: z.string(),
|
||||||
email: z.string(),
|
email: z.string()
|
||||||
})
|
})
|
||||||
|
|
||||||
export type User = z.infer<typeof UserShape>
|
export type User = z.infer<typeof UserShape>
|
||||||
@@ -27,7 +27,7 @@ export const userRouter = router({
|
|||||||
.mutation((req) => {
|
.mutation((req) => {
|
||||||
return $fetch<User>(`${baseURL}/users`, {
|
return $fetch<User>(`${baseURL}/users`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: req.input,
|
body: req.input
|
||||||
})
|
})
|
||||||
}),
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,19 +5,19 @@ import type { Context } from './context'
|
|||||||
|
|
||||||
const t = initTRPC.context<Context>().create({
|
const t = initTRPC.context<Context>().create({
|
||||||
transformer: superjson,
|
transformer: superjson,
|
||||||
errorFormatter({ shape, error }) {
|
errorFormatter ({ shape, error }) {
|
||||||
return {
|
return {
|
||||||
...shape,
|
...shape,
|
||||||
data: {
|
data: {
|
||||||
...shape.data,
|
...shape.data,
|
||||||
zodError:
|
zodError:
|
||||||
error.code === 'BAD_REQUEST'
|
error.code === 'BAD_REQUEST' &&
|
||||||
&& error.cause instanceof ZodError
|
error.cause instanceof ZodError
|
||||||
? error.cause!.flatten()
|
? error.cause!.flatten()
|
||||||
: null,
|
: null
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user