Skip to content

Commit

Permalink
fix(middleware): fix promise initialisation (#193)
Browse files Browse the repository at this point in the history
fix promise initialisation

GH-181
  • Loading branch information
Surbhi-sharma1 authored Nov 14, 2024
1 parent be67cb3 commit b10a585
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 21 additions & 20 deletions src/middleware/ratelimit.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,35 @@ export class RatelimitMiddlewareProvider implements Provider<Middleware> {
}

// Perform rate limiting now
const promise = new Promise<void>((resolve, reject) => {
// First check if rate limit options available at method level
const operationMetadata = metadata ? metadata.options : {};
const promise = () =>
new Promise<void>((resolve, reject) => {
// First check if rate limit options available at method level
const operationMetadata = metadata ? metadata.options : {};

// Create options based on global config and method level config
const opts = {...this.config, ...operationMetadata};
// Create options based on global config and method level config
const opts = {...this.config, ...operationMetadata};

if (dataStore) {
opts.store = dataStore;
}
if (dataStore) {
opts.store = dataStore;
}

opts.message = new HttpErrors.TooManyRequests(
opts.message?.toString() ?? 'Method rate limit reached !',
);
opts.message = new HttpErrors.TooManyRequests(
opts.message?.toString() ?? 'Method rate limit reached !',
);

const limiter = RateLimit.default(opts);
limiter(request, response, (err: unknown) => {
if (err) {
reject(err);
}
resolve();
const limiter = RateLimit.default(opts);
limiter(request, response, (err: unknown) => {
if (err) {
reject(err);
}
resolve();
});
});
});
if (enabledByDefault === true) {
await promise;
await promise();
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
} else if (enabledByDefault === false && metadata && metadata.enabled) {
await promise;
await promise();
} else {
return Promise.resolve();
}
Expand Down

0 comments on commit b10a585

Please sign in to comment.