@supabase/storage-js 2.79.1-canary.1 → 2.80.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 (197) hide show
  1. package/README.md +920 -12
  2. package/dist/main/StorageClient.d.ts +28 -0
  3. package/dist/main/StorageClient.d.ts.map +1 -1
  4. package/dist/main/StorageClient.js +38 -5
  5. package/dist/main/StorageClient.js.map +1 -1
  6. package/dist/main/index.d.ts +2 -0
  7. package/dist/main/index.d.ts.map +1 -1
  8. package/dist/main/index.js +7 -17
  9. package/dist/main/index.js.map +1 -1
  10. package/dist/main/lib/constants.d.ts.map +1 -1
  11. package/dist/main/lib/constants.js +3 -1
  12. package/dist/main/lib/constants.js.map +1 -1
  13. package/dist/main/lib/fetch.d.ts.map +1 -1
  14. package/dist/main/lib/fetch.js +16 -17
  15. package/dist/main/lib/fetch.js.map +1 -1
  16. package/dist/main/lib/helpers.d.ts +1 -1
  17. package/dist/main/lib/helpers.d.ts.map +1 -1
  18. package/dist/main/lib/helpers.js +4 -57
  19. package/dist/main/lib/helpers.js.map +1 -1
  20. package/dist/main/lib/index.d.ts +1 -0
  21. package/dist/main/lib/index.d.ts.map +1 -1
  22. package/dist/main/lib/index.js +6 -18
  23. package/dist/main/lib/index.js.map +1 -1
  24. package/dist/main/lib/types.d.ts +74 -4
  25. package/dist/main/lib/types.d.ts.map +1 -1
  26. package/dist/main/lib/vectors/StorageVectorsClient.d.ts +310 -0
  27. package/dist/main/lib/vectors/StorageVectorsClient.d.ts.map +1 -0
  28. package/dist/main/lib/vectors/StorageVectorsClient.js +366 -0
  29. package/dist/main/lib/vectors/StorageVectorsClient.js.map +1 -0
  30. package/dist/main/lib/vectors/VectorBucketApi.d.ts +129 -0
  31. package/dist/main/lib/vectors/VectorBucketApi.d.ts.map +1 -0
  32. package/dist/main/lib/vectors/VectorBucketApi.js +199 -0
  33. package/dist/main/lib/vectors/VectorBucketApi.js.map +1 -0
  34. package/dist/main/lib/vectors/VectorDataApi.d.ts +221 -0
  35. package/dist/main/lib/vectors/VectorDataApi.d.ts.map +1 -0
  36. package/dist/main/lib/vectors/VectorDataApi.js +336 -0
  37. package/dist/main/lib/vectors/VectorDataApi.js.map +1 -0
  38. package/dist/main/lib/vectors/VectorIndexApi.d.ts +157 -0
  39. package/dist/main/lib/vectors/VectorIndexApi.d.ts.map +1 -0
  40. package/dist/main/lib/vectors/VectorIndexApi.js +218 -0
  41. package/dist/main/lib/vectors/VectorIndexApi.js.map +1 -0
  42. package/dist/main/lib/vectors/constants.d.ts +5 -0
  43. package/dist/main/lib/vectors/constants.d.ts.map +1 -0
  44. package/dist/main/lib/vectors/constants.js +9 -0
  45. package/dist/main/lib/vectors/constants.js.map +1 -0
  46. package/dist/main/lib/vectors/errors.d.ts +55 -0
  47. package/dist/main/lib/vectors/errors.d.ts.map +1 -0
  48. package/dist/main/lib/vectors/errors.js +76 -0
  49. package/dist/main/lib/vectors/errors.js.map +1 -0
  50. package/dist/main/lib/vectors/fetch.d.ts +57 -0
  51. package/dist/main/lib/vectors/fetch.d.ts.map +1 -0
  52. package/dist/main/lib/vectors/fetch.js +167 -0
  53. package/dist/main/lib/vectors/fetch.js.map +1 -0
  54. package/dist/main/lib/vectors/helpers.d.ts +46 -0
  55. package/dist/main/lib/vectors/helpers.d.ts.map +1 -0
  56. package/dist/main/lib/vectors/helpers.js +74 -0
  57. package/dist/main/lib/vectors/helpers.js.map +1 -0
  58. package/dist/main/lib/vectors/index.d.ts +11 -0
  59. package/dist/main/lib/vectors/index.d.ts.map +1 -0
  60. package/dist/main/lib/vectors/index.js +31 -0
  61. package/dist/main/lib/vectors/index.js.map +1 -0
  62. package/dist/main/lib/vectors/types.d.ts +277 -0
  63. package/dist/main/lib/vectors/types.d.ts.map +1 -0
  64. package/dist/main/lib/vectors/types.js +3 -0
  65. package/dist/main/lib/vectors/types.js.map +1 -0
  66. package/dist/main/lib/version.d.ts +1 -1
  67. package/dist/main/lib/version.d.ts.map +1 -1
  68. package/dist/main/lib/version.js +1 -1
  69. package/dist/main/lib/version.js.map +1 -1
  70. package/dist/main/packages/BlobDownloadBuilder.d.ts +6 -1
  71. package/dist/main/packages/BlobDownloadBuilder.d.ts.map +1 -1
  72. package/dist/main/packages/BlobDownloadBuilder.js +20 -15
  73. package/dist/main/packages/BlobDownloadBuilder.js.map +1 -1
  74. package/dist/main/packages/StorageAnalyticsApi.d.ts +123 -0
  75. package/dist/main/packages/StorageAnalyticsApi.d.ts.map +1 -0
  76. package/dist/main/packages/StorageAnalyticsApi.js +164 -0
  77. package/dist/main/packages/StorageAnalyticsApi.js.map +1 -0
  78. package/dist/main/packages/StorageBucketApi.d.ts +3 -2
  79. package/dist/main/packages/StorageBucketApi.d.ts.map +1 -1
  80. package/dist/main/packages/StorageBucketApi.js +34 -18
  81. package/dist/main/packages/StorageBucketApi.js.map +1 -1
  82. package/dist/main/packages/StorageFileApi.d.ts.map +1 -1
  83. package/dist/main/packages/StorageFileApi.js +28 -29
  84. package/dist/main/packages/StorageFileApi.js.map +1 -1
  85. package/dist/main/packages/StreamDownloadBuilder.js +2 -10
  86. package/dist/main/packages/StreamDownloadBuilder.js.map +1 -1
  87. package/dist/module/StorageClient.d.ts +28 -0
  88. package/dist/module/StorageClient.d.ts.map +1 -1
  89. package/dist/module/StorageClient.js +35 -0
  90. package/dist/module/StorageClient.js.map +1 -1
  91. package/dist/module/index.d.ts +2 -0
  92. package/dist/module/index.d.ts.map +1 -1
  93. package/dist/module/index.js +2 -0
  94. package/dist/module/index.js.map +1 -1
  95. package/dist/module/lib/constants.d.ts.map +1 -1
  96. package/dist/module/lib/constants.js +3 -1
  97. package/dist/module/lib/constants.js.map +1 -1
  98. package/dist/module/lib/fetch.d.ts.map +1 -1
  99. package/dist/module/lib/fetch.js +9 -10
  100. package/dist/module/lib/fetch.js.map +1 -1
  101. package/dist/module/lib/helpers.d.ts +1 -1
  102. package/dist/module/lib/helpers.d.ts.map +1 -1
  103. package/dist/module/lib/helpers.js +4 -24
  104. package/dist/module/lib/helpers.js.map +1 -1
  105. package/dist/module/lib/index.d.ts +1 -0
  106. package/dist/module/lib/index.d.ts.map +1 -1
  107. package/dist/module/lib/index.js +1 -0
  108. package/dist/module/lib/index.js.map +1 -1
  109. package/dist/module/lib/types.d.ts +74 -4
  110. package/dist/module/lib/types.d.ts.map +1 -1
  111. package/dist/module/lib/vectors/StorageVectorsClient.d.ts +310 -0
  112. package/dist/module/lib/vectors/StorageVectorsClient.d.ts.map +1 -0
  113. package/dist/module/lib/vectors/StorageVectorsClient.js +360 -0
  114. package/dist/module/lib/vectors/StorageVectorsClient.js.map +1 -0
  115. package/dist/module/lib/vectors/VectorBucketApi.d.ts +129 -0
  116. package/dist/module/lib/vectors/VectorBucketApi.d.ts.map +1 -0
  117. package/dist/module/lib/vectors/VectorBucketApi.js +196 -0
  118. package/dist/module/lib/vectors/VectorBucketApi.js.map +1 -0
  119. package/dist/module/lib/vectors/VectorDataApi.d.ts +221 -0
  120. package/dist/module/lib/vectors/VectorDataApi.d.ts.map +1 -0
  121. package/dist/module/lib/vectors/VectorDataApi.js +333 -0
  122. package/dist/module/lib/vectors/VectorDataApi.js.map +1 -0
  123. package/dist/module/lib/vectors/VectorIndexApi.d.ts +157 -0
  124. package/dist/module/lib/vectors/VectorIndexApi.d.ts.map +1 -0
  125. package/dist/module/lib/vectors/VectorIndexApi.js +215 -0
  126. package/dist/module/lib/vectors/VectorIndexApi.js.map +1 -0
  127. package/dist/module/lib/vectors/constants.d.ts +5 -0
  128. package/dist/module/lib/vectors/constants.d.ts.map +1 -0
  129. package/dist/module/lib/vectors/constants.js +6 -0
  130. package/dist/module/lib/vectors/constants.js.map +1 -0
  131. package/dist/module/lib/vectors/errors.d.ts +55 -0
  132. package/dist/module/lib/vectors/errors.d.ts.map +1 -0
  133. package/dist/module/lib/vectors/errors.js +69 -0
  134. package/dist/module/lib/vectors/errors.js.map +1 -0
  135. package/dist/module/lib/vectors/fetch.d.ts +57 -0
  136. package/dist/module/lib/vectors/fetch.d.ts.map +1 -0
  137. package/dist/module/lib/vectors/fetch.js +161 -0
  138. package/dist/module/lib/vectors/fetch.js.map +1 -0
  139. package/dist/module/lib/vectors/helpers.d.ts +46 -0
  140. package/dist/module/lib/vectors/helpers.d.ts.map +1 -0
  141. package/dist/module/lib/vectors/helpers.js +66 -0
  142. package/dist/module/lib/vectors/helpers.js.map +1 -0
  143. package/dist/module/lib/vectors/index.d.ts +11 -0
  144. package/dist/module/lib/vectors/index.d.ts.map +1 -0
  145. package/dist/module/lib/vectors/index.js +11 -0
  146. package/dist/module/lib/vectors/index.js.map +1 -0
  147. package/dist/module/lib/vectors/types.d.ts +277 -0
  148. package/dist/module/lib/vectors/types.d.ts.map +1 -0
  149. package/dist/module/lib/vectors/types.js +2 -0
  150. package/dist/module/lib/vectors/types.js.map +1 -0
  151. package/dist/module/lib/version.d.ts +1 -1
  152. package/dist/module/lib/version.d.ts.map +1 -1
  153. package/dist/module/lib/version.js +1 -1
  154. package/dist/module/lib/version.js.map +1 -1
  155. package/dist/module/packages/BlobDownloadBuilder.d.ts +6 -1
  156. package/dist/module/packages/BlobDownloadBuilder.d.ts.map +1 -1
  157. package/dist/module/packages/BlobDownloadBuilder.js +20 -11
  158. package/dist/module/packages/BlobDownloadBuilder.js.map +1 -1
  159. package/dist/module/packages/StorageAnalyticsApi.d.ts +123 -0
  160. package/dist/module/packages/StorageAnalyticsApi.d.ts.map +1 -0
  161. package/dist/module/packages/StorageAnalyticsApi.js +161 -0
  162. package/dist/module/packages/StorageAnalyticsApi.js.map +1 -0
  163. package/dist/module/packages/StorageBucketApi.d.ts +3 -2
  164. package/dist/module/packages/StorageBucketApi.d.ts.map +1 -1
  165. package/dist/module/packages/StorageBucketApi.js +28 -12
  166. package/dist/module/packages/StorageBucketApi.js.map +1 -1
  167. package/dist/module/packages/StorageFileApi.d.ts.map +1 -1
  168. package/dist/module/packages/StorageFileApi.js +13 -11
  169. package/dist/module/packages/StorageFileApi.js.map +1 -1
  170. package/dist/module/packages/StreamDownloadBuilder.js +1 -9
  171. package/dist/module/packages/StreamDownloadBuilder.js.map +1 -1
  172. package/dist/tsconfig.module.tsbuildinfo +1 -0
  173. package/dist/tsconfig.tsbuildinfo +1 -0
  174. package/dist/umd/supabase.js +1 -1
  175. package/package.json +6 -13
  176. package/src/StorageClient.ts +37 -0
  177. package/src/index.ts +2 -0
  178. package/src/lib/constants.ts +3 -1
  179. package/src/lib/fetch.ts +5 -1
  180. package/src/lib/helpers.ts +3 -14
  181. package/src/lib/index.ts +1 -0
  182. package/src/lib/types.ts +83 -2
  183. package/src/lib/vectors/StorageVectorsClient.ts +405 -0
  184. package/src/lib/vectors/VectorBucketApi.ts +217 -0
  185. package/src/lib/vectors/VectorDataApi.ts +341 -0
  186. package/src/lib/vectors/VectorIndexApi.ts +245 -0
  187. package/src/lib/vectors/constants.ts +5 -0
  188. package/src/lib/vectors/errors.ts +78 -0
  189. package/src/lib/vectors/fetch.ts +218 -0
  190. package/src/lib/vectors/helpers.ts +79 -0
  191. package/src/lib/vectors/index.ts +66 -0
  192. package/src/lib/vectors/types.ts +299 -0
  193. package/src/lib/version.ts +1 -1
  194. package/src/packages/BlobDownloadBuilder.ts +22 -2
  195. package/src/packages/StorageAnalyticsApi.ts +199 -0
  196. package/src/packages/StorageBucketApi.ts +29 -4
  197. package/src/packages/StorageFileApi.ts +15 -2
@@ -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,5 @@
1
+ import { version } from '../version'
2
+ export const DEFAULT_HEADERS = {
3
+ 'X-Client-Info': `storage-js/${version}`,
4
+ 'Content-Type': 'application/json',
5
+ }
@@ -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
+ }
@@ -0,0 +1,218 @@
1
+ import { StorageVectorsApiError, StorageVectorsUnknownError } from './errors'
2
+ import { isPlainObject, resolveResponse } from './helpers'
3
+ import { VectorFetchParameters } from './types'
4
+
5
+ export type Fetch = typeof fetch
6
+
7
+ /**
8
+ * Options for fetch requests
9
+ * @property headers - Custom HTTP headers
10
+ * @property noResolveJson - If true, return raw Response instead of parsing JSON
11
+ */
12
+ export interface FetchOptions {
13
+ headers?: {
14
+ [key: string]: string
15
+ }
16
+ noResolveJson?: boolean
17
+ }
18
+
19
+ /**
20
+ * HTTP methods supported by the API
21
+ */
22
+ export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE'
23
+
24
+ /**
25
+ * Extracts error message from various error response formats
26
+ * @param err - Error object from API
27
+ * @returns Human-readable error message
28
+ */
29
+ const _getErrorMessage = (err: any): string =>
30
+ err.msg || err.message || err.error_description || err.error || JSON.stringify(err)
31
+
32
+ /**
33
+ * Handles fetch errors and converts them to StorageVectors error types
34
+ * @param error - The error caught from fetch
35
+ * @param reject - Promise rejection function
36
+ * @param options - Fetch options that may affect error handling
37
+ */
38
+ const handleError = async (
39
+ error: unknown,
40
+ reject: (reason?: any) => void,
41
+ options?: FetchOptions
42
+ ) => {
43
+ // Check if error is a Response-like object (has status and ok properties)
44
+ // This is more reliable than instanceof which can fail across realms
45
+ const isResponseLike =
46
+ error &&
47
+ typeof error === 'object' &&
48
+ 'status' in error &&
49
+ 'ok' in error &&
50
+ typeof (error as any).status === 'number'
51
+
52
+ if (isResponseLike && !options?.noResolveJson) {
53
+ const status = (error as any).status || 500
54
+ const responseError = error as any
55
+
56
+ // Try to parse JSON body if available
57
+ if (typeof responseError.json === 'function') {
58
+ responseError
59
+ .json()
60
+ .then((err: any) => {
61
+ const statusCode = err?.statusCode || err?.code || status + ''
62
+ reject(new StorageVectorsApiError(_getErrorMessage(err), status, statusCode))
63
+ })
64
+ .catch(() => {
65
+ // If JSON parsing fails, create an ApiError with the HTTP status code
66
+ const statusCode = status + ''
67
+ const message = responseError.statusText || `HTTP ${status} error`
68
+ reject(new StorageVectorsApiError(message, status, statusCode))
69
+ })
70
+ } else {
71
+ // No json() method available, create error from status
72
+ const statusCode = status + ''
73
+ const message = responseError.statusText || `HTTP ${status} error`
74
+ reject(new StorageVectorsApiError(message, status, statusCode))
75
+ }
76
+ } else {
77
+ reject(new StorageVectorsUnknownError(_getErrorMessage(error), error))
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Builds request parameters for fetch calls
83
+ * @param method - HTTP method
84
+ * @param options - Custom fetch options
85
+ * @param parameters - Additional fetch parameters like AbortSignal
86
+ * @param body - Request body (will be JSON stringified if plain object)
87
+ * @returns Complete fetch request parameters
88
+ */
89
+ const _getRequestParams = (
90
+ method: RequestMethodType,
91
+ options?: FetchOptions,
92
+ parameters?: VectorFetchParameters,
93
+ body?: object
94
+ ) => {
95
+ const params: { [k: string]: any } = { method, headers: options?.headers || {} }
96
+
97
+ if (method === 'GET' || !body) {
98
+ return params
99
+ }
100
+
101
+ if (isPlainObject(body)) {
102
+ params.headers = { 'Content-Type': 'application/json', ...options?.headers }
103
+ params.body = JSON.stringify(body)
104
+ } else {
105
+ params.body = body
106
+ }
107
+
108
+ return { ...params, ...parameters }
109
+ }
110
+
111
+ /**
112
+ * Internal request handler that wraps fetch with error handling
113
+ * @param fetcher - Fetch function to use
114
+ * @param method - HTTP method
115
+ * @param url - Request URL
116
+ * @param options - Custom fetch options
117
+ * @param parameters - Additional fetch parameters
118
+ * @param body - Request body
119
+ * @returns Promise with parsed response or error
120
+ */
121
+ async function _handleRequest(
122
+ fetcher: Fetch,
123
+ method: RequestMethodType,
124
+ url: string,
125
+ options?: FetchOptions,
126
+ parameters?: VectorFetchParameters,
127
+ body?: object
128
+ ): Promise<any> {
129
+ return new Promise((resolve, reject) => {
130
+ fetcher(url, _getRequestParams(method, options, parameters, body))
131
+ .then((result) => {
132
+ if (!result.ok) throw result
133
+ if (options?.noResolveJson) return result
134
+ // Handle empty responses (204, empty body)
135
+ const contentType = result.headers.get('content-type')
136
+ if (!contentType || !contentType.includes('application/json')) {
137
+ return {}
138
+ }
139
+ return result.json()
140
+ })
141
+ .then((data) => resolve(data))
142
+ .catch((error) => handleError(error, reject, options))
143
+ })
144
+ }
145
+
146
+ /**
147
+ * Performs a GET request
148
+ * @param fetcher - Fetch function to use
149
+ * @param url - Request URL
150
+ * @param options - Custom fetch options
151
+ * @param parameters - Additional fetch parameters
152
+ * @returns Promise with parsed response
153
+ */
154
+ export async function get(
155
+ fetcher: Fetch,
156
+ url: string,
157
+ options?: FetchOptions,
158
+ parameters?: VectorFetchParameters
159
+ ): Promise<any> {
160
+ return _handleRequest(fetcher, 'GET', url, options, parameters)
161
+ }
162
+
163
+ /**
164
+ * Performs a POST request
165
+ * @param fetcher - Fetch function to use
166
+ * @param url - Request URL
167
+ * @param body - Request body to be JSON stringified
168
+ * @param options - Custom fetch options
169
+ * @param parameters - Additional fetch parameters
170
+ * @returns Promise with parsed response
171
+ */
172
+ export async function post(
173
+ fetcher: Fetch,
174
+ url: string,
175
+ body: object,
176
+ options?: FetchOptions,
177
+ parameters?: VectorFetchParameters
178
+ ): Promise<any> {
179
+ return _handleRequest(fetcher, 'POST', url, options, parameters, body)
180
+ }
181
+
182
+ /**
183
+ * Performs a PUT request
184
+ * @param fetcher - Fetch function to use
185
+ * @param url - Request URL
186
+ * @param body - Request body to be JSON stringified
187
+ * @param options - Custom fetch options
188
+ * @param parameters - Additional fetch parameters
189
+ * @returns Promise with parsed response
190
+ */
191
+ export async function put(
192
+ fetcher: Fetch,
193
+ url: string,
194
+ body: object,
195
+ options?: FetchOptions,
196
+ parameters?: VectorFetchParameters
197
+ ): Promise<any> {
198
+ return _handleRequest(fetcher, 'PUT', url, options, parameters, body)
199
+ }
200
+
201
+ /**
202
+ * Performs a DELETE request
203
+ * @param fetcher - Fetch function to use
204
+ * @param url - Request URL
205
+ * @param body - Request body to be JSON stringified
206
+ * @param options - Custom fetch options
207
+ * @param parameters - Additional fetch parameters
208
+ * @returns Promise with parsed response
209
+ */
210
+ export async function remove(
211
+ fetcher: Fetch,
212
+ url: string,
213
+ body: object,
214
+ options?: FetchOptions,
215
+ parameters?: VectorFetchParameters
216
+ ): Promise<any> {
217
+ return _handleRequest(fetcher, 'DELETE', url, options, parameters, body)
218
+ }
@@ -0,0 +1,79 @@
1
+ type Fetch = typeof fetch
2
+
3
+ /**
4
+ * Resolves the fetch implementation to use
5
+ * Uses custom fetch if provided, otherwise uses native fetch
6
+ *
7
+ * @param customFetch - Optional custom fetch implementation
8
+ * @returns Resolved fetch function
9
+ */
10
+ export const resolveFetch = (customFetch?: Fetch): Fetch => {
11
+ if (customFetch) {
12
+ return (...args) => customFetch(...args)
13
+ }
14
+ return (...args) => fetch(...args)
15
+ }
16
+
17
+ /**
18
+ * Resolves the Response constructor to use
19
+ * Returns native Response constructor
20
+ *
21
+ * @returns Response constructor
22
+ */
23
+ export const resolveResponse = (): typeof Response => {
24
+ return Response
25
+ }
26
+
27
+ /**
28
+ * Determine if input is a plain object
29
+ * An object is plain if it's created by either {}, new Object(), or Object.create(null)
30
+ *
31
+ * @param value - Value to check
32
+ * @returns True if value is a plain object
33
+ * @source https://github.com/sindresorhus/is-plain-obj
34
+ */
35
+ export const isPlainObject = (value: object): boolean => {
36
+ if (typeof value !== 'object' || value === null) {
37
+ return false
38
+ }
39
+
40
+ const prototype = Object.getPrototypeOf(value)
41
+ return (
42
+ (prototype === null ||
43
+ prototype === Object.prototype ||
44
+ Object.getPrototypeOf(prototype) === null) &&
45
+ !(Symbol.toStringTag in value) &&
46
+ !(Symbol.iterator in value)
47
+ )
48
+ }
49
+
50
+ /**
51
+ * Normalizes a number array to float32 format
52
+ * Ensures all vector values are valid 32-bit floats
53
+ *
54
+ * @param values - Array of numbers to normalize
55
+ * @returns Normalized float32 array
56
+ */
57
+ export const normalizeToFloat32 = (values: number[]): number[] => {
58
+ // Use Float32Array to ensure proper precision
59
+ return Array.from(new Float32Array(values))
60
+ }
61
+
62
+ /**
63
+ * Validates vector dimensions match expected dimension
64
+ * Throws error if dimensions don't match
65
+ *
66
+ * @param vector - Vector data to validate
67
+ * @param expectedDimension - Expected vector dimension
68
+ * @throws Error if dimensions don't match
69
+ */
70
+ export const validateVectorDimension = (
71
+ vector: { float32: number[] },
72
+ expectedDimension?: number
73
+ ): void => {
74
+ if (expectedDimension !== undefined && vector.float32.length !== expectedDimension) {
75
+ throw new Error(
76
+ `Vector dimension mismatch: expected ${expectedDimension}, got ${vector.float32.length}`
77
+ )
78
+ }
79
+ }
@@ -0,0 +1,66 @@
1
+ // Main client
2
+ export { StorageVectorsClient, VectorBucketScope, VectorIndexScope } from './StorageVectorsClient'
3
+ export type { StorageVectorsClientOptions } from './StorageVectorsClient'
4
+
5
+ // API classes (for advanced usage)
6
+ export { default as VectorBucketApi } from './VectorBucketApi'
7
+ export { default as VectorIndexApi } from './VectorIndexApi'
8
+ export { default as VectorDataApi } from './VectorDataApi'
9
+ export type { CreateIndexOptions } from './VectorIndexApi'
10
+
11
+ // Types
12
+ export type {
13
+ // Core types
14
+ VectorBucket,
15
+ VectorIndex,
16
+ VectorData,
17
+ VectorMetadata,
18
+ VectorObject,
19
+ VectorMatch,
20
+ EncryptionConfiguration,
21
+ MetadataConfiguration,
22
+ VectorDataType,
23
+ DistanceMetric,
24
+ VectorFilter,
25
+
26
+ // Request/Response types
27
+ ListVectorBucketsOptions,
28
+ ListVectorBucketsResponse,
29
+ ListIndexesOptions,
30
+ ListIndexesResponse,
31
+ PutVectorsOptions,
32
+ GetVectorsOptions,
33
+ GetVectorsResponse,
34
+ DeleteVectorsOptions,
35
+ ListVectorsOptions,
36
+ ListVectorsResponse,
37
+ QueryVectorsOptions,
38
+ QueryVectorsResponse,
39
+
40
+ // Response wrappers
41
+ ApiResponse,
42
+ SuccessResponse,
43
+ ErrorResponse,
44
+ VectorFetchParameters,
45
+ } from './types'
46
+
47
+ // Errors
48
+ export {
49
+ StorageVectorsError,
50
+ StorageVectorsApiError,
51
+ StorageVectorsUnknownError,
52
+ StorageVectorsErrorCode,
53
+ isStorageVectorsError,
54
+ } from './errors'
55
+
56
+ // Fetch utilities (for custom implementations)
57
+ export type { Fetch, FetchOptions, RequestMethodType } from './fetch'
58
+
59
+ // Helper utilities
60
+ export {
61
+ resolveFetch,
62
+ resolveResponse,
63
+ isPlainObject,
64
+ normalizeToFloat32,
65
+ validateVectorDimension,
66
+ } from './helpers'