Skip to content

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

typescript
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> = {}

Hierarchy

  • HttpClient

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:41

Properties

auth

Protected auth: AuthCredentials

Defined in

HttpClient/HttpClient.impl.ts:40


baseUrl

baseUrl: URL

Defined in

HttpClient/HttpClient.impl.ts:35


config

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

Defined in

HttpClient/HttpClient.impl.ts:31


logger

logger: Logger

Defined in

HttpClient/HttpClient.impl.ts:30


name

name: string = 'HttpClient'

Defined in

HttpClient/HttpClient.impl.ts:29


spanProcessor

spanProcessor: undefined | SpanProcessor

Defined in

HttpClient/HttpClient.impl.ts:37


timeout

timeout: number

Defined in

HttpClient/HttpClient.impl.ts:33


traceProvider

traceProvider: NodeTracerProvider

Defined in

HttpClient/HttpClient.impl.ts:38

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:319


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:183


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:279


getTracer

getTracer(): Tracer

Returns open telemetry tracer of this service

Returns

Tracer

Tracer

Defined in

HttpClient/HttpClient.impl.ts:84


getUrlAndHeader

getUrlAndHeader(path, options?): Object

Parameters

NameType
pathstring
options?HttpClientRequestOptions

Returns

Object

NameType
headersRecord<string, string>
urlURL

Defined in

HttpClient/HttpClient.impl.ts:131


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:309


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:289


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:299


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:170


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:96