Skip to content

Commit

Permalink
deprecate!: deprecate reason for ArcjetAllowDecision
Browse files Browse the repository at this point in the history
  • Loading branch information
e-moran committed Jan 2, 2025
1 parent 02e4435 commit 252d92f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
4 changes: 3 additions & 1 deletion decorate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function setRateLimitHeaders(
.sort(sortByLowestMax)
.map(toPolicyString)
.join(", ");
} else {
} else if (typeof decision.reason !== "undefined") {
// For cached decisions, we may not have rule results, but we'd still have
// the top-level reason.
if (isRateLimitReason(decision.reason)) {
Expand All @@ -229,6 +229,8 @@ export function setRateLimitHeaders(
} else {
return;
}
} else {
return;
}

if (isHeaderLike(value)) {
Expand Down
3 changes: 2 additions & 1 deletion protocol/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "./convert.js";
import type {
ArcjetContext,
ArcjetDecisionWithReason,
ArcjetRequestDetails,
ArcjetRule,
} from "./index.js";
Expand Down Expand Up @@ -127,7 +128,7 @@ export function createClient(options: ClientOptions): Client {
report(
context: ArcjetContext,
details: ArcjetRequestDetails,
decision: ArcjetDecision,
decision: ArcjetDecisionWithReason,
rules: ArcjetRule[],
): void {
const { log } = context;
Expand Down
5 changes: 4 additions & 1 deletion protocol/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ArcjetSlidingWindowRateLimitRule,
ArcjetShieldRule,
ArcjetSensitiveInfoRule,
ArcjetDecisionWithReason,
} from "./index.js";
import {
ArcjetAllowDecision,
Expand Down Expand Up @@ -391,7 +392,9 @@ export function ArcjetRuleResultFromProtocol(
});
}

export function ArcjetDecisionToProtocol(decision: ArcjetDecision): Decision {
export function ArcjetDecisionToProtocol(
decision: ArcjetDecisionWithReason,
): Decision {
return new Decision({
id: decision.id,
ttl: decision.ttl,
Expand Down
11 changes: 8 additions & 3 deletions protocol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ export abstract class ArcjetDecision {
ip: ArcjetIpDetails;

abstract conclusion: ArcjetConclusion;
abstract reason: ArcjetReason;
abstract reason: ArcjetReason | undefined;

constructor(init: {
id?: string;
Expand Down Expand Up @@ -627,15 +627,20 @@ export abstract class ArcjetDecision {
}
}

export type ArcjetDecisionWithReason = ArcjetDecision & {
reason: NonNullable<ArcjetDecision["reason"]>;
};

export class ArcjetAllowDecision extends ArcjetDecision {
conclusion = "ALLOW" as const;
reason: ArcjetReason;
/** @deprecated */
reason: ArcjetReason | undefined;

constructor(init: {
id?: string;
results: ArcjetRuleResult[];
ttl: number;
reason: ArcjetReason;
reason?: ArcjetReason;
ip?: ArcjetIpDetails;
}) {
super(init);
Expand Down
3 changes: 2 additions & 1 deletion protocol/test/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
ArcjetFixedWindowRateLimitRule,
ArcjetSlidingWindowRateLimitRule,
ArcjetShieldRule,
ArcjetDecisionWithReason,
} from "../index.js";
import {
ArcjetAllowDecision,
Expand Down Expand Up @@ -519,7 +520,7 @@ describe("convert", () => {
results: [],
reason: new ArcjetReason(),
ip: new ArcjetIpDetails(),
}),
}) as ArcjetDecisionWithReason,
),
).toEqual(
new Decision({
Expand Down

0 comments on commit 252d92f

Please sign in to comment.