new RestfulJsonClient(baseUrlopt, nullable, baseOptionsopt, nullable, useNativeopt, strictopt, nullable)
Creates a new RestfulJsonClient
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
baseUrl |
String |
<optional> <nullable> |
window.location.origin | the base URL for all queries, based on which final request URLs will be built, adding the paths, may be absolute or relative to current origin |
baseOptions |
Object |
<optional> <nullable> |
null | the base request options, can be expanded later via options() (see createFetchRequests for details) |
useNative |
Boolean | String |
<optional> |
false | determines if the native Fetch implementation of the browser should be used, true forces usage, "auto" uses it only if available |
strict |
Boolean |
<optional> <nullable> |
true | if true, enforces "application/json" as accept header as well as response mime type, if false, accept header is not set and different mime type only results in warning |
- Source:
- See:
Methods
data(datanullable) → {RestfulJsonClient}
Sets data payload for POST, PUT and PATCH requests.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
Object |
<nullable> |
data payload to send with the next POST, PUT or PATCH request, this parameter will set a permanent payload; for one-off payloads, use the verb method's data parameter; if nullish, data will be emptied |
- Source:
Throws:
error if given data is not a plain object
Returns:
- Type
- RestfulJsonClient
Example
(new RestfulJsonClient('/food/order')).data({pizza : 'diavolo'}).post();
delete() → {Basic.Deferred.<Requests.JsonFetchResponse>}
Queries the current URL with DELETE.
- Source:
Throws:
error in strict mode if response content type is not "application/json"
Returns:
Example
(new RestfulJsonClient('/')).data({command : 'rm -rf'}).delete();
get() → {Basic.Deferred.<Requests.JsonFetchResponse>}
Queries the current URL with GET.
- Source:
Throws:
error in strict mode if response content type is not "application/json"
Returns:
Example
(new RestfulJsonClient('/food/')).path('pizza').get();
getConfig() → {RestfulJsonClientConfig}
Returns the current config.
baseUrl is retrievable from the url property (via origin). Options are the merged result of baseOptions and currently set option values.
Changes to this object, will not reflect in the client config directly, use the client's methods to alter config values.
- Source:
Returns:
- Type
- RestfulJsonClientConfig
Example
client.getConfig().url.toString()
=> https://pizza.com
header(key, valuenullable) → {RestfulJsonClient}
Sets a header for all subsequent requests. Use a nullish value to unset a header.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
String | the header to set for all following requests |
|
value |
String |
<nullable> |
the header's value; a nullish value will remove the header again |
- Source:
Returns:
- Type
- RestfulJsonClient
Example
(new RestfulJsonClient('/run-forrest-run')).header('X-Test', 42).header('X-Test', null);
options(optionsnullable) → {RestfulJsonClient}
Sets the current request options, which will be merged with baseOptions.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Object |
<nullable> |
plain options object to merge with baseOptions to define current request options (see createFetchRequest for details and defaults); if nullish, baseOptions will be used |
- Source:
- See:
Throws:
error if given options are not a plain object
Returns:
- Type
- RestfulJsonClient
Example
(new RestfulJsonClient('/run-forrest-run')).options({timeout : 1});
param(key, valuenullable, appendopt, nullable) → {RestfulJsonClient}
Sets a query parameter to be added to the request URL. Use a nullish value to unset a parameter.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
String | the parameter name to set |
||
value |
String |
<nullable> |
the parameter's value; a nullish value will remove the param again |
|
append |
Boolean |
<optional> <nullable> |
false | if true, the parameter is appended instead of overwritten |
- Source:
Returns:
- Type
- RestfulJsonClient
Example
(new RestfulJsonClient('/food/search')).param('q', 'delicious pizza').get();
params(paramsnullable) → {RestfulJsonClient}
Sets query parameters to be added to the request URL.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
params |
* |
<nullable> |
query parameters to set on the current URL, this parameter takes all regular definitions for URLSearchParams constructor, as well as flat plain objects, which may also have arrays as values; if nullish, parameters are emptied |
- Source:
- See:
Returns:
- Type
- RestfulJsonClient
Example
(new RestfulJsonClient('/food/search')).params({q : 'delicious pizza'}).get();
patch(dataopt, nullable) → {Basic.Deferred.<Requests.JsonFetchResponse>}
Queries the current URL with PATCH using defined payload/data.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
data |
Object |
<optional> <nullable> |
null | one-off data to use in this request, will replace any central data defined before and will only be valid for this request |
- Source:
Throws:
error in strict mode if response content type is not "application/json"
Returns:
Example
(new RestfulJsonClient('/software')).data({version : 2}).patch();
path(path) → {RestfulJsonClient}
Sets the current request path, which will be concatenated to baseUrl.
Parameters:
Name | Type | Description |
---|---|---|
path |
String | the current path to request from baseUrl |
- Source:
Returns:
- Type
- RestfulJsonClient
Example
(new RestfulJsonClient('/food')).path('/pizzas').get();
post(dataopt, nullable) → {Basic.Deferred.<Requests.JsonFetchResponse>}
Queries the current URL with POST using defined payload/data.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
data |
Object |
<optional> <nullable> |
null | one-off data to use in this request, will replace any central data defined before and will only be valid for this request |
- Source:
Throws:
error in strict mode if response content type is not "application/json"
Returns:
Example
(new RestfulJsonClient('/food/order')).post({pizza : 'diavolo'});
put(dataopt, nullable) → {Basic.Deferred.<Requests.JsonFetchResponse>}
Queries the current URL with PUT using defined payload/data.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
data |
Object |
<optional> <nullable> |
null | one-off data to use in this request, will replace any central data defined before and will only be valid for this request |
- Source:
Throws:
error in strict mode if response content type is not "application/json"
Returns:
Example
(new RestfulJsonClient('/food/deliver')).put({pizza : 'diavolo'});