Data-fetching composables
The Frappe-aware composables give Vue screens one reactive way to read documents, query lists, call server methods, and submit document changes. They turn each request into shared data and request state, while keeping document and list views aligned when requests overlap.
Composable family
The family maps common Frappe data tasks to focused composables. Each returns reactive state and request controls instead of making a screen manage raw HTTP details.
| Composable | Main purpose | Typical result |
|---|---|---|
useCall | Read or call any Frappe endpoint | data, error, request state, execute, and submit |
useDoc | Read one document and expose document actions | A reactive doc plus save, delete, and named method actions |
useList | Read a document list | Rows, paging controls, row updates, and list actions |
useDoctype | Group document-wide actions for one doctype | Insert, save, delete, document methods, and doctype methods |
useNewDoc | Hold and submit a new document draft | A reactive doc and the created document |
The public package entry point exports this family from src/…/index.ts, and the package root re-exports it from src/index.ts. The lower-level fetch object is not part of this boundary. Consumers use the composables and the public FrappeResponseError type rather than the internal request builder.
References: src/…/index.ts• L15-29 src/index.ts• L12-21
Reactive request state
Each composable exposes the result together with state that lets a view render the request lifecycle. A request can be started immediately, started by execute or fetch, or submitted with parameters. Reactive URLs and parameters can also start a new request when refetch is enabled.
Read state
Read composables expose a common set of signals and controls:
dataordoccontains the current result.errorcontains the current request error.loadingandisFetchingshow active work.isFinishedshows whether the current request cycle has ended.canAbort,aborted, andabortdescribe and control cancellation.execute,fetch, andreloadstart the same read again.
useList adds page position, next and previous page controls, and hasNextPage. useDoc binds its result to the current reactive document name, so a name that changes later selects a different document without rebuilding the composable.
References: src/…/useCall.ts• L322-345 src/…/useDoc.ts• L205-257 src/…/useList.ts• L250-278
Write state
Write actions expose data, error, loading, isLoading, and submit. loading covers all active submits for the action. isLoading can narrow that state to one target such as a document name, so a list can show activity on the correct row.
submit has a clear outcome contract: it resolves with the server result and rejects when validation or the request fails. A read through execute keeps its result in the reactive state and does not use the same rejection behavior as a write submit. A failed write keeps the last successful data; error identifies the failed submit.
References: src/…/useAction.ts• L37-73 src/…/useAction.ts• L135-233 src/…/useDoctype.ts• L20-26
Document reads and actions
useDoc represents one document identified by a doctype and name. It reads that document, keeps it available through the shared document state, and provides actions that affect the same document.
The methods option adds named server methods to the returned object. The composable also provides setValue and delete. These actions update both the document view and matching list rows when the server accepts a change. A caller can transform the document for display, while the shared stored copy remains the server-shaped value.
References: src/…/useDoc.ts• L58-89 src/…/useDoc.ts• L133-203
List reads and actions
useList turns doctype query options into a reactive collection. It supports fields, filters, ordering, grouping, parent scope, page size, and an optional custom endpoint. Reactive query inputs can refetch the list when they change.
List pages append to the current result when moving forward. The list also provides local row update and removal controls, plus insert, setValue, and delete actions. Successful document actions can refetch the list, while their direct store updates keep visible rows current without waiting for a new list response.
An optional cache key enables a local fallback while a request is pending or when an eligible transport error occurs. staleOnError controls whether an old cached value may remain after an error; a Frappe response error is not hidden by that fallback.
References: src/…/types.ts• L26-48 src/…/useList.ts• L23-66 src/…/useList.ts• L136-238 src/…/useList.ts• L281-337
Calls and remote actions
useCall is the general composable for a Frappe read or endpoint call. It accepts a URL, HTTP method, optional parameters, initial data, a transform, and success or error hooks. A GET places parameters in the query string. Other methods submit them as the request body.
useDoctype gives common document and method actions a stable shape. It groups operations that act on a doctype, including inserts, saves, deletes, document methods, and doctype methods. Each action can report aggregate activity or activity for its target.
This diagram shows how a screen request becomes reactive state and then reaches shared document and list views when the response carries document data.
flowchart LR
Screen[Vue screen] --> Composable[Data-fetching composable]
Composable --> Request[Frappe request]
Request --> Result[Reactive data and request state]
Request --> Shared[Shared document and list state]
Shared --> Screen
References: src/…/types.ts• L6-23 src/…/useCall.ts• L37-55 src/…/useDoctype.ts• L10-26
New document flow
useNewDoc holds a draft as a reactive object and submits it as a document insert. The draft excludes server-managed fields from its type, and its current values become the request payload only when submit runs.
The composable keeps insert requests explicit: editing the draft does not create a document, and each submit sends one request. The returned promise contains the document created by that submit. A successful insert also enters shared document state, subject to the same freshness rules as other writes.
References: src/…/useNewDoc.ts• L15-35 src/…/useNewDoc.ts• L41-69 src/…/useNewDoc.ts• L71-105
Stale-write protection
The composables protect shared document and list state from an old response arriving after a newer request. The system orders requests by dispatch order, not by response order. A store accepts a mutating response only when its request stamp is not older than the newest applied stamp for that document.
Reads can update state but do not record a write stamp. Deletes remove the document and seal its key, so an older in-flight response cannot restore it. Local seed writes are admitted without taking a network order. The same document key is used for document state and list rows, so both views reject the same stale response.
The request path carries the stamp from dispatch to the response. Composable instances do not decide whether a shared store write is fresh; the stores make that decision. This matters when two different screens or two different action types change one document at the same time.
References: src/…/writeGate.ts• L1-25 src/…/writeGate.ts• L30-45 src/…/writeGate.ts• L68-89 src/…/useFrappeFetch.ts• L40-44 src/…/useFrappeFetch.ts• L83-131
Concurrent action state
Actions also protect their own visible result and callbacks. The newest submit owns the action's data and error, while each submit still returns its own server result to its caller. A late submit does not replace the newer visible result or run a stale callback.
This instance-level rule is separate from store freshness. Store writes from every successful response carry their request stamp, because a response for another document may be valid even when it is not the newest submit in that action instance.
References: src/…/useAction.ts• L50-79 src/…/useAction.ts• L135-231 src/…/useIsolatedCall.ts• L35-53
Public integration boundary
Consumers integrate through the package root. The supported v2 boundary includes useCall, useDoc, useDoctype, useList, useNewDoc, their public option types, and FrappeResponseError.
The internal fetch wrapper adds JSON headers and Frappe site and CSRF headers, parses Frappe error responses, and feeds document payloads into shared state. It is an internal seam rather than a consumer API. The root also exposes the older frappeRequest, call, and v1 resource APIs; those use the separate FrappeResourceError contract and should not be mixed with the v2 FrappeResponseError contract when handling errors.
References: src/…/index.ts• L20-29 src/index.ts• L12-21 src/…/useFrappeFetch.ts• L6-38 src/…/frappeRequest.ts• L20-35
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public narrowing in src/…/index.ts aligned with the root export in src/index.ts; internal store-write hooks must not become consumer options by accident.
- Keep document and list store updates paired when a write changes a document, or one view can show a stale value while the other is fresh.
- Preserve the request stamp from dispatch through every shared-store write; moving a write into a later promise callback removes its freshness order.
- Keep action target keys aligned with the UI target used by
isLoading; a changed key can make row-level loading state point to the wrong item. - Keep v2
FrappeResponseErrorhandling separate from the v1FrappeResourceErrorcontract.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A late response writes shared state without its dispatch stamp | An older server result can replace a newer document and list row | Route shared writes through the stamped request path and the write gate |
A document action name collides with a built-in useDoc member | The custom method can hide doc, delete, or request controls | useDoc rejects reserved method names |
refetch is enabled for a new-document draft | Each draft edit can submit another insert | useNewDoc fixes immediate and refetch to explicit-submit behavior |
| A newer action submit fails after an older one succeeds | Treating the failed submit as a successful freshness mark would hide the older server write | Only successful same-target submits advance action callback freshness |
| A delete races with a read or save | A late response can restore a document that the server deleted | Delete removal seals the document key |
A v1 resource error is checked with instanceof FrappeResponseError | Error handling misses the v1 error shape | Use the error contract for the API family that made the request |
Key files
| File | Why to read it |
|---|---|
| src/…/index.ts | Defines the supported v2 composable and error export boundary |
| src/index.ts | Shows how consumers reach the data-fetching family from the package root |
| src/…/useCall.ts | Defines the common reactive call and submit behavior |
| src/…/useDoc.ts | Shows the single-document read and document action model |
| src/…/useList.ts | Shows list query, paging, caching, row updates, and list actions |
| src/…/useDoctype.ts | Shows the grouped doctype action model |
| src/…/useNewDoc.ts | Shows the draft-to-insert flow |
| src/…/useAction.ts | Defines concurrent action state and target-level callback freshness |
| src/…/useFrappeFetch.ts | Connects requests, Frappe errors, response stamps, and shared state |
| src/…/writeGate.ts | Defines the dispatch-order rule for shared writes |
| src/…/docStore.ts | Shows document cache publication and delete sealing |
| src/…/listStore.ts | Shows how document writes fan out to list rows |
| src/…/frappeRequest.ts | Defines the separate v1 request and error boundary |