@kitql/client
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -5,2 +5,2 @@ export { InMemoryCache, LocalStorageCache } from './lib/toExport'; | ||
export { defaultStoreValue, KitQLClient, RequestFrom, RequestStatus } from './lib/toExport'; | ||
export type { Credential, HeaderContentType, LogType, Policy, RequestParameters, RequestQueryParameters, RequestResult } from './lib/toExport'; | ||
export type { Credential, HeaderContentType, LogType, Policy, RequestParameters, RequestQueryParameters, RequestResult, PatchType } from './lib/toExport'; |
187
index.js
@@ -212,124 +212,2 @@ 'use strict'; | ||
// '_$id(2)' or '[]$add(2)' or '[]$add' | ||
function extractKeyValue(str) { | ||
if (str.includes('[]$add')) { | ||
const [key, value] = str.split('[]$add'); | ||
return { | ||
key, | ||
value: value ? extractKeyValue('_$PROPS' + value).value : -1 | ||
}; | ||
} | ||
else if (str.includes('[]$remove(')) { | ||
const [key, value] = str.split('[]$remove('); | ||
return { key, value: value.substring(0, value.length - 1) }; | ||
} | ||
else if (str.includes('[]$filter(')) { | ||
const [key, value] = str.split('[]$filter('); | ||
return { key, value: value.substring(0, value.length - 1) }; | ||
} | ||
else if (str.startsWith('_$')) { | ||
const [key, value] = str.substring(2).split('('); | ||
return { | ||
key, | ||
value: value.substring(0, value.length - 1) | ||
}; | ||
} | ||
return null; | ||
} | ||
function objUpdate(found, obj, newData, xPath) { | ||
// replace directly obj with newData | ||
if (xPath === null) { | ||
return { found: true, obj: newData }; | ||
} | ||
let segments = xPath.split('.'); | ||
let toReturn = { found, obj }; | ||
// Already found! | ||
if (toReturn.found) { | ||
return toReturn; | ||
} | ||
segments.forEach((segment) => { | ||
if (toReturn.found) { | ||
return; | ||
} | ||
if (segments.length === 1) { | ||
if (segment.includes('[]$add')) { | ||
const kvp = extractKeyValue(segment); | ||
let pos = kvp.value === -1 ? obj[kvp.key].length : kvp.value; | ||
let newArray = obj[kvp.key]; | ||
newArray.splice(pos, 0, newData); | ||
toReturn = { found: true, obj: { ...obj, [kvp.key]: newArray } }; | ||
} | ||
else if (segment.includes('[]$remove')) { | ||
const kvp = extractKeyValue(segment); | ||
const kvpElement = extractKeyValue(kvp.value); | ||
let newArray = obj[kvp.key]; | ||
newArray = newArray.filter((c) => { | ||
return c[kvpElement.key].toString() !== kvpElement.value; | ||
}); | ||
const ToRmvFound = newArray.length !== obj[kvp.key].length; | ||
toReturn = { found: ToRmvFound, obj: { ...obj, [kvp.key]: newArray } }; | ||
} | ||
else if (segment.includes('_$')) { | ||
const kvp = extractKeyValue(segment); | ||
if (kvp) { | ||
if (obj[kvp.key] !== undefined && obj[kvp.key].toString() === kvp.value) { | ||
toReturn = { found: true, obj: newData }; | ||
return; | ||
} | ||
} | ||
else { | ||
throw new Error('objUpdate - Invalid segment: ' + segment); | ||
} | ||
} | ||
else { | ||
if (obj && obj[segment] !== undefined) { | ||
// replace in the existing obj the new data | ||
toReturn = { found: true, obj: { ...obj, [segment]: newData } }; | ||
return; | ||
} | ||
} | ||
} | ||
else if (segment.includes('[]')) { | ||
let [propertyName] = segment.split('[]'); | ||
if (obj && obj[propertyName] !== undefined) { | ||
const kvp = segment.includes('[]$filter(') ? extractKeyValue(segment) : null; | ||
const kvpElement = kvp ? extractKeyValue(kvp.value) : null; | ||
let xPathNext = segments.slice(1).join('.'); | ||
obj[propertyName].forEach((arrayItem, i) => { | ||
if (kvpElement) { | ||
if (obj[propertyName][i][kvpElement.key].toString() === kvpElement.value) { | ||
const result = objUpdate(toReturn.found, obj[propertyName][i], newData, xPathNext); | ||
obj[propertyName][i] = result.obj; | ||
toReturn.found = result.found; | ||
if (toReturn.found) { | ||
return; | ||
} | ||
} | ||
} | ||
else { | ||
const result = objUpdate(toReturn.found, obj[propertyName][i], newData, xPathNext); | ||
obj[propertyName][i] = result.obj; | ||
toReturn.found = result.found; | ||
if (toReturn.found) { | ||
return; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
else { | ||
if (obj && obj[segment] !== undefined) { | ||
let xPathNext = segments.slice(1).join('.'); | ||
const result = objUpdate(toReturn.found, obj[segment], newData, xPathNext); | ||
obj[segment] = result.obj; | ||
toReturn.found = result.found; | ||
if (toReturn.found) { | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
return toReturn; | ||
} | ||
(function (RequestStatus) { | ||
@@ -350,2 +228,3 @@ RequestStatus["NEVER"] = "NEVER"; | ||
date: new Date().getTime(), | ||
operationName: '???', | ||
variables: null, | ||
@@ -360,3 +239,3 @@ data: null, | ||
var _a, _b; | ||
const { url, cacheMs, credentials, headers, policy, headersContentType } = options !== null && options !== void 0 ? options : {}; | ||
const { url, cacheMs, credentials, headers, policy, headersContentType, endpointNetworkDelayMs, endpointSSRDelayMs } = options !== null && options !== void 0 ? options : {}; | ||
this.url = url; | ||
@@ -370,2 +249,4 @@ this.policy = policy !== null && policy !== void 0 ? policy : 'cache-first'; | ||
this.cacheData = (_b = options.cacheImplementation) !== null && _b !== void 0 ? _b : new InMemoryCache(); | ||
this.endpointNetworkDelayMs = endpointNetworkDelayMs !== null && endpointNetworkDelayMs !== void 0 ? endpointNetworkDelayMs : 0; | ||
this.endpointSSRDelayMs = endpointSSRDelayMs !== null && endpointSSRDelayMs !== void 0 ? endpointSSRDelayMs : 0; | ||
this.log = new helper.Log('KitQL Client'); | ||
@@ -389,7 +270,7 @@ } | ||
} | ||
requestCache({ variables, cacheKey, cacheMs, browser }) { | ||
requestCache({ variables, operationName, cacheMs, browser }) { | ||
const logStatements = this.getLogsStatements(browser); | ||
// No caching in the server for now! (Need to have a session identification to not mix things up) | ||
if (browser) { | ||
const cachedData = this.cacheData.get(cacheKey, variables); | ||
const cachedData = this.cacheData.get(operationName, variables); | ||
if (cachedData !== undefined) { | ||
@@ -400,6 +281,6 @@ const xMs = new Date().getTime() - cachedData.date; | ||
if (logStatements.logOpVar) { | ||
this.logOperation(exports.RequestFrom.CACHE, cacheKey, helper.stry(variables, 0)); | ||
this.logOperation(exports.RequestFrom.CACHE, operationName, helper.stry(variables, 0)); | ||
} | ||
else if (logStatements.logOp) { | ||
this.logOperation(exports.RequestFrom.CACHE, cacheKey); | ||
this.logOperation(exports.RequestFrom.CACHE, operationName); | ||
} | ||
@@ -415,3 +296,3 @@ return { ...cachedData, from: exports.RequestFrom.CACHE, isOutdated: false }; | ||
} | ||
async request({ skFetch, document, variables, cacheKey, browser }) { | ||
async request({ skFetch, document, variables, operationName, browser }) { | ||
const logStatements = this.getLogsStatements(browser); | ||
@@ -426,3 +307,3 @@ // User help, he is doing wrong | ||
`\n\t export async function load({ ${helper.logYellow(`fetch`)} }) {` + | ||
`\n\t await ${helper.logCyan(cacheKey)}.query({ ${helper.logYellow(`fetch`)}, variables: { ... } });` + | ||
`\n\t await ${helper.logCyan(operationName)}.query({ ${helper.logYellow(`fetch`)}, variables: { ... } });` + | ||
`\n\t return {};` + | ||
@@ -437,2 +318,3 @@ `\n\t }` + | ||
date: new Date().getTime(), | ||
operationName, | ||
variables, | ||
@@ -457,8 +339,12 @@ from: exports.RequestFrom.NETWORK, | ||
dataToReturn.from = exports.RequestFrom.SSR; | ||
await helper.sleep(this.endpointSSRDelayMs); // adding the delay after the request | ||
} | ||
else { | ||
await helper.sleep(this.endpointNetworkDelayMs); // adding the delay after the request | ||
} | ||
if (logStatements.logOpVar) { | ||
this.logOperation(dataToReturn.from, cacheKey, helper.stry(variables, 0)); | ||
this.logOperation(dataToReturn.from, operationName, helper.stry(variables, 0)); | ||
} | ||
else if (logStatements.logOp) { | ||
this.logOperation(dataToReturn.from, cacheKey); | ||
this.logOperation(dataToReturn.from, operationName); | ||
} | ||
@@ -485,3 +371,3 @@ if (res.status !== 200) { | ||
if (browser) { | ||
this.cacheData.set(cacheKey, dataToReturn); | ||
this.cacheData.set(operationName, dataToReturn); | ||
} | ||
@@ -497,31 +383,20 @@ return dataToReturn; | ||
const nbDeleted = this.cacheData.remove(operationKey, params.variables, params.allOperationKey); | ||
this.logInfo(operationKey, 'ResetCache', nbDeleted.toString()); | ||
return nbDeleted; | ||
} | ||
logInfo(operationName, key, value) { | ||
const browserAndWantLog = this.logType.includes('client'); | ||
if (browserAndWantLog) { | ||
this.log.info(`${helper.logCyan('ResetCache:')} ${helper.logGreen(nbDeleted.toString())}, ` + | ||
`${helper.logCyan('Operation:')} ${helper.logGreen(operationKey)}`); | ||
this.log.info(`${helper.logCyan(`${key}:`)} ${helper.logGreen(value)}, ` + | ||
`${helper.logCyan('Operation:')} ${helper.logGreen(operationName)}`); | ||
} | ||
return nbDeleted; | ||
} | ||
patch(operationKey, store, newData, // To be fragments only? | ||
xPath = null) { | ||
// remove all from the cache, we will update only the current store | ||
// Can be improved later ;) => Updating all cached data (with option? Perf?) | ||
this.cacheData.remove(operationKey, null, true); | ||
let storeDataUpdated = objUpdate(false, store.data, newData, xPath); | ||
const browserAndWantLog = this.logType.includes('client'); | ||
if (!storeDataUpdated.found) { | ||
if (browserAndWantLog) { | ||
this.log.info(`${helper.logCyan('StoreUpdate:')} xPath ${helper.logGreen(xPath)} ` + | ||
`${helper.logYellow('not found')}, ` + | ||
`${helper.logCyan('Store:')} ${helper.logGreen(operationKey)}`); | ||
} | ||
cacheUpdate(operationKey, data, params) { | ||
const dataCached = this.cacheData.get(operationKey, params.variables); | ||
if (dataCached) { | ||
let toReturn = { ...dataCached, data, variables: params.variables }; | ||
this.cacheData.set(operationKey, toReturn); | ||
return toReturn; | ||
} | ||
else { | ||
this.cacheData.set(operationKey, store); | ||
if (browserAndWantLog) { | ||
this.log.info(`${helper.logCyan('StoreUpdate:')} ${helper.logGreen('1')}, ` + | ||
`${helper.logCyan('Store:')} ${helper.logGreen(operationKey)}`); | ||
} | ||
} | ||
return { ...store, data: storeDataUpdated.obj }; | ||
return undefined; | ||
} | ||
@@ -528,0 +403,0 @@ } |
@@ -13,4 +13,4 @@ import type { ResponseResult } from '../kitQLClient'; | ||
set: <DataType, VariablesType>(operationKey: string, data: ResponseResult<DataType, VariablesType>) => void; | ||
get: <DataType, VariablesType>(operationKey: string, variables: {} | null) => ResponseResult<DataType, VariablesType>; | ||
get: <DataType, VariablesType>(operationKey: string, variables: {} | null) => ResponseResult<DataType, VariablesType> | undefined; | ||
remove: (operationKey: string, variables?: {}, allOperationKey?: boolean) => number; | ||
} |
@@ -5,2 +5,2 @@ export { InMemoryCache, LocalStorageCache } from './cache'; | ||
export { defaultStoreValue, KitQLClient, RequestFrom, RequestStatus } from './kitQLClient'; | ||
export type { Credential, HeaderContentType, LogType, Policy, RequestParameters, RequestQueryParameters, RequestResult } from './kitQLClient'; | ||
export type { Credential, HeaderContentType, LogType, Policy, RequestParameters, RequestQueryParameters, RequestResult, PatchType } from './kitQLClient'; |
@@ -40,2 +40,12 @@ import type { ICacheData } from './cache/ICacheData'; | ||
cacheImplementation?: ICacheData; | ||
/** | ||
* @default 0 | ||
* @description endpoint delay in miliseconds. Usefull to simulate slow network by configuration. | ||
*/ | ||
endpointNetworkDelayMs?: number; | ||
/** | ||
* @default 0 | ||
* @description endpoint delay in miliseconds. Usefull to simulate slow ssr by configuration. | ||
*/ | ||
endpointSSRDelayMs?: number; | ||
}; | ||
@@ -56,2 +66,3 @@ export declare type RequestSettings = { | ||
export declare type LogType = 'server' | 'client' | 'operation' | 'operationAndvariables' | 'rawResult'; | ||
export declare type PatchType = 'store-only' | 'cache-only' | 'cache-and-store'; | ||
export declare type RequestParameters<V> = { | ||
@@ -77,2 +88,3 @@ fetch?: typeof fetch; | ||
date: number; | ||
operationName: string; | ||
variables?: V; | ||
@@ -92,2 +104,3 @@ data?: D | null; | ||
date: number; | ||
operationName: string; | ||
variables: any; | ||
@@ -109,16 +122,18 @@ data: any; | ||
private log; | ||
private endpointNetworkDelayMs; | ||
private endpointSSRDelayMs; | ||
constructor(options: ClientSettings); | ||
private logOperation; | ||
private getLogsStatements; | ||
requestCache<D, V>({ variables, cacheKey, cacheMs, browser }: { | ||
requestCache<D, V>({ variables, operationName, cacheMs, browser }: { | ||
variables: any; | ||
cacheKey: string; | ||
operationName: string; | ||
cacheMs: number | null; | ||
browser: boolean; | ||
}): ResponseResult<D, V> | null; | ||
request<D, V>({ skFetch, document, variables, cacheKey, browser }: { | ||
request<D, V>({ skFetch, document, variables, operationName, browser }: { | ||
skFetch: any; | ||
document: any; | ||
variables: any; | ||
cacheKey: any; | ||
operationName: any; | ||
browser: any; | ||
@@ -130,4 +145,6 @@ }): Promise<ResponseResult<D, V>>; | ||
} | null): number; | ||
patch<D, V>(operationKey: string, store: RequestResult<D, V>, newData: Object | null, // To be fragments only? | ||
xPath?: string | null): RequestResult<D, V>; | ||
logInfo(operationName: string, key: string, value: string): void; | ||
cacheUpdate<D, V>(operationKey: string, data: D, params?: { | ||
variables?: V | null; | ||
} | null): RequestResult<D, V> | undefined; | ||
} |
{ | ||
"name": "@kitql/client", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"dependencies": { | ||
"@kitql/helper": "0.1.6" | ||
"@kitql/helper": "0.1.7" | ||
}, | ||
@@ -7,0 +7,0 @@ "repository": { |
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
40934
12
941
+ Added@kitql/helper@0.1.7(transitive)
- Removed@kitql/helper@0.1.6(transitive)
Updated@kitql/helper@0.1.7