Skip to content

prefer

Parses the Prefer request header per RFC 7240. Works in concert with send()’s prefer: true option to automatically handle return=minimal (200 → 204) and return=representation behaviors.

Pipeline stage: Negotiation

import { prefer } from "@centralping/ergo";

None. The prefer() factory takes no parameters.

Returns parsed preferences stored at acc.prefer:

{
return: "minimal",
// other preferences as key-value pairs
}

When send() has prefer: true enabled and the client sends Prefer: return=minimal, success responses are automatically converted: 200 → 204 with the body stripped and a Preference-Applied: return=minimal header added.

None.

import { compose, prefer, handler } from "@centralping/ergo";
const pipeline = compose(
{fn: prefer(), setPath: "prefer"},
(req, res, acc) => ({
response: { body: { id: 1, name: "item" } },
}),
);
// With send's prefer option enabled
const server = http.createServer(
handler(pipeline, { prefer: true }),
);
// Client: Prefer: return=minimal
// Response: 204 No Content + Preference-Applied: return=minimal

See the auto-generated prefer API docs.