new Kom()
Server communication abstraction
This class is the main object to deal with when requesting something from the server.
It handle all urls calls (GET,POST), treat responses or handle errors usingPromise.
Because it usesPromise, success and errors are to be handled in the caller function, using.then()and.catch(). To properly deal withPOSTrequest, the session must contain a csrf token in cookies. Otherwise, thosePOSTcall may fail.
- Since:
- June 2020
Members
-
private _csrfToken :string
-
User session CSRF token to use in POST request
-
private _headers :Array.<array>
-
Array of HTTP headers to be used in HTTP calls
Methods
-
async, private, static _checkValidity()
-
Check the Kom instance validity to ensure its properties validity.
-
private, static _createRequestHeaders() → {Array.<array>}
-
Fills Kom
_headersprivate member array, to use in HTTP requests later on. This method is required to be called on construction.Returns:
Array.<array> -- The headers array, length 3, to be used in HTTP requests
-
private, static _getCsrfCookie() → {string}
-
Extract CSRF token value from client cookies and returns it as a string. Returns an empty string by default. This method is required to be called on construction.
Returns:
string -- The CSRF token string
-
private, static _getErrorCodeFromHTTPStatus(code) → {string}
-
This method is called whenever a server request didn't went well. In case a request (from any type) fails, its HTTP status code have to be handle in the method, so it returns an error code can be handled in the user interface (with notification, console or else).
Parameters:
Name Type Description codenumber The HTTP status code to handle, in supported ones from HttpStatusCode enumeration
Returns:
string -The HTTP status as an error code
-
async, private, static _resolveAs(type, response) → {Promise}
-
Generic tool method used by private methods on fetch responses to format output in the provided format. It must be either `json`, `text` or `raw`.
Parameters:
Name Type Description typeString The type of resolution, can be
json,textorrawresponseObject The
fetchresponse objectReturns:
Promise -The request
Promise, format response as an object on resolve, as error code string on reject -
async, private, static _resolveAsJSON(response) → {Promise}
-
Tool method used by public methods on fetch responses to format output data as JSON to be read in JavaScript code as objects.
Parameters:
Name Type Description responseObject The
fetchresponse objectReturns:
Promise -The request
Promise, format response as an object on resolve, as error code string on reject -
async, private, static _resolveAsRaw(response) → {Promise}
-
Tool method used by XmlHTTPRequests to format server response as raw binary data.
Parameters:
Name Type Description responseObject The
XmlHTTPRequestresponse status objectReturns:
Promise -The request
Promise, doesn't format response on resolve, send error code string on reject -
async, private, static _resolveAsText(response) → {Promise}
-
Tool method used by public methods on fetch responses to format output data as text to be read in JavaScript code as string (mostly to parse HTML templates).
Parameters:
Name Type Description responseObject The
fetchresponse objectReturns:
Promise -The request
Promise, format response as a string on resolve, as error code string on reject -
async, static get(url) → {Promise}
-
GETHTTP request using the fetch API.resolvereturns the response as anObject.rejectreturns an error key as aString. It is meant to perform API call to access database through the user interface.Parameters:
Name Type Description urlString The
GETurl to fetch data from, in supported back URLsReturns:
Promise -The request
Promise -
async, static getRaw(url) → {Promise}
-
GETHTTP request using anXMLHttpRequest, with an override mime type hack to pass bytes through unprocessed.resolvereturns the response as raw binary data.rejectreturns an error code as aString.Parameters:
Name Type Description urlString The url to fetch raw data from
Returns:
Promise -The request
Promise -
async, static getText(url) → {Promise}
-
GETHTTP request using the fetch API.resolvereturns the response as aString.rejectreturns an error key as aString. It is meant to perform API call to get HTML templates as string to be parsed as documents/documents fragments.Parameters:
Name Type Description urlString The
GETurl to fetch data from, in supported back URLsReturns:
Promise -The request
Promise -
async, static post(url, data) → {Promise}
-
POSTHTTP request using the fetch API.
Beware that the given options object match the url expectations.resolvereturns the response as anObject.rejectreturns an error key as aString.Parameters:
Name Type Description urlString The
POSTurl to fetch data fromdataObject The
JSONobject that containsPOSTparametersReturns:
Promise -The request
Promise -
async, static postRaw(url, data) → {Promise}
-
POSTHTTP request using the fetch API.
Beware that the given options object match the url expectations.resolve, with an override mime type hack to pass bytes through unprocessed.resolvereturns the response as raw binary data.rejectreturns an error code as aString.Parameters:
Name Type Description urlString The url to fetch raw data from
dataObject The
JSONobject that containsPOSTparametersReturns:
Promise -The request
Promise -
async, static postText(url, data) → {Promise}
-
POSTHTTP request using the fetch API.
Beware that the given options object match the url expectations.resolvereturns the response as aString.rejectreturns an error key as aString.Parameters:
Name Type Description urlString The
POSTurl to fetch data fromdataObject The
JSONobject that containsPOSTparametersReturns:
Promise -The request
Promise