|
| 1 | +import type { Logger } from '@sim/logger' |
| 2 | +import { NextResponse } from 'next/server' |
| 3 | +import { processFilesToUserFiles, type RawFileInput } from '@/lib/uploads/utils/file-utils' |
| 4 | +import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server' |
| 5 | +import { assertToolFileAccess } from '@/app/api/files/authorization' |
| 6 | +import { mapPsp, UPTIMEROBOT_API_BASE } from '@/tools/uptimerobot/types' |
| 7 | + |
| 8 | +/** Fields shared by the PSP create and update routes (before the files). */ |
| 9 | +interface PspFormFields { |
| 10 | + friendlyName?: string | null |
| 11 | + monitorIds?: string | null |
| 12 | + status?: string | null |
| 13 | + password?: string | null |
| 14 | + customDomain?: string | null |
| 15 | + hideUrlLinks?: boolean | null |
| 16 | + noIndex?: boolean | null |
| 17 | + logo?: unknown |
| 18 | + icon?: unknown |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Appends a single optional image file (logo or icon) to the form after |
| 23 | + * downloading it from storage and verifying the caller may access it. |
| 24 | + * |
| 25 | + * @returns an error `NextResponse` if access is denied, otherwise `null`. |
| 26 | + */ |
| 27 | +async function appendPspImage( |
| 28 | + form: FormData, |
| 29 | + field: 'logo' | 'icon', |
| 30 | + file: unknown, |
| 31 | + userId: string, |
| 32 | + requestId: string, |
| 33 | + logger: Logger |
| 34 | +): Promise<NextResponse | null> { |
| 35 | + const userFiles = processFilesToUserFiles([file as RawFileInput], requestId, logger) |
| 36 | + if (userFiles.length === 0) return null |
| 37 | + |
| 38 | + const userFile = userFiles[0] |
| 39 | + const denied = await assertToolFileAccess(userFile.key, userId, requestId, logger) |
| 40 | + if (denied) return denied |
| 41 | + |
| 42 | + const buffer = await downloadFileFromStorage(userFile, requestId, logger) |
| 43 | + const mimeType = userFile.type || 'application/octet-stream' |
| 44 | + form.append(field, new Blob([new Uint8Array(buffer)], { type: mimeType }), userFile.name) |
| 45 | + return null |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Builds the multipart form for a PSP request, downloads any referenced |
| 50 | + * logo/icon files, forwards the request to UptimeRobot, and returns a typed |
| 51 | + * `{ success, output: { psp } }` envelope as a `NextResponse`. |
| 52 | + */ |
| 53 | +export async function forwardPspRequest(options: { |
| 54 | + apiKey: string |
| 55 | + method: 'POST' | 'PATCH' |
| 56 | + path: string |
| 57 | + fields: PspFormFields |
| 58 | + userId: string |
| 59 | + requestId: string |
| 60 | + logger: Logger |
| 61 | +}): Promise<NextResponse> { |
| 62 | + const { apiKey, method, path, fields, userId, requestId, logger } = options |
| 63 | + |
| 64 | + const form = new FormData() |
| 65 | + if (fields.friendlyName) form.append('friendlyName', fields.friendlyName) |
| 66 | + if (fields.status) form.append('status', fields.status) |
| 67 | + if (fields.password) form.append('password', fields.password) |
| 68 | + if (fields.customDomain) form.append('customDomain', fields.customDomain) |
| 69 | + if (typeof fields.hideUrlLinks === 'boolean') { |
| 70 | + form.append('hideUrlLinks', String(fields.hideUrlLinks)) |
| 71 | + } |
| 72 | + if (typeof fields.noIndex === 'boolean') form.append('noIndex', String(fields.noIndex)) |
| 73 | + if (fields.monitorIds) { |
| 74 | + for (const id of fields.monitorIds.split(',')) { |
| 75 | + const trimmed = id.trim() |
| 76 | + if (trimmed) form.append('monitorIds', trimmed) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (fields.logo) { |
| 81 | + const denied = await appendPspImage(form, 'logo', fields.logo, userId, requestId, logger) |
| 82 | + if (denied) return denied |
| 83 | + } |
| 84 | + if (fields.icon) { |
| 85 | + const denied = await appendPspImage(form, 'icon', fields.icon, userId, requestId, logger) |
| 86 | + if (denied) return denied |
| 87 | + } |
| 88 | + |
| 89 | + const response = await fetch(`${UPTIMEROBOT_API_BASE}${path}`, { |
| 90 | + method, |
| 91 | + headers: { Authorization: `Bearer ${apiKey}`, Accept: 'application/json' }, |
| 92 | + body: form, |
| 93 | + }) |
| 94 | + |
| 95 | + const text = await response.text() |
| 96 | + if (!response.ok) { |
| 97 | + let message: string | undefined |
| 98 | + try { |
| 99 | + message = JSON.parse(text)?.message |
| 100 | + } catch { |
| 101 | + message = undefined |
| 102 | + } |
| 103 | + logger.error(`[${requestId}] UptimeRobot PSP request failed`, { |
| 104 | + status: response.status, |
| 105 | + body: text, |
| 106 | + }) |
| 107 | + return NextResponse.json( |
| 108 | + { success: false, error: message || `UptimeRobot API error (HTTP ${response.status})` }, |
| 109 | + { status: response.status } |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + const data = text ? JSON.parse(text) : {} |
| 114 | + return NextResponse.json({ success: true, output: { psp: mapPsp(data) } }) |
| 115 | +} |
0 commit comments