api requests work, added simple tests... TODO: URL Object needs to be cloned for memory safety
This commit is contained in:
@@ -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 baseURL = "https://yande.re";
|
||||||
|
|
||||||
const apikeys = [
|
const apikeys = [
|
||||||
@@ -52,6 +52,17 @@ function prepareURLObjectForRequest(type: APIKeys) {
|
|||||||
|
|
||||||
export function getPosts(search? : string){
|
export function getPosts(search? : string){
|
||||||
const url = prepareURLObjectForRequest("post")
|
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)]"))
|
||||||
|
})
|
||||||
@@ -49,24 +49,27 @@ type PostResponse = {
|
|||||||
const postSearchkeys = ["limit", "page", "tags"];
|
const postSearchkeys = ["limit", "page", "tags"];
|
||||||
|
|
||||||
function parsePostListArgs(url: URL, args: string) {
|
function parsePostListArgs(url: URL, args: string) {
|
||||||
const urlCopy: URL = copyObject(url);
|
// const urlCopy: URL = copyObject<URL>(url);
|
||||||
const argarr = args.replaceAll(/(\[|\|\ ])/g, "").split(",");
|
const argarr = args.replaceAll(/(\[|\ |\])/g, "").split(",");
|
||||||
for (const arg of argarr) {
|
for (const arg of argarr) {
|
||||||
const [k, v] = arg.split(":");
|
const [k, v] = arg.split(":");
|
||||||
|
if (url === undefined) throw Error("undefined Object: url!")
|
||||||
if (k === undefined || v === undefined) {
|
if (k === undefined || v === undefined) {
|
||||||
throw Error(
|
throw Error(
|
||||||
`undefined key or value in ${parsePostListArgs.name}, got k:${k}, v:${v}`,
|
`undefined key or value in ${parsePostListArgs.name}, got k:${k}, v:${v}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (k in postSearchkeys) {
|
if (postSearchkeys.includes(k)) {
|
||||||
urlCopy.searchParams.append(k, v);
|
console.debug(k, v)
|
||||||
|
console.debug(JSON.stringify(url))
|
||||||
|
url.searchParams.append(k, v);
|
||||||
} else {
|
} else {
|
||||||
throw Error(
|
throw Error(
|
||||||
`unknown parameter was given in ${parsePostListArgs.name}, got k:${k}, v:${v}`,
|
`unknown parameter was given in ${parsePostListArgs.name}, got k:${k}, v:${v}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return urlCopy;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function returnDiscordEmbeds(
|
async function returnDiscordEmbeds(
|
||||||
@@ -95,9 +98,9 @@ async function returnDiscordEmbeds(
|
|||||||
return embeds;
|
return embeds;
|
||||||
}
|
}
|
||||||
|
|
||||||
function copyObject<T>(obj: T) {
|
// function copyObject<T>(obj: T) {
|
||||||
return JSON.parse(JSON.stringify(obj));
|
// const newObject: T = Object.
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Post-Request Handler Function, that returns DiscordEmbeds.
|
* Post-Request Handler Function, that returns DiscordEmbeds.
|
||||||
@@ -105,11 +108,11 @@ function copyObject<T>(obj: T) {
|
|||||||
* @param search Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss.
|
* @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<DiscordEmbed[]> {
|
export async function handlePostRequest(url: URL, search?: string): Promise<DiscordEmbed[]> {
|
||||||
const urlCopy = copyObject(url);
|
// const urlCopy = copyObject<URL>(url);
|
||||||
//parse
|
//parse
|
||||||
if (search) {
|
if (search) {
|
||||||
return await returnDiscordEmbeds(parsePostListArgs(urlCopy, search));
|
return await returnDiscordEmbeds(parsePostListArgs(url, search));
|
||||||
} else {
|
} else {
|
||||||
return await returnDiscordEmbeds(urlCopy);
|
return await returnDiscordEmbeds(url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<T>(keys: string[], key: string): key is T {
|
|
||||||
return keys.includes(key as any);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function requestParser<T>(requestString: string) {
|
|
||||||
const res = requestString.match(/\[([\s*w+:\s*[\d+|\w+,]+)\]/g);
|
|
||||||
const map = new Map<T, string>();
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user