diff --git a/ktbs-api/Ktbs.js b/ktbs-api/Ktbs.js index d97f10e..c24d96c 100644 --- a/ktbs-api/Ktbs.js +++ b/ktbs-api/Ktbs.js @@ -100,27 +100,6 @@ export class Ktbs extends Resource { throw new KtbsError("This Ktbs service does not support builtin method \"" + builtin_method_id + "\""); } - /** - * Gets the URIs of the bases in the Ktbs root - * \return Array of URL - * \protected - */ - _get_bases_uris() { - if(!this._bases_uris) { - this._bases_uris = new Array(); - - if(this._JSONData.hasBase instanceof Array) { - for(let i = 0; i < this._JSONData.hasBase.length; i++) { - let base_uri_string = this._JSONData.hasBase[i]; - let base_uri = this.resolve_link_uri(base_uri_string); - this._bases_uris.push(base_uri); - } - } - } - - return this._bases_uris; - } - /** * Gets the bases in the Ktbs root * \return Array of Base @@ -129,11 +108,24 @@ export class Ktbs extends Resource { get bases() { if(!this._bases) { this._bases = new Array(); - let bases_uris = this._get_bases_uris(); - for(let i = 0; i < bases_uris.length; i++) { - let base_uri = bases_uris[i]; - let base = ResourceMultiton.get_resource(Base, base_uri); + for(let i = 0; i < this._JSONData.hasBase.length; i++) { + const base_data = this._JSONData.hasBase[i]; + let base; + + if(base_data instanceof Object) { + const base_uri = this.resolve_link_uri(base_data["@id"]); + const base_label = base_data["label"]; + base = ResourceMultiton.get_resource(Base, base_uri); + + if(base_label && !base.label) + base.label = base_label; + } + else { + const base_uri = this.resolve_link_uri(base_data); + base = ResourceMultiton.get_resource(Base, base_uri); + } + this._bases.push(base); } } @@ -157,4 +149,18 @@ export class Ktbs extends Resource { delete(abortSignal = null, credentials = null) { throw new KtbsError("Ktbs roots can not be deleted"); } + + /** + * Gets the uri to query in order to read resource's data (For some resource types, this might be different from the resource URI, for instance if we need to add some query parameters. In such case, descending resource types must override this method) + * \return URL + * \protected + */ + get _data_read_uri() { + if(!this._dataReadUri) { + this._dataReadUri = new URL(this.uri); + this._dataReadUri.searchParams.append("prop", "label"); + } + + return this._dataReadUri; + } } diff --git a/ktbs-api/Resource.js b/ktbs-api/Resource.js index 7932295..af69909 100644 --- a/ktbs-api/Resource.js +++ b/ktbs-api/Resource.js @@ -447,6 +447,8 @@ export class Resource { if(window.localStorage) this._removeOwnCredentialsFromStorage(localStorage); + + this._credentials = null; } /** @@ -514,21 +516,21 @@ export class Resource { * \public */ get credentials() { - let credentials = null; - - if(window.sessionStorage) - credentials = this._getOwnCredentialsFromStorage(window.sessionStorage); - - if((credentials == null) && (window.localStorage)) - credentials = this._getOwnCredentialsFromStorage(window.localStorage); - - if((credentials == null) && (this._use_parent_credentials) && (window.sessionStorage)) - credentials = this._getParentsCredentialsFromStorage(window.sessionStorage); - - if((credentials == null) && (this._use_parent_credentials) && (window.localStorage)) - credentials = this._getParentsCredentialsFromStorage(window.localStorage); + if(!this._credentials) { + if(window.sessionStorage) + this._credentials = this._getOwnCredentialsFromStorage(window.sessionStorage); + + if(!this._credentials && (window.localStorage)) + this._credentials = this._getOwnCredentialsFromStorage(window.localStorage); + + if(!this._credentials && (this._use_parent_credentials) && (window.sessionStorage)) + this._credentials = this._getParentsCredentialsFromStorage(window.sessionStorage); + + if(!this._credentials && (this._use_parent_credentials) && (window.localStorage)) + this._credentials = this._getParentsCredentialsFromStorage(window.localStorage); + } - return credentials; + return this._credentials; } /** @@ -574,17 +576,18 @@ export class Resource { let fetchParameters = { method: "GET", headers: new Headers({ - "Accept": "application/json" + "Accept": "application/json", + "X-Requested-With": "XMLHttpRequest" }), cache: "default" }; - if(credentials == null) + if(!credentials && this.credentials) credentials = this.credentials; - if((credentials != null) && credentials.id && credentials.password) + if(credentials && credentials.id && credentials.password) fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password)); - + if(this._etag) fetchParameters.headers.append("If-None-Match", this._etag); @@ -780,15 +783,16 @@ export class Resource { let fetchParameters = { method: "POST", headers: new Headers({ - "content-type": "application/json" + "content-type": "application/json", + "X-Requested-With": "XMLHttpRequest" }), body: postBody }; - if(credentials == null) + if(!credentials && this.credentials) credentials = this.credentials; - if((credentials != null) && credentials.id && credentials.password) + if(credentials && credentials.id && credentials.password) fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password)); if(abortSignal) @@ -900,15 +904,16 @@ export class Resource { headers: new Headers({ "Accept": "application/json", "content-type": "application/json", - "If-Match": this._etag + "If-Match": this._etag, + "X-Requested-With": "XMLHttpRequest" }), body: JSON.stringify(this._getPutData()) }; - if(credentials == null) + if(!credentials && this.credentials) credentials = this.credentials; - if((credentials != null) && credentials.id && credentials.password) + if(credentials && credentials.id && credentials.password) fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password)); if(abortSignal) @@ -974,17 +979,18 @@ export class Resource { let fetchParameters = { method: "DELETE", headers: new Headers({ - "Accept": "application/json" + "Accept": "application/json", + "X-Requested-With": "XMLHttpRequest" }), }; if(this._etag) fetchParameters.headers.append("If-Match", this._etag); - if(credentials == null) + if(!credentials && this.credentials) credentials = this.credentials; - if((credentials != null) && credentials.id && credentials.password) + if(credentials && credentials.id && credentials.password) fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password)); if(abortSignal)