vexip-ui
Version:
A Vue 3 UI library, Highly customizability, full TypeScript, performance pretty good
1 lines • 4.12 kB
Source Map (JSON)
{"version":3,"file":"request.cjs","sources":["../../../components/upload/request.ts"],"sourcesContent":["import { has, isDefined, noop } from '@vexip-ui/utils'\n\nimport type { UploadFetchOptions, UploadHttpError } from './symbol'\n\nfunction getError(url: string, xhr: XMLHttpRequest) {\n const message = `fail to post ${url} ${xhr.status}'`\n const error = new Error(message) as UploadHttpError\n\n error.response = getBody(xhr)\n error.url = url\n error.status = xhr.status\n error.method = 'POST'\n\n return error\n}\n\nfunction getBody(xhr: XMLHttpRequest) {\n const text = xhr.responseText || xhr.response\n\n if (!text) {\n return text\n }\n\n try {\n return JSON.parse(text)\n } catch (e) {\n return text\n }\n}\n\nexport function upload(options: UploadFetchOptions) {\n if (typeof XMLHttpRequest === 'undefined') {\n return noop\n }\n\n const xhr = new XMLHttpRequest()\n const url = options.url\n\n if (xhr.upload && options.onProgress) {\n xhr.upload.onprogress = event => {\n let percent = 0\n\n if (event.total > 0) {\n percent = (event.loaded / event.total) * 100\n }\n\n options.onProgress!(percent)\n }\n }\n\n const formData = new FormData()\n\n if (options.data) {\n const data = options.data\n\n Object.keys(data).forEach(key => {\n formData.append(key, data[key])\n })\n }\n\n formData.append(options.field || 'file', options.file)\n options.file.path && formData.append(options.pathField || 'path', options.file.path)\n\n if (options.onError) {\n xhr.onerror = () => {\n options.onError!(getError(url, xhr))\n }\n }\n\n xhr.onload = () => {\n if (xhr.status < 200 || xhr.status >= 300) {\n if (options.onError) {\n options.onError(getError(url, xhr))\n }\n\n return\n }\n\n const body = getBody(xhr)\n\n options.onSuccess?.(body)\n }\n\n if (options.onAbort) {\n xhr.onabort = () => {\n options.onAbort!()\n }\n }\n\n xhr.open('POST', url, true)\n\n if (options.withCredentials && 'withCredentials' in xhr) {\n xhr.withCredentials = true\n }\n\n const headers = options.headers ?? {}\n\n Object.keys(headers).forEach(header => {\n if (has(headers, header) && isDefined(headers[header])) {\n xhr.setRequestHeader(header, headers[header])\n }\n })\n\n xhr.send(formData)\n\n return () => xhr.abort()\n}\n"],"names":["getError","url","xhr","message","error","getBody","text","upload","options","noop","event","percent","formData","data","key","body","_a","headers","header","has","isDefined"],"mappings":"mHAIA,SAASA,EAASC,EAAaC,EAAqB,CAClD,MAAMC,EAAU,gBAAgBF,CAAG,IAAIC,EAAI,MAAM,IAC3CE,EAAQ,IAAI,MAAMD,CAAO,EAEzB,OAAAC,EAAA,SAAWC,EAAQH,CAAG,EAC5BE,EAAM,IAAMH,EACZG,EAAM,OAASF,EAAI,OACnBE,EAAM,OAAS,OAERA,CACT,CAEA,SAASC,EAAQH,EAAqB,CAC9B,MAAAI,EAAOJ,EAAI,cAAgBA,EAAI,SAErC,GAAI,CAACI,EACI,OAAAA,EAGL,GAAA,CACK,OAAA,KAAK,MAAMA,CAAI,OACZ,CACH,OAAAA,CAAA,CAEX,CAEO,SAASC,EAAOC,EAA6B,CAC9C,GAAA,OAAO,eAAmB,IACrB,OAAAC,EAAA,KAGH,MAAAP,EAAM,IAAI,eACVD,EAAMO,EAAQ,IAEhBN,EAAI,QAAUM,EAAQ,aACpBN,EAAA,OAAO,WAAsBQ,GAAA,CAC/B,IAAIC,EAAU,EAEVD,EAAM,MAAQ,IACLC,EAAAD,EAAM,OAASA,EAAM,MAAS,KAG3CF,EAAQ,WAAYG,CAAO,CAC7B,GAGI,MAAAC,EAAW,IAAI,SAErB,GAAIJ,EAAQ,KAAM,CAChB,MAAMK,EAAOL,EAAQ,KAErB,OAAO,KAAKK,CAAI,EAAE,QAAeC,GAAA,CAC/BF,EAAS,OAAOE,EAAKD,EAAKC,CAAG,CAAC,CAAA,CAC/B,CAAA,CAGHF,EAAS,OAAOJ,EAAQ,OAAS,OAAQA,EAAQ,IAAI,EAC7CA,EAAA,KAAK,MAAQI,EAAS,OAAOJ,EAAQ,WAAa,OAAQA,EAAQ,KAAK,IAAI,EAE/EA,EAAQ,UACVN,EAAI,QAAU,IAAM,CAClBM,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