feat: add limit parameter to search API
This commit is contained in:
@@ -5,4 +5,5 @@ node_modules
|
||||
bun.lockb
|
||||
.prettierignore
|
||||
.gitignore
|
||||
Dockerfile
|
||||
Dockerfile
|
||||
*.snap
|
||||
@@ -1,9 +1,10 @@
|
||||
export async function fetchSearchResultsFromAmvstrm(
|
||||
query: string,
|
||||
page: number,
|
||||
limit: number,
|
||||
) {
|
||||
return fetch(
|
||||
`https://api-amvstrm.nyt92.eu.org/api/v2/search?q=${query}&p=${page}`,
|
||||
`https://api-amvstrm.nyt92.eu.org/api/v2/search?q=${query}&p=${page}&limit=${limit}`,
|
||||
)
|
||||
.then((res) => res.json<any>())
|
||||
.then(({ pageInfo: { hasNextPage }, results }) => ({
|
||||
|
||||
@@ -2,8 +2,8 @@ import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
const SearchQuery = graphql(`
|
||||
query Search($query: String!, $page: Int!) {
|
||||
Page(page: $page) {
|
||||
query Search($query: String!, $page: Int!, $limit: Int!) {
|
||||
Page(page: $page, perPage: $limit) {
|
||||
media(search: $query, type: ANIME, sort: [POPULARITY_DESC, SCORE_DESC]) {
|
||||
id
|
||||
title {
|
||||
@@ -26,11 +26,12 @@ const SearchQuery = graphql(`
|
||||
export async function fetchSearchResultsFromAnilist(
|
||||
query: string,
|
||||
page: number,
|
||||
limit: number,
|
||||
) {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
|
||||
return client
|
||||
.request(SearchQuery, { page, query })
|
||||
.request(SearchQuery, { page, query, limit })
|
||||
.then((data) => data?.Page)
|
||||
.then((page) => {
|
||||
if (!page || page.media?.length === 0) {
|
||||
|
||||
@@ -19,6 +19,7 @@ const route = createRoute({
|
||||
query: z.object({
|
||||
query: z.string(),
|
||||
page: z.number({ coerce: true }).int().min(1).default(1),
|
||||
limit: z.number({ coerce: true }).int().default(10),
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
@@ -36,10 +37,11 @@ const route = createRoute({
|
||||
app.openapi(route, async (c) => {
|
||||
const query = c.req.query("query") ?? "";
|
||||
const page = Number(c.req.query("page") ?? 1);
|
||||
const limit = Number(c.req.query("limit") ?? 10);
|
||||
|
||||
const response = await fetchFromMultipleSources([
|
||||
() => fetchSearchResultsFromAnilist(query, page),
|
||||
() => fetchSearchResultsFromAmvstrm(query, page),
|
||||
() => fetchSearchResultsFromAnilist(query, page, limit),
|
||||
() => fetchSearchResultsFromAmvstrm(query, page, limit),
|
||||
]);
|
||||
if (!response) {
|
||||
return c.json({ success: false, results: [], hasNextPage: false }, 200);
|
||||
|
||||
Reference in New Issue
Block a user