Skip to content
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

Draft
wants to merge 33 commits into
base: redwood
Choose a base branch
from

Conversation

dthyresson
Copy link

@dthyresson dthyresson commented May 4, 2024

What does this PR do?

Adds withUnkey RedwoodJS middleware to rate limit requests.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How should this be tested?

Tests are written in vitest:

Run via:

pnpm test

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs -- not yet :)
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • Updated the Unkey Docs if changes were necessary

Copy link

changeset-bot bot commented May 4, 2024

⚠️ No Changeset found

Latest commit: 550c271

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented May 4, 2024

@dthyresson is attempting to deploy a commit to the Unkey Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

github-actions bot commented May 4, 2024

Thank you for following the naming conventions for pull request titles! 🙏

@dthyresson dthyresson marked this pull request as draft May 4, 2024 12:55
export async function doTheMagicHere() {
const _unkey = new Unkey({
rootKey: "GET_THIS_FROM_ENV_SOMEHOW",
wrapperSdkVersion: `@unkey/redwoodjs@${version}`,
Copy link
Author

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";
Copy link
Author

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) => {
Copy link
Author

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.

@@ -0,0 +1,6 @@
/// <reference types="next" />
Copy link
Author

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.

@dthyresson dthyresson changed the title (wip): RedwoodJS Middleware to rate limit requests feat: RedwoodJS Middleware to rate limit requests May 4, 2024
@@ -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. */,
Copy link
Author

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";
Copy link
Author

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.

@dthyresson dthyresson marked this pull request as ready for review May 8, 2024 16:17
@dthyresson dthyresson marked this pull request as draft May 10, 2024 21:55
@dthyresson
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants