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

1.0.0-alpha.27 • Public • Published
Arcjet Logo

@arcjet/decorate

npm badge

Arcjet utilities for decorating responses with information.

Installation

npm install -S @arcjet/decorate

Example

import arcjet, { fixedWindow } from "@arcjet/next";
import { setRateLimitHeaders } from "@arcjet/decorate";
import { NextApiRequest, NextApiResponse } from "next";

const aj = arcjet({
  key: process.env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
  rules: [
    // Create a fixed window rate limit. Other algorithms are supported.
    fixedWindow({
      mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
      window: "1m", // 1 min fixed window
      max: 1, // allow a single request
    }),
  ],
});

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

  setRateLimitHeaders(res, decision);

  if (decision.isDenied()) {
    return res.status(429).json({
      error: "Too Many Requests",
      reason: decision.reason,
    });
  }

  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/decorate

Homepage

arcjet.com

Weekly Downloads

195

Version

1.0.0-alpha.27

License

Apache-2.0

Unpacked Size

30.8 kB

Total Files

6

Last publish

Collaborators

  • blaine-arcjet
  • davidmytton