Get started
Fetch data

Pagination and safe writes

Pagination and safe writes

Listings arrive a page at a time. Writes carry an idempotency key, and can be made conditional on the version you read.

Read a whole listing

paginate follows the cursors for you and yields one row at a time:

import { paginate } from "@mountaya/client";

const names: string[] = [];
for await (const itinerary of paginate(mountaya.studio.itineraries.getItineraries, {
  query: { sort: "-updated_at" },
})) {
  names.push(itinerary.name);
}

Pass a client method and its usual arguments. Each row is typed as that method's own. A page is requested only when you reach it, so breaking out of the loop stops the requests.

OptionDefaultMeans
pageSize50Rows per request, from 1 to 100
maxPagesNoneThe most requests one walk makes
signalNoneStops the walk

Retry a write safely

The client sends an Idempotency-Key with every write, and reuses it on each retry, so a write lands once. If you retry yourself, after a page reload or from a queue, pass the key you used the first time:

const idempotencyKey = crypto.randomUUID();

await mountaya.studio.itineraries.postItinerary({ body, idempotencyKey });

Write only if nothing changed

Read the resource, then send its etag back as ifMatch. The write lands only if nobody changed the resource in between:

import { MountayaPreconditionFailedError } from "@mountaya/client";

const { data: itinerary } = await mountaya.studio.itineraries.getItinerary({
  itinerary_id: itineraryId,
});

try {
  await mountaya.studio.itineraries.patchItinerary({
    body: { name: "Tour du Mont-Blanc, reversed" },
    ifMatch: itinerary.etag,
    itinerary_id: itineraryId,
  });
} catch (error) {
  if (error instanceof MountayaPreconditionFailedError) {
    // Somebody changed it: read it again, and decide what the write should be now.
  }
}

A write without ifMatch replaces what is there.

Next steps

Last updated