Skip to content

API Reference

ExportImportDescription
expectStatusexpect-statusDefault instance — validate status, narrow types, dispatch errors
createExpectStatusexpect-statusFactory — create a configured instance with defaults, hooks, and adapter
fetchExpectexpect-status/fetchNative fetch wrapper — fetches, parses JSON, validates status
defaultExtractMessageexpect-statusBuilt-in message extractor (tries message, detail, error, etc.)
chainExtractorsexpect-statusCompose multiple extractors into one — first non-undefined wins
expectStatus(successStatus, response, dispatch?)
ParameterTypeDescription
successStatusnumber | string | ArrayWhich status(es) count as success — see Status Reference
responseR | PromiseLike<R>Response object or promise. Must have status and body fields
dispatchobject (optional)Per-call handlers, messages, and options — see below
KeyTypeDescription
[status]stringMessage — throws ExpectStatusError with this message
[status](body) => THandler — receives typed body, can return or throw
[range]string | FunctionRange key ('4xx', '5xx') — catches entire HTTP class
[group]string | FunctionCustom group key ('auth') — catches group members
throwsfalseReturn SafeResult<T> instead of throwing
exhaustivetrueType error if any error status is uncovered
transform(body) => TReshape the success body before returning
recover(error) => TCatch-all — return a fallback value instead of throwing
onError(err, res) => voidObservability hook — fires before throwing (shadows instance)
onSuccess(res) => voidObservability hook — fires on success (shadows instance)
OptionTypeDefaultDescription
adapter(res) => { status, body }Normalize non-standard response shapes (Axios, custom envelopes)
defaults{ [key]: string | Function }{}Instance-wide flat dispatch entries — shadowed by per-call
groups{ [name]: number[] }{}Custom named status groups (auth: [401, 403])
errorFactory(msg, res) => ErrorExpectStatusErrorCustom error class or factory
extractMessage(body) => string?defaultExtractMessagePull error message from response body
fallbackMessagestring'Request failed...'Last-resort message when nothing else matches
onError(err, res) => voidInstance observability hook for errors
onSuccess(res) => voidInstance observability hook for success
statusFieldstring'status'Custom field name for status discriminator
bodyFieldstring'body'Custom field name for response body

When adapter is provided, statusField and bodyField are ignored.

OptionTypeDescription
initRequestInitPassed through to fetch (method, headers, body, etc.)
[status]string | FunctionFlat dispatch — same as expectStatus
errorFactory(msg, res) => ErrorCustom error factory
extractMessage(body) => string?Custom message extractor
fallbackMessagestringFallback message
ScenarioReturn type
DefaultPromise<SuccessBody> — typed body of the matching branch
Handler returns TPromise<SuccessBody | T> — widened union
throws: falsePromise<SafeResult<SuccessBody>>
recover returns TPromise<SuccessBody | T>
transform returns TPromise<T>
ExportWhat it extracts
messageFieldbody.message
problemDetailbody.detail (RFC 7807)
stringBodyThe body itself if it’s a string
arrayErrorsbody.errors[0] or body.errors.join()
springErrorbody.error (Spring Boot format)

Compose them with chainExtractors — first non-undefined result wins:

import { chainExtractors, problemDetail, messageField, stringBody } from 'expect-status'
const extractMessage = chainExtractors(problemDetail, messageField, stringBody)
import { ExpectStatusError } from 'expect-status'
// Properties
err.message // string — extracted or fallback message
err.status // number — the HTTP status code
err.body // unknown — the raw response body
TypeDescription
SafeResult<T>{ ok: true; data: T } | { ok: false; error: Error; status: number; body: unknown }
StatusResponseBase type for status-discriminated unions
StatusRange'1xx' | '2xx' | '3xx' | '4xx' | '5xx'
StatusGroup'success' | 'error'
StatusSpecifierStatusRange | StatusGroup
NegatedSpecifier`!${StatusSpecifier}`
StatusArgUnion of all valid first-argument forms
ResolveSuccessBody<R, S>Extracts the body type for a given status from a response union
ResolveErrorStatus<R, S>Extracts the error status codes not covered by S
StatusOf<R>All status codes in a response union
BodyOf<R, S>Body type for a specific status
ExpectStatusOptionsType for createExpectStatus options
ExpectStatusFnType for the returned expectStatus function
ExpectStatusDispatchType for per-call dispatch object
ExpectStatusDefaultsType for instance defaults
ExhaustiveCheckConditional type — boolean when covered, error when not
Extractor(body: unknown) => string | undefined