Skip to content

Commit efb3d6f

Browse files
committed
refactor: biome
1 parent 1079e8e commit efb3d6f

12 files changed

+71
-86
lines changed

biome.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
33
"vcs": { "enabled": false, "clientKind": "git", "useIgnoreFile": false },
4-
"files": { "ignoreUnknown": false, "ignore": [] },
4+
"files": { "ignoreUnknown": false, "ignore": ["**/dist/**"] },
55
"formatter": {
66
"enabled": true,
77
"useEditorconfig": true,

example/basic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Elysia } from 'elysia'
21
import { swagger } from '@elysiajs/swagger'
2+
import { Elysia } from 'elysia'
33

44
import { rateLimit } from '../src'
55

example/multiInstance.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Elysia } from 'elysia'
21
import { swagger } from '@elysiajs/swagger'
2+
import { Elysia } from 'elysia'
33
import { ip } from 'elysia-ip' // just a glitch pls ignore this
44

55
import { rateLimit } from '../src'

example/multiInstanceInjected.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Elysia } from 'elysia'
21
import { swagger } from '@elysiajs/swagger'
2+
import { Elysia } from 'elysia'
33

44
import { rateLimit } from '../src'
55

6-
import type { Options } from '../src'
76
import type { Server } from 'bun'
7+
import type { Options } from '../src'
88

99
let server: Server | null
1010

package.json

+2-10
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
"type": "git",
1212
"url": "git+https://github.com/rayriffy/elysia-rate-limit.git"
1313
},
14-
"files": [
15-
"dist"
16-
],
14+
"files": ["dist"],
1715
"main": "./dist/cjs/index.js",
1816
"module": "./dist/index.mjs",
1917
"types": "./dist/index.d.ts",
@@ -31,13 +29,7 @@
3129
"dev:inject": "DEBUG=* bun run --hot example/multiInstanceInjected.ts",
3230
"build": "bun run scripts/build.ts"
3331
},
34-
"keywords": [
35-
"elysia",
36-
"rate",
37-
"limit",
38-
"ratelimit",
39-
"rate-limit"
40-
],
32+
"keywords": ["elysia", "rate", "limit", "ratelimit", "rate-limit"],
4133
"publishConfig": {
4234
"access": "public",
4335
"registry": "https://registry.npmjs.org"

scripts/build.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
import { $ } from "bun";
2-
import pc from "picocolors";
3-
import { build, type Options } from "tsup";
1+
import { $ } from 'bun'
2+
import pc from 'picocolors'
3+
import { type Options, build } from 'tsup'
44

5-
await $`rm -rf dist`;
5+
await $`rm -rf dist`
66

77
const tsupConfig: Options = {
8-
entry: ["src/**/*.ts", "!src/**/*.spec.ts"],
8+
entry: ['src/**/*.ts', '!src/**/*.spec.ts'],
99
splitting: false,
1010
sourcemap: false,
1111
clean: true,
1212
bundle: true,
13-
} satisfies Options;
13+
} satisfies Options
1414

1515
await Promise.all([
1616
build({
17-
outDir: "dist",
18-
format: "esm",
19-
target: "node20",
17+
outDir: 'dist',
18+
format: 'esm',
19+
target: 'node20',
2020
cjsInterop: false,
2121
...tsupConfig,
2222
}),
2323
build({
24-
outDir: "dist/cjs",
25-
format: "cjs",
26-
target: "node20",
24+
outDir: 'dist/cjs',
25+
format: 'cjs',
26+
target: 'node20',
2727
...tsupConfig,
2828
}),
29-
]);
29+
])
3030

3131
await $`tsc --project tsconfig.dts.json`.then(() =>
32-
console.log(pc.magenta("DTS"), "⚡ Files generated"),
33-
);
32+
console.log(pc.magenta('DTS'), '⚡ Files generated')
33+
)
3434

35-
await Promise.all([$`cp dist/*.d.ts dist/cjs`]);
35+
await Promise.all([$`cp dist/*.d.ts dist/cjs`])
3636

37-
process.exit();
37+
process.exit()

src/constants/defaultOptions.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'bun:test'
2-
import { defaultOptions } from './defaultOptions'
32
import { defaultKeyGenerator } from '../services/defaultKeyGenerator'
3+
import { defaultOptions } from './defaultOptions'
44

55
describe('defaultOptions', () => {
66
it('should have the expected default values', () => {
@@ -15,13 +15,13 @@ describe('defaultOptions', () => {
1515
skip: expect.any(Function),
1616
})
1717
})
18-
18+
1919
it('should have a skip function that returns false by default', () => {
2020
const mockRequest = {} as Request
2121
expect(defaultOptions.skip(mockRequest)).toBe(false)
2222
})
23-
23+
2424
it('should use defaultKeyGenerator as the generator function', () => {
2525
expect(defaultOptions.generator).toBe(defaultKeyGenerator)
2626
})
27-
})
27+
})

src/services/defaultContext.spec.ts

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,110 @@
1-
import { describe, expect, it, beforeEach, afterEach } from 'bun:test'
2-
import { DefaultContext } from './defaultContext'
3-
import { defaultOptions } from '../constants/defaultOptions'
1+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
42
import type { Options } from '../@types/Options'
3+
import { defaultOptions } from '../constants/defaultOptions'
4+
import { DefaultContext } from './defaultContext'
55

66
describe('DefaultContext', () => {
77
let context: DefaultContext
8-
8+
99
beforeEach(() => {
1010
context = new DefaultContext()
1111
context.init({
1212
...defaultOptions,
1313
})
1414
})
15-
15+
1616
afterEach(async () => {
1717
await context.kill()
1818
})
19-
19+
2020
it('should initialize with default maxSize', () => {
2121
const ctx = new DefaultContext()
2222
expect(ctx).toBeInstanceOf(DefaultContext)
2323
})
24-
24+
2525
it('should initialize with custom maxSize', () => {
2626
const ctx = new DefaultContext(1000)
2727
expect(ctx).toBeInstanceOf(DefaultContext)
2828
})
29-
29+
3030
it('should increment counter for new key', async () => {
3131
const key = 'test-key-1'
3232
const result = await context.increment(key)
33-
33+
3434
expect(result.count).toBe(1)
3535
expect(result.nextReset).toBeInstanceOf(Date)
3636
expect(result.nextReset.getTime()).toBeGreaterThan(Date.now())
3737
})
38-
38+
3939
it('should increment counter for existing key', async () => {
4040
const key = 'test-key-2'
41-
41+
4242
await context.increment(key)
4343
const result = await context.increment(key)
44-
44+
4545
expect(result.count).toBe(2)
4646
})
47-
47+
4848
it('should decrement counter', async () => {
4949
const key = 'test-key-3'
50-
50+
5151
await context.increment(key)
5252
await context.increment(key)
5353
await context.decrement(key)
54-
54+
5555
const result = await context.increment(key)
5656
expect(result.count).toBe(2) // It should be 2 after decrement and new increment
5757
})
58-
58+
5959
it('should reset counter for specific key', async () => {
6060
const key1 = 'test-key-4'
6161
const key2 = 'test-key-5'
62-
62+
6363
await context.increment(key1)
6464
await context.increment(key2)
6565
await context.reset(key1)
66-
66+
6767
const result1 = await context.increment(key1)
6868
expect(result1.count).toBe(1) // Should be reset
69-
69+
7070
const result2 = await context.increment(key2)
7171
expect(result2.count).toBe(2) // Should still be incremented
7272
})
73-
73+
7474
it('should reset all counters', async () => {
7575
const key1 = 'test-key-6'
7676
const key2 = 'test-key-7'
77-
77+
7878
await context.increment(key1)
7979
await context.increment(key2)
8080
await context.reset()
81-
81+
8282
const result1 = await context.increment(key1)
8383
expect(result1.count).toBe(1)
84-
84+
8585
const result2 = await context.increment(key2)
8686
expect(result2.count).toBe(1)
8787
})
88-
88+
8989
it('should handle expired keys correctly', async () => {
9090
// Create context with a very short duration
9191
const shortContext = new DefaultContext()
9292
shortContext.init({
9393
...defaultOptions,
94-
duration: 10
94+
duration: 10,
9595
} as Omit<Options, 'context'>)
96-
96+
9797
const key = 'test-key-8'
98-
98+
9999
await shortContext.increment(key)
100-
100+
101101
// Wait for the timeout to expire
102102
await new Promise<void>(resolve => setTimeout(resolve, 15))
103-
103+
104104
// After expiration, the count should reset
105105
const result = await shortContext.increment(key)
106106
expect(result.count).toBe(1)
107-
107+
108108
await shortContext.kill()
109109
})
110-
})
110+
})

src/services/defaultContext.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import AllocQuickLRU from '@alloc/quick-lru'
22

3-
import type { Options } from '../@types/Options'
43
import type { Context } from '../@types/Context'
4+
import type { Options } from '../@types/Options'
55
import { logger } from './logger'
66

77
interface Item {
@@ -65,7 +65,7 @@ export class DefaultContext implements Context {
6565
}
6666

6767
public async decrement(key: string) {
68-
let item = this.store.get(key)
68+
const item = this.store.get(key)
6969

7070
// perform actions only if an item is found
7171
if (item !== undefined) {

src/services/plugin.spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { describe, expect, it, mock } from 'bun:test'
22
import { Elysia } from 'elysia'
3-
import { plugin } from './plugin'
4-
import { DefaultContext } from './defaultContext'
53
import type { Options } from '../@types/Options'
4+
import { DefaultContext } from './defaultContext'
5+
import { plugin } from './plugin'
66

77
describe('rate limit plugin', () => {
88
it('should initialize with default options', () => {
99
const app = new Elysia()
1010
const rateLimitPlugin = plugin()
1111
const appWithPlugin = rateLimitPlugin(app)
12-
12+
1313
expect(appWithPlugin).toBeInstanceOf(Elysia)
1414
})
1515

@@ -18,16 +18,16 @@ describe('rate limit plugin', () => {
1818
const customContext = new DefaultContext()
1919
const initSpy = mock((options: Omit<Options, 'context'>) => {})
2020
customContext.init = initSpy
21-
21+
2222
const rateLimitPlugin = plugin({
2323
max: 10,
2424
duration: 60000,
2525
context: customContext,
2626
})
27-
27+
2828
const appWithPlugin = rateLimitPlugin(app)
29-
29+
3030
expect(appWithPlugin).toBeInstanceOf(Elysia)
3131
expect(initSpy).toHaveBeenCalled()
3232
})
33-
})
33+
})

tsconfig.dts.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"rootDir": "./src",
1010
"outDir": "./dist"
1111
},
12-
"include": [
13-
"./src/**/*.ts"
14-
],
15-
"exclude": [
16-
"./src/**/*.spec.ts"
17-
]
12+
"include": ["./src/**/*.ts"],
13+
"exclude": ["./src/**/*.spec.ts"]
1814
}

tsconfig.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
"compilerOptions": {
33
"target": "esnext",
44
"module": "esnext",
5-
"lib": [
6-
"esnext",
7-
"DOM"
8-
],
5+
"lib": ["esnext", "DOM"],
96
"moduleResolution": "bundler",
107
"allowImportingTsExtensions": true,
118
"verbatimModuleSyntax": true,

0 commit comments

Comments
 (0)