sourcefuse / loopback4-ratelimiter

A Rate Limiting Extension for Loopback 4 Applications
https://www.npmjs.com/package/loopback4-ratelimiter
MIT License
37 stars 10 forks source link

datasource not found error #116

Closed islam3rafah closed 1 year ago

islam3rafah commented 1 year ago

I am using the following configuration in application.ts but I am getting that The key 'datasources.undefined' is not bound to any value in context:

this.bind(RateLimitSecurityBindings.CONFIG).to({
  RatelimitActionMiddleware: true,
  name: 'redis',
  type: 'RedisStore',
  windowMs: 10 * 1000,
  max: 3,
  message: "limit reached",
  standardHeaders: true
});
this.component(RateLimiterComponent as any);
shubhamp-sf commented 1 year ago

Hi @islam3rafah, Do you have the corresponding datasource defined with the dataSourceName property set to redis?

islam3rafah commented 1 year ago

image

shubhamp-sf commented 1 year ago

Can you share the @loopback/core's package version you're using?

Also the RatelimitActionMiddleware: true, should be bound to RateLimitSecurityBindings.RATELIMITCONFIG as in your config it's mixed with general config.

islam3rafah commented 1 year ago
shubhamp-sf commented 1 year ago

Seems the LoopBack version you're using is quite old. Can you try using v4.1.2 of the loopback4-ratelimiter along with your current LoopBack version or update LoopBack versions (See https://loopback.io/doc/en/lb4/Update-generator.html) to use the latest release of this extension and see if that solves your issue.

If it persists please help me by providing a minimal reproduction of the issue on https://codesandbox.io/.

islam3rafah commented 1 year ago

sample project ratelimiter datasource issue.zip

islam3rafah commented 1 year ago

I really appreciate your support and consideration. this is a sample minimal project that still has the same problem after using new version of loopback core and the ratelimiter. the only rest api is the ping api.

shubhamp-sf commented 1 year ago

Thanks for providing the reproduction @islam3rafah, the quick (but not-recommended) workaround would be to move the binding below the this.component statement. Due to the config options being set to null in the existing extension code which makes it inaccessible to the actual handlers.

I've raised a PR #117 fixing the same and can be expected to be merged and release real soon.

Thanks for raising the issue.

shubhamp-sf commented 1 year ago

This issue has been resolved in version 5.0.1 🎉

islam3rafah commented 1 year ago

Thank you so much, I tried 5.0.1 and it worked fine