@supabase/storage-js 2.91.1 → 2.91.2
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/dist/index.cjs +476 -865
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1441 -1491
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1441 -1491
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +477 -861
- package/dist/index.mjs.map +1 -1
- package/dist/umd/supabase.js +1 -1
- package/package.json +1 -1
- package/src/StorageClient.ts +2 -2
- package/src/index.ts +15 -2
- package/src/lib/common/BaseApiClient.ts +90 -0
- package/src/lib/common/errors.ts +144 -0
- package/src/lib/common/fetch.ts +286 -0
- package/src/lib/{helpers.ts → common/helpers.ts} +71 -17
- package/src/lib/types.ts +304 -1
- package/src/lib/version.ts +1 -1
- package/src/packages/BlobDownloadBuilder.ts +1 -1
- package/src/packages/StorageAnalyticsClient.ts +17 -68
- package/src/packages/StorageBucketApi.ts +25 -109
- package/src/packages/StorageFileApi.ts +44 -172
- package/src/{lib/vectors → packages}/StorageVectorsClient.ts +2 -2
- package/src/packages/StreamDownloadBuilder.ts +1 -1
- package/src/packages/VectorBucketApi.ts +73 -0
- package/src/packages/VectorDataApi.ts +98 -0
- package/src/{lib/vectors → packages}/VectorIndexApi.ts +21 -66
- package/src/lib/errors.ts +0 -43
- package/src/lib/fetch.ts +0 -148
- package/src/lib/index.ts +0 -5
- package/src/lib/vectors/VectorBucketApi.ts +0 -118
- package/src/lib/vectors/VectorDataApi.ts +0 -152
- package/src/lib/vectors/constants.ts +0 -5
- package/src/lib/vectors/errors.ts +0 -78
- package/src/lib/vectors/fetch.ts +0 -218
- package/src/lib/vectors/helpers.ts +0 -79
- package/src/lib/vectors/index.ts +0 -66
- package/src/lib/vectors/types.ts +0 -301
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { DEFAULT_HEADERS } from '
|
|
2
|
-
import {
|
|
3
|
-
import { Fetch, post } from '
|
|
4
|
-
import
|
|
1
|
+
import { DEFAULT_HEADERS } from '../lib/constants'
|
|
2
|
+
import { StorageError } from '../lib/common/errors'
|
|
3
|
+
import { Fetch, post } from '../lib/common/fetch'
|
|
4
|
+
import BaseApiClient from '../lib/common/BaseApiClient'
|
|
5
5
|
import {
|
|
6
6
|
ApiResponse,
|
|
7
7
|
VectorIndex,
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
VectorDataType,
|
|
11
11
|
DistanceMetric,
|
|
12
12
|
MetadataConfiguration,
|
|
13
|
-
} from '
|
|
13
|
+
} from '../lib/types'
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* @alpha
|
|
@@ -33,41 +33,22 @@ export interface CreateIndexOptions {
|
|
|
33
33
|
* Base implementation for vector index operations.
|
|
34
34
|
* Use {@link VectorBucketScope} via `supabase.storage.vectors.from('bucket')` instead.
|
|
35
35
|
*/
|
|
36
|
-
export default class VectorIndexApi {
|
|
37
|
-
protected url: string
|
|
38
|
-
protected headers: { [key: string]: string }
|
|
39
|
-
protected fetch: Fetch
|
|
40
|
-
protected shouldThrowOnError = false
|
|
41
|
-
|
|
36
|
+
export default class VectorIndexApi extends BaseApiClient<StorageError> {
|
|
42
37
|
/** Creates a new VectorIndexApi instance */
|
|
43
38
|
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Enable throwing errors instead of returning them in the response */
|
|
50
|
-
public throwOnError(): this {
|
|
51
|
-
this.shouldThrowOnError = true
|
|
52
|
-
return this
|
|
39
|
+
const finalUrl = url.replace(/\/$/, '')
|
|
40
|
+
const finalHeaders = { ...DEFAULT_HEADERS, 'Content-Type': 'application/json', ...headers }
|
|
41
|
+
super(finalUrl, finalHeaders, fetch, 'vectors')
|
|
53
42
|
}
|
|
54
43
|
|
|
55
44
|
/** Creates a new vector index within a bucket */
|
|
56
45
|
async createIndex(options: CreateIndexOptions): Promise<ApiResponse<undefined>> {
|
|
57
|
-
|
|
46
|
+
return this.handleOperation(async () => {
|
|
58
47
|
const data = await post(this.fetch, `${this.url}/CreateIndex`, options, {
|
|
59
48
|
headers: this.headers,
|
|
60
49
|
})
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
if (this.shouldThrowOnError) {
|
|
64
|
-
throw error
|
|
65
|
-
}
|
|
66
|
-
if (isStorageVectorsError(error)) {
|
|
67
|
-
return { data: null, error }
|
|
68
|
-
}
|
|
69
|
-
throw error
|
|
70
|
-
}
|
|
50
|
+
return data || {}
|
|
51
|
+
})
|
|
71
52
|
}
|
|
72
53
|
|
|
73
54
|
/** Retrieves metadata for a specific vector index */
|
|
@@ -75,61 +56,35 @@ export default class VectorIndexApi {
|
|
|
75
56
|
vectorBucketName: string,
|
|
76
57
|
indexName: string
|
|
77
58
|
): Promise<ApiResponse<{ index: VectorIndex }>> {
|
|
78
|
-
|
|
79
|
-
|
|
59
|
+
return this.handleOperation(async () => {
|
|
60
|
+
return await post(
|
|
80
61
|
this.fetch,
|
|
81
62
|
`${this.url}/GetIndex`,
|
|
82
63
|
{ vectorBucketName, indexName },
|
|
83
64
|
{ headers: this.headers }
|
|
84
65
|
)
|
|
85
|
-
|
|
86
|
-
} catch (error) {
|
|
87
|
-
if (this.shouldThrowOnError) {
|
|
88
|
-
throw error
|
|
89
|
-
}
|
|
90
|
-
if (isStorageVectorsError(error)) {
|
|
91
|
-
return { data: null, error }
|
|
92
|
-
}
|
|
93
|
-
throw error
|
|
94
|
-
}
|
|
66
|
+
})
|
|
95
67
|
}
|
|
96
68
|
|
|
97
69
|
/** Lists vector indexes within a bucket with optional filtering and pagination */
|
|
98
70
|
async listIndexes(options: ListIndexesOptions): Promise<ApiResponse<ListIndexesResponse>> {
|
|
99
|
-
|
|
100
|
-
|
|
71
|
+
return this.handleOperation(async () => {
|
|
72
|
+
return await post(this.fetch, `${this.url}/ListIndexes`, options, {
|
|
101
73
|
headers: this.headers,
|
|
102
74
|
})
|
|
103
|
-
|
|
104
|
-
} catch (error) {
|
|
105
|
-
if (this.shouldThrowOnError) {
|
|
106
|
-
throw error
|
|
107
|
-
}
|
|
108
|
-
if (isStorageVectorsError(error)) {
|
|
109
|
-
return { data: null, error }
|
|
110
|
-
}
|
|
111
|
-
throw error
|
|
112
|
-
}
|
|
75
|
+
})
|
|
113
76
|
}
|
|
114
77
|
|
|
115
78
|
/** Deletes a vector index and all its data */
|
|
116
79
|
async deleteIndex(vectorBucketName: string, indexName: string): Promise<ApiResponse<undefined>> {
|
|
117
|
-
|
|
80
|
+
return this.handleOperation(async () => {
|
|
118
81
|
const data = await post(
|
|
119
82
|
this.fetch,
|
|
120
83
|
`${this.url}/DeleteIndex`,
|
|
121
84
|
{ vectorBucketName, indexName },
|
|
122
85
|
{ headers: this.headers }
|
|
123
86
|
)
|
|
124
|
-
return
|
|
125
|
-
}
|
|
126
|
-
if (this.shouldThrowOnError) {
|
|
127
|
-
throw error
|
|
128
|
-
}
|
|
129
|
-
if (isStorageVectorsError(error)) {
|
|
130
|
-
return { data: null, error }
|
|
131
|
-
}
|
|
132
|
-
throw error
|
|
133
|
-
}
|
|
87
|
+
return data || {}
|
|
88
|
+
})
|
|
134
89
|
}
|
|
135
90
|
}
|
package/src/lib/errors.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
export class StorageError extends Error {
|
|
2
|
-
protected __isStorageError = true
|
|
3
|
-
|
|
4
|
-
constructor(message: string) {
|
|
5
|
-
super(message)
|
|
6
|
-
this.name = 'StorageError'
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function isStorageError(error: unknown): error is StorageError {
|
|
11
|
-
return typeof error === 'object' && error !== null && '__isStorageError' in error
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class StorageApiError extends StorageError {
|
|
15
|
-
status: number
|
|
16
|
-
statusCode: string
|
|
17
|
-
|
|
18
|
-
constructor(message: string, status: number, statusCode: string) {
|
|
19
|
-
super(message)
|
|
20
|
-
this.name = 'StorageApiError'
|
|
21
|
-
this.status = status
|
|
22
|
-
this.statusCode = statusCode
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
toJSON() {
|
|
26
|
-
return {
|
|
27
|
-
name: this.name,
|
|
28
|
-
message: this.message,
|
|
29
|
-
status: this.status,
|
|
30
|
-
statusCode: this.statusCode,
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export class StorageUnknownError extends StorageError {
|
|
36
|
-
originalError: unknown
|
|
37
|
-
|
|
38
|
-
constructor(message: string, originalError: unknown) {
|
|
39
|
-
super(message)
|
|
40
|
-
this.name = 'StorageUnknownError'
|
|
41
|
-
this.originalError = originalError
|
|
42
|
-
}
|
|
43
|
-
}
|
package/src/lib/fetch.ts
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import { StorageApiError, StorageUnknownError } from './errors'
|
|
2
|
-
import { isPlainObject, resolveResponse } from './helpers'
|
|
3
|
-
import { FetchParameters } from './types'
|
|
4
|
-
|
|
5
|
-
export type Fetch = typeof fetch
|
|
6
|
-
|
|
7
|
-
export interface FetchOptions {
|
|
8
|
-
headers?: {
|
|
9
|
-
[key: string]: string
|
|
10
|
-
}
|
|
11
|
-
duplex?: string
|
|
12
|
-
noResolveJson?: boolean
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD'
|
|
16
|
-
|
|
17
|
-
const _getErrorMessage = (err: any): string =>
|
|
18
|
-
err.msg ||
|
|
19
|
-
err.message ||
|
|
20
|
-
err.error_description ||
|
|
21
|
-
(typeof err.error === 'string' ? err.error : err.error?.message) ||
|
|
22
|
-
JSON.stringify(err)
|
|
23
|
-
|
|
24
|
-
const handleError = async (
|
|
25
|
-
error: unknown,
|
|
26
|
-
reject: (reason?: any) => void,
|
|
27
|
-
options?: FetchOptions
|
|
28
|
-
) => {
|
|
29
|
-
const Res = await resolveResponse()
|
|
30
|
-
|
|
31
|
-
if (error instanceof Res && !options?.noResolveJson) {
|
|
32
|
-
error
|
|
33
|
-
.json()
|
|
34
|
-
.then((err) => {
|
|
35
|
-
const status = error.status || 500
|
|
36
|
-
const statusCode = err?.statusCode || status + ''
|
|
37
|
-
reject(new StorageApiError(_getErrorMessage(err), status, statusCode))
|
|
38
|
-
})
|
|
39
|
-
.catch((err) => {
|
|
40
|
-
reject(new StorageUnknownError(_getErrorMessage(err), err))
|
|
41
|
-
})
|
|
42
|
-
} else {
|
|
43
|
-
reject(new StorageUnknownError(_getErrorMessage(error), error))
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const _getRequestParams = (
|
|
48
|
-
method: RequestMethodType,
|
|
49
|
-
options?: FetchOptions,
|
|
50
|
-
parameters?: FetchParameters,
|
|
51
|
-
body?: object
|
|
52
|
-
) => {
|
|
53
|
-
const params: { [k: string]: any } = { method, headers: options?.headers || {} }
|
|
54
|
-
|
|
55
|
-
if (method === 'GET' || !body) {
|
|
56
|
-
return params
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (isPlainObject(body)) {
|
|
60
|
-
params.headers = { 'Content-Type': 'application/json', ...options?.headers }
|
|
61
|
-
params.body = JSON.stringify(body)
|
|
62
|
-
} else {
|
|
63
|
-
params.body = body
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (options?.duplex) {
|
|
67
|
-
params.duplex = options.duplex
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return { ...params, ...parameters }
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function _handleRequest(
|
|
74
|
-
fetcher: Fetch,
|
|
75
|
-
method: RequestMethodType,
|
|
76
|
-
url: string,
|
|
77
|
-
options?: FetchOptions,
|
|
78
|
-
parameters?: FetchParameters,
|
|
79
|
-
body?: object
|
|
80
|
-
): Promise<any> {
|
|
81
|
-
return new Promise((resolve, reject) => {
|
|
82
|
-
fetcher(url, _getRequestParams(method, options, parameters, body))
|
|
83
|
-
.then((result) => {
|
|
84
|
-
if (!result.ok) throw result
|
|
85
|
-
if (options?.noResolveJson) return result
|
|
86
|
-
return result.json()
|
|
87
|
-
})
|
|
88
|
-
.then((data) => resolve(data))
|
|
89
|
-
.catch((error) => handleError(error, reject, options))
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export async function get(
|
|
94
|
-
fetcher: Fetch,
|
|
95
|
-
url: string,
|
|
96
|
-
options?: FetchOptions,
|
|
97
|
-
parameters?: FetchParameters
|
|
98
|
-
): Promise<any> {
|
|
99
|
-
return _handleRequest(fetcher, 'GET', url, options, parameters)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export async function post(
|
|
103
|
-
fetcher: Fetch,
|
|
104
|
-
url: string,
|
|
105
|
-
body: object,
|
|
106
|
-
options?: FetchOptions,
|
|
107
|
-
parameters?: FetchParameters
|
|
108
|
-
): Promise<any> {
|
|
109
|
-
return _handleRequest(fetcher, 'POST', url, options, parameters, body)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export async function put(
|
|
113
|
-
fetcher: Fetch,
|
|
114
|
-
url: string,
|
|
115
|
-
body: object,
|
|
116
|
-
options?: FetchOptions,
|
|
117
|
-
parameters?: FetchParameters
|
|
118
|
-
): Promise<any> {
|
|
119
|
-
return _handleRequest(fetcher, 'PUT', url, options, parameters, body)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export async function head(
|
|
123
|
-
fetcher: Fetch,
|
|
124
|
-
url: string,
|
|
125
|
-
options?: FetchOptions,
|
|
126
|
-
parameters?: FetchParameters
|
|
127
|
-
): Promise<any> {
|
|
128
|
-
return _handleRequest(
|
|
129
|
-
fetcher,
|
|
130
|
-
'HEAD',
|
|
131
|
-
url,
|
|
132
|
-
{
|
|
133
|
-
...options,
|
|
134
|
-
noResolveJson: true,
|
|
135
|
-
},
|
|
136
|
-
parameters
|
|
137
|
-
)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export async function remove(
|
|
141
|
-
fetcher: Fetch,
|
|
142
|
-
url: string,
|
|
143
|
-
body: object,
|
|
144
|
-
options?: FetchOptions,
|
|
145
|
-
parameters?: FetchParameters
|
|
146
|
-
): Promise<any> {
|
|
147
|
-
return _handleRequest(fetcher, 'DELETE', url, options, parameters, body)
|
|
148
|
-
}
|
package/src/lib/index.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
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
|
-
VectorBucket,
|
|
8
|
-
ListVectorBucketsOptions,
|
|
9
|
-
ListVectorBucketsResponse,
|
|
10
|
-
} from './types'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @hidden
|
|
14
|
-
* Base implementation for vector bucket operations.
|
|
15
|
-
* Use {@link StorageVectorsClient} via `supabase.storage.vectors` instead.
|
|
16
|
-
*/
|
|
17
|
-
export default class VectorBucketApi {
|
|
18
|
-
protected url: string
|
|
19
|
-
protected headers: { [key: string]: string }
|
|
20
|
-
protected fetch: Fetch
|
|
21
|
-
protected shouldThrowOnError = false
|
|
22
|
-
|
|
23
|
-
/** Creates a new VectorBucketApi instance */
|
|
24
|
-
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
|
|
25
|
-
this.url = url.replace(/\/$/, '')
|
|
26
|
-
this.headers = { ...DEFAULT_HEADERS, ...headers }
|
|
27
|
-
this.fetch = resolveFetch(fetch)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Enable throwing errors instead of returning them in the response */
|
|
31
|
-
public throwOnError(): this {
|
|
32
|
-
this.shouldThrowOnError = true
|
|
33
|
-
return this
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Creates a new vector bucket */
|
|
37
|
-
async createBucket(vectorBucketName: string): Promise<ApiResponse<undefined>> {
|
|
38
|
-
try {
|
|
39
|
-
const data = await post(
|
|
40
|
-
this.fetch,
|
|
41
|
-
`${this.url}/CreateVectorBucket`,
|
|
42
|
-
{ vectorBucketName },
|
|
43
|
-
{ headers: this.headers }
|
|
44
|
-
)
|
|
45
|
-
return { data: data || {}, error: null }
|
|
46
|
-
} catch (error) {
|
|
47
|
-
if (this.shouldThrowOnError) {
|
|
48
|
-
throw error
|
|
49
|
-
}
|
|
50
|
-
if (isStorageVectorsError(error)) {
|
|
51
|
-
return { data: null, error }
|
|
52
|
-
}
|
|
53
|
-
throw error
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Retrieves metadata for a specific vector bucket */
|
|
58
|
-
async getBucket(vectorBucketName: string): Promise<ApiResponse<{ vectorBucket: VectorBucket }>> {
|
|
59
|
-
try {
|
|
60
|
-
const data = await post(
|
|
61
|
-
this.fetch,
|
|
62
|
-
`${this.url}/GetVectorBucket`,
|
|
63
|
-
{ vectorBucketName },
|
|
64
|
-
{ headers: this.headers }
|
|
65
|
-
)
|
|
66
|
-
return { data, error: null }
|
|
67
|
-
} catch (error) {
|
|
68
|
-
if (this.shouldThrowOnError) {
|
|
69
|
-
throw error
|
|
70
|
-
}
|
|
71
|
-
if (isStorageVectorsError(error)) {
|
|
72
|
-
return { data: null, error }
|
|
73
|
-
}
|
|
74
|
-
throw error
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Lists vector buckets with optional filtering and pagination */
|
|
79
|
-
async listBuckets(
|
|
80
|
-
options: ListVectorBucketsOptions = {}
|
|
81
|
-
): Promise<ApiResponse<ListVectorBucketsResponse>> {
|
|
82
|
-
try {
|
|
83
|
-
const data = await post(this.fetch, `${this.url}/ListVectorBuckets`, options, {
|
|
84
|
-
headers: this.headers,
|
|
85
|
-
})
|
|
86
|
-
return { data, error: null }
|
|
87
|
-
} catch (error) {
|
|
88
|
-
if (this.shouldThrowOnError) {
|
|
89
|
-
throw error
|
|
90
|
-
}
|
|
91
|
-
if (isStorageVectorsError(error)) {
|
|
92
|
-
return { data: null, error }
|
|
93
|
-
}
|
|
94
|
-
throw error
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Deletes a vector bucket (must be empty first) */
|
|
99
|
-
async deleteBucket(vectorBucketName: string): Promise<ApiResponse<undefined>> {
|
|
100
|
-
try {
|
|
101
|
-
const data = await post(
|
|
102
|
-
this.fetch,
|
|
103
|
-
`${this.url}/DeleteVectorBucket`,
|
|
104
|
-
{ vectorBucketName },
|
|
105
|
-
{ headers: this.headers }
|
|
106
|
-
)
|
|
107
|
-
return { data: data || {}, error: null }
|
|
108
|
-
} catch (error) {
|
|
109
|
-
if (this.shouldThrowOnError) {
|
|
110
|
-
throw error
|
|
111
|
-
}
|
|
112
|
-
if (isStorageVectorsError(error)) {
|
|
113
|
-
return { data: null, error }
|
|
114
|
-
}
|
|
115
|
-
throw error
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
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
|
-
* @hidden
|
|
19
|
-
* Base implementation for vector data operations.
|
|
20
|
-
* Use {@link VectorIndexScope} via `supabase.storage.vectors.from('bucket').index('idx')` instead.
|
|
21
|
-
*/
|
|
22
|
-
export default class VectorDataApi {
|
|
23
|
-
protected url: string
|
|
24
|
-
protected headers: { [key: string]: string }
|
|
25
|
-
protected fetch: Fetch
|
|
26
|
-
protected shouldThrowOnError = false
|
|
27
|
-
|
|
28
|
-
/** Creates a new VectorDataApi instance */
|
|
29
|
-
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
|
|
30
|
-
this.url = url.replace(/\/$/, '')
|
|
31
|
-
this.headers = { ...DEFAULT_HEADERS, ...headers }
|
|
32
|
-
this.fetch = resolveFetch(fetch)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Enable throwing errors instead of returning them in the response */
|
|
36
|
-
public throwOnError(): this {
|
|
37
|
-
this.shouldThrowOnError = true
|
|
38
|
-
return this
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Inserts or updates vectors in batch (1-500 per request) */
|
|
42
|
-
async putVectors(options: PutVectorsOptions): Promise<ApiResponse<undefined>> {
|
|
43
|
-
try {
|
|
44
|
-
// Validate batch size
|
|
45
|
-
if (options.vectors.length < 1 || options.vectors.length > 500) {
|
|
46
|
-
throw new Error('Vector batch size must be between 1 and 500 items')
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const data = await post(this.fetch, `${this.url}/PutVectors`, options, {
|
|
50
|
-
headers: this.headers,
|
|
51
|
-
})
|
|
52
|
-
return { data: data || {}, error: null }
|
|
53
|
-
} catch (error) {
|
|
54
|
-
if (this.shouldThrowOnError) {
|
|
55
|
-
throw error
|
|
56
|
-
}
|
|
57
|
-
if (isStorageVectorsError(error)) {
|
|
58
|
-
return { data: null, error }
|
|
59
|
-
}
|
|
60
|
-
throw error
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Retrieves vectors by their keys in batch */
|
|
65
|
-
async getVectors(options: GetVectorsOptions): Promise<ApiResponse<GetVectorsResponse>> {
|
|
66
|
-
try {
|
|
67
|
-
const data = await post(this.fetch, `${this.url}/GetVectors`, options, {
|
|
68
|
-
headers: this.headers,
|
|
69
|
-
})
|
|
70
|
-
return { data, error: null }
|
|
71
|
-
} catch (error) {
|
|
72
|
-
if (this.shouldThrowOnError) {
|
|
73
|
-
throw error
|
|
74
|
-
}
|
|
75
|
-
if (isStorageVectorsError(error)) {
|
|
76
|
-
return { data: null, error }
|
|
77
|
-
}
|
|
78
|
-
throw error
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Lists vectors in an index with pagination */
|
|
83
|
-
async listVectors(options: ListVectorsOptions): Promise<ApiResponse<ListVectorsResponse>> {
|
|
84
|
-
try {
|
|
85
|
-
// Validate segment configuration
|
|
86
|
-
if (options.segmentCount !== undefined) {
|
|
87
|
-
if (options.segmentCount < 1 || options.segmentCount > 16) {
|
|
88
|
-
throw new Error('segmentCount must be between 1 and 16')
|
|
89
|
-
}
|
|
90
|
-
if (options.segmentIndex !== undefined) {
|
|
91
|
-
if (options.segmentIndex < 0 || options.segmentIndex >= options.segmentCount) {
|
|
92
|
-
throw new Error(`segmentIndex must be between 0 and ${options.segmentCount - 1}`)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const data = await post(this.fetch, `${this.url}/ListVectors`, options, {
|
|
98
|
-
headers: this.headers,
|
|
99
|
-
})
|
|
100
|
-
return { data, error: null }
|
|
101
|
-
} catch (error) {
|
|
102
|
-
if (this.shouldThrowOnError) {
|
|
103
|
-
throw error
|
|
104
|
-
}
|
|
105
|
-
if (isStorageVectorsError(error)) {
|
|
106
|
-
return { data: null, error }
|
|
107
|
-
}
|
|
108
|
-
throw error
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Queries for similar vectors using approximate nearest neighbor search */
|
|
113
|
-
async queryVectors(options: QueryVectorsOptions): Promise<ApiResponse<QueryVectorsResponse>> {
|
|
114
|
-
try {
|
|
115
|
-
const data = await post(this.fetch, `${this.url}/QueryVectors`, options, {
|
|
116
|
-
headers: this.headers,
|
|
117
|
-
})
|
|
118
|
-
return { data, error: null }
|
|
119
|
-
} catch (error) {
|
|
120
|
-
if (this.shouldThrowOnError) {
|
|
121
|
-
throw error
|
|
122
|
-
}
|
|
123
|
-
if (isStorageVectorsError(error)) {
|
|
124
|
-
return { data: null, error }
|
|
125
|
-
}
|
|
126
|
-
throw error
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** Deletes vectors by their keys in batch (1-500 per request) */
|
|
131
|
-
async deleteVectors(options: DeleteVectorsOptions): Promise<ApiResponse<undefined>> {
|
|
132
|
-
try {
|
|
133
|
-
// Validate batch size
|
|
134
|
-
if (options.keys.length < 1 || options.keys.length > 500) {
|
|
135
|
-
throw new Error('Keys batch size must be between 1 and 500 items')
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const data = await post(this.fetch, `${this.url}/DeleteVectors`, options, {
|
|
139
|
-
headers: this.headers,
|
|
140
|
-
})
|
|
141
|
-
return { data: data || {}, error: null }
|
|
142
|
-
} catch (error) {
|
|
143
|
-
if (this.shouldThrowOnError) {
|
|
144
|
-
throw error
|
|
145
|
-
}
|
|
146
|
-
if (isStorageVectorsError(error)) {
|
|
147
|
-
return { data: null, error }
|
|
148
|
-
}
|
|
149
|
-
throw error
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
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
|
-
}
|