@kitql/client
Advanced tools
Comparing version 0.2.4 to 0.2.5
133
index.js
@@ -7,2 +7,55 @@ 'use strict'; | ||
class InMemoryCache { | ||
constructor() { | ||
this.cacheIndexes = {}; | ||
this.cacheData = {}; | ||
} | ||
set(operationKey, data) { | ||
const v = helper.stry(data.variables, 0); | ||
const fullKey = helper.stry({ k: operationKey, v }, 0); | ||
// 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 = helper.stry(variables, 0); | ||
const fullKey = helper.stry({ k: operationKey, v }, 0); | ||
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 = helper.stry({ k: operationKey, v }, 0); | ||
delete this.cacheData[fullKey]; | ||
nbDeleted++; | ||
} | ||
delete this.cacheIndexes[operationKey]; | ||
} | ||
else { | ||
const v = helper.stry(variables, 0); | ||
const fullKey = helper.stry({ k: operationKey, v }, 0); | ||
if (this.cacheData[fullKey] !== undefined) { | ||
delete this.cacheData[fullKey]; | ||
this.cacheIndexes[operationKey] = this.cacheIndexes[operationKey].filter((c) => c !== v); | ||
nbDeleted = 1; | ||
} | ||
} | ||
} | ||
return nbDeleted; | ||
} | ||
} | ||
function printBlockString(n) { | ||
@@ -107,65 +160,2 @@ return '"""\n' + JSON.stringify(n).slice(1, -1) + '\n"""'; | ||
// Next step: IndexedDB? | ||
/** | ||
* 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 = helper.stry(data.variables, 0); | ||
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 = helper.stry(variables, 0); | ||
const fullKey = helper.stry({ k: operationKey, v }, 0); | ||
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 = helper.stry({ k: operationKey, v }, 0); | ||
delete this.cacheData[fullKey]; | ||
nbDeleted++; | ||
} | ||
delete this.cacheIndexes[operationKey]; | ||
} | ||
else { | ||
const v = helper.stry(variables, 0); | ||
const fullKey = helper.stry({ k: operationKey, v }, 0); | ||
if (this.cacheData[fullKey] !== undefined) { | ||
delete this.cacheData[fullKey]; | ||
this.cacheIndexes[operationKey] = this.cacheIndexes[operationKey].filter((c) => c !== v); | ||
nbDeleted = 1; | ||
} | ||
} | ||
} | ||
return nbDeleted; | ||
} | ||
} | ||
// '_$id(2)' or '[]$add(2)' or '[]$add' | ||
@@ -316,3 +306,3 @@ function extractKeyValue(str) { | ||
constructor(options) { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
const { url, defaultCache, credentials, headers, defaultPolicy } = options !== null && options !== void 0 ? options : {}; | ||
@@ -327,3 +317,3 @@ this.defaultPolicy = defaultPolicy !== null && defaultPolicy !== void 0 ? defaultPolicy : 'cache-first'; | ||
this.log = new helper.Log('KitQL Client'); | ||
this.cacheData = new CacheData(); | ||
this.cacheData = (_c = options.cacheImplementation) !== null && _c !== void 0 ? _c : new InMemoryCache(); | ||
} | ||
@@ -375,4 +365,13 @@ logOperation(from, operation, variables = null) { | ||
this.log.error(`I think that either:` + | ||
`\n\t\t1/ you forgot to provide \`fetch\`! As we are in SSR & include here. > ${cacheKey}({ fetch: ??? })` + | ||
`\n\t\t2/ you should run this in a browser only.`); | ||
`\n\t${helper.logRed(`1/`)} you forgot to provide \`${helper.logYellow(`fetch`)}\`! As we are in SSR & include here. ` + | ||
`\n\t It should be something like:` + | ||
`\n` + | ||
`\n\t<script context="module" lang="ts">` + | ||
`\n\t export async function load({ ${helper.logYellow(`fetch`)} }) {` + | ||
`\n\t await ${helper.logCyan(cacheKey)}.query({ ${helper.logYellow(`fetch`)}, variables: { ... } });` + | ||
`\n\t return {};` + | ||
`\n\t }` + | ||
`\n\t</script>` + | ||
`\n` + | ||
`\n\t${helper.logRed(`2/`)} you should run this in a browser only.`); | ||
} | ||
@@ -379,0 +378,0 @@ const fetchToUse = skFetch ? skFetch : fetch; |
@@ -0,3 +1,5 @@ | ||
export { InMemoryCache, LocalStorageCache } from './cache'; | ||
export type { ICacheData } from './cache'; | ||
export { isLoading } from './isLoading'; | ||
export { defaultStoreValue, KitQLClient, RequestFrom, RequestStatus } from './kitQLClient'; | ||
export type { RequestParameters, RequestResult } from './kitQLClient'; |
@@ -0,1 +1,2 @@ | ||
import type { ICacheData } from './cache/ICacheData'; | ||
export declare type ClientSettings = { | ||
@@ -25,3 +26,3 @@ /** | ||
/** | ||
* Default to `/graphql+json`. But if your server is a bit legacy, you can go back to `/json` | ||
* @Default to `/graphql+json`. But if your server is a bit legacy, you can go back to `/json` | ||
*/ | ||
@@ -33,2 +34,7 @@ headersContentType?: 'application/graphql+json' | 'application/json'; | ||
logType?: ('server' | 'client' | 'operation' | 'operationAndvariables' | 'rawResult')[]; | ||
/** | ||
* @Default InMemory that mean a cache in a variable | ||
* @description You can provide any implementation of the CacheData interface, it can store the cache in any place | ||
*/ | ||
cacheImplementation?: ICacheData; | ||
}; | ||
@@ -35,0 +41,0 @@ export declare type RequestSettings = { |
{ | ||
"name": "@kitql/client", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"dependencies": { | ||
"@kitql/helper": "0.1.6" | ||
}, | ||
"repository": { | ||
@@ -5,0 +8,0 @@ "type": "git", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45722
13
1061
1
+ Added@kitql/helper@0.1.6
+ Added@kitql/helper@0.1.6(transitive)