Resource
Resource is a thin abstract base for CRUD APIs. Subclass it, set an endpoint and (optionally) bind an Http instance, and you get typed list / get / create / update / delete / publish for free. Resources resolve their Http instance lazily — setCurrentHttp(...) once at app bootstrap and every Resource subclass picks it up.
For the narrative guide, see Resource.
Import
import { Resource } from "@mongez/http";Subclassing
import { Resource } from "@mongez/http";
class UsersResource extends Resource { route = "/users";}
export const usersResource = new UsersResource();That’s the entire setup. Every method below works on usersResource.
Properties
| Name | Type | Description |
|---|---|---|
route | string | Base path for the resource, e.g. "/users". Required. |
Instance methods
Every method below returns CancellablePromise<HttpResult<T>>. Pass options.throw = true to throw HttpError instead.
list(params?, options?)
list( params?: HttpParams, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>GET {route}?{params} — fetch a paginated or filtered list. params becomes the query string.
get(id, options?)
get( id: number | string, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>GET {route}/{id} — fetch a single record.
create(data, options?)
create( data: HttpData, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>POST {route} with data as the body.
update(id, data, options?)
update( id: number | string, data: HttpData, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>PUT {route}/{id} — full replacement. With putToPost: true on the bound Http, this is sent as POST + _method=PUT automatically.
patch(id, options?)
patch( id: number | string, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>PATCH {route}/{id} — partial update. Pass the body via options.data.
delete(id, options?)
delete( id: number | string, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>DELETE {route}/{id}.
bulkDelete(data, options?)
bulkDelete( data: HttpData, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>DELETE {route} with data (typically { ids: [...] }) as the body.
publish(id, published, publishKey?, options?)
publish( id: number | string, published: boolean | HttpData, publishKey?: string, options?: RequestOptions,): CancellablePromise<HttpResult<unknown>>PATCH {route}/{id} with { [publishKey]: published } in the body. Defaults publishKey to the bound Http’s config.publishKey (default: "published").
action<T>(id, actionName, data?, options?, method?)
action<T = unknown>( id: number | string, actionName: string, data?: HttpData, options?: RequestOptions, method?: HttpMethod,): CancellablePromise<HttpResult<unknown>>{method} {route}/{id}/{actionName} — for non-CRUD endpoints like POST /users/42/reset-password. Default method is POST.
path(suffix?)
path(suffix?: string | number): stringBuild a full path relative to route. path() returns "/users"; path(42) returns "/users/42"; path("export") returns "/users/export".
actionPath(id, actionName)
actionPath(id: string | number, actionName: string): stringBuild the path for a named action — actionPath(42, "reset-password") returns "/users/42/reset-password".
Binding to a specific Http instance
By default, a Resource uses whatever Http was registered via setCurrentHttp(...). To pin a resource to a specific instance:
class AdminUsersResource extends Resource { route = "/users";}
const adminHttp = new Http({ baseURL: "https://admin.api.com" });export const adminUsers = new AdminUsersResource().useHttp(adminHttp);See also
- Http — the underlying client
- ResourceService — interface every Resource implements
- Guide: Resource — narrative walkthrough with examples