Browse Source

Add `fetchOptions` to AssetManager configs

pull/5010/head
Artur Arseniev 3 years ago
parent
commit
6184b6c86c
  1. 6
      src/asset_manager/config/config.ts
  2. 8
      src/asset_manager/view/FileUploader.ts

6
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

8
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

Loading…
Cancel
Save