@arcjet/inspect
v1.0.0
Published
Arcjet utilities for inspecting decisions made an the SDK
Readme
@arcjet/inspect
Arcjet utilities for inspecting decisions made by an SDK.
What is this?
Arcjet attaches lots of metadata to every decision. This package exists to more easily interact with Arcjet decisions for common patterns.
When should I use this?
You can access metadata on decisions directly but you can use this package for common patterns.
Install
This package is ESM only. Install with npm in Node.js:
npm install @arcjet/inspectExample
import http from "node:http";
import arcjet, { detectBot } from "@arcjet/next";
import { isMissingUserAgent } from "@arcjet/inspect";
// Get your Arcjet key at <https://app.arcjet.com>.
// Set it as an environment variable instead of hard coding it.
const arcjetKey = process.env.ARCJET_KEY;
if (!arcjetKey) {
throw new Error("Cannot find `ARCJET_KEY` environment variable");
}
const aj = arcjet({
key: arcjetKey,
rules: [
// `detectBot` lets you manage automated clients and bots.
detectBot({ allow: [], mode: "LIVE" }),
],
});
const server = http.createServer(async function (
request: http.IncomingMessage,
response: http.ServerResponse,
) {
const decision = await aj.protect(request);
if (decision.isDenied()) {
response.writeHead(403, { "Content-Type": "application/json" });
response.end(JSON.stringify({ message: "Forbidden" }));
return;
}
if (decision.results.some(isMissingUserAgent)) {
response.writeHead(403, { "Content-Type": "application/json" });
response.end(JSON.stringify({ message: "You are a bot!" }));
return;
}
response.writeHead(200, { "Content-Type": "application/json" });
response.end(JSON.stringify({ message: "Hello world" }));
});
server.listen(8000);