@coreviz/sdk 1.0.19 → 1.1.1

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.
@@ -4,9 +4,125 @@ exports.CoreViz = void 0;
4
4
  const resize_native_1 = require("./resize.native");
5
5
  class CoreViz {
6
6
  constructor(config = {}) {
7
+ this._orgIdCache = null;
7
8
  // React Native / Expo doesn't provide `process.env` in the same way; keep config explicit.
8
9
  this.apiKey = config.apiKey;
9
10
  this.token = config.token;
11
+ this._baseUrl = config.baseUrl || 'https://lab.coreviz.io';
12
+ this.collections = {
13
+ list: async () => {
14
+ const { organizationId } = await this._me();
15
+ const data = await this._fetch(`/api/organization/${organizationId}/datasets`);
16
+ return Array.isArray(data) ? data : data.datasets ?? [];
17
+ },
18
+ create: async (name, icon) => {
19
+ const { organizationId } = await this._me();
20
+ const data = await this._fetchMethod('POST', `/api/organization/${organizationId}/datasets`, { name, icon });
21
+ return data.dataset;
22
+ },
23
+ };
24
+ this.media = {
25
+ browse: async (collectionId, options = {}) => {
26
+ const params = new URLSearchParams();
27
+ if (options.path)
28
+ params.set('path', options.path);
29
+ if (options.limit != null)
30
+ params.set('limit', String(options.limit));
31
+ if (options.offset != null)
32
+ params.set('offset', String(options.offset));
33
+ if (options.type)
34
+ params.set('type', options.type);
35
+ if (options.dateFrom)
36
+ params.set('dateFrom', options.dateFrom);
37
+ if (options.dateTo)
38
+ params.set('dateTo', options.dateTo);
39
+ if (options.sortBy)
40
+ params.set('sortBy', options.sortBy);
41
+ if (options.sortDirection)
42
+ params.set('sortDirection', options.sortDirection);
43
+ if (options.tagFilters)
44
+ params.set('tagFilters', JSON.stringify(options.tagFilters));
45
+ const qs = params.toString();
46
+ return this._fetch(`/api/dataset/${collectionId}/media${qs ? `?${qs}` : ''}`);
47
+ },
48
+ search: async (query, options = {}) => {
49
+ const { organizationId } = await this._me();
50
+ const params = new URLSearchParams({ q: query, organizationId });
51
+ if (options.limit != null)
52
+ params.set('limit', String(options.limit));
53
+ const data = await this._fetch(`/api/search?${params.toString()}`);
54
+ return (data.results || []).map((r) => ({
55
+ mediaId: r.media?.id, mediaName: r.media?.name, mediaType: r.media?.type,
56
+ blobUrl: r.blob, objects: (r.objects || []).map((o) => ({ id: o.id, type: o.type, label: o.label })),
57
+ rank: r.rank, caption: r.captions?.[0]?.text,
58
+ }));
59
+ },
60
+ get: async (mediaId) => {
61
+ const data = await this._fetch(`/api/media/${mediaId}`);
62
+ return data.media;
63
+ },
64
+ rename: async (mediaId, name) => {
65
+ const data = await this._fetchMethod('PATCH', `/api/media/${mediaId}`, { name });
66
+ return data.media;
67
+ },
68
+ move: async (mediaId, destinationPath) => {
69
+ return this._fetchMethod('PATCH', `/api/media/${mediaId}/move`, { destinationPath });
70
+ },
71
+ addTag: async (mediaId, label, value) => {
72
+ await this._fetchMethod('POST', `/api/media/${mediaId}/tags`, { label, value });
73
+ },
74
+ removeTag: async (mediaId, label, value) => {
75
+ await this._fetchMethod('DELETE', `/api/media/${mediaId}/tags`, { label, value });
76
+ },
77
+ findSimilar: async (collectionId, objectId, options = {}) => {
78
+ const params = new URLSearchParams({ similarToObjectId: objectId });
79
+ if (options.limit != null)
80
+ params.set('limit', String(options.limit));
81
+ if (options.model)
82
+ params.set('similarToObjectModel', options.model);
83
+ return this._fetch(`/api/dataset/${collectionId}/media?${params.toString()}`);
84
+ },
85
+ upload: async (file, options) => {
86
+ if (typeof file === 'string') {
87
+ throw new Error('File path strings are not supported on React Native. Pass a File or Blob object instead.');
88
+ }
89
+ const formData = new FormData();
90
+ formData.append('datasetId', options.collectionId);
91
+ if (options.path)
92
+ formData.append('path', options.path);
93
+ const fileName = options.name || (file instanceof File ? file.name : 'upload');
94
+ formData.append('file', file, fileName);
95
+ if (options.name)
96
+ formData.append('name', options.name);
97
+ const authHeaders = {};
98
+ if (this.token) {
99
+ authHeaders['Authorization'] = `Bearer ${this.token}`;
100
+ }
101
+ else {
102
+ authHeaders['x-api-key'] = this.apiKey || '';
103
+ }
104
+ const response = await fetch(`${this._baseUrl}/api/upload/multipart`, {
105
+ method: 'POST',
106
+ headers: authHeaders,
107
+ body: formData,
108
+ });
109
+ return this.handleResponse(response);
110
+ },
111
+ };
112
+ this.folders = {
113
+ create: async (collectionId, name, path) => {
114
+ const data = await this._fetchMethod('POST', '/api/folder', {
115
+ datasetId: collectionId, name, ...(path ? { path } : {}),
116
+ });
117
+ return data.folder;
118
+ },
119
+ };
120
+ this.tags = {
121
+ list: async (collectionId) => {
122
+ const data = await this._fetch(`/api/dataset/${collectionId}/tags`);
123
+ return data.tags;
124
+ },
125
+ };
10
126
  }
11
127
  getHeaders() {
12
128
  const headers = {
@@ -20,6 +136,29 @@ class CoreViz {
20
136
  }
21
137
  return headers;
22
138
  }
139
+ async _me() {
140
+ const data = await this._fetch('/api/me');
141
+ if (!this._orgIdCache)
142
+ this._orgIdCache = data.organizationId;
143
+ return data;
144
+ }
145
+ async _fetch(path) {
146
+ const response = await fetch(`${this._baseUrl}${path}`, {
147
+ method: 'GET',
148
+ headers: this.getHeaders(),
149
+ });
150
+ return this.handleResponse(response);
151
+ }
152
+ async _fetchMethod(method, path, body) {
153
+ const response = await fetch(`${this._baseUrl}${path}`, {
154
+ method,
155
+ headers: this.getHeaders(),
156
+ body: body !== undefined ? JSON.stringify(body) : undefined,
157
+ });
158
+ if (response.status === 204)
159
+ return undefined;
160
+ return this.handleResponse(response);
161
+ }
23
162
  async handleResponse(response) {
24
163
  if (response.status === 402) {
25
164
  throw new Error('Insufficient credits');
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CoreViz, CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse } from './coreviz';
1
+ import { CoreViz, CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse, GenerateOptions, UserContext, Collection, Media, MediaObject, MediaFrame, Folder, BrowseOptions, BrowseResult, SearchResult, SearchOptions, SimilarityOptions, UploadOptions, UploadResult, CollectionsNamespace, MediaNamespace, FoldersNamespace, TagsNamespace, FolderUpdateOptions, CollectionUpdateOptions, DeleteVersionResult } from './coreviz';
2
2
  import { resize } from './resize';
3
3
  export { CoreViz, resize };
4
- export type { CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse };
4
+ export type { CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse, GenerateOptions, UserContext, Collection, Media, MediaObject, MediaFrame, Folder, BrowseOptions, BrowseResult, SearchResult, SearchOptions, SimilarityOptions, UploadOptions, UploadResult, CollectionsNamespace, MediaNamespace, FoldersNamespace, TagsNamespace, FolderUpdateOptions, CollectionUpdateOptions, DeleteVersionResult };
@@ -1,4 +1,4 @@
1
- import { CoreViz, CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse } from './coreviz.native';
1
+ import { CoreViz, CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse, GenerateOptions, UserContext, Collection, Media, MediaObject, MediaFrame, Folder, BrowseOptions, BrowseResult, SearchResult, SearchOptions, SimilarityOptions, UploadOptions, UploadResult, CollectionsNamespace, MediaNamespace, FoldersNamespace, TagsNamespace } from './coreviz.native';
2
2
  import { resize } from './resize.native';
3
3
  export { CoreViz, resize };
4
- export type { CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse };
4
+ export type { CoreVizConfig, DescribeOptions, EditOptions, TagOptions, TagResponse, EmbedOptions, EmbedResponse, GenerateOptions, UserContext, Collection, Media, MediaObject, MediaFrame, Folder, BrowseOptions, BrowseResult, SearchResult, SearchOptions, SimilarityOptions, UploadOptions, UploadResult, CollectionsNamespace, MediaNamespace, FoldersNamespace, TagsNamespace };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coreviz/sdk",
3
- "version": "1.0.19",
3
+ "version": "1.1.1",
4
4
  "description": "CoreViz SDK",
5
5
  "main": "dist/index.js",
6
6
  "react-native": "dist/index.native.js",