ยฉ 2026 Hedgehog Software, LLC
ts-pattern
@effect/match
const contentTypeIsJson = (headers: Headers): boolean => pipe( headers.get(CONTENT_TYPE_RESPONSE_HEADER), O.fromNullable, O.map(flow(ContentTypeHelpers.parse, (result) => result.type)), O.exists((type) => type === CONTENT_TYPE_JSON), ) const statusCodeIs40x = (status: number): boolean => status >= 400 && status <= 451 const statusCodeIs50x = (status: number): boolean => status >= 500 && status <= 511 const matchResponse = pipe( Match.type<Response>(), Match.when({ status: statusCodeIs40x }, (r) => Z.fail(new HttpClientError(r.status))), Match.when({ status: statusCodeIs50x }, (r) => Z.fail(new HttpServerError(r.status))), Match.when({ headers: contentTypeIsJson }, (r) => Z.tryCatchPromise( () => r.json() as Promise<Json>, (error) => new JsonParseError( error instanceof Error ? error.message : 'Unknown error.', ), )), Match.orElse(() => Z.fail(new NotJson)) )
headers: contentTypeIsJson
Response
r
never