Socket
Socket
Sign inDemoInstall

@quantos/micro-request

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@quantos/micro-request - npm Package Compare versions

Comparing version 1.0.9 to 1.1.0

.github/workflows/publish_npm.yml

4

dist/redis_cache.d.ts

@@ -5,3 +5,5 @@ import { Entity } from './interfaces/entity';

static getMany<Y, T extends Entity<Y>>(ids: Y[], cachePrefix?: string): Promise<T[]>;
static set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string): Promise<void>;
static set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string, options?: {
ttl?: number;
}): Promise<void>;
}

@@ -11,18 +11,16 @@ "use strict";

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisCache = void 0;
const redis_helper_1 = require("@quantos/redis-helper");
const redis_1 = __importDefault(require("redis"));
const redis_1 = require("redis");
let redisClient;
let _redisEnabled = false;
if (process.env.REDIS_PORT && process.env.REDIS_HOST) {
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT);
redisClient = redis_1.default.createClient(Number.parseInt(process.env.REDIS_PORT), process.env.REDIS_HOST, { enable_offline_queue: false });
redisClient.on('ready', () => {
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_URL);
redisClient = (0, redis_1.createClient)({ url: process.env.REDIS_URL });
redisClient
.connect()
.then(() => {
_redisEnabled = true;
});
redisClient.on('error', (error) => {
})
.catch((error) => {
console.error('[@quantos/micro-request][Init redis client] ' + error);

@@ -38,5 +36,5 @@ _redisEnabled = false;

}
const cache = new redis_helper_1.RedisValueHelper({ client: redisClient, prefix: cachePrefix, disableListeners: true });
const cached = yield cache.getCached(`${id}`);
if (cached) {
const data = yield redisClient.get(`${cachePrefix}:${id}`);
if (data && data.length) {
const cached = JSON.parse(data);
return cached;

@@ -51,10 +49,13 @@ }

}
const cache = new redis_helper_1.RedisValueHelper({ client: redisClient, prefix: cachePrefix, disableListeners: true });
const cacheds = yield cache.getCacheds(ids.map(String));
if (cacheds) {
return cacheds;
const cacheds = [];
for (const key of ids) {
const cached = yield this.get(key);
if (cached) {
cacheds.push(cached);
}
}
return cacheds;
});
}
static set(id, data, cachePrefix) {
static set(id, data, cachePrefix, options) {
return __awaiter(this, void 0, void 0, function* () {

@@ -64,4 +65,7 @@ if (!_redisEnabled) {

}
const cache = new redis_helper_1.RedisValueHelper({ client: redisClient, prefix: cachePrefix, disableListeners: true });
yield cache.setCached(`${id}`, data);
try {
yield redisClient.set(`${cachePrefix}:${id}`, JSON.stringify(data), { EX: options === null || options === void 0 ? void 0 : options.ttl });
}
catch (e) {
}
});

@@ -68,0 +72,0 @@ }

{
"name": "@quantos/micro-request",
"version": "1.0.9",
"version": "1.1.0",
"description": "Quantos http client with cache, retry and fallback mechanism",

@@ -12,11 +12,11 @@ "author": "fabriziotognetto <info@quantos.it>",

"devDependencies": {
"@types/redis": "^2.8.28",
"ts-loader": "^5.3.3",
"@types/redis": "^4.0.11",
"ts-loader": "^9.4.2",
"tslint": "^6.1.3",
"typescript": "^4.0.5"
"typescript": "^4.9.5"
},
"dependencies": {
"@quantos/redis-helper": "^1.0.10",
"axios": "^0.21.1"
"redis": "^4.6.5",
"axios": "^1.3.4"
}
}

@@ -1,43 +0,58 @@

import { RedisValueHelper } from '@quantos/redis-helper';
import redis from 'redis';
import { createClient, RedisClientType } from 'redis';
import { Entity } from './interfaces/entity';
/** Init a redis client */
let redisClient: redis.RedisClient;
let redisClient: RedisClientType;
let _redisEnabled = false;
if (process.env.REDIS_PORT && process.env.REDIS_HOST) {
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT);
redisClient = redis.createClient(Number.parseInt(process.env.REDIS_PORT), process.env.REDIS_HOST, {enable_offline_queue: false});
redisClient.on('ready', () => {
_redisEnabled = true;
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_URL);
redisClient = createClient({ url: process.env.REDIS_URL });
redisClient
.connect()
.then(() => {
_redisEnabled = true;
})
.catch((error: any) => {
console.error('[@quantos/micro-request][Init redis client] ' + error);
_redisEnabled = false;
});
redisClient.on('error', (error: any) => {
console.error('[@quantos/micro-request][Init redis client] ' + error);
_redisEnabled = false;
});
}
export class RedisCache {
static async get<T>(id: any, cachePrefix?: string): Promise<T | null> {
if (!_redisEnabled) { return null; }
const cache = new RedisValueHelper<T>({ client: redisClient, prefix: cachePrefix, disableListeners: true });
const cached = await cache.getCached(`${id}`);
if (cached) { return cached; }
static async get<T>(id: any, cachePrefix?: string): Promise<T | null> {
if (!_redisEnabled) {
return null;
}
const data = await redisClient.get(`${cachePrefix}:${id}`);
if (data && data.length) {
const cached: T = JSON.parse(data);
return cached;
}
}
static async getMany<Y, T extends Entity<Y>>(ids: Y[], cachePrefix?: string): Promise<T[]> {
if (!_redisEnabled) { return null; }
const cache = new RedisValueHelper<T>({ client: redisClient, prefix: cachePrefix, disableListeners: true });
const cacheds = await cache.getCacheds(ids.map(String));
if (cacheds) { return cacheds; }
static async getMany<Y, T extends Entity<Y>>(ids: Y[], cachePrefix?: string): Promise<T[]> {
if (!_redisEnabled) {
return null;
}
const cacheds: T[] = [];
for (const key of ids) {
const cached = await this.get<T>(key);
if (cached) {
cacheds.push(cached);
}
}
return cacheds;
}
static async set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string): Promise<void> {
if (!_redisEnabled) { return; }
const cache = new RedisValueHelper<T>({ client: redisClient, prefix: cachePrefix, disableListeners: true });
await cache.setCached(`${id}`, data);
static async set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string, options?: { ttl?: number }): Promise<void> {
if (!_redisEnabled) {
return;
}
try {
await redisClient.set(`${cachePrefix}:${id}`, JSON.stringify(data), { EX: options?.ttl });
} catch (e) {
// do nothing
}
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc