@vertesia/client 0.57.0 → 0.59.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.
@@ -1,31 +1,33 @@
1
1
  import { ApiTopic, ClientBase } from "@vertesia/api-fetch-client";
2
2
  import {
3
- ComplexSearchPayload,
4
- ComputeObjectFacetPayload,
5
- ContentObject,
6
- ContentObjectItem,
7
- ContentSource,
8
- CreateContentObjectPayload,
9
- Embedding,
10
- ExportPropertiesPayload,
11
- ExportPropertiesResponse,
12
- FindPayload,
13
- GetFileUrlPayload,
14
- GetFileUrlResponse,
15
- GetRenditionResponse,
16
- GetUploadUrlPayload,
17
- ListWorkflowRunsResponse,
18
- ObjectSearchPayload,
19
- ObjectSearchQuery,
20
- SupportedEmbeddingTypes,
3
+ ComplexSearchPayload,
4
+ ComputeObjectFacetPayload,
5
+ ContentObject,
6
+ ContentObjectItem,
7
+ ContentSource,
8
+ CreateContentObjectPayload,
9
+ Embedding,
10
+ ExportPropertiesPayload,
11
+ ExportPropertiesResponse,
12
+ FindPayload,
13
+ GetFileUrlPayload,
14
+ GetFileUrlResponse,
15
+ GetRenditionParams,
16
+ GetRenditionResponse,
17
+ GetUploadUrlPayload,
18
+ ListWorkflowRunsResponse,
19
+ ObjectSearchPayload,
20
+ ObjectSearchQuery,
21
+ SupportedEmbeddingTypes,
21
22
  } from "@vertesia/common";
22
23
 
23
24
  import { StreamSource } from "../StreamSource.js";
24
25
  import { AnalyzeDocApi } from "./AnalyzeDocApi.js";
25
26
  import { ZenoClient } from "./client.js";
26
27
 
27
- export interface UploadContentObjectPayload extends Omit<CreateContentObjectPayload, "content"> {
28
- content?:
28
+ export interface UploadContentObjectPayload
29
+ extends Omit<CreateContentObjectPayload, "content"> {
30
+ content?:
29
31
  | StreamSource
30
32
  | File
31
33
  | {
@@ -38,123 +40,127 @@ export interface UploadContentObjectPayload extends Omit<CreateContentObjectPayl
38
40
 
39
41
  // the target id in the content store
40
42
  id?: string;
41
- };
43
+ };
42
44
  }
43
45
 
44
46
  export interface ComputeFacetsResponse {
45
- type?: { _id: string; count: number }[];
46
- location?: { _id: string; count: number }[];
47
- status?: { _id: string; count: number }[];
48
- total?: { count: number }[];
47
+ type?: { _id: string; count: number }[];
48
+ location?: { _id: string; count: number }[];
49
+ status?: { _id: string; count: number }[];
50
+ total?: { count: number }[];
49
51
  }
50
52
 
51
53
  export class ObjectsApi extends ApiTopic {
52
- constructor(parent: ClientBase) {
53
- super(parent, "/api/v1/objects");
54
- }
55
-
56
- analyze(objectId: string) {
57
- return new AnalyzeDocApi(this, objectId);
58
- }
59
-
60
- getUploadUrl(payload: GetUploadUrlPayload): Promise<GetFileUrlResponse> {
61
- return this.post("/upload-url", {
62
- payload,
63
- });
64
- }
65
-
66
- getDownloadUrl(fileUri: string): Promise<{ url: string }> {
67
- return this.post("/download-url", {
68
- payload: {
69
- file: fileUri,
70
- } satisfies GetFileUrlPayload,
71
- });
72
- }
73
-
74
- getContentSource(objectId: string): Promise<ContentSource> {
75
- return this.get(`/${objectId}/content-source`);
76
- }
77
-
78
- /**
79
- * List objects with revision filtering options
80
- *
81
- * @param payload Search/filter parameters
82
- * @returns Matching content objects
83
- */
84
- list<T = any>(payload: ObjectSearchPayload = {}): Promise<ContentObjectItem<T>[]> {
85
- const limit = payload.limit || 100;
86
- const offset = payload.offset || 0;
87
- const query = payload.query || ({} as ObjectSearchQuery);
88
-
89
- // Add revision filtering options
90
- const showAllRevisions = payload.all_revisions === true;
91
- const revisionRoot = payload.from_root;
92
-
93
- return this.get("/", {
94
- query: {
95
- limit,
96
- offset,
97
- ...query,
98
- all_revisions: showAllRevisions ? "true" : undefined,
99
- from_root: revisionRoot,
100
- },
101
- });
102
- }
103
-
104
- computeFacets(query: ComputeObjectFacetPayload): Promise<ComputeFacetsResponse> {
105
- return this.post("/facets", {
106
- payload: query,
107
- });
108
- }
109
-
110
- listFolders(path: string = "/") {
111
- path; //TODO
112
- }
113
-
114
- /** Find object based on query */
115
- find(payload: FindPayload): Promise<ContentObject[]> {
116
- return this.post("/find", {
117
- payload,
118
- });
119
- }
120
-
121
- /** Count number of objects matching this query */
122
- count(payload: FindPayload): Promise<{ count: number }> {
123
- return this.post("/count", {
124
- payload,
125
- });
126
- }
127
-
128
- /** Search object — different from find because allow full text search */
129
- search(payload: ComplexSearchPayload): Promise<ContentObjectItem[]> {
130
- return this.post("/search", {
131
- payload,
132
- });
133
- }
134
-
135
- retrieve(id: string, select?: string): Promise<ContentObject> {
136
- return this.get(`/${id}`, {
137
- query: {
138
- select,
139
- },
140
- });
141
- }
142
-
143
- getObjectText(id: string): Promise<{ text: string }> {
144
- return this.get(`/${id}/text`);
145
- }
146
-
147
- async upload(source: StreamSource | File) {
148
- const isStream = source instanceof StreamSource;
149
- // get a signed URL to upload the file a computed mimeType and the file object id.
150
- const { url, id, mime_type } = await this.getUploadUrl({
151
- id: isStream ? source.id : undefined,
152
- name: source.name,
153
- mime_type: source.type,
154
- });
155
-
156
- // upload the file content to the signed URL
157
- /*const res = await this.fetch(url, {
54
+ constructor(parent: ClientBase) {
55
+ super(parent, "/api/v1/objects");
56
+ }
57
+
58
+ analyze(objectId: string) {
59
+ return new AnalyzeDocApi(this, objectId);
60
+ }
61
+
62
+ getUploadUrl(payload: GetUploadUrlPayload): Promise<GetFileUrlResponse> {
63
+ return this.post("/upload-url", {
64
+ payload,
65
+ });
66
+ }
67
+
68
+ getDownloadUrl(fileUri: string): Promise<{ url: string }> {
69
+ return this.post("/download-url", {
70
+ payload: {
71
+ file: fileUri,
72
+ } satisfies GetFileUrlPayload,
73
+ });
74
+ }
75
+
76
+ getContentSource(objectId: string): Promise<ContentSource> {
77
+ return this.get(`/${objectId}/content-source`);
78
+ }
79
+
80
+ /**
81
+ * List objects with revision filtering options
82
+ *
83
+ * @param payload Search/filter parameters
84
+ * @returns Matching content objects
85
+ */
86
+ list<T = any>(
87
+ payload: ObjectSearchPayload = {},
88
+ ): Promise<ContentObjectItem<T>[]> {
89
+ const limit = payload.limit || 100;
90
+ const offset = payload.offset || 0;
91
+ const query = payload.query || ({} as ObjectSearchQuery);
92
+
93
+ // Add revision filtering options
94
+ const showAllRevisions = payload.all_revisions === true;
95
+ const revisionRoot = payload.from_root;
96
+
97
+ return this.get("/", {
98
+ query: {
99
+ limit,
100
+ offset,
101
+ ...query,
102
+ all_revisions: showAllRevisions ? "true" : undefined,
103
+ from_root: revisionRoot,
104
+ },
105
+ });
106
+ }
107
+
108
+ computeFacets(
109
+ query: ComputeObjectFacetPayload,
110
+ ): Promise<ComputeFacetsResponse> {
111
+ return this.post("/facets", {
112
+ payload: query,
113
+ });
114
+ }
115
+
116
+ listFolders(path: string = "/") {
117
+ path; //TODO
118
+ }
119
+
120
+ /** Find object based on query */
121
+ find(payload: FindPayload): Promise<ContentObject[]> {
122
+ return this.post("/find", {
123
+ payload,
124
+ });
125
+ }
126
+
127
+ /** Count number of objects matching this query */
128
+ count(payload: FindPayload): Promise<{ count: number }> {
129
+ return this.post("/count", {
130
+ payload,
131
+ });
132
+ }
133
+
134
+ /** Search object — different from find because allow full text search */
135
+ search(payload: ComplexSearchPayload): Promise<ContentObjectItem[]> {
136
+ return this.post("/search", {
137
+ payload,
138
+ });
139
+ }
140
+
141
+ retrieve(id: string, select?: string): Promise<ContentObject> {
142
+ return this.get(`/${id}`, {
143
+ query: {
144
+ select,
145
+ },
146
+ });
147
+ }
148
+
149
+ getObjectText(id: string): Promise<{ text: string }> {
150
+ return this.get(`/${id}/text`);
151
+ }
152
+
153
+ async upload(source: StreamSource | File) {
154
+ const isStream = source instanceof StreamSource;
155
+ // get a signed URL to upload the file a computed mimeType and the file object id.
156
+ const { url, id, mime_type } = await this.getUploadUrl({
157
+ id: isStream ? source.id : undefined,
158
+ name: source.name,
159
+ mime_type: source.type,
160
+ });
161
+
162
+ // upload the file content to the signed URL
163
+ /*const res = await this.fetch(url, {
158
164
  method: 'PUT',
159
165
  //@ts-ignore: duplex is not in the types. See https://github.com/node-fetch/node-fetch/issues/1769
160
166
  duplex: isStream ? "half" : undefined,
@@ -171,177 +177,186 @@ export class ObjectsApi extends ApiTopic {
171
177
  }
172
178
  });*/
173
179
 
174
- const res = await fetch(url, {
175
- method: "PUT",
176
- body: isStream ? source.stream : source,
177
- //@ts-ignore: duplex is not in the types. See https://github.com/node-fetch/node-fetch/issues/1769
178
- duplex: isStream ? "half" : undefined,
179
- headers: {
180
- "Content-Type": mime_type || "application/octet-stream",
181
- },
182
- })
183
- .then((res: Response) => {
184
- if (res.ok) {
185
- return res;
186
- } else {
187
- console.log(res);
188
- throw new Error(`Failed to upload file: ${res.statusText}`);
189
- }
190
- })
191
- .catch((err) => {
192
- console.error("Failed to upload file", err);
193
- throw err;
194
- });
195
-
196
- //Etag need to be unquoted
197
- //When a server returns an ETag header, it includes the quotes around the actual hash value.
198
- //This is part of the HTTP specification (RFC 7232), which states that ETags should be
199
- //enclosed in double quotes.
200
- const etag = res.headers.get("etag")?.replace(/^"(.*)"$/, "$1");
201
-
202
- return {
203
- source: id,
204
- name: source.name,
205
- type: mime_type,
206
- etag,
207
- };
208
- }
209
-
210
- async create(
211
- payload: UploadContentObjectPayload,
212
- options?: {
213
- collection_id?: string;
214
- },
215
- ): Promise<ContentObject> {
216
- const createPayload: CreateContentObjectPayload = {
217
- ...payload,
218
- };
219
- if (payload.content instanceof StreamSource || payload.content instanceof File) {
220
- createPayload.content = await this.upload(payload.content);
221
- }
222
- return await this.post("/", {
223
- payload: createPayload,
224
- query: options || {},
225
- });
226
- }
227
-
228
- /**
229
- * Create an object which holds a reference to an external blob (i.e. not in the project bucket)
230
- * The uri should starts either with gs:// or s3://. Not other protocols are supported yet.
231
- * For the s3 blobs you must use a hash with the blob #region. Ex: s3://bucket/path/to/file#us-east-1
232
- * @param uri
233
- * @param payload
234
- * @returns
235
- */
236
- async createFromExternalSource(uri: string, payload: CreateContentObjectPayload = {}): Promise<ContentObject> {
237
- const metadata = await (this.client as ZenoClient).files.getMetadata(uri);
238
- const createPayload: CreateContentObjectPayload = {
239
- ...payload,
240
- content: {
241
- source: uri,
242
- name: metadata.name,
243
- type: metadata.contentType,
244
- etag: metadata.etag,
245
- },
246
- };
247
- return await this.post("/", {
248
- payload: createPayload,
249
- });
250
- }
251
-
252
- /**
253
- * Updates an existing object or creates a new revision
254
- * Handles file uploads similar to the create method
255
- *
256
- * @param id The ID of the object to update
257
- * @param payload The changes to apply
258
- * @param options Additional options
259
- * @param options.createRevision Whether to create a new revision instead of updating in place
260
- * @param options.revisionLabel Optional label for the revision (e.g., "v1.2")
261
- * @returns The updated object or newly created revision
262
- */
263
- async update(
264
- id: string,
265
- payload: Partial<CreateContentObjectPayload>,
266
- options?: {
267
- createRevision?: boolean;
268
- revisionLabel?: string;
269
- },
270
- ): Promise<ContentObject> {
271
- const updatePayload: Partial<CreateContentObjectPayload> = {
272
- ...payload,
273
- };
274
-
275
- // Handle file upload if content is provided as File or StreamSource
276
- if (payload.content instanceof StreamSource || payload.content instanceof File) {
277
- updatePayload.content = await this.upload(payload.content);
278
- }
279
-
280
- if (options?.createRevision) {
281
- return this.put(`/${id}`, {
282
- payload: updatePayload,
283
- headers: {
284
- "x-create-revision": "true",
285
- "x-revision-label": options.revisionLabel || "",
286
- },
287
- });
180
+ const res = await fetch(url, {
181
+ method: "PUT",
182
+ body: isStream ? source.stream : source,
183
+ //@ts-ignore: duplex is not in the types. See https://github.com/node-fetch/node-fetch/issues/1769
184
+ duplex: isStream ? "half" : undefined,
185
+ headers: {
186
+ "Content-Type": mime_type || "application/octet-stream",
187
+ },
188
+ })
189
+ .then((res: Response) => {
190
+ if (res.ok) {
191
+ return res;
288
192
  } else {
289
- return this.put(`/${id}`, {
290
- payload: updatePayload,
291
- });
193
+ console.log(res);
194
+ throw new Error(`Failed to upload file: ${res.statusText}`);
292
195
  }
196
+ })
197
+ .catch((err) => {
198
+ console.error("Failed to upload file", err);
199
+ throw err;
200
+ });
201
+
202
+ //Etag need to be unquoted
203
+ //When a server returns an ETag header, it includes the quotes around the actual hash value.
204
+ //This is part of the HTTP specification (RFC 7232), which states that ETags should be
205
+ //enclosed in double quotes.
206
+ const etag = res.headers.get("etag")?.replace(/^"(.*)"$/, "$1");
207
+
208
+ return {
209
+ source: id,
210
+ name: source.name,
211
+ type: mime_type,
212
+ etag,
213
+ };
214
+ }
215
+
216
+ async create(
217
+ payload: UploadContentObjectPayload,
218
+ options?: {
219
+ collection_id?: string;
220
+ },
221
+ ): Promise<ContentObject> {
222
+ const createPayload: CreateContentObjectPayload = {
223
+ ...payload,
224
+ };
225
+ if (
226
+ payload.content instanceof StreamSource ||
227
+ payload.content instanceof File
228
+ ) {
229
+ createPayload.content = await this.upload(payload.content);
293
230
  }
231
+ return await this.post("/", {
232
+ payload: createPayload,
233
+ query: options || {},
234
+ });
235
+ }
236
+
237
+ /**
238
+ * Create an object which holds a reference to an external blob (i.e. not in the project bucket)
239
+ * The uri should starts either with gs:// or s3://. Not other protocols are supported yet.
240
+ * For the s3 blobs you must use a hash with the blob #region. Ex: s3://bucket/path/to/file#us-east-1
241
+ * @param uri
242
+ * @param payload
243
+ * @returns
244
+ */
245
+ async createFromExternalSource(
246
+ uri: string,
247
+ payload: CreateContentObjectPayload = {},
248
+ ): Promise<ContentObject> {
249
+ const metadata = await (this.client as ZenoClient).files.getMetadata(uri);
250
+ const createPayload: CreateContentObjectPayload = {
251
+ ...payload,
252
+ content: {
253
+ source: uri,
254
+ name: metadata.name,
255
+ type: metadata.contentType,
256
+ etag: metadata.etag,
257
+ },
258
+ };
259
+ return await this.post("/", {
260
+ payload: createPayload,
261
+ });
262
+ }
263
+
264
+ /**
265
+ * Updates an existing object or creates a new revision
266
+ * Handles file uploads similar to the create method
267
+ *
268
+ * @param id The ID of the object to update
269
+ * @param payload The changes to apply
270
+ * @param options Additional options
271
+ * @param options.createRevision Whether to create a new revision instead of updating in place
272
+ * @param options.revisionLabel Optional label for the revision (e.g., "v1.2")
273
+ * @returns The updated object or newly created revision
274
+ */
275
+ async update(
276
+ id: string,
277
+ payload: Partial<CreateContentObjectPayload>,
278
+ options?: {
279
+ createRevision?: boolean;
280
+ revisionLabel?: string;
281
+ },
282
+ ): Promise<ContentObject> {
283
+ const updatePayload: Partial<CreateContentObjectPayload> = {
284
+ ...payload,
285
+ };
294
286
 
295
- /**
296
- * Retrieves all revisions of a content object
297
- *
298
- * @param id The ID of any revision in the object's history
299
- * @returns Array of all revisions sharing the same root
300
- */
301
- getRevisions(id: string): Promise<ContentObjectItem[]> {
302
- return this.get(`/${id}/revisions`);
303
- }
304
-
305
- delete(id: string): Promise<{ id: string }> {
306
- return this.del(`/${id}`);
307
- }
308
-
309
- listWorkflowRuns(documentId: string): Promise<ListWorkflowRunsResponse> {
310
- return this.get(`/${documentId}/workflow-runs`);
311
- }
312
-
313
- listRenditions(documentId: string): Promise<ContentObjectItem[]> {
314
- return this.get(`/${documentId}/renditions`);
315
- }
316
-
317
- getRendition(documentId: string, options: GetRenditionParams): Promise<GetRenditionResponse> {
318
- const query = {
319
- max_hw: options.max_hw,
320
- generate_if_missing: options.generate_if_missing,
321
- };
322
-
323
- return this.get(`/${documentId}/renditions/${options.format}`, { query });
324
- }
325
-
326
- exportProperties(payload: ExportPropertiesPayload): Promise<ExportPropertiesResponse> {
327
- return this.post("/export", {
328
- payload,
329
- });
287
+ // Handle file upload if content is provided as File or StreamSource
288
+ if (
289
+ payload.content instanceof StreamSource ||
290
+ payload.content instanceof File
291
+ ) {
292
+ updatePayload.content = await this.upload(payload.content);
330
293
  }
331
294
 
332
- setEmbedding(
333
- id: string,
334
- type: SupportedEmbeddingTypes,
335
- payload: Embedding,
336
- ): Promise<Record<SupportedEmbeddingTypes, Embedding>> {
337
- return this.put(`/${id}/embeddings/${type}`, {
338
- payload,
339
- });
295
+ if (options?.createRevision) {
296
+ return this.put(`/${id}`, {
297
+ payload: updatePayload,
298
+ headers: {
299
+ "x-create-revision": "true",
300
+ "x-revision-label": options.revisionLabel || "",
301
+ },
302
+ });
303
+ } else {
304
+ return this.put(`/${id}`, {
305
+ payload: updatePayload,
306
+ });
340
307
  }
341
- }
308
+ }
309
+
310
+ /**
311
+ * Retrieves all revisions of a content object
312
+ *
313
+ * @param id The ID of any revision in the object's history
314
+ * @returns Array of all revisions sharing the same root
315
+ */
316
+ getRevisions(id: string): Promise<ContentObjectItem[]> {
317
+ return this.get(`/${id}/revisions`);
318
+ }
319
+
320
+ delete(id: string): Promise<{ id: string }> {
321
+ return this.del(`/${id}`);
322
+ }
323
+
324
+ listWorkflowRuns(documentId: string): Promise<ListWorkflowRunsResponse> {
325
+ return this.get(`/${documentId}/workflow-runs`);
326
+ }
327
+
328
+ listRenditions(documentId: string): Promise<ContentObjectItem[]> {
329
+ return this.get(`/${documentId}/renditions`);
330
+ }
331
+
332
+ getRendition(
333
+ documentId: string,
334
+ options: GetRenditionParams,
335
+ ): Promise<GetRenditionResponse> {
336
+ const query = {
337
+ max_hw: options.max_hw,
338
+ generate_if_missing: options.generate_if_missing,
339
+ as_signed_url: options.sign_url,
340
+ };
342
341
 
343
- interface GetRenditionParams {
344
- format: string;
345
- max_hw?: number;
346
- generate_if_missing?: boolean;
342
+ return this.get(`/${documentId}/renditions/${options.format}`, { query });
343
+ }
344
+
345
+ exportProperties(
346
+ payload: ExportPropertiesPayload,
347
+ ): Promise<ExportPropertiesResponse> {
348
+ return this.post("/export", {
349
+ payload,
350
+ });
351
+ }
352
+
353
+ setEmbedding(
354
+ id: string,
355
+ type: SupportedEmbeddingTypes,
356
+ payload: Embedding,
357
+ ): Promise<Record<SupportedEmbeddingTypes, Embedding>> {
358
+ return this.put(`/${id}/embeddings/${type}`, {
359
+ payload,
360
+ });
361
+ }
347
362
  }