Skip to content

Commit 0b0afe0

Browse files
committed
fix: expose fileOptions on upload/update file methods
1 parent bcd1c90 commit 0b0afe0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/lib/storage/StorageApi.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { get, post, put, remove } from './fetch'
22
import { isBrowser } from './helpers'
3-
import { Bucket, FileObject, Metadata, SearchOptions } from './types'
3+
import { Bucket, FileObject, FileOptions, Metadata, SearchOptions } from './types'
44

55
const DEFAULT_SEARCH_OPTIONS = {
66
limit: 0,
@@ -11,6 +11,10 @@ const DEFAULT_SEARCH_OPTIONS = {
1111
},
1212
}
1313

14+
const DEFAULT_FILE_OPTIONS: FileOptions = {
15+
cacheControl: '3600',
16+
}
17+
1418
export class StorageApi {
1519
url: string
1620
headers: { [key: string]: string }
@@ -98,17 +102,22 @@ export class StorageApi {
98102
*
99103
* @param path The relative file path including the bucket ID. Should be of the format `bucket/folder/subfolder`. The bucket already exist before attempting to upload.
100104
* @param file The File object to be stored in the bucket.
105+
* @param fileOptions HTTP headers. For example `cacheControl`
101106
*/
102107
async uploadFile(
103108
path: string,
104-
file: File
109+
file: File,
110+
fileOptions?: FileOptions
105111
): Promise<{ data: { message: string } | null; error: Error | null }> {
106112
try {
107113
if (!isBrowser()) throw new Error('No browser detected.')
108114

109115
const formData = new FormData()
110116
formData.append('', file, file.name)
111117

118+
const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
119+
formData.append('cacheControl', options.cacheControl)
120+
112121
const res = await fetch(`${this.url}/object/${path}`, {
113122
method: 'POST',
114123
body: formData,
@@ -132,17 +141,22 @@ export class StorageApi {
132141
*
133142
* @param path The relative file path including the bucket ID. Should be of the format `bucket/folder/subfolder`. The bucket already exist before attempting to upload.
134143
* @param file The file object to be stored in the bucket.
144+
* @param fileOptions HTTP headers. For example `cacheControl`
135145
*/
136146
async updateFile(
137147
path: string,
138-
file: File
148+
file: File,
149+
fileOptions?: FileOptions
139150
): Promise<{ data: { Key: string } | null; error: Error | null }> {
140151
try {
141152
if (!isBrowser()) throw new Error('No browser detected.')
142153

143154
const formData = new FormData()
144155
formData.append('', file, file.name)
145156

157+
const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
158+
formData.append('cacheControl', options.cacheControl)
159+
146160
const res = await fetch(`${this.url}/object/${path}`, {
147161
method: 'PUT',
148162
body: formData,

src/lib/storage/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export interface SortBy {
2222
column?: string
2323
order?: string
2424
}
25+
26+
export interface FileOptions {
27+
cacheControl: string
28+
}
29+
2530
export interface SearchOptions {
2631
/** The number of files you want to be returned. */
2732
limit?: number

0 commit comments

Comments
 (0)