Custom Endpoints (Vue / TypeScript)
The standard actions (list, retrieve, create, …) map to fixed paths and verbs. A BE viewset that serves more than those — GET /items/search, POST /items/clone — is matched on the FE by a method in the ViewSet's own class body, written against this.request().
A method in the class body
import { restViewSet, LookupMixin, ReadOnlyViewSetMixin } from '@dynamicforms/fastapi-viewsets';
interface Item { id: number; name: string; description: string | null }
interface CloneRequest { source_id: number; new_name: string }
class ItemApi extends restViewSet<Item>()('id', [ReadOnlyViewSetMixin, LookupMixin]) {
/** Search items by name fragment. */
async search(q: string): Promise<Item[]> {
return this.request<Item[]>('GET', '/search', { query: { q } });
}
/** Clone an item under a new name. */
async clone(body: CloneRequest): Promise<Item> {
return this.request<Item>('POST', '/clone', { body });
}
}
const itemsApi = new ItemApi({ basePath: '/items' });
// Declared actions and custom methods, on the same object:
const all = await itemsApi.list();
const one = await itemsApi.retrieve(1);
const results = await itemsApi.search('widget');
const cloned = await itemsApi.clone({ source_id: 1, new_name: 'Widget copy' });
// TS2339: create was not declared
await itemsApi.create({ name: 'Widget', description: null });Four things about the base-class expression, each of which the compiler enforces:
- The empty
()inrestViewSet<Item>()is required. TypeScript has no partial type-argument inference, so the model cannot be given explicitly while the pk field and the mixin list are inferred from arguments in the same call (TS2558). Currying is the only way to have both. 'id'is an argument, not a type argument. It is checked against the model's fields — a field that does not exist, or whose type cannot be a key, is TS2345 — and the pk type every action takes is read offItem['id']. It is not repeated in the constructor options.- The mixin list decides the public surface. Calling an action it does not name is a compile error, not a 404 at run time.
- Constructor options are
{ basePath, validateSchema?, repeat?, events?, topic?, axiosInstance?, attemptTimeoutMs? }forrestViewSetand{ basePath, validateSchema?, repeat?, events?, topic?, peer, headers?, timeoutMs? }formuxwsViewSet.pkFieldNameanddeclaresare bound by the factory; passing either is TS2353.
The method's name is also what the schema check looks for. The schema names the BE method that serves each custom endpoint (x-viewset-action), and the check wants a FE method of that name in camelCase or of the name itself: GET /items/search, served by search, wants search(); GET /items/top-rated, served by top_rated, wants topRated() or top_rated(). It warns when the ViewSet has neither.
See Mixins for the mixin list itself.
A ViewSet with custom endpoints only
A BE viewset with no mixin serves its custom endpoints and GET {basePath}/schema:
@route_viewset(router, base_path="/reports", pk_field_name="id")
class ReportViewSet:
__router = APIRouter()
@__router.get("summary")
async def summary(self, context: Context) -> Report: ...
@__router.get("top-rated")
async def top_rated(self, context: Context) -> list[Report]: ...
@__router.post("{pk}/approve")
async def approve(self, context: Context, pk: int) -> Report: ...Its FE ViewSet declares an empty mixin list. The class has no standard action; its methods are the class body's own:
class ReportApi extends restViewSet<Report>()('id', []) {
async summary(): Promise<Report> {
return this.request<Report>('GET', '/summary');
}
async topRated(): Promise<Report[]> {
return this.request<Report[]>('GET', '/top-rated');
}
async approve(pk: number): Promise<Report> {
return this.request<Report>('POST', `/${pk}/approve`);
}
}
const reportsApi = new ReportApi({ basePath: '/reports' });
// TS2339: list was not declared
await reportsApi.list();The schema check matches the three endpoints to summary(), topRated() and approve(), and reports any standard endpoint the BE serves. muxwsViewSet<Report>()('id', []) builds the same ViewSet on muxws.
this.request()
request(method, path, options?) sends one call and returns the decoded body. It is the only thing a transport implements, which is why a method written against it works unchanged on REST and on muxws.
| Argument | Value |
|---|---|
method | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' |
path | relative to basePath: '' for the collection, '/search', `/${pk}/history` |
options | { query?, body?, headers?, key?, repeat?, background?, onUploadProgress?, responseType? }, all optional |
body is sent as JSON, or, when it is a FormData, as multipart/form-data (REST only). onUploadProgress and responseType: 'blob' are REST only as well; see Files.
request() returns a ViewSetCall<R>: the promise of the body, with the call's Idempotency-Key as key; with responseType: 'blob' it is a ViewSetCall<FileDownload>. The call is repeated with that key when an attempt ends without an answer; key sets the key, repeat overrides the proxy's repeat options for this call (see Idempotent Calls).
A method that calls request() before its first await returns a promise with the same key, also when the method is declared async and typed Promise<R>; a method that awaits something before its first request returns a promise without key. To have key in the method's type, return the ViewSetCall from a method that is not async:
import type { ViewSetCall } from '@dynamicforms/fastapi-viewsets';
class ItemApi extends restViewSet<Item>()('id', [ReadOnlyViewSetMixin, LookupMixin]) {
search(q: string): ViewSetCall<Item[]> {
return this.request<Item[]>('GET', '/search', { query: { q } });
}
}
const call = itemsApi.search('widget');
call.key; // the Idempotency-Key of GET /items/searchAn array-valued query entry becomes a repeated key, which is how FastAPI binds a list[str] parameter. A status of 400 or above throws on both transports, and what is thrown and what a handler reads off it is in handling a failed call.
Files
A custom endpoint that receives a file (multipart/form-data) or answers with one (a PDF, an export) is written against this.request() like any other. The BE side is in Custom Endpoints.
Uploading a file
A FormData body is sent as multipart/form-data as it is; axios sets the Content-Type with its boundary, again on every repeat. onUploadProgress(loaded, total) is called as the body is sent: loaded is the bytes sent so far, total the size of the body, undefined when it is not known. A repeated attempt reports from 0 again.
import { ReadOnlyViewSetMixin, restViewSet, type ViewSetCall } from '@dynamicforms/fastapi-viewsets';
interface Track { id: number; title: string }
interface Attachment { filename: string; size: number }
class TrackApi extends restViewSet<Track>()('id', [ReadOnlyViewSetMixin]) {
/** Served by the BE method `upload_attachment` (POST /tracks/attachments). */
uploadAttachment(file: File, onProgress?: (percent: number) => void): ViewSetCall<Attachment> {
const body = new FormData();
body.append('file', file);
return this.request<Attachment>('POST', '/attachments', {
body,
onUploadProgress: (loaded, total) => onProgress?.(Math.round((100 * loaded) / (total ?? file.size))),
});
}
}The field name, file here, is the name of the BE method's UploadFile parameter.
Downloading a file
With responseType: 'blob' the call resolves to a FileDownload instead of a decoded body:
| Field | Type | Value |
|---|---|---|
blob | Blob | The response body; its type is contentType |
filename | string | null | From Content-Disposition: filename* (RFC 5987, e.g. UTF-8''track-1-%C3%A9.pdf) when present, else filename, else null |
contentType | string | The Content-Type header, '' when there is none |
headers | Record<string, string> | The response headers, names in lower case |
contentDispositionFilename(header) is the function that reads filename: it takes a Content-Disposition value (or null / undefined) and returns the file name, or null.
import type { FileDownload, ViewSetCall } from '@dynamicforms/fastapi-viewsets';
class TrackApi extends restViewSet<Track>()('id', [ReadOnlyViewSetMixin]) {
/** Served by the BE method `pdf` (GET /tracks/{pk}/pdf). */
pdf(pk: number): ViewSetCall<FileDownload> {
return this.request('GET', `/${pk}/pdf`, { responseType: 'blob' });
}
}
const tracksApi = new TrackApi({ basePath: '/tracks' });
const file = await tracksApi.pdf(1);
const url = URL.createObjectURL(file.blob);
const a = document.createElement('a');
a.href = url;
a.download = file.filename ?? 'track.pdf';
document.body.appendChild(a);
a.click();
a.remove();
setTimeout(() => URL.revokeObjectURL(url), 1000);A page on another origin reads Content-Disposition only when the server lists it in Access-Control-Expose-Headers; otherwise filename is null.
The attempt timeout applies to file calls as to every REST call: an attempt is aborted after attemptTimeoutMs without progress, counted from its start or its last upload or download progress event, whichever is later. An upload whose bytes keep flowing does not time out.
A failed file call
A blob call fails as a JSON call does. An answer of 400 or above whose Content-Type is application/json or +json is decoded into an object before the failure is classified, so errorBodyOf(error) reads the ApiErrorBody, and a 502–504 with an ApiErrorBody is the application's answer and is not repeated. Idempotency-Key, repeats and background behave as for a JSON call.
import { errorBodyOf } from '@dynamicforms/fastapi-viewsets';
try {
await tracksApi.pdf(999999999);
} catch (error) {
errorBodyOf(error).detail_code; // 'fastapi-viewsets.not_found'
}Over muxws
The muxws transport sends JSON bodies and decodes every answer as JSON. A call with a FormData body, with onUploadProgress or with responseType: 'blob' returns a rejected ViewSetCall; the Error names the option and says the muxws transport does not support it. A file endpoint shared by both transports, as in One endpoint, both transports, is called on the REST ViewSet.
Accessing protected members
A factory-built ViewSet descends from ViewSetInternals, which holds what a ViewSet's own methods need and nothing a caller does:
| Member | Type | Description |
|---|---|---|
this.request(method, path, opts?) | ViewSetCall<R> (ViewSetCall<FileDownload> with responseType: 'blob') | Sends one call and returns the decoded body, or a FileDownload with responseType: 'blob', with the call's key. Transport-independent. |
this.basePath | string | The base path, trailing slash removed, e.g. '/items' |
Both are protected, so reaching them from outside the class body is TS2445.
this.http is not available on a factory-built class (TS2339): the factory types its result as these internals plus the declared actions, and the axios instance belongs to the REST transport alone. A file upload with progress and a file download go through this.request() (see Files); a method that needs an axios option request() does not take — an abort signal, an onDownloadProgress callback — extends RestProxyImpl instead.
One endpoint, both transports
When the same custom endpoint has to be on a REST ViewSet and a muxws one, put it in a class-expression mixin. The two classes come from separate factory calls and so have no common ancestor to hang a shared method on.
import {
CursorListMixin,
RetrieveMixin,
ViewSetInternals,
muxwsViewSet,
restViewSet,
} from '@dynamicforms/fastapi-viewsets';
interface CountEndpoint {
count(): Promise<number>;
}
/**
* The return type is annotated on purpose: an inferred one names the anonymous class and leaks its
* protected members into the emitted .d.ts (TS4094).
*/
function WithCount<TBase extends abstract new (...args: any[]) => ViewSetInternals>(
Base: TBase,
): TBase & (abstract new (...args: any[]) => CountEndpoint) {
abstract class Counting extends Base {
/** Served by the BE method `count` (GET /items/count). */
async count(): Promise<number> {
return this.request<number>('GET', '/count');
}
}
return Counting;
}
const DECLARES = [CursorListMixin, RetrieveMixin];
export class ItemRestApi extends WithCount(restViewSet<Item>()('id', DECLARES)) {}
export class ItemMuxwsApi extends WithCount(muxwsViewSet<Item>()('id', DECLARES)) {}ViewSetInternals is the right constraint because it is exactly what the shared method uses; bounding TBase by a transport would tie the method to that transport. Both classes keep their declared actions and their own constructor options — new ItemRestApi({ basePath: '/items' }), new ItemMuxwsApi({ basePath: '/items', peer }) — and the method is on the prototype, so the schema check finds it.
The demo does this for all six of its ViewSets: demo/frontend/src/viewsets.ts.
Reaching axios: extending RestProxyImpl
RestProxyImpl is the REST transport itself, and a subclass of it has this.http. The cost is that the class is HTTP-only, and that its type is the whole proxy: every standard action is present whether or not the BE serves it, so only the schema check — at run time — can disagree.
import axios from 'axios';
import { BulkViewSetMixin, LookupMixin, RestProxyImpl } from '@dynamicforms/fastapi-viewsets';
class ItemApi extends RestProxyImpl<number, Item, 'id'> {
static declares = [BulkViewSetMixin, LookupMixin];
/** Needs axios itself: request() takes no abort signal. */
async exportCsv(signal: AbortSignal): Promise<Blob> {
const res = await this.http.get<Blob>(`${this.basePath}/export`, { responseType: 'blob', signal });
return res.data;
}
}
const http = axios.create({
baseURL: 'https://api.example.com',
headers: { Authorization: 'Bearer my-token' },
});
const itemsApi = new ItemApi({ basePath: '/items', pkFieldName: 'id', axiosInstance: http });A call made through this.http directly carries no Idempotency-Key and is not repeated; only request() does both. It does not start the schema check either; a method that may be the instance's first call calls this.ensureSchemaCheck() to start it.
Here pkFieldName is a constructor option and declares a static, since there is no factory call to bind them. A custom axios instance is passed the same way to a factory-built REST ViewSet: new ItemApi({ basePath: '/items', axiosInstance: http }).
The other form: route_rest
route_rest and route_muxws are supported. They build a bare proxy and cast it to the mixin interface named as M:
import { route_rest, BulkViewSetMixin } from '@dynamicforms/fastapi-viewsets';
/** A type token: route_rest uses it for nothing but its type. */
class ItemTypeToken extends BulkViewSetMixin<number, Item, 'id'> {}
interface ItemApiInterface extends BulkViewSetMixin<number, Item, 'id'> {
search(q: string): Promise<Item[]>;
}
const itemsApi = route_rest<ItemApiInterface>(ItemTypeToken, '/items', 'id');
await itemsApi.list(); // works
await itemsApi.search('widget'); // compiles, and throws: search is undefinedThe cast is the limitation. The object is a RestProxyImpl, so the standard actions are there but a custom endpoint named in M exists only in the type. A custom endpoint needs a class of your own: the factory above, or RestProxyImpl extended directly.
