Paged search: response contracts
Project milestone
Accept paginated search results only after validation. Each page has at most 50 distinct items with string IDs and labels, and a next cursor that is null or a bounded nonempty string.
Run the examples
In projects/paged-search/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {normalizePage} = require('./search.cjs');console.log(JSON.stringify(normalizePage({items:[{id:'a',label:'Pen',debug:true}],next:null})));Expected console output:
{"items":[{"id":"a","label":"Pen"}],"next":null}Projection removes fields outside the view contract.
In projects/paged-search/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {mergeItems} = require('./search.cjs');console.log(JSON.stringify(mergeItems([{id:'a',label:'Old'}],[{id:'a',label:'New'},{id:'b',label:'Pen'}])));Expected console output:
[{"id":"a","label":"New"},{"id":"b","label":"Pen"}]Map replacement updates a matching ID while retaining its position.
Implementation and contract
normalizePage() copies only the fields the view needs. It rejects duplicate IDs within a page and invalid cursor types. A Map then merges results by ID, replacing a record when a later page provides an updated label; the combined result is bounded at 500 items. The transport is injected, so no network service is required.
Common mistakes
An HTTP success status would not establish that the response has the expected shape. Do not silently coerce a numeric ID to a string.
Acceptance task
Create a page containing an extra field and two pages sharing one ID. Verify the extra field is omitted and the later label wins.
Project source, setup and tests: projects/paged-search/README.md. Read the implementation before modifying it; the tests check the supplied baseline, not every possible implementation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Paged search: response contracts — Knowledge Check
Assess implementation decisions and project behavior.
What does null next mean in this lab?
Why project ID and label into new records?
What happens to duplicate IDs across successive pages?
In the Paged search: response contracts lab, what does this independent example log? const {normalizePage} = require('./search.cjs'); console.log(JSON.stringify(normalizePage({items:[{id:'a',label:'Pen',debug:true}],next:null})));
In the Paged search: response contracts lab, what does this independent example log? const {mergeItems} = require('./search.cjs'); console.log(JSON.stringify(mergeItems([{id:'a',label:'Old'}],[{id:'a',label:'New'},{id:'b',label:'Pen'}])));