mirror of
https://github.com/NickKaramoff/toot
synced 2025-01-30 17:15:17 +01:00
Move detection logic to Astro actions
This commit is contained in:
parent
c9f849d1f4
commit
2fa5f8baa8
50
src/actions/index.ts
Normal file
50
src/actions/index.ts
Normal file
@ -0,0 +1,50 @@
|
||||
/*!
|
||||
* This file is part of Share₂Fedi
|
||||
* https://github.com/kytta/share2fedi
|
||||
*
|
||||
* SPDX-FileCopyrightText: © 2023 Nikita Karamov <me@kytta.dev>
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { defineAction, ActionError } from "astro:actions";
|
||||
import { z } from "astro:schema";
|
||||
|
||||
import { getSoftwareName } from "@lib/nodeinfo";
|
||||
import { type ProjectPublishConfig, supportedProjects } from "@lib/project";
|
||||
|
||||
export type Detection = {
|
||||
domain: string;
|
||||
project: keyof typeof supportedProjects;
|
||||
} & ProjectPublishConfig;
|
||||
|
||||
export const server = {
|
||||
detect: defineAction({
|
||||
input: z.string().min(5).includes("."),
|
||||
handler: async (domain): Promise<Detection> => {
|
||||
const softwareName = await getSoftwareName(domain);
|
||||
if (softwareName === undefined) {
|
||||
throw new ActionError({
|
||||
message: "Could not detect Fediverse project.",
|
||||
code: "BAD_REQUEST",
|
||||
});
|
||||
}
|
||||
|
||||
if (!(softwareName in supportedProjects)) {
|
||||
throw new ActionError({
|
||||
message: `Fediverse project "${softwareName}" is not supported yet.`,
|
||||
code: "BAD_REQUEST",
|
||||
});
|
||||
}
|
||||
|
||||
const publishConfig = supportedProjects[
|
||||
softwareName
|
||||
] as ProjectPublishConfig;
|
||||
|
||||
return {
|
||||
domain,
|
||||
project: softwareName,
|
||||
...publishConfig,
|
||||
};
|
||||
},
|
||||
}),
|
||||
};
|
@ -6,38 +6,21 @@
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { getSoftwareName } from "@lib/nodeinfo";
|
||||
import { type ProjectPublishConfig, supportedProjects } from "@lib/project";
|
||||
import { error, json } from "@lib/response";
|
||||
import type { APIRoute } from "astro";
|
||||
import { actions } from "astro:actions";
|
||||
|
||||
export type Detection = {
|
||||
domain: string;
|
||||
project: keyof typeof supportedProjects;
|
||||
} & ProjectPublishConfig;
|
||||
import { error, json } from "@lib/response";
|
||||
|
||||
export const GET: APIRoute = async ({ params }) => {
|
||||
export const GET: APIRoute = async ({ params, callAction }) => {
|
||||
const domain = params.domain as string;
|
||||
|
||||
const softwareName = await getSoftwareName(domain);
|
||||
if (softwareName === undefined) {
|
||||
return error("Could not detect Fediverse project.");
|
||||
const { data, error: actionError } = await callAction(actions.detect, domain);
|
||||
|
||||
if (actionError) {
|
||||
return error(actionError.message);
|
||||
}
|
||||
|
||||
if (!(softwareName in supportedProjects)) {
|
||||
return error(`Fediverse project "${softwareName}" is not supported yet.`);
|
||||
}
|
||||
|
||||
const publishConfig = supportedProjects[softwareName] as ProjectPublishConfig;
|
||||
return json(
|
||||
{
|
||||
domain,
|
||||
project: softwareName,
|
||||
...publishConfig,
|
||||
},
|
||||
200,
|
||||
{
|
||||
return json(data, 200, {
|
||||
"Cache-Control": "public, s-maxage=604800, max-age=604800",
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
|
@ -6,11 +6,13 @@
|
||||
* SPDX-FileCopyrightText: © 2023 Nikita Karamov <me@kytta.dev>
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { actions } from "astro:actions";
|
||||
|
||||
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]";
|
||||
import type { Detection } from "@actions/index.ts";
|
||||
|
||||
const searchParameters = new URL(Astro.request.url).searchParams;
|
||||
let text: string | null = searchParameters.get("text");
|
||||
@ -31,15 +33,12 @@ if (Astro.request.method === "POST") {
|
||||
errors.instance += "Please enter instance domain. ";
|
||||
} else {
|
||||
instance = getUrlDomain(instance);
|
||||
const { data, error } = await actions.detect(instance);
|
||||
|
||||
const detectResponse = await fetch(
|
||||
new URL(`/api/detect/${instance}`, Astro.url),
|
||||
);
|
||||
const detectJson = await detectResponse.json();
|
||||
if (detectJson.error) {
|
||||
errors.instance += detectJson.error + " ";
|
||||
if (error) {
|
||||
errors.instance += error.message + " ";
|
||||
} else {
|
||||
detection = detectJson;
|
||||
detection = data;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@actions/*": ["src/actions/*"],
|
||||
"@components/*": ["src/components/*"],
|
||||
"@i18n/*": ["src/i18n/*"],
|
||||
"@layouts/*": ["src/layouts/*"],
|
||||
|
Loading…
x
Reference in New Issue
Block a user