fix: some issues
This commit is contained in:
42
routes/api/query/index.ts
Normal file
42
routes/api/query/index.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
import { getAllMovies, Movie } from "@lib/resource/movies.ts";
|
||||
import { Article, getAllArticles } from "@lib/resource/articles.ts";
|
||||
import { getAllRecipes, Recipe } from "@lib/resource/recipes.ts";
|
||||
|
||||
const isResource = (
|
||||
item: Movie | Article | Recipe | boolean,
|
||||
): item is Movie | Article | Recipe => {
|
||||
return !!item;
|
||||
};
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(req) {
|
||||
const url = new URL(req.url);
|
||||
|
||||
const types = url.searchParams.get("type")?.split(", ");
|
||||
|
||||
let resources = (await Promise.all([
|
||||
(!types || types.includes("movie")) && getAllMovies(),
|
||||
(!types || types.includes("article")) && getAllArticles(),
|
||||
(!types || types.includes("recipe")) && getAllRecipes(),
|
||||
])).flat().filter(isResource);
|
||||
|
||||
const tags = url.searchParams?.get("tags")?.split(",");
|
||||
if (tags?.length) {
|
||||
resources = resources.filter((r) => {
|
||||
return tags?.every((t) => r.tags.includes(t));
|
||||
});
|
||||
}
|
||||
|
||||
const authors = url.searchParams?.get("author")?.split(",");
|
||||
if (authors?.length) {
|
||||
console.log({ authors });
|
||||
resources = resources.filter((r) => {
|
||||
return r?.meta?.author && authors.includes(r?.meta?.author);
|
||||
});
|
||||
}
|
||||
|
||||
return json(resources);
|
||||
},
|
||||
};
|
@ -1,30 +0,0 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
function GET() {
|
||||
let timer: number | undefined = undefined;
|
||||
const body = new ReadableStream({
|
||||
start(controller) {
|
||||
timer = setInterval(() => {
|
||||
const message = `It is ${new Date().toISOString()}\n`;
|
||||
controller.enqueue(new TextEncoder().encode(message));
|
||||
}, 1000);
|
||||
},
|
||||
|
||||
cancel() {
|
||||
if (timer !== undefined) {
|
||||
clearInterval(timer);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(body, {
|
||||
headers: {
|
||||
"content-type": "text/plain",
|
||||
"x-content-type-options": "nosniff",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export const handler: Handlers = {
|
||||
GET,
|
||||
};
|
Reference in New Issue
Block a user