REST API services¶
This guide describes how frontend code calls REST endpoints through API services built on the shared BaseRestApi class (shared/api/baseRestApi.ts).
Note: Directly using REST in frontend applications is discouraged. For new features, prefer GraphQL endpoints to achieve more efficient, consistent, and flexible data management. See GraphQL services and definitions.
Extend BaseRestApi¶
Keep REST transport in an API service under the module's api/ folder. Components, composables, and stores call the service; they do not call fetch directly.
- Export a class that extends
BaseRestApi, plus a singleton instance. Export the singleton by name in a new module (export const settingsApi = new SettingsApi()); in an existing module follow its services, most of which still use a default export. - Pass paths relative to the application root;
BaseRestApiprefixes them withwindow.urlPrefix. - Use the inherited methods.
gettakes the query string without a leading?and optional request options (keepalivefor a request that must outlive a page navigation);post,putandpatchsenddataas a JSON body;deletesends none.
get<T>(path: string, parameters?: string, init?: Pick<RequestInit, "keepalive">): Promise<T | null>
post<T>(path: string, data: unknown): Promise<T>
put<T>(path: string, data: unknown): Promise<T>
patch<T>(path: string, data: unknown): Promise<T>
delete<T>(path: string): Promise<T>
Type async results¶
Declare every service method's return type as a Promise: Promise<T> for a parsed response, Promise<void> when callers do not use the body. Never declare an async method as returning void. Pass the response type to the inherited method, for example this.get<Settings>(...).
A 204 No Content response resolves to null, so type a method that can receive it as Promise<T | null>. get already returns T | null; post, put, patch and delete return the generic as given, so include null in it: this.post<Settings | null>(...).
Handle errors¶
The inherited methods reject with a RestApiError when the response is not OK. It carries the response status, for a service that handles one status differently. The message joins the response errors array with |, or falls back to error.message. An expired session redirects to login and rejects with "Session expired". The code that starts the request catches the rejection and owns the resulting loading and error state (Own loading and errors).
Example¶
bookmarks is a made-up module: the example shows the pattern, not existing code.
import { BaseRestApi } from "@/src/shared/api/baseRestApi";
import type { BookmarkSettings } from "@/src/bookmarks/types";
export class BookmarkSettingsApi extends BaseRestApi {
private static readonly settingsPath = "/bookmarks/settings.json";
async fetchSettings(): Promise<BookmarkSettings | null> {
return this.get<BookmarkSettings>(BookmarkSettingsApi.settingsPath);
}
async updateSettings(settings: BookmarkSettings): Promise<void> {
await this.put(BookmarkSettingsApi.settingsPath, settings);
}
}
export const bookmarkSettingsApi = new BookmarkSettingsApi();