Skip to main content

Class: HttpClient<CustomConfig>


PURISTA API / Modules / @purista/core / HttpClient

Class: HttpClient<CustomConfig>

@purista/core.HttpClient

A HTTP client which will provide simple methods for GET, POST, PATCH, PUT and DELETE. Body payload will be handled as JSON requests It includes timeout and error handling and simple json response body parsing

Example

const client = new HttpClient({baseUrl: 'http://localhost/api})

// GET http://localhost/api/v1/orders
const result = await client.get('v1/orders')

Type parameters

NameType
CustomConfigextends Record<string, unknown> = {}

Implements

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new HttpClient<CustomConfig>(config): HttpClient<CustomConfig>

Type parameters

NameType
CustomConfigextends Record<string, unknown> = {}

Parameters

NameType
config{ [K in string | number | symbol]: (Object & CustomConfig)[K] }

Returns

HttpClient<CustomConfig>

Defined in

HttpClient/HttpClient.impl.ts:37open in new window

Properties

auth

Private auth: AuthCredentials

Defined in

HttpClient/HttpClient.impl.ts:36open in new window


baseUrl

baseUrl: URL

Defined in

HttpClient/HttpClient.impl.ts:31open in new window


config

config: { [K in string | number | symbol]: (Object & CustomConfig)[K] }

Defined in

HttpClient/HttpClient.impl.ts:27open in new window


logger

logger: Logger

Defined in

HttpClient/HttpClient.impl.ts:26open in new window


name

name: string = 'HttpClient'

Defined in

HttpClient/HttpClient.impl.ts:25open in new window


spanProcessor

spanProcessor: undefined | SpanProcessor

Defined in

HttpClient/HttpClient.impl.ts:33open in new window


timeout

timeout: number

Defined in

HttpClient/HttpClient.impl.ts:29open in new window


traceProvider

traceProvider: NodeTracerProvider

Defined in

HttpClient/HttpClient.impl.ts:34open in new window

Methods

delete

delete<T>(path, options?, payload?): Promise<T>

DELETE request

Type parameters

Name
T

Parameters

NameType
pathstring
options?HttpClientRequestOptions
payload?unknown

Returns

Promise<T>

Implementation of

RestClient.delete

Defined in

HttpClient/HttpClient.impl.ts:318open in new window


execute

execute(method, path, options?, payload?): Promise<any>

Helper method

Parameters

NameType
methodstring
pathstring
options?HttpClientRequestOptions
payload?unknown

Returns

Promise<any>

Throws

UnhandledError

Defined in

HttpClient/HttpClient.impl.ts:179open in new window


get

get<T>(path, options?): Promise<T>

GET request

Type parameters

Name
T

Parameters

NameType
pathstring
options?HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.get

Defined in

HttpClient/HttpClient.impl.ts:278open in new window


getTracer

getTracer(): Tracer

Returns open telemetry tracer of this service

Returns

Tracer

Tracer

Defined in

HttpClient/HttpClient.impl.ts:80open in new window


getUrlAndHeader

getUrlAndHeader(path, options?): Object

Parameters

NameType
pathstring
options?HttpClientRequestOptions

Returns

Object

NameType
headersRecord<string, string>
urlURL

Defined in

HttpClient/HttpClient.impl.ts:127open in new window


patch

patch<T>(path, payload, options?): Promise<T>

PATCH request

Type parameters

Name
T

Parameters

NameType
pathstring
payloadunknown
options?HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.patch

Defined in

HttpClient/HttpClient.impl.ts:308open in new window


post

post<T>(path, payload, options?): Promise<T>

POST request

Type parameters

Name
T

Parameters

NameType
pathstring
payloadunknown
options?HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.post

Defined in

HttpClient/HttpClient.impl.ts:288open in new window


put

put<T>(path, payload, options?): Promise<T>

PUT request

Type parameters

Name
T

Parameters

NameType
pathstring
payloadunknown
options?HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.put

Defined in

HttpClient/HttpClient.impl.ts:298open in new window


setBearerToken

setBearerToken(token): void

Set the bearer token for all following requests.

Parameters

NameTypeDescription
tokenundefined | stringthe bearer token

Returns

void

Implementation of

RestClient.setBearerToken

Defined in

HttpClient/HttpClient.impl.ts:166open in new window


startActiveSpan

startActiveSpan<F>(name, opts, context, fn): Promise<F>

Start a child span for opentelemetry tracking

Type parameters

Name
F

Parameters

NameTypeDescription
namestringname of span
optsSpanOptionsspan options
contextundefined | Contextoptional context
fn(span: Span) => Promise<F>function to be executed within the span

Returns

Promise<F>

return value of fn

Defined in

HttpClient/HttpClient.impl.ts:92open in new window

Last update:
Contributors: Sebastian Wessel