Static class to manage REST services and related. It will return itself if it is tried to be instantiated.
- Source:
- To Do:
-
- Think about accepting an onAbort function as option (with also default and permanent).
Members
-
<static> SERVER_URL_DEFAULT :string
-
Defines the URL for the default REST server to use when the "serverURL" parameter is not available. Used by the
CB_Net.XHR.callRESTfunction as the default "serverURL" parameter.Type:
- string
- Default Value:
-
- http://localhost/CrossBrowdy/tests/net/fake_rest_server/index.php
- Source:
-
<static> actions :Object
-
List of REST actions. Each property name is the name of the action and its value is a
CB_Net.REST.actionProcess_ACTIONSobject. Used by theCB_Net.REST.actionProcessfunction.Type:
- Object
- Default Value:
-
- {}
- Source:
-
<static> allowedSuccessStatuses_DEFAULT :array
-
Default "allowedSuccessStatuses", containing the statuses to considerer a successful REST action (must be a numeric array of integers). Used by the
CB_Net.REST.actionProcessfunction.Type:
- array
- Default Value:
-
- [200,201,206]
- Source:
-
<static> allowedSuccessStatuses_PERMANENT :array
-
Permanent values for the "allowedSuccessStatuses" parameter, containing the statuses to considerer a successful REST action (must be a numeric array of integers). Used by the
CB_Net.REST.actionProcessfunction. This is permanent for each REST action.Type:
- array
- Default Value:
-
- []
- Source:
-
<static> avoidProxy_DEFAULT :boolean
-
Default value when the "avoidProxy" parameter is not available. Used by the
CB_Net.REST.actionProcessfunction.
NOTE: Edit the "CB_proxy.config.php" file to configure the default proxy (set by default in the value of theCB_Configuration.CrossBase.CB_Net_XHR_PROXY_URLproperty). Apart from configuring it, adding some security measures is highly recommended.
Have in mind that, for safety reasons, the default proxy only allows to request the URLs defined in the "$allowedURLs" array in the "CB_proxy.config.php" file. Just edit it to allow other URLs.Type:
- boolean
- Source:
-
<static> callbackBefore_DEFAULT :CB_Net.REST.actionProcess_CALLBACK_BEFORE
-
Default "callbackBefore", containing a callback function before performing each REST action returning a boolean (to decide whether to do it or not). The function assigned by default returns true. Used by the
CB_Net.REST.actionProcessfunction.Type:
-
<static> callbackBefore_PERMANENT :CB_Net.REST.actionProcess_CALLBACK_BEFORE
-
Permanent callback function before performing each REST action returning a boolean (to decide whether to do it or not). Executed before the default or the set "callbackBefore" function (if any). The function assigned by default returns true. Used by the
CB_Net.REST.actionProcessfunction. This is permanent for each REST action.Type:
-
<static> callbackError_DEFAULT :CB_Net.REST.actionProcess_CALLBACK_ERROR
-
Default "callbackError", containing a callback function when an error happens processing the REST action. Used by the
CB_Net.REST.actionProcessfunction.Type:
-
<static> callbackError_PERMANENT :CB_Net.REST.actionProcess_CALLBACK_ERROR
-
Permanent callback function when an error happens processing any REST action. Used by the
CB_Net.REST.actionProcessfunction. Executed before the default or the set "callbackError" function (if any). This is permanent for each REST action.Type:
-
<static> callbackOk_DEFAULT :CB_Net.REST.actionProcess_CALLBACK_OK
-
Default "callbackOk", containing a callback function when the REST action has been successful. Used by the
CB_Net.REST.actionProcessfunction.Type:
-
<static> callbackOk_PERMANENT :CB_Net.REST.actionProcess_CALLBACK_OK
-
Permanent callback function when each REST action has been successful. Used by the
CB_Net.REST.actionProcessfunction. Executed before the default or the set "callbackOk" function (if any). This is permanent for each REST action.Type:
-
<static> dataURL_DEFAULT :CB_Net.REST.actionProcess_DATA_URL
-
Default "dataURL" for REST actions (must return a string in URL/GET parameters format). The function assigned by default returns an empty string (""). Used by the
CB_Net.REST.actionProcessfunction. NOTE: remember to useCB_Net.URLValueEncodeif needed.Type:
-
<static> dataURL_PERMANENT :CB_Net.REST.actionProcess_DATA_URL
-
Permanent function (must return a string in URL/GET parameters format) to execute the same way as the "dataURL" parameter. The function assigned by default returns an empty string (""). Used by the
CB_Net.REST.actionProcessfunction. The returning data will be placed in the beginning of the URL, followed by the rest of the "dataURL" desired (if any). NOTE: remember to useCB_Net.URLValueEncodeif needed. This is permanent for each REST action.Type:
-
<static> data_DEFAULT :CB_Net.REST.actionProcess_DATA
-
Default "data" for REST actions (must return a string in URL/GET parameters format or a JSON format string). The function assigned by default returns an empty string (""). Used by the
CB_Net.REST.actionProcessfunction. The values returned by all functions that are mean to return the data should always return the same type of data (all strinngs or all objects) sinceCB_Net.REST.actionProcesswill use theCB_combineAutomaticallyfunction to combine them. NOTE: remember to useCB_Net.URLValueEncodeif needed.Type:
-
<static> data_PERMANENT :CB_Net.REST.actionProcess_DATA
-
Permanent function (must return a string in URL/GET parameters format or a JSON format string) to execute the same way as the "data" parameter. The function assigned by default returns an empty string (""). Used by the
CB_Net.REST.actionProcessfunction. The values returned by all functions that are mean to return the data should always return the same type of data (all strinngs or all objects) sinceCB_Net.REST.actionProcesswill use theCB_combineAutomaticallyfunction to combine them. This is permanent for each REST action. NOTE: remember to useCB_Net.URLValueEncodeif needed.Type:
-
<static> forceJSON_DEFAULT :boolean
-
Tells whether to force JSON response by default or not. Used by the
CB_Net.REST.actionProcessfunction when the REST action does not provided it and it is not null, as the "forceJSON" option.Type:
- boolean
- Default Value:
-
- true
- Source:
-
<static> getHeadersOneDimensionValues_DEFAULT :boolean
-
Tells whether to get response HTTP headers values in one-dimension (just one value per key) by default or not (needs "getHeaders" set to true). Used by the
CB_Net.REST.actionProcessfunction when the REST action does not provided it and it is not null, as the "getHeadersOneDimensionValues" option.Type:
- boolean
-
<static> getHeadersOneDimension_DEFAULT :boolean
-
Tells whether to get response HTTP headers in one-dimension array by default or not (needs "getHeaders" set to true). Used by the
CB_Net.REST.actionProcessfunction when the REST action does not provided it and it is not null, as the "getHeadersOneDimension" option.Type:
- boolean
-
<static> getHeaders_DEFAULT :boolean
-
Tells whether get response HTTP headers by default or not. Used by the
CB_Net.REST.actionProcessfunction when the REST action does not provided it and it is not null, as the "getHeaders" option.Type:
- boolean
-
<static> headers_DEFAULT :CB_Net.REST.actionProcess_HEADERS
-
Default "headers" for REST actions (must be a function returning a
CB_Net.XHR.HEADERSobject with the HTTP headers). The function assigned by default returns { "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8" }. Used by theCB_Net.REST.actionProcessfunction.Type:
-
<static> headers_PERMANENT :CB_Net.REST.actionProcess_HEADERS
-
Permanent function (must be a function returning a
CB_Net.XHR.HEADERSobject with the HTTP headers) to execute the same way as the "headers" parameter. The function assigned by default returns { "Cache-Control" : "no-cache", "Pragma" : "no-cache" }. Used by theCB_Net.REST.actionProcessfunction. This is permanent for each REST action.Type:
-
<static> method_DEFAULT :string
-
Default "method" for REST actions. Used by the
CB_Net.REST.actionProcessfunction.Type:
- string
- Default Value:
-
- GET
- Source:
-
<static> responseType_DEFAULT :string
-
Default "responseType" for REST actions when it is not provided and it is not null. Used by the
CB_Net.REST.actionProcessfunction.Type:
- string
- Default Value:
-
- text
- Source:
-
<static> routeWildcardData_DEFAULT :CB_Net.REST.actionProcess_ROUTE_WILDCARD_DATA
-
Default "routeWildcardData" for REST actions (must be a function returning a JSON object or null if we do not want to modify the route). The function assigned by default returns null. Used by the
CB_Net.REST.actionProcessfunction.Type:
- Source:
-
<static> routeWildcardData_PERMANENT :CB_Net.REST.actionProcess_ROUTE_WILDCARD_DATA
-
Permanent function (must return a JSON object or null if we do not want to modify it) to execute the same way as the "routeWildcardData" parameter (if other "routeWildcardData" are provided or set, they will be executed before this one). The function assigned by default returns null. Used by the
CB_Net.REST.actionProcessfunction. This is permanent for each REST action.Type:
- Source:
-
<static> transparentHeaders_DEFAULT :boolean
-
Tells whether get HTTP headers transparently from the server by default or not. Used by the
CB_Net.REST.actionProcessfunction when the REST action does not provided it and it is not null, as the "transparentHeaders" option.Type:
- boolean
- Default Value:
-
- true
- Source:
-
<static> transparentStatus_DEFAULT :boolean
-
Tells whether get status transparently from the server by default or not. Used by the
CB_Net.REST.actionProcessfunction when the REST action does not provided it and it is not null, as the "transparentStatus" option.Type:
- boolean
- Default Value:
-
- true
- Source:
Methods
-
<static> actionAbort(XHR) → {boolean}
-
Cancels a REST action (by its AJAX/XHR request associated) by executing the "abort" method of the given XHR object (if available) and sets its "aborted" property to true if succeeds. When a REST action is aborted, the callbackOk functions associated (if any) will not be called (unless they have been called already).
Parameters:
Name Type Description XHRObject The XHR object that we want to abort.
Returns:
Returns true if the "abort" method of the given XHR object could be called and the "aborted" property was set to true. Otherwise, returns false.
- Type
- boolean
-
<static> actionAbortCancel(XHR) → {boolean}
-
Tries to revert the abortion of a REST action (by its AJAX/XHR request associated) by setting the "aborted" property of the given XHR object to false. If this function is called before the "callbackOk" functions associated to the REST action (if any) are called, they could finally be called perhaps (depending on each case, could be different). Have in mind that, after calling
CB_Net.REST.actionAbort, the "abort" method of the XHR object was probably called already (if available).Parameters:
Name Type Description XHRObject The XHR object whose abortion we want to cancel.
Returns:
Returns true if the "aborted" property was set to false. Otherwise, returns false.
- Type
- boolean
-
<static> actionIsAborted(XHR) → {boolean}
-
Parameters:
Name Type Description XHRObject The XHR object that we want to check.
Returns:
Returns true if the "aborted" property is set to true. Otherwise, returns false.
- Type
- boolean
-
<static> actionProcess(actionName [, additionalData] [, XHR] [, serverURL] [, avoidProxy]) → {Object|null}
-
Processes a desired REST action (which must be previously defined in the
CB_Net.REST.actionsobject) by its name.
NOTE: Edit the "CB_proxy.config.php" file to configure the default proxy (set by default in the value of theCB_Configuration.CrossBase.CB_Net_XHR_PROXY_URLproperty). Apart from configuring it, adding some security measures is highly recommended.
Have in mind that, for safety reasons, the default proxy only allows to request the URLs defined in the "$allowedURLs" array in the "CB_proxy.config.php" file. Just edit it to allow other URLs.Parameters:
Name Type Argument Description actionNamestring The name of the REST action that we want to perform. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.additionalData* <optional>
Any additional data desired. It will be passed as a parameter when different callbacks are called internally, as their "additionalData" parameter.
XHRObject <optional>
Used for the "XHR" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.serverURLstring <optional>
Used for the "serverURL" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally. If provided, the value defined in the "serverURL" parameter of the REST action being performed (if any) will be ignored.avoidProxyboolean <optional>
Used for the "avoidProxy" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally. If provided, the value defined in the "avoidProxy" parameter of the REST action being performed (if any) and the default one defined inCB_Net.REST.avoidProxy_DEFAULT(if any) will be both ignored.Returns:
Returns null if the
CB_Net.XHR.callRESTfunction is not called at all (because XHR is not supported or the REST action object cannot be found or the route is not well defined or the "callbackBefore" returns false, etc.). Otherwise, returns the same that theCB_Net.XHR.callRESTfunction returns (called internally).- Type
- Object | null
Type Definitions
-
actionProcess_ACTIONS
-
Object that represents a REST action.
Type:
- Object
- Source:
Properties:
Name Type Argument Default Description routestring The REST route (path). Belongs to the "route" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally. It is mandatory.callbackBeforeCB_Net.REST.actionProcess_CALLBACK_BEFORE <optional>
CB_Net.REST.callbackBefore_DEFAULTCallback run before performing the REST action which will decide whether to continue with it or not.
avoidProxyboolean <optional>
CB_Net.REST.avoidProxy_DEFAULTBelongs to the "avoidProxy" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.routeWildcardDataCB_Net.REST.actionProcess_ROUTE_WILDCARD_DATA <optional>
CB_Net.REST.routeWildcardData_DEFAULTCallback returning an object that will be used for parsing the REST route. The object returned should follow the same rules as the "JSONObject" parameter of the
CB_renderStringfunction.dataURLCB_Net.REST.actionProcess_DATA_URL <optional>
CB_Net.REST.dataURL_DEFAULTBelongs to the "dataURL" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.dataCB_Net.REST.actionProcess_DATA <optional>
CB_Net.REST.data_DEFAULTBelongs to the "data" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.headersCB_Net.REST.actionProcess_HEADERS <optional>
CB_Net.REST.headers_DEFAULTBelongs to the "headers" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.allowedSuccessStatusesarray <optional>
CB_Net.REST.allowedSuccessStatuses_DEFAULTBelongs to the "allowedSuccessStatuses" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.callbackErrorCB_Net.REST.actionProcess_CALLBACK_ERROR <optional>
CB_Net.REST.callbackError_DEFAULTCallback run when the REST action has not been performed successfully.
callbackOkCB_Net.REST.actionProcess_CALLBACK_OK <optional>
CB_Net.REST.callbackOk_DEFAULTCallback run when the REST action has been performed successfully.
serverURLstring <optional>
CB_Net.REST.SERVER_URL_DEFAULTBelongs to the "serverURL" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.methodstring <optional>
CB_Net.REST.method_DEFAULTBelongs to the "method" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally.responseTypestring <optional>
CB_Net.REST.responseType_DEFAULTBelongs to the "responseType" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally and will be sent even it is null. If it is undefined, it will use the value ofCB_Net.REST.responseType_DEFAULTinstead.forceJSONboolean <optional>
CB_Net.REST.forceJSON_DEFAULTBelongs to the "forceJSON" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally and will be sent even it is null. If it is undefined, it will use the value ofCB_Net.REST.forceJSON_DEFAULTinstead.getHeadersboolean <optional>
CB_Net.REST.getHeaders_DEFAULTBelongs to the "getHeaders" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally and will be sent even it is null. If it is undefined, it will use the value ofCB_Net.REST.getHeaders_DEFAULTinstead.getHeadersOneDimensionboolean <optional>
CB_Net.REST.getHeadersOneDimension_DEFAULTBelongs to the "getHeadersOneDimension" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally and will be sent even it is null. If it is undefined, it will use the value ofCB_Net.REST.getHeadersOneDimension_DEFAULTinstead.getHeadersOneDimensionValuesboolean <optional>
CB_Net.REST.getHeadersOneDimensionValues_DEFAULTBelongs to the "getHeadersOneDimensionValues" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally and will be sent even it is null. If it is undefined, it will use the value ofCB_Net.REST.getHeadersOneDimensionValues_DEFAULTinstead.transparentStatusboolean <optional>
CB_Net.REST.transparentStatus_DEFAULTBelongs to the "transparentStatus" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally and will be sent even it is null. If it is undefined, it will use the value ofCB_Net.REST.transparentStatus_DEFAULTinstead.transparentHeadersboolean <optional>
CB_Net.REST.transparentHeaders_DEFAULTBelongs to the "transparentHeaders" parameter of the
CB_Net.XHR.callRESTfunction when it is called internally and will be sent even it is null. If it is undefined, it will use the value ofCB_Net.REST.transparentHeaders_DEFAULTinstead. -
actionProcess_CALLBACK_BEFORE(actionName, additionalData) → {boolean}
-
Callback that will be run before performing a REST action and must return a boolean defining whether we want to continue performing the action (returning true) or not (returning false).
Parameters:
Name Type Description actionNamestring The name of the REST action being performed. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.additionalData* Any additional data associated to the REST action being performed (belongs to the "additionalData" parameter used when the
CB_Net.REST.actionProcessfunction was called).Returns:
It should return true in the case that we want to continue performing the current REST action or false otherwise.
- Type
- boolean
-
actionProcess_CALLBACK_ERROR(actionName [, XHR], additionalData)
-
Callback that will be run when the REST action has been performed successfully.
Parameters:
Name Type Argument Description actionNamestring The name of the REST action being performed. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.XHRObject <optional>
The XHR object that has been used to perform the REST action.
additionalData* Any additional data associated to the REST action being performed (belongs to the "additionalData" parameter used when the
CB_Net.REST.actionProcessfunction was called). -
actionProcess_CALLBACK_OK(actionName [, XHR], callbackError, additionalData)
-
Callback that will be run when the REST action has been performed successfully.
Parameters:
Name Type Argument Description actionNamestring The name of the REST action being performed. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.XHRObject <optional>
The XHR object that has been used to perform the REST action.
callbackErrorCB_Net.REST.actionProcess_CALLBACK_ERROR The error callback function associated to the REST action being performed, just in case we want to call it.
additionalData* Any additional data associated to the REST action being performed (belongs to the "additionalData" parameter used when the
CB_Net.REST.actionProcessfunction was called). -
actionProcess_DATA(actionName, additionalData) → {string|Object}
-
Callback returning the data (string or object) which will be used as the "data" parameter when the
CB_Net.XHR.callRESTfunction is called internally byCB_Net.REST.actionProcess. The values returned by all functions that are mean to return the data should always return the same type of data (all strinngs or all objects) sinceCB_Net.REST.actionProcesswill use theCB_combineAutomaticallyfunction to combine them.Parameters:
Name Type Description actionNamestring The name of the REST action being performed. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.additionalData* Any additional data associated to the REST action being performed (belongs to the "additionalData" parameter used when the
CB_Net.REST.actionProcessfunction was called).Returns:
It should return a string or object containing the data. Return null or an empty string (recommended) in the case that we do not want to add anything. The value returned (if any) will be used as the "data" parameter of the
CB_Net.XHR.callRESTfunction when it is called byCB_Net.REST.actionProcessinternally.- Type
- string | Object
-
actionProcess_DATA_URL(actionName, additionalData) → {string}
-
Callback returning a string containing the URL (GET) data which will be used as the "dataURL" parameter when the
CB_Net.XHR.callRESTfunction is called internally byCB_Net.REST.actionProcess.Parameters:
Name Type Description actionNamestring The name of the REST action being performed. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.additionalData* Any additional data associated to the REST action being performed (belongs to the "additionalData" parameter used when the
CB_Net.REST.actionProcessfunction was called).Returns:
It should return a string containing the URL (GET) data. Return an empty string in the case that we do not want to add anything. The string returned (if any) will be used as the "dataURL" parameter of the
CB_Net.XHR.callRESTfunction when it is called byCB_Net.REST.actionProcessinternally.- Type
- string
-
actionProcess_HEADERS(actionName, additionalData) → {CB_Net.XHR.HEADERS|null}
-
Callback returning an object containing the HTTP headers which will be used as the "headers" parameter when the
CB_Net.XHR.callRESTfunction is called internally byCB_Net.REST.actionProcess.Parameters:
Name Type Description actionNamestring The name of the REST action being performed. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.additionalData* Any additional data associated to the REST action being performed (belongs to the "additionalData" parameter used when the
CB_Net.REST.actionProcessfunction was called).Returns:
It should return an object containing the HTTP headers. Return null in the case that we do not want to add anything. The object returned (if any) will be used as the "headers" parameter of the
CB_Net.XHR.callRESTfunction when it is called byCB_Net.REST.actionProcessinternally.- Type
- CB_Net.XHR.HEADERS | null
-
actionProcess_ROUTE_WILDCARD_DATA(actionName, additionalData) → {Object|null}
-
Callback returning an object that will be used for parsing the REST route. The object returned should follow the same rules as the "JSONObject" parameter of the
CB_renderStringfunction.Parameters:
Name Type Description actionNamestring The name of the REST action being performed. It should be the name of an index defined in the
CB_Net.REST.actionsobject whose value should be aCB_Net.REST.actionProcess_ACTIONSobject.additionalData* Any additional data associated to the REST action being performed (belongs to the "additionalData" parameter used when the
CB_Net.REST.actionProcessfunction was called).- Source:
Returns:
It should return an object containing the wildcards that will be used to parse the REST route of the action being performed. Return null in the case that we do not want to modify anything. The object returned (if any) will be used as the "JSONObject" parameter of the
CB_renderStringfunction (the first parameter will be the route which should be a string) when it is called byCB_Net.REST.actionProcessinternally.- Type
- Object | null