Skip to content

Commit

Permalink
Merged.
Browse files Browse the repository at this point in the history
  • Loading branch information
timb-103 committed Aug 10, 2023
1 parent f7dfbfe commit 7b87704
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ By default, this module will add a rate limit to any requests to a `/api` endpoi
- 🛑 Set rate limits per API route
- 🕒 Returns seconds until reset
- ⚡ Takes seconds to setup
- 🧾 Response x-ratelimit headers

## Quick Setup

Expand All @@ -37,6 +38,7 @@ That's it! You can now use Nuxt Rate Limit in your Nuxt app ✨
| name | type | default | description |
| --- | --- | --- | --- |
| `enabled` | `boolean` | `true` | Enabled/disable the rate limit module |
| `enabled` | `boolean` | `true` | Add x-ratelimit headers to response |
| `routes` | `object` | [`{}`](https://github.com/timb-103/nuxt-rate-limit/edit/master/README.md#default-rate-limit) | [Add rate limits per route](https://github.com/timb-103/nuxt-rate-limit/edit/master/README.md#different-limits-per-route) |

## Default Rate Limit
Expand Down
5 changes: 2 additions & 3 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<p>response: {{ helloResponse }}</p>
<button @click="hello()">Send request to /hello</button>
<div class="group" v-if="helloPayload">
<pre v-html="JSON.stringify(helloPayload)" />
<pre v-html="JSON.stringify(helloPayload)"></pre>
</div>
</div>

Expand All @@ -23,7 +23,6 @@
<p>response: {{ goodbyeResponse }}</p>
<button @click="goodbye()">Send request to /goodbye</button>
</div>

</template>

<script setup lang="ts">
Expand All @@ -43,7 +42,7 @@ async function hello() {
limit: response.headers.get('x-ratelimit-limit'),
reset: response.headers.get('x-ratelimit-reset'),
}
helloResponse.value = response._data
helloResponse.value = response._data || ''
} catch (error: any) {
helloResponse.value = error.statusMessage
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/middleware/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineEventHandler, createError, setHeader, getRequestURL } from 'h3'
import { defineEventHandler, createError, setHeader } from 'h3'
import { getRateLimitPayload} from '../utils/rate-limit'
import { useRuntimeConfig } from '#imports'

Expand Down
16 changes: 12 additions & 4 deletions src/runtime/server/utils/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import {H3Event, getRequestHeader, getRequestURL} from 'h3'
import { H3Event, getRequestHeader } from 'h3'
import { getRouteRules } from '#imports'
import type { RateLimit } from '../../../types'
import {RouteRateLimitOptions} from "../../../types";
import { RouteRateLimit } from '../../../types'

interface RateLimitResponse {
limited: boolean
limit: number
current: number
secondsUntilReset: number
}

// store rate limits for each IP address and URL
const rateLimit: RateLimit = {}
Expand All @@ -13,12 +20,13 @@ const rateLimit: RateLimit = {}
*
* @param event
*/
export function getRateLimitPayload(event: H3Event) : false | { limited: boolean; limit: number; current: number; secondsUntilReset: number } {
export function getRateLimitPayload(event: H3Event): false | RateLimitResponse {
const routeRules = getRouteRules(event)
if (!routeRules['nuxt-rate-limit']) {
return false
}
const { maxRequests, intervalSeconds, route } = routeRules['nuxt-rate-limit'] as RouteRateLimitOptions

const { maxRequests, intervalSeconds, route }: RouteRateLimit = routeRules['nuxt-rate-limit']
const intervalMs = intervalSeconds * 1000
const ip = getIP(event)

Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export interface RouteRateLimit {
maxRequests: number
intervalSeconds: number
route: string
}

export interface RouteRateLimitOptions {
maxRequests: number
intervalSeconds: number
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "./playground/.nuxt/tsconfig.json",
"extends": "./playground/.nuxt/tsconfig.json"
}

0 comments on commit 7b87704

Please sign in to comment.