Skip to content

Commit

Permalink
docs(examples): Expanded AI example with rate limit by user ID (#221)
Browse files Browse the repository at this point in the history
Improvements to the Next.js example apps:

- Added an example for rate limiting by custom user ID.
- Improved the comments throughout the example.
- Added an environment var type definition to avoid TS complaining.
- Includes a new example to [submit to the Next.js repo](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md). Maintaining a copy of the code here so we can keep track of all the example apps we create.
  • Loading branch information
davidmytton authored Feb 13, 2024
1 parent c102c64 commit 915d3fc
Show file tree
Hide file tree
Showing 44 changed files with 654 additions and 360 deletions.
1 change: 1 addition & 0 deletions examples/nextjs-13-pages-wrap/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARCJET_KEY=
6 changes: 1 addition & 5 deletions examples/nextjs-13-pages-wrap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ Route](https://nextjs.org/docs/pages/building-your-application/routing/api-route
npm ci
```

3. Add your Arcjet key to `.env.local`

```env
ARCJET_KEY=
```
3. Rename `.env.local.example` to `.env.local` and add your Arcjet key.

4. Start the dev server.

Expand Down
5 changes: 5 additions & 0 deletions examples/nextjs-13-pages-wrap/environment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare namespace NodeJS {
export interface ProcessEnv {
readonly ARCJET_KEY: string;
}
}
5 changes: 4 additions & 1 deletion examples/nextjs-13-pages-wrap/next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
// Disabled due to errors when building. See
// https://nextjs.org/docs/messages/failed-loading-swc
swcMinify: false,
}

module.exports = nextConfig
module.exports = nextConfig
12 changes: 6 additions & 6 deletions examples/nextjs-13-pages-wrap/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions examples/nextjs-13-pages-wrap/pages/api/arcjet-edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ const aj = arcjet({
// Get your site key from https://app.arcjet.com
// and set it as an environment variable rather than hard coding.
// See: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
key: process.env.ARCJET_KEY!,
key: process.env.ARCJET_KEY,
rules: [
// Fixed window rate limit. Arcjet also supports sliding window and token
// bucket.
fixedWindow({
mode: "LIVE",
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
// Limiting by ip.src is the default if not specified
//characteristics: ["ip.src"],
window: "1m",
max: 1,
window: "1m", // 1 min fixed window
max: 1, // allow a single request (for demo purposes)
}),
],
});
Expand Down
10 changes: 6 additions & 4 deletions examples/nextjs-13-pages-wrap/pages/api/arcjet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ const aj = arcjet({
// Get your site key from https://app.arcjet.com
// and set it as an environment variable rather than hard coding.
// See: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
key: process.env.ARCJET_KEY!,
key: process.env.ARCJET_KEY,
rules: [
// Fixed window rate limit. Arcjet also supports sliding window and token
// bucket.
fixedWindow({
mode: "LIVE",
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
// Limiting by ip.src is the default if not specified
//characteristics: ["ip.src"],
window: "1m",
max: 1,
window: "1m", // 1 min fixed window
max: 1, // allow a single request (for demo purposes)
}),
],
});
Expand Down
1 change: 1 addition & 0 deletions examples/nextjs-14-app-dir-rl/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARCJET_KEY=
6 changes: 1 addition & 5 deletions examples/nextjs-14-app-dir-rl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ handler](https://nextjs.org/docs/app/building-your-application/routing/route-han
npm ci
```

3. Add your Arcjet key to `.env.local`

```env
ARCJET_KEY=
```
3. Rename `.env.local.example` to `.env.local` and add your Arcjet key.

4. Start the dev server.

Expand Down
11 changes: 7 additions & 4 deletions examples/nextjs-14-app-dir-rl/app/api/arcjet/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const aj = arcjet({
// See: https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
key: process.env.ARCJET_KEY!,
rules: [
// Fixed window rate limit. Arcjet also supports sliding window and token
// bucket.
fixedWindow({
mode: "LIVE",
characteristics: ["ip.src"],
window: "1h",
max: 1,
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
// Limiting by ip.src is the default if not specified
//characteristics: ["ip.src"],
window: "1m", // 1 min fixed window
max: 1, // allow a single request (for demo purposes)
}),
],
});
Expand Down
4 changes: 2 additions & 2 deletions examples/nextjs-14-app-dir-rl/app/api/custom_timeout/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const aj = arcjet({
key: process.env.ARCJET_KEY!,
rules: [
validateEmail({
mode: "LIVE",
block: ["NO_MX_RECORDS"],
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
block: ["NO_MX_RECORDS"], // block email addresses with no MX records
}),
],
client,
Expand Down
5 changes: 5 additions & 0 deletions examples/nextjs-14-app-dir-rl/environment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare namespace NodeJS {
export interface ProcessEnv {
readonly ARCJET_KEY: string;
}
}
120 changes: 52 additions & 68 deletions examples/nextjs-14-app-dir-rl/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 915d3fc

Please sign in to comment.