Skip to content

PURISTA API


PURISTA API / @purista/core / HttpClient

Class: HttpClient<CustomConfig>

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

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

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; metricsRecorder?: PuristaMetricsRecorderInterface; name?: string; spanProcessor?: SpanProcessor; traceId?: string } & CustomConfig)[K] }

Returns

HttpClient<CustomConfig>

Properties

auth

protected auth: AuthCredentials

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


baseUrl

baseUrl: URL | undefined = undefined

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


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; metricsRecorder?: PuristaMetricsRecorderInterface; name?: string; spanProcessor?: SpanProcessor; traceId?: string } & CustomConfig)[K] }

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


logger

logger: Logger

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


name

name: string = 'HttpClient'

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


spanProcessor

spanProcessor: SpanProcessor | undefined

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


timeout

timeout: number

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


traceProvider

traceProvider: NodeTracerProvider

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

Methods

delete()

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

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

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

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

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

Returns open telemetry tracer of this service

Returns

Tracer

Tracer


getUrlAndHeader()

protected getUrlAndHeader(path, options?): object

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

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

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

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

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

Set the bearer token for all following requests.

Parameters

token

string | undefined

the bearer token

Returns

void

Implementation of

RestClient.setBearerToken


startActiveSpan()

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

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

Start a child span for opentelemetry tracking

Type Parameters

F

F

Parameters

name

string

name of span

opts

SpanOptions

span options

context

Context | undefined

optional context

fn

(span) => Promise<F>

function to be executed within the span

Returns

Promise<F>

return value of fn