mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 20:19:33 +01:00
replace eslint config
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { CreateTRPCClientOptions, inferRouterProxyClient } from '@trpc/client'
|
||||
import { createTRPCProxyClient } from '@trpc/client'
|
||||
import type {
|
||||
AnyRouter,
|
||||
AnyRouter
|
||||
} from '@trpc/server'
|
||||
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
||||
import { hash } from 'ohash'
|
||||
@@ -13,14 +13,14 @@ import { getCurrentInstance, onScopeDispose, useAsyncData } from '#imports'
|
||||
/**
|
||||
* Calculates the key used for `useAsyncData` call
|
||||
*/
|
||||
export function getQueryKey(
|
||||
export function getQueryKey (
|
||||
path: string,
|
||||
input: unknown,
|
||||
input: unknown
|
||||
): string {
|
||||
return input === undefined ? path : `${path}-${hash(input || '')}`
|
||||
}
|
||||
|
||||
function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, client: inferRouterProxyClient<TRouter>) {
|
||||
function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: string, client: inferRouterProxyClient<TRouter>) {
|
||||
return createRecursiveProxy((opts) => {
|
||||
const args = opts.args
|
||||
|
||||
@@ -50,12 +50,12 @@ function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, clie
|
||||
|
||||
return useAsyncData(queryKey, () => (client as any)[path][lastArg](input, {
|
||||
signal: controller?.signal,
|
||||
...trpc,
|
||||
...trpc
|
||||
}), asyncDataOptions)
|
||||
})
|
||||
}
|
||||
|
||||
export function createTRPCNuxtProxyClient<TRouter extends AnyRouter>(opts: CreateTRPCClientOptions<TRouter>) {
|
||||
export function createTRPCNuxtProxyClient<TRouter extends AnyRouter> (opts: CreateTRPCClientOptions<TRouter>) {
|
||||
const client = createTRPCProxyClient(opts)
|
||||
|
||||
const decoratedClient = createFlatProxy((key) => {
|
||||
|
||||
@@ -4,10 +4,10 @@ import type {
|
||||
AnyRouter,
|
||||
ProcedureType,
|
||||
inferRouterContext,
|
||||
inferRouterError,
|
||||
inferRouterError
|
||||
} from '@trpc/server'
|
||||
import {
|
||||
TRPCError,
|
||||
TRPCError
|
||||
} from '@trpc/server'
|
||||
import { createURL } from 'ufo'
|
||||
import type { H3Event } from 'h3'
|
||||
@@ -49,27 +49,25 @@ export interface ResolveHTTPRequestOptions<TRouter extends AnyRouter> {
|
||||
}
|
||||
}
|
||||
|
||||
function getPath(event: H3Event): string | null {
|
||||
if (typeof event.context.params.trpc === 'string')
|
||||
return event.context.params.trpc
|
||||
function getPath (event: H3Event): string | null {
|
||||
if (typeof event.context.params.trpc === 'string') { return event.context.params.trpc }
|
||||
|
||||
if (Array.isArray(event.context.params.trpc))
|
||||
return event.context.params.trpc.join('/')
|
||||
if (Array.isArray(event.context.params.trpc)) { return event.context.params.trpc.join('/') }
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function createNuxtApiHandler<TRouter extends AnyRouter>({
|
||||
export function createNuxtApiHandler<TRouter extends AnyRouter> ({
|
||||
router,
|
||||
createContext,
|
||||
responseMeta,
|
||||
onError,
|
||||
batching,
|
||||
batching
|
||||
}: ResolveHTTPRequestOptions<TRouter>) {
|
||||
return defineEventHandler(async (event) => {
|
||||
const {
|
||||
req,
|
||||
res,
|
||||
res
|
||||
} = event
|
||||
|
||||
const $url = createURL(req.url!)
|
||||
@@ -81,17 +79,17 @@ export function createNuxtApiHandler<TRouter extends AnyRouter>({
|
||||
error: new TRPCError({
|
||||
message:
|
||||
'Param "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
||||
code: 'INTERNAL_SERVER_ERROR',
|
||||
code: 'INTERNAL_SERVER_ERROR'
|
||||
}),
|
||||
type: 'unknown',
|
||||
ctx: undefined,
|
||||
path: undefined,
|
||||
input: undefined,
|
||||
input: undefined
|
||||
})
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: JSON.stringify(error),
|
||||
statusMessage: JSON.stringify(error)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -102,17 +100,17 @@ export function createNuxtApiHandler<TRouter extends AnyRouter>({
|
||||
method: req.method!,
|
||||
headers: req.headers,
|
||||
body: isMethod(event, 'GET') ? null : await readBody(event),
|
||||
query: $url.searchParams,
|
||||
query: $url.searchParams
|
||||
},
|
||||
path,
|
||||
createContext: async () => createContext?.(event),
|
||||
createContext: async () => await createContext?.(event),
|
||||
responseMeta,
|
||||
onError: (o) => {
|
||||
onError?.({
|
||||
...o,
|
||||
req,
|
||||
req
|
||||
})
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const { status, headers, body } = httpResponse
|
||||
|
||||
Reference in New Issue
Block a user