@synnaxlabs/client 0.23.1 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/.turbo/turbo-build.log +9 -10
  2. package/dist/auth/auth.d.ts +4 -4
  3. package/dist/channel/client.d.ts +13 -6
  4. package/dist/channel/client.d.ts.map +1 -1
  5. package/dist/channel/creator.d.ts +1 -1
  6. package/dist/channel/payload.d.ts +1 -1
  7. package/dist/channel/retriever.d.ts +8 -6
  8. package/dist/channel/retriever.d.ts.map +1 -1
  9. package/dist/channel/writer.d.ts +5 -3
  10. package/dist/channel/writer.d.ts.map +1 -1
  11. package/dist/client.cjs +22 -18
  12. package/dist/client.d.ts +12 -12
  13. package/dist/client.js +4406 -4865
  14. package/dist/connection/checker.d.ts +2 -2
  15. package/dist/control/client.d.ts +1 -1
  16. package/dist/control/state.d.ts +5 -5
  17. package/dist/errors.d.ts +33 -8
  18. package/dist/errors.d.ts.map +1 -1
  19. package/dist/errors.spec.d.ts +2 -0
  20. package/dist/errors.spec.d.ts.map +1 -0
  21. package/dist/framer/adapter.d.ts +3 -3
  22. package/dist/framer/client.d.ts +12 -11
  23. package/dist/framer/client.d.ts.map +1 -1
  24. package/dist/framer/deleter.d.ts +2 -2
  25. package/dist/framer/frame.d.ts +2 -2
  26. package/dist/framer/iterator.d.ts +16 -6
  27. package/dist/framer/iterator.d.ts.map +1 -1
  28. package/dist/framer/streamProxy.d.ts +1 -1
  29. package/dist/framer/streamer.d.ts +5 -5
  30. package/dist/framer/writer.d.ts +6 -6
  31. package/dist/hardware/client.d.ts +2 -2
  32. package/dist/hardware/device/client.d.ts +7 -7
  33. package/dist/hardware/device/client.d.ts.map +1 -1
  34. package/dist/hardware/rack/client.d.ts +5 -5
  35. package/dist/hardware/task/client.d.ts +22 -11
  36. package/dist/hardware/task/client.d.ts.map +1 -1
  37. package/dist/index.d.ts +2 -3
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/label/client.d.ts +6 -6
  40. package/dist/label/payload.d.ts +1 -1
  41. package/dist/label/payload.d.ts.map +1 -1
  42. package/dist/label/retriever.d.ts +2 -2
  43. package/dist/label/writer.d.ts +3 -3
  44. package/dist/ontology/client.d.ts +10 -10
  45. package/dist/ontology/group/client.d.ts +2 -2
  46. package/dist/ontology/group/external.d.ts +1 -0
  47. package/dist/ontology/group/external.d.ts.map +1 -1
  48. package/dist/ontology/group/payload.d.ts +2 -2
  49. package/dist/ontology/group/payload.d.ts.map +1 -1
  50. package/dist/ontology/group/writer.d.ts +2 -2
  51. package/dist/ontology/group/writer.d.ts.map +1 -1
  52. package/dist/ontology/payload.d.ts +35 -35
  53. package/dist/ontology/payload.d.ts.map +1 -1
  54. package/dist/ontology/writer.d.ts +1 -1
  55. package/dist/ranger/active.d.ts +1 -1
  56. package/dist/ranger/active.d.ts.map +1 -1
  57. package/dist/ranger/alias.d.ts +6 -6
  58. package/dist/ranger/client.d.ts +10 -10
  59. package/dist/ranger/kv.d.ts +2 -2
  60. package/dist/ranger/payload.d.ts +1 -1
  61. package/dist/ranger/range.d.ts +8 -8
  62. package/dist/ranger/range.d.ts.map +1 -1
  63. package/dist/ranger/writer.d.ts +1 -1
  64. package/dist/signals/observable.d.ts +4 -4
  65. package/dist/signals/observable.d.ts.map +1 -1
  66. package/dist/transport.d.ts +1 -1
  67. package/dist/util/retrieve.d.ts +1 -1
  68. package/dist/util/retrieve.d.ts.map +1 -1
  69. package/dist/util/zod.d.ts.map +1 -1
  70. package/dist/workspace/client.d.ts +6 -6
  71. package/dist/workspace/lineplot/client.d.ts +3 -3
  72. package/dist/workspace/lineplot/payload.d.ts +1 -1
  73. package/dist/workspace/lineplot/retriever.d.ts +1 -1
  74. package/dist/workspace/lineplot/writer.d.ts +3 -3
  75. package/dist/workspace/payload.d.ts +1 -1
  76. package/dist/workspace/retriever.d.ts +1 -1
  77. package/dist/workspace/schematic/client.d.ts +3 -3
  78. package/dist/workspace/schematic/payload.d.ts +1 -1
  79. package/dist/workspace/schematic/retriever.d.ts +1 -1
  80. package/dist/workspace/schematic/writer.d.ts +3 -3
  81. package/dist/workspace/writer.d.ts +3 -3
  82. package/examples/node/basicReadWrite.js +4 -4
  83. package/examples/node/package-lock.json +8 -8
  84. package/package.json +10 -11
  85. package/src/auth/auth.ts +1 -1
  86. package/src/channel/channel.spec.ts +6 -5
  87. package/src/channel/client.ts +30 -1
  88. package/src/channel/retriever.ts +60 -10
  89. package/src/channel/writer.ts +13 -10
  90. package/src/client.ts +2 -2
  91. package/src/errors.spec.ts +39 -0
  92. package/src/errors.ts +35 -7
  93. package/src/framer/client.spec.ts +6 -0
  94. package/src/framer/client.ts +25 -18
  95. package/src/framer/deleter.spec.ts +39 -38
  96. package/src/framer/iterator.spec.ts +26 -1
  97. package/src/framer/iterator.ts +15 -1
  98. package/src/framer/streamProxy.ts +1 -1
  99. package/src/framer/streamer.ts +1 -1
  100. package/src/framer/writer.spec.ts +29 -1
  101. package/src/hardware/device/client.ts +2 -2
  102. package/src/hardware/task/client.ts +45 -6
  103. package/src/hardware/task/task.spec.ts +14 -2
  104. package/src/index.ts +3 -2
  105. package/src/ontology/group/external.ts +1 -0
  106. package/src/ontology/group/payload.ts +2 -2
  107. package/src/ontology/group/writer.ts +2 -2
  108. package/src/ontology/ontology.spec.ts +1 -1
  109. package/src/ranger/active.ts +2 -2
@@ -1,5 +1,5 @@
1
- import { z } from 'zod';
2
1
  import { UnknownRecord } from '@synnaxlabs/x/record';
2
+ import { z } from 'zod';
3
3
 
4
4
  export declare const keyZ: z.ZodString;
5
5
  export type Key = z.infer<typeof keyZ>;
@@ -1,5 +1,5 @@
1
- import { LinePlot, Params } from './payload';
2
1
  import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { LinePlot, Params } from './payload';
3
3
 
4
4
  export declare class Retriever {
5
5
  private readonly ENDPOINT;
@@ -1,7 +1,7 @@
1
- import { Key, LinePlot, Params } from './payload';
2
- import { z } from 'zod';
3
- import { UnknownRecord } from '@synnaxlabs/x/record';
4
1
  import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { UnknownRecord } from '@synnaxlabs/x/record';
3
+ import { z } from 'zod';
4
+ import { Key, LinePlot, Params } from './payload';
5
5
 
6
6
  export declare const newLinePlotZ: z.ZodEffects<z.ZodObject<{
7
7
  name: z.ZodString;
@@ -1,5 +1,5 @@
1
- import { z } from 'zod';
2
1
  import { UnknownRecord } from '@synnaxlabs/x/record';
2
+ import { z } from 'zod';
3
3
 
4
4
  export declare const keyZ: z.ZodString;
5
5
  export type Key = z.infer<typeof keyZ>;
@@ -1,5 +1,5 @@
1
- import { Params, Workspace } from './payload';
2
1
  import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { Params, Workspace } from './payload';
3
3
 
4
4
  export declare class Retriever {
5
5
  private static readonly ENDPOINT;
@@ -1,7 +1,7 @@
1
- import { NewSchematic } from './writer';
2
- import { Key, Params, Schematic } from './payload';
3
- import { UnknownRecord } from '@synnaxlabs/x/record';
4
1
  import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { UnknownRecord } from '@synnaxlabs/x/record';
3
+ import { Key, Params, Schematic } from './payload';
4
+ import { NewSchematic } from './writer';
5
5
 
6
6
  export declare class Client {
7
7
  private readonly writer;
@@ -1,5 +1,5 @@
1
- import { z } from 'zod';
2
1
  import { UnknownRecord } from '@synnaxlabs/x/record';
2
+ import { z } from 'zod';
3
3
 
4
4
  export declare const keyZ: z.ZodString;
5
5
  export type Key = z.infer<typeof keyZ>;
@@ -1,5 +1,5 @@
1
- import { Params, Schematic } from './payload';
2
1
  import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { Params, Schematic } from './payload';
3
3
 
4
4
  export declare class Retriever {
5
5
  private readonly ENDPOINT;
@@ -1,7 +1,7 @@
1
- import { Key, Params, Schematic } from './payload';
2
- import { z } from 'zod';
3
- import { UnknownRecord } from '@synnaxlabs/x/record';
4
1
  import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { UnknownRecord } from '@synnaxlabs/x/record';
3
+ import { z } from 'zod';
4
+ import { Key, Params, Schematic } from './payload';
5
5
 
6
6
  export declare const newSchematicZ: z.ZodEffects<z.ZodObject<{
7
7
  name: z.ZodString;
@@ -1,7 +1,7 @@
1
- import { Workspace } from './payload';
2
- import { z } from 'zod';
3
- import { UnknownRecord } from '@synnaxlabs/x/record';
4
1
  import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { UnknownRecord } from '@synnaxlabs/x/record';
3
+ import { z } from 'zod';
4
+ import { Workspace } from './payload';
5
5
 
6
6
  declare const newWorkspaceZ: z.ZodEffects<z.ZodObject<{
7
7
  name: z.ZodString;
@@ -30,7 +30,7 @@ const timeChannel = await client.channels.create({
30
30
  });
31
31
 
32
32
  // Create a channel that will be used to store our data.
33
- const indexChannel = await client.channels.create({
33
+ const dataChannel = await client.channels.create({
34
34
  name: "basic_read_write_data",
35
35
  isIndex: false,
36
36
  dataType: DataType.FLOAT32,
@@ -54,14 +54,14 @@ const data = Float32Array.from({ length: N_SAMPLES }, (_, i) => Math.sin(i / 100
54
54
  // otherwise writing the data will fail. Notice how we align the writes with the 'start'
55
55
  // timestamp.
56
56
  await timeChannel.write(start, time);
57
- await indexChannel.write(start, data);
57
+ await dataChannel.write(start, data);
58
58
 
59
59
  // Define the time range to read the data back from
60
60
  const tr = new TimeRange(start, start.add(TimeSpan.milliseconds(N_SAMPLES)));
61
61
 
62
62
  // Read the data back. The order doesn't matter here.
63
63
  const readTime = await timeChannel.read(tr);
64
- const readData = await indexChannel.read(tr);
64
+ const readData = await dataChannel.read(tr);
65
65
 
66
66
  // Print out some information.
67
67
  console.log({
@@ -73,4 +73,4 @@ console.log({
73
73
  });
74
74
 
75
75
  // Make sure to close the client when you're done.
76
- client.close();
76
+ client.close();
@@ -9,7 +9,7 @@
9
9
  "version": "1.0.0",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "@synnaxlabs/client": "^0.20.0"
12
+ "@synnaxlabs/client": "^0.21.0"
13
13
  }
14
14
  },
15
15
  "node_modules/@grpc/grpc-js": {
@@ -3653,12 +3653,12 @@
3653
3653
  }
3654
3654
  },
3655
3655
  "node_modules/@synnaxlabs/client": {
3656
- "version": "0.20.0",
3657
- "resolved": "https://registry.npmjs.org/@synnaxlabs/client/-/client-0.20.0.tgz",
3658
- "integrity": "sha512-L4Qve63NTNmqdFiDlDy/rgM1yzAxkyaw6hf+SrsGLSb/u871Hwp/yUoUhYRpY1JjhvjL2N7YPXpr5Y+n8/y5iQ==",
3656
+ "version": "0.21.0",
3657
+ "resolved": "https://registry.npmjs.org/@synnaxlabs/client/-/client-0.21.0.tgz",
3658
+ "integrity": "sha512-WZ5TQ7TaSZgE2AVQSVdj/4XX5mEIEr5fGLzrfI6kSi1vqm8l1Jo9nmpr/81AN8PLz1THMCd5lkz20qmSGtcx8g==",
3659
3659
  "dependencies": {
3660
3660
  "@synnaxlabs/freighter": "0.9.4",
3661
- "@synnaxlabs/x": "0.16.0",
3661
+ "@synnaxlabs/x": "0.16.1",
3662
3662
  "async-mutex": "^0.4.1",
3663
3663
  "zod": "3.22.4"
3664
3664
  }
@@ -3692,9 +3692,9 @@
3692
3692
  }
3693
3693
  },
3694
3694
  "node_modules/@synnaxlabs/x": {
3695
- "version": "0.16.0",
3696
- "resolved": "https://registry.npmjs.org/@synnaxlabs/x/-/x-0.16.0.tgz",
3697
- "integrity": "sha512-7sJKLZfPR0I9Bpgfe3UYqiGoFhxQ7FgmsEXBwXggThbUdzEl8JsydSQfR6e4GC0OKZZVrGZc0gTdDeSdYQHCKw==",
3695
+ "version": "0.16.1",
3696
+ "resolved": "https://registry.npmjs.org/@synnaxlabs/x/-/x-0.16.1.tgz",
3697
+ "integrity": "sha512-vIqspjkkOregeriSQotu56NQPuX8xLV/7usudYILS3tKLzOCQQ2nR2r3lkVLseSMeiNxw3nLeY+roH3KxK13pA==",
3698
3698
  "dependencies": {
3699
3699
  "async-mutex": "^0.4.1",
3700
3700
  "js-convert-case": "^4.2.0",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@synnaxlabs/client",
3
3
  "private": false,
4
- "version": "0.23.1",
4
+ "version": "0.25.0",
5
5
  "type": "module",
6
6
  "description": "The Client Library for Synnax",
7
7
  "repository": "https://github.com/synnaxlabs/synnax/tree/main/client/ts",
@@ -17,20 +17,19 @@
17
17
  ],
18
18
  "dependencies": {
19
19
  "async-mutex": "^0.5.0",
20
- "nanoid": "^3.0.0",
21
20
  "zod": "3.23.8",
22
- "@synnaxlabs/x": "0.23.0",
23
- "@synnaxlabs/freighter": "0.23.0"
21
+ "@synnaxlabs/freighter": "0.25.0",
22
+ "@synnaxlabs/x": "0.25.0"
24
23
  },
25
24
  "devDependencies": {
26
- "@types/node": "^20.12.7",
27
- "@vitest/coverage-v8": "^1.5.0",
28
- "eslint": "^9.0.0",
29
- "typescript": "^5.4.5",
30
- "vite": "5.2.11",
31
- "vitest": "^1.5.0",
32
- "@synnaxlabs/vite-plugin": "0.0.1",
25
+ "@types/node": "^20.14.9",
26
+ "@vitest/coverage-v8": "^1.6.0",
27
+ "eslint": "^9.6.0",
28
+ "typescript": "^5.5.3",
29
+ "vite": "5.3.3",
30
+ "vitest": "^1.6.0",
33
31
  "@synnaxlabs/tsconfig": "0.0.2",
32
+ "@synnaxlabs/vite-plugin": "0.0.1",
34
33
  "eslint-config-synnaxlabs": "0.0.1"
35
34
  },
36
35
  "peerDependencies": {
package/src/auth/auth.ts CHANGED
@@ -73,7 +73,7 @@ export class Client {
73
73
  }
74
74
  reqCtx.params.Authorization = `Bearer ${this.token}`;
75
75
  const [resCtx, err] = await next(reqCtx);
76
- if (err instanceof InvalidTokenError && this.retryCount < MAX_RETRIES) {
76
+ if (InvalidTokenError.matches(err) && this.retryCount < MAX_RETRIES) {
77
77
  this.authenticated = false;
78
78
  this.authenticating = undefined;
79
79
  this.retryCount += 1;
@@ -246,14 +246,15 @@ describe("Channel", () => {
246
246
  dataType: DataType.FLOAT32,
247
247
  },
248
248
  ]);
249
+ // Retrieve channels here to ensure we check for cache invalidation
250
+ const initial = await client.channels.retrieve(channels.map((c) => c.key));
251
+ expect(initial[0].name).toEqual("test1");
252
+ expect(initial[1].name).toEqual("test2");
249
253
  await client.channels.rename(
250
- [channels[0].key, channels[1].key],
254
+ channels.map((c) => c.key),
251
255
  ["test3", "test4"],
252
256
  );
253
- const renamed = await client.channels.retrieve([
254
- channels[0].key,
255
- channels[1].key,
256
- ]);
257
+ const renamed = await client.channels.retrieve(channels.map((c) => c.key));
257
258
  expect(renamed[0].name).toEqual("test3");
258
259
  expect(renamed[1].name).toEqual("test4");
259
260
  });
@@ -7,7 +7,7 @@
7
7
  // License, use of this software will be governed by the Apache License, Version 2.0,
8
8
  // included in the file licenses/APL.txt.
9
9
 
10
- import { type UnaryClient } from "@synnaxlabs/freighter";
10
+ import { sendRequired, type UnaryClient } from "@synnaxlabs/freighter";
11
11
  import { type AsyncTermSearcher } from "@synnaxlabs/x/search";
12
12
  import {
13
13
  type CrudeDensity,
@@ -19,6 +19,7 @@ import {
19
19
  type TypedArray,
20
20
  } from "@synnaxlabs/x/telem";
21
21
  import { toArray } from "@synnaxlabs/x/toArray";
22
+ import { z } from "zod";
22
23
 
23
24
  import {
24
25
  type Key,
@@ -39,6 +40,8 @@ import {
39
40
  import { type Writer } from "@/channel/writer";
40
41
  import { ValidationError } from "@/errors";
41
42
  import { type framer } from "@/framer";
43
+ import { ontology } from "@/ontology";
44
+ import { group } from "@/ontology/group";
42
45
  import { checkForMultipleOrNoResults } from "@/util/retrieve";
43
46
 
44
47
  interface CreateOptions {
@@ -150,6 +153,13 @@ export class Channel {
150
153
  });
151
154
  }
152
155
 
156
+ /***
157
+ * @returns the ontology ID of the channel
158
+ */
159
+ get ontologyID(): ontology.ID {
160
+ return new ontology.ID({ type: "channel", key: this.key.toString() });
161
+ }
162
+
153
163
  /**
154
164
  * Reads telemetry from the channel between the two timestamps.
155
165
  *
@@ -172,6 +182,14 @@ export class Channel {
172
182
  }
173
183
  }
174
184
 
185
+ const RETRIEVE_GROUP_ENDPOINT = "/channel/retrieve-group";
186
+
187
+ const retrieveGroupReqZ = z.object({});
188
+
189
+ const retrieveGroupResZ = z.object({
190
+ group: group.groupZ,
191
+ });
192
+
175
193
  /**
176
194
  * The core client class for executing channel operations against a Synnax
177
195
  * cluster. This class should not be instantiated directly, and instead should be used
@@ -382,4 +400,15 @@ export class Client implements AsyncTermSearcher<string, Key, Channel> {
382
400
  const { frameClient } = this;
383
401
  return payloads.map((p) => new Channel({ ...p, frameClient }));
384
402
  }
403
+
404
+ async retrieveGroup(): Promise<group.Group> {
405
+ const res = await sendRequired(
406
+ this.client,
407
+ RETRIEVE_GROUP_ENDPOINT,
408
+ {},
409
+ retrieveGroupReqZ,
410
+ retrieveGroupResZ,
411
+ );
412
+ return new group.Group(res.group.name, res.group.key);
413
+ }
385
414
  }
@@ -103,7 +103,7 @@ export class ClusterRetriever implements Retriever {
103
103
 
104
104
  export class CacheRetriever implements Retriever {
105
105
  private readonly cache: Map<number, Payload>;
106
- private readonly namesToKeys: Map<string, number>;
106
+ private readonly namesToKeys: Map<string, Set<number>>;
107
107
  private readonly wrapped: Retriever;
108
108
 
109
109
  constructor(wrapped: Retriever) {
@@ -128,27 +128,77 @@ export class CacheRetriever implements Retriever {
128
128
  const results: Payload[] = [];
129
129
  const toFetch: KeysOrNames = [];
130
130
  normalized.forEach((keyOrName) => {
131
- const c = this.getFromCache(keyOrName);
132
- if (c != null) results.push(c);
131
+ const c = this.get(keyOrName);
132
+ if (c != null) results.push(...c);
133
133
  else toFetch.push(keyOrName as never);
134
134
  });
135
135
  if (toFetch.length === 0) return results;
136
136
  const fetched = await this.wrapped.retrieve(toFetch, options);
137
- this.updateCache(fetched);
137
+ this.set(fetched);
138
138
  return results.concat(fetched);
139
139
  }
140
140
 
141
- private updateCache(channels: Payload[]): void {
141
+ delete(channels: Params): void {
142
+ const { variant, normalized } = analyzeChannelParams(channels);
143
+ if (variant === "names")
144
+ (normalized as string[]).forEach((name) => {
145
+ const keys = this.namesToKeys.get(name);
146
+ if (keys == null) return;
147
+ keys.forEach((k) => this.cache.delete(k));
148
+ this.namesToKeys.delete(name);
149
+ });
150
+ else
151
+ (normalized as number[]).forEach((key) => {
152
+ const channel = this.cache.get(key);
153
+ if (channel == null) return;
154
+ this.cache.delete(key);
155
+ this.namesToKeys.delete(channel.name);
156
+ });
157
+ }
158
+
159
+ rename(keys: Key[], names: string[]): void {
160
+ keys.forEach((key, i) => {
161
+ const name = names[i];
162
+ const ch = this.cache.get(key);
163
+ if (ch == null) return;
164
+ this.cache.delete(key);
165
+ const keys = this.namesToKeys.get(ch.name);
166
+ if (keys != null) {
167
+ keys.delete(key);
168
+ if (keys.size === 0) this.namesToKeys.delete(ch.name);
169
+ }
170
+ ch.name = name;
171
+ this.cache.set(key, ch);
172
+ const newKeys = this.namesToKeys.get(name);
173
+ if (newKeys == null) this.namesToKeys.set(name, new Set([key]));
174
+ else newKeys.add(key);
175
+ });
176
+ }
177
+
178
+ set(channels: Payload[]): void {
142
179
  channels.forEach((channel) => {
143
180
  this.cache.set(channel.key, channel);
144
- this.namesToKeys.set(channel.name, channel.key);
181
+ const keys = this.namesToKeys.get(channel.name);
182
+ if (keys == null) this.namesToKeys.set(channel.name, new Set([channel.key]));
183
+ else keys.add(channel.key);
145
184
  });
146
185
  }
147
186
 
148
- private getFromCache(channel: KeyOrName): Payload | undefined {
149
- const key = typeof channel === "number" ? channel : this.namesToKeys.get(channel);
150
- if (key == null) return undefined;
151
- return this.cache.get(key);
187
+ private get(channel: KeyOrName): Payload[] | undefined {
188
+ if (typeof channel === "number") {
189
+ const ch = this.cache.get(channel);
190
+ if (ch == null) return undefined;
191
+ return [ch];
192
+ }
193
+ const keys = this.namesToKeys.get(channel);
194
+ if (keys == null) return undefined;
195
+ const channels: Payload[] = [];
196
+ keys.forEach((key) => {
197
+ const ch = this.cache.get(key);
198
+ if (ch != null) channels.push(ch);
199
+ });
200
+ if (channels.length === 0) return undefined;
201
+ return channels;
152
202
  }
153
203
  }
154
204
 
@@ -18,6 +18,7 @@ import {
18
18
  type Payload,
19
19
  payload,
20
20
  } from "@/channel/payload";
21
+ import { CacheRetriever } from "@/channel/retriever";
21
22
 
22
23
  const createReqZ = z.object({ channels: newPayload.array() });
23
24
  const createResZ = z.object({ channels: payload.array() });
@@ -43,21 +44,20 @@ export type RenameProps = z.input<typeof renameReqZ>;
43
44
 
44
45
  export class Writer {
45
46
  private readonly client: UnaryClient;
47
+ private readonly cache: CacheRetriever;
46
48
 
47
- constructor(client: UnaryClient) {
49
+ constructor(client: UnaryClient, cache: CacheRetriever) {
48
50
  this.client = client;
51
+ this.cache = cache;
49
52
  }
50
53
 
51
54
  async create(channels: NewPayload[]): Promise<Payload[]> {
52
- return (
53
- await sendRequired<typeof createReqZ, typeof createResZ>(
54
- this.client,
55
- CREATE_ENDPOINT,
56
- { channels },
57
- createReqZ,
58
- createResZ,
59
- )
60
- ).channels;
55
+ const { channels: created } = await sendRequired<
56
+ typeof createReqZ,
57
+ typeof createResZ
58
+ >(this.client, CREATE_ENDPOINT, { channels }, createReqZ, createResZ);
59
+ this.cache.set(created);
60
+ return created;
61
61
  }
62
62
 
63
63
  async delete(props: DeleteProps): Promise<void> {
@@ -68,6 +68,8 @@ export class Writer {
68
68
  deleteReqZ,
69
69
  deleteResZ,
70
70
  );
71
+ if (props.keys != null) this.cache.delete(props.keys);
72
+ if (props.names != null) this.cache.delete(props.names);
71
73
  }
72
74
 
73
75
  async rename(keys: Key[], names: string[]): Promise<void> {
@@ -78,5 +80,6 @@ export class Writer {
78
80
  renameReqZ,
79
81
  renameResZ,
80
82
  );
83
+ this.cache.rename(keys, names);
81
84
  }
82
85
  }
package/src/client.ts CHANGED
@@ -83,7 +83,7 @@ export default class Synnax extends framer.Client {
83
83
  * @param props.password - Password for authentication. Not required if the
84
84
  * cluster is insecure.
85
85
  * @param props.connectivityPollFrequency - Frequency at which to poll the
86
- * cluster for connectivity information. Defaults to 5 seconds.
86
+ * cluster for connectivity information. Defaults to 30 seconds.
87
87
  * @param props.secure - Whether to connect to the cluster using TLS. The cluster
88
88
  * must be configured to support TLS. Defaults to false.
89
89
  *
@@ -106,7 +106,7 @@ export default class Synnax extends framer.Client {
106
106
  const chRetriever = new channel.CacheRetriever(
107
107
  new channel.ClusterRetriever(transport.unary),
108
108
  );
109
- const chCreator = new channel.Writer(transport.unary);
109
+ const chCreator = new channel.Writer(transport.unary, chRetriever);
110
110
  super(transport.stream, transport.unary, chRetriever);
111
111
  this.createdAt = TimeStamp.now();
112
112
  this.props = props;
@@ -0,0 +1,39 @@
1
+ import { MatchableErrorType } from "@synnaxlabs/freighter/src/errors";
2
+ import { describe, expect, test } from "vitest";
3
+
4
+ import {
5
+ AuthError,
6
+ ContiguityError,
7
+ ControlError,
8
+ FieldError,
9
+ InvalidTokenError,
10
+ MultipleFoundError,
11
+ NotFoundError,
12
+ QueryError,
13
+ RouteError,
14
+ UnauthorizedError,
15
+ UnexpectedError,
16
+ ValidationError,
17
+ } from "@/errors";
18
+
19
+ describe("error", () => {
20
+ describe("type matching", () => {
21
+ const ERRORS: [string, Error, MatchableErrorType][] = [
22
+ [ValidationError.TYPE, new ValidationError(), ValidationError],
23
+ [FieldError.TYPE, new FieldError("field", "message"), FieldError],
24
+ [AuthError.TYPE, new AuthError(), AuthError],
25
+ [InvalidTokenError.TYPE, new InvalidTokenError(), InvalidTokenError],
26
+ [UnexpectedError.TYPE, new UnexpectedError("message"), UnexpectedError],
27
+ [QueryError.TYPE, new QueryError("message"), QueryError],
28
+ [NotFoundError.TYPE, new NotFoundError("message"), NotFoundError],
29
+ [MultipleFoundError.TYPE, new MultipleFoundError("message"), MultipleFoundError],
30
+ [RouteError.TYPE, new RouteError("message", ""), RouteError],
31
+ [ControlError.TYPE, new ControlError("message"), ControlError],
32
+ [UnauthorizedError.TYPE, new UnauthorizedError("message"), UnauthorizedError],
33
+ [ContiguityError.TYPE, new ContiguityError("message"), ContiguityError],
34
+ ];
35
+ ERRORS.forEach(([typeName, error, type]) =>
36
+ test(`matches ${typeName}`, () => expect(type.matches(error)).toBeTruthy()),
37
+ );
38
+ });
39
+ });
package/src/errors.ts CHANGED
@@ -8,6 +8,8 @@
8
8
  // included in the file licenses/APL.txt.
9
9
 
10
10
  import {
11
+ BaseTypedError,
12
+ errorMatcher,
11
13
  type ErrorPayload,
12
14
  type Middleware,
13
15
  registerError,
@@ -24,12 +26,16 @@ export interface Field {
24
26
  /**
25
27
  * Raised when a validation error occurs.
26
28
  */
27
- export class ValidationError extends Error {
29
+ export class ValidationError extends BaseTypedError {
28
30
  static readonly TYPE = _FREIGHTER_EXCEPTION_PREFIX + "validation";
31
+ type = ValidationError.TYPE;
32
+ static readonly matches = errorMatcher(ValidationError.TYPE);
29
33
  }
30
34
 
31
35
  export class FieldError extends ValidationError {
32
36
  static readonly TYPE = ValidationError.TYPE + ".field";
37
+ type = FieldError.TYPE;
38
+ static readonly matches = errorMatcher(FieldError.TYPE);
33
39
  readonly field: string;
34
40
  readonly message: string;
35
41
 
@@ -43,8 +49,10 @@ export class FieldError extends ValidationError {
43
49
  /**
44
50
  * AuthError is raised when an authentication error occurs.
45
51
  */
46
- export class AuthError extends Error {
52
+ export class AuthError extends BaseTypedError {
47
53
  static readonly TYPE = _FREIGHTER_EXCEPTION_PREFIX + "auth";
54
+ type = AuthError.TYPE;
55
+ static readonly matches = errorMatcher(AuthError.TYPE);
48
56
  }
49
57
 
50
58
  /**
@@ -52,13 +60,17 @@ export class AuthError extends Error {
52
60
  */
53
61
  export class InvalidTokenError extends AuthError {
54
62
  static readonly TYPE = AuthError.TYPE + ".invalid-token";
63
+ type = InvalidTokenError.TYPE;
64
+ static readonly matches = errorMatcher(InvalidTokenError.TYPE);
55
65
  }
56
66
 
57
67
  /**
58
68
  * UnexpectedError is raised when an unexpected error occurs.
59
69
  */
60
- export class UnexpectedError extends Error {
70
+ export class UnexpectedError extends BaseTypedError {
61
71
  static readonly TYPE = _FREIGHTER_EXCEPTION_PREFIX + "unexpected";
72
+ type = UnexpectedError.TYPE;
73
+ static readonly matches = errorMatcher(UnexpectedError.TYPE);
62
74
 
63
75
  constructor(message: string) {
64
76
  super(`
@@ -74,23 +86,31 @@ export class UnexpectedError extends Error {
74
86
  /**
75
87
  * QueryError is raised when a query error occurs.
76
88
  */
77
- export class QueryError extends Error {
89
+ export class QueryError extends BaseTypedError {
78
90
  static readonly TYPE = _FREIGHTER_EXCEPTION_PREFIX + "query";
91
+ type = QueryError.TYPE;
92
+ static readonly matches = errorMatcher(QueryError.TYPE);
79
93
  }
80
94
 
81
95
  export class NotFoundError extends QueryError {
82
96
  static readonly TYPE = QueryError.TYPE + ".not_found";
97
+ type = NotFoundError.TYPE;
98
+ static readonly matches = errorMatcher(NotFoundError.TYPE);
83
99
  }
84
100
 
85
101
  export class MultipleFoundError extends QueryError {
86
102
  static readonly TYPE = QueryError.TYPE + ".multiple_results";
103
+ type = MultipleFoundError.TYPE;
104
+ static readonly matches = errorMatcher(MultipleFoundError.TYPE);
87
105
  }
88
106
 
89
107
  /**
90
108
  * RouteError is raised when a routing error occurs.
91
109
  */
92
- export class RouteError extends Error {
110
+ export class RouteError extends BaseTypedError {
93
111
  static readonly TYPE = _FREIGHTER_EXCEPTION_PREFIX + "route";
112
+ type = RouteError.TYPE;
113
+ static readonly matches = errorMatcher(RouteError.TYPE);
94
114
  path: string;
95
115
 
96
116
  constructor(message: string, path: string) {
@@ -99,18 +119,26 @@ export class RouteError extends Error {
99
119
  }
100
120
  }
101
121
 
102
- export class ControlError extends Error {
122
+ export class ControlError extends BaseTypedError {
103
123
  static readonly TYPE = _FREIGHTER_EXCEPTION_PREFIX + "control";
124
+ type = ControlError.TYPE;
125
+ static readonly matches = errorMatcher(ControlError.TYPE);
104
126
  }
105
127
 
106
128
  export class UnauthorizedError extends ControlError {
107
129
  static readonly TYPE = ControlError.TYPE + ".unauthorized";
130
+ type = UnauthorizedError.TYPE;
131
+ static readonly matches = errorMatcher(UnauthorizedError.TYPE);
108
132
  }
109
133
 
110
134
  /**
111
135
  * Raised when time-series data is not contiguous.
112
136
  */
113
- export class ContiguityError extends Error {}
137
+ export class ContiguityError extends BaseTypedError {
138
+ static readonly TYPE = _FREIGHTER_EXCEPTION_PREFIX + "contiguity";
139
+ type = ContiguityError.TYPE;
140
+ static readonly matches = errorMatcher(ContiguityError.TYPE);
141
+ }
114
142
 
115
143
  const decode = (payload: ErrorPayload): Error | null => {
116
144
  if (!payload.type.startsWith(_FREIGHTER_EXCEPTION_PREFIX)) return null;
@@ -60,4 +60,10 @@ describe("Client", () => {
60
60
  await client.write(start, data.key, 1);
61
61
  });
62
62
  });
63
+ describe("retrieveGroup", () => {
64
+ it("should correctly retrieve the main channel group", async () => {
65
+ const group = await client.channels.retrieveGroup();
66
+ expect(group.name).toEqual("Channels");
67
+ });
68
+ });
63
69
  });