@dasch-swiss/dsp-js
Version:
TypeScript client library for DSP-API
177 lines • 6.69 kB
JavaScript
import { JsonConvert, OperationMode, ValueCheckingMode, PropertyMatchingRule } from 'json2typescript';
import { throwError } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { ApiResponseError } from '../models/api-response-error';
import { DataError } from '../models/data-error';
import { retryOnError } from './operators/retryOnError';
/**
* @category Internal
*/
export class Endpoint {
/**
* The session token
*/
get jsonWebToken() {
return this.knoraApiConfig.jsonWebToken;
}
/**
* The session token
*/
set jsonWebToken(value) {
this.knoraApiConfig.jsonWebToken = value;
}
/**
* Constructor.
*/
constructor(knoraApiConfig, path) {
this.knoraApiConfig = knoraApiConfig;
this.path = path;
this.maxRetries = 0;
this.delay = 500;
this.retryOnErrorStatus = [0, 500];
/**
* JsonConvert instance
*/
this.jsonConvert = new JsonConvert(OperationMode.ENABLE, ValueCheckingMode.DISALLOW_NULL, false, PropertyMatchingRule.CASE_STRICT);
}
/**
* Performs a general GET request.
*
* @param path the relative URL for the request
* @param headerOpts additional headers, if any.
*/
httpGet(path, headerOpts) {
if (path === undefined)
path = '';
return ajax(this.setAjaxRequest(path, 'GET', undefined, this.constructHeader(undefined, headerOpts))).pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
}
/**
* Performs a general POST request.
*
* @param path the relative URL for the request
* @param body the body of the request, if any.
* @param contentType content content type of body, if any.
* @param headerOpts additional headers, if any.
*/
httpPost(path, body, contentType = 'json', headerOpts) {
if (path === undefined)
path = '';
return ajax(this.setAjaxRequest(path, 'POST', body, this.constructHeader(contentType, headerOpts))).pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
}
/**
* Performs a general PUT request.
*
* @param path the relative URL for the request
* @param body the body of the request
* @param contentType content content type of body, if any.
* @param headerOpts additional headers, if any.
*/
httpPut(path, body, contentType = 'json', headerOpts) {
if (path === undefined)
path = '';
return ajax(this.setAjaxRequest(path, 'PUT', body, this.constructHeader(contentType, headerOpts))).pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
}
/**
* Performs a general PATCH request.
*
* @param path the relative URL for the request
* @param body the body of the request
* @param contentType content content type of body, if any.
* @param headerOpts additional headers, if any.
*/
httpPatch(path, body, contentType = 'json', headerOpts) {
if (path === undefined)
path = '';
return ajax(this.setAjaxRequest(path, 'PATCH', body, this.constructHeader(contentType, headerOpts))).pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
}
/**
* Performs a general PUT request.
*
* @param path the relative URL for the request.
* @param headerOpts additional headers, if any.
*/
httpDelete(path, headerOpts) {
if (path === undefined)
path = '';
return ajax(this.setAjaxRequest(path, 'DELETE', undefined, this.constructHeader(undefined, headerOpts))).pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
}
/**
* Handles parsing errors.
* @param error the error class provided by us
*/
handleError(error) {
let responseError;
if (this.knoraApiConfig.logErrors) {
console.error(error);
}
// Check the type of error and save it to the responseError
if (error instanceof DataError) {
responseError = error.response;
if (this.knoraApiConfig.logErrors) {
console.error(`Parse Error in Knora API request: ${responseError.error}`);
}
}
else {
responseError = ApiResponseError.fromAjaxError(error);
if (this.knoraApiConfig.logErrors) {
console.error(`Ajax Error in Knora API request: ${responseError.method} ${responseError.url}`);
}
}
return throwError(() => responseError);
}
/**
* Creates a header for a HTTP request.
* If the client has obtained a token, it is included.
*
* @param contentType Sets the content type, if any.
* @param headerOpts additional headers, if any.
*/
constructHeader(contentType, headerOpts) {
const header = {};
if (this.jsonWebToken !== '') {
// NOTE: I think this is not needed anymore because with the `withCredentials = true`
// the cookie will always be sent with each request.
// But for the moment I'll keep it
header['Authorization'] = `Bearer ${this.jsonWebToken}`;
}
if (contentType !== undefined) {
if (contentType === 'json') {
header['Content-Type'] = 'application/json; charset=utf-8';
}
else if (contentType === 'sparql') {
header['Content-Type'] = 'application/sparql-query; charset=utf-8';
}
}
if (headerOpts !== undefined) {
const headerProps = Object.keys(headerOpts);
headerProps.forEach(prop => {
header[prop] = headerOpts[prop];
});
}
return header;
}
/**
* Sets ajax request
* @param path string
* @param method 'GET', 'POST', 'PUT', 'PATCH' or 'DELETE'
* @param [body] any
* @param [headers] IHeaderOptions
* @returns AjaxRequest object
*/
setAjaxRequest(path, method, body, headers) {
const apiUrl = this.knoraApiConfig.apiUrl;
const ajaxRequest = {
url: apiUrl + this.path + path,
method,
body,
async: true,
withCredentials: true,
headers: headers || {},
timeout: 0,
crossDomain: false,
responseType: 'json',
};
return ajaxRequest;
}
}
//# sourceMappingURL=endpoint.js.map