APIResource
A generic API resource for making requests based on URLs, suitable for any HTTP interface. It inherits from the FlowResource base class and extends it with request configuration and refresh(). Unlike MultiRecordResource and SingleRecordResource, APIResource does not depend on a resource name; it requests directly by URL, making it suitable for custom interfaces, third-party APIs, and other scenarios.
Creation method: ctx.makeResource('APIResource') or ctx.initResource('APIResource'). You must call setURL() before use. In the RunJS context, ctx.api (APIClient) is automatically injected, so there is no need to call setAPIClient manually.
Use Cases
Base Class Capabilities (FlowResource)
All Resources possess the following:
Request Configuration
URL Format
- Resource Style: Supports NocoBase resource shorthand, such as
users:listorposts:get, which will be concatenated with thebaseURL. - Relative Path: e.g.,
/api/custom/endpoint, concatenated with the application'sbaseURL. - Full URL: Use full addresses for cross-origin requests; the target must have CORS configured.
Data Fetching
Examples
Basic GET Request
Resource Style URL
POST Request (with Request Body)
Listening to the refresh Event
Error Handling
Custom Request Headers
Notes
- ctx.api Dependency: In RunJS,
ctx.apiis injected by the environment; manualsetAPIClientis usually unnecessary. If used in a context-less scenario, you must set it yourself. - Refresh Means Request:
refresh()initiates a request based on the current configuration; method, params, data, etc., must be configured before calling. - Errors Do Not Update Data: On failure,
getData()keeps its previous value; error information can be retrieved viagetError(). - Vs ctx.request: Use
ctx.request()for simple one-time requests; useAPIResourcewhen reactive data, events, and error state management are required.
Related
- ctx.resource - The resource instance in the current context
- ctx.initResource() - Initialize and bind to
ctx.resource - ctx.makeResource() - Create a new resource instance without binding
- ctx.request() - General HTTP request, suitable for simple one-time calls
- MultiRecordResource - For Collections/lists, supports CRUD and pagination
- SingleRecordResource - For single records

