32 lines
990 B
TypeScript
32 lines
990 B
TypeScript
"use server";
|
|
|
|
import { APIResult, GCIAPIResult } from "./types";
|
|
|
|
const feelingFree = async (query: string): Promise<APIResult> => {
|
|
console.log(query);
|
|
try {
|
|
const response = await fetch(
|
|
`https://guncadindex.com/api/releases/?${query}`,
|
|
);
|
|
if (!response.ok) {
|
|
console.error(response.status);
|
|
return { error: true, payload: "Something went wrong." };
|
|
}
|
|
const { results }: GCIAPIResult = await response.json();
|
|
const randomResult = results[Math.round(Math.random() * results.length)];
|
|
const slug = randomResult.url.split("/").filter(Boolean).pop();
|
|
if (!slug) {
|
|
return { error: true, payload: "Could not retrieve results." };
|
|
}
|
|
console.log(slug);
|
|
return { payload: `https://guncadindex.com/detail/${slug}` };
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return { error: true, payload: error.message };
|
|
}
|
|
return { error: true, payload: "Something went wrong." };
|
|
}
|
|
};
|
|
|
|
export { feelingFree };
|