Skip to content

Commit

Permalink
fix(provider): fix promise intialisation (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
moetezch authored Nov 14, 2024
1 parent 245a982 commit 06d32d1
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/providers/ratelimit-action.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,35 @@ export class RatelimitActionProvider implements Provider<RateLimitAction> {
}

// 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 06d32d1

Please sign in to comment.