-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite errorResponse, and add a way to inject server into plugin (#40)
* - add update all dependencies ( using pnpm ) - update dependencies * - remove cache bun.lockb * - add using server - add getServer type * better getServer ? * change get server to using object store server * - add throw error when rate-limit reached - add type needRequestIP - edit getserver for support only need requestIP function * add getServer Docs * add throwOnError Docs * export default option * fix for #40 (review) * update dependencies * add default options use as global option in example * fix: export variable name as-is * refactor: grouping if condition * refactor: errorResponse * chore: regenerate lockfile * refactor: getServer -> injectServer * docs: changeset * fix: redundant typeof check --------- Co-authored-by: Phumrapee Limpianchop <[email protected]>
- Loading branch information
1 parent
762a4af
commit 17f10e3
Showing
13 changed files
with
208 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"elysia-rate-limit": major | ||
--- | ||
|
||
**BREAKING CHANGES** remove `responseCode`, and `responseMessage` in favor of new `errorResponse` option. please consult with documentation for more details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"elysia-rate-limit": minor | ||
--- | ||
|
||
added `injectServer` option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Elysia } from 'elysia' | ||
import { swagger } from '@elysiajs/swagger' | ||
|
||
import { rateLimit } from '../src' | ||
|
||
import type { Options } from '../src' | ||
import type { Server } from 'bun' | ||
|
||
let server: Server | null | ||
|
||
const options: Partial<Options> = { | ||
scoping: 'local', | ||
duration: 200 * 1000, | ||
injectServer: () => { | ||
return server! | ||
}, | ||
} | ||
|
||
// const keyGenerator: Generator<{ ip: string }> = async (req, server, { ip }) => Bun.hash(JSON.stringify(ip)).toString() | ||
|
||
const aInstance = new Elysia() | ||
.use(rateLimit(options)) | ||
.get('/a', () => 'a') | ||
|
||
const bInstance = new Elysia() | ||
.use(rateLimit(options)) | ||
.get('/b', () => 'b') | ||
|
||
const app = new Elysia() | ||
.use(swagger()) | ||
.use(aInstance) | ||
.use(bInstance) | ||
.get('/', () => 'hello') | ||
.listen(3000, () => { | ||
console.log('🦊 Swagger is active at: http://localhost:3000/swagger') | ||
}) | ||
|
||
server = app.server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import type { Server } from 'bun' | ||
import type { MaybePromise } from 'elysia' | ||
|
||
export type Generator<T extends object = {}> = (equest: Request, server: Server | null, derived: T) => MaybePromise<string> | ||
export type Generator<T extends object = {}> = ( | ||
request: Request, | ||
server: Server | null, | ||
derived: T | ||
) => MaybePromise<string> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.