Home Reference Source
import {HttpClient} from '@tilfin/flatout/src/core/http.js'
public class | source

HttpClient

HTTP Client

Constructor Summary

Public Constructor
public

Constructor.

Member Summary

Public Members
public

baseURL: *

public
public

headers: *

Method Summary

Public Methods
public

hook async function called before throw an error.

public

async beforeRequest(path: String, ctx: object)

hook async function called before the request.

public

delete(path: String, ctx: object): Promise: *

do DELETE request.

public

async exec(method: string, path: string, query: object, body: string | object, headers: object): Promise: *

execute http request.

public

get(path: String, ctx: object): Promise: *

do GET request.

public

patch(path: String, ctx: object): Promise: *

do PATCH request.

public

post(path: String, ctx: object): Promise: *

do POST request.

public

put(path: String, ctx: object): Promise: *

do PUT request.

Private Methods
private

_formatParams(params: *): *

private

_fromFormData(body: *): *

private

_request(method: *, path: *, objectPattern2: {"query": *, "body": *, "headers": *}): *

Public Constructors

public constructor(opts: object) source

Constructor.

Params:

NameTypeAttributeDescription
opts object
  • optional

options

opts.baseURL string
  • optional

base URL (default empty).

opts.headers object
  • optional

custom headers

opts.bodyType string
  • optional

post/put type form | json

Public Members

public baseURL: * source

public contentType: string source

public headers: * source

Public Methods

public async beforeError(err: HttpError): Promise<boolean> source

hook async function called before throw an error.

Params:

NameTypeAttributeDescription
err HttpError

request path

Return:

Promise<boolean>

if return false, stop throwing the error.

public async beforeRequest(path: String, ctx: object) source

hook async function called before the request.

Params:

NameTypeAttributeDescription
path String

request path

ctx object
  • optional

context

ctx.query object
  • optional

request query data

ctx.body object
  • optional

request body

ctx.headers object
  • optional

header name and value object

public delete(path: String, ctx: object): Promise: * source

do DELETE request.

Params:

NameTypeAttributeDescription
path String

request path

ctx object
  • optional

context

ctx.query object
  • optional

request query data

ctx.body object
  • optional

request body

ctx.headers object
  • optional

header name and value object

Return:

Promise

Promise resolves response bodystatus: xhr.status,

public async exec(method: string, path: string, query: object, body: string | object, headers: object): Promise: * source

execute http request.

Params:

NameTypeAttributeDescription
method string

method

path string

request path

query object

request query data

body string | object

request body

headers object

header name and value object

Return:

Promise

Promise

public get(path: String, ctx: object): Promise: * source

do GET request.

Params:

NameTypeAttributeDescription
path String

request path

ctx object
  • optional

context

ctx.query object
  • optional

request query data

ctx.body object
  • optional

request body

ctx.headers object
  • optional

header name and value object

Return:

Promise

Promise resolves response bodystatus: xhr.status,

public patch(path: String, ctx: object): Promise: * source

do PATCH request.

Params:

NameTypeAttributeDescription
path String

request path

ctx object
  • optional

context

ctx.query object
  • optional

request query data

ctx.body object
  • optional

request body

ctx.headers object
  • optional

header name and value object

Return:

Promise

Promise resolves response bodystatus: xhr.status,

public post(path: String, ctx: object): Promise: * source

do POST request.

Params:

NameTypeAttributeDescription
path String

request path

ctx object
  • optional

context

ctx.query object
  • optional

request query data

ctx.body object
  • optional

request body

ctx.headers object
  • optional

header name and value object

Return:

Promise

Promise resolves response bodystatus: xhr.status,

public put(path: String, ctx: object): Promise: * source

do PUT request.

Params:

NameTypeAttributeDescription
path String

request path

ctx object
  • optional

context

ctx.query object
  • optional

request query data

ctx.body object
  • optional

request body

ctx.headers object
  • optional

header name and value object

Return:

Promise

Promise resolves response bodystatus: xhr.status,

Private Methods

private _formatParams(params: *): * source

Params:

NameTypeAttributeDescription
params *

Return:

*

private _fromFormData(body: *): * source

Params:

NameTypeAttributeDescription
body *

Return:

*

private _request(method: *, path: *, objectPattern2: {"query": *, "body": *, "headers": *}): * source

Params:

NameTypeAttributeDescription
method *
path *
objectPattern2 {"query": *, "body": *, "headers": *}
  • default: {"query":null,"body":null,"headers":null}

Return:

*