URL shaping
RFC 6570 path templates and qs-style nested query serialization — how params and query become the URL.
A stitch builds its URL from an
RFC 6570 URI template plus the
params and query you pass at call time. Simple {id} interpolation is the
common case; the full operator set is there when you need it.
Absolute url vs relative path
How you spell the endpoint decides whether a shared
baseUrl applies:
pathis relative and is joined ontobaseUrl— the origin a stitch inherits from a seam or a shared fragment. Reach for it whenever many stitches share one host.urlis the complete endpoint (host included). It is templated and query-aware just likepath, but it ignoresbaseUrl— settingurlmeans "this stitch addresses itself." Reach for it for a one-off endpoint.
import { } from 'stitchapi';
// `path` is relative → it joins `baseUrl` (typically shared via a seam or fragment).
const = ({
: 'https://api.example.com',
: '/users/{id}',
});
await ({ : { : 1 } });
// → GET https://api.example.com/users/1[!WARNING]
A relative
urlis not joined tobaseUrl.url: '/users/{id}'overrides the base and resolves to the bare/users/{id}, which isn't fetchable — it fails at call time with aStitchConfigError("request URL … is not absolute"). When you mean "relative tobaseUrl", usepath, noturl.
Path templates
import { } from 'stitchapi';
const = ('https://api.example.com/users/{id}');
await ({ : { : 1 }, : { : 'roles' } });
// → GET https://api.example.com/users/1?expand=rolesTemplate variables are filled from params; params, query, headers, and
body all travel in one input object. The RFC 6570 operators:
| Operator | Template | Role |
|---|---|---|
| simple | {id} | percent-encoded interpolation (the default) |
| reserved | {+path} | keep reserved chars (/, ?) unencoded |
| path | {/id} | prefix the value with / |
| query | {?id,sort} | append ?id=…&sort=… |
| explode | {list*} | one key=value pair per array/object entry |
| prefix | {id:1} | take only the first n characters |
Query serialization
The query builder serializes nested objects and arrays qs-style:
import { } from 'stitchapi';
const = ('https://api.example.com/users');
await ({ : { : { : 'admin' }, : [1, 2] } });
// → /users?filter[type]=admin&ids[0]=1&ids[1]=2Array encoding is configurable with
arrayFormat — indices (the default shown
above), brackets, or repeat.
[!NOTE]
Values are percent-encoded with
encodeURIComponent, so a space in a query value goes on the wire as%20, not+likeURLSearchParams. Both decode to a space server-side; see Migration notes if a byte-for-byte fixture trips on it.
Query defaults in the path
Query keys can be baked into the path string; call-time query merges over them,
so a stitch can carry sensible defaults that each call selectively overrides:
import { } from 'stitchapi';
const = ('https://api.example.com/users?sort=name&type=admin');
await ({ : { : 'user' } });
// → /users?sort=name&type=user