Skip to content

@purista/core v2.0.5


PURISTA API / @purista/core / HttpClient

Class: HttpClient<CustomConfig>

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:33

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 extends Record<string, unknown> = EmptyObject

Implements

Constructors

new HttpClient()

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

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:46

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: packages/core/src/HttpClient/HttpClient.impl.ts:45


baseUrl

baseUrl: URL

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:40


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: packages/core/src/HttpClient/HttpClient.impl.ts:36


logger

logger: Logger

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:35


name

name: string = 'HttpClient'

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:34


spanProcessor

spanProcessor: undefined | SpanProcessor

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:42


timeout

timeout: number

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:38


traceProvider

traceProvider: NodeTracerProvider

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:43

Methods

delete()

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

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:324

DELETE request

Type Parameters

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: packages/core/src/HttpClient/HttpClient.impl.ts:188

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: packages/core/src/HttpClient/HttpClient.impl.ts:284

GET request

Type Parameters

T

Parameters

path

string

options?

HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.get


getTracer()

getTracer(): Tracer

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:89

Returns open telemetry tracer of this service

Returns

Tracer

Tracer


getUrlAndHeader()

protected getUrlAndHeader(path, options?): object

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:136

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: packages/core/src/HttpClient/HttpClient.impl.ts:314

PATCH request

Type Parameters

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: packages/core/src/HttpClient/HttpClient.impl.ts:294

POST request

Type Parameters

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: packages/core/src/HttpClient/HttpClient.impl.ts:304

PUT request

Type Parameters

T

Parameters

path

string

payload

unknown

options?

HttpClientRequestOptions

Returns

Promise<T>

Implementation of

RestClient.put


setBearerToken()

setBearerToken(token): void

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:175

Set the bearer token for all following requests.

Parameters

token

the bearer token

undefined | string

Returns

void

Implementation of

RestClient.setBearerToken


startActiveSpan()

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

Defined in: packages/core/src/HttpClient/HttpClient.impl.ts:101

Start a child span for opentelemetry tracking

Type Parameters

F

Parameters

name

string

name of span

opts

SpanOptions

span options

context

optional context

undefined | Context

fn

(span) => Promise<F>

function to be executed within the span

Returns

Promise<F>

return value of fn