@jbouduin/hal-rest-client
Advanced tools
Comparing version 0.6.0-beta.6 to 0.7.0
import { IHalResource } from "./hal-resource.interface"; | ||
import { IHalRestClient } from "./hal-rest-client.interface"; | ||
export declare type HalCacheType = 'Client' | 'Resource'; | ||
/** | ||
* a function that accepts the calculated cachekey and returns true if the resource or client may be cached | ||
*/ | ||
export declare type KeyValidatorFn = (key: string) => boolean; | ||
export interface IHalCache { | ||
readonly isEnabled: boolean; | ||
/** | ||
* Enables the cache | ||
*/ | ||
enable(): void; | ||
/** | ||
* Disables the cache and clears all the contents | ||
*/ | ||
disable(): void; | ||
/** | ||
* Selective purge of a cache. To clear the cache completely use reset. | ||
@@ -32,2 +45,4 @@ * | ||
setResource(uri: string, value: IHalResource): void; | ||
setClientKeyValidator(validator: KeyValidatorFn): void; | ||
setResourceKeyValidator(validator: KeyValidatorFn): void; | ||
} |
@@ -6,6 +6,23 @@ "use strict"; | ||
class HalCache { | ||
//#endregion | ||
//#region Constructor & C° -------------------------------------------------- | ||
constructor() { | ||
this.clientCache = new Map(); | ||
this.resourceCache = new Map(); | ||
this._isEnabled = true; | ||
} | ||
//#endregion | ||
//#region IHalCache properties ---------------------------------------------- | ||
get isEnabled() { | ||
return this._isEnabled; | ||
} | ||
//#endregion | ||
//#region IHalCach interface methods ---------------------------------------- | ||
enable() { | ||
this._isEnabled = true; | ||
} | ||
disable() { | ||
this._isEnabled = false; | ||
this.reset(); | ||
} | ||
reset(type) { | ||
@@ -56,3 +73,5 @@ if (!type || type === 'Client') { | ||
setClient(uri, value) { | ||
this.clientCache.set(uri, value); | ||
if (this._isEnabled && (!this.clientKeyValidator || this.clientKeyValidator(uri))) { | ||
this.clientCache.set(uri, value); | ||
} | ||
} | ||
@@ -66,6 +85,14 @@ hasResource(uri) { | ||
setResource(uri, value) { | ||
this.resourceCache.set(uri, value); | ||
if (this._isEnabled && (!this.resourceKeyValidator || this.resourceKeyValidator(uri))) { | ||
this.resourceCache.set(uri, value); | ||
} | ||
} | ||
setClientKeyValidator(validator) { | ||
this.clientKeyValidator = validator; | ||
} | ||
setResourceKeyValidator(validator) { | ||
this.resourceKeyValidator = validator; | ||
} | ||
} | ||
exports.HalCache = HalCache; | ||
//# sourceMappingURL=hal-cache.js.map |
@@ -9,10 +9,13 @@ import { AxiosRequestConfig } from 'axios'; | ||
* Create hal rest client | ||
* If not baseUri is provided, a new HalRestClient is created. | ||
* Othwersie, if a client with same base uri already exists, it is retrieved from the cache and returned. If it does not exists yet, a new client is created and cached. | ||
* If no baseUri is provided, a new HalRestClient is created. | ||
* Othwerise, if a client with same base uri already exists, it is retrieved from the cache and returned. | ||
* If it does not exists yet, a new client is created. If the cached parameter is not set to false this new client is cached. | ||
* | ||
* @param {string} baseUri - the baseUri that will be used to configure Axios | ||
* @param {string} baseUri - the baseUri that will be used to configure Axios. | ||
* @param {AxiosRequestConfig} options - the options that will be passed to Axios | ||
* @param {boolean} cached - if set to false, the client will not be added to the cache. Remark: even if set to false, an existing cached entry will be returned. | ||
* Defaults to 'false' | ||
* @returns {IHalRestClient} - a IHalrestClient | ||
*/ | ||
export declare function createClient(baseUri?: string, options?: AxiosRequestConfig): IHalRestClient; | ||
export declare function createClient(baseUri?: string, options?: AxiosRequestConfig, cached?: boolean): IHalRestClient; | ||
/** | ||
@@ -19,0 +22,0 @@ * Create a HalResource of the given type. If no uri is specified, an 'empty' resource is created. |
@@ -11,10 +11,13 @@ "use strict"; | ||
* Create hal rest client | ||
* If not baseUri is provided, a new HalRestClient is created. | ||
* Othwersie, if a client with same base uri already exists, it is retrieved from the cache and returned. If it does not exists yet, a new client is created and cached. | ||
* If no baseUri is provided, a new HalRestClient is created. | ||
* Othwerise, if a client with same base uri already exists, it is retrieved from the cache and returned. | ||
* If it does not exists yet, a new client is created. If the cached parameter is not set to false this new client is cached. | ||
* | ||
* @param {string} baseUri - the baseUri that will be used to configure Axios | ||
* @param {string} baseUri - the baseUri that will be used to configure Axios. | ||
* @param {AxiosRequestConfig} options - the options that will be passed to Axios | ||
* @param {boolean} cached - if set to false, the client will not be added to the cache. Remark: even if set to false, an existing cached entry will be returned. | ||
* Defaults to 'false' | ||
* @returns {IHalRestClient} - a IHalrestClient | ||
*/ | ||
function createClient(baseUri, options = {}) { | ||
function createClient(baseUri, options = {}, cached = true) { | ||
let result; | ||
@@ -30,3 +33,5 @@ if (!baseUri) { | ||
result = new hal_rest_client_1.HalRestClient(baseUri, options); | ||
exports.cache.setClient(baseUri, result); | ||
if (cached) { | ||
exports.cache.setClient(baseUri, result); | ||
} | ||
} | ||
@@ -33,0 +38,0 @@ else { |
@@ -8,3 +8,3 @@ import { IJSONSerializer } from './hal-json-serializer.interface'; | ||
private _restClient; | ||
private _uri?; | ||
private _uri; | ||
private readonly settedProps; | ||
@@ -21,3 +21,3 @@ private readonly settedLinks; | ||
get linkKeys(): Array<string>; | ||
constructor(restClient: IHalRestClient, uri?: UriData); | ||
constructor(restClient: IHalRestClient, uri: UriData); | ||
fetch(options?: IResourceFetchOptions): Promise<this>; | ||
@@ -43,2 +43,4 @@ setProperty<T>(name: string, value?: T): void; | ||
private parseProp; | ||
private toJSON; | ||
private linkToJson; | ||
} |
@@ -208,4 +208,37 @@ "use strict"; | ||
} | ||
//#endregion | ||
//#region toJSON ------------------------------------------------------------ | ||
toJSON() { | ||
const result = {}; | ||
result['restClient'] = this._restClient; | ||
result['uri'] = this._uri; | ||
result['cacheKey'] = this._uri.calculateCacheKey(this._restClient.config.baseURL); | ||
result['links'] = {}; | ||
this.linkKeys.forEach((linkKey) => { | ||
const linkInstance = this.getLink(linkKey); | ||
if (Array.isArray(linkInstance)) { | ||
result['links'][linkKey] = linkInstance.map((l) => this.linkToJson(l)); | ||
} | ||
else { | ||
result['links'][linkKey] = this.linkToJson(linkInstance); | ||
} | ||
}); | ||
result['properties'] = {}; | ||
this.propertyKeys.forEach((propertyKey) => { | ||
result['properties'][propertyKey] = this.getProperty(propertyKey); | ||
}); | ||
return result; | ||
} | ||
linkToJson(link) { | ||
const result = {}; | ||
result['uri'] = link.uri; | ||
result['cacheKey'] = link['_uri'].calculateCacheKey(this._restClient.config.baseURL); | ||
result['type'] = link['type']; | ||
result['name'] = link['name']; | ||
result['title'] = link['title']; | ||
result['isLoaded'] = link.isLoaded; | ||
return result; | ||
} | ||
} | ||
exports.HalResource = HalResource; | ||
//# sourceMappingURL=hal-resource.js.map |
@@ -34,2 +34,3 @@ import "reflect-metadata"; | ||
fetchInternal<T extends IHalResource>(uri: string, type: IHalResourceConstructor<T>, resource?: T): Promise<T>; | ||
private toJSON; | ||
} |
@@ -148,4 +148,14 @@ "use strict"; | ||
} | ||
//#endregion | ||
//#region toJSON ------------------------------------------------------------ | ||
toJSON() { | ||
return { | ||
baseUrl: this.config.baseURL, | ||
headers: this.config.headers, | ||
requestInterceptors: this.requestInterceptors, | ||
responseInterceptors: this.responseInterceptors | ||
}; | ||
} | ||
} | ||
exports.HalRestClient = HalRestClient; | ||
//# sourceMappingURL=hal-rest-client.js.map |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.6.0-beta.6", | ||
"version": "0.7.0", | ||
"description": "Hal rest client for typescript", | ||
@@ -11,0 +11,0 @@ "tags": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
121316
1519
0