initial framework
This commit is contained in:
31
actions.ts
Normal file
31
actions.ts
Normal file
@ -0,0 +1,31 @@
|
||||
"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 };
|
||||
Reference in New Issue
Block a user