|
| 1 | +import { NextResponse, NextRequest } from "next/server"; |
| 2 | +import type { PostgrestSingleResponse } from "@supabase/supabase-js"; |
| 3 | +import { createClient } from "~/utils/supabase/server"; |
| 4 | +import { |
| 5 | + createApiResponse, |
| 6 | + handleRouteError, |
| 7 | + defaultOptionsHandler, |
| 8 | + asPostgrestFailure, |
| 9 | +} from "~/utils/supabase/apiUtils"; |
| 10 | + |
| 11 | +type SyncTaskInfo = { |
| 12 | + worker: string; |
| 13 | + timeout?: string; |
| 14 | + task_interval?: string; |
| 15 | +}; |
| 16 | + |
| 17 | +const SYNC_DEFAULTS: Partial<SyncTaskInfo> = { |
| 18 | + timeout: "20s", |
| 19 | + task_interval: "45s", |
| 20 | +}; |
| 21 | + |
| 22 | +type ApiParams = Promise<{ target: string; fn: string }>; |
| 23 | +export type SegmentDataType = { params: ApiParams }; |
| 24 | + |
| 25 | +// POST with the SyncTaskInfo to the /supabase/sync-task/{function_name}/{target} endpoint |
| 26 | +export const POST = async ( |
| 27 | + request: NextRequest, |
| 28 | + segmentData: SegmentDataType, |
| 29 | +): Promise<NextResponse> => { |
| 30 | + try { |
| 31 | + const { target, fn } = await segmentData.params; |
| 32 | + const targetN = Number.parseInt(target); |
| 33 | + if (isNaN(targetN)) { |
| 34 | + return createApiResponse( |
| 35 | + request, |
| 36 | + asPostgrestFailure(`${target} is not a number`, "type"), |
| 37 | + ); |
| 38 | + } |
| 39 | + const info: SyncTaskInfo = { ...SYNC_DEFAULTS, ...(await request.json()) }; |
| 40 | + if (!info.worker) { |
| 41 | + return createApiResponse( |
| 42 | + request, |
| 43 | + asPostgrestFailure("Worker field is required", "invalid"), |
| 44 | + ); |
| 45 | + } |
| 46 | + const supabase = await createClient(); |
| 47 | + const response = (await supabase.rpc("propose_sync_task", { |
| 48 | + s_target: targetN, |
| 49 | + s_function: fn, |
| 50 | + s_worker: info.worker, |
| 51 | + timeout: info.timeout, |
| 52 | + task_interval: info.task_interval, |
| 53 | + })) as PostgrestSingleResponse<Date | null | boolean>; |
| 54 | + if (response.data === null) { |
| 55 | + // NextJS responses cannot handle null values, convert to boolean success indicator |
| 56 | + response.data = true; |
| 57 | + } |
| 58 | + |
| 59 | + return createApiResponse(request, response); |
| 60 | + } catch (e: unknown) { |
| 61 | + return handleRouteError(request, e, "/api/supabase/route"); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +// GET the sync_info table from /supabase/sync-task/{function_name}/{target} (should not be necessary) |
| 66 | +export const GET = async ( |
| 67 | + request: NextRequest, |
| 68 | + segmentData: SegmentDataType, |
| 69 | +): Promise<NextResponse> => { |
| 70 | + const { target, fn } = await segmentData.params; |
| 71 | + const targetN = Number.parseInt(target); |
| 72 | + if (isNaN(targetN)) { |
| 73 | + return createApiResponse( |
| 74 | + request, |
| 75 | + asPostgrestFailure(`${targetN} is not a number`, "type"), |
| 76 | + ); |
| 77 | + } |
| 78 | + const supabase = await createClient(); |
| 79 | + const response = await supabase |
| 80 | + .from("sync_info") |
| 81 | + .select() |
| 82 | + .eq("sync_target", targetN) |
| 83 | + .eq("sync_function", fn) |
| 84 | + .maybeSingle(); |
| 85 | + return createApiResponse(request, response); |
| 86 | +}; |
| 87 | + |
| 88 | +export const OPTIONS = defaultOptionsHandler; |
0 commit comments