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 withPOST
request, the session must contain a csrf token in cookies. Otherwise, thosePOST
call 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
_headers
private 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 code
number 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 type
String The type of resolution, can be
json
,text
orraw
response
Object The
fetch
response 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 response
Object The
fetch
response 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 response
Object The
XmlHTTPRequest
response 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 response
Object The
fetch
response objectReturns:
Promise -The request
Promise
, format response as a string on resolve, as error code string on reject -
async, static get(url) → {Promise}
-
GET
HTTP request using the fetch API.resolve
returns the response as anObject
.reject
returns an error key as aString
. It is meant to perform API call to access database through the user interface.Parameters:
Name Type Description url
String The
GET
url to fetch data from, in supported back URLsReturns:
Promise -The request
Promise
-
async, static getRaw(url) → {Promise}
-
GET
HTTP request using anXMLHttpRequest
, with an override mime type hack to pass bytes through unprocessed.resolve
returns the response as raw binary data.reject
returns an error code as aString
.Parameters:
Name Type Description url
String The url to fetch raw data from
Returns:
Promise -The request
Promise
-
async, static getText(url) → {Promise}
-
GET
HTTP request using the fetch API.resolve
returns the response as aString
.reject
returns 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 url
String The
GET
url to fetch data from, in supported back URLsReturns:
Promise -The request
Promise
-
async, static post(url, data) → {Promise}
-
POST
HTTP request using the fetch API.
Beware that the given options object match the url expectations.resolve
returns the response as anObject
.reject
returns an error key as aString
.Parameters:
Name Type Description url
String The
POST
url to fetch data fromdata
Object The
JSON
object that containsPOST
parametersReturns:
Promise -The request
Promise
-
async, static postRaw(url, data) → {Promise}
-
POST
HTTP 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.resolve
returns the response as raw binary data.reject
returns an error code as aString
.Parameters:
Name Type Description url
String The url to fetch raw data from
data
Object The
JSON
object that containsPOST
parametersReturns:
Promise -The request
Promise
-
async, static postText(url, data) → {Promise}
-
POST
HTTP request using the fetch API.
Beware that the given options object match the url expectations.resolve
returns the response as aString
.reject
returns an error key as aString
.Parameters:
Name Type Description url
String The
POST
url to fetch data fromdata
Object The
JSON
object that containsPOST
parametersReturns:
Promise -The request
Promise