Skip to content

Commit

Permalink
fix(chore): fix sonar code smells (#105)
Browse files Browse the repository at this point in the history
* fix(chore): fixing sonar smells

resolving sonar smells to improve quality gate

GH-94

* fix(chore): fix sonar code smells

Resolving sonar smells to improve quality gate

GH-94

---------

Co-authored-by: Raghav <Raghav.arora@sourcefuse.com>
  • Loading branch information
arpit1503khanna and RaghavaroraSF committed May 17, 2023
1 parent 8a00196 commit 0158612
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
57 changes: 28 additions & 29 deletions src/providers/ratelimit-datasource.provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {CoreBindings, inject, Provider} from '@loopback/core';
import {Getter} from '@loopback/repository';
import {Getter, juggler} from '@loopback/repository';
import {RateLimitMetadata, RateLimitOptions, Store} from '../types';
import {RateLimitSecurityBindings} from '../keys';
import RedisStore, {RedisReply} from 'rate-limit-redis';
import MemcachedStore from 'rate-limit-memcached';
import MongoStore from 'rate-limit-mongo';
import {juggler} from '@loopback/repository';
import {HttpErrors, RestApplication} from '@loopback/rest';
import {TextDecoder} from 'util';
const decoder = new TextDecoder('utf-8');
Expand All @@ -26,47 +25,47 @@ export class RatelimitDatasourceProvider implements Provider<Store> {
async action(): Promise<Store> {
const metadata: RateLimitMetadata = await this.getMetadata();

// First check if rate limit options available at method level
const operationMetadata = metadata ? metadata.options : {};

// // First check if rate limit options available at method level
const operationMetadata = metadata?.options ?? {};
// Create options based on global config and method level config
const opts = Object.assign({}, this.config, operationMetadata);
const opts = {...this.config, ...operationMetadata};
const var1 = 60;
const var2 = 1000;

if (this.config?.type === 'MemcachedStore') {
return new MemcachedStore({
client: this.config?.client,
expiration: (opts.windowMs ?? 60 * 1000) / 1000,
});
const expiration = (opts.windowMs ?? var1 * var2) / var2;
return new MemcachedStore({client: this.config?.client, expiration});
} else if (this.config?.type === 'MongoStore') {
const expireTimeMs = (opts.windowMs ?? var1 * var2) / var2;
return new MongoStore({
uri: this.config?.uri,
collectionName: this.config?.collectionName,
expireTimeMs: (opts.windowMs ?? 60 * 1000) / 1000,
expireTimeMs,
});
} else {
const redisDS = (await this.application.get(
`datasources.${this.config?.name}`,
)) as juggler.DataSource;
if (redisDS?.connector) {
return new RedisStore({
sendCommand: async (...args: string[]) => {
const command = `${args[0]}`;
args.splice(0, 1);
let res;
try {
res = await this.executeRedisCommand(redisDS, command, args);
if (command.toLocaleLowerCase() === 'script') {
res = decoder.decode(res as ArrayBuffer);
}
} catch (err) {
throw new Error(`Could not execute redis command ${err}`);
}
return res as RedisReply;
},
});
} else {
if (!redisDS?.connector) {
throw new HttpErrors.InternalServerError('Invalid Datasource');
}

return new RedisStore({
sendCommand: async (...args: string[]) => {
const command = `${args[0]}`;
args.splice(0, 1);
let res;
try {
res = await this.executeRedisCommand(redisDS, command, args);
if (command.toLocaleLowerCase() === 'script') {
res = decoder.decode(res as ArrayBuffer);
}
} catch (err) {
throw new Error(`Could not execute redis command ${err}`);
}
return res as RedisReply;
},
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type RedisClientType = IORedis.Redis | RedisClient;

export interface DataSourceConfig {
name: string;
client?: string | RedisClientType | undefined;
client?: string | RedisClientType;
type?: string;
uri?: string;
collectionName?: string;
Expand Down

0 comments on commit 0158612

Please sign in to comment.