The browser SDK
@cardinal/sdk, the TypeScript functions a desk plugin or app frontend uses to call the server.
@cardinal/sdk wraps the REST and RPC routes. A desk plugin gets the desk's copy through the import map, so it is not bundled. Every call runs as the signed-in user.
import { listRecords, saveRecord, request, t } from '@cardinal/sdk'
const open = await listRecords('CRM Task', {
fields: ['name', 'title', 'status'],
filters: [['status', '=', 'Todo']],
order_by: 'modified desc',
limit: 50,
})Records
| Function | Does |
|---|---|
listRecords(rectype, args) | Rows the user may read. args: fields, filters, or_filters, order_by, limit (20 by default), offset, group_by, distinct |
countRecords(rectype, filters, cap) | How many match |
getRecord(rectype, name) | One record |
newRecord(rectype) | An unsaved record with defaults |
insertRecord(rectype, doc), saveRecord(rectype, name, doc) | Write |
submitRecord, cancelRecord, amendRecord, discardRecord | Move the docstatus |
renameRecord(rectype, name, newName, merge), deleteRecord, copyRecord | Rename, delete, copy |
mappedRecord(rpc, sourceName) | The unsaved record a Create menu RPC answers |
serverAction(rpc, rectype, doc) | Calls a RecType's Server Action |
searchLink(rectype, txt, args), linkTitles(rectype, names) | Link search and titles |
timeline(...), connections(rectype, name) | A form's timeline and Connections panel |
Calls, files and session
| Function | Does |
|---|---|
request<T>(method, path, body) | Any route. Returns the parsed body, throws an ApiError on failure |
uploadFile(...), removeFile(name), fileUrl(name, download) | Files |
me(), login(login, password), logout() | The session |
loadBoot() | The desk boot, including apps.<name> from each app's boot/1 |
subscribe(topic, handlers) | Realtime. See Realtime and the bell |
t(msg, ...args), tc(context, msg, ...args) | Translations in the user's language |
formatCurrency, formatDate, parseDate, today and other format helpers | Values as the user's locale shows them |
compileExpr, evalMany | The expression language in the browser, through WASM |
slug(name) | A RecType's desk slug, CRM Lead to crm-lead |
Call an RPC
const doc = (await request<{ data: unknown }>('POST', '/api/rpc/crm.crm_holiday_list.clear_table', { doc })).dataA failed call throws an ApiError with status, type, message and fields (path to message). Its subclass names the status: AuthError (401), PermissionError (403), NotFoundError (404), ConflictError (a stale save), ValidationError.
In an app's own frontend
A frontend served under /<mount>/ (the manifest's frontend) reads its boot first:
import { appBoot, registerAppWorker } from '@cardinal/sdk'
const boot = appBoot() // { user, language, app: { name, version, mount }, csrf_token }
await registerAppWorker(boot.app.mount)appBoot() hands the CSRF token to the sdk, so later unsafe calls send it. registerAppWorker(mount) registers the service worker the server answers at /<mount>/sw.js, which keeps the shell for offline loads.