Resource

external class Resource(options: Any)(source)

A resource that includes the location and any other parameters we need to retrieve it or create derived resources. It also provides the ability to retry requests.

function refreshTokenRetryCallback(resource, error) {
if (error.statusCode === 403) {
// 403 status code means a new token should be generated
return getNewAccessToken()
.then(function(token) {
resource.queryParameters.access_token = token;
return true;
})
.catch(function() {
return false;
});
}

return false;
}

const resource = new Resource({
url: 'http://server.com/path/to/resource.json',
proxy: new DefaultProxy('/proxy/'),
headers: {
'X-My-Header': 'valueOfHeader'
},
queryParameters: {
'access_token': '123-435-456-000'
},
retryCallback: refreshTokenRetryCallback,
retryAttempts: 1
});

Parameters

options

A url or an object describing initialization options

See also

Constructors

Link copied to clipboard
constructor(options: Any)

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Initialization options for the Resource constructor

Link copied to clipboard
interface DeleteOptions
Link copied to clipboard
Link copied to clipboard
interface FetchOptions
Link copied to clipboard
Link copied to clipboard
interface HeadOptions
Link copied to clipboard
interface OptionsOptions
Link copied to clipboard
interface PatchOptions
Link copied to clipboard
interface PostOptions
Link copied to clipboard
interface PutOptions

Properties

Link copied to clipboard

The file extension of the resource.

Link copied to clipboard

True if the Resource has request headers. This is equivalent to checking if the headers property has any keys.

Link copied to clipboard

Additional HTTP headers that will be sent with the request.

Link copied to clipboard

True if the Resource refers to a blob URI.

Link copied to clipboard

True if the Resource refers to a cross origin URL.

Link copied to clipboard

True if the Resource refers to a data URI.

Link copied to clipboard

A proxy to be used when loading the resource.

Link copied to clipboard

Query parameters appended to the url.

Link copied to clipboard

A Request object that will be used. Intended for internal use only.

Link copied to clipboard

The number of times the retryCallback should be called before giving up.

Link copied to clipboard

Function to call when a request for this resource fails. If it returns true or a Promise that resolves to true, the request will be retried.

Link copied to clipboard

The key/value pairs used to replace template parameters in the url.

Link copied to clipboard
var url: String

The url to the resource with template values replaced, query string appended and encoded by proxy if one was set.

Functions

Link copied to clipboard

Appends a forward slash to the URL.

Link copied to clipboard

Combines the specified object and the existing query parameters. This allows you to add many parameters at once, as opposed to adding them one at a time to the queryParameters property.

Link copied to clipboard
fun clone(result: Resource? = definedExternally): Resource

Duplicates a Resource instance.

Link copied to clipboard
suspend fun delete(options: Resource.DeleteOptions? = definedExternally): Any?

Asynchronously deletes the given resource. Returns a promise that will resolve to the result once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
fun deleteAsync(options: Resource.DeleteOptions? = definedExternally): Promise<Any>?
Link copied to clipboard
suspend fun fetch(options: Resource.FetchOptions? = definedExternally): Any?

Asynchronously loads the given resource. Returns a promise that will resolve to the result once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled. It's recommended that you use the more specific functions eg. fetchJson, fetchBlob, etc.

Link copied to clipboard

Asynchronously loads the resource as raw binary data. Returns a promise that will resolve to an ArrayBuffer once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
fun fetchAsync(options: Resource.FetchOptions? = definedExternally): Promise<Any>?
Link copied to clipboard
suspend fun fetchBlob(): Blob?

Asynchronously loads the given resource as a blob. Returns a promise that will resolve to a Blob once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
Link copied to clipboard
suspend fun fetchImage(options: Resource.FetchImageOptions? = definedExternally): CanvasImageSource?

Asynchronously loads the given image resource. Returns a promise that will resolve to an ImageBitmap if preferImageBitmap is true and the browser supports createImageBitmap or otherwise an Image once loaded, or reject if the image failed to load.

Link copied to clipboard
Link copied to clipboard
suspend fun fetchJson(): Any?

Asynchronously loads the given resource as JSON. Returns a promise that will resolve to a JSON object once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled. This function adds 'Accept: application/json,*/*;q=0.01' to the request headers, if not already specified.

Link copied to clipboard
Link copied to clipboard
suspend fun fetchJsonp(callbackParameterName: String? = definedExternally): Any?

Requests a resource using JSONP.

Link copied to clipboard
fun fetchJsonpAsync(callbackParameterName: String? = definedExternally): Promise<Any>?
Link copied to clipboard
suspend fun fetchText(): String?

Asynchronously loads the given resource as text. Returns a promise that will resolve to a String once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
Link copied to clipboard
suspend fun fetchXML(): XMLDocument?

Asynchronously loads the given resource as XML. Returns a promise that will resolve to an XML Document once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
Link copied to clipboard
fun getBaseUri(includeQuery: Boolean? = definedExternally): String

Returns the base path of the Resource.

Link copied to clipboard

Returns a resource relative to the current instance. All properties remain the same as the current instance unless overridden in options.

Link copied to clipboard
fun getUrlComponent(query: Boolean? = definedExternally, proxy: Boolean? = definedExternally): String

Returns the url, optional with the query string and processed by a proxy.

Link copied to clipboard
suspend fun head(options: Resource.HeadOptions? = definedExternally): Any?

Asynchronously gets headers the given resource. Returns a promise that will resolve to the result once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
fun headAsync(options: Resource.HeadOptions? = definedExternally): Promise<Any>?
Link copied to clipboard
suspend fun options(options: Resource.OptionsOptions? = definedExternally): Any?

Asynchronously gets options the given resource. Returns a promise that will resolve to the result once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
fun optionsAsync(options: Resource.OptionsOptions? = definedExternally): Promise<Any>?
Link copied to clipboard
suspend fun patch(data: Any, options: Resource.PatchOptions? = definedExternally): Any?

Asynchronously patches data to the given resource. Returns a promise that will resolve to the result once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
fun patchAsync(data: Any, options: Resource.PatchOptions? = definedExternally): Promise<Any>?
Link copied to clipboard
suspend fun post(data: Any, options: Resource.PostOptions? = definedExternally): Any?

Asynchronously posts data to the given resource. Returns a promise that will resolve to the result once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
fun postAsync(data: Any, options: Resource.PostOptions? = definedExternally): Promise<Any>?
Link copied to clipboard
suspend fun put(data: Any, options: Resource.PutOptions? = definedExternally): Any?

Asynchronously puts data to the given resource. Returns a promise that will resolve to the result once loaded, or reject if the resource failed to load. The data is loaded using XMLHttpRequest, which means that in order to make requests to another origin, the server must have Cross-Origin Resource Sharing (CORS) headers enabled.

Link copied to clipboard
fun putAsync(data: Any, options: Resource.PutOptions? = definedExternally): Promise<Any>?
Link copied to clipboard
fun setQueryParameters(params: Any, useAsDefault: Boolean? = definedExternally)

Combines the specified object and the existing query parameters. This allows you to add many parameters at once, as opposed to adding them one at a time to the queryParameters property. If a value is already set, it will be replaced with the new value.

Link copied to clipboard
fun setTemplateValues(template: Any, useAsDefault: Boolean? = definedExternally)

Combines the specified object and the existing template values. This allows you to add many values at once, as opposed to adding them one at a time to the templateValues property. If a value is already set, it will become an array and the new value will be appended.