Release candidate — 1.0.0-rc.6
StitchAPI
GuidesData shaping

transform

Reshape a response before pick and validation — for example, scrape HTML into structured data.

Use transform when the raw response isn't yet the shape the rest of the pipeline expects — scrape HTML text into a structured object, or flatten an odd envelope — so a single function reshapes the body before pick and validation ever see it.

Example

import {  } from 'stitchapi';

const  = ({
    : 'https://api.example.com',
    : '/listings',
    // The body is `unknown` — narrow it, then reshape the raw envelope into a
    // plain array before pick and validation run.
    : () => ( as { : unknown[] }).,
});

The raw body arrives as a wrapped { results: [...] }; transform returns the inner array, and everything downstream works against that array instead.

Options

transform?: (body: unknown) => unknown runs first in the response pipeline — before pick and before validation (and drift). That ordering is the point: reshape a raw body — scrape HTML text into a structured object, or flatten an odd envelope — into the shape pick and validation are written for, rather than bending them around the source's quirks.

body is typed unknown, so narrow it before reading (a cast or a type guard) and return any reshaped value. See Reference → Config types for every field.

Anti-pattern: don't reach for transform to pull the inner field out of an envelope — a cast like reading .results off the body is an opaque function whose output is unchecked, so a wrong narrowing slips past and only surfaces later as drift. Name the path with pick instead, which is declarative, round-trips as JSON, and feeds validation and drift directly. See pick.

See also

On this page