From ab6ec7fc3d5c919d147ab3ca6a72fa3cede51366 Mon Sep 17 00:00:00 2001 From: fzzin <43480857+fzzinchemical@users.noreply.github.com> Date: Thu, 10 Apr 2025 11:37:18 +0200 Subject: [PATCH] api requests work, added simple tests... TODO: URL Object needs to be cloned for memory safety --- src/plugins/yandere/api/api.ts | 15 ++++++++++-- src/plugins/yandere/api/post.ts | 25 +++++++++++--------- src/plugins/yandere/plugin.ts | 42 --------------------------------- 3 files changed, 27 insertions(+), 55 deletions(-) diff --git a/src/plugins/yandere/api/api.ts b/src/plugins/yandere/api/api.ts index 951e592..08faec0 100644 --- a/src/plugins/yandere/api/api.ts +++ b/src/plugins/yandere/api/api.ts @@ -1,4 +1,4 @@ -import { requestJSON } from "@root/structures/apiRequest.ts"; +import { handlePostRequest } from "@root/plugins/yandere/api/post.ts"; const baseURL = "https://yande.re"; const apikeys = [ @@ -52,6 +52,17 @@ function prepareURLObjectForRequest(type: APIKeys) { export function getPosts(search? : string){ const url = prepareURLObjectForRequest("post") - return requestJSON(url) + return handlePostRequest(url, search) } +Deno.test("Empty Post Request", async() => { + console.debug(await getPosts()) +}) + +Deno.test("Post Request with tag", async() => { + console.debug(await getPosts("[tags:love_live!_(series)]")) +}) + +Deno.test("Post Request with limit", async() => { + console.debug(await getPosts("[limit: 1)]")) +}) \ No newline at end of file diff --git a/src/plugins/yandere/api/post.ts b/src/plugins/yandere/api/post.ts index 84023a9..a671daa 100644 --- a/src/plugins/yandere/api/post.ts +++ b/src/plugins/yandere/api/post.ts @@ -49,24 +49,27 @@ type PostResponse = { const postSearchkeys = ["limit", "page", "tags"]; function parsePostListArgs(url: URL, args: string) { - const urlCopy: URL = copyObject(url); - const argarr = args.replaceAll(/(\[|\|\ ])/g, "").split(","); + // const urlCopy: URL = copyObject(url); + const argarr = args.replaceAll(/(\[|\ |\])/g, "").split(","); for (const arg of argarr) { const [k, v] = arg.split(":"); + if (url === undefined) throw Error("undefined Object: url!") if (k === undefined || v === undefined) { throw Error( `undefined key or value in ${parsePostListArgs.name}, got k:${k}, v:${v}`, ); } - if (k in postSearchkeys) { - urlCopy.searchParams.append(k, v); + if (postSearchkeys.includes(k)) { + console.debug(k, v) + console.debug(JSON.stringify(url)) + url.searchParams.append(k, v); } else { throw Error( `unknown parameter was given in ${parsePostListArgs.name}, got k:${k}, v:${v}`, ); } } - return urlCopy; + return url; } async function returnDiscordEmbeds( @@ -95,9 +98,9 @@ async function returnDiscordEmbeds( return embeds; } -function copyObject(obj: T) { - return JSON.parse(JSON.stringify(obj)); -} +// function copyObject(obj: T) { +// const newObject: T = Object. + /** * Post-Request Handler Function, that returns DiscordEmbeds. @@ -105,11 +108,11 @@ function copyObject(obj: T) { * @param search Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss. */ export async function handlePostRequest(url: URL, search?: string): Promise { - const urlCopy = copyObject(url); + // const urlCopy = copyObject(url); //parse if (search) { - return await returnDiscordEmbeds(parsePostListArgs(urlCopy, search)); + return await returnDiscordEmbeds(parsePostListArgs(url, search)); } else { - return await returnDiscordEmbeds(urlCopy); + return await returnDiscordEmbeds(url); } } diff --git a/src/plugins/yandere/plugin.ts b/src/plugins/yandere/plugin.ts index 6881746..e69de29 100644 --- a/src/plugins/yandere/plugin.ts +++ b/src/plugins/yandere/plugin.ts @@ -1,42 +0,0 @@ -import {DiscordEmbedImage} from "npm:discordeno@18.0.1"; -import {} - -export function drop(): DiscordEmbedImage { - const tmp = await - return {url: } -} - -export function requestWorker(request: string): DiscordEmbedImage[] { - -} - - -function isKey(keys: string[], key: string): key is T { - return keys.includes(key as any); -} - -export function requestParser(requestString: string) { - const res = requestString.match(/\[([\s*w+:\s*[\d+|\w+,]+)\]/g); - const map = new Map(); - if (res !== null) { - res[0] - .split(",") - .forEach((param) => { - const match = param.match(/\s*(\w+)\s*:\s*([\w+,+]+)/); - console.debug({ match }); - if (match !== null) { - if (match[1] === undefined || match[2] === undefined) { - throw Error("Unreachable"); - } - if (!isKey(match[1])) throw Error(`Key: ${match[1]} is not a Key!`); - map.set(match[1], match[2]); - } else { - throw Error(`match returned null in param = ${Deno.inspect(param)}`); - } - }); - } else { - throw Error("Request String had some major issues chief"); - } - console.debug(); - return map; -} \ No newline at end of file