fix: multiple tags issue

This commit is contained in:
fzzinchemical
2025-03-31 20:28:24 +02:00
parent fe4e6690d4
commit 2a7a172f62
2 changed files with 20 additions and 8 deletions

View File

@@ -49,13 +49,13 @@ export const postUrl = new URL(`${baseUrl}/index.php?page=dapi&s=post&q=index&js
const tagUrl = `${baseUrl}/index.php?page=dapi&s=tag&q=index`; const tagUrl = `${baseUrl}/index.php?page=dapi&s=tag&q=index`;
const commentsUrl = `${baseUrl}/index.php?page=dapi&s=comment&q=index`; const commentsUrl = `${baseUrl}/index.php?page=dapi&s=comment&q=index`;
export async function requestJSON<T>(URL: string) { export async function requestJSON<T>(url: string) {
const response = await requestRaw(URL); const response = await requestRaw(url);
return <T> await response.json(); return <T> await response.json();
} }
async function requestRaw(URL: string) { async function requestRaw(url: string) {
const response = await fetch(URL, { const response = await fetch(url, {
headers: { "Accept": "application/json" }, headers: { "Accept": "application/json" },
}); });
if (!response.ok) { if (!response.ok) {

View File

@@ -1,7 +1,6 @@
import { assert } from "@std/assert/assert"; import { assert } from "@std/assert/assert";
import {requestJSON, postUrl, ImageResponse} from "./api.ts" import {requestJSON, postUrl, ImageResponse} from "./api.ts"
const keys = ["limit" , "id" , "pid" , "tags"] as const const keys = ["limit" , "id" , "pid" , "tags"] as const
type PostKeys = typeof keys[number] type PostKeys = typeof keys[number]
@@ -36,7 +35,8 @@ export function requestParser(requestString: string) {
res[0] res[0]
.split(",") .split(",")
.forEach(param => { .forEach(param => {
const match = param.match(/\s*(\w+)\s*:\s*([\w+,]+)/) const match = param.match(/\s*(\w+)\s*:\s*([\w+,+]+)/)
console.debug({match})
if (match !== null) { if (match !== null) {
if (match[1] === undefined || match[2] === undefined) throw Error("Unreachable") if (match[1] === undefined || match[2] === undefined) throw Error("Unreachable")
if (!isKey(match[1])) throw Error(`Key: ${match[1]} is not a Key!`) if (!isKey(match[1])) throw Error(`Key: ${match[1]} is not a Key!`)
@@ -48,13 +48,21 @@ export function requestParser(requestString: string) {
} else { } else {
throw Error("Request String had some major issues chief") throw Error("Request String had some major issues chief")
} }
console.debug()
return map return map
} }
// + is replaced with a space... this is pain
// see percent code + === 2B
export function generateRequestURL(requestString: string) { export function generateRequestURL(requestString: string) {
const postCpy = new URL(postUrl.toString()) const postCpy = new URL(postUrl.toString())
postCpy.search = new URLSearchParams([...postCpy.searchParams, ...requestParser(requestString)]).toString()
return postCpy.toString() for (const [k, v] of requestParser(requestString)){
postCpy.searchParams.append(k, v)
}
console.debug(postCpy.searchParams.toString())
console.debug({postCpy})
return postCpy.href.toString().replaceAll("%2B", '+')
} }
Deno.test("Test Request Parser", () => { Deno.test("Test Request Parser", () => {
@@ -72,3 +80,7 @@ Deno.test("Test Drop", async() => {
Deno.test("Test Request Workder", async() => { Deno.test("Test Request Workder", async() => {
console.debug(await requestWorker("[limit: 12,tags:sfw]")) console.debug(await requestWorker("[limit: 12,tags:sfw]"))
}) })
Deno.test("Test Request Workder 2", async() => {
console.debug(await requestWorker("[limit: 5,tags: AI+catgirl]"))
})