@webinmove/ombi
v1.0.0
Published
Lightweight HTTP request wrapper built on top of got with normalised responses and managed error handling.
Downloads
41
Readme
Ombi
Description
Ombi means "request" in Swahili.
Lightweight HTTP request wrapper built on top of got with normalised responses and managed error handling.
Installation
npm install @webinmove/ombiUsage
import Request from '@webinmove/ombi';
const request = new Request('https://api.example.com');
// GET
const response = await request.call('GET', 'users/1');
// POST with a body
const created = await request.call('POST', 'users', {
body: { name: 'Alice' }
});
// GET with query string params
const list = await request.call('GET', 'users', {
qs: { page: 1, limit: 20 }
});
// Custom headers
const secured = await request.call('GET', 'protected', {}, {
Authorization: 'Bearer <token>'
});API
new Request(endpoint)
| Parameter | Type | Description |
|------------|----------|---------------------------|
| endpoint | string | Base URL for all requests |
request.call(method, path, params?, headers?, parentScope?, responseType?)
| Parameter | Type | Default | Description |
|----------------|------------------|----------|----------------------------------------------------------------|
| method | string | none | HTTP method (GET, POST, PUT, ...) |
| path | string | none | Path appended to the base endpoint |
| params | object | {} | body (sent as JSON for POST/PUT) and/or qs (query string) |
| headers | object | {} | Additional request headers |
| parentScope | string | '' | Scope label forwarded to ManagedError on HTTP failures |
| responseType | string \| null | 'json' | got response type; pass null to leave it unset |
Returns a Promise that resolves to:
{
ok: boolean,
statusCode: number,
statusMessage: string,
body: unknown,
headers: object
}HTTP errors (4xx / 5xx) are caught and re-thrown as a ManagedError
(from @webinmove/kosa). All other errors are re-thrown as-is.
License
ISC
