Skip to content

Commit

Permalink
fix(protocol)!: Improve deprecation message on enum-like field usage
Browse files Browse the repository at this point in the history
  • Loading branch information
blaine-arcjet committed Jan 14, 2025
1 parent 8053716 commit 498d08c
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 85 deletions.
2 changes: 1 addition & 1 deletion arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import type {
ArcjetIdentifiedEntity,
ArcjetWellKnownBot,
ArcjetBotCategory,
ArcjetEmailType,
} from "@arcjet/protocol";
import {
ArcjetBotReason,
ArcjetEmailReason,
ArcjetEmailType,
ArcjetErrorReason,
ArcjetMode,
ArcjetReason,
Expand Down
89 changes: 31 additions & 58 deletions arcjet/test/arcjet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { expect } from "expect";

import type { ArcjetRule, ArcjetLocalRule, Primitive, Arcjet } from "../index";
import arcjet, {
ArcjetMode,
detectBot,
validateEmail,
protectSignup,
ArcjetEmailType,
ArcjetAllowDecision,
ArcjetDenyDecision,
ArcjetErrorDecision,
Expand Down Expand Up @@ -481,10 +479,6 @@ describe("Primitive > detectBot", () => {
});

test("denies curl", async () => {
const options = {
mode: ArcjetMode.LIVE,
allow: [],
};
const context = {
key: "test-key",
fingerprint: "test-fingerprint",
Expand All @@ -507,7 +501,10 @@ describe("Primitive > detectBot", () => {
},
};

const [rule] = detectBot(options);
const [rule] = detectBot({
mode: "LIVE",
allow: [],
});
expect(rule.type).toEqual("BOT");
assertIsLocalRule(rule);
const result = await rule.protect(context, details);
Expand Down Expand Up @@ -560,7 +557,7 @@ describe("Primitive > detectBot", () => {
};

const [rule] = detectBot({
mode: ArcjetMode.LIVE,
mode: "LIVE",
deny: ["CURL"],
});
expect(rule.type).toEqual("BOT");
Expand Down Expand Up @@ -613,7 +610,7 @@ describe("Primitive > detectBot", () => {
};

const [rule] = detectBot({
mode: ArcjetMode.LIVE,
mode: "LIVE",
allow: ["CURL"],
});
expect(rule.type).toEqual("BOT");
Expand Down Expand Up @@ -1302,17 +1299,9 @@ describe("Primitive > validateEmail", () => {
});

test("allows specifying EmailTypes to deny", async () => {
const options = {
deny: [
ArcjetEmailType.DISPOSABLE,
ArcjetEmailType.FREE,
ArcjetEmailType.NO_GRAVATAR,
ArcjetEmailType.NO_MX_RECORDS,
ArcjetEmailType.INVALID,
],
};

const [rule] = validateEmail(options);
const [rule] = validateEmail({
deny: ["DISPOSABLE", "FREE", "NO_GRAVATAR", "NO_MX_RECORDS", "INVALID"],
});
expect(rule.type).toEqual("EMAIL");
expect(rule).toHaveProperty("deny", [
"DISPOSABLE",
Expand All @@ -1324,17 +1313,9 @@ describe("Primitive > validateEmail", () => {
});

test("allows specifying EmailTypes to block and maps these to deny", async () => {
const options = {
block: [
ArcjetEmailType.DISPOSABLE,
ArcjetEmailType.FREE,
ArcjetEmailType.NO_GRAVATAR,
ArcjetEmailType.NO_MX_RECORDS,
ArcjetEmailType.INVALID,
],
};

const [rule] = validateEmail(options);
const [rule] = validateEmail({
block: ["DISPOSABLE", "FREE", "NO_GRAVATAR", "NO_MX_RECORDS", "INVALID"],
});
expect(rule.type).toEqual("EMAIL");
expect(rule).toHaveProperty("deny", [
"DISPOSABLE",
Expand All @@ -1346,17 +1327,9 @@ describe("Primitive > validateEmail", () => {
});

test("allows specifying EmailTypes to allow", async () => {
const options = {
allow: [
ArcjetEmailType.DISPOSABLE,
ArcjetEmailType.FREE,
ArcjetEmailType.NO_GRAVATAR,
ArcjetEmailType.NO_MX_RECORDS,
ArcjetEmailType.INVALID,
],
};

const [rule] = validateEmail(options);
const [rule] = validateEmail({
allow: ["DISPOSABLE", "FREE", "NO_GRAVATAR", "NO_MX_RECORDS", "INVALID"],
});
expect(rule.type).toEqual("EMAIL");
expect(rule).toHaveProperty("allow", [
"DISPOSABLE",
Expand Down Expand Up @@ -2404,18 +2377,18 @@ describe("Products > protectSignup", () => {
test("allows configuration of rateLimit, bot, and email", () => {
const rules = protectSignup({
rateLimit: {
mode: ArcjetMode.DRY_RUN,
mode: "DRY_RUN",
characteristics: ["ip.src"],
interval: 60 /* minutes */ * 60 /* seconds */,
max: 1,
},
bots: {
mode: ArcjetMode.DRY_RUN,
mode: "DRY_RUN",
allow: [],
},
email: {
allow: [],
mode: ArcjetMode.LIVE,
mode: "LIVE",
},
});
expect(rules.length).toEqual(3);
Expand All @@ -2425,7 +2398,7 @@ describe("Products > protectSignup", () => {
describe("SDK", () => {
function testRuleLocalAllowed() {
return {
mode: ArcjetMode.LIVE,
mode: "LIVE",
type: "TEST_RULE_LOCAL_ALLOWED",
priority: 1,
validate: mock.fn(),
Expand All @@ -2438,11 +2411,11 @@ describe("SDK", () => {
reason: new ArcjetTestReason(),
}),
),
};
} as const;
}
function testRuleLocalDenied() {
return {
mode: ArcjetMode.LIVE,
mode: "LIVE",
type: "TEST_RULE_LOCAL_DENIED",
priority: 1,
validate: mock.fn(),
Expand All @@ -2455,16 +2428,16 @@ describe("SDK", () => {
reason: new ArcjetTestReason(),
}),
),
};
} as const;
}
function testRuleLocalIncorrect() {
return {
mode: ArcjetMode.LIVE,
mode: "LIVE",
type: "TEST_RULE_LOCAL_INCORRECT",
priority: 1,
validate: mock.fn(),
protect: mock.fn(async () => undefined),
};
} as const;
}

function testRuleRemote(): ArcjetRule {
Expand All @@ -2485,27 +2458,27 @@ describe("SDK", () => {

function testRuleInvalidType(): ArcjetRule {
return {
mode: ArcjetMode.LIVE,
mode: "LIVE",
type: "TEST_RULE_INVALID_TYPE",
priority: 1,
};
}

function testRuleLocalThrow() {
return {
mode: ArcjetMode.LIVE,
mode: "LIVE",
type: "TEST_RULE_LOCAL_THROW",
priority: 1,
validate: mock.fn(),
protect: mock.fn(async () => {
throw new Error("Local rule protect failed");
}),
};
} as const;
}

function testRuleLocalDryRun() {
return {
mode: ArcjetMode.DRY_RUN,
mode: "DRY_RUN",
type: "TEST_RULE_LOCAL_DRY_RUN",
priority: 1,
validate: mock.fn(),
Expand All @@ -2517,7 +2490,7 @@ describe("SDK", () => {
reason: new ArcjetTestReason(),
});
}),
};
} as const;
}

function testRuleProps(): Primitive<{ abc: number }> {
Expand Down Expand Up @@ -3688,7 +3661,7 @@ describe("SDK", () => {

function testRuleLocalThrowString(): ArcjetLocalRule {
return {
mode: ArcjetMode.LIVE,
mode: "LIVE",
type: "TEST_RULE_LOCAL_THROW_STRING",
priority: 1,
validate: mock.fn(),
Expand Down Expand Up @@ -3745,7 +3718,7 @@ describe("SDK", () => {

function testRuleLocalThrowNull(): ArcjetLocalRule {
return {
mode: ArcjetMode.LIVE,
mode: "LIVE",
type: "TEST_RULE_LOCAL_THROW_NULL",
priority: 1,
validate: mock.fn(),
Expand Down
Loading

0 comments on commit 498d08c

Please sign in to comment.