@arcjet/decorate 1.0.0-beta.10 → 1.0.0-beta.11

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.
Files changed (3) hide show
  1. package/index.d.ts +27 -15
  2. package/index.js +13 -7
  3. package/package.json +8 -9
package/index.d.ts CHANGED
@@ -1,28 +1,40 @@
1
1
  import type { ArcjetDecision } from "@arcjet/protocol";
2
2
  interface HeaderLike {
3
- has(name: string): boolean;
4
3
  get(name: string): string | null;
4
+ has(name: string): boolean;
5
5
  set(name: string, value: string): void;
6
6
  }
7
- export interface ArcjetCanDecorate {
8
- has?: (name: string) => boolean;
9
- get?: (name: string) => string | null;
10
- set?: (name: string, value: string) => void;
11
- headers?: HeaderLike;
12
- headersSent?: boolean;
13
- hasHeader?: (name: string) => boolean;
14
- getHeader?: (name: string) => number | string | string[] | undefined;
15
- setHeader?: (name: string, value: number | string | ReadonlyArray<string>) => unknown;
7
+ interface ResponseLike {
8
+ headers: HeaderLike;
9
+ }
10
+ interface OutgoingMessageLike {
11
+ getHeader: (name: string) => string[] | number | string | undefined;
12
+ headersSent: boolean;
13
+ hasHeader: (name: string) => boolean;
14
+ setHeader: (name: string, value: ReadonlyArray<string> | number | string) => unknown;
16
15
  }
17
16
  /**
18
- * Decorates an object with `RateLimit` and `RateLimit-Policy` headers based
19
- * on an {@link ArcjetDecision} and conforming to the [Rate Limit fields for
17
+ * Decorable value.
18
+ *
19
+ * Something that looks like `Headers` (Fetch),
20
+ * `OutgoingMessage` (Node.js), or
21
+ * `Response` (Fetch).
22
+ */
23
+ export type ArcjetCanDecorate = HeaderLike | OutgoingMessageLike | ResponseLike;
24
+ /**
25
+ * Decorate something based on an Arcjet decision with rate limit headers.
26
+ *
27
+ * Sets `RateLimit-Policy` and `RateLimit` and conform to the
28
+ * [Rate Limit fields for
20
29
  * HTTP](https://ietf-wg-httpapi.github.io/ratelimit-headers/draft-ietf-httpapi-ratelimit-headers.html)
21
30
  * draft specification.
22
31
  *
23
- * @param value The object to decorate—must be similar to {@link Headers}, {@link Response} or
24
- * {@link OutgoingMessage}.
25
- * @param decision The {@link ArcjetDecision} that was made by calling `protect()` on the SDK.
32
+ * @param value
33
+ * Decorable value.
34
+ * @param decision
35
+ * Decision from `protect()`.
36
+ * @returns
37
+ * Nothing.
26
38
  */
27
39
  export declare function setRateLimitHeaders(value: ArcjetCanDecorate, decision: ArcjetDecision): void;
28
40
  export {};
package/index.js CHANGED
@@ -13,13 +13,14 @@ function isHeaderLike(value) {
13
13
  return false;
14
14
  }
15
15
  function isResponseLike(value) {
16
- if (typeof value.headers === "undefined") {
16
+ if (!("headers" in value) || typeof value.headers === "undefined") {
17
17
  return false;
18
18
  }
19
19
  return isHeaderLike(value.headers);
20
20
  }
21
21
  function isOutgoingMessageLike(response) {
22
- if (typeof response.headersSent !== "boolean") {
22
+ if (!("headersSent" in response) ||
23
+ typeof response.headersSent !== "boolean") {
23
24
  return false;
24
25
  }
25
26
  if (typeof response.hasHeader !== "function") {
@@ -71,14 +72,19 @@ function nearestLimit(current, next) {
71
72
  return next;
72
73
  }
73
74
  /**
74
- * Decorates an object with `RateLimit` and `RateLimit-Policy` headers based
75
- * on an {@link ArcjetDecision} and conforming to the [Rate Limit fields for
75
+ * Decorate something based on an Arcjet decision with rate limit headers.
76
+ *
77
+ * Sets `RateLimit-Policy` and `RateLimit` and conform to the
78
+ * [Rate Limit fields for
76
79
  * HTTP](https://ietf-wg-httpapi.github.io/ratelimit-headers/draft-ietf-httpapi-ratelimit-headers.html)
77
80
  * draft specification.
78
81
  *
79
- * @param value The object to decorate—must be similar to {@link Headers}, {@link Response} or
80
- * {@link OutgoingMessage}.
81
- * @param decision The {@link ArcjetDecision} that was made by calling `protect()` on the SDK.
82
+ * @param value
83
+ * Decorable value.
84
+ * @param decision
85
+ * Decision from `protect()`.
86
+ * @returns
87
+ * Nothing.
82
88
  */
83
89
  function setRateLimitHeaders(value, decision) {
84
90
  const rateLimitReasons = decision.results
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcjet/decorate",
3
- "version": "1.0.0-beta.10",
3
+ "version": "1.0.0-beta.11",
4
4
  "description": "Arcjet utilities for decorating responses with information",
5
5
  "keywords": [
6
6
  "arcjet",
@@ -43,16 +43,15 @@
43
43
  "test": "npm run build && npm run lint && npm run test-coverage"
44
44
  },
45
45
  "dependencies": {
46
- "@arcjet/protocol": "1.0.0-beta.10",
47
- "@arcjet/sprintf": "1.0.0-beta.10"
46
+ "@arcjet/protocol": "1.0.0-beta.11",
47
+ "@arcjet/sprintf": "1.0.0-beta.11"
48
48
  },
49
49
  "devDependencies": {
50
- "@arcjet/eslint-config": "1.0.0-beta.10",
51
- "@arcjet/rollup-config": "1.0.0-beta.10",
52
- "@arcjet/tsconfig": "1.0.0-beta.10",
53
- "@rollup/wasm-node": "4.46.2",
54
- "@types/node": "18.18.0",
55
- "eslint": "9.32.0",
50
+ "@arcjet/eslint-config": "1.0.0-beta.11",
51
+ "@arcjet/rollup-config": "1.0.0-beta.11",
52
+ "@rollup/wasm-node": "4.50.0",
53
+ "@types/node": "24.3.0",
54
+ "eslint": "9.34.0",
56
55
  "typescript": "5.9.2"
57
56
  },
58
57
  "publishConfig": {