@@ -208,18 +208,44 @@ export async function list(storageZone: StorageZone.StorageZone, path: string):
208208 } ) ) ;
209209}
210210
211+ /**
212+ * Options for the deletion operations ([remove], [removeDirectory]).
213+ */
214+ export type RemoveOptions = {
215+ /**
216+ * When `true`, the operation throws on a failed request (mirroring the
217+ * behaviour of [upload] and [download]) instead of resolving to `false`.
218+ *
219+ * Defaults to `false` to preserve backwards compatibility: by default a
220+ * failed request still resolves to `false`.
221+ *
222+ * @deprecated The opt-in is temporary. In v1 throwing becomes the default
223+ * behaviour and this option (along with the `boolean` return) will be
224+ * removed. Adopt `{ throwOnError: true }` now to ease the migration.
225+ */
226+ throwOnError ?: boolean ;
227+ } ;
228+
211229/**
212230 * Remove files and folders in a directory from a [StorageZone].
213231 *
214- * @throws
232+ * By default a failed request resolves to `false`. Pass
233+ * `{ throwOnError: true }` to instead throw on failure, mirroring [upload]
234+ * and [download].
235+ *
236+ * @throws When the request fails and `options.throwOnError` is `true`.
215237 */
216- export async function remove ( storageZone : StorageZone . StorageZone , path : string ) : Promise < boolean > {
238+ export async function remove ( storageZone : StorageZone . StorageZone , path : string , options ?: RemoveOptions ) : Promise < boolean > {
217239 const url = StorageZone . addr ( storageZone ) ;
218240 url . pathname = `${ url . pathname } ${ path } ` ;
219241
220242 const [ auth_header , key ] = StorageZone . key ( storageZone ) ;
221243 const response = await fetch ( url , { method : "DELETE" , headers : { [ auth_header ] : key } } ) ;
222244
245+ if ( ! response . ok && options ?. throwOnError ) {
246+ throw statusCodeToException ( storageZone , response . status , path ) ;
247+ }
248+
223249 return response . ok ;
224250}
225251
@@ -241,15 +267,23 @@ export async function createDirectory(storageZone: StorageZone.StorageZone, path
241267/**
242268 * Remove recursively a Directory in the [StorageZone].
243269 *
244- * @throws
270+ * By default a failed request resolves to `false`. Pass
271+ * `{ throwOnError: true }` to instead throw on failure, mirroring [upload]
272+ * and [download].
273+ *
274+ * @throws When the request fails and `options.throwOnError` is `true`.
245275 */
246- export async function removeDirectory ( storageZone : StorageZone . StorageZone , path : string ) : Promise < boolean > {
276+ export async function removeDirectory ( storageZone : StorageZone . StorageZone , path : string , options ?: RemoveOptions ) : Promise < boolean > {
247277 const url = StorageZone . addr ( storageZone ) ;
248278 const directory_path = path . endsWith ( "/" ) ? path : `${ path } /` ;
249279 url . pathname = `${ url . pathname } ${ directory_path } ` ;
250280 const [ auth_header , key ] = StorageZone . key ( storageZone ) ;
251281 const response = await fetch ( url , { method : "DELETE" , headers : { [ auth_header ] : key } } ) ;
252282
283+ if ( ! response . ok && options ?. throwOnError ) {
284+ throw statusCodeToException ( storageZone , response . status , path ) ;
285+ }
286+
253287 return response . ok ;
254288}
255289
0 commit comments