defineStitch()
Bind a set of base fragments once and get a reusable stitch factory.
Use defineStitch when many stitches share the same defaults — a base URL,
retry policy, headers — and you want to bind those once and stamp out each
stitch from the resulting factory instead of repeating the base on every call.
Example
import { } from 'stitchapi';
// Bind the base once; the factory carries it into every stitch it builds.
const = ({
: 'https://api.example.com',
: { : 3 },
});
const = ('/users/{id}'); // inherits baseUrl + retry
const = ({ : 'POST', : '/users' });Both getUser and createUser inherit baseUrl and retry from the bound
base; each call adds only what is specific to that stitch.
Options
defineStitch(...fragments) takes one or more fragments — a Partial<StitchConfig>,
a preset(), another stitch, or a bare string
(shorthand for { path }) — and returns a factory. Calling the factory with a
string or a partial config produces a stitch whose own config is layered on top
of the bound fragments.
The split is: defineStitch binds what is shared across the family; each
factory call adds what is unique to that one stitch. A
preset() is a named fragment you can pass in
as a base, reuse across several factories, or merge per call.
Under the hood the bound fragments are prepended to each stitch's
extends list, so they deep-merge with
later layers winning. The merge order and precedence rules live with extends.
The bound base is prepended, so a value set on an individual factory call
overrides the same value from defineStitch — later layers win.