Documentation / @zerotal/core / http / Http
Class: Http
Defined in: http/Http.ts:47
Fluent facade for outgoing (server-to-server) HTTP requests. Static verb
methods (Http.get, Http.post, …) return a PendingRequest
that can be awaited directly or chained (.json(), .text()). Prefix a request
with configuration (Http.withHeaders, Http.withToken,
Http.timeout, Http.retry) to apply it before the verb.
The fake/assert* helpers intercept requests in tests so no real network
traffic occurs.
Example
// GET with a custom header, retry, and timeout
const users = await Http
.withHeaders({ "X-Trace": "abc" })
.retry(3, 200) // up to 3 attempts, 200ms apart
.timeout(5000) // abort after 5s
.get("https://api.example.com/users")
.json();
// POST a JSON body with a bearer token
const created = await Http
.withToken("secret")
.post("https://api.example.com/users", { name: "Alice" })
.json();
// Testing — intercept requests
Http.fake([{ url: "https://api.example.com/*", body: { ok: true } }]);
await Http.get("https://api.example.com/users");
Http.assertSent((req) => req.url.includes("/users"));
Http.resetFakes();
Constructors
Constructor
new Http():
Http
Returns
Http
Configuration
withHeaders()
staticwithHeaders(headers):_Builder
Defined in: http/Http.ts:55
Start a request with the given headers applied.
Returns a builder whose get/post/etc. methods issue the actual request.
Parameters
headers
Record<string, string>
Returns
_Builder
withToken()
staticwithToken(token):_Builder
Defined in: http/Http.ts:63
Set a Bearer token on the next request.
Parameters
token
string
Returns
_Builder
withBasicAuth()
staticwithBasicAuth(username,password):_Builder
Defined in: http/Http.ts:71
Set HTTP Basic Auth credentials.
Parameters
username
string
password
string
Returns
_Builder
Requests
get()
staticget(url):PendingRequest
Defined in: http/Http.ts:97
Begin a GET request to url.
Parameters
url
string
Returns
post()
staticpost(url,data?):PendingRequest
Defined in: http/Http.ts:105
Begin a POST request to url, attaching data as a JSON body when given.
Parameters
url
string
data?
unknown
Returns
put()
staticput(url,data?):PendingRequest
Defined in: http/Http.ts:115
Begin a PUT request to url, attaching data as a JSON body when given.
Parameters
url
string
data?
unknown
Returns
patch()
staticpatch(url,data?):PendingRequest
Defined in: http/Http.ts:125
Begin a PATCH request to url, attaching data as a JSON body when given.
Parameters
url
string
data?
unknown
Returns
delete()
staticdelete(url):PendingRequest
Defined in: http/Http.ts:135
Begin a DELETE request to url.
Parameters
url
string
Returns
head()
statichead(url):PendingRequest
Defined in: http/Http.ts:143
Begin a HEAD request to url.
Parameters
url
string
Returns
Retries & resilience
timeout()
statictimeout(milliseconds):_Builder
Defined in: http/Http.ts:79
Set the request timeout in milliseconds.
Parameters
milliseconds
number
Returns
_Builder
retry()
staticretry(times,delay?):_Builder
Defined in: http/Http.ts:87
Retry up to times times with optional delay.
Parameters
times
number
delay?
number
Returns
_Builder
Testing
fake()
staticfake(stubs?):void
Defined in: http/Http.ts:161
Intercept all outgoing HTTP requests with the given stubs. No real HTTP requests will be made while fakes are active.
Parameters
stubs?
FakeStub[] = ...
Array of URL-response pairs. Use '*' to match any URL.
Returns
void
Example
Http.fake([
{ url: 'https://api.example.com/users', body: [{ id: 1 }] },
{ url: '*', status: 503 },
]);
resetFakes()
staticresetFakes():void
Defined in: http/Http.ts:169
Remove all fake stubs and restore real HTTP behaviour.
Returns
void
recorded()
staticrecorded():object[]
Defined in: http/Http.ts:177
Return all requests recorded since Http.fake() was called.
Returns
object[]
assertSent()
staticassertSent(predicate):void
Defined in: http/Http.ts:186
Assert that at least one recorded request matches the predicate.
Parameters
predicate
(req) => boolean
Returns
void
Throws
when no recorded request matches.
assertNotSent()
staticassertNotSent(predicate):void
Defined in: http/Http.ts:206
Assert that no recorded request matches the predicate.
Parameters
predicate
(req) => boolean
Returns
void
Throws
when a recorded request matches.
assertSentCount()
staticassertSentCount(count):void
Defined in: http/Http.ts:220
Assert that exactly count requests were recorded.
Parameters
count
number
Returns
void
Throws
when the recorded count differs from count.
assertNothingSent()
staticassertNothingSent():void
Defined in: http/Http.ts:232
Assert that no requests were recorded.
Returns
void
Throws
when any request was recorded.