--- /*! * This file is part of Share₂Fedi * https://github.com/kytta/share2fedi * * SPDX-FileCopyrightText: © 2023 Nikita Karamov * SPDX-License-Identifier: AGPL-3.0-only */ import Layout from "@layouts/layout.astro"; import InstanceSelect from "@components/instance-select.astro"; import { getUrlDomain } from "@lib/url"; import type { Detection } from "./api/detect/[domain]"; const searchParameters = new URL(Astro.request.url).searchParams; let text: string | null = searchParameters.get("text"); let instance: string | null = searchParameters.get("instance"); const errors = { text: "", instance: "" }; if (Astro.request.method === "POST") { const formData = await Astro.request.formData(); text = formData.get("text") as string | null; instance = formData.get("instance") as string | null; if (typeof text !== "string" || text.length === 0) { errors.text += "Please enter post text. "; } let detection: Detection | undefined; if (typeof instance !== "string" || instance.length === 0) { errors.instance += "Please enter instance domain. "; } else { instance = getUrlDomain(instance); const detectResponse = await fetch( new URL(`/api/detect/${instance}`, Astro.url), ); const detectJson = await detectResponse.json(); if (detectJson.error) { errors.instance += detectJson.error + " "; } else { detection = detectJson; } } const hasErrors = Object.values(errors).some(Boolean); if (!hasErrors && detection !== undefined) { const { domain, endpoint, params } = detection; const publishUrl = new URL(endpoint, `https://${domain}/`); publishUrl.search = new URLSearchParams([ [params.text, text as string], ]).toString(); // eslint-disable-next-line unicorn/prefer-module return Astro.redirect(publishUrl.toString(), 303); } } ---