mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
fix(api): one helper for list pages, exact cut signal, total order
listPage (src/lib/http/list-query.ts) reads one row past the limit and cuts it back, on top of the existing list-cap signal rather than beside it. Every capped list now gets its page there, and every read passed to it orders by something ending in the row id, so rows that share a sort value cannot repeat or vanish at a page boundary. - practitioners, vaccinations and encounters were cut at their take with no signal; they now answer meta.truncated and accept offset, and their web readers walk the pages instead of stopping at the first - symptoms/events reported hasMore on a page that was exactly full; it is now true only when a row past the page exists - the document list's hand-rolled limit + 1 goes through the helper - security-activity answers an out-of-range limit with the 422 its published schema already promised instead of clamping it list-page-guard binds every apiListSuccess route to listPage, forbids hand-cut lists and length-derived hasMore, and requires the id tiebreaker in every listPage read.
This commit is contained in:
1 parent
2265763160
commit
8db7c71b0e
35 files changed
+1024
-394
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { combinedCap, listPage, parseListQuery } from "../list-query";
|
||||
|
||||
describe("listPage", () => {
|
||||
it("asks for one row past the limit and reports a cut only when that row exists", async () => {
|
||||
const read = vi.fn(async (take: number) =>
|
||||
Array.from({ length: Math.min(take, 5) }, (_, i) => i),
|
||||
);
|
||||
|
||||
const cut = await listPage(3, read);
|
||||
expect(read).toHaveBeenLastCalledWith(4);
|
||||
expect(cut.rows).toEqual([0, 1, 2]);
|
||||
expect(cut.cap).toEqual({ truncated: true, limit: 3 });
|
||||
|
||||
// Exactly full is complete: five rows, a limit of five.
|
||||
const full = await listPage(5, read);
|
||||
expect(full.rows).toHaveLength(5);
|
||||
expect(full.cap).toEqual({ truncated: false, limit: 5 });
|
||||
|
||||
const short = await listPage(9, read);
|
||||
expect(short.rows).toHaveLength(5);
|
||||
expect(short.cap.truncated).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("combinedCap", () => {
|
||||
it("is truncated when any list was cut", () => {
|
||||
expect(
|
||||
combinedCap(
|
||||
{ truncated: false, limit: 50 },
|
||||
{ truncated: true, limit: 50 },
|
||||
),
|
||||
).toEqual({ truncated: true, limit: 50 });
|
||||
expect(
|
||||
combinedCap(
|
||||
{ truncated: false, limit: 50 },
|
||||
{ truncated: false, limit: 50 },
|
||||
).truncated,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseListQuery", () => {
|
||||
const options = { defaultLimit: 50, maxLimit: 200, offset: true };
|
||||
|
||||
it("applies the default and reads both fields", () => {
|
||||
expect(parseListQuery(new URLSearchParams(), options).data).toEqual({
|
||||
limit: 50,
|
||||
offset: 0,
|
||||
});
|
||||
expect(
|
||||
parseListQuery(new URLSearchParams("limit=10&offset=20"), options).data,
|
||||
).toEqual({ limit: 10, offset: 20 });
|
||||
});
|
||||
|
||||
it("refuses a limit past the ceiling and a negative offset, both at once", () => {
|
||||
const parsed = parseListQuery(
|
||||
new URLSearchParams("limit=500&offset=-1"),
|
||||
options,
|
||||
);
|
||||
expect(parsed.success).toBe(false);
|
||||
expect(parsed.error?.issues.map((i) => i.path[0]).sort()).toEqual([
|
||||
"limit",
|
||||
"offset",
|
||||
]);
|
||||
});
|
||||
|
||||
it("ignores offset on a route that does not page", () => {
|
||||
expect(
|
||||
parseListQuery(new URLSearchParams("offset=20"), {
|
||||
defaultLimit: 50,
|
||||
maxLimit: 200,
|
||||
}).data,
|
||||
).toEqual({ limit: 50, offset: 0 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* The `limit` and `offset` query fields of a paged list, safe for any bundle.
|
||||
*
|
||||
* Kept apart from `./list-query.ts` (which reaches the response envelope) so a
|
||||
* request schema in `src/lib/validations/` can use them without pulling server
|
||||
* code into a client that imports the same schema.
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
/** The `limit` field, for a route whose query schema has other fields too. */
|
||||
export function listLimitField({
|
||||
defaultLimit,
|
||||
maxLimit,
|
||||
}: {
|
||||
defaultLimit: number;
|
||||
maxLimit: number;
|
||||
}) {
|
||||
return z.coerce.number().int().min(1).max(maxLimit).default(defaultLimit);
|
||||
}
|
||||
|
||||
/** The `offset` field, for a route whose query schema has other fields too. */
|
||||
export function listOffsetField() {
|
||||
return z.coerce.number().int().min(0).max(100_000).default(0);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* One way to read a page of a list, and one way to say whether it was cut.
|
||||
*
|
||||
* The list routes had grown five answers to "is there more": `meta.truncated`
|
||||
* from `src/lib/api/list-cap.ts`, a `hasMore` computed as `rows.length ===
|
||||
* limit` (true on a list that is exactly full, so a client asks for a page
|
||||
* that does not exist), a `total` from a second count query, a sync cursor,
|
||||
* and nothing at all. This module is where a capped or paged list gets its
|
||||
* page, so the signal is computed the same way everywhere:
|
||||
*
|
||||
* - {@link parseListQuery} reads `limit` (and `offset`, where the route
|
||||
* pages) with the route's own default and ceiling;
|
||||
* - {@link listPage} runs the read for one row past the limit and cuts it
|
||||
* back, so `cap.truncated` is exact: true only when a row past the page
|
||||
* exists.
|
||||
*
|
||||
* The cap is the one in `list-cap.ts` — this module adds no second signal.
|
||||
* A route answers with `apiListSuccess(data, cap)` (the envelope's `meta`),
|
||||
* or, where a route already published a `hasMore` in its body, derives that
|
||||
* field from `cap.truncated`.
|
||||
*
|
||||
* Every read through here must have a total order: the `orderBy` ends in the
|
||||
* row id, so a page boundary between two rows that share a timestamp does not
|
||||
* repeat one of them on the next page or drop it. `list-page-guard.test.ts`
|
||||
* holds every capped list to this module and to that tiebreaker.
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
import { capRows, overfetch, type ListCap } from "@/lib/api/list-cap";
|
||||
|
||||
import { listLimitField, listOffsetField } from "./list-query-fields";
|
||||
|
||||
export { listLimitField, listOffsetField } from "./list-query-fields";
|
||||
|
||||
export interface ListQueryOptions {
|
||||
/** The limit when the request names none. */
|
||||
defaultLimit: number;
|
||||
/** The largest limit a request may ask for. */
|
||||
maxLimit: number;
|
||||
/** Whether the route pages with `offset`. */
|
||||
offset?: boolean;
|
||||
}
|
||||
|
||||
export interface ListQuery {
|
||||
limit: number;
|
||||
offset: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read `limit` (and `offset`) from a request's query string.
|
||||
*
|
||||
* Returns the Zod result, so the route answers a bad value with the same
|
||||
* multi-issue 422 it gives every other field.
|
||||
*/
|
||||
export function parseListQuery(
|
||||
params: URLSearchParams,
|
||||
options: ListQueryOptions,
|
||||
) {
|
||||
const schema = z.object({
|
||||
limit: listLimitField(options),
|
||||
offset: options.offset ? listOffsetField() : z.literal(0).default(0),
|
||||
});
|
||||
return schema.safeParse({
|
||||
limit: params.get("limit") ?? undefined,
|
||||
offset: options.offset ? (params.get("offset") ?? undefined) : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a list read for one page and say whether rows past it exist.
|
||||
*
|
||||
* `read` receives the `take` to pass to Prisma (one past `limit`); the rows
|
||||
* come back cut to `limit` with the exact cap beside them.
|
||||
*/
|
||||
export async function listPage<T>(
|
||||
limit: number,
|
||||
read: (take: number) => Promise<readonly T[]>,
|
||||
): Promise<{ rows: T[]; cap: ListCap }> {
|
||||
return capRows(await read(overfetch(limit)), limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* The cap of a response that carries several capped lists at once (the
|
||||
* encounters' upcoming and past arms, the grants given and received): more
|
||||
* rows exist when any one of them was cut.
|
||||
*/
|
||||
export function combinedCap(...caps: ListCap[]): ListCap {
|
||||
return {
|
||||
truncated: caps.some((cap) => cap.truncated),
|
||||
limit: Math.max(...caps.map((cap) => cap.limit)),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user