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
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
protectedauth: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?
payload?
unknown
Returns
Promise<T>
Implementation of
execute()
protectedexecute(method,path,options?,payload?):Promise<any>
Defined in: HttpClient/HttpClient.impl.ts:194
Helper method
Parameters
method
string
path
string
options?
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?
Returns
Promise<T>
Implementation of
getTracer()
getTracer():
Tracer
Defined in: HttpClient/HttpClient.impl.ts:95
Returns open telemetry tracer of this service
Returns
Tracer
Tracer
getUrlAndHeader()
protectedgetUrlAndHeader(path,options?):object
Defined in: HttpClient/HttpClient.impl.ts:142
Parameters
path
string
options?
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?
Returns
Promise<T>
Implementation of
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?
Returns
Promise<T>
Implementation of
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?
Returns
Promise<T>
Implementation of
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
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
