Skip to content

PURISTA API


PURISTA API / @purista/core / HttpClient

Class: HttpClient<CustomConfig>

Defined in: HttpClient/HttpClient.impl.ts:40

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')

Extended by

Type Parameters

CustomConfig

CustomConfig extends Record<string, unknown> = EmptyObject

Implements

Constructors

Constructor

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

Defined in: HttpClient/HttpClient.impl.ts:53

Parameters

config

{ [K in string | number | symbol]: ({ baseUrl?: string; basicAuth?: { password: string; username: string }; bearerToken?: string; defaultHeaders?: Record<string, string>; defaultTimeout?: number; enableOpentelemetry?: boolean; isKeepAlive?: boolean; logger?: Logger; logLevel?: LogLevelName; name?: string; spanProcessor?: SpanProcessor; traceId?: string } & CustomConfig)[K] }

Returns

HttpClient<CustomConfig>

Properties

auth

protected auth: AuthCredentials

Defined in: HttpClient/HttpClient.impl.ts:52


baseUrl

baseUrl: URL | undefined = undefined

Defined in: HttpClient/HttpClient.impl.ts:47


config

config: { [K in string | number | symbol]: ({ baseUrl?: string; basicAuth?: { password: string; username: string }; bearerToken?: string; defaultHeaders?: Record<string, string>; defaultTimeout?: number; enableOpentelemetry?: boolean; isKeepAlive?: boolean; logger?: Logger; logLevel?: LogLevelName; name?: string; spanProcessor?: SpanProcessor; traceId?: string } & CustomConfig)[K] }

Defined in: HttpClient/HttpClient.impl.ts:43


logger

logger: Logger

Defined in: HttpClient/HttpClient.impl.ts:42


name

name: string = 'HttpClient'

Defined in: HttpClient/HttpClient.impl.ts:41


spanProcessor

spanProcessor: SpanProcessor | undefined

Defined in: HttpClient/HttpClient.impl.ts:49


timeout

timeout: number

Defined in: HttpClient/HttpClient.impl.ts:45


traceProvider

traceProvider: NodeTracerProvider

Defined in: HttpClient/HttpClient.impl.ts:50

Methods

delete()

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

Defined in: HttpClient/HttpClient.impl.ts:330

DELETE request

Type Parameters

T

T

Parameters

path

string

options?

HttpClientRequestOptions

payload?

unknown

Returns

Promise<T>

Implementation of

RestClient.delete


execute()

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

Defined in: HttpClient/HttpClient.impl.ts:194

Helper method

Parameters

method

string

path

string

options?

HttpClientRequestOptions

payload?

unknown

Returns

Promise<any>

Throws

UnhandledError


get()

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

Defined in: HttpClient/HttpClient.impl.ts:290

GET request

Type Parameters

T

T

Parameters

path

string

options?

HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.get


getTracer()

getTracer(): Tracer

Defined in: HttpClient/HttpClient.impl.ts:95

Returns open telemetry tracer of this service

Returns

Tracer

Tracer


getUrlAndHeader()

protected getUrlAndHeader(path, options?): object

Defined in: HttpClient/HttpClient.impl.ts:142

Parameters

path

string

options?

HttpClientRequestOptions

Returns

object

headers

headers: Record<string, string>

url

url: URL


patch()

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

Defined in: HttpClient/HttpClient.impl.ts:320

PATCH request

Type Parameters

T

T

Parameters

path

string

payload

unknown

options?

HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.patch


post()

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

Defined in: HttpClient/HttpClient.impl.ts:300

POST request

Type Parameters

T

T

Parameters

path

string

payload

unknown

options?

HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.post


put()

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

Defined in: HttpClient/HttpClient.impl.ts:310

PUT request

Type Parameters

T

T

Parameters

path

string

payload

unknown

options?

HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.put


setBearerToken()

setBearerToken(token): void

Defined in: HttpClient/HttpClient.impl.ts:181

Set the bearer token for all following requests.

Parameters

token

the bearer token

string | undefined

Returns

void

Implementation of

RestClient.setBearerToken


startActiveSpan()

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

Defined in: HttpClient/HttpClient.impl.ts:107

Start a child span for opentelemetry tracking

Type Parameters

F

F

Parameters

name

string

name of span

opts

SpanOptions

span options

context

optional context

Context | undefined

fn

(span) => Promise<F>

function to be executed within the span

Returns

Promise<F>

return value of fn