Я пытаюсь создать работника CloudFlare, используя itty-router. Следуя руководству по TypeScript, мы должны определить типы TypeScript, как в коде ниже.Argument of type '({ content }: IRequest, env: Args[0], ctx: Args[1]) => Promise' is not assignable to parameter of type 'RequestHandler'.
Target signature provides too few arguments. Expected 3 or more, but got 1.ts(2345)
< /code>
import { createClient } from '@supabase/supabase-js'
import { AutoRouter, RequestHandler, IRequest, withContent } from 'itty-router'
interface Env {
SUPABASE_URL: string
SUPABASE_KEY: string
}
type CFArgs = [Env, ExecutionContext]
const router = AutoRouter()
router.post(
'/clients',
withContent,
// async (request: RequestHandler) => {
// async ({ content }: IRequest, env: Env, ctx: ExecutionContext) => {
// async ({ content }, env, ctx) => {
async ({ content }, env, ctx) => {
try {
const body = content as { [key: string]: any }
if (!body || Object.keys(body).length === 0) {
throw new Error('Invalid request body: Empty or undefined')
}
const newClient = { ...body }
const supabase = createClient(env.SUPABASE_URL, env.SUPABASE_KEY)
const { data, error } = await supabase.from('clients').insert(newClient)
if (error) throw new Error(`Supabase error: ${error.message}`)
return {
success: true,
status: 200,
data: newClient,
error: null,
}
} catch (error: any) {
return { success: false, status: 500, data: null, error: error.message }
}
}
)
Подробнее здесь: https://stackoverflow.com/questions/796 ... middleware