Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kitql/client

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kitql/client - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

5

index.d.ts

@@ -1,2 +0,3 @@

export { defaultStoreValue, RequestFrom, KitQLClient, RequestStatus } from './kitQLClient';
export type { RequestParameters, RequestResult } from './kitQLClient';
export { isLoading } from './lib/toExport';
export { defaultStoreValue, KitQLClient, RequestFrom, RequestStatus } from './lib/toExport';
export type { RequestParameters, RequestResult } from './lib/toExport';

122

index.js

@@ -5,5 +5,68 @@ 'use strict';

const helper = require('@kitql/helper');
const graphql = require('graphql');
const helper = require('@kitql/helper');
const safeStableStringify = require('safe-stable-stringify');
/**
* Indexes
* (KEY1) : [VAR1, VAR2, VAR3]
*
* Data
* (KEY1 : VAR1) : DATA1
* (KEY1 : VAR2) : DATA2
* (KEY1 : VAR3) : DATA3
*/
class CacheData {
constructor() {
this.cacheIndexes = {};
this.cacheData = {};
}
set(operationKey, data) {
const v = safeStableStringify.stringify(data.variables);
const fullKey = JSON.stringify({ k: operationKey, v });
// Indexes
if (this.cacheIndexes[operationKey] !== undefined) {
if (this.cacheData[fullKey] === undefined) {
this.cacheIndexes[operationKey].push(v);
}
}
else {
this.cacheIndexes[operationKey] = [v];
}
// Data
this.cacheData[fullKey] = data;
}
get(operationKey, variables = null) {
//Data
const v = safeStableStringify.stringify(variables);
const fullKey = safeStableStringify.stringify({ k: operationKey, v });
return this.cacheData[fullKey];
}
remove(operationKey, variables = null, allOperationKey = true) {
let nbDeleted = 0;
if (this.cacheIndexes[operationKey] !== undefined) {
if (allOperationKey) {
const keys = this.cacheIndexes[operationKey];
for (let i = 0; i < keys.length; i++) {
const v = keys[i];
const fullKey = safeStableStringify.stringify({ k: operationKey, v });
delete this.cacheData[fullKey];
nbDeleted++;
}
delete this.cacheIndexes[operationKey];
}
else {
const v = safeStableStringify.stringify(variables);
const fullKey = safeStableStringify.stringify({ k: operationKey, v });
if (this.cacheData[fullKey] !== undefined) {
delete this.cacheData[fullKey];
this.cacheIndexes[operationKey] = this.cacheIndexes[operationKey].filter((c) => c !== v);
nbDeleted = 1;
}
}
}
return nbDeleted;
}
}
(function (RequestStatus) {

@@ -31,3 +94,2 @@ RequestStatus["NEVER"] = "NEVER";

var _a, _b;
this.cacheData = {};
const { url, defaultCache, credentials, headers } = options !== null && options !== void 0 ? options : {};

@@ -41,2 +103,3 @@ this.url = url;

this.log = new helper.Log('KitQL Client', { withTime: false });
this.cacheData = new CacheData();
}

@@ -52,4 +115,3 @@ logOperation(browser, from, operation, variables = null) {

async request({ skFetch, document, variables, cacheKey, cache, browser }) {
//Cache key... Relys on the order of the variables :s
const key = JSON.stringify({ cacheKey, variables });
// Logging variables
const browserAndWantLog = browser && this.logType.includes('client');

@@ -63,13 +125,16 @@ const serverAndWantLog = !browser && this.logType.includes('server');

// Check the cache
if (cache !== 0 && this.cacheData[key] !== undefined) {
const xMs = new Date().getTime() - this.cacheData[key].date;
// cache time of the query or of the default config
if (xMs < (cache !== null && cache !== void 0 ? cache : this.cache)) {
if (logOpVar) {
this.logOperation(browser, exports.RequestFrom.CACHE, cacheKey, JSON.stringify(variables));
if (cache !== 0) {
const cachedData = this.cacheData.get(cacheKey, variables);
if (cachedData !== undefined) {
const xMs = new Date().getTime() - cachedData.date;
// cache time of the query or of the default config
if (xMs < (cache !== null && cache !== void 0 ? cache : this.cache)) {
if (logOpVar) {
this.logOperation(browser, exports.RequestFrom.CACHE, cacheKey, safeStableStringify.stringify(variables));
}
else if (logOp) {
this.logOperation(browser, exports.RequestFrom.CACHE, cacheKey);
}
return { ...cachedData, from: exports.RequestFrom.CACHE };
}
else if (logOp) {
this.logOperation(browser, exports.RequestFrom.CACHE, cacheKey);
}
return { ...this.cacheData[key], from: exports.RequestFrom.CACHE };
}

@@ -110,3 +175,3 @@ }

if (logOpVar) {
this.logOperation(browser, dateToReturn.from, cacheKey, JSON.stringify(variables));
this.logOperation(browser, dateToReturn.from, cacheKey, safeStableStringify.stringify(variables));
}

@@ -127,3 +192,3 @@ else if (logOp) {

if (logRawResult) {
this.log.info(`${helper.logCyan('dataJson:')} ` + `${JSON.stringify(dataJson)}`);
this.log.info(`${helper.logCyan('dataJson:')} ` + `${safeStableStringify.stringify(dataJson)}`);
}

@@ -135,3 +200,6 @@ if (dataJson.errors) {

dateToReturn.data = dataJson.data;
this.cacheData[key] = dateToReturn;
// No caching in the server for now! (Need to have a session identification to not mix things up)
if (browser) {
this.cacheData.set(cacheKey, dateToReturn);
}
return dateToReturn;

@@ -144,5 +212,25 @@ }

}
cacheRemove(operationKey, params) {
const nbDeleted = this.cacheData.remove(operationKey, params.variables, params.allOperationKey);
if (this.logType.includes('client')) {
this.log.info(`${helper.logCyan('ResetCache:')} ${helper.logGreen(nbDeleted.toString())}, ` +
`${helper.logCyan('Operation:')} ${helper.logGreen(operationKey)}`);
}
}
}
/**
* Check if on of the stores is in loading state
* @param stores one or more stores to check
* @returns true if at least 1 store is in loading state
*/
function isLoading(stores) {
if (Array.isArray(stores)) {
return stores.some((store) => store.status === exports.RequestStatus.LOADING);
}
return stores.status === exports.RequestStatus.LOADING;
}
exports.KitQLClient = KitQLClient;
exports.defaultStoreValue = defaultStoreValue;
exports.isLoading = isLoading;
{
"name": "@kitql/client",
"version": "0.1.4",
"version": "0.1.5",
"dependencies": {
"@kitql/helper": "0.1.3",
"graphql": "16.3.0"
"graphql": "16.3.0",
"safe-stable-stringify": "2.3.1"
},

@@ -8,0 +9,0 @@ "repository": {

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