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
const client = new HttpClient({baseUrl: 'http://localhost/api})
// GET http://localhost/api/v1/orders
const result = await client.get('v1/orders')
Type parameters
Name | Type |
---|---|
CustomConfig | extends Record <string , unknown > = {} |
Hierarchy
HttpClient
Implements
Table of contents
Constructors
Properties
Methods
Constructors
constructor
• new HttpClient<CustomConfig
>(config
): HttpClient
<CustomConfig
>
Type parameters
Name | Type |
---|---|
CustomConfig | extends Record <string , unknown > = {} |
Parameters
Name | Type |
---|---|
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
Name | Type |
---|---|
path | string |
options? | HttpClientRequestOptions |
payload? | unknown |
Returns
Promise
<T
>
Implementation of
Defined in
HttpClient/HttpClient.impl.ts:319
execute
▸ execute(method
, path
, options?
, payload?
): Promise
<any
>
Helper method
Parameters
Name | Type |
---|---|
method | string |
path | string |
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
Name | Type |
---|---|
path | string |
options? | HttpClientRequestOptions |
Returns
Promise
<T
>
Implementation of
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
Name | Type |
---|---|
path | string |
options? | HttpClientRequestOptions |
Returns
Object
Name | Type |
---|---|
headers | Record <string , string > |
url | URL |
Defined in
HttpClient/HttpClient.impl.ts:131
patch
▸ patch<T
>(path
, payload
, options?
): Promise
<T
>
PATCH request
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
path | string |
payload | unknown |
options? | HttpClientRequestOptions |
Returns
Promise
<T
>
Implementation of
Defined in
HttpClient/HttpClient.impl.ts:309
post
▸ post<T
>(path
, payload
, options?
): Promise
<T
>
POST request
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
path | string |
payload | unknown |
options? | HttpClientRequestOptions |
Returns
Promise
<T
>
Implementation of
Defined in
HttpClient/HttpClient.impl.ts:289
put
▸ put<T
>(path
, payload
, options?
): Promise
<T
>
PUT request
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
path | string |
payload | unknown |
options? | HttpClientRequestOptions |
Returns
Promise
<T
>
Implementation of
Defined in
HttpClient/HttpClient.impl.ts:299
setBearerToken
▸ setBearerToken(token
): void
Set the bearer token for all following requests.
Parameters
Name | Type | Description |
---|---|---|
token | undefined | string | the bearer token |
Returns
void
Implementation of
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
Name | Type | Description |
---|---|---|
name | string | name of span |
opts | SpanOptions | span options |
context | undefined | Context | optional context |
fn | (span : Span ) => Promise <F > | function to be executed within the span |
Returns
Promise
<F
>
return value of fn