-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add provider and limiter service
- Loading branch information
1 parent
707803e
commit f1a41d5
Showing
6 changed files
with
194 additions
and
11 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,15 @@ | ||
/* | ||
* @adonisjs/limiter | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
export * as errors from './src/errors.js' | ||
export { Limiter } from './src/limiter.js' | ||
export { LimiterResponse } from './src/response.js' | ||
export { HttpLimiter } from './src/http_limiter.js' | ||
export { LimiterManager } from './src/limiter_manager.js' | ||
export { defineConfig, stores } from './src/define_config.js' |
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,43 @@ | ||
/* | ||
* @adonisjs/limiter | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { configProvider } from '@adonisjs/core' | ||
import { ApplicationService } from '@adonisjs/core/types' | ||
import { RuntimeException } from '@adonisjs/core/exceptions' | ||
|
||
import { LimiterManager } from '../index.js' | ||
import type { LimiterService } from '../src/types.js' | ||
|
||
declare module '@adonisjs/core/types' { | ||
export interface ContainerBindings { | ||
'limiter.manager': LimiterService | ||
} | ||
} | ||
|
||
export default class LimiterProvider { | ||
constructor(protected app: ApplicationService) {} | ||
|
||
register() { | ||
this.app.container.singleton('limiter.manager', async () => { | ||
const limiterConfigProvider = this.app.config.get('limiter', {}) | ||
|
||
/** | ||
* Resolve config from the provider | ||
*/ | ||
const config = await configProvider.resolve<any>(this.app, limiterConfigProvider) | ||
if (!config) { | ||
throw new RuntimeException( | ||
'Invalid "config/limiter.ts" file. Make sure you are using the "defineConfig" method' | ||
) | ||
} | ||
|
||
return new LimiterManager(config) | ||
}) | ||
} | ||
} |
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,23 @@ | ||
/* | ||
* @adonisjs/limiter | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import app from '@adonisjs/core/services/app' | ||
import { LimiterService } from '../src/types.js' | ||
|
||
let limiter: LimiterService | ||
|
||
/** | ||
* Returns a singleton instance of the LimiterManager class from the | ||
* container. | ||
*/ | ||
await app.booted(async () => { | ||
limiter = await app.container.make('limiter.manager') | ||
}) | ||
|
||
export { limiter as default } |
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,88 @@ | ||
/* | ||
* @adonisjs/limiter | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { test } from '@japa/runner' | ||
import { IgnitorFactory } from '@adonisjs/core/factories' | ||
import type { RedisService } from '@adonisjs/redis/types' | ||
|
||
import { createRedis } from './helpers.js' | ||
import { LimiterManager, defineConfig, stores } from '../index.js' | ||
|
||
const BASE_URL = new URL('./tmp/', import.meta.url) | ||
const IMPORTER = (filePath: string) => { | ||
if (filePath.startsWith('./') || filePath.startsWith('../')) { | ||
return import(new URL(filePath, BASE_URL).href) | ||
} | ||
return import(filePath) | ||
} | ||
|
||
test.group('Limiter provider', () => { | ||
test('register limiter provider', async ({ assert }) => { | ||
const redis = createRedis() as unknown as RedisService | ||
|
||
const ignitor = new IgnitorFactory() | ||
.merge({ | ||
rcFileContents: { | ||
providers: [() => import('../providers/limiter_provider.js')], | ||
}, | ||
}) | ||
.withCoreConfig() | ||
.withCoreProviders() | ||
.merge({ | ||
config: { | ||
limiter: defineConfig({ | ||
default: 'redis', | ||
stores: { | ||
redis: stores.redis({ | ||
connectionName: 'local', | ||
}), | ||
}, | ||
}), | ||
}, | ||
}) | ||
.create(BASE_URL, { | ||
importer: IMPORTER, | ||
}) | ||
|
||
const app = ignitor.createApp('web') | ||
await app.init() | ||
app.container.singleton('redis', () => redis) | ||
await app.boot() | ||
|
||
assert.instanceOf(await app.container.make('limiter.manager'), LimiterManager) | ||
}) | ||
|
||
test('throw error when config is invalid', async () => { | ||
const redis = createRedis() as unknown as RedisService | ||
|
||
const ignitor = new IgnitorFactory() | ||
.merge({ | ||
rcFileContents: { | ||
providers: [() => import('../providers/limiter_provider.js')], | ||
}, | ||
}) | ||
.withCoreConfig() | ||
.withCoreProviders() | ||
.merge({ | ||
config: { | ||
limiter: {}, | ||
}, | ||
}) | ||
.create(BASE_URL, { | ||
importer: IMPORTER, | ||
}) | ||
|
||
const app = ignitor.createApp('web') | ||
await app.init() | ||
app.container.singleton('redis', () => redis) | ||
await app.boot() | ||
|
||
await app.container.make('limiter.manager') | ||
}).throws('Invalid "config/limiter.ts" file. Make sure you are using the "defineConfig" method') | ||
}) |