Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(comp:upload): actions and files are optional #1128

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions packages/components/upload/src/composables/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ref } from 'vue'

import { isFunction, isUndefined } from 'lodash-es'

import { callEmit, throwError } from '@idux/cdk/utils'
import { callEmit } from '@idux/cdk/utils'
import { useGlobalConfig } from '@idux/components/config'

import { getTargetFile, getTargetFileIndex, setFileStatus } from '../util/fileHandle'
Expand Down Expand Up @@ -165,14 +165,9 @@ export function useRequest(props: UploadProps, files: ComputedRef<UploadFile[]>)
}

async function getAction(props: UploadProps, file: UploadFile) {
if (!props.action) {
throwError('components/upload', 'action not found.')
}
const action = isFunction(props.action) ? await props.action(file) : props.action
return action
return isFunction(props.action) ? await props.action(file) : props.action
}

async function getRequestData(props: UploadProps, file: UploadFile) {
const requestData = isFunction(props.requestData) ? await props.requestData(file) : props.requestData ?? {}
return requestData
return isFunction(props.requestData) ? await props.requestData(file) : props.requestData ?? {}
}
4 changes: 2 additions & 2 deletions packages/components/upload/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export interface UploadRequestChangeOption<K = VKey> {
export const uploadProps = {
files: {
type: Array as PropType<UploadFile[]>,
required: true,
default: [],
},
accept: String,
action: {
type: [String, Function] as PropType<string | ((file: UploadFile<any>) => Promise<string>)>,
required: true,
default: undefined,
},
dragable: {
type: Boolean,
Expand Down
30 changes: 21 additions & 9 deletions packages/components/upload/src/util/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ type UploadReturnType = {
abort: () => void
}

function getError(option: UploadRequestOption, xhr: XMLHttpRequest) {
let msg: string
if (xhr.response) {
msg = `${xhr.response.error || xhr.response}`
} else if (xhr.responseText) {
msg = `${xhr.responseText}`
} else {
msg = `fail to ${option.requestMethod} ${option.action} ${xhr.status}`
function getErrorMsg(option: UploadRequestOption, xhr: XMLHttpRequest) {
const errorMap = new Map<(option: UploadRequestOption, xhr: XMLHttpRequest) => boolean, string>([])
.set((option, xhr) => !!xhr.response, `${xhr.response?.error || xhr.response}`)
.set((option, xhr) => !!xhr.responseText, `${xhr.responseText}`)
.set(option => !option.action, 'action not found.')
.set(() => true, `fail to ${option.requestMethod} ${option.action} ${xhr.status}`)

for (const [testFn, errorMsg] of errorMap) {
if (testFn(option, xhr)) {
return errorMsg
}
}
return ''
}

function getError(option: UploadRequestOption, xhr: XMLHttpRequest) {
const msg = getErrorMsg(option, xhr)
const err = new Error(msg) as UploadRequestError
err.status = xhr.status
err.method = option.requestMethod
Expand All @@ -45,7 +53,7 @@ function getBody(xhr: XMLHttpRequest) {
}
}

export default function upload(option: UploadRequestOption): UploadReturnType {
export default function upload(option: UploadRequestOption): UploadReturnType | null {
const xhr = new XMLHttpRequest()

if (option.onProgress && xhr.upload) {
Expand Down Expand Up @@ -87,6 +95,10 @@ export default function upload(option: UploadRequestOption): UploadReturnType {
return option.onSuccess?.(getBody(xhr))
}

if (!option.action) {
option.onError?.(getError(option, xhr))
return null
}
xhr.open(option.requestMethod, option.action, true)

if (option.withCredentials && 'withCredentials' in xhr) {
Expand Down