@synnaxlabs/client 0.18.3 → 0.19.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@synnaxlabs/client",
3
3
  "private": false,
4
- "version": "0.18.3",
4
+ "version": "0.19.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",
@@ -18,8 +18,8 @@
18
18
  "dependencies": {
19
19
  "async-mutex": "^0.4.0",
20
20
  "zod": "3.22.4",
21
- "@synnaxlabs/freighter": "0.9.4",
22
- "@synnaxlabs/x": "0.15.3"
21
+ "@synnaxlabs/x": "0.15.4",
22
+ "@synnaxlabs/freighter": "0.9.4"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/node": "^20.10.5",
@@ -28,9 +28,9 @@
28
28
  "typescript": "^5.3.3",
29
29
  "vite": "^5.1.2",
30
30
  "vitest": "^1.2.2",
31
- "@synnaxlabs/tsconfig": "0.0.2",
32
31
  "eslint-config-synnaxlabs": "0.0.1",
33
- "@synnaxlabs/vite-plugin": "0.0.1"
32
+ "@synnaxlabs/vite-plugin": "0.0.1",
33
+ "@synnaxlabs/tsconfig": "0.0.2"
34
34
  },
35
35
  "main": "dist/client.cjs",
36
36
  "module": "dist/client.js",
@@ -193,4 +193,31 @@ describe("Channel", () => {
193
193
  expect(retrieved.length).toBeGreaterThan(0);
194
194
  retrieved.forEach((ch) => expect(ch.name).toEqual("test"));
195
195
  });
196
+
197
+ describe("delete", async () => {
198
+ test("delete by key", async () => {
199
+ const channel = await client.channels.create({
200
+ name: "test",
201
+ leaseholder: 1,
202
+ rate: Rate.hz(1),
203
+ dataType: DataType.FLOAT32,
204
+ });
205
+ await client.channels.delete(channel.key);
206
+ await expect(
207
+ async () => await client.channels.retrieve(channel.key),
208
+ ).rejects.toThrow(QueryError);
209
+ });
210
+ test("delete by name", async () => {
211
+ const channel = await client.channels.create({
212
+ name: "test",
213
+ leaseholder: 1,
214
+ rate: Rate.hz(1),
215
+ dataType: DataType.FLOAT32,
216
+ });
217
+ await client.channels.delete(["test"]);
218
+ await expect(
219
+ async () => await client.channels.retrieve(channel.key),
220
+ ).rejects.toThrow(QueryError);
221
+ });
222
+ });
196
223
  });
@@ -20,7 +20,6 @@ import {
20
20
  type CrudeTimeStamp,
21
21
  } from "@synnaxlabs/x";
22
22
 
23
- import { type Creator } from "@/channel/creator";
24
23
  import {
25
24
  type Key,
26
25
  type KeyOrName,
@@ -36,6 +35,7 @@ import {
36
35
  DebouncedBatchRetriever,
37
36
  type Retriever,
38
37
  } from "@/channel/retriever";
38
+ import { type Writer } from "@/channel/writer";
39
39
  import { MultipleResultsError, NoResultsError, ValidationError } from "@/errors";
40
40
  import { type framer } from "@/framer";
41
41
 
@@ -170,20 +170,20 @@ export class Channel {
170
170
  */
171
171
  export class Client implements AsyncTermSearcher<string, Key, Channel> {
172
172
  private readonly frameClient: framer.Client;
173
- readonly retriever: Retriever;
174
- private readonly creator: Creator;
175
173
  private readonly client: UnaryClient;
174
+ readonly retriever: Retriever;
175
+ readonly writer: Writer;
176
176
 
177
177
  constructor(
178
178
  frameClient: framer.Client,
179
179
  retriever: Retriever,
180
180
  client: UnaryClient,
181
- creator: Creator,
181
+ writer: Writer,
182
182
  ) {
183
183
  this.frameClient = frameClient;
184
184
  this.retriever = retriever;
185
185
  this.client = client;
186
- this.creator = creator;
186
+ this.writer = writer;
187
187
  }
188
188
 
189
189
  /**
@@ -261,7 +261,7 @@ export class Client implements AsyncTermSearcher<string, Key, Channel> {
261
261
  toCreate = toCreate.filter((c) => !existingNames.has(c.name));
262
262
  created = this.sugar(res);
263
263
  }
264
- created = created.concat(this.sugar(await this.creator.create(toCreate)));
264
+ created = created.concat(this.sugar(await this.writer.create(toCreate)));
265
265
  return single ? created[0] : created;
266
266
  }
267
267
 
@@ -322,6 +322,12 @@ export class Client implements AsyncTermSearcher<string, Key, Channel> {
322
322
  return res[0];
323
323
  }
324
324
 
325
+ async delete(channels: Params): Promise<void> {
326
+ const { normalized, variant } = analyzeParams(channels);
327
+ if (variant === "keys") return await this.writer.delete({ keys: normalized });
328
+ return await this.writer.delete({ names: normalized });
329
+ }
330
+
325
331
  async search(term: string, rangeKey?: string): Promise<Channel[]> {
326
332
  return this.sugar(await this.retriever.search(term, rangeKey));
327
333
  }
@@ -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
- export * from "@/channel/creator";
10
+ export * from "@/channel/writer";
11
11
  export * from "@/channel/client";
12
12
  export * from "@/channel/payload";
13
13
  export * from "@/channel/retriever";
@@ -36,18 +36,8 @@ export const newPayload = payload.extend({
36
36
  key: z.number().optional(),
37
37
  leaseholder: z.number().optional(),
38
38
  index: z.number().optional(),
39
- rate: Rate.z.optional(),
39
+ rate: Rate.z.optional().default(0),
40
40
  isIndex: z.boolean().optional(),
41
41
  });
42
42
 
43
43
  export type NewPayload = z.input<typeof newPayload>;
44
-
45
- export const parseChannels = (channels: NewPayload[]): NewPayload[] =>
46
- channels.map((channel) => ({
47
- name: channel.name,
48
- dataType: channel.dataType,
49
- rate: channel.rate ?? 0,
50
- leaseholder: channel.leaseholder,
51
- index: channel.index,
52
- isIndex: channel.isIndex,
53
- }));
@@ -22,7 +22,7 @@ import {
22
22
  type Payload,
23
23
  payload,
24
24
  } from "@/channel/payload";
25
- import { QueryError, ValidationError } from "@/errors";
25
+ import { QueryError } from "@/errors";
26
26
 
27
27
  const reqZ = z.object({
28
28
  leaseholder: z.number().optional(),
@@ -0,0 +1,63 @@
1
+ // Copyright 2023 Synnax Labs, Inc.
2
+ //
3
+ // Use of this software is governed by the Business Source License included in the file
4
+ // licenses/BSL.txt.
5
+ //
6
+ // As of the Change Date specified in that file, in accordance with the Business Source
7
+ // License, use of this software will be governed by the Apache License, Version 2.0,
8
+ // included in the file licenses/APL.txt.
9
+
10
+ import { sendRequired, type UnaryClient } from "@synnaxlabs/freighter";
11
+ import { z } from "zod";
12
+
13
+ import {
14
+ type Payload,
15
+ payload,
16
+ newPayload,
17
+ type NewPayload,
18
+ keyZ,
19
+ } from "@/channel/payload";
20
+
21
+ const createReqZ = z.object({ channels: newPayload.array() });
22
+ const createResZ = z.object({ channels: payload.array() });
23
+
24
+ const deleteReqZ = z.object({
25
+ keys: keyZ.array().optional(),
26
+ names: z.string().array().optional(),
27
+ });
28
+ const deleteResZ = z.object({});
29
+
30
+ const CREATE_ENDPOINT = "/channel/create";
31
+ const DELETE_ENDPOINT = "/channel/delete";
32
+
33
+ export type DeleteProps = z.input<typeof deleteReqZ>;
34
+
35
+ export class Writer {
36
+ private readonly client: UnaryClient;
37
+
38
+ constructor(client: UnaryClient) {
39
+ this.client = client;
40
+ }
41
+
42
+ async create(channels: NewPayload[]): Promise<Payload[]> {
43
+ return (
44
+ await sendRequired<typeof createReqZ, typeof createResZ>(
45
+ this.client,
46
+ CREATE_ENDPOINT,
47
+ { channels },
48
+ createReqZ,
49
+ createResZ,
50
+ )
51
+ ).channels;
52
+ }
53
+
54
+ async delete(props: DeleteProps): Promise<void> {
55
+ await sendRequired<typeof deleteReqZ, typeof deleteResZ>(
56
+ this.client,
57
+ DELETE_ENDPOINT,
58
+ props,
59
+ deleteReqZ,
60
+ deleteResZ,
61
+ );
62
+ }
63
+ }
package/src/client.ts CHANGED
@@ -94,7 +94,7 @@ export default class Synnax {
94
94
  const chRetriever = new channel.CacheRetriever(
95
95
  new channel.ClusterRetriever(this.transport.unary),
96
96
  );
97
- const chCreator = new channel.Creator(this.transport.unary);
97
+ const chCreator = new channel.Writer(this.transport.unary);
98
98
  this.telem = new framer.Client(this.transport.stream, chRetriever);
99
99
  this.channels = new channel.Client(
100
100
  this.telem,
@@ -1,8 +0,0 @@
1
- import type { UnaryClient } from "@synnaxlabs/freighter";
2
- import { type Payload, type NewPayload } from './payload';
3
- export declare class Creator {
4
- private static readonly ENDPOINT;
5
- private readonly client;
6
- constructor(client: UnaryClient);
7
- create(channels: NewPayload[]): Promise<Payload[]>;
8
- }
@@ -1,44 +0,0 @@
1
- // Copyright 2023 Synnax Labs, Inc.
2
- //
3
- // Use of this software is governed by the Business Source License included in the file
4
- // licenses/BSL.txt.
5
- //
6
- // As of the Change Date specified in that file, in accordance with the Business Source
7
- // License, use of this software will be governed by the Apache License, Version 2.0,
8
- // included in the file licenses/APL.txt.
9
-
10
- import type { UnaryClient } from "@synnaxlabs/freighter";
11
- import { z } from "zod";
12
-
13
- import {
14
- type Payload,
15
- payload,
16
- parseChannels,
17
- newPayload,
18
- type NewPayload,
19
- } from "@/channel/payload";
20
-
21
- const reqZ = z.object({ channels: newPayload.array() });
22
-
23
- const resZ = z.object({ channels: payload.array() });
24
-
25
- export class Creator {
26
- private static readonly ENDPOINT = "/channel/create";
27
- private readonly client: UnaryClient;
28
-
29
- constructor(client: UnaryClient) {
30
- this.client = client;
31
- }
32
-
33
- async create(channels: NewPayload[]): Promise<Payload[]> {
34
- const req = { channels: parseChannels(channels) };
35
- const [res, err] = await this.client.send<typeof reqZ, typeof resZ>(
36
- Creator.ENDPOINT,
37
- req,
38
- reqZ,
39
- resZ,
40
- );
41
- if (err != null) throw err;
42
- return res.channels;
43
- }
44
- }