Home Reference Source
import Api from 'rest-api-handler/src/Api.js'
public class | source

Api

Class for handling responses and requests.

Static Member Summary

Static Public Members
public static

FORMATS: {JSON_FORMAT: string, FORM_DATA_FORMAT: string}

List of formatter you can use to process content of body request.

Static Method Summary

Static Public Methods
public static

convertData(data: Object, to: Format): string | FormData

Convert data in object to format of Fetch body.

public static

Convert object to url parameters string.

Constructor Summary

Public Constructor
public

constructor(apiUrl: string, processors: Array<ProcessorAdapter>, defaultHeaders: Object, defaultOptions: RequestOptions)

Constructor.

Member Summary

Public Members
public

Base api url

public

Base http headers

public

defaultOptions: RequestOptions

Base settings for Fetch Request

public

List of processors that parse response from server.

Method Summary

Public Methods
public

delete(namespace: string, headers: Object): Promise<ProcessedResponse>

Send a DELETE request.

public

get(namespace: string, parameters: Object, headers: Object): Promise<ProcessedResponse>

Send a GET request.

public

getDefaultHeaders(): Headers

Get default headers.

public

post(namespace: string, data: Object, format: Format, headers: Object): Promise<ProcessedResponse>

Send a POST request.

public

put(namespace: string, data: Object, format: Format, headers: Object): Promise<ProcessedResponse>

Send a PUT request.

public

Remove default header.

public

request(namespace: string, method: MethodType, options: RequestOptions, headers: Object): Promise<ProcessedResponse>

Request given API endpoint.

public

setDefaultHeader(name: string, value: string): void

Add default HTTP header.

public

setDefaultHeaders(headers: Headers): void

Set default headers.

Protected Methods
protected

fetchRequest(request: Request): Promise<Response>

Fetch API url.

protected

requestWithBody(namespace: string, method: MethodType, data: Object, format: Format, headers: Object): Promise<ProcessedResponse>

Send a request with body.

Static Public Members

public static FORMATS: {JSON_FORMAT: string, FORM_DATA_FORMAT: string} source

List of formatter you can use to process content of body request.

Static Public Methods

public static convertData(data: Object, to: Format): string | FormData source

Convert data in object to format of Fetch body.

Params:

NameTypeAttributeDescription
data Object

Data to convert

to Format

Format to which convert the data. Default is JSON.

Return:

string | FormData

Converted data

Example:

const body = Api.convertData({ a: 'b' }, Api.FORMATS.JSON_FORMAT);
// output is {"a":"b"}  

public static convertParametersToUrl(parameters: Object): string source

Convert object to url parameters string.

Params:

NameTypeAttributeDescription
parameters Object

List of parameters

Return:

string

Encoded string with ? prefix and variables separated by &

Example:

const parameters = Api.convertData({ a: '%b%' });
// output is ?a=%25b%25  

Public Constructors

public constructor(apiUrl: string, processors: Array<ProcessorAdapter>, defaultHeaders: Object, defaultOptions: RequestOptions) source

Constructor.

Params:

NameTypeAttributeDescription
apiUrl string

Base api url

processors Array<ProcessorAdapter>

List of processors that parse response from server.

defaultHeaders Object

Base settings for Fetch Request

defaultOptions RequestOptions

List of processors that parse response from server.

Public Members

public apiUrl: string source

Base api url

public defaultHeaders: Object source

Base http headers

public defaultOptions: RequestOptions source

Base settings for Fetch Request

public processors: Array source

List of processors that parse response from server.

Public Methods

public delete(namespace: string, headers: Object): Promise<ProcessedResponse> source

Send a DELETE request.

Params:

NameTypeAttributeDescription
namespace string

Api endpoint

headers Object

custom headers

Return:

Promise<ProcessedResponse>

Processed response

public get(namespace: string, parameters: Object, headers: Object): Promise<ProcessedResponse> source

Send a GET request.

Params:

NameTypeAttributeDescription
namespace string

api endpoint

parameters Object

get parameters

headers Object

custom headers

Return:

Promise<ProcessedResponse>

processed response

Example:

const { data } = await api.get('brand', { id: 5 })
// will call YOUR_URI/brand?id=5
console.log(data);  

public getDefaultHeaders(): Headers source

Get default headers.

Return:

Headers

Get Default headers

public post(namespace: string, data: Object, format: Format, headers: Object): Promise<ProcessedResponse> source

Send a POST request.

Params:

NameTypeAttributeDescription
namespace string

Api endpoint

data Object

Request object

format Format
  • nullable: true

Format of body request

headers Object

custom headers

Return:

Promise<ProcessedResponse>

Processed response

public put(namespace: string, data: Object, format: Format, headers: Object): Promise<ProcessedResponse> source

Send a PUT request.

Params:

NameTypeAttributeDescription
namespace string

Api endpoint

data Object

Request object

format Format
  • nullable: true

Format of body request

headers Object

custom headers

Return:

Promise<ProcessedResponse>

Processed response

public removeDefaultHeader(name: string): void source

Remove default header.

Params:

NameTypeAttributeDescription
name string

Name of header

Return:

void

public request(namespace: string, method: MethodType, options: RequestOptions, headers: Object): Promise<ProcessedResponse> source

Request given API endpoint.

Params:

NameTypeAttributeDescription
namespace string

Api endpoint or full url

method MethodType

Request method eg. POST or GET

options RequestOptions

Fetch options

headers Object

Custom headers

Return:

Promise<ProcessedResponse>

processed response

Example:

const { data } = await api.request('ad', 'POST', {
    body: '{"ad":1}'
})

const { data } = await api.request('http://i-can-request-full-url.com/?a=b', 'GET')  

public setDefaultHeader(name: string, value: string): void source

Add default HTTP header.

Params:

NameTypeAttributeDescription
name string

Name of header

value string

Value for header

Return:

void

Example:

api.setDefaultHeader('content-type', 'application/json');  

public setDefaultHeaders(headers: Headers): void source

Set default headers.

Params:

NameTypeAttributeDescription
headers Headers

HTTP headers

Return:

void

Protected Methods

protected fetchRequest(request: Request): Promise<Response> source

Fetch API url.

Params:

NameTypeAttributeDescription
request Request

Fetch request

Return:

Promise<Response>

Fetch response

protected requestWithBody(namespace: string, method: MethodType, data: Object, format: Format, headers: Object): Promise<ProcessedResponse> source

Send a request with body.

Params:

NameTypeAttributeDescription
namespace string

api endpoint

method MethodType

api method

data Object

body JSON parameters

format Format

format of body request

headers Object

custom headers

Return:

Promise<ProcessedResponse>

processed response