Resource

open external class Resource(options: JsAny)(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: JsAny)

Types

Link copied to clipboard
external object Companion
Link copied to clipboard
external interface ConstructorOptions

Initialization options for the Resource constructor

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

Properties

Link copied to clipboard
external val extension: String

The file extension of the resource.

Link copied to clipboard
external var hasHeaders: Boolean

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

Link copied to clipboard
external var headers: JsAny

Additional HTTP headers that will be sent with the request.

Link copied to clipboard
external var isBlobUri: Boolean

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
external var isDataUri: Boolean

True if the Resource refers to a data URI.

Link copied to clipboard
external var proxy: Proxy

A proxy to be used when loading the resource.

Link copied to clipboard
external val queryParameters: JsAny

Query parameters appended to the url.

Link copied to clipboard
external var request: Request

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

Link copied to clipboard
external var retryAttempts: Int

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

Link copied to clipboard
external var retryCallback: Function<*>

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
external val templateValues: JsAny

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

Link copied to clipboard
external 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
external fun appendForwardSlash()

Appends a forward slash to the URL.

Link copied to clipboard
external fun appendQueryParameters(params: JsAny)

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
external fun clone(result: Resource? = definedExternally): Resource

Duplicates a Resource instance.

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

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
external fun deleteAsync(options: Resource.DeleteOptions? = definedExternally): Promise<JsAny>?
Link copied to clipboard
suspend external fun fetch(options: Resource.FetchOptions? = definedExternally): JsAny?

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
suspend external fun fetchArrayBuffer(): ArrayBuffer?

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
Link copied to clipboard
external fun fetchAsync(options: Resource.FetchOptions? = definedExternally): Promise<JsAny>?
Link copied to clipboard
suspend external 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
external fun fetchBlobAsync(): Promise<Blob>?
Link copied to clipboard
suspend external 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
external fun fetchImageAsync(options: Resource.FetchImageOptions? = definedExternally): Promise<CanvasImageSource>?
Link copied to clipboard
suspend external fun fetchJson(): JsAny?

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
external fun fetchJsonAsync(): Promise<JsAny>?
Link copied to clipboard
suspend external fun fetchJsonp(callbackParameterName: String? = definedExternally): JsAny?

Requests a resource using JSONP.

Link copied to clipboard
external fun fetchJsonpAsync(callbackParameterName: String? = definedExternally): Promise<JsAny>?
Link copied to clipboard
suspend external 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
external fun fetchTextAsync(): Promise<JsString>?
Link copied to clipboard
suspend external 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
external 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
external 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 external fun head(options: Resource.HeadOptions? = definedExternally): JsAny?

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
external fun headAsync(options: Resource.HeadOptions? = definedExternally): Promise<JsAny>?
Link copied to clipboard
suspend external fun options(options: Resource.OptionsOptions? = definedExternally): JsAny?

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
external fun optionsAsync(options: Resource.OptionsOptions? = definedExternally): Promise<JsAny>?
Link copied to clipboard
suspend external fun patch(data: JsAny, options: Resource.PatchOptions? = definedExternally): JsAny?

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
external fun patchAsync(data: JsAny, options: Resource.PatchOptions? = definedExternally): Promise<JsAny>?
Link copied to clipboard
suspend external fun post(data: JsAny, options: Resource.PostOptions? = definedExternally): JsAny?

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
external fun postAsync(data: JsAny, options: Resource.PostOptions? = definedExternally): Promise<JsAny>?
Link copied to clipboard
suspend external fun put(data: JsAny, options: Resource.PutOptions? = definedExternally): JsAny?

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
external fun putAsync(data: JsAny, options: Resource.PutOptions? = definedExternally): Promise<JsAny>?
Link copied to clipboard
external fun setQueryParameters(params: JsAny, 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
external fun setTemplateValues(template: JsAny, 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.