Frappe request transport
Frappe request transport is the network boundary between the package and a Frappe site. It turns method names and request options into fetch calls, applies site and authentication-facing settings, maps Frappe response envelopes to useful values, and gives data consumers a consistent way to observe failures. See Data access for the wider data model.
Transport model
The package has a Frappe-aware request helper and a lower fetch path for reactive data composables. Both use fetch, but they serve different callers and have different response handling responsibilities.
Direct request boundary
frappeRequest is the public Frappe-aware request boundary. It accepts a URL or dotted method name and returns a promise for the unwrapped response value. The imperative call helper uses this boundary for one-off POST operations, while data composables use the lower fetch path described below. See src/…/frappeRequest.ts• L79-136 and src/…/call.ts• L19-51.
References: src/…/frappeRequest.ts• L79-136 src/…/call.ts• L19-51 src/index.ts• L12-21
Reactive fetch boundary
useFrappeFetch is the fetch adapter used by useCall and the higher-level document and list composables. It adds Frappe headers, parses the response for reactive state, and sends document data to the shared stores. This path also carries request order information so shared state can reject stale writes. See Document and list state for the store effects.
References: src/…/useFrappeFetch.ts• L40-75 src/…/useFrappeFetch.ts• L77-103 src/…/useCall.ts• L152-173
Request selection
The transport selects a URL based on the form supplied by the caller:
- A dotted method such as
frappe.client.get_listbecomes/api/method/frappe.client.get_list. - A path that starts with
/is used as an API path. - An absolute
httporhttpsURL is used without a Frappe method prefix. - A configured base URL is added to relative paths. This supports local UI development against a remote Frappe site.
The transport checks the URL scheme rather than only its first letters. A method whose app name starts with http therefore still receives the Frappe method prefix. See src/…/frappeRequest.ts• L111-128.
References: src/…/frappeRequest.ts• L57-68 src/…/frappeRequest.ts• L111-128 src/…/request.ts• L40-50
Request configuration
Request configuration defines the site target and the authentication context for direct Frappe requests. Applications set shared policy once through setConfig, then use per-request options only for deliberate exceptions.
URL and method settings
requestBaseUrl supplies the remote site origin for relative URLs. When it is set, cookie-based requests default to credentials: 'include'; a per-request credentials value can override that choice. The lower request helper sends GET parameters in the query string and JSON-encodes parameters for other methods. frappeRequest uses POST as its default method because its normal target is a Frappe method endpoint. See src/…/config.ts• L28-32, src/…/frappeRequest.ts• L118-135, and src/…/request.ts• L30-60.
References: src/…/config.ts• L28-32 src/…/frappeRequest.ts• L118-135 src/…/request.ts• L30-60
Header policy
Every frappeRequest gets JSON content negotiation headers and the local browser host as X-Frappe-Site-Name. requestHeaders adds application headers from either a fixed object or a function, and request-level headers take priority over configured headers. This is the main configuration-facing place for a token such as Authorization: token <key>:<secret>.
The CSRF header comes from window.csrf_token when the value is present and is not the unresolved template marker. A remote base URL normally uses cookies, while token-based requests usually omit cookies by passing credentials: 'omit'. Cross-origin cookie use also requires the remote server to allow credentials for the requesting origin. See src/…/frappeRequest.ts• L92-135 and src/…/config.ts• L34-37.
References: src/…/frappeRequest.ts• L92-135 src/…/config.ts• L24-37
Composable headers
The reactive fetch boundary applies its own JSON, site, and CSRF headers before a data request is sent. Its URL comes from the composable's url and baseUrl options, so data composables do not use frappeRequest's requestBaseUrl or requestHeaders settings as part of this path. See src/…/useFrappeFetch.ts• L60-79, src/…/useFrappeFetch.ts• L163-185, and src/…/types.ts• L6-23.
This distinction matters when an application changes authentication policy. Direct call and frappeRequest share the global request configuration, while useCall, useDoc, and useList need the options supported by their reactive fetch path. See Remote actions and mutations for the public operation choices.
References: src/…/useFrappeFetch.ts• L60-79 src/…/useFrappeFetch.ts• L163-185 src/…/types.ts• L6-23 src/…/call.ts• L25-34
Response handling
Frappe responses often wrap the useful result in a message field. The transport removes that wrapper for normal direct requests, while preserving document payloads and the full login payload when those responses need their surrounding fields.
Direct response values
For a successful frappeRequest, a response with docs is returned as the complete response object. The login endpoint is also returned as a complete object because its name and home-page fields sit beside message. Other successful responses resolve to data.message, including a method-specific dotted path that merely ends in login.
The call helper always sends a POST with its argument object as the JSON body. It returns the method's message value, except for the login response, and accepts an absolute API path when a caller does not want dotted-method conversion. See src/…/frappeRequest.ts• L137-169 and src/…/call.ts• L19-34.
References: src/…/frappeRequest.ts• L64-68 src/…/frappeRequest.ts• L137-169 src/…/call.ts• L19-34
Reactive response values
The reactive path parses the response into data used by useCall. When the response contains documents, it publishes those documents to both document and list state with the same request stamp. This is why one server result can update a document view and a matching list row. See Document and list state.
The response adapter also exposes server debug information in development-oriented console output. It does not turn that information into application data. See src/…/useFrappeFetch.ts• L81-103.
References: src/…/useFrappeFetch.ts• L81-103 src/…/useCall.ts• L89-137
Server messages
Frappe can return user-facing messages in _server_messages even when the HTTP response succeeds. The direct transport parses these messages and sends them to the configured serverMessagesHandler, or to the handler supplied for that request. A response value can still resolve normally after the messages are reported.
References: src/…/frappeRequest.ts• L146-168 src/…/config.ts• L24-26 src/…/call.ts• L25-27
Error handling
The transport keeps HTTP application errors separate from failures that happen before a valid Frappe response exists. This lets screens decide whether to show a server rejection, a network problem, or a client-side parse problem.
Frappe request errors
For a non-success HTTP response, frappeRequest reads the response body and creates a FrappeRequestError. The error carries the response, status, exception type, exception text, and normalized user messages. If the server gives no usable message, it falls back to the server error text or Internal Server Error.
The onError callback receives this error once, and the request promise still rejects. The once-only rule covers both the response transformation and the final rejection path, so a caller does not show the same failure twice. See src/…/frappeRequest.ts• L170-221.
References: src/…/frappeRequest.ts• L170-221 src/…/frappeRequest.test.ts• L127-201
Imperative error callbacks
call keeps the same rejected error shape but narrows its callback contract. Its onError callback runs only when the error has an HTTP response, and it receives the response, status, and error together. A transport failure still rejects the returned promise without calling this callback. See src/…/call.ts• L35-50.
References: src/…/call.ts• L35-50 src/…/call.test.ts• L131-180
Reactive fetch errors
The reactive fetch path converts a structured Frappe error response into FrappeResponseError. A network failure or an unrecognized error body remains the original fetch error, while a successful response that fails during client parsing is treated as a client-side error. useCall exposes the result through its reactive error state and error hook.
This distinction affects stale data policy. A known Frappe response error must remain visible instead of being hidden by cached data, while a suitable network failure can use stale fallback when the composable enables it. See Document and list state.
References: src/…/useFrappeFetch.ts• L105-159 src/…/useCall.ts• L140-150 src/…/useCall.ts• L278-280
Network path
All low-level calls end at the browser's global fetch. The direct helper uses a small request wrapper with transformation hooks, while the data composables use a VueUse fetch instance that adds reactive state and shared-store effects around the same browser operation.
Bare fetch wrapper
request is the lower direct wrapper beneath frappeRequest. It validates the URL, builds query strings or JSON bodies, passes headers, credentials, and abort signals to fetch, and applies response and error transforms. It is internal because it does not understand Frappe's message, docs, server-message, or exception conventions.
References: src/…/request.ts• L1-17 src/…/request.ts• L23-75 src/…/request.ts• L76-93
Data composable path
The data path starts at useCall and uses useFrappeFetch for a reactive request. The fetch adapter adds the request stamp before dispatch, parses the response after fetch, and hands document results to the document and list stores. Higher-level composables build their read and write behavior on this path.
When a request is aborted or fails, the composable reports the fetch error through its reactive state. When it succeeds, useCall can transform the returned value, persist a cache-keyed result, and run its success hook. See Remote actions and mutations for the request lifecycle above this boundary.
References: src/…/useFrappeFetch.ts• L60-103 src/…/useCall.ts• L91-148 src/…/useCall.ts• L152-190
Request cancellation
Both request surfaces can pass an abort signal or expose an abort action. Cancellation stops the fetch operation and leaves the caller to interpret the resulting request state; it does not turn a server response into a successful result. See src/…/request.ts• L7-16, src/…/request.ts• L54-60, and src/…/useCall.ts• L175-190.
References: src/…/request.ts• L7-16 src/…/request.ts• L54-60 src/…/useCall.ts• L175-190
Public transport choices
The public API offers a direct helper for one operation and a reactive composable for stateful data access. Select the surface based on whether the caller needs reactive request state or only a promise result.
| Need | Surface | Main result |
|---|---|---|
| Send one Frappe method request | call | A promise for the method result |
| Send a Frappe-aware request with custom response handling | frappeRequest | A promise with Frappe response mapping and error details |
| Keep request data and status reactive | useCall | Reactive data, loading, error, and submit state |
| Read or change a shared document or list | useDoc, useList, or useDoctype | Higher-level state and actions built on the reactive fetch path |
Use Data access to choose the data boundary. Use Document and list state when the question concerns shared records, and Remote actions and mutations when it concerns concurrent writes.
Change checklist
When these files or sections are changed, remember to consider these:
- When a Frappe response envelope changes, check direct unwrapping, reactive response parsing, login handling, and document or list store publication together.
- When authentication settings change, check the difference between
frappeRequestconfiguration and the headers and URL options available touseCall. - When a base URL changes, check relative method conversion, cross-origin credentials, CSRF behavior, and absolute URL handling.
- When error fields or server-message formats change, check
FrappeRequestError,FrappeResponseError, callback contracts, and stale-data fallback rules. - When the fetch adapter changes, preserve request stamps on responses so shared document and list writes keep one ordering decision.
- When request transforms or parameter encoding changes, check GET query values, non-GET JSON bodies, abort signals, and parse failures.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A dotted method is mistaken for an absolute URL | Frappe receives the wrong path and the method fails or reaches no endpoint | Prefix only paths and URLs with a real http or https scheme |
| A remote base URL uses cookies without server CORS support | The browser blocks the cross-origin response or omits the session | Configure credentialed CORS with a specific allowed origin, or use token headers with omitted credentials |
| A token is configured only for direct requests | call works while a data composable remains unauthenticated | Configure authentication through the options supported by the reactive fetch path as well |
| A Frappe error is treated as a network error | The UI can hide an explicit server rejection behind stale data | Preserve the FrappeResponseError distinction |
A transport error is reported through an HTTP-only call callback | The caller misses the failure while the promise still rejects | Handle the rejected promise for offline, CORS, and abort failures |
| One failure passes through both error hooks | A screen can show duplicate error feedback | Keep the once-only reporting marker in the Frappe request path |
| A successful response fails during JSON parsing | The request appears successful at HTTP level but has no usable data | Let the final fetch error path report the parse failure |
| The reactive fetch adapter loses the response stamp | An older document or list response can overwrite newer shared state | Keep dispatch stamps attached to each response passed to the stores |
Key files
| File | Why to read it |
|---|---|
| src/…/frappeRequest.ts | Defines Frappe URL mapping, headers, credentials, response unwrapping, server messages, and direct error reporting. |
| src/…/request.ts | Defines the lower direct fetch wrapper, parameter encoding, transforms, abort support, and rejection path. |
| src/…/call.ts | Defines the one-off POST helper and its HTTP-only callback contract. |
| src/…/config.ts | Defines shared request base URL, authentication-facing headers, and server-message policy. |
| src/…/useFrappeFetch.ts | Defines the reactive fetch adapter, Frappe error conversion, response stamps, and shared-store handoff. |
| src/…/useCall.ts | Shows how reactive calls use the fetch adapter and expose data, status, cache, and error state. |
| src/…/types.ts | Defines the public reactive call options for URLs, methods, parameters, timing, and callbacks. |
| src/…/index.ts | Shows which request and error surfaces are public and which fetch layer stays internal. |
| src/…/frappeRequest.test.ts | Verifies URL mapping, base URL and header behavior, login response handling, and once-only errors. |
| src/…/call.test.ts | Verifies imperative call payloads, shared configuration, server messages, and callback behavior. |