HTTP (Http)
Lightweight fetch helpers: timeout, retry, query string, FormData.
When to use
- List/search:
Http.get(url, { query })to avoid manual URL - Avoid freeze:
timeoutMsfor auto timeout with friendly UI - Retry transient errors:
retry: { times, delay } - Form upload:
toFormData(obj)for quickFormData - Any Promise:
withTimeout(promise, ms) - Unified errors: non‑2xx throws with status
Basics
Http.get/post/put/delete(url, options)returns JSON or text- Options:
headers,query,timeoutMs,retry: { times, delay },body
ts
import { Http } from 'nex-lib'
await Http.get('https://example.com/api', { query: { q: 'x' }, timeoutMs: 1000 })withQuery(url, params)
Object to query string.
ts
import { withQuery } from 'nex-lib'
withQuery('https://a.com/path', { a: 1, b: true, c: null })toFormData(obj)
Object to FormData (objects JSON‑serialized).
ts
import { toFormData } from 'nex-lib'
const fd = toFormData({ a: 1, b: 'x', c: { k: 'v' } })withTimeout(promise, ms)
Add timeout for any promise (throws Timeout).
ts
import { withTimeout } from 'nex-lib'
const p = new Promise(resolve => setTimeout(resolve, 50))
await withTimeout(p, 10)withRetry(fn, times, delay)
Retry wrapper; align with ObjectUtils.retry.
Tips:
- Combine timeout and retry: apply
timeoutMsper attempt, then wrap overall withretry. - Do not retry for 400/401 (invalid params/permission).