Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(endpoint)!: resolve set-cookie issue (DSP-1827) #361

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/api/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { JsonConvert, OperationMode, ValueCheckingMode } from "json2typescript";
import { PropertyMatchingRule } from "json2typescript/src/json2typescript/json-convert-enums";
import { Observable, of, throwError } from "rxjs";
import { ajax, AjaxError, AjaxResponse } from "rxjs/ajax";

import { Observable, throwError } from "rxjs";
import { ajax, AjaxError, AjaxRequest, AjaxResponse } from "rxjs/ajax";
import { KnoraApiConfig } from "../knora-api-config";
import { ApiResponseError } from "../models/api-response-error";
import { DataError } from "../models/data-error";
Expand Down Expand Up @@ -71,7 +70,7 @@ export class Endpoint {

if (path === undefined) path = "";

return ajax.get(this.knoraApiConfig.apiUrl + this.path + path, this.constructHeader(undefined, headerOpts))
return ajax(this.setAjaxRequest(path, "GET", undefined, this.constructHeader(undefined, headerOpts)))
.pipe(
retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors)
);
Expand All @@ -90,7 +89,7 @@ export class Endpoint {

if (path === undefined) path = "";

return ajax.post(this.knoraApiConfig.apiUrl + this.path + path, body, this.constructHeader(contentType, headerOpts))
return ajax(this.setAjaxRequest(path, "POST", body, this.constructHeader(contentType, headerOpts)))
.pipe(
retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors)
);
Expand All @@ -109,7 +108,7 @@ export class Endpoint {

if (path === undefined) path = "";

return ajax.put(this.knoraApiConfig.apiUrl + this.path + path, body, this.constructHeader(contentType, headerOpts))
return ajax(this.setAjaxRequest(path, "PUT", body, this.constructHeader(contentType, headerOpts)))
.pipe(
retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors)
);
Expand All @@ -128,7 +127,7 @@ export class Endpoint {

if (path === undefined) path = "";

return ajax.patch(this.knoraApiConfig.apiUrl + this.path + path, body, this.constructHeader(contentType, headerOpts))
return ajax(this.setAjaxRequest(path, "PATCH", body, this.constructHeader(contentType, headerOpts)))
.pipe(
retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors)
);
Expand All @@ -145,7 +144,7 @@ export class Endpoint {

if (path === undefined) path = "";

return ajax.delete(this.knoraApiConfig.apiUrl + this.path + path, this.constructHeader(undefined, headerOpts))
return ajax(this.setAjaxRequest(path, "DELETE", undefined, this.constructHeader(undefined, headerOpts)))
.pipe(
retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors)
);
Expand Down Expand Up @@ -194,11 +193,14 @@ export class Endpoint {
* @param contentType Sets the content type, if any.
* @param headerOpts additional headers, if any.
*/
private constructHeader(contentType?: "json" | "sparql", headerOpts?: IHeaderOptions): object {
private constructHeader(contentType?: "json" | "sparql", headerOpts?: IHeaderOptions): IHeaderOptions {

const header: IHeaderOptions = {};

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;
}

Expand All @@ -222,4 +224,26 @@ export class Endpoint {
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
*/
private setAjaxRequest(path: string, method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", body?: any, headers?: IHeaderOptions): AjaxRequest {

let ajaxRequest: AjaxRequest = {
url: this.knoraApiConfig.apiUrl + this.path + path,
method: method,
body: body,
async: true,
withCredentials: true,
headers: headers
};

return ajaxRequest;
}

}