-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: RedwoodJS Middleware to rate limit requests #1606
base: redwood
Are you sure you want to change the base?
Conversation
|
@dthyresson is attempting to deploy a commit to the Unkey Team on Vercel. A member of the Team first needs to authorize it. |
|
Thank you for following the naming conventions for pull request titles! 🙏 |
export async function doTheMagicHere() { | ||
const _unkey = new Unkey({ | ||
rootKey: "GET_THIS_FROM_ENV_SOMEHOW", | ||
wrapperSdkVersion: `@unkey/redwoodjs@${version}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chronark I noticed that the rate limit sdk didn't have an option to add the version like the main Unkey api did.
Do you still want to include the telemetry option as well?
import { Ratelimit } from "@unkey/ratelimit"; | ||
import type { RatelimitConfig } from "@unkey/ratelimit"; | ||
|
||
import type { MiddlewareRequest } from "@redwoodjs/vite/dist/middleware"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our RW middleware is still canary+ hence with install from a version 8+ redwood package and need /dist .. this should change in near future.
ratelimitErrorResponseFn?: (req: MiddlewareRequest) => MiddlewareResponse; | ||
}; | ||
|
||
const withUnkey = (options: withUnkeyOptions) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments and console removal to come.
apps/web/next-env.d.ts
Outdated
@@ -0,0 +1,6 @@ | |||
/// <reference types="next" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chronark I didn't realize this got committed. I'll remove.
Accidentally added
@@ -25,9 +23,9 @@ | |||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ | |||
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ | |||
/* Modules */ | |||
// "module": "CommonJS", /* Specify what module code is generated. */ | |||
"module": "NodeNext" /* Specify what module code is generated. */, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed as redwoodjs middleware is NodeNext.
@@ -0,0 +1,4 @@ | |||
import path from "node:path"; | |||
import { defineConfig } from "vitest/config"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to make this a .mts file otherwise thought was CommonJS and TS was sad.
Want to refactor and rename middleware in anticipation of having api key middleware as well and the need to distinguish— so keeping as draft for now now until I update tomorrow |
What does this PR do?
Adds withUnkey RedwoodJS middleware to rate limit requests.
How should this be tested?
Tests are written in
vitest
:Run via:
pnpm test
Checklist
Required
pnpm build
pnpm fmt
console.logs
-- not yet :)git pull origin main
Appreciated