diff --git a/src/asset_manager/config/config.ts b/src/asset_manager/config/config.ts index a68910fdf..4e08866a3 100644 --- a/src/asset_manager/config/config.ts +++ b/src/asset_manager/config/config.ts @@ -56,6 +56,12 @@ export interface AssetManagerConfig { * @default true */ autoAdd?: boolean; + /** + * Customize the options passed to the default Fetch API. + * @example + * fetchOptions: (options) => ({ ...options, method: 'put' }), + */ + fetchOptions?: (options: RequestInit) => RequestInit; /** * To upload your assets, the module uses Fetch API. With this option you can overwrite it with your own logic. The custom function should return a Promise. * @example diff --git a/src/asset_manager/view/FileUploader.ts b/src/asset_manager/view/FileUploader.ts index 6c5849f61..47e6c4bd5 100644 --- a/src/asset_manager/view/FileUploader.ts +++ b/src/asset_manager/view/FileUploader.ts @@ -147,7 +147,7 @@ export default class FileUploaderView extends View { if (beforeUploadResponse === false) return; const body = new FormData(); - const { params, customFetch } = config; + const { params, customFetch, fetchOptions } = config; for (let param in params) { body.append(param, params[param]); @@ -161,7 +161,6 @@ export default class FileUploaderView extends View { body.append(config.uploadName!, files[0]); } - var target = this.target; const url = config.upload; const headers = config.headers!; const reqHead = 'X-Requested-With'; @@ -178,9 +177,10 @@ export default class FileUploaderView extends View { headers, body, }; + const fetchOptsResult = fetchOptions?.(fetchOpts) || fetchOpts; const fetchResult = customFetch - ? customFetch(url, fetchOpts) - : fetch(url, fetchOpts).then((res: any) => + ? customFetch(url, fetchOptsResult) + : fetch(url, fetchOptsResult).then((res: any) => ((res.status / 200) | 0) == 1 ? res.text() : res.text().then((text: string) => Promise.reject(text)) ); return fetchResult