@arcjet/inspect
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-beta.5 • Public • Published
Arcjet Logo

@arcjet/inspect

npm badge

Arcjet utilities for inspecting decisions made by an SDK.

Installation

npm install -S @arcjet/inspect

Example

import arcjet, { detectBot } from "@arcjet/next";
import { isSpoofedBot, isMissingUserAgent } from "@arcjet/inspect";
import { NextApiRequest, NextApiResponse } from "next";

const aj = arcjet({
  key: process.env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
  rules: [
    detectBot({
      mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
      allow: [], // "allow none" will block all detected bots
    }),
  ],
});

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  const decision = await aj.protect(req);

  if (decision.isDenied()) {
    return res.status(403).json({ error: "Forbidden" });
  }

  // We expect all non-bot clients to have the User-Agent header
  if (decision.results.some(isMissingUserAgent)) {
    return res.status(403).json({ error: "You are a bot!" });
  }

  if (decision.results.some(isSpoofedBot)) {
    return res
      .status(403)
      .json({ error: "You are pretending to be a good bot!" });
  }

  res.status(200).json({ name: "Hello world" });
}

License

Licensed under the Apache License, Version 2.0.

Readme

Keywords

none

Package Sidebar

Install

npm i @arcjet/inspect

Homepage

arcjet.com

Weekly Downloads

127

Version

1.0.0-beta.5

License

Apache-2.0

Unpacked Size

27.2 kB

Total Files

5

Last publish

Collaborators

  • blaine-arcjet
  • davidmytton