@shivaedev/http 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.0 - 2026-11-20
4
+
5
+ ### Added
6
+
7
+ - `filterStatus` and `checkStatus` failures (`RemoteUnauthorized`, `RemoteNotFound`, `RemoteGone`, `RemoteRateLimited`, `RemoteHttpError`) carry an optional `bodyExcerpt`: at most the first 2048 bytes of the error response's text, with whitespace collapsed and secrets masked by `@shivaedev/errors` redaction. Empty, binary, failing or unfinished bodies (one second on Effect's clock) leave it out, and the status failure is still reported.
8
+ - `ReadClientError` with reason `status` carries the same `bodyExcerpt`.
9
+
10
+ ### Changed
11
+
12
+ - Require Effect 4.1.4 and its matching platform packages.
13
+ - Every status failure's `message` now reads `HTTP <status>: <method> <host><path>`, followed by `: <excerpt>` when there is one; previously only `RemoteHttpError` had a message.
14
+ - A status failure now reads up to 2048 bytes of the error body, for up to one second, before failing, instead of discarding the body unread.
15
+ - `@shivaedev/http` now depends on `@shivaedev/errors`.
16
+
17
+ ## 0.8.2 - 2026-11-11
18
+
19
+ ### Changed
20
+
21
+ - Align published dependency requirements with the workspace.
22
+
3
23
  ## 0.8.1 - 2026-11-07
4
24
 
5
25
  ### Changed
package/README.md CHANGED
@@ -55,11 +55,13 @@ const makeFoodSource = Effect.gen(function* () {
55
55
 
56
56
  `get(url)` checks the status with `filterStatus` and decodes through `readBoundedJson`. The timeout covers the request and body read, including decoding. The cache is bounded by `capacity` entries, keys by the complete URL, evicts the least recently read entry, and keeps only successful decoded values for `timeToLive`. A failed read is fetched again on the next call.
57
57
 
58
- Failures are `ReadClientError` from `readClient.ts`, with the configured `source` and a `reason`: `timeout`, `body-too-large`, `status`, `decode`, `transport` or `invalid-limit`. A status failure also carries `status`. They retain no raw request, response body or decoding cause. Each cache miss emits an Effect debug log with the source, GET method and URL origin/path, dropping userinfo, query and fragment. Native HTTP spans redact query values too.
58
+ Failures are `ReadClientError` from `readClient.ts`, with the configured `source` and a `reason`: `timeout`, `body-too-large`, `status`, `decode`, `transport` or `invalid-limit`. A status failure also carries `status`, and `bodyExcerpt` when the status failure has one. They retain no raw request, response body or decoding cause. Each cache miss emits an Effect debug log with the source, GET method and URL origin/path, dropping userinfo, query and fragment. Native HTTP spans redact query values too.
59
59
 
60
60
  `optionalField(schema)` from `optionalField.ts` makes a Struct field optional and treats a field's decoding failure as an absent key. Valid values still decode normally. Use it for unreliable outside fields; required fields elsewhere in the payload still fail decoding.
61
61
 
62
- `filterStatus(client)` passes 2xx responses through and fails other statuses with Schema tagged errors from `errors.ts`: `RemoteUnauthorized` (401), `RemoteNotFound` (404), `RemoteGone` (410), `RemoteRateLimited` (429, or 503 carrying `Retry-After`) and `RemoteHttpError` for other statuses. Errors carry the method, the URL without userinfo, query string or fragment and the status, without headers or body. `RemoteRateLimited.retryAfter` is an optional UTC `DateTime`: delay-seconds use Effect's clock, and HTTP dates name the instant directly. Missing or invalid values leave it undefined; `parseRetryAfter(header, now)` exposes the same parsing.
62
+ `filterStatus(client)` passes 2xx responses through and fails other statuses with Schema tagged errors from `errors.ts`: `RemoteUnauthorized` (401), `RemoteNotFound` (404), `RemoteGone` (410), `RemoteRateLimited` (429, or 503 carrying `Retry-After`) and `RemoteHttpError` for other statuses. Errors carry the method, the URL without userinfo, query string or fragment, the status and an optional `bodyExcerpt`, without headers. Their `message` reads `HTTP 400: POST api.example/token: {"error":"invalid_grant"}`, ending with the excerpt when there is one.
63
+
64
+ The excerpt is the remote's own explanation, read from the error response only, never from a 2xx answer. At most the first 2048 bytes are read; reading stops there and releases the rest of the body, and a cut drops its last partial word and ends with `…`. Whitespace runs collapse to one space. The text then passes through `redactText` from `@shivaedev/errors/sensitive.ts` with its default sensitive keys, so secrets such as `"access_token":"…"` values and `Bearer` credentials read `<redacted>`. A body that is empty, not UTF-8 or contains control bytes has no excerpt. If the body fails, was already read, or has not ended or reached 2048 bytes within one second of Effect's clock, the status failure is reported without an excerpt. `RemoteRateLimited.retryAfter` is an optional UTC `DateTime`: delay-seconds use Effect's clock, and HTTP dates name the instant directly. Missing or invalid values leave it undefined; `parseRetryAfter(header, now)` exposes the same parsing.
63
65
 
64
66
  `checkStatus(response)` from `checkStatus.ts` applies the same status mapping to an existing native response. `filterStatus` delegates to it. Use `isRemoteStatusError(error)` from `errors.ts` to recognize any of these typed status failures.
65
67
 
@@ -90,4 +92,4 @@ Oversized `content-length` headers are rejected before reading. If `MaxBodySize`
90
92
 
91
93
  ### Install and limits
92
94
 
93
- Install `@shivaedev/http` with `effect` as its peer. The package has no root entry; import the module that defines the name.
95
+ Install `@shivaedev/http` with `effect` as its peer; it depends on `@shivaedev/errors` for redaction. The package has no root entry; import the module that defines the name.
@@ -0,0 +1,7 @@
1
+ import { Duration, Effect } from "effect";
2
+ import type { HttpClientResponse } from "effect/http";
3
+ export declare const BODY_EXCERPT_BYTES = 2048;
4
+ export declare const bodyExcerptDeadline: Duration.Duration;
5
+ export declare function bodyExcerpt(bytes: Uint8Array, truncated: boolean): string | undefined;
6
+ export declare function readBodyExcerpt(response: HttpClientResponse.HttpClientResponse): Effect.Effect<string | undefined>;
7
+ //# sourceMappingURL=bodyExcerpt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bodyExcerpt.d.ts","sourceRoot":"","sources":["../src/bodyExcerpt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGtD,eAAO,MAAM,kBAAkB,OAAO,CAAC;AACvC,eAAO,MAAM,mBAAmB,mBAAsB,CAAC;AAgBvD,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAYrF;AAED,wBAAgB,eAAe,CAAC,QAAQ,EAAE,kBAAkB,CAAC,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,SAAS,CAAC,CAsBlH"}
@@ -0,0 +1,46 @@
1
+ import { Duration, Effect, Stream } from "effect";
2
+ import { isSensitiveKey, redactText } from "@shivaedev/errors/sensitive.ts";
3
+ export const BODY_EXCERPT_BYTES = 2048;
4
+ export const bodyExcerptDeadline = Duration.seconds(1);
5
+ // A cut can split a token or a JWT; dropping the partial word keeps half a secret out of the excerpt.
6
+ const PARTIAL_WORD = /[^\s"'{}[\]()<>,;:]+$/u;
7
+ function isBinary(text) {
8
+ for (const character of text) {
9
+ const code = character.codePointAt(0) ?? 0;
10
+ const control = (code < 0x20 && character !== "\t" && character !== "\n" && character !== "\r") || code === 0x7f;
11
+ if (control || character === "\uFFFD") {
12
+ return true;
13
+ }
14
+ }
15
+ return false;
16
+ }
17
+ export function bodyExcerpt(bytes, truncated) {
18
+ const decoded = new TextDecoder().decode(bytes, { stream: true });
19
+ if (isBinary(decoded)) {
20
+ return undefined;
21
+ }
22
+ const collapsed = decoded.replaceAll(/\s+/gu, " ").trim();
23
+ const text = truncated ? collapsed.replace(PARTIAL_WORD, "").trimEnd() : collapsed;
24
+ if (text === "") {
25
+ return undefined;
26
+ }
27
+ const redacted = redactText(text, isSensitiveKey, BODY_EXCERPT_BYTES);
28
+ return truncated ? `${redacted}…` : redacted;
29
+ }
30
+ export function readBodyExcerpt(response) {
31
+ return Effect.suspend(() => {
32
+ const kept = new Uint8Array(BODY_EXCERPT_BYTES);
33
+ let size = 0;
34
+ let truncated = false;
35
+ return response.stream.pipe(Stream.runForEachWhile((chunk) => Effect.sync(() => {
36
+ const room = BODY_EXCERPT_BYTES - size;
37
+ kept.set(chunk.subarray(0, room), size);
38
+ size += Math.min(room, chunk.byteLength);
39
+ truncated = chunk.byteLength > room;
40
+ return !truncated;
41
+ })), Effect.timeout(bodyExcerptDeadline), Effect.map(() => bodyExcerpt(kept.subarray(0, size), truncated)), Effect.orElseSucceed(() => undefined),
42
+ // A body another reader already consumed dies when its stream opens; the status failure still wins.
43
+ Effect.catchDefect(() => Effect.succeed(undefined)));
44
+ });
45
+ }
46
+ //# sourceMappingURL=bodyExcerpt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bodyExcerpt.js","sourceRoot":"","sources":["../src/bodyExcerpt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAE5E,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,CAAC;AACvC,MAAM,CAAC,MAAM,mBAAmB,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAEvD,sGAAsG;AACtG,MAAM,YAAY,GAAG,wBAAwB,CAAC;AAE9C,SAAS,QAAQ,CAAC,IAAY;IAC7B,KAAK,MAAM,SAAS,IAAI,IAAI,EAAE,CAAC;QAC9B,MAAM,IAAI,GAAG,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAC3C,MAAM,OAAO,GAAG,CAAC,IAAI,GAAG,IAAI,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,CAAC;QACjH,IAAI,OAAO,IAAI,SAAS,KAAK,QAAQ,EAAE,CAAC;YACvC,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAiB,EAAE,SAAkB;IAChE,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAClE,IAAI,QAAQ,CAAC,OAAO,CAAC,EAA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,CAAC,CAAC,CACF,EACD,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,EACnC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,SAAS,CAAC,CAAC,EAChE,MAAM,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;QACrC,oGAAoG;QACpG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CACnD,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"checkStatus.d.ts","sourceRoot":"","sources":["../src/checkStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,MAAM,EAAU,MAAM,QAAQ,CAAC;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,EAAE,iBAAiB,EAA0B,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGxI,eAAO,MAAM,WAAW,0MA8BtB,CAAC"}
1
+ {"version":3,"file":"checkStatus.d.ts","sourceRoot":"","sources":["../src/checkStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,EAAE,iBAAiB,EAA0B,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGxI,eAAO,MAAM,WAAW,0MA+BtB,CAAC"}
@@ -1,5 +1,6 @@
1
- import { DateTime, Effect, Stream } from "effect";
1
+ import { DateTime, Effect } from "effect";
2
2
  import * as HttpStatus from "effect/http/HttpStatus";
3
+ import { readBodyExcerpt } from "#bodyExcerpt.ts";
3
4
  import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, RemoteUnauthorized } from "#errors.ts";
4
5
  import { parseRetryAfter } from "#parseRetryAfter.ts";
5
6
  export const checkStatus = Effect.fnUntraced(function* (response) {
@@ -7,8 +8,10 @@ export const checkStatus = Effect.fnUntraced(function* (response) {
7
8
  if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
8
9
  return response;
9
10
  }
11
+ const now = yield* DateTime.now;
12
+ const bodyExcerpt = yield* readBodyExcerpt(response);
10
13
  const url = new URL(request.url);
11
- const fields = { method: request.method, status, url: `${url.origin}${url.pathname}` };
14
+ const fields = { method: request.method, status, url: `${url.origin}${url.pathname}`, ...(bodyExcerpt === undefined ? {} : { bodyExcerpt }) };
12
15
  let error;
13
16
  switch (status) {
14
17
  case HttpStatus.fromLiteral("Unauthorized"):
@@ -23,7 +26,7 @@ export const checkStatus = Effect.fnUntraced(function* (response) {
23
26
  default: {
24
27
  const header = response.headers["retry-after"];
25
28
  if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
26
- const retryAfter = parseRetryAfter(header, yield* DateTime.now);
29
+ const retryAfter = parseRetryAfter(header, now);
27
30
  error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
28
31
  }
29
32
  else {
@@ -31,7 +34,6 @@ export const checkStatus = Effect.fnUntraced(function* (response) {
31
34
  }
32
35
  }
33
36
  }
34
- yield* Effect.scoped(Stream.toPull(response.stream));
35
37
  return yield* Effect.fail(error);
36
38
  });
37
39
  //# sourceMappingURL=checkStatus.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkStatus.js","sourceRoot":"","sources":["../src/checkStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAElD,OAAO,KAAK,UAAU,MAAM,wBAAwB,CA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,UAAU,CAAC,WAAW,CAAC,oBAAoB,CAAC,IAAI,MAAM,KAAK,SAAS,CAAC,EAAE,CAAC;gBAC/I,MAAM,UAAU,GAAG,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;gBAChD,KAAK,GAAG,IAAI,iBAAiB,CAAC,EAAE,GAAG,MAAM,EAAE,GAAG,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;YACnG,CAAC;iBAAM,CAAC;gBACP,KAAK,GAAG,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;YACrC,CAAC;QACF,CAAC;IACF,CAAC;IACD,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC,CAAC,CAAC"}
package/dist/errors.d.ts CHANGED
@@ -1,34 +1,43 @@
1
1
  import { Schema } from "effect";
2
2
  declare const RemoteUnauthorized_base: Schema.Class<RemoteUnauthorized, Schema.TaggedStruct<"RemoteUnauthorized", {
3
+ bodyExcerpt: Schema.optionalKey<Schema.String>;
3
4
  method: Schema.String;
4
5
  status: Schema.Number;
5
6
  url: Schema.String;
6
7
  }>, import("effect/Cause").YieldableError>;
7
8
  export declare class RemoteUnauthorized extends RemoteUnauthorized_base {
9
+ get message(): string;
8
10
  }
9
11
  declare const RemoteNotFound_base: Schema.Class<RemoteNotFound, Schema.TaggedStruct<"RemoteNotFound", {
12
+ bodyExcerpt: Schema.optionalKey<Schema.String>;
10
13
  method: Schema.String;
11
14
  status: Schema.Number;
12
15
  url: Schema.String;
13
16
  }>, import("effect/Cause").YieldableError>;
14
17
  export declare class RemoteNotFound extends RemoteNotFound_base {
18
+ get message(): string;
15
19
  }
16
20
  declare const RemoteGone_base: Schema.Class<RemoteGone, Schema.TaggedStruct<"RemoteGone", {
21
+ bodyExcerpt: Schema.optionalKey<Schema.String>;
17
22
  method: Schema.String;
18
23
  status: Schema.Number;
19
24
  url: Schema.String;
20
25
  }>, import("effect/Cause").YieldableError>;
21
26
  export declare class RemoteGone extends RemoteGone_base {
27
+ get message(): string;
22
28
  }
23
29
  declare const RemoteRateLimited_base: Schema.Class<RemoteRateLimited, Schema.TaggedStruct<"RemoteRateLimited", {
30
+ readonly bodyExcerpt: Schema.optionalKey<Schema.String>;
24
31
  readonly method: Schema.String;
25
32
  readonly status: Schema.Number;
26
33
  readonly url: Schema.String;
27
34
  readonly retryAfter: Schema.optionalKey<Schema.DateTimeUtc>;
28
35
  }>, import("effect/Cause").YieldableError>;
29
36
  export declare class RemoteRateLimited extends RemoteRateLimited_base {
37
+ get message(): string;
30
38
  }
31
39
  declare const RemoteHttpError_base: Schema.Class<RemoteHttpError, Schema.TaggedStruct<"RemoteHttpError", {
40
+ bodyExcerpt: Schema.optionalKey<Schema.String>;
32
41
  method: Schema.String;
33
42
  status: Schema.Number;
34
43
  url: Schema.String;
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;;;;;;AAIhC,qBAAa,kBAAmB,SAAQ,uBAAsE;CAAG;;;;;;AACjH,qBAAa,cAAe,SAAQ,mBAA8D;CAAG;;;;;;AACrG,qBAAa,UAAW,SAAQ,eAAsD;CAAG;;;;;;;AACzF,qBAAa,iBAAkB,SAAQ,sBAGrC;CAAG;;;;;;AACL,qBAAa,eAAgB,SAAQ,oBAAgE;IACpG,IAAa,OAAO,IAAI,MAAM,CAG7B;CACD;AAED,QAAA,MAAM,iBAAiB,gJAAqG,CAAC;AAC7H,MAAM,MAAM,iBAAiB,GAAG,OAAO,iBAAiB,CAAC,IAAI,CAAC;AAC9D,eAAO,MAAM,mBAAmB,wHAA+B,CAAC;;;;;AAEhE,qBAAa,mBAAoB,SAAQ,wBAGvC;CAAG;;;;AAEL,qBAAa,oBAAqB,SAAQ,yBAExC;CAAG;;;;AAEL,qBAAa,gBAAiB,SAAQ,qBAEpC;CAAG"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;;;;;;;AAUhC,qBAAa,kBAAmB,SAAQ,uBAAsE;IAC7G,IAAa,OAAO,IAAI,MAAM,CAE7B;CACD;;;;;;;AACD,qBAAa,cAAe,SAAQ,mBAA8D;IACjG,IAAa,OAAO,IAAI,MAAM,CAE7B;CACD;;;;;;;AACD,qBAAa,UAAW,SAAQ,eAAsD;IACrF,IAAa,OAAO,IAAI,MAAM,CAE7B;CACD;;;;;;;;AACD,qBAAa,iBAAkB,SAAQ,sBAGrC;IACD,IAAa,OAAO,IAAI,MAAM,CAE7B;CACD;;;;;;;AACD,qBAAa,eAAgB,SAAQ,oBAAgE;IACpG,IAAa,OAAO,IAAI,MAAM,CAE7B;CACD;AAED,QAAA,MAAM,iBAAiB,gJAAqG,CAAC;AAC7H,MAAM,MAAM,iBAAiB,GAAG,OAAO,iBAAiB,CAAC,IAAI,CAAC;AAC9D,eAAO,MAAM,mBAAmB,wHAA+B,CAAC;;;;;AAEhE,qBAAa,mBAAoB,SAAQ,wBAGvC;CAAG;;;;AAEL,qBAAa,oBAAqB,SAAQ,yBAExC;CAAG;;;;AAEL,qBAAa,gBAAiB,SAAQ,qBAEpC;CAAG"}
package/dist/errors.js CHANGED
@@ -1,20 +1,36 @@
1
1
  import { Schema } from "effect";
2
- const answer = { method: Schema.String, status: Schema.Number, url: Schema.String };
2
+ const answer = { bodyExcerpt: Schema.optionalKey(Schema.String), method: Schema.String, status: Schema.Number, url: Schema.String };
3
+ function describe(answered) {
4
+ const url = new URL(answered.url);
5
+ const call = `HTTP ${answered.status}: ${answered.method} ${url.host}${url.pathname}`;
6
+ return answered.bodyExcerpt === undefined ? call : `${call}: ${answered.bodyExcerpt}`;
7
+ }
3
8
  export class RemoteUnauthorized extends Schema.TaggedError()("RemoteUnauthorized", answer) {
9
+ get message() {
10
+ return describe(this);
11
+ }
4
12
  }
5
13
  export class RemoteNotFound extends Schema.TaggedError()("RemoteNotFound", answer) {
14
+ get message() {
15
+ return describe(this);
16
+ }
6
17
  }
7
18
  export class RemoteGone extends Schema.TaggedError()("RemoteGone", answer) {
19
+ get message() {
20
+ return describe(this);
21
+ }
8
22
  }
9
23
  export class RemoteRateLimited extends Schema.TaggedError()("RemoteRateLimited", {
10
24
  ...answer,
11
25
  retryAfter: Schema.optionalKey(Schema.DateTimeUtc),
12
26
  }) {
27
+ get message() {
28
+ return describe(this);
29
+ }
13
30
  }
14
31
  export class RemoteHttpError extends Schema.TaggedError()("RemoteHttpError", answer) {
15
32
  get message() {
16
- const url = new URL(this.url);
17
- return `HTTP ${this.status}: ${this.method} ${url.host}${url.pathname}`;
33
+ return describe(this);
18
34
  }
19
35
  }
20
36
  const RemoteStatusError = Schema.Union([RemoteUnauthorized, RemoteNotFound, RemoteGone, RemoteRateLimited, RemoteHttpError]);
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,MAAM,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;AAEpF,MAAM,OAAO,kBAAmB,SAAQ,MAAM,CAAC,WAAW,EAAsB,CAAC,oBAAoB,EAAE,MAAM,CAAC;CAAG;AACjH,MAAM,OAAO,cAAe,SAAQ,MAAM,CAAC,WAAW,EAAkB,CAAC,gBAAgB,EAAE,MAAM,CAAC;CAAG;AACrG,MAAM,OAAO,UAAW,SAAQ,MAAM,CAAC,WAAW,EAAc,CAAC,YAAY,EAAE,MAAM,CAAC;CAAG;AACzF,MAAM,OAAO,iBAAkB,S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E;IACnG,GAAG,MAAM;IACT,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC;CAClD,CAAC;IACD,IAAa,OAAO;QACnB,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;CACD;AACD,MAAM,OAAO,eAAgB,SAAQ,MAAM,CAAC,WAAW,EAAmB,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACpG,IAAa,OAAO;QACnB,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;CACD;AAED,MAAM,iBAAiB,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,kBAAkB,EAAE,cAAc,EAAE,UAAU,EAAE,iBAAiB,EAAE,eAAe,CAAC,CAAC,CAAC;AAE7H,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC;AAEhE,MAAM,OAAO,mBAAoB,SAAQ,MAAM,CAAC,WAAW,EAAuB,CAAC,qBAAqB,EAAE;IACzG,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;IACpD,GAAG,EAAE,MAAM,CAAC,MAAM;CAClB,CAAC;CAAG;AAEL,MAAM,OAAO,oBAAqB,SAAQ,MAAM,CAAC,WAAW,EAAwB,CAAC,sBAAsB,EAAE;IAC5G,KAAK,EAAE,MAAM,CAAC,MAAM;CACpB,CAAC;CAAG;AAEL,MAAM,OAAO,gBAAiB,SAAQ,MAAM,CAAC,WAAW,EAAoB,CAAC,kBAAkB,EAAE;IAChG,KAAK,EAAE,MAAM,CAAC,MAAM;CACpB,CAAC;CAAG"}
@@ -1,6 +1,7 @@
1
1
  import { type Duration, Effect, Schema } from "effect";
2
2
  import { HttpClient } from "effect/http";
3
3
  declare const ReadClientError_base: Schema.Class<ReadClientError, Schema.TaggedStruct<"ReadClientError", {
4
+ readonly bodyExcerpt: Schema.optionalKey<Schema.String>;
4
5
  readonly reason: Schema.Literals<readonly ["timeout", "body-too-large", "status", "decode", "transport", "invalid-limit"]>;
5
6
  readonly source: Schema.String;
6
7
  readonly status: Schema.optionalKey<Schema.Number>;
@@ -1 +1 @@
1
- {"version":3,"file":"readClient.d.ts","sourceRoot":"","sources":["../src/readClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,QAAQ,EAAE,MAAM,EAAQ,MAAM,EAAE,MAAM,QAAQ,CAAC;AACpE,OAAO,EAAE,UAAU,EAAa,MAAM,aAAa,CAAC;;;;;;AAKpD,qBAAa,eAAgB,SAAQ,oBAInC;CAAG;AAEL,MAAM,WAAW,iBAAiB,CAAC,OAAO,SAAS,MAAM,CAAC,UAAU;IACnE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC;IACjC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,KAAK,CAAC;CACpC;AAED,eAAO,MAAM,cAAc,GAAgC,OAAO,SAAS,MAAM,CAAC,UAAU;eAoCvE,MAAM;8EACzB,CAAC"}
1
+ {"version":3,"file":"readClient.d.ts","sourceRoot":"","sources":["../src/readClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,QAAQ,EAAE,MAAM,EAAQ,MAAM,EAAE,MAAM,QAAQ,CAAC;AACpE,OAAO,EAAE,UAAU,EAAa,MAAM,aAAa,CAAC;;;;;;;AAKpD,qBAAa,eAAgB,SAAQ,oBAKnC;CAAG;AAkBL,MAAM,WAAW,iBAAiB,CAAC,OAAO,SAAS,MAAM,CAAC,UAAU;IACnE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC;IACjC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,KAAK,CAAC;CACpC;AAED,eAAO,MAAM,cAAc,GAAgC,OAAO,SAAS,MAAM,CAAC,UAAU;eAuBvE,MAAM;8EACzB,CAAC"}
@@ -4,11 +4,29 @@ import { readBoundedJson } from "#body.ts";
4
4
  import { InvalidBodyLimit, isRemoteStatusError, ResponseBodyTooLarge } from "#errors.ts";
5
5
  import { filterStatus } from "#filterStatus.ts";
6
6
  export class ReadClientError extends Schema.TaggedError()("ReadClientError", {
7
+ bodyExcerpt: Schema.optionalKey(Schema.String),
7
8
  reason: Schema.Literals(["timeout", "body-too-large", "status", "decode", "transport", "invalid-limit"]),
8
9
  source: Schema.String,
9
10
  status: Schema.optionalKey(Schema.Number),
10
11
  }) {
11
12
  }
13
+ function readFailure(error, source) {
14
+ if (isRemoteStatusError(error)) {
15
+ const { bodyExcerpt, status } = error;
16
+ return new ReadClientError({ reason: "status", source, status, ...(bodyExcerpt === undefined ? {} : { bodyExcerpt }) });
17
+ }
18
+ let reason = "transport";
19
+ if (error instanceof ResponseBodyTooLarge) {
20
+ reason = "body-too-large";
21
+ }
22
+ else if (error instanceof InvalidBodyLimit) {
23
+ reason = "invalid-limit";
24
+ }
25
+ else if (error instanceof Schema.SchemaError) {
26
+ reason = "decode";
27
+ }
28
+ return new ReadClientError({ reason, source });
29
+ }
12
30
  export const makeReadClient = Effect.fnUntraced(function* (options) {
13
31
  const client = filterStatus(yield* HttpClient.HttpClient);
14
32
  function lookup(url) {
@@ -17,22 +35,7 @@ export const makeReadClient = Effect.fnUntraced(function* (options) {
17
35
  yield* Effect.logDebug("Outside API request").pipe(Effect.annotateLogs({ method: "GET", source: options.source, url: parsed === null ? "<invalid>" : `${parsed.origin}${parsed.pathname}` }));
18
36
  const response = yield* client.get(url);
19
37
  return yield* readBoundedJson(response, options.bodyLimit, options.schema);
20
- }).pipe(Effect.mapError((error) => {
21
- if (isRemoteStatusError(error)) {
22
- return new ReadClientError({ reason: "status", source: options.source, status: error.status });
23
- }
24
- let reason = "transport";
25
- if (error instanceof ResponseBodyTooLarge) {
26
- reason = "body-too-large";
27
- }
28
- else if (error instanceof InvalidBodyLimit) {
29
- reason = "invalid-limit";
30
- }
31
- else if (error instanceof Schema.SchemaError) {
32
- reason = "decode";
33
- }
34
- return new ReadClientError({ reason, source: options.source });
35
- }), Effect.timeoutOrElse({
38
+ }).pipe(Effect.mapError((error) => readFailure(error, options.source)), Effect.timeoutOrElse({
36
39
  duration: options.timeout,
37
40
  orElse: () => Effect.fail(new ReadClientError({ reason: "timeout", source: options.source })),
38
41
  }), Effect.provideService(UrlParams.CurrentRedactedNames, [/.*/u]));
@@ -1 +1 @@
1
- {"version":3,"file":"readClient.js","sourceRoot":"","sources":["../src/readClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAiB,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD,MAAM,OAAO,eAAgB,SAAQ,MAAM,CAAC,WAAW,EAAmB,CAAC,iBAAiB,EAAE;IAC7F,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,EAAE,gBAAgB,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;IACxG,MAAM,EAAE,MAAM,CAAC,MAAM;IACrB,MAAM,EAAE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,CAAC,MAAM,CAAC,CAAC,EAC9D,MAAM,CAAC,aAAa,CAAC;YACpB,QAAQ,EAAE,OAAO,CAAC,OAAO;YACzB,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,eAAe,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;SAC7F,CAAC,EACF,MAAM,CAAC,cAAc,CAAC,SAAS,CAAC,oBAAoB,EAAE,CAAC,KAAK,CAAC,CAAC,CAC9D,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE;QAC3C,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;KACrE,CAAC,CAAC;IACH,OAAO,EAAE,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;AACxD,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/http",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "Bounded HTTP bodies, status failures and paginated clients over Effect HTTP",
5
5
  "keywords": [
6
6
  "effect"
@@ -44,17 +44,20 @@
44
44
  "CHANGELOG.md",
45
45
  "README.md"
46
46
  ],
47
+ "dependencies": {
48
+ "@shivaedev/errors": "0.13.1"
49
+ },
47
50
  "devDependencies": {
48
- "@effect/platform-node": "4.1.1",
49
- "@shivaedev/quality": "0.70.1",
50
- "@shivaedev/test-story": "0.45.2",
51
+ "@effect/platform-node": "4.1.4",
52
+ "@shivaedev/quality": "0.78.5",
53
+ "@shivaedev/test-story": "0.49.0",
51
54
  "@types/node": "24.19.2",
52
- "effect": "4.1.1",
55
+ "effect": "4.1.4",
53
56
  "typescript": "7.0.2",
54
- "vitest": "5.0.5"
57
+ "vitest": "5.1.0"
55
58
  },
56
59
  "peerDependencies": {
57
- "effect": "4.1.1"
60
+ "effect": "4.1.4"
58
61
  },
59
62
  "engines": {
60
63
  "node": ">=24"
@@ -0,0 +1,58 @@
1
+ import { Duration, Effect, Stream } from "effect";
2
+ import type { HttpClientResponse } from "effect/http";
3
+ import { isSensitiveKey, redactText } from "@shivaedev/errors/sensitive.ts";
4
+
5
+ export const BODY_EXCERPT_BYTES = 2048;
6
+ export const bodyExcerptDeadline = Duration.seconds(1);
7
+
8
+ // A cut can split a token or a JWT; dropping the partial word keeps half a secret out of the excerpt.
9
+ const PARTIAL_WORD = /[^\s"'{}[\]()<>,;:]+$/u;
10
+
11
+ function isBinary(text: string): boolean {
12
+ for (const character of text) {
13
+ const code = character.codePointAt(0) ?? 0;
14
+ const control = (code < 0x20 && character !== "\t" && character !== "\n" && character !== "\r") || code === 0x7f;
15
+ if (control || character === "\uFFFD") {
16
+ return true;
17
+ }
18
+ }
19
+ return false;
20
+ }
21
+
22
+ export function bodyExcerpt(bytes: Uint8Array, truncated: boolean): string | undefined {
23
+ const decoded = new TextDecoder().decode(bytes, { stream: true });
24
+ if (isBinary(decoded)) {
25
+ return undefined;
26
+ }
27
+ const collapsed = decoded.replaceAll(/\s+/gu, " ").trim();
28
+ const text = truncated ? collapsed.replace(PARTIAL_WORD, "").trimEnd() : collapsed;
29
+ if (text === "") {
30
+ return undefined;
31
+ }
32
+ const redacted = redactText(text, isSensitiveKey, BODY_EXCERPT_BYTES);
33
+ return truncated ? `${redacted}…` : redacted;
34
+ }
35
+
36
+ export function readBodyExcerpt(response: HttpClientResponse.HttpClientResponse): Effect.Effect<string | undefined> {
37
+ return Effect.suspend(() => {
38
+ const kept = new Uint8Array(BODY_EXCERPT_BYTES);
39
+ let size = 0;
40
+ let truncated = false;
41
+ return response.stream.pipe(
42
+ Stream.runForEachWhile((chunk) =>
43
+ Effect.sync(() => {
44
+ const room = BODY_EXCERPT_BYTES - size;
45
+ kept.set(chunk.subarray(0, room), size);
46
+ size += Math.min(room, chunk.byteLength);
47
+ truncated = chunk.byteLength > room;
48
+ return !truncated;
49
+ }),
50
+ ),
51
+ Effect.timeout(bodyExcerptDeadline),
52
+ Effect.map(() => bodyExcerpt(kept.subarray(0, size), truncated)),
53
+ Effect.orElseSucceed(() => undefined),
54
+ // A body another reader already consumed dies when its stream opens; the status failure still wins.
55
+ Effect.catchDefect(() => Effect.succeed(undefined)),
56
+ );
57
+ });
58
+ }
@@ -1,6 +1,7 @@
1
- import { DateTime, Effect, Stream } from "effect";
1
+ import { DateTime, Effect } from "effect";
2
2
  import type { HttpClientResponse } from "effect/http";
3
3
  import * as HttpStatus from "effect/http/HttpStatus";
4
+ import { readBodyExcerpt } from "#bodyExcerpt.ts";
4
5
  import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, type RemoteStatusError, RemoteUnauthorized } from "#errors.ts";
5
6
  import { parseRetryAfter } from "#parseRetryAfter.ts";
6
7
 
@@ -9,8 +10,10 @@ export const checkStatus = Effect.fnUntraced(function* (response: HttpClientResp
9
10
  if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
10
11
  return response;
11
12
  }
13
+ const now = yield* DateTime.now;
14
+ const bodyExcerpt = yield* readBodyExcerpt(response);
12
15
  const url = new URL(request.url);
13
- const fields = { method: request.method, status, url: `${url.origin}${url.pathname}` };
16
+ const fields = { method: request.method, status, url: `${url.origin}${url.pathname}`, ...(bodyExcerpt === undefined ? {} : { bodyExcerpt }) };
14
17
  let error: RemoteStatusError;
15
18
  switch (status) {
16
19
  case HttpStatus.fromLiteral("Unauthorized"):
@@ -25,13 +28,12 @@ export const checkStatus = Effect.fnUntraced(function* (response: HttpClientResp
25
28
  default: {
26
29
  const header = response.headers["retry-after"];
27
30
  if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
28
- const retryAfter = parseRetryAfter(header, yield* DateTime.now);
31
+ const retryAfter = parseRetryAfter(header, now);
29
32
  error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
30
33
  } else {
31
34
  error = new RemoteHttpError(fields);
32
35
  }
33
36
  }
34
37
  }
35
- yield* Effect.scoped(Stream.toPull(response.stream));
36
38
  return yield* Effect.fail(error);
37
39
  });
package/src/errors.ts CHANGED
@@ -1,18 +1,39 @@
1
1
  import { Schema } from "effect";
2
2
 
3
- const answer = { method: Schema.String, status: Schema.Number, url: Schema.String };
3
+ const answer = { bodyExcerpt: Schema.optionalKey(Schema.String), method: Schema.String, status: Schema.Number, url: Schema.String };
4
4
 
5
- export class RemoteUnauthorized extends Schema.TaggedError<RemoteUnauthorized>()("RemoteUnauthorized", answer) {}
6
- export class RemoteNotFound extends Schema.TaggedError<RemoteNotFound>()("RemoteNotFound", answer) {}
7
- export class RemoteGone extends Schema.TaggedError<RemoteGone>()("RemoteGone", answer) {}
5
+ function describe(answered: { readonly bodyExcerpt?: string; readonly method: string; readonly status: number; readonly url: string }): string {
6
+ const url = new URL(answered.url);
7
+ const call = `HTTP ${answered.status}: ${answered.method} ${url.host}${url.pathname}`;
8
+ return answered.bodyExcerpt === undefined ? call : `${call}: ${answered.bodyExcerpt}`;
9
+ }
10
+
11
+ export class RemoteUnauthorized extends Schema.TaggedError<RemoteUnauthorized>()("RemoteUnauthorized", answer) {
12
+ override get message(): string {
13
+ return describe(this);
14
+ }
15
+ }
16
+ export class RemoteNotFound extends Schema.TaggedError<RemoteNotFound>()("RemoteNotFound", answer) {
17
+ override get message(): string {
18
+ return describe(this);
19
+ }
20
+ }
21
+ export class RemoteGone extends Schema.TaggedError<RemoteGone>()("RemoteGone", answer) {
22
+ override get message(): string {
23
+ return describe(this);
24
+ }
25
+ }
8
26
  export class RemoteRateLimited extends Schema.TaggedError<RemoteRateLimited>()("RemoteRateLimited", {
9
27
  ...answer,
10
28
  retryAfter: Schema.optionalKey(Schema.DateTimeUtc),
11
- }) {}
29
+ }) {
30
+ override get message(): string {
31
+ return describe(this);
32
+ }
33
+ }
12
34
  export class RemoteHttpError extends Schema.TaggedError<RemoteHttpError>()("RemoteHttpError", answer) {
13
35
  override get message(): string {
14
- const url = new URL(this.url);
15
- return `HTTP ${this.status}: ${this.method} ${url.host}${url.pathname}`;
36
+ return describe(this);
16
37
  }
17
38
  }
18
39
 
package/src/readClient.ts CHANGED
@@ -5,11 +5,28 @@ import { InvalidBodyLimit, isRemoteStatusError, ResponseBodyTooLarge } from "#er
5
5
  import { filterStatus } from "#filterStatus.ts";
6
6
 
7
7
  export class ReadClientError extends Schema.TaggedError<ReadClientError>()("ReadClientError", {
8
+ bodyExcerpt: Schema.optionalKey(Schema.String),
8
9
  reason: Schema.Literals(["timeout", "body-too-large", "status", "decode", "transport", "invalid-limit"]),
9
10
  source: Schema.String,
10
11
  status: Schema.optionalKey(Schema.Number),
11
12
  }) {}
12
13
 
14
+ function readFailure(error: unknown, source: string): ReadClientError {
15
+ if (isRemoteStatusError(error)) {
16
+ const { bodyExcerpt, status } = error;
17
+ return new ReadClientError({ reason: "status", source, status, ...(bodyExcerpt === undefined ? {} : { bodyExcerpt }) });
18
+ }
19
+ let reason: ReadClientError["reason"] = "transport";
20
+ if (error instanceof ResponseBodyTooLarge) {
21
+ reason = "body-too-large";
22
+ } else if (error instanceof InvalidBodyLimit) {
23
+ reason = "invalid-limit";
24
+ } else if (error instanceof Schema.SchemaError) {
25
+ reason = "decode";
26
+ }
27
+ return new ReadClientError({ reason, source });
28
+ }
29
+
13
30
  export interface ReadClientOptions<TSchema extends Schema.Constraint> {
14
31
  readonly bodyLimit: number;
15
32
  readonly capacity: number;
@@ -30,20 +47,7 @@ export const makeReadClient = Effect.fnUntraced(function* <TSchema extends Schem
30
47
  const response = yield* client.get(url);
31
48
  return yield* readBoundedJson(response, options.bodyLimit, options.schema);
32
49
  }).pipe(
33
- Effect.mapError((error) => {
34
- if (isRemoteStatusError(error)) {
35
- return new ReadClientError({ reason: "status", source: options.source, status: error.status });
36
- }
37
- let reason: ReadClientError["reason"] = "transport";
38
- if (error instanceof ResponseBodyTooLarge) {
39
- reason = "body-too-large";
40
- } else if (error instanceof InvalidBodyLimit) {
41
- reason = "invalid-limit";
42
- } else if (error instanceof Schema.SchemaError) {
43
- reason = "decode";
44
- }
45
- return new ReadClientError({ reason, source: options.source });
46
- }),
50
+ Effect.mapError((error) => readFailure(error, options.source)),
47
51
  Effect.timeoutOrElse({
48
52
  duration: options.timeout,
49
53
  orElse: () => Effect.fail(new ReadClientError({ reason: "timeout", source: options.source })),