Home Reference Source Repository
import OlapicRestClient from 'OlapicDevKit-ES6/src/tools/rest.js'
public class | source

OlapicRestClient

Handles all the connections between DevKit and the Olapic API. This class is instantiated by the DevKit singletion and saved on the rest property.

See:

Constructor Summary

Public Constructor
public

Rest client class constructor.

Member Summary

Public Members
public

A flag to determine if it should show debug messages on the console.

public

A flag to determine if the urls on the pre cache should be cleaned from the Olapic CDN sharding pattern.

public

This object will store all the pre cache entries using their urls as key.

public

A list of the embedded properties that can be pre cached.

public

An extra validation for the embedded properties that can be pre cached.

public

A flag to determine if the pre cache it's enabled.

public

For the pre cache to be executed after a request, the endpoint has to match this expression.

public

A list of entities which names will be replaced when they are retrieved from the pre cache.

public

A regex used to remove the sharding pattern from the pre cache urls.

Method Summary

Public Methods
public

Removes all the entries from the pre cache.

public

get(url: string, query: Object, headers: Object): Promise<Object, Error>

Makes a GET request to the API.

public

Get access to the fetch object that the rest client uses for every request to the API.

public

getOption(name: string): *

Get the value of one of the editable options.

public

Gets a list of base headers to be included on every request.

public

Gets a list of base parameters that are included on every request query string.

public

post(url: string, data: Object, query: Object, headers: Object): Promise<Object, Error>

Makes a POST request to the API.

public

Set a fetch object to be used for the API requests.

public

setOption(name: string, value: *, append: boolean): *

Change the value of one of th editable options of the class.

public

setOptions(options: Object): void

A shortcut to call {setOption} for a list of options.

public

Sets a list of headers to be included on every request.

public

Sets a list of parameters to be included on every request query string.

public

This method is for when the embedded properties are used from the raw response and should be removed from the pre cache.

Public Constructors

public constructor source

Rest client class constructor.

Public Members

public debug: boolean source

A flag to determine if it should show debug messages on the console.

public filterSharding: boolean source

A flag to determine if the urls on the pre cache should be cleaned from the Olapic CDN sharding pattern.

public preCache: Object source

This object will store all the pre cache entries using their urls as key.

public preCacheEmbeddedProperties: Array source

A list of the embedded properties that can be pre cached.

public preCacheEmbeddedPropertiesPerEndpoint: Object source

An extra validation for the embedded properties that can be pre cached. This is optional, and if a property is listed, the main endpoint (where the request was made) should match against the specified expression.

Example:

.preCacheEmbeddedPropertiesPerEndpoint = {
    'streams:all': /\/media\/[0-9]+\?/g,
}

public preCacheEnabled: boolean source

A flag to determine if the pre cache it's enabled.

public preCacheEndpoints: RegExp source

For the pre cache to be executed after a request, the endpoint has to match this expression.

public preCacheEntityNamesReplacement: Object source

A list of entities which names will be replaced when they are retrieved from the pre cache.

Example:

.preCacheEntityNamesReplacement = {
    base_image: 'media',
    cover_media: 'media',
}

public shardingPattern: RegExp source

A regex used to remove the sharding pattern from the pre cache urls.

Public Methods

public cleanCache(): Array source

Removes all the entries from the pre cache.

Return:

Array

A list of the urls that were removed.

public get(url: string, query: Object, headers: Object): Promise<Object, Error> source

Makes a GET request to the API.

Params:

NameTypeAttributeDescription
url string

The API url.

query Object
  • optional
  • default: {}

A list of parameters for the query string.

headers Object
  • optional
  • default: {}

A list of headers for the request.

Return:

Promise<Object, Error>

If everything goes well, it will return the response.

Example:

.get('/media/12', {auth_token: 'abc'}).then((response) => {
    console.log(response);
});

public getFetchObject(): Object source

Get access to the fetch object that the rest client uses for every request to the API.

Return:

Object

If no object was injected with {setFetchObject}, it will return the native fetch object.

public getOption(name: string): * source

Get the value of one of the editable options.

Params:

NameTypeAttributeDescription
name string

The option name.

Return:

*

The option value.

TODO:

  • Remove this and replace the editable options with simple properties and/or getters and setters.

public getRequestBaseHeaders(): Object source

Gets a list of base headers to be included on every request.

Return:

Object

The list of headers and their values.

public getRequestBaseQuery(): Object source

Gets a list of base parameters that are included on every request query string.

Return:

Object

The list of parameters for every request query string.

public post(url: string, data: Object, query: Object, headers: Object): Promise<Object, Error> source

Makes a POST request to the API.

Params:

NameTypeAttributeDescription
url string

The API url.

data Object
  • optional
  • default: {}

A list of form fields to post as the body of the request.

query Object
  • optional
  • default: {}

A list of parameters for the query string.

headers Object
  • optional
  • default: {}

A list of headers for the request.

Return:

Promise<Object, Error>

If everything goes well, it will return the response.

Example:

.post('/media/12/report', {reason: '...'}, {auth_token: 'abc'}).then((response) => {
    console.log(response);
});

public setFetchObject(obj: Object) source

Set a fetch object to be used for the API requests. This is intended to inject a stub that can be used for unit testing.

Params:

NameTypeAttributeDescription
obj Object

The new fetch object.

public setOption(name: string, value: *, append: boolean): * source

Change the value of one of th editable options of the class.

Params:

NameTypeAttributeDescription
name string

The option name.

value *

The option new value.

append boolean
  • optional
  • default: false

If true and both the current and new value are Object, they'll be merged.

Return:

*

TODO:

  • Remove this and replace the editable options with simple properties and/or getters and setters.

public setOptions(options: Object): void source

A shortcut to call {setOption} for a list of options.

Params:

NameTypeAttributeDescription
options Object

A dictionary with the options to edit and their values.

Return:

void

Example:

.setOptions({
    option1: 'value1',
    option2: 'value2',
});

TODO:

  • Remove this and replace the editable options with simple properties and/or getters and setters.

public setRequestBaseHeaders(headers: Object) source

Sets a list of headers to be included on every request.

Params:

NameTypeAttributeDescription
headers Object
  • optional
  • default: {}

A list of headers and their values.

public setRequestBaseQuery(query: Object) source

Sets a list of parameters to be included on every request query string.

Params:

NameTypeAttributeDescription
query Object
  • optional
  • default: {}

The list of parameters and their values.

public useCache(url: string): boolean source

This method is for when the embedded properties are used from the raw response and should be removed from the pre cache.

Params:

NameTypeAttributeDescription
url string

The url for the entry.

Return:

boolean

Whether there was an entry or not.