This is a fresh brief that combines Days 3 to 6, not another copy of the Review Queue. Sketch the decisions before you write code. Plan on about 30 minutes for a first attempt at the acceptance cases; finish them, and the last section, in another sitting if you need to.
The brief
Reviewers want an activity feed: GET /api/activity?after=3&limit=2. Write
the rule as run({ actor, rows, query }), where query holds the raw query
strings (or undefined when a parameter is absent), and return everything the
route handler needs to answer: status, body, cacheControl and a log
event.
- Who (Day 1). An actor without a non-empty
tenantIdgets401with codeunauthenticated. Only rows in the actor's tenant appear. - Which page (Day 3).
limitdefaults to 10 and must be a whole number from 1 to 50;afterdefaults to 0 and must be a whole number. Anything else is400withinvalid_limitorinvalid_cursor, checked in that order. Page by id with one extra row, sonextCursoris the last id shown when more rows exist, andnullwhen they don't. - What leaves (Days 1 and 3). Items carry
idandtitleonly. An error body is exactly{ code }. - Who may cache it (Day 5). A page is per-tenant data, so
cacheControlis"private, no-store". A rejection is"no-store". - What's logged (Day 6). A page logs
{ name: "activity_page", count, hasMore }; a rejection logs{ name: "activity_rejected", code }. Never titles or tenant ids: the log is read by more people than the feed.
Keep run deterministic and leave its input unchanged.
Acceptance cases
The workspace's examples hold four rows of activity, one from another tenant,
and these four cases:
- Ada's tenant,
limit: "2": items 1 and 3,nextCursor: 3,hasMore: true. - The same with
after: "3": item 4 only,nextCursor: null. limit: "0":400 invalid_limit, logged as a rejection.- No actor:
401 unauthenticated.
npm install and npm run dev. Before writing run, add at least two cases of your own to examples: the
least convenient inputs you can find, such as limit: "1.5", after: "x", or
a cursor past the last row. Work out their results first.
Hint 1: the idea
Decide in order: is there an actor, are the inputs valid, which rows, which
fields. Every early answer is a rejection with its own code; only the end of
the function builds a page. One small reject(code) helper keeps all the
rejections the same shape.
Hint 2: a first step
Write a helper that turns a raw query string into a whole number: undefined
gives the default, a string of digits gives its number, anything else gives
NaN. Then !(limit >= 1 && limit <= 50) rejects NaN as well.
Show one solution
npm install and npm run dev. /^\d+$/ accepts only digits, so "1.5", "-1", "1e3" and "" are all
invalid, where Number() alone would accept some of them. The page uses
limit + 1 rows to learn hasMore without a second query. The items and the
event are built from allowlists, so a new column in rows can't leak into
either.
The route handler around it is short, because every decision is in run:
export async function GET(request) {
const params = new URL(request.url).searchParams;
const query = {
after: params.get("after") ?? undefined,
limit: params.get("limit") ?? undefined,
};
const result = run({ actor: await currentActor(), rows: await loadActivity(), query });
console.log(JSON.stringify(result.event));
return Response.json(result.body, {
status: result.status,
headers: { "Cache-Control": result.cacheControl },
});
}loadActivity() stands for the database query. In a real project, the tenant
filter, the id > after condition and the LIMIT belong in that SQL, with an
index on (tenantId, id), rather than loading every row and filtering in
JavaScript.
Continue independently
Add an optional type filter (approved, edited, archived) as an
allowlist: an unknown type is 400 invalid_type. Then explain what a cursor
from a filtered page means if the client changes the filter, and what your
route should do about it.
Completion is saved in this browser only. It does not measure mastery.