Skip to content

Commit

Permalink
feat(providers): add option to apply ratelimiting over single API
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbhi-sharma1 committed Feb 15, 2023
1 parent 2f60f35 commit 6f25cd4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ For redis datasource, you have to pass the name of a loopback4 datasource
```ts
this.bind(RateLimitSecurityBindings.CONFIG).to({
name: 'redis',
type:'RedisStore'
type: 'RedisStore',
enabledByDefault: true,
});
```


For memcache datasource

```ts
this.bind(RateLimitSecurityBindings.CONFIG).to({
client: memoryClient,
type:'MemcachedStore'
type: 'MemcachedStore',
});
```

Expand Down Expand Up @@ -72,9 +72,19 @@ this.bind(RateLimitSecurityBindings.CONFIG).to({
type: 'RedisStore',
max: 60,
keyGenerator: rateLimitKeyGen,
enabledByDefault:true
});
```

## EnabledbyDefault

enabledByDefault option in Config Binding will provide a configurable mode.
When its enabled (default value is true),it will provide a way to
ratelimit all API's except a few that are disabled using a decorator.

When its disabled (enabledByDefault is set to false) ,ratelimiting will be
disabled for all APIs except those that are enabled using the decorator.

- The component exposes a sequence action which can be added to your server sequence class. Adding this will trigger ratelimiter middleware for all the requests passing through.

```ts
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/acceptance/fixtures/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class TestApplication extends BootMixin(
name: 'inMemory',
max: 5,
windowMs: 2000,
enabledByDefault: true,
});
}
}
3 changes: 3 additions & 0 deletions src/__tests__/unit/ratelimit-datasource.provider.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Rate Limit datasource Service', () => {
const config: RateLimitOptions = {
name: 'test_name',
type: 'MemcachedStore',
enabledByDefault: true,
};
const ratelimitDatasourceProvider = new RatelimitDatasourceProvider(
() => {
Expand All @@ -34,6 +35,7 @@ describe('Rate Limit datasource Service', () => {
type: 'MongoStore',
uri: 'test_uri',
collectionName: 'test_collection_name',
enabledByDefault: true,
};
const ratelimitDatasourceProvider = new RatelimitDatasourceProvider(
() => {
Expand All @@ -54,6 +56,7 @@ describe('Rate Limit datasource Service', () => {
it('returns undefined if there is no redisDS', async () => {
const config: RateLimitOptions = {
name: 'test_name',
enabledByDefault: true,
};
const ratelimitDatasourceProvider = await new RatelimitDatasourceProvider(
() => {
Expand Down
12 changes: 11 additions & 1 deletion src/providers/ratelimit-action.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ export class RatelimitActionProvider implements Provider<RateLimitAction> {
resolve();
});
});
await promise;
if (this.config?.enabledByDefault === true) {
await promise;
} else if (
this.config?.enabledByDefault === false &&
metadata &&
metadata.enabled
) {
await promise;
} else {
return Promise.resolve();
}
}
}
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ export interface DataSourceConfig {
uri?: string;
collectionName?: string;
}

export interface RateLimitConfig {
enabledByDefault: boolean;
}
export interface RateLimitAction {
(request: Request, response: Response): Promise<void>;
}

export type RateLimitOptions = Writable<Partial<Options>> & DataSourceConfig;
export type RateLimitOptions = Writable<Partial<Options>> &
DataSourceConfig &
RateLimitConfig;

/**
* Rate limit metadata interface for the method decorator
Expand Down

0 comments on commit 6f25cd4

Please sign in to comment.