@supabase/storage-js 2.75.1 → 2.75.2-canary.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.
- package/README.md +917 -12
- package/dist/main/StorageClient.d.ts +28 -0
- package/dist/main/StorageClient.d.ts.map +1 -1
- package/dist/main/StorageClient.js +38 -5
- package/dist/main/StorageClient.js.map +1 -1
- package/dist/main/index.d.ts +2 -0
- package/dist/main/index.d.ts.map +1 -1
- package/dist/main/index.js +7 -17
- package/dist/main/index.js.map +1 -1
- package/dist/main/lib/constants.d.ts.map +1 -1
- package/dist/main/lib/constants.js +3 -1
- package/dist/main/lib/constants.js.map +1 -1
- package/dist/main/lib/fetch.js +8 -16
- package/dist/main/lib/fetch.js.map +1 -1
- package/dist/main/lib/helpers.js +4 -45
- package/dist/main/lib/helpers.js.map +1 -1
- package/dist/main/lib/index.d.ts +1 -0
- package/dist/main/lib/index.d.ts.map +1 -1
- package/dist/main/lib/index.js +6 -18
- package/dist/main/lib/index.js.map +1 -1
- package/dist/main/lib/types.d.ts +36 -4
- package/dist/main/lib/types.d.ts.map +1 -1
- package/dist/main/lib/vectors/StorageVectorsClient.d.ts +310 -0
- package/dist/main/lib/vectors/StorageVectorsClient.d.ts.map +1 -0
- package/dist/main/lib/vectors/StorageVectorsClient.js +366 -0
- package/dist/main/lib/vectors/StorageVectorsClient.js.map +1 -0
- package/dist/main/lib/vectors/VectorBucketApi.d.ts +129 -0
- package/dist/main/lib/vectors/VectorBucketApi.d.ts.map +1 -0
- package/dist/main/lib/vectors/VectorBucketApi.js +199 -0
- package/dist/main/lib/vectors/VectorBucketApi.js.map +1 -0
- package/dist/main/lib/vectors/VectorDataApi.d.ts +221 -0
- package/dist/main/lib/vectors/VectorDataApi.d.ts.map +1 -0
- package/dist/main/lib/vectors/VectorDataApi.js +336 -0
- package/dist/main/lib/vectors/VectorDataApi.js.map +1 -0
- package/dist/main/lib/vectors/VectorIndexApi.d.ts +157 -0
- package/dist/main/lib/vectors/VectorIndexApi.d.ts.map +1 -0
- package/dist/main/lib/vectors/VectorIndexApi.js +218 -0
- package/dist/main/lib/vectors/VectorIndexApi.js.map +1 -0
- package/dist/main/lib/vectors/constants.d.ts +5 -0
- package/dist/main/lib/vectors/constants.d.ts.map +1 -0
- package/dist/main/lib/vectors/constants.js +9 -0
- package/dist/main/lib/vectors/constants.js.map +1 -0
- package/dist/main/lib/vectors/errors.d.ts +55 -0
- package/dist/main/lib/vectors/errors.d.ts.map +1 -0
- package/dist/main/lib/vectors/errors.js +76 -0
- package/dist/main/lib/vectors/errors.js.map +1 -0
- package/dist/main/lib/vectors/fetch.d.ts +57 -0
- package/dist/main/lib/vectors/fetch.d.ts.map +1 -0
- package/dist/main/lib/vectors/fetch.js +167 -0
- package/dist/main/lib/vectors/fetch.js.map +1 -0
- package/dist/main/lib/vectors/helpers.d.ts +49 -0
- package/dist/main/lib/vectors/helpers.d.ts.map +1 -0
- package/dist/main/lib/vectors/helpers.js +89 -0
- package/dist/main/lib/vectors/helpers.js.map +1 -0
- package/dist/main/lib/vectors/index.d.ts +11 -0
- package/dist/main/lib/vectors/index.d.ts.map +1 -0
- package/dist/main/lib/vectors/index.js +31 -0
- package/dist/main/lib/vectors/index.js.map +1 -0
- package/dist/main/lib/vectors/types.d.ts +277 -0
- package/dist/main/lib/vectors/types.d.ts.map +1 -0
- package/dist/main/lib/vectors/types.js +3 -0
- package/dist/main/lib/vectors/types.js.map +1 -0
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.d.ts.map +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/main/lib/version.js.map +1 -1
- package/dist/main/packages/BlobDownloadBuilder.js +3 -14
- package/dist/main/packages/BlobDownloadBuilder.js.map +1 -1
- package/dist/main/packages/StorageAnalyticsApi.d.ts +123 -0
- package/dist/main/packages/StorageAnalyticsApi.d.ts.map +1 -0
- package/dist/main/packages/StorageAnalyticsApi.js +168 -0
- package/dist/main/packages/StorageAnalyticsApi.js.map +1 -0
- package/dist/main/packages/StorageBucketApi.js +7 -15
- package/dist/main/packages/StorageBucketApi.js.map +1 -1
- package/dist/main/packages/StorageFileApi.js +16 -27
- package/dist/main/packages/StorageFileApi.js.map +1 -1
- package/dist/main/packages/StreamDownloadBuilder.js +2 -10
- package/dist/main/packages/StreamDownloadBuilder.js.map +1 -1
- package/dist/module/StorageClient.d.ts +28 -0
- package/dist/module/StorageClient.d.ts.map +1 -1
- package/dist/module/StorageClient.js +35 -0
- package/dist/module/StorageClient.js.map +1 -1
- package/dist/module/index.d.ts +2 -0
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +2 -0
- package/dist/module/index.js.map +1 -1
- package/dist/module/lib/constants.d.ts.map +1 -1
- package/dist/module/lib/constants.js +3 -1
- package/dist/module/lib/constants.js.map +1 -1
- package/dist/module/lib/fetch.js +1 -9
- package/dist/module/lib/fetch.js.map +1 -1
- package/dist/module/lib/helpers.js +1 -9
- package/dist/module/lib/helpers.js.map +1 -1
- package/dist/module/lib/index.d.ts +1 -0
- package/dist/module/lib/index.d.ts.map +1 -1
- package/dist/module/lib/index.js +1 -0
- package/dist/module/lib/index.js.map +1 -1
- package/dist/module/lib/types.d.ts +36 -4
- package/dist/module/lib/types.d.ts.map +1 -1
- package/dist/module/lib/vectors/StorageVectorsClient.d.ts +310 -0
- package/dist/module/lib/vectors/StorageVectorsClient.d.ts.map +1 -0
- package/dist/module/lib/vectors/StorageVectorsClient.js +360 -0
- package/dist/module/lib/vectors/StorageVectorsClient.js.map +1 -0
- package/dist/module/lib/vectors/VectorBucketApi.d.ts +129 -0
- package/dist/module/lib/vectors/VectorBucketApi.d.ts.map +1 -0
- package/dist/module/lib/vectors/VectorBucketApi.js +196 -0
- package/dist/module/lib/vectors/VectorBucketApi.js.map +1 -0
- package/dist/module/lib/vectors/VectorDataApi.d.ts +221 -0
- package/dist/module/lib/vectors/VectorDataApi.d.ts.map +1 -0
- package/dist/module/lib/vectors/VectorDataApi.js +333 -0
- package/dist/module/lib/vectors/VectorDataApi.js.map +1 -0
- package/dist/module/lib/vectors/VectorIndexApi.d.ts +157 -0
- package/dist/module/lib/vectors/VectorIndexApi.d.ts.map +1 -0
- package/dist/module/lib/vectors/VectorIndexApi.js +215 -0
- package/dist/module/lib/vectors/VectorIndexApi.js.map +1 -0
- package/dist/module/lib/vectors/constants.d.ts +5 -0
- package/dist/module/lib/vectors/constants.d.ts.map +1 -0
- package/dist/module/lib/vectors/constants.js +6 -0
- package/dist/module/lib/vectors/constants.js.map +1 -0
- package/dist/module/lib/vectors/errors.d.ts +55 -0
- package/dist/module/lib/vectors/errors.d.ts.map +1 -0
- package/dist/module/lib/vectors/errors.js +69 -0
- package/dist/module/lib/vectors/errors.js.map +1 -0
- package/dist/module/lib/vectors/fetch.d.ts +57 -0
- package/dist/module/lib/vectors/fetch.d.ts.map +1 -0
- package/dist/module/lib/vectors/fetch.js +161 -0
- package/dist/module/lib/vectors/fetch.js.map +1 -0
- package/dist/module/lib/vectors/helpers.d.ts +49 -0
- package/dist/module/lib/vectors/helpers.d.ts.map +1 -0
- package/dist/module/lib/vectors/helpers.js +81 -0
- package/dist/module/lib/vectors/helpers.js.map +1 -0
- package/dist/module/lib/vectors/index.d.ts +11 -0
- package/dist/module/lib/vectors/index.d.ts.map +1 -0
- package/dist/module/lib/vectors/index.js +11 -0
- package/dist/module/lib/vectors/index.js.map +1 -0
- package/dist/module/lib/vectors/types.d.ts +277 -0
- package/dist/module/lib/vectors/types.d.ts.map +1 -0
- package/dist/module/lib/vectors/types.js +2 -0
- package/dist/module/lib/vectors/types.js.map +1 -0
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.d.ts.map +1 -1
- package/dist/module/lib/version.js +1 -1
- package/dist/module/lib/version.js.map +1 -1
- package/dist/module/packages/BlobDownloadBuilder.js +1 -9
- package/dist/module/packages/BlobDownloadBuilder.js.map +1 -1
- package/dist/module/packages/StorageAnalyticsApi.d.ts +123 -0
- package/dist/module/packages/StorageAnalyticsApi.d.ts.map +1 -0
- package/dist/module/packages/StorageAnalyticsApi.js +165 -0
- package/dist/module/packages/StorageAnalyticsApi.js.map +1 -0
- package/dist/module/packages/StorageBucketApi.js +1 -9
- package/dist/module/packages/StorageBucketApi.js.map +1 -1
- package/dist/module/packages/StorageFileApi.js +1 -9
- package/dist/module/packages/StorageFileApi.js.map +1 -1
- package/dist/module/packages/StreamDownloadBuilder.js +1 -9
- package/dist/module/packages/StreamDownloadBuilder.js.map +1 -1
- package/dist/tsconfig.module.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/umd/supabase.js +1 -1
- package/package.json +2 -2
- package/src/StorageClient.ts +37 -0
- package/src/index.ts +2 -0
- package/src/lib/constants.ts +3 -1
- package/src/lib/index.ts +1 -0
- package/src/lib/types.ts +39 -2
- package/src/lib/vectors/StorageVectorsClient.ts +405 -0
- package/src/lib/vectors/VectorBucketApi.ts +217 -0
- package/src/lib/vectors/VectorDataApi.ts +341 -0
- package/src/lib/vectors/VectorIndexApi.ts +245 -0
- package/src/lib/vectors/constants.ts +5 -0
- package/src/lib/vectors/errors.ts +78 -0
- package/src/lib/vectors/fetch.ts +218 -0
- package/src/lib/vectors/helpers.ts +93 -0
- package/src/lib/vectors/index.ts +66 -0
- package/src/lib/vectors/types.ts +299 -0
- package/src/lib/version.ts +1 -1
- package/src/packages/StorageAnalyticsApi.ts +202 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { DEFAULT_HEADERS } from './constants'
|
|
2
|
+
import { isStorageVectorsError } from './errors'
|
|
3
|
+
import { Fetch, post } from './fetch'
|
|
4
|
+
import { resolveFetch } from './helpers'
|
|
5
|
+
import {
|
|
6
|
+
ApiResponse,
|
|
7
|
+
PutVectorsOptions,
|
|
8
|
+
GetVectorsOptions,
|
|
9
|
+
GetVectorsResponse,
|
|
10
|
+
DeleteVectorsOptions,
|
|
11
|
+
ListVectorsOptions,
|
|
12
|
+
ListVectorsResponse,
|
|
13
|
+
QueryVectorsOptions,
|
|
14
|
+
QueryVectorsResponse,
|
|
15
|
+
} from './types'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* API class for managing Vector Data within Vector Indexes
|
|
19
|
+
* Provides methods for inserting, querying, listing, and deleting vector embeddings
|
|
20
|
+
*/
|
|
21
|
+
export default class VectorDataApi {
|
|
22
|
+
protected url: string
|
|
23
|
+
protected headers: { [key: string]: string }
|
|
24
|
+
protected fetch: Fetch
|
|
25
|
+
protected shouldThrowOnError = false
|
|
26
|
+
|
|
27
|
+
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
|
|
28
|
+
this.url = url.replace(/\/$/, '')
|
|
29
|
+
this.headers = { ...DEFAULT_HEADERS, ...headers }
|
|
30
|
+
this.fetch = resolveFetch(fetch)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Enable throwing errors instead of returning them in the response
|
|
35
|
+
* When enabled, failed operations will throw instead of returning { data: null, error }
|
|
36
|
+
*
|
|
37
|
+
* @returns This instance for method chaining
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const client = new VectorDataApi(url, headers)
|
|
41
|
+
* client.throwOnError()
|
|
42
|
+
* const { data } = await client.putVectors(options) // throws on error
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
public throwOnError(): this {
|
|
46
|
+
this.shouldThrowOnError = true
|
|
47
|
+
return this
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Inserts or updates vectors in batch (upsert operation)
|
|
52
|
+
* Accepts 1-500 vectors per request. Larger batches should be split
|
|
53
|
+
*
|
|
54
|
+
* @param options - Vector insertion options
|
|
55
|
+
* @param options.vectorBucketName - Name of the parent vector bucket
|
|
56
|
+
* @param options.indexName - Name of the target index
|
|
57
|
+
* @param options.vectors - Array of vectors to insert/update (1-500 items)
|
|
58
|
+
* @returns Promise with empty response on success or error
|
|
59
|
+
*
|
|
60
|
+
* @throws {StorageVectorsApiError} With code:
|
|
61
|
+
* - `S3VectorConflictException` if duplicate key conflict occurs (HTTP 409)
|
|
62
|
+
* - `S3VectorNotFoundException` if bucket or index doesn't exist (HTTP 404)
|
|
63
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const { data, error } = await client.putVectors({
|
|
68
|
+
* vectorBucketName: 'embeddings-prod',
|
|
69
|
+
* indexName: 'documents-openai-small',
|
|
70
|
+
* vectors: [
|
|
71
|
+
* {
|
|
72
|
+
* key: 'doc-1',
|
|
73
|
+
* data: { float32: [0.1, 0.2, 0.3, ...] }, // 1536 dimensions
|
|
74
|
+
* metadata: { title: 'Introduction', page: 1 }
|
|
75
|
+
* },
|
|
76
|
+
* {
|
|
77
|
+
* key: 'doc-2',
|
|
78
|
+
* data: { float32: [0.4, 0.5, 0.6, ...] },
|
|
79
|
+
* metadata: { title: 'Conclusion', page: 42 }
|
|
80
|
+
* }
|
|
81
|
+
* ]
|
|
82
|
+
* })
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
async putVectors(options: PutVectorsOptions): Promise<ApiResponse<undefined>> {
|
|
86
|
+
try {
|
|
87
|
+
// Validate batch size
|
|
88
|
+
if (options.vectors.length < 1 || options.vectors.length > 500) {
|
|
89
|
+
throw new Error('Vector batch size must be between 1 and 500 items')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const data = await post(this.fetch, `${this.url}/PutVectors`, options, {
|
|
93
|
+
headers: this.headers,
|
|
94
|
+
})
|
|
95
|
+
return { data: data || {}, error: null }
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (this.shouldThrowOnError) {
|
|
98
|
+
throw error
|
|
99
|
+
}
|
|
100
|
+
if (isStorageVectorsError(error)) {
|
|
101
|
+
return { data: null, error }
|
|
102
|
+
}
|
|
103
|
+
throw error
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Retrieves vectors by their keys in batch
|
|
109
|
+
* Optionally includes vector data and/or metadata in response
|
|
110
|
+
* Additional permissions required when returning data or metadata
|
|
111
|
+
*
|
|
112
|
+
* @param options - Vector retrieval options
|
|
113
|
+
* @param options.vectorBucketName - Name of the parent vector bucket
|
|
114
|
+
* @param options.indexName - Name of the index
|
|
115
|
+
* @param options.keys - Array of vector keys to retrieve
|
|
116
|
+
* @param options.returnData - Whether to include vector embeddings (requires permission)
|
|
117
|
+
* @param options.returnMetadata - Whether to include metadata (requires permission)
|
|
118
|
+
* @returns Promise with array of vectors or error
|
|
119
|
+
*
|
|
120
|
+
* @throws {StorageVectorsApiError} With code:
|
|
121
|
+
* - `S3VectorNotFoundException` if bucket or index doesn't exist (HTTP 404)
|
|
122
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const { data, error } = await client.getVectors({
|
|
127
|
+
* vectorBucketName: 'embeddings-prod',
|
|
128
|
+
* indexName: 'documents-openai-small',
|
|
129
|
+
* keys: ['doc-1', 'doc-2', 'doc-3'],
|
|
130
|
+
* returnData: false, // Don't return embeddings
|
|
131
|
+
* returnMetadata: true // Return metadata only
|
|
132
|
+
* })
|
|
133
|
+
* if (data) {
|
|
134
|
+
* data.vectors.forEach(v => console.log(v.key, v.metadata))
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
async getVectors(options: GetVectorsOptions): Promise<ApiResponse<GetVectorsResponse>> {
|
|
139
|
+
try {
|
|
140
|
+
const data = await post(this.fetch, `${this.url}/GetVectors`, options, {
|
|
141
|
+
headers: this.headers,
|
|
142
|
+
})
|
|
143
|
+
return { data, error: null }
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (this.shouldThrowOnError) {
|
|
146
|
+
throw error
|
|
147
|
+
}
|
|
148
|
+
if (isStorageVectorsError(error)) {
|
|
149
|
+
return { data: null, error }
|
|
150
|
+
}
|
|
151
|
+
throw error
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Lists/scans vectors in an index with pagination
|
|
157
|
+
* Supports parallel scanning via segment configuration for high-throughput scenarios
|
|
158
|
+
* Additional permissions required when returning data or metadata
|
|
159
|
+
*
|
|
160
|
+
* @param options - Vector listing options
|
|
161
|
+
* @param options.vectorBucketName - Name of the parent vector bucket
|
|
162
|
+
* @param options.indexName - Name of the index
|
|
163
|
+
* @param options.maxResults - Maximum results per page (default: 500, max: 1000)
|
|
164
|
+
* @param options.nextToken - Pagination token from previous response
|
|
165
|
+
* @param options.returnData - Whether to include vector embeddings (requires permission)
|
|
166
|
+
* @param options.returnMetadata - Whether to include metadata (requires permission)
|
|
167
|
+
* @param options.segmentCount - Total parallel segments (1-16) for distributed scanning
|
|
168
|
+
* @param options.segmentIndex - Zero-based segment index (0 to segmentCount-1)
|
|
169
|
+
* @returns Promise with array of vectors, pagination token, or error
|
|
170
|
+
*
|
|
171
|
+
* @throws {StorageVectorsApiError} With code:
|
|
172
|
+
* - `S3VectorNotFoundException` if bucket or index doesn't exist (HTTP 404)
|
|
173
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* // Simple pagination
|
|
178
|
+
* let nextToken: string | undefined
|
|
179
|
+
* do {
|
|
180
|
+
* const { data, error } = await client.listVectors({
|
|
181
|
+
* vectorBucketName: 'embeddings-prod',
|
|
182
|
+
* indexName: 'documents-openai-small',
|
|
183
|
+
* maxResults: 500,
|
|
184
|
+
* nextToken,
|
|
185
|
+
* returnMetadata: true
|
|
186
|
+
* })
|
|
187
|
+
* if (error) break
|
|
188
|
+
* console.log('Batch:', data.vectors.length)
|
|
189
|
+
* nextToken = data.nextToken
|
|
190
|
+
* } while (nextToken)
|
|
191
|
+
*
|
|
192
|
+
* // Parallel scanning (4 concurrent workers)
|
|
193
|
+
* const workers = [0, 1, 2, 3].map(async (segmentIndex) => {
|
|
194
|
+
* const { data } = await client.listVectors({
|
|
195
|
+
* vectorBucketName: 'embeddings-prod',
|
|
196
|
+
* indexName: 'documents-openai-small',
|
|
197
|
+
* segmentCount: 4,
|
|
198
|
+
* segmentIndex,
|
|
199
|
+
* returnMetadata: true
|
|
200
|
+
* })
|
|
201
|
+
* return data?.vectors || []
|
|
202
|
+
* })
|
|
203
|
+
* const results = await Promise.all(workers)
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
async listVectors(options: ListVectorsOptions): Promise<ApiResponse<ListVectorsResponse>> {
|
|
207
|
+
try {
|
|
208
|
+
// Validate segment configuration
|
|
209
|
+
if (options.segmentCount !== undefined) {
|
|
210
|
+
if (options.segmentCount < 1 || options.segmentCount > 16) {
|
|
211
|
+
throw new Error('segmentCount must be between 1 and 16')
|
|
212
|
+
}
|
|
213
|
+
if (options.segmentIndex !== undefined) {
|
|
214
|
+
if (options.segmentIndex < 0 || options.segmentIndex >= options.segmentCount) {
|
|
215
|
+
throw new Error(`segmentIndex must be between 0 and ${options.segmentCount - 1}`)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const data = await post(this.fetch, `${this.url}/ListVectors`, options, {
|
|
221
|
+
headers: this.headers,
|
|
222
|
+
})
|
|
223
|
+
return { data, error: null }
|
|
224
|
+
} catch (error) {
|
|
225
|
+
if (this.shouldThrowOnError) {
|
|
226
|
+
throw error
|
|
227
|
+
}
|
|
228
|
+
if (isStorageVectorsError(error)) {
|
|
229
|
+
return { data: null, error }
|
|
230
|
+
}
|
|
231
|
+
throw error
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Queries for similar vectors using approximate nearest neighbor (ANN) search
|
|
237
|
+
* Returns top-K most similar vectors based on the configured distance metric
|
|
238
|
+
* Supports optional metadata filtering (requires GetVectors permission)
|
|
239
|
+
*
|
|
240
|
+
* @param options - Query options
|
|
241
|
+
* @param options.vectorBucketName - Name of the parent vector bucket
|
|
242
|
+
* @param options.indexName - Name of the index
|
|
243
|
+
* @param options.queryVector - Query embedding to find similar vectors
|
|
244
|
+
* @param options.topK - Number of nearest neighbors to return (default: 10)
|
|
245
|
+
* @param options.filter - Optional JSON filter for metadata (requires GetVectors permission)
|
|
246
|
+
* @param options.returnDistance - Whether to include similarity distances
|
|
247
|
+
* @param options.returnMetadata - Whether to include metadata (requires GetVectors permission)
|
|
248
|
+
* @returns Promise with array of similar vectors ordered by distance
|
|
249
|
+
*
|
|
250
|
+
* @throws {StorageVectorsApiError} With code:
|
|
251
|
+
* - `S3VectorNotFoundException` if bucket or index doesn't exist (HTTP 404)
|
|
252
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* // Semantic search with filtering
|
|
257
|
+
* const { data, error } = await client.queryVectors({
|
|
258
|
+
* vectorBucketName: 'embeddings-prod',
|
|
259
|
+
* indexName: 'documents-openai-small',
|
|
260
|
+
* queryVector: { float32: [0.1, 0.2, 0.3, ...] }, // 1536 dimensions
|
|
261
|
+
* topK: 5,
|
|
262
|
+
* filter: {
|
|
263
|
+
* category: 'technical',
|
|
264
|
+
* published: true
|
|
265
|
+
* },
|
|
266
|
+
* returnDistance: true,
|
|
267
|
+
* returnMetadata: true
|
|
268
|
+
* })
|
|
269
|
+
* if (data) {
|
|
270
|
+
* data.matches.forEach(match => {
|
|
271
|
+
* console.log(`${match.key}: distance=${match.distance}`)
|
|
272
|
+
* console.log('Metadata:', match.metadata)
|
|
273
|
+
* })
|
|
274
|
+
* }
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
async queryVectors(options: QueryVectorsOptions): Promise<ApiResponse<QueryVectorsResponse>> {
|
|
278
|
+
try {
|
|
279
|
+
const data = await post(this.fetch, `${this.url}/QueryVectors`, options, {
|
|
280
|
+
headers: this.headers,
|
|
281
|
+
})
|
|
282
|
+
return { data, error: null }
|
|
283
|
+
} catch (error) {
|
|
284
|
+
if (this.shouldThrowOnError) {
|
|
285
|
+
throw error
|
|
286
|
+
}
|
|
287
|
+
if (isStorageVectorsError(error)) {
|
|
288
|
+
return { data: null, error }
|
|
289
|
+
}
|
|
290
|
+
throw error
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Deletes vectors by their keys in batch
|
|
296
|
+
* Accepts 1-500 keys per request
|
|
297
|
+
*
|
|
298
|
+
* @param options - Vector deletion options
|
|
299
|
+
* @param options.vectorBucketName - Name of the parent vector bucket
|
|
300
|
+
* @param options.indexName - Name of the index
|
|
301
|
+
* @param options.keys - Array of vector keys to delete (1-500 items)
|
|
302
|
+
* @returns Promise with empty response on success or error
|
|
303
|
+
*
|
|
304
|
+
* @throws {StorageVectorsApiError} With code:
|
|
305
|
+
* - `S3VectorNotFoundException` if bucket or index doesn't exist (HTTP 404)
|
|
306
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* const { error } = await client.deleteVectors({
|
|
311
|
+
* vectorBucketName: 'embeddings-prod',
|
|
312
|
+
* indexName: 'documents-openai-small',
|
|
313
|
+
* keys: ['doc-1', 'doc-2', 'doc-3']
|
|
314
|
+
* })
|
|
315
|
+
* if (!error) {
|
|
316
|
+
* console.log('Vectors deleted successfully')
|
|
317
|
+
* }
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
async deleteVectors(options: DeleteVectorsOptions): Promise<ApiResponse<undefined>> {
|
|
321
|
+
try {
|
|
322
|
+
// Validate batch size
|
|
323
|
+
if (options.keys.length < 1 || options.keys.length > 500) {
|
|
324
|
+
throw new Error('Keys batch size must be between 1 and 500 items')
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const data = await post(this.fetch, `${this.url}/DeleteVectors`, options, {
|
|
328
|
+
headers: this.headers,
|
|
329
|
+
})
|
|
330
|
+
return { data: data || {}, error: null }
|
|
331
|
+
} catch (error) {
|
|
332
|
+
if (this.shouldThrowOnError) {
|
|
333
|
+
throw error
|
|
334
|
+
}
|
|
335
|
+
if (isStorageVectorsError(error)) {
|
|
336
|
+
return { data: null, error }
|
|
337
|
+
}
|
|
338
|
+
throw error
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { DEFAULT_HEADERS } from './constants'
|
|
2
|
+
import { isStorageVectorsError } from './errors'
|
|
3
|
+
import { Fetch, post } from './fetch'
|
|
4
|
+
import { resolveFetch } from './helpers'
|
|
5
|
+
import {
|
|
6
|
+
ApiResponse,
|
|
7
|
+
VectorIndex,
|
|
8
|
+
ListIndexesOptions,
|
|
9
|
+
ListIndexesResponse,
|
|
10
|
+
VectorDataType,
|
|
11
|
+
DistanceMetric,
|
|
12
|
+
MetadataConfiguration,
|
|
13
|
+
} from './types'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for creating a vector index
|
|
17
|
+
*/
|
|
18
|
+
export interface CreateIndexOptions {
|
|
19
|
+
vectorBucketName: string
|
|
20
|
+
indexName: string
|
|
21
|
+
dataType: VectorDataType
|
|
22
|
+
dimension: number
|
|
23
|
+
distanceMetric: DistanceMetric
|
|
24
|
+
metadataConfiguration?: MetadataConfiguration
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* API class for managing Vector Indexes within Vector Buckets
|
|
29
|
+
* Provides methods for creating, reading, listing, and deleting vector indexes
|
|
30
|
+
*/
|
|
31
|
+
export default class VectorIndexApi {
|
|
32
|
+
protected url: string
|
|
33
|
+
protected headers: { [key: string]: string }
|
|
34
|
+
protected fetch: Fetch
|
|
35
|
+
protected shouldThrowOnError = false
|
|
36
|
+
|
|
37
|
+
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
|
|
38
|
+
this.url = url.replace(/\/$/, '')
|
|
39
|
+
this.headers = { ...DEFAULT_HEADERS, ...headers }
|
|
40
|
+
this.fetch = resolveFetch(fetch)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Enable throwing errors instead of returning them in the response
|
|
45
|
+
* When enabled, failed operations will throw instead of returning { data: null, error }
|
|
46
|
+
*
|
|
47
|
+
* @returns This instance for method chaining
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const client = new VectorIndexApi(url, headers)
|
|
51
|
+
* client.throwOnError()
|
|
52
|
+
* const { data } = await client.createIndex(options) // throws on error
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
public throwOnError(): this {
|
|
56
|
+
this.shouldThrowOnError = true
|
|
57
|
+
return this
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new vector index within a bucket
|
|
62
|
+
* Defines the schema for vectors including dimensionality, distance metric, and metadata config
|
|
63
|
+
*
|
|
64
|
+
* @param options - Index configuration
|
|
65
|
+
* @param options.vectorBucketName - Name of the parent vector bucket
|
|
66
|
+
* @param options.indexName - Unique name for the index within the bucket
|
|
67
|
+
* @param options.dataType - Data type for vector components (currently only 'float32')
|
|
68
|
+
* @param options.dimension - Dimensionality of vectors (e.g., 384, 768, 1536)
|
|
69
|
+
* @param options.distanceMetric - Similarity metric ('cosine', 'euclidean', 'dotproduct')
|
|
70
|
+
* @param options.metadataConfiguration - Optional config for non-filterable metadata keys
|
|
71
|
+
* @returns Promise with empty response on success or error
|
|
72
|
+
*
|
|
73
|
+
* @throws {StorageVectorsApiError} With code:
|
|
74
|
+
* - `S3VectorConflictException` if index already exists (HTTP 409)
|
|
75
|
+
* - `S3VectorMaxIndexesExceeded` if quota exceeded (HTTP 400)
|
|
76
|
+
* - `S3VectorNotFoundException` if bucket doesn't exist (HTTP 404)
|
|
77
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const { data, error } = await client.createIndex({
|
|
82
|
+
* vectorBucketName: 'embeddings-prod',
|
|
83
|
+
* indexName: 'documents-openai-small',
|
|
84
|
+
* dataType: 'float32',
|
|
85
|
+
* dimension: 1536,
|
|
86
|
+
* distanceMetric: 'cosine',
|
|
87
|
+
* metadataConfiguration: {
|
|
88
|
+
* nonFilterableMetadataKeys: ['raw_text', 'internal_id']
|
|
89
|
+
* }
|
|
90
|
+
* })
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
async createIndex(options: CreateIndexOptions): Promise<ApiResponse<undefined>> {
|
|
94
|
+
try {
|
|
95
|
+
const data = await post(this.fetch, `${this.url}/CreateIndex`, options, {
|
|
96
|
+
headers: this.headers,
|
|
97
|
+
})
|
|
98
|
+
return { data: data || {}, error: null }
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (this.shouldThrowOnError) {
|
|
101
|
+
throw error
|
|
102
|
+
}
|
|
103
|
+
if (isStorageVectorsError(error)) {
|
|
104
|
+
return { data: null, error }
|
|
105
|
+
}
|
|
106
|
+
throw error
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Retrieves metadata for a specific vector index
|
|
112
|
+
* Returns index configuration including dimension, distance metric, and metadata settings
|
|
113
|
+
*
|
|
114
|
+
* @param vectorBucketName - Name of the parent vector bucket
|
|
115
|
+
* @param indexName - Name of the index to retrieve
|
|
116
|
+
* @returns Promise with index metadata or error
|
|
117
|
+
*
|
|
118
|
+
* @throws {StorageVectorsApiError} With code:
|
|
119
|
+
* - `S3VectorNotFoundException` if index or bucket doesn't exist (HTTP 404)
|
|
120
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const { data, error } = await client.getIndex('embeddings-prod', 'documents-openai-small')
|
|
125
|
+
* if (data) {
|
|
126
|
+
* console.log('Index dimension:', data.index.dimension)
|
|
127
|
+
* console.log('Distance metric:', data.index.distanceMetric)
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
async getIndex(
|
|
132
|
+
vectorBucketName: string,
|
|
133
|
+
indexName: string
|
|
134
|
+
): Promise<ApiResponse<{ index: VectorIndex }>> {
|
|
135
|
+
try {
|
|
136
|
+
const data = await post(
|
|
137
|
+
this.fetch,
|
|
138
|
+
`${this.url}/GetIndex`,
|
|
139
|
+
{ vectorBucketName, indexName },
|
|
140
|
+
{ headers: this.headers }
|
|
141
|
+
)
|
|
142
|
+
return { data, error: null }
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (this.shouldThrowOnError) {
|
|
145
|
+
throw error
|
|
146
|
+
}
|
|
147
|
+
if (isStorageVectorsError(error)) {
|
|
148
|
+
return { data: null, error }
|
|
149
|
+
}
|
|
150
|
+
throw error
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Lists vector indexes within a bucket with optional filtering and pagination
|
|
156
|
+
* Supports prefix-based filtering and paginated results
|
|
157
|
+
*
|
|
158
|
+
* @param options - Listing options
|
|
159
|
+
* @param options.vectorBucketName - Name of the parent vector bucket
|
|
160
|
+
* @param options.prefix - Filter indexes by name prefix
|
|
161
|
+
* @param options.maxResults - Maximum results per page (default: 100)
|
|
162
|
+
* @param options.nextToken - Pagination token from previous response
|
|
163
|
+
* @returns Promise with list of indexes and pagination token
|
|
164
|
+
*
|
|
165
|
+
* @throws {StorageVectorsApiError} With code:
|
|
166
|
+
* - `S3VectorNotFoundException` if bucket doesn't exist (HTTP 404)
|
|
167
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* // List all indexes in a bucket
|
|
172
|
+
* const { data, error } = await client.listIndexes({
|
|
173
|
+
* vectorBucketName: 'embeddings-prod',
|
|
174
|
+
* prefix: 'documents-'
|
|
175
|
+
* })
|
|
176
|
+
* if (data) {
|
|
177
|
+
* console.log('Found indexes:', data.indexes.map(i => i.indexName))
|
|
178
|
+
* // Fetch next page if available
|
|
179
|
+
* if (data.nextToken) {
|
|
180
|
+
* const next = await client.listIndexes({
|
|
181
|
+
* vectorBucketName: 'embeddings-prod',
|
|
182
|
+
* nextToken: data.nextToken
|
|
183
|
+
* })
|
|
184
|
+
* }
|
|
185
|
+
* }
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
async listIndexes(options: ListIndexesOptions): Promise<ApiResponse<ListIndexesResponse>> {
|
|
189
|
+
try {
|
|
190
|
+
const data = await post(this.fetch, `${this.url}/ListIndexes`, options, {
|
|
191
|
+
headers: this.headers,
|
|
192
|
+
})
|
|
193
|
+
return { data, error: null }
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (this.shouldThrowOnError) {
|
|
196
|
+
throw error
|
|
197
|
+
}
|
|
198
|
+
if (isStorageVectorsError(error)) {
|
|
199
|
+
return { data: null, error }
|
|
200
|
+
}
|
|
201
|
+
throw error
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Deletes a vector index and all its data
|
|
207
|
+
* This operation removes the index schema and all vectors stored in the index
|
|
208
|
+
*
|
|
209
|
+
* @param vectorBucketName - Name of the parent vector bucket
|
|
210
|
+
* @param indexName - Name of the index to delete
|
|
211
|
+
* @returns Promise with empty response on success or error
|
|
212
|
+
*
|
|
213
|
+
* @throws {StorageVectorsApiError} With code:
|
|
214
|
+
* - `S3VectorNotFoundException` if index or bucket doesn't exist (HTTP 404)
|
|
215
|
+
* - `InternalError` for server errors (HTTP 500)
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* // Delete an index and all its vectors
|
|
220
|
+
* const { error } = await client.deleteIndex('embeddings-prod', 'old-index')
|
|
221
|
+
* if (!error) {
|
|
222
|
+
* console.log('Index deleted successfully')
|
|
223
|
+
* }
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
async deleteIndex(vectorBucketName: string, indexName: string): Promise<ApiResponse<undefined>> {
|
|
227
|
+
try {
|
|
228
|
+
const data = await post(
|
|
229
|
+
this.fetch,
|
|
230
|
+
`${this.url}/DeleteIndex`,
|
|
231
|
+
{ vectorBucketName, indexName },
|
|
232
|
+
{ headers: this.headers }
|
|
233
|
+
)
|
|
234
|
+
return { data: data || {}, error: null }
|
|
235
|
+
} catch (error) {
|
|
236
|
+
if (this.shouldThrowOnError) {
|
|
237
|
+
throw error
|
|
238
|
+
}
|
|
239
|
+
if (isStorageVectorsError(error)) {
|
|
240
|
+
return { data: null, error }
|
|
241
|
+
}
|
|
242
|
+
throw error
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all Storage Vectors errors
|
|
3
|
+
*/
|
|
4
|
+
export class StorageVectorsError extends Error {
|
|
5
|
+
protected __isStorageVectorsError = true
|
|
6
|
+
|
|
7
|
+
constructor(message: string) {
|
|
8
|
+
super(message)
|
|
9
|
+
this.name = 'StorageVectorsError'
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Type guard to check if an error is a StorageVectorsError
|
|
15
|
+
* @param error - The error to check
|
|
16
|
+
* @returns True if the error is a StorageVectorsError
|
|
17
|
+
*/
|
|
18
|
+
export function isStorageVectorsError(error: unknown): error is StorageVectorsError {
|
|
19
|
+
return typeof error === 'object' && error !== null && '__isStorageVectorsError' in error
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* API error returned from S3 Vectors service
|
|
24
|
+
* Includes HTTP status code and service-specific error code
|
|
25
|
+
*/
|
|
26
|
+
export class StorageVectorsApiError extends StorageVectorsError {
|
|
27
|
+
status: number
|
|
28
|
+
statusCode: string
|
|
29
|
+
|
|
30
|
+
constructor(message: string, status: number, statusCode: string) {
|
|
31
|
+
super(message)
|
|
32
|
+
this.name = 'StorageVectorsApiError'
|
|
33
|
+
this.status = status
|
|
34
|
+
this.statusCode = statusCode
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
toJSON() {
|
|
38
|
+
return {
|
|
39
|
+
name: this.name,
|
|
40
|
+
message: this.message,
|
|
41
|
+
status: this.status,
|
|
42
|
+
statusCode: this.statusCode,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Unknown error that doesn't match expected error patterns
|
|
49
|
+
* Wraps the original error for debugging
|
|
50
|
+
*/
|
|
51
|
+
export class StorageVectorsUnknownError extends StorageVectorsError {
|
|
52
|
+
originalError: unknown
|
|
53
|
+
|
|
54
|
+
constructor(message: string, originalError: unknown) {
|
|
55
|
+
super(message)
|
|
56
|
+
this.name = 'StorageVectorsUnknownError'
|
|
57
|
+
this.originalError = originalError
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Error codes specific to S3 Vectors API
|
|
63
|
+
* Maps AWS service errors to application-friendly error codes
|
|
64
|
+
*/
|
|
65
|
+
export enum StorageVectorsErrorCode {
|
|
66
|
+
/** Internal server fault (HTTP 500) */
|
|
67
|
+
InternalError = 'InternalError',
|
|
68
|
+
/** Resource already exists / conflict (HTTP 409) */
|
|
69
|
+
S3VectorConflictException = 'S3VectorConflictException',
|
|
70
|
+
/** Resource not found (HTTP 404) */
|
|
71
|
+
S3VectorNotFoundException = 'S3VectorNotFoundException',
|
|
72
|
+
/** Delete bucket while not empty (HTTP 400) */
|
|
73
|
+
S3VectorBucketNotEmpty = 'S3VectorBucketNotEmpty',
|
|
74
|
+
/** Exceeds bucket quota/limit (HTTP 400) */
|
|
75
|
+
S3VectorMaxBucketsExceeded = 'S3VectorMaxBucketsExceeded',
|
|
76
|
+
/** Exceeds index quota/limit (HTTP 400) */
|
|
77
|
+
S3VectorMaxIndexesExceeded = 'S3VectorMaxIndexesExceeded',
|
|
78
|
+
}
|