Getting Started
Install
From npm (3.x until 4.0.0 is released):
npm install @ulyxie/cs-rest
The 4.0.0 development branch, straight from git (the prepare script builds it on install):
npm install git+https://git.derg.cz/content-server/cs-rest.git#4.0.0-develop
Node 20.19 or newer, Bun, or a current browser. The package is ES modules only; require() works on the Node releases that can load ES modules (20.19+, 22.12+).
In a web page, without a build step
The package is plain ES modules, so a page can load it from a CDN or your own server. Pin the version.
<!-- esm.sh: no setup -->
<script type="module">
import { Session } from "https://esm.sh/@ulyxie/cs-rest@4.0.1";
import "https://esm.sh/@ulyxie/cs-rest@4.0.1/workspaces";
</script>
<!-- the npm files (jsDelivr, unpkg, or lib/ copied to your server), with an import map for #platform -->
<script type="importmap">
{ "imports": { "#platform": "https://cdn.jsdelivr.net/npm/@ulyxie/cs-rest@4.0.1/lib/platform/browser.js" } }
</script>
<script type="module">
import { Session } from "https://cdn.jsdelivr.net/npm/@ulyxie/cs-rest@4.0.1/lib/index.js";
</script>
Not jsDelivr's /+esm URLs: they bundle each entry point separately, and a module could get its own Session. The server must allow the page's origin (Configure Security Parameters > Trusted CORS Domain), and a file:// page cannot be allowed - serve it over HTTP.
Create a session
import { Session } from "@ulyxie/cs-rest";
const session = new Session({
baseUrl: "https://server/otcs/cs.exe",
username: "Admin",
password: "******",
});
| Option | Meaning |
|---|---|
baseUrl |
The full URL of the CGI, e.g. https://server/otcs/cs.exe |
username, password |
Log in with credentials; the ticket is fetched and renewed for you |
otcsticket |
Use an existing ticket instead of credentials |
version |
The Content Server release to gate against, e.g. "24.4". Default "latest", which gates nothing. See Version Gating |
detectVersion |
Ask the server for its release once, instead of being told. Default false |
agent |
Node: the https.Agent for every request - a private CA, a proxy, a custom DNS lookup. See Transports |
fetch |
Send requests through this fetch instead of the default (Node's http in Node, fetch elsewhere). See Transports |
transport |
Send requests some other way entirely, such as axiosTransport(). See Transports |
headers |
Headers to send with every request |
Content Server returns a fresh OTCSTicket with each successful call, and the session keeps it for the next one.
Call a handler
Operations are grouped into handlers, created on first use. The core ones are always there; the rest are modules, each one import away:
const { data, result } = await session.nodes.node(2000);
result.name; // normalised: the same read from v1 or v2
data; // the body exactly as the server sent it
import "@ulyxie/cs-rest/workflow"; // once, anywhere in the app
await session.workflow.start(mapId);
Every method resolves with { data, status, statusText, headers }. Methods that return a known kind of entity add result; see Normalised Results. The full list is on Handlers.
const folder = await session.nodes.addFolder(2000, "Reports");
await session.nodes.addDocument({ parent_id: folder.result.id, fileHandler: "./q3.pdf" });
await session.categories.apply(folder.result.id, 9830, { "9830_2": "a value" });
const page = await session.nodes.children(2000, { limit: 50 });
page.result.items; // TNode[]
Which generation does a method call?
The name says so:
- unsuffixed calls v2:
nodes.node(id)isGET /v2/nodes/{id} - a
V1suffix calls v1:nodes.nodeV1(id)isGET /v1/nodes/{id} - a
V3suffix calls one of the few v3 routes:server.statusV3()isGET /v3/status
A V1 method that has a v2 twin is marked @deprecated. One where v1 is the only generation, such as nodes.ancestorsV1, is not.
Raw requests
For anything without a method, the session's get, post, put, patch, delete, head and options add authentication. They take the URL, a body for writes, and { params, headers, responseType, signal }:
await session.get("api/v2/nodes/2000");
Content Server does not accept application/json bodies. Use the form helpers, which build multipart/form-data and JSON-encode objects and arrays:
| Helper | Sends |
|---|---|
postForm(url, fields), putForm, patchForm |
each field as a form field |
postBody(url, body), putBody, patchBody |
one body field holding body as JSON |
deleteForm(url, params), deleteBody |
parameters in the query string, where Content Server reads DELETE parameters |
await session.postForm("api/v2/nodes", { type: 0, parent_id: 2000, name: "New Folder" });
Errors
- A response outside 2xx rejects with an
HTTPError.error.response.statusanderror.response.datahold what the server sent, the same shape an axios error had; test for one withisHTTPError(e). Network failures reject with the transport's own error. - A call the target release cannot answer rejects with
UnsupportedOperationErrorbefore anything is sent; test for it withisUnsupportedOperationError(e). See Version Gating. - Deprecated calls still work, and warn once per method in yellow on a terminal.
cs-rest 4.0.0
Using it
Working on it