Skip to content

precondition

Enforces that unsafe requests include a conditional header (If-Match or If-Unmodified-Since) before proceeding. Prevents lost-update problems by requiring clients to prove they have a current representation.

Pipeline stage: Negotiation (Fast Fail)

import {precondition} from '@centralping/ergo';
Option Type Default Description
methods string[] | Set<string> Methods to enforce on; omit to enforce unconditionally

Returns undefined on success — the middleware is a gate.

Status Condition
428 Precondition Required Neither If-Match nor If-Unmodified-Since header is present

Use preconditionRequired with the default etag: true behavior to enforce optimistic concurrency on write operations:

import {compose, precondition} from '@centralping/ergo';
// Enforce on all methods
const pipeline = compose(
precondition(),
);
// Enforce only on PUT and PATCH
const pipeline = compose(
precondition({methods: ['PUT', 'PATCH']}),
);

ETag generation without enforcement — clients CAN send conditional headers but are not required to:

import {compose, precondition} from '@centralping/ergo';
// Simply omit precondition() from the pipeline.
// etag: true remains the send() default — ETags are generated,
// If-Match is evaluated when present, but not enforced.
const pipeline = compose(
(req, res, acc) => ({
response: {body: {settings: acc.settings}},
}),
);
  • Testing Patterns — ETag round-trip and 304 conditional request testing patterns

See the auto-generated precondition API docs.