@synnaxlabs/client 0.35.0 → 0.36.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 (121) hide show
  1. package/.turbo/turbo-build.log +7 -7
  2. package/dist/client.cjs +28 -28
  3. package/dist/client.d.ts +35 -0
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/client.js +3078 -2955
  6. package/dist/control/state.d.ts.map +1 -1
  7. package/dist/framer/deleter.d.ts.map +1 -1
  8. package/dist/framer/frame.d.ts +14 -7
  9. package/dist/framer/frame.d.ts.map +1 -1
  10. package/dist/framer/writer.d.ts +8 -8
  11. package/dist/framer/writer.d.ts.map +1 -1
  12. package/dist/hardware/device/payload.d.ts +2 -2
  13. package/dist/hardware/device/payload.d.ts.map +1 -1
  14. package/dist/hardware/task/payload.d.ts +4 -4
  15. package/dist/hardware/task/payload.d.ts.map +1 -1
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/ontology/group/payload.d.ts +3 -0
  19. package/dist/ontology/group/payload.d.ts.map +1 -1
  20. package/dist/ranger/client.d.ts +1 -0
  21. package/dist/ranger/client.d.ts.map +1 -1
  22. package/dist/ranger/payload.d.ts.map +1 -1
  23. package/dist/ranger/writer.d.ts.map +1 -1
  24. package/dist/transport.d.ts +2 -1
  25. package/dist/transport.d.ts.map +1 -1
  26. package/dist/workspace/client.d.ts +68 -11
  27. package/dist/workspace/client.d.ts.map +1 -1
  28. package/dist/workspace/index.d.ts +1 -1
  29. package/dist/workspace/index.d.ts.map +1 -1
  30. package/dist/workspace/lineplot/client.d.ts +48 -6
  31. package/dist/workspace/lineplot/client.d.ts.map +1 -1
  32. package/dist/workspace/lineplot/index.d.ts +1 -1
  33. package/dist/workspace/lineplot/index.d.ts.map +1 -1
  34. package/dist/workspace/log/client.d.ts +48 -6
  35. package/dist/workspace/log/client.d.ts.map +1 -1
  36. package/dist/workspace/log/index.d.ts +1 -1
  37. package/dist/workspace/log/index.d.ts.map +1 -1
  38. package/dist/workspace/schematic/client.d.ts +72 -6
  39. package/dist/workspace/schematic/client.d.ts.map +1 -1
  40. package/dist/workspace/schematic/index.d.ts +1 -1
  41. package/dist/workspace/schematic/index.d.ts.map +1 -1
  42. package/dist/workspace/table/client.d.ts +61 -6
  43. package/dist/workspace/table/client.d.ts.map +1 -1
  44. package/dist/workspace/table/index.d.ts +1 -1
  45. package/dist/workspace/table/index.d.ts.map +1 -1
  46. package/package.json +9 -9
  47. package/src/client.ts +16 -2
  48. package/src/control/state.ts +1 -1
  49. package/src/framer/frame.spec.ts +32 -3
  50. package/src/framer/frame.ts +17 -0
  51. package/src/index.ts +1 -0
  52. package/src/ontology/group/payload.ts +8 -7
  53. package/src/ranger/client.ts +4 -1
  54. package/src/transport.ts +8 -3
  55. package/src/workspace/client.ts +148 -47
  56. package/src/workspace/index.ts +1 -1
  57. package/src/workspace/lineplot/client.ts +96 -28
  58. package/src/workspace/lineplot/index.ts +1 -1
  59. package/src/workspace/log/client.ts +93 -28
  60. package/src/workspace/log/index.ts +1 -1
  61. package/src/workspace/schematic/client.ts +121 -29
  62. package/src/workspace/schematic/index.ts +1 -1
  63. package/src/workspace/table/client.ts +102 -28
  64. package/src/workspace/table/index.ts +1 -1
  65. package/dist/workspace/external.d.ts +0 -3
  66. package/dist/workspace/external.d.ts.map +0 -1
  67. package/dist/workspace/lineplot/external.d.ts +0 -3
  68. package/dist/workspace/lineplot/external.d.ts.map +0 -1
  69. package/dist/workspace/lineplot/retriever.d.ts +0 -9
  70. package/dist/workspace/lineplot/retriever.d.ts.map +0 -1
  71. package/dist/workspace/lineplot/writer.d.ts +0 -35
  72. package/dist/workspace/lineplot/writer.d.ts.map +0 -1
  73. package/dist/workspace/log/external.d.ts +0 -3
  74. package/dist/workspace/log/external.d.ts.map +0 -1
  75. package/dist/workspace/log/payload.d.ts +0 -36
  76. package/dist/workspace/log/payload.d.ts.map +0 -1
  77. package/dist/workspace/log/retriever.d.ts +0 -9
  78. package/dist/workspace/log/retriever.d.ts.map +0 -1
  79. package/dist/workspace/log/writer.d.ts +0 -35
  80. package/dist/workspace/log/writer.d.ts.map +0 -1
  81. package/dist/workspace/payload.d.ts +0 -38
  82. package/dist/workspace/payload.d.ts.map +0 -1
  83. package/dist/workspace/retriever.d.ts +0 -13
  84. package/dist/workspace/retriever.d.ts.map +0 -1
  85. package/dist/workspace/schematic/external.d.ts +0 -3
  86. package/dist/workspace/schematic/external.d.ts.map +0 -1
  87. package/dist/workspace/schematic/payload.d.ts +0 -42
  88. package/dist/workspace/schematic/payload.d.ts.map +0 -1
  89. package/dist/workspace/schematic/retriever.d.ts +0 -9
  90. package/dist/workspace/schematic/retriever.d.ts.map +0 -1
  91. package/dist/workspace/schematic/writer.d.ts +0 -41
  92. package/dist/workspace/schematic/writer.d.ts.map +0 -1
  93. package/dist/workspace/table/external.d.ts +0 -3
  94. package/dist/workspace/table/external.d.ts.map +0 -1
  95. package/dist/workspace/table/payload.d.ts +0 -36
  96. package/dist/workspace/table/payload.d.ts.map +0 -1
  97. package/dist/workspace/table/retriever.d.ts +0 -8
  98. package/dist/workspace/table/retriever.d.ts.map +0 -1
  99. package/dist/workspace/table/writer.d.ts +0 -35
  100. package/dist/workspace/table/writer.d.ts.map +0 -1
  101. package/dist/workspace/writer.d.ts +0 -66
  102. package/dist/workspace/writer.d.ts.map +0 -1
  103. package/src/workspace/external.ts +0 -11
  104. package/src/workspace/lineplot/external.ts +0 -11
  105. package/src/workspace/lineplot/retriever.ts +0 -34
  106. package/src/workspace/lineplot/writer.ts +0 -115
  107. package/src/workspace/log/external.ts +0 -11
  108. package/src/workspace/log/payload.ts +0 -36
  109. package/src/workspace/log/retriever.ts +0 -38
  110. package/src/workspace/log/writer.ts +0 -116
  111. package/src/workspace/payload.ts +0 -44
  112. package/src/workspace/retriever.ts +0 -70
  113. package/src/workspace/schematic/external.ts +0 -11
  114. package/src/workspace/schematic/payload.ts +0 -38
  115. package/src/workspace/schematic/retriever.ts +0 -42
  116. package/src/workspace/schematic/writer.ts +0 -140
  117. package/src/workspace/table/external.ts +0 -11
  118. package/src/workspace/table/payload.ts +0 -36
  119. package/src/workspace/table/retriever.ts +0 -33
  120. package/src/workspace/table/writer.ts +0 -102
  121. package/src/workspace/writer.ts +0 -111
@@ -1,23 +1,72 @@
1
- // Copyright 2024 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 { type UnknownRecord } from "@synnaxlabs/x/record";
12
- import { type AsyncTermSearcher } from "@synnaxlabs/x/search";
13
-
1
+ import { ontology } from "@/ontology";
2
+ import { sendRequired, UnaryClient } from "@synnaxlabs/freighter";
3
+ import { toArray, UnknownRecord } from "@synnaxlabs/x";
4
+ import { unknownRecordZ } from "@synnaxlabs/x/record";
5
+ import { z } from "zod";
6
+ import { AsyncTermSearcher } from "@synnaxlabs/x/search";
14
7
  import { linePlot } from "@/workspace/lineplot";
15
8
  import { log } from "@/workspace/log";
16
- import { type Key, type Workspace } from "@/workspace/payload";
17
- import { Retriever } from "@/workspace/retriever";
18
9
  import { schematic } from "@/workspace/schematic";
19
10
  import { table } from "@/workspace/table";
20
- import { type NewWorkspace, Writer } from "@/workspace/writer";
11
+ import { nullableArrayZ } from "@/util/zod";
12
+
13
+ export const keyZ = z.string().uuid();
14
+ export type Key = z.infer<typeof keyZ>;
15
+ export type Params = Key | Key[];
16
+
17
+ // --- VERY IMPORTANT ---
18
+ // Synnax's encoders (in the binary package inside x) automatically convert the case
19
+ // of keys in objects to snake_case and back to camelCase when encoding and decoding
20
+ // respectively. This is done to ensure that the keys are consistent across all
21
+ // languages and platforms. Sometimes workspaces have keys that are uuids, which have
22
+ // dashes, and those get messed up. So we just use regular JSON for workspaces.
23
+ const parse = (s: string): UnknownRecord => JSON.parse(s) as UnknownRecord;
24
+
25
+ export const workspaceZ = z.object({
26
+ key: z.string(),
27
+ name: z.string(),
28
+ layout: unknownRecordZ.or(z.string().transform((s) => parse(s) as UnknownRecord)),
29
+ });
30
+
31
+ export type Workspace = z.infer<typeof workspaceZ>;
32
+
33
+ export const ONTOLOGY_TYPE: ontology.ResourceType = "workspace";
34
+
35
+ export const ontologyID = (key: Key): ontology.ID =>
36
+ new ontology.ID({ type: ONTOLOGY_TYPE, key });
37
+
38
+ const RETRIEVE_ENDPOINT = "/workspace/retrieve";
39
+ const CREATE_ENDPOINT = "/workspace/create";
40
+ const RENAME_ENDPOINT = "/workspace/rename";
41
+ const SET_LAYOUT_ENDPOINT = "/workspace/set-layout";
42
+ const DELETE_ENDPOINT = "/workspace/delete";
43
+
44
+ export const newWorkspaceZ = workspaceZ.partial({ key: true }).transform((p) => ({
45
+ ...p,
46
+ layout: JSON.stringify(p.layout),
47
+ }));
48
+
49
+ export const workspaceRemoteZ = workspaceZ.omit({ layout: true }).extend({
50
+ layout: z.string().transform((s) => parse(s) as UnknownRecord),
51
+ });
52
+
53
+ export type NewWorkspace = z.input<typeof newWorkspaceZ>;
54
+
55
+ const retrieveReqZ = z.object({
56
+ keys: z.string().array().optional(),
57
+ search: z.string().optional(),
58
+ author: z.string().uuid().optional(),
59
+ offset: z.number().optional(),
60
+ limit: z.number().optional(),
61
+ });
62
+ const createReqZ = z.object({ workspaces: newWorkspaceZ.array() });
63
+ const renameReqZ = z.object({ key: z.string(), name: z.string() });
64
+ const setLayoutReqZ = z.object({ key: z.string(), layout: z.string() });
65
+ const deleteReqZ = z.object({ keys: z.string().array() });
66
+
67
+ const retrieveResZ = z.object({ workspaces: nullableArrayZ(workspaceZ) });
68
+ const createResZ = z.object({ workspaces: workspaceRemoteZ.array() });
69
+ const emptyResZ = z.object({});
21
70
 
22
71
  export class Client implements AsyncTermSearcher<string, Key, Workspace> {
23
72
  readonly type = "workspace";
@@ -25,59 +74,111 @@ export class Client implements AsyncTermSearcher<string, Key, Workspace> {
25
74
  readonly linePlot: linePlot.Client;
26
75
  readonly log: log.Client;
27
76
  readonly table: table.Client;
28
- private readonly retriever: Retriever;
29
- private readonly writer: Writer;
77
+ private readonly client: UnaryClient;
30
78
 
31
79
  constructor(client: UnaryClient) {
80
+ this.client = client;
32
81
  this.schematic = new schematic.Client(client);
33
82
  this.linePlot = new linePlot.Client(client);
34
- this.retriever = new Retriever(client);
35
83
  this.log = new log.Client(client);
36
- this.writer = new Writer(client);
37
84
  this.table = new table.Client(client);
38
85
  }
39
86
 
40
- async search(term: string): Promise<Workspace[]> {
41
- return await this.retriever.search(term);
87
+ async create(workspace: NewWorkspace): Promise<Workspace>;
88
+ async create(workspaces: NewWorkspace[]): Promise<Workspace[]>;
89
+ async create(
90
+ workspaces: NewWorkspace | NewWorkspace[],
91
+ ): Promise<Workspace | Workspace[]> {
92
+ const isMany = Array.isArray(workspaces);
93
+ const normalized = toArray(workspaces);
94
+ const res = await sendRequired(
95
+ this.client,
96
+ CREATE_ENDPOINT,
97
+ { workspaces: normalized },
98
+ createReqZ,
99
+ createResZ,
100
+ );
101
+ return isMany ? res.workspaces : res.workspaces[0];
42
102
  }
43
103
 
44
- async retrieveByAuthor(author: string): Promise<Workspace[]> {
45
- return await this.retriever.retrieveByAuthor(author);
104
+ async rename(key: Key, name: string): Promise<void> {
105
+ await sendRequired(
106
+ this.client,
107
+ RENAME_ENDPOINT,
108
+ { key, name },
109
+ renameReqZ,
110
+ emptyResZ,
111
+ );
46
112
  }
47
113
 
48
- async retrieve(key: Key): Promise<Workspace>;
114
+ async setLayout(key: Key, layout: UnknownRecord): Promise<void> {
115
+ await sendRequired(
116
+ this.client,
117
+ SET_LAYOUT_ENDPOINT,
118
+ { key, layout: JSON.stringify(layout) },
119
+ setLayoutReqZ,
120
+ emptyResZ,
121
+ );
122
+ }
49
123
 
124
+ async retrieve(key: Key): Promise<Workspace>;
50
125
  async retrieve(keys: Key[]): Promise<Workspace[]>;
51
-
52
- async retrieve(keys: Key | Key[]): Promise<Workspace | Workspace[]> {
126
+ async retrieve(keys: Params): Promise<Workspace | Workspace[]> {
53
127
  const isMany = Array.isArray(keys);
54
- const res = await this.retriever.retrieve(keys);
55
- return isMany ? res : res[0];
128
+ const normalized = toArray(keys);
129
+ const res = await sendRequired(
130
+ this.client,
131
+ RETRIEVE_ENDPOINT,
132
+ { keys: normalized },
133
+ retrieveReqZ,
134
+ retrieveResZ,
135
+ );
136
+ return isMany ? res.workspaces : res.workspaces[0];
56
137
  }
57
138
 
58
- async page(offset: number, limit: number): Promise<Workspace[]> {
59
- return await this.retriever.page(offset, limit);
60
- }
61
-
62
- async create(workspace: NewWorkspace): Promise<Workspace>;
63
-
64
- async create(
65
- workspaces: NewWorkspace | NewWorkspace[],
66
- ): Promise<Workspace | Workspace[]> {
67
- const isMany = Array.isArray(workspaces);
68
- const res = await this.writer.create(workspaces);
69
- return isMany ? res : res[0];
139
+ async retrieveByAuthor(author: string): Promise<Workspace[]> {
140
+ const res = await sendRequired(
141
+ this.client,
142
+ RETRIEVE_ENDPOINT,
143
+ { author },
144
+ retrieveReqZ,
145
+ retrieveResZ,
146
+ );
147
+ return res.workspaces;
70
148
  }
71
149
 
72
- async rename(key: Key, name: string): Promise<void> {
73
- await this.writer.rename(key, name);
150
+ async search(term: string): Promise<Workspace[]> {
151
+ const res = await sendRequired(
152
+ this.client,
153
+ RETRIEVE_ENDPOINT,
154
+ { search: term },
155
+ retrieveReqZ,
156
+ retrieveResZ,
157
+ );
158
+ return res.workspaces;
74
159
  }
75
160
 
76
- async setLayout(key: Key, layout: UnknownRecord): Promise<void> {
77
- await this.writer.setLayout(key, layout);
161
+ async page(offset: number, limit: number): Promise<Workspace[]> {
162
+ const res = await sendRequired(
163
+ this.client,
164
+ RETRIEVE_ENDPOINT,
165
+ { offset, limit },
166
+ retrieveReqZ,
167
+ retrieveResZ,
168
+ );
169
+ return res.workspaces;
78
170
  }
79
171
 
80
- async delete(...keys: Key[]): Promise<void> {
81
- await this.writer.delete(keys);
172
+ async delete(key: Key): Promise<void>;
173
+ async delete(keys: Key[]): Promise<void>;
174
+ async delete(keys: Params): Promise<void> {
175
+ const normalized = toArray(keys);
176
+ await sendRequired(
177
+ this.client,
178
+ DELETE_ENDPOINT,
179
+ { keys: normalized },
180
+ deleteReqZ,
181
+ emptyResZ,
182
+ );
82
183
  }
83
184
  }
@@ -7,4 +7,4 @@
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 * as workspace from "@/workspace/external";
10
+ export * as workspace from "@/workspace/client";
@@ -1,51 +1,119 @@
1
- // Copyright 2024 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 { type UnknownRecord } from "@synnaxlabs/x/record";
12
-
13
- import { type Key, type LinePlot, type Params } from "@/workspace/lineplot/payload";
14
- import { Retriever } from "@/workspace/lineplot/retriever";
15
- import { type NewLinePlot, Writer } from "@/workspace/lineplot/writer";
1
+ import { ontology } from "@/ontology";
2
+ import { nullableArrayZ } from "@/util/zod";
3
+ import { sendRequired, UnaryClient } from "@synnaxlabs/freighter";
4
+ import { breaker, TimeSpan, toArray, UnknownRecord } from "@synnaxlabs/x";
5
+ import { unknownRecordZ } from "@synnaxlabs/x/record";
6
+ import { z } from "zod";
7
+
8
+ export const keyZ = z.string().uuid();
9
+ export type Key = z.infer<typeof keyZ>;
10
+ export type Params = Key | Key[];
11
+
12
+ export const linePlotZ = z.object({
13
+ key: z.string(),
14
+ name: z.string(),
15
+ data: unknownRecordZ.or(z.string().transform((s) => JSON.parse(s) as UnknownRecord)),
16
+ });
17
+
18
+ export type LinePlot = z.infer<typeof linePlotZ>;
19
+
20
+ export const ONTOLOGY_TYPE: ontology.ResourceType = "lineplot";
21
+
22
+ export const ontologyID = (key: Key): ontology.ID =>
23
+ new ontology.ID({ type: ONTOLOGY_TYPE, key });
24
+
25
+ const RETRIEVE_ENDPOINT = "/workspace/lineplot/retrieve";
26
+ const CREATE_ENDPOINT = "/workspace/lineplot/create";
27
+ const RENAME_ENDPOINT = "/workspace/lineplot/rename";
28
+ const SET_DATA_ENDPOINT = "/workspace/lineplot/set-data";
29
+ const DELETE_ENDPOINT = "/workspace/lineplot/delete";
30
+
31
+ export const newLinePlotZ = linePlotZ.partial({ key: true }).transform((p) => ({
32
+ ...p,
33
+ data: JSON.stringify(p.data),
34
+ }));
35
+
36
+ export type NewLinePlot = z.input<typeof newLinePlotZ>;
37
+
38
+ const retrieveReqZ = z.object({ keys: keyZ.array() });
39
+ const createReqZ = z.object({ workspace: z.string(), linePlots: newLinePlotZ.array() });
40
+ const renameReqZ = z.object({ key: keyZ, name: z.string() });
41
+ const setDataReqZ = z.object({ key: keyZ, data: z.string() });
42
+ const deleteReqZ = z.object({ keys: keyZ.array() });
43
+ const retrieveResZ = z.object({ linePlots: nullableArrayZ(linePlotZ) });
44
+ const createResZ = z.object({ linePlots: linePlotZ.array() });
45
+ const emptyResZ = z.object({});
16
46
 
17
47
  export class Client {
18
- private readonly writer: Writer;
19
- private readonly retriever: Retriever;
48
+ private readonly client: UnaryClient;
20
49
 
21
50
  constructor(client: UnaryClient) {
22
- this.writer = new Writer(client);
23
- this.retriever = new Retriever(client);
51
+ this.client = client;
24
52
  }
25
53
 
26
- async create(workspace: string, linePlot: NewLinePlot): Promise<LinePlot> {
27
- return await this.writer.create(workspace, linePlot);
54
+ async create(workspace: string, linePlot: NewLinePlot): Promise<LinePlot>;
55
+ async create(workspace: string, linePlots: NewLinePlot[]): Promise<LinePlot[]>;
56
+ async create(
57
+ workspace: string,
58
+ linePlots: NewLinePlot | NewLinePlot[],
59
+ ): Promise<LinePlot | LinePlot[]> {
60
+ const isMany = Array.isArray(linePlots);
61
+ const normalized = toArray(linePlots);
62
+ const res = await sendRequired(
63
+ this.client,
64
+ CREATE_ENDPOINT,
65
+ { workspace, linePlots: normalized },
66
+ createReqZ,
67
+ createResZ,
68
+ );
69
+ return isMany ? res.linePlots : res.linePlots[0];
28
70
  }
29
71
 
30
72
  async rename(key: Key, name: string): Promise<void> {
31
- await this.writer.rename(key, name);
73
+ await sendRequired(
74
+ this.client,
75
+ RENAME_ENDPOINT,
76
+ { key, name },
77
+ renameReqZ,
78
+ emptyResZ,
79
+ );
32
80
  }
33
81
 
34
82
  async setData(key: Key, data: UnknownRecord): Promise<void> {
35
- await this.writer.setData(key, data);
83
+ await sendRequired(
84
+ this.client,
85
+ SET_DATA_ENDPOINT,
86
+ { key, data: JSON.stringify(data) },
87
+ setDataReqZ,
88
+ emptyResZ,
89
+ );
36
90
  }
37
91
 
38
92
  async retrieve(key: Key): Promise<LinePlot>;
39
-
40
93
  async retrieve(keys: Key[]): Promise<LinePlot[]>;
41
-
42
94
  async retrieve(keys: Params): Promise<LinePlot | LinePlot[]> {
43
95
  const isMany = Array.isArray(keys);
44
- const res = await this.retriever.retrieve(keys);
45
- return isMany ? res : res[0];
96
+ const normalized = toArray(keys);
97
+ const res = await sendRequired(
98
+ this.client,
99
+ RETRIEVE_ENDPOINT,
100
+ { keys: normalized },
101
+ retrieveReqZ,
102
+ retrieveResZ,
103
+ );
104
+ return isMany ? res.linePlots : res.linePlots[0];
46
105
  }
47
106
 
107
+ async delete(key: Key): Promise<void>;
108
+ async delete(keys: Key[]): Promise<void>;
48
109
  async delete(keys: Params): Promise<void> {
49
- await this.writer.delete(keys);
110
+ const normalized = toArray(keys);
111
+ await sendRequired(
112
+ this.client,
113
+ DELETE_ENDPOINT,
114
+ { keys: normalized },
115
+ deleteReqZ,
116
+ emptyResZ,
117
+ );
50
118
  }
51
119
  }
@@ -7,4 +7,4 @@
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 * as linePlot from "@/workspace/lineplot/external";
10
+ export * as linePlot from "@/workspace/lineplot/client";
@@ -1,51 +1,116 @@
1
- // Copyright 2024 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 { type UnknownRecord } from "@synnaxlabs/x/record";
12
-
13
- import { type Key, type Log, type Params } from "@/workspace/log/payload";
14
- import { Retriever } from "@/workspace/log/retriever";
15
- import { type NewLog, Writer } from "@/workspace/log/writer";
1
+ import { ontology } from "@/ontology";
2
+ import { sendRequired, UnaryClient } from "@synnaxlabs/freighter";
3
+ import { toArray, UnknownRecord } from "@synnaxlabs/x";
4
+ import { unknownRecordZ } from "@synnaxlabs/x/record";
5
+ import { z } from "zod";
6
+
7
+ export const keyZ = z.string().uuid();
8
+ export type Key = z.infer<typeof keyZ>;
9
+ export type Params = Key | Key[];
10
+
11
+ export const logZ = z.object({
12
+ key: z.string(),
13
+ name: z.string(),
14
+ data: unknownRecordZ.or(z.string().transform((s) => JSON.parse(s) as UnknownRecord)),
15
+ });
16
+
17
+ export type Log = z.infer<typeof logZ>;
18
+
19
+ export const ONTOLOGY_TYPE: ontology.ResourceType = "log";
20
+
21
+ export const ontologyID = (key: Key): ontology.ID =>
22
+ new ontology.ID({ type: ONTOLOGY_TYPE, key });
23
+
24
+ const RETRIEVE_ENDPOINT = "/workspace/log/retrieve";
25
+ const CREATE_ENDPOINT = "/workspace/log/create";
26
+ const RENAME_ENDPOINT = "/workspace/log/rename";
27
+ const SET_DATA_ENDPOINT = "/workspace/log/set-data";
28
+ const DELETE_ENDPOINT = "/workspace/log/delete";
29
+
30
+ export const newLogZ = logZ.partial({ key: true }).transform((p) => ({
31
+ ...p,
32
+ data: JSON.stringify(p.data),
33
+ }));
34
+
35
+ export type NewLog = z.input<typeof newLogZ>;
36
+
37
+ const retrieveReqZ = z.object({ keys: z.string().array() });
38
+ const createReqZ = z.object({ workspace: z.string(), logs: newLogZ.array() });
39
+ const renameReqZ = z.object({ key: z.string(), name: z.string() });
40
+ const setDataReqZ = z.object({ key: z.string(), data: z.string() });
41
+ const deleteReqZ = z.object({ keys: z.string().array() });
42
+
43
+ const retrieveResZ = z.object({ logs: logZ.array() });
44
+ const createResZ = z.object({ logs: logZ.array() });
45
+ const emptyResZ = z.object({});
16
46
 
17
47
  export class Client {
18
- private readonly writer: Writer;
19
- private readonly retriever: Retriever;
48
+ private readonly client: UnaryClient;
20
49
 
21
50
  constructor(client: UnaryClient) {
22
- this.writer = new Writer(client);
23
- this.retriever = new Retriever(client);
51
+ this.client = client;
24
52
  }
25
53
 
26
- async create(workspace: string, log: NewLog): Promise<Log> {
27
- return await this.writer.create(workspace, log);
54
+ async create(workspace: string, log: NewLog): Promise<Log>;
55
+ async create(workspace: string, logs: NewLog[]): Promise<Log[]>;
56
+ async create(workspace: string, logs: NewLog | NewLog[]): Promise<Log | Log[]> {
57
+ const isMany = Array.isArray(logs);
58
+ const normalized = toArray(logs);
59
+ const res = await sendRequired(
60
+ this.client,
61
+ CREATE_ENDPOINT,
62
+ { workspace, logs: normalized },
63
+ createReqZ,
64
+ createResZ,
65
+ );
66
+ return isMany ? res.logs : res.logs[0];
28
67
  }
29
68
 
30
69
  async rename(key: Key, name: string): Promise<void> {
31
- await this.writer.rename(key, name);
70
+ await sendRequired(
71
+ this.client,
72
+ RENAME_ENDPOINT,
73
+ { key, name },
74
+ renameReqZ,
75
+ emptyResZ,
76
+ );
32
77
  }
33
78
 
34
79
  async setData(key: Key, data: UnknownRecord): Promise<void> {
35
- await this.writer.setData(key, data);
80
+ await sendRequired(
81
+ this.client,
82
+ SET_DATA_ENDPOINT,
83
+ { key, data: JSON.stringify(data) },
84
+ setDataReqZ,
85
+ emptyResZ,
86
+ );
36
87
  }
37
88
 
38
89
  async retrieve(key: Key): Promise<Log>;
39
-
40
90
  async retrieve(keys: Key[]): Promise<Log[]>;
41
-
42
91
  async retrieve(keys: Params): Promise<Log | Log[]> {
43
92
  const isMany = Array.isArray(keys);
44
- const res = await this.retriever.retrieve(keys);
45
- return isMany ? res : res[0];
93
+ const normalized = toArray(keys);
94
+ const res = await sendRequired(
95
+ this.client,
96
+ RETRIEVE_ENDPOINT,
97
+ { keys: normalized },
98
+ retrieveReqZ,
99
+ retrieveResZ,
100
+ );
101
+ return isMany ? res.logs : res.logs[0];
46
102
  }
47
103
 
104
+ async delete(key: Key): Promise<void>;
105
+ async delete(keys: Key[]): Promise<void>;
48
106
  async delete(keys: Params): Promise<void> {
49
- await this.writer.delete(keys);
107
+ const normalized = toArray(keys);
108
+ await sendRequired(
109
+ this.client,
110
+ DELETE_ENDPOINT,
111
+ { keys: normalized },
112
+ deleteReqZ,
113
+ emptyResZ,
114
+ );
50
115
  }
51
116
  }
@@ -7,4 +7,4 @@
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 * as log from "@/workspace/log/external";
10
+ export * as log from "@/workspace/log/client";