Function useResource

  • Resource-scoped CRUD helpers and filter-widget factory for one resource.

    Resolves the resource by name from the app context and returns:

    • resource - the resolved ResourceConfig (or null until loaded).
    • components - a map of field name -> filter component. The widget is chosen by field shape (boolean, FK, enum, number, date, inline-ref, or, via field.autocomplete, text/token/email/phone). In client filter mode option suggestions derive from data; in server mode FK/ref options are fetched (FK options are cached globally per target resource).
    • list, get, create, update, remove - CRUD via the shared axios client. create/update run sanitizePayload (resolving FK ids to objects). patch is present only when the spec exposes a PATCH operation.
    • stream - subscribe to an x-sse stream via EventSource, resolving {pathParams}; present only for streaming resources.
    • loading / error - request state (error parsed from the response body: an array of {msg} errors, a detail string, or message).

    Notes:

    • list/get accept both a bare array and { total, items }; paginated resources require the latter.
    • Methods throw (and set error) when the resource is not loaded yet.

    Parameters

    • resourceName: string

      The resource name (URL path segment) to bind to.

    Returns UseResourceReturn

    The bound resource, CRUD methods, filter components, streaming subscription and request state.

    const { resource, list, create } = useResource("expenses");