main repo

This commit is contained in:
Basilosaurusrex
2025-11-24 18:09:40 +01:00
parent b636ee5e70
commit f027651f9b
34146 changed files with 4436636 additions and 0 deletions

18
node_modules/@supabase/storage-js/src/StorageClient.ts generated vendored Normal file
View File

@@ -0,0 +1,18 @@
import StorageFileApi from './packages/StorageFileApi'
import StorageBucketApi from './packages/StorageBucketApi'
import { Fetch } from './lib/fetch'
export class StorageClient extends StorageBucketApi {
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
super(url, headers, fetch)
}
/**
* Perform file operation in a bucket.
*
* @param id The bucket id to operate on.
*/
from(id: string): StorageFileApi {
return new StorageFileApi(this.url, this.headers, id, this.fetch)
}
}

3
node_modules/@supabase/storage-js/src/index.ts generated vendored Normal file
View File

@@ -0,0 +1,3 @@
export { StorageClient as StorageClient } from './StorageClient'
export * from './lib/types'
export * from './lib/errors'

View File

@@ -0,0 +1,2 @@
import { version } from './version'
export const DEFAULT_HEADERS = { 'X-Client-Info': `storage-js/${version}` }

40
node_modules/@supabase/storage-js/src/lib/errors.ts generated vendored Normal file
View File

@@ -0,0 +1,40 @@
export class StorageError extends Error {
protected __isStorageError = true
constructor(message: string) {
super(message)
this.name = 'StorageError'
}
}
export function isStorageError(error: unknown): error is StorageError {
return typeof error === 'object' && error !== null && '__isStorageError' in error
}
export class StorageApiError extends StorageError {
status: number
constructor(message: string, status: number) {
super(message)
this.name = 'StorageApiError'
this.status = status
}
toJSON() {
return {
name: this.name,
message: this.message,
status: this.status,
}
}
}
export class StorageUnknownError extends StorageError {
originalError: unknown
constructor(message: string, originalError: unknown) {
super(message)
this.name = 'StorageUnknownError'
this.originalError = originalError
}
}

135
node_modules/@supabase/storage-js/src/lib/fetch.ts generated vendored Normal file
View File

@@ -0,0 +1,135 @@
import { StorageApiError, StorageUnknownError } from './errors'
import { resolveResponse } from './helpers'
import { FetchParameters } from './types'
export type Fetch = typeof fetch
export interface FetchOptions {
headers?: {
[key: string]: string
}
noResolveJson?: boolean
}
export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD'
const _getErrorMessage = (err: any): string =>
err.msg || err.message || err.error_description || err.error || JSON.stringify(err)
const handleError = async (
error: unknown,
reject: (reason?: any) => void,
options?: FetchOptions
) => {
const Res = await resolveResponse()
if (error instanceof Res && !options?.noResolveJson) {
error
.json()
.then((err) => {
reject(new StorageApiError(_getErrorMessage(err), error.status || 500))
})
.catch((err) => {
reject(new StorageUnknownError(_getErrorMessage(err), err))
})
} else {
reject(new StorageUnknownError(_getErrorMessage(error), error))
}
}
const _getRequestParams = (
method: RequestMethodType,
options?: FetchOptions,
parameters?: FetchParameters,
body?: object
) => {
const params: { [k: string]: any } = { method, headers: options?.headers || {} }
if (method === 'GET') {
return params
}
params.headers = { 'Content-Type': 'application/json', ...options?.headers }
if (body) {
params.body = JSON.stringify(body)
}
return { ...params, ...parameters }
}
async function _handleRequest(
fetcher: Fetch,
method: RequestMethodType,
url: string,
options?: FetchOptions,
parameters?: FetchParameters,
body?: object
): Promise<any> {
return new Promise((resolve, reject) => {
fetcher(url, _getRequestParams(method, options, parameters, body))
.then((result) => {
if (!result.ok) throw result
if (options?.noResolveJson) return result
return result.json()
})
.then((data) => resolve(data))
.catch((error) => handleError(error, reject, options))
})
}
export async function get(
fetcher: Fetch,
url: string,
options?: FetchOptions,
parameters?: FetchParameters
): Promise<any> {
return _handleRequest(fetcher, 'GET', url, options, parameters)
}
export async function post(
fetcher: Fetch,
url: string,
body: object,
options?: FetchOptions,
parameters?: FetchParameters
): Promise<any> {
return _handleRequest(fetcher, 'POST', url, options, parameters, body)
}
export async function put(
fetcher: Fetch,
url: string,
body: object,
options?: FetchOptions,
parameters?: FetchParameters
): Promise<any> {
return _handleRequest(fetcher, 'PUT', url, options, parameters, body)
}
export async function head(
fetcher: Fetch,
url: string,
options?: FetchOptions,
parameters?: FetchParameters
): Promise<any> {
return _handleRequest(
fetcher,
'HEAD',
url,
{
...options,
noResolveJson: true,
},
parameters
)
}
export async function remove(
fetcher: Fetch,
url: string,
body: object,
options?: FetchOptions,
parameters?: FetchParameters
): Promise<any> {
return _handleRequest(fetcher, 'DELETE', url, options, parameters, body)
}

39
node_modules/@supabase/storage-js/src/lib/helpers.ts generated vendored Normal file
View File

@@ -0,0 +1,39 @@
type Fetch = typeof fetch
export const resolveFetch = (customFetch?: Fetch): Fetch => {
let _fetch: Fetch
if (customFetch) {
_fetch = customFetch
} else if (typeof fetch === 'undefined') {
_fetch = (...args) =>
import('@supabase/node-fetch' as any).then(({ default: fetch }) => fetch(...args))
} else {
_fetch = fetch
}
return (...args) => _fetch(...args)
}
export const resolveResponse = async (): Promise<typeof Response> => {
if (typeof Response === 'undefined') {
// @ts-ignore
return (await import('@supabase/node-fetch' as any)).Response
}
return Response
}
export const recursiveToCamel = (item: Record<string, any>): unknown => {
if (Array.isArray(item)) {
return item.map((el) => recursiveToCamel(el))
} else if (typeof item === 'function' || item !== Object(item)) {
return item
}
const result: Record<string, any> = {}
Object.entries(item).forEach(([key, value]) => {
const newKey = key.replace(/([-_][a-z])/gi, (c) => c.toUpperCase().replace(/[-_]/g, ''))
result[newKey] = recursiveToCamel(value)
})
return result
}

4
node_modules/@supabase/storage-js/src/lib/index.ts generated vendored Normal file
View File

@@ -0,0 +1,4 @@
export * from '../packages/StorageBucketApi'
export * from '../packages/StorageFileApi'
export * from './types'
export * from './constants'

149
node_modules/@supabase/storage-js/src/lib/types.ts generated vendored Normal file
View File

@@ -0,0 +1,149 @@
export interface Bucket {
id: string
name: string
owner: string
file_size_limit?: number
allowed_mime_types?: string[]
created_at: string
updated_at: string
public: boolean
}
export interface FileObject {
name: string
bucket_id: string
owner: string
id: string
updated_at: string
created_at: string
last_accessed_at: string
metadata: Record<string, any>
buckets: Bucket
}
export interface FileObjectV2 {
id: string
version: string
name: string
bucket_id: string
updated_at: string
created_at: string
last_accessed_at: string
size?: number
cache_control?: string
content_type?: string
etag?: string
last_modified?: string
metadata?: Record<string, any>
}
export interface SortBy {
column?: string
order?: string
}
export interface FileOptions {
/**
* The number of seconds the asset is cached in the browser and in the Supabase CDN. This is set in the `Cache-Control: max-age=<seconds>` header. Defaults to 3600 seconds.
*/
cacheControl?: string
/**
* the `Content-Type` header value. Should be specified if using a `fileBody` that is neither `Blob` nor `File` nor `FormData`, otherwise will default to `text/plain;charset=UTF-8`.
*/
contentType?: string
/**
* When upsert is set to true, the file is overwritten if it exists. When set to false, an error is thrown if the object already exists. Defaults to false.
*/
upsert?: boolean
/**
* The duplex option is a string parameter that enables or disables duplex streaming, allowing for both reading and writing data in the same stream. It can be passed as an option to the fetch() method.
*/
duplex?: string
/**
* The metadata option is an object that allows you to store additional information about the file. This information can be used to filter and search for files. The metadata object can contain any key-value pairs you want to store.
*/
metadata?: Record<string, any>
/**
* Optionally add extra headers
*/
headers?: Record<string, string>
}
export interface DestinationOptions {
destinationBucket?: string
}
export interface SearchOptions {
/**
* The number of files you want to be returned.
*/
limit?: number
/**
* The starting position.
*/
offset?: number
/**
* The column to sort by. Can be any column inside a FileObject.
*/
sortBy?: SortBy
/**
* The search string to filter files by.
*/
search?: string
}
export interface FetchParameters {
/**
* Pass in an AbortController's signal to cancel the request.
*/
signal?: AbortSignal
}
// TODO: need to check for metadata props. The api swagger doesnt have.
export interface Metadata {
name: string
}
export interface TransformOptions {
/**
* The width of the image in pixels.
*/
width?: number
/**
* The height of the image in pixels.
*/
height?: number
/**
* The resize mode can be cover, contain or fill. Defaults to cover.
* Cover resizes the image to maintain it's aspect ratio while filling the entire width and height.
* Contain resizes the image to maintain it's aspect ratio while fitting the entire image within the width and height.
* Fill resizes the image to fill the entire width and height. If the object's aspect ratio does not match the width and height, the image will be stretched to fit.
*/
resize?: 'cover' | 'contain' | 'fill'
/**
* Set the quality of the returned image.
* A number from 20 to 100, with 100 being the highest quality.
* Defaults to 80
*/
quality?: number
/**
* Specify the format of the image requested.
*
* When using 'origin' we force the format to be the same as the original image.
* When this option is not passed in, images are optimized to modern image formats like Webp.
*/
format?: 'origin'
}
type CamelCase<S extends string> = S extends `${infer P1}_${infer P2}${infer P3}`
? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}`
: S
export type Camelize<T> = {
[K in keyof T as CamelCase<Extract<K, string>>]: T[K]
}

2
node_modules/@supabase/storage-js/src/lib/version.ts generated vendored Normal file
View File

@@ -0,0 +1,2 @@
// generated by genversion
export const version = '2.7.1'

View File

@@ -0,0 +1,247 @@
import { DEFAULT_HEADERS } from '../lib/constants'
import { isStorageError, StorageError } from '../lib/errors'
import { Fetch, get, post, put, remove } from '../lib/fetch'
import { resolveFetch } from '../lib/helpers'
import { Bucket } from '../lib/types'
export default class StorageBucketApi {
protected url: string
protected headers: { [key: string]: string }
protected fetch: Fetch
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
this.url = url
this.headers = { ...DEFAULT_HEADERS, ...headers }
this.fetch = resolveFetch(fetch)
}
/**
* Retrieves the details of all Storage buckets within an existing project.
*/
async listBuckets(): Promise<
| {
data: Bucket[]
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await get(this.fetch, `${this.url}/bucket`, { headers: this.headers })
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Retrieves the details of an existing Storage bucket.
*
* @param id The unique identifier of the bucket you would like to retrieve.
*/
async getBucket(
id: string
): Promise<
| {
data: Bucket
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await get(this.fetch, `${this.url}/bucket/${id}`, { headers: this.headers })
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Creates a new Storage bucket
*
* @param id A unique identifier for the bucket you are creating.
* @param options.public The visibility of the bucket. Public buckets don't require an authorization token to download objects, but still require a valid token for all other operations. By default, buckets are private.
* @param options.fileSizeLimit specifies the max file size in bytes that can be uploaded to this bucket.
* The global file size limit takes precedence over this value.
* The default value is null, which doesn't set a per bucket file size limit.
* @param options.allowedMimeTypes specifies the allowed mime types that this bucket can accept during upload.
* The default value is null, which allows files with all mime types to be uploaded.
* Each mime type specified can be a wildcard, e.g. image/*, or a specific mime type, e.g. image/png.
* @returns newly created bucket id
*/
async createBucket(
id: string,
options: {
public: boolean
fileSizeLimit?: number | string | null
allowedMimeTypes?: string[] | null
} = {
public: false,
}
): Promise<
| {
data: Pick<Bucket, 'name'>
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await post(
this.fetch,
`${this.url}/bucket`,
{
id,
name: id,
public: options.public,
file_size_limit: options.fileSizeLimit,
allowed_mime_types: options.allowedMimeTypes,
},
{ headers: this.headers }
)
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Updates a Storage bucket
*
* @param id A unique identifier for the bucket you are updating.
* @param options.public The visibility of the bucket. Public buckets don't require an authorization token to download objects, but still require a valid token for all other operations.
* @param options.fileSizeLimit specifies the max file size in bytes that can be uploaded to this bucket.
* The global file size limit takes precedence over this value.
* The default value is null, which doesn't set a per bucket file size limit.
* @param options.allowedMimeTypes specifies the allowed mime types that this bucket can accept during upload.
* The default value is null, which allows files with all mime types to be uploaded.
* Each mime type specified can be a wildcard, e.g. image/*, or a specific mime type, e.g. image/png.
*/
async updateBucket(
id: string,
options: {
public: boolean
fileSizeLimit?: number | string | null
allowedMimeTypes?: string[] | null
}
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await put(
this.fetch,
`${this.url}/bucket/${id}`,
{
id,
name: id,
public: options.public,
file_size_limit: options.fileSizeLimit,
allowed_mime_types: options.allowedMimeTypes,
},
{ headers: this.headers }
)
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Removes all objects inside a single bucket.
*
* @param id The unique identifier of the bucket you would like to empty.
*/
async emptyBucket(
id: string
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await post(
this.fetch,
`${this.url}/bucket/${id}/empty`,
{},
{ headers: this.headers }
)
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Deletes an existing bucket. A bucket can't be deleted with existing objects inside it.
* You must first `empty()` the bucket.
*
* @param id The unique identifier of the bucket you would like to delete.
*/
async deleteBucket(
id: string
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await remove(
this.fetch,
`${this.url}/bucket/${id}`,
{},
{ headers: this.headers }
)
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
}

View File

@@ -0,0 +1,834 @@
import { isStorageError, StorageError, StorageUnknownError } from '../lib/errors'
import { Fetch, get, head, post, remove } from '../lib/fetch'
import { recursiveToCamel, resolveFetch } from '../lib/helpers'
import {
FileObject,
FileOptions,
SearchOptions,
FetchParameters,
TransformOptions,
DestinationOptions,
FileObjectV2,
Camelize,
} from '../lib/types'
const DEFAULT_SEARCH_OPTIONS = {
limit: 100,
offset: 0,
sortBy: {
column: 'name',
order: 'asc',
},
}
const DEFAULT_FILE_OPTIONS: FileOptions = {
cacheControl: '3600',
contentType: 'text/plain;charset=UTF-8',
upsert: false,
}
type FileBody =
| ArrayBuffer
| ArrayBufferView
| Blob
| Buffer
| File
| FormData
| NodeJS.ReadableStream
| ReadableStream<Uint8Array>
| URLSearchParams
| string
export default class StorageFileApi {
protected url: string
protected headers: { [key: string]: string }
protected bucketId?: string
protected fetch: Fetch
constructor(
url: string,
headers: { [key: string]: string } = {},
bucketId?: string,
fetch?: Fetch
) {
this.url = url
this.headers = headers
this.bucketId = bucketId
this.fetch = resolveFetch(fetch)
}
/**
* Uploads a file to an existing bucket or replaces an existing file at the specified path with a new one.
*
* @param method HTTP method.
* @param path The relative file path. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
* @param fileBody The body of the file to be stored in the bucket.
*/
private async uploadOrUpdate(
method: 'POST' | 'PUT',
path: string,
fileBody: FileBody,
fileOptions?: FileOptions
): Promise<
| {
data: { id: string; path: string; fullPath: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
let body
const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
let headers: Record<string, string> = {
...this.headers,
...(method === 'POST' && { 'x-upsert': String(options.upsert as boolean) }),
}
const metadata = options.metadata
if (typeof Blob !== 'undefined' && fileBody instanceof Blob) {
body = new FormData()
body.append('cacheControl', options.cacheControl as string)
if (metadata) {
body.append('metadata', this.encodeMetadata(metadata))
}
body.append('', fileBody)
} else if (typeof FormData !== 'undefined' && fileBody instanceof FormData) {
body = fileBody
body.append('cacheControl', options.cacheControl as string)
if (metadata) {
body.append('metadata', this.encodeMetadata(metadata))
}
} else {
body = fileBody
headers['cache-control'] = `max-age=${options.cacheControl}`
headers['content-type'] = options.contentType as string
if (metadata) {
headers['x-metadata'] = this.toBase64(this.encodeMetadata(metadata))
}
}
if (fileOptions?.headers) {
headers = { ...headers, ...fileOptions.headers }
}
const cleanPath = this._removeEmptyFolders(path)
const _path = this._getFinalPath(cleanPath)
const res = await this.fetch(`${this.url}/object/${_path}`, {
method,
body: body as BodyInit,
headers,
...(options?.duplex ? { duplex: options.duplex } : {}),
})
const data = await res.json()
if (res.ok) {
return {
data: { path: cleanPath, id: data.Id, fullPath: data.Key },
error: null,
}
} else {
const error = data
return { data: null, error }
}
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Uploads a file to an existing bucket.
*
* @param path The file path, including the file name. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
* @param fileBody The body of the file to be stored in the bucket.
*/
async upload(
path: string,
fileBody: FileBody,
fileOptions?: FileOptions
): Promise<
| {
data: { id: string; path: string; fullPath: string }
error: null
}
| {
data: null
error: StorageError
}
> {
return this.uploadOrUpdate('POST', path, fileBody, fileOptions)
}
/**
* Upload a file with a token generated from `createSignedUploadUrl`.
* @param path The file path, including the file name. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
* @param token The token generated from `createSignedUploadUrl`
* @param fileBody The body of the file to be stored in the bucket.
*/
async uploadToSignedUrl(
path: string,
token: string,
fileBody: FileBody,
fileOptions?: FileOptions
) {
const cleanPath = this._removeEmptyFolders(path)
const _path = this._getFinalPath(cleanPath)
const url = new URL(this.url + `/object/upload/sign/${_path}`)
url.searchParams.set('token', token)
try {
let body
const options = { upsert: DEFAULT_FILE_OPTIONS.upsert, ...fileOptions }
const headers: Record<string, string> = {
...this.headers,
...{ 'x-upsert': String(options.upsert as boolean) },
}
if (typeof Blob !== 'undefined' && fileBody instanceof Blob) {
body = new FormData()
body.append('cacheControl', options.cacheControl as string)
body.append('', fileBody)
} else if (typeof FormData !== 'undefined' && fileBody instanceof FormData) {
body = fileBody
body.append('cacheControl', options.cacheControl as string)
} else {
body = fileBody
headers['cache-control'] = `max-age=${options.cacheControl}`
headers['content-type'] = options.contentType as string
}
const res = await this.fetch(url.toString(), {
method: 'PUT',
body: body as BodyInit,
headers,
})
const data = await res.json()
if (res.ok) {
return {
data: { path: cleanPath, fullPath: data.Key },
error: null,
}
} else {
const error = data
return { data: null, error }
}
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Creates a signed upload URL.
* Signed upload URLs can be used to upload files to the bucket without further authentication.
* They are valid for 2 hours.
* @param path The file path, including the current file name. For example `folder/image.png`.
* @param options.upsert If set to true, allows the file to be overwritten if it already exists.
*/
async createSignedUploadUrl(
path: string,
options?: { upsert: boolean }
): Promise<
| {
data: { signedUrl: string; token: string; path: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
let _path = this._getFinalPath(path)
const headers = { ...this.headers }
if (options?.upsert) {
headers['x-upsert'] = 'true'
}
const data = await post(
this.fetch,
`${this.url}/object/upload/sign/${_path}`,
{},
{ headers }
)
const url = new URL(this.url + data.url)
const token = url.searchParams.get('token')
if (!token) {
throw new StorageError('No token returned by API')
}
return { data: { signedUrl: url.toString(), path, token }, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Replaces an existing file at the specified path with a new one.
*
* @param path The relative file path. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to update.
* @param fileBody The body of the file to be stored in the bucket.
*/
async update(
path: string,
fileBody:
| ArrayBuffer
| ArrayBufferView
| Blob
| Buffer
| File
| FormData
| NodeJS.ReadableStream
| ReadableStream<Uint8Array>
| URLSearchParams
| string,
fileOptions?: FileOptions
): Promise<
| {
data: { id: string; path: string; fullPath: string }
error: null
}
| {
data: null
error: StorageError
}
> {
return this.uploadOrUpdate('PUT', path, fileBody, fileOptions)
}
/**
* Moves an existing file to a new path in the same bucket.
*
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
* @param toPath The new file path, including the new file name. For example `folder/image-new.png`.
* @param options The destination options.
*/
async move(
fromPath: string,
toPath: string,
options?: DestinationOptions
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await post(
this.fetch,
`${this.url}/object/move`,
{
bucketId: this.bucketId,
sourceKey: fromPath,
destinationKey: toPath,
destinationBucket: options?.destinationBucket,
},
{ headers: this.headers }
)
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Copies an existing file to a new path in the same bucket.
*
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
* @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
* @param options The destination options.
*/
async copy(
fromPath: string,
toPath: string,
options?: DestinationOptions
): Promise<
| {
data: { path: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await post(
this.fetch,
`${this.url}/object/copy`,
{
bucketId: this.bucketId,
sourceKey: fromPath,
destinationKey: toPath,
destinationBucket: options?.destinationBucket,
},
{ headers: this.headers }
)
return { data: { path: data.Key }, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Creates a signed URL. Use a signed URL to share a file for a fixed amount of time.
*
* @param path The file path, including the current file name. For example `folder/image.png`.
* @param expiresIn The number of seconds until the signed URL expires. For example, `60` for a URL which is valid for one minute.
* @param options.download triggers the file as a download if set to true. Set this parameter as the name of the file if you want to trigger the download with a different filename.
* @param options.transform Transform the asset before serving it to the client.
*/
async createSignedUrl(
path: string,
expiresIn: number,
options?: { download?: string | boolean; transform?: TransformOptions }
): Promise<
| {
data: { signedUrl: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
let _path = this._getFinalPath(path)
let data = await post(
this.fetch,
`${this.url}/object/sign/${_path}`,
{ expiresIn, ...(options?.transform ? { transform: options.transform } : {}) },
{ headers: this.headers }
)
const downloadQueryParam = options?.download
? `&download=${options.download === true ? '' : options.download}`
: ''
const signedUrl = encodeURI(`${this.url}${data.signedURL}${downloadQueryParam}`)
data = { signedUrl }
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Creates multiple signed URLs. Use a signed URL to share a file for a fixed amount of time.
*
* @param paths The file paths to be downloaded, including the current file names. For example `['folder/image.png', 'folder2/image2.png']`.
* @param expiresIn The number of seconds until the signed URLs expire. For example, `60` for URLs which are valid for one minute.
* @param options.download triggers the file as a download if set to true. Set this parameter as the name of the file if you want to trigger the download with a different filename.
*/
async createSignedUrls(
paths: string[],
expiresIn: number,
options?: { download: string | boolean }
): Promise<
| {
data: { error: string | null; path: string | null; signedUrl: string }[]
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await post(
this.fetch,
`${this.url}/object/sign/${this.bucketId}`,
{ expiresIn, paths },
{ headers: this.headers }
)
const downloadQueryParam = options?.download
? `&download=${options.download === true ? '' : options.download}`
: ''
return {
data: data.map((datum: { signedURL: string }) => ({
...datum,
signedUrl: datum.signedURL
? encodeURI(`${this.url}${datum.signedURL}${downloadQueryParam}`)
: null,
})),
error: null,
}
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Downloads a file from a private bucket. For public buckets, make a request to the URL returned from `getPublicUrl` instead.
*
* @param path The full path and file name of the file to be downloaded. For example `folder/image.png`.
* @param options.transform Transform the asset before serving it to the client.
*/
async download(
path: string,
options?: { transform?: TransformOptions }
): Promise<
| {
data: Blob
error: null
}
| {
data: null
error: StorageError
}
> {
const wantsTransformation = typeof options?.transform !== 'undefined'
const renderPath = wantsTransformation ? 'render/image/authenticated' : 'object'
const transformationQuery = this.transformOptsToQueryString(options?.transform || {})
const queryString = transformationQuery ? `?${transformationQuery}` : ''
try {
const _path = this._getFinalPath(path)
const res = await get(this.fetch, `${this.url}/${renderPath}/${_path}${queryString}`, {
headers: this.headers,
noResolveJson: true,
})
const data = await res.blob()
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Retrieves the details of an existing file.
* @param path
*/
async info(
path: string
): Promise<
| {
data: Camelize<FileObjectV2>
error: null
}
| {
data: null
error: StorageError
}
> {
const _path = this._getFinalPath(path)
try {
const data = await get(this.fetch, `${this.url}/object/info/${_path}`, {
headers: this.headers,
})
return { data: recursiveToCamel(data) as Camelize<FileObjectV2>, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Checks the existence of a file.
* @param path
*/
async exists(
path: string
): Promise<
| {
data: boolean
error: null
}
| {
data: boolean
error: StorageError
}
> {
const _path = this._getFinalPath(path)
try {
await head(this.fetch, `${this.url}/object/${_path}`, {
headers: this.headers,
})
return { data: true, error: null }
} catch (error) {
if (isStorageError(error) && error instanceof StorageUnknownError) {
const originalError = (error.originalError as unknown) as { status: number }
if ([400, 404].includes(originalError?.status)) {
return { data: false, error }
}
}
throw error
}
}
/**
* A simple convenience function to get the URL for an asset in a public bucket. If you do not want to use this function, you can construct the public URL by concatenating the bucket URL with the path to the asset.
* This function does not verify if the bucket is public. If a public URL is created for a bucket which is not public, you will not be able to download the asset.
*
* @param path The path and name of the file to generate the public URL for. For example `folder/image.png`.
* @param options.download Triggers the file as a download if set to true. Set this parameter as the name of the file if you want to trigger the download with a different filename.
* @param options.transform Transform the asset before serving it to the client.
*/
getPublicUrl(
path: string,
options?: { download?: string | boolean; transform?: TransformOptions }
): { data: { publicUrl: string } } {
const _path = this._getFinalPath(path)
const _queryString = []
const downloadQueryParam = options?.download
? `download=${options.download === true ? '' : options.download}`
: ''
if (downloadQueryParam !== '') {
_queryString.push(downloadQueryParam)
}
const wantsTransformation = typeof options?.transform !== 'undefined'
const renderPath = wantsTransformation ? 'render/image' : 'object'
const transformationQuery = this.transformOptsToQueryString(options?.transform || {})
if (transformationQuery !== '') {
_queryString.push(transformationQuery)
}
let queryString = _queryString.join('&')
if (queryString !== '') {
queryString = `?${queryString}`
}
return {
data: { publicUrl: encodeURI(`${this.url}/${renderPath}/public/${_path}${queryString}`) },
}
}
/**
* Deletes files within the same bucket
*
* @param paths An array of files to delete, including the path and file name. For example [`'folder/image.png'`].
*/
async remove(
paths: string[]
): Promise<
| {
data: FileObject[]
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await remove(
this.fetch,
`${this.url}/object/${this.bucketId}`,
{ prefixes: paths },
{ headers: this.headers }
)
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
/**
* Get file metadata
* @param id the file id to retrieve metadata
*/
// async getMetadata(
// id: string
// ): Promise<
// | {
// data: Metadata
// error: null
// }
// | {
// data: null
// error: StorageError
// }
// > {
// try {
// const data = await get(this.fetch, `${this.url}/metadata/${id}`, { headers: this.headers })
// return { data, error: null }
// } catch (error) {
// if (isStorageError(error)) {
// return { data: null, error }
// }
// throw error
// }
// }
/**
* Update file metadata
* @param id the file id to update metadata
* @param meta the new file metadata
*/
// async updateMetadata(
// id: string,
// meta: Metadata
// ): Promise<
// | {
// data: Metadata
// error: null
// }
// | {
// data: null
// error: StorageError
// }
// > {
// try {
// const data = await post(
// this.fetch,
// `${this.url}/metadata/${id}`,
// { ...meta },
// { headers: this.headers }
// )
// return { data, error: null }
// } catch (error) {
// if (isStorageError(error)) {
// return { data: null, error }
// }
// throw error
// }
// }
/**
* Lists all the files within a bucket.
* @param path The folder path.
*/
async list(
path?: string,
options?: SearchOptions,
parameters?: FetchParameters
): Promise<
| {
data: FileObject[]
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
const data = await post(
this.fetch,
`${this.url}/object/list/${this.bucketId}`,
body,
{ headers: this.headers },
parameters
)
return { data, error: null }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
}
throw error
}
}
protected encodeMetadata(metadata: Record<string, any>) {
return JSON.stringify(metadata)
}
toBase64(data: string) {
if (typeof Buffer !== 'undefined') {
return Buffer.from(data).toString('base64')
}
return btoa(data)
}
private _getFinalPath(path: string) {
return `${this.bucketId}/${path}`
}
private _removeEmptyFolders(path: string) {
return path.replace(/^\/|\/$/g, '').replace(/\/+/g, '/')
}
private transformOptsToQueryString(transform: TransformOptions) {
const params = []
if (transform.width) {
params.push(`width=${transform.width}`)
}
if (transform.height) {
params.push(`height=${transform.height}`)
}
if (transform.resize) {
params.push(`resize=${transform.resize}`)
}
if (transform.format) {
params.push(`format=${transform.format}`)
}
if (transform.quality) {
params.push(`quality=${transform.quality}`)
}
return params.join('&')
}
}