mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 20:24:27 -04:00
Create page with next/prev url
This commit is contained in:
parent
50002907e3
commit
879d2959d5
@ -14,8 +14,8 @@ import {
|
|||||||
Genre,
|
Genre,
|
||||||
isUuid,
|
isUuid,
|
||||||
keysetPaginate,
|
keysetPaginate,
|
||||||
|
Page,
|
||||||
processLanguages,
|
processLanguages,
|
||||||
type Page,
|
|
||||||
createPage,
|
createPage,
|
||||||
} from "~/models/utils";
|
} from "~/models/utils";
|
||||||
|
|
||||||
@ -147,7 +147,6 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
{
|
{
|
||||||
status: 422,
|
status: 422,
|
||||||
message: "Accept-Language header could not be satisfied.",
|
message: "Accept-Language header could not be satisfied.",
|
||||||
details: undefined,
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@ -222,21 +221,20 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
`,
|
`,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
// response: {
|
response: {
|
||||||
// 200: Page(Movie, {
|
200: Page(Movie, {
|
||||||
// description: "Paginated list of movies that match filters.",
|
description: "Paginated list of movies that match filters.",
|
||||||
// }),
|
}),
|
||||||
// 422: {
|
422: {
|
||||||
// ...KError,
|
...KError,
|
||||||
// description: "Invalid query parameters.",
|
description: "Invalid query parameters.",
|
||||||
// examples: [
|
examples: [
|
||||||
// {
|
{
|
||||||
// status: 422,
|
status: 422,
|
||||||
// message: "Accept-Language header could not be satisfied.",
|
message: "Accept-Language header could not be satisfied.",
|
||||||
// details: undefined,
|
},
|
||||||
// },
|
],
|
||||||
// ],
|
},
|
||||||
// },
|
},
|
||||||
// },
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -3,6 +3,10 @@ import { eq, or, type Column, and, gt, lt } from "drizzle-orm";
|
|||||||
|
|
||||||
type Table<Name extends string> = Record<Name, Column>;
|
type Table<Name extends string> = Record<Name, Column>;
|
||||||
|
|
||||||
|
type After = Record<string, string | number | boolean | undefined> & {
|
||||||
|
reverse?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
// Create a filter (where) expression on the query to skip everything before/after the referenceID.
|
// Create a filter (where) expression on the query to skip everything before/after the referenceID.
|
||||||
// The generalized expression for this in pseudocode is:
|
// The generalized expression for this in pseudocode is:
|
||||||
// (x > a) OR
|
// (x > a) OR
|
||||||
@ -27,7 +31,7 @@ export const keysetPaginate = <
|
|||||||
sort: Sort<T, Remap>;
|
sort: Sort<T, Remap>;
|
||||||
}) => {
|
}) => {
|
||||||
if (!after) return undefined;
|
if (!after) return undefined;
|
||||||
const cursor: Record<string, string | number> = JSON.parse(
|
const { reverse, ...cursor }: After = JSON.parse(
|
||||||
Buffer.from(after, "base64").toString("utf-8"),
|
Buffer.from(after, "base64").toString("utf-8"),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -36,7 +40,7 @@ export const keysetPaginate = <
|
|||||||
let where = undefined;
|
let where = undefined;
|
||||||
let previous = undefined;
|
let previous = undefined;
|
||||||
for (const by of [...sort, { key: "pk" as const, desc: false }]) {
|
for (const by of [...sort, { key: "pk" as const, desc: false }]) {
|
||||||
const cmp = by.desc ? lt : gt;
|
const cmp = by.desc !== reverse ? lt : gt;
|
||||||
where = or(where, and(previous, cmp(table[by.key], cursor[by.key])));
|
where = or(where, and(previous, cmp(table[by.key], cursor[by.key])));
|
||||||
previous = and(previous, eq(table[by.key], cursor[by.key]));
|
previous = and(previous, eq(table[by.key], cursor[by.key]));
|
||||||
}
|
}
|
||||||
@ -44,4 +48,14 @@ export const keysetPaginate = <
|
|||||||
return where;
|
return where;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const generateAfter = (
|
||||||
|
cursor: any,
|
||||||
|
sort: Sort<any, any>,
|
||||||
|
reverse?: boolean,
|
||||||
|
) => {
|
||||||
|
const ret: After = { reverse };
|
||||||
|
for (const by of sort) {
|
||||||
|
ret[by.key] = cursor[by.key];
|
||||||
|
}
|
||||||
|
return Buffer.from(JSON.stringify(ret), "utf-8").toString("base64");
|
||||||
|
};
|
||||||
|
@ -1,13 +1,34 @@
|
|||||||
import type { ObjectOptions } from "@sinclair/typebox";
|
import type { ObjectOptions } from "@sinclair/typebox";
|
||||||
import { t, type TSchema } from "elysia";
|
import { t, type TSchema } from "elysia";
|
||||||
|
import type { Sort } from "./sort";
|
||||||
|
import { generateAfter } from "./keyset-paginate";
|
||||||
|
|
||||||
export const Page = <T extends TSchema>(schema: T, options?: ObjectOptions) =>
|
export const Page = <T extends TSchema>(schema: T, options?: ObjectOptions) =>
|
||||||
t.Object(
|
t.Object(
|
||||||
{
|
{
|
||||||
items: t.Array(schema),
|
items: t.Array(schema),
|
||||||
this: t.String({ format: "uri" }),
|
this: t.String({ format: "uri" }),
|
||||||
prev: t.String({ format: "uri" }),
|
prev: t.Nullable(t.String({ format: "uri" })),
|
||||||
next: t.String({ format: "uri" }),
|
next: t.Nullable(t.String({ format: "uri" })),
|
||||||
},
|
},
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const createPage = <T>(
|
||||||
|
items: T[],
|
||||||
|
{ url, sort }: { url: string; sort: Sort<any, any> },
|
||||||
|
) => {
|
||||||
|
let prev: string | null = null;
|
||||||
|
let next: string | null = null;
|
||||||
|
const uri = new URL(url);
|
||||||
|
|
||||||
|
if (uri.searchParams.has("after")) {
|
||||||
|
uri.searchParams.set("after", generateAfter(items[0], sort, true));
|
||||||
|
prev = uri.toString();
|
||||||
|
}
|
||||||
|
if (items.length) {
|
||||||
|
uri.searchParams.set("after", generateAfter(items[items.length - 1], sort));
|
||||||
|
next = uri.toString();
|
||||||
|
}
|
||||||
|
return { items, this: url, prev, next };
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user