Skip to content

Commit 28ec261

Browse files
committed
Add TypeScript Client documentation
1 parent 856aa62 commit 28ec261

2 files changed

Lines changed: 255 additions & 0 deletions

File tree

docs/.vitepress/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ export default defineConfig({
146146
// },
147147
],
148148
},
149+
{
150+
text: "TypeScript Client",
151+
collapsed: true,
152+
link: "/how-to/typescript-client/typescript-client-examples",
153+
items: [
154+
{
155+
text: "Manage Data With the TypeScript Client",
156+
link: "/how-to/typescript-client/typescript-client-examples",
157+
},
158+
],
159+
},
149160
{
150161
text: "SensorThings API",
151162
collapsed: true,
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
# How to Manage Data with the TypeScript Client
2+
3+
The HydroServer TypeScript client is built for developers who want to create HydroServer frontends with TypeScript or JavaScript. It provides a browser-first API, typed models, and convenience helpers for common data management workflows.
4+
5+
This guide focuses on practical usage patterns you can apply in real applications.
6+
7+
## Install
8+
9+
```bash
10+
npm install @hydroserver/client
11+
```
12+
13+
## Initialize the client
14+
15+
Initialize the client once at app startup. The client uses session cookies for authentication.
16+
17+
```ts
18+
import hs, { createHydroServer } from "@hydroserver/client";
19+
20+
await createHydroServer({ host: "https://playground.hydroserver.org" });
21+
```
22+
23+
`createHydroServer` initializes the client and binds the default `hs` export to a shared, ready-to-use instance. Call it once at app startup (for example in `main.ts`) and then import `hs` anywhere you need to access the API.
24+
25+
## Alternative: HydroServer.initialize()
26+
27+
If you prefer to manage the instance yourself, call `HydroServer.initialize()` directly. This returns a client instance without relying on the shared `hs` proxy.
28+
29+
```ts
30+
import { HydroServer } from "@hydroserver/client";
31+
32+
const client = await HydroServer.initialize({
33+
host: "https://playground.hydroserver.org",
34+
});
35+
36+
const things = await client.things.listAllItems();
37+
```
38+
39+
If your UI is served from the same domain as HydroServer, pass `host: ""` to use same-origin requests. For local development against a remote server, configure a dev proxy or CORS on the server to avoid browser CORS errors.
40+
41+
## Core collections
42+
43+
The client exposes the following core collections:
44+
45+
- workspaces
46+
- things
47+
- datastreams
48+
- sensors
49+
- units
50+
- processingLevels
51+
- observedProperties
52+
- resultQualifiers
53+
- orchestrationSystems
54+
- dataConnections
55+
- tasks
56+
57+
## Responses and errors
58+
59+
Most methods return an `ApiResponse` with `ok`, `data`, `status`, `message`, and optional `meta` fields.
60+
61+
```ts
62+
const res = await hs.workspaces.list();
63+
if (!res.ok) {
64+
console.error(res.message);
65+
} else {
66+
console.log(res.data);
67+
}
68+
```
69+
70+
## Collections
71+
72+
All collection services support `list`, `listItems`, and `listAllItems`.
73+
74+
- `list` returns an `ApiResponse` containing the page.
75+
- `listItems` returns just the data array (or `[]` on error).
76+
- `listAllItems` fetches all pages.
77+
78+
### Example: Fetch all workspaces you belong to
79+
80+
```ts
81+
const workspaces = await hs.workspaces.listAllItems({
82+
is_associated: true,
83+
expand_related: true,
84+
});
85+
```
86+
87+
### Example: Paginate and sort
88+
89+
```ts
90+
const res = await hs.datastreams.list({
91+
page: 2,
92+
page_size: 50,
93+
order_by: ["name"],
94+
});
95+
```
96+
97+
## Authentication and session
98+
99+
```ts
100+
const loginRes = await hs.session.login("user@example.com", "password");
101+
if (!loginRes.ok) {
102+
console.error(loginRes.message);
103+
}
104+
105+
if (hs.session.isAuthenticated) {
106+
console.log("Logged in");
107+
}
108+
109+
await hs.session.logout();
110+
```
111+
112+
## Workspaces
113+
114+
### Example: Update a workspace
115+
116+
```ts
117+
const workspace = await hs.workspaces.getItem(workspaceId);
118+
if (!workspace) {
119+
throw new Error("Workspace not found");
120+
}
121+
122+
workspace.name = "Updated Workspace Name";
123+
await hs.workspaces.update(workspace);
124+
```
125+
126+
### Example: Manage collaborators
127+
128+
```ts
129+
await hs.workspaces.addCollaborator(workspaceId, "user@example.com", roleId);
130+
await hs.workspaces.updateCollaboratorRole(
131+
workspaceId,
132+
"user@example.com",
133+
roleId
134+
);
135+
await hs.workspaces.removeCollaborator(workspaceId, "user@example.com");
136+
```
137+
138+
## Sites (Things)
139+
140+
### Example: Update site privacy
141+
142+
```ts
143+
await hs.things.updatePrivacy(siteId, true);
144+
```
145+
146+
### Example: Add a tag
147+
148+
```ts
149+
await hs.things.createTag(siteId, { key: "region", value: "utah" });
150+
```
151+
152+
## Datastreams
153+
154+
### Example: List datastreams for a site
155+
156+
```ts
157+
const datastreams = await hs.datastreams.listAllItems({
158+
thing_id: [siteId],
159+
});
160+
```
161+
162+
### Example: Fetch observations
163+
164+
```ts
165+
const res = await hs.datastreams.getObservations(datastreamId, {
166+
order_by: ["phenomenonTime"],
167+
page_size: 1000,
168+
format: "row",
169+
phenomenon_time_min: "2025-01-01T00:00:00Z",
170+
phenomenon_time_max: "2025-02-01T00:00:00Z",
171+
});
172+
```
173+
174+
### Example: Download a CSV
175+
176+
```ts
177+
await hs.datastreams.downloadCsv(datastreamId);
178+
```
179+
180+
## Orchestration systems
181+
182+
### Example: List orchestration systems in a workspace
183+
184+
```ts
185+
const systems = await hs.orchestrationSystems.listAllItems({
186+
workspace_id: [workspaceId],
187+
});
188+
```
189+
190+
## Data connections
191+
192+
### Example: Create a data connection
193+
194+
```ts
195+
import { DataConnection, Workspace } from "@hydroserver/client";
196+
197+
const dataConnection = new DataConnection();
198+
dataConnection.name = "Daily CSV loader";
199+
const workspace = new Workspace();
200+
workspace.id = workspaceId;
201+
dataConnection.workspace = workspace;
202+
dataConnection.extractor.settings.sourceUri = "https://example.com/data.csv";
203+
204+
const res = await hs.dataConnections.create(dataConnection);
205+
```
206+
207+
### Example: List data connections with details
208+
209+
```ts
210+
const connections = await hs.dataConnections.listAllItems({
211+
workspace_id: [workspaceId],
212+
expand_related: true,
213+
order_by: ["name"],
214+
});
215+
```
216+
217+
## Tasks
218+
219+
### Example: Create a task
220+
221+
```ts
222+
import { Task } from "@hydroserver/client";
223+
224+
const task = new Task();
225+
task.name = "Daily load";
226+
task.workspaceId = workspaceId;
227+
task.dataConnectionId = dataConnectionId;
228+
task.orchestrationSystemId = orchestrationSystemId;
229+
230+
const res = await hs.tasks.create(task);
231+
```
232+
233+
### Example: Run a task and inspect status
234+
235+
```ts
236+
await hs.tasks.runTask(taskId);
237+
238+
const tasks = await hs.tasks.listAllItems({
239+
workspace_id: [workspaceId],
240+
expand_related: true,
241+
});
242+
243+
const statusText = hs.tasks.getStatusText(tasks[0]);
244+
```

0 commit comments

Comments
 (0)