forked from Auto-Plugin/milkup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.ts
More file actions
75 lines (62 loc) · 2.1 KB
/
Copy pathupload.ts
File metadata and controls
75 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { BodyType, RequestMethod, UploadStore } from '../types'
import { useStorage } from '@vueuse/core'
import { computed, readonly } from 'vue'
import { g } from '../utils'
const defaultUpload: UploadStore = {
url: '',
method: 'post',
headers: {},
bodyType: 'multipart/form-data',
fileField: 'file',
responseUrlPath: 'data.url',
extraBody: {},
}
function useUploadStore() {
const upload = useStorage<UploadStore>(g('upload'), defaultUpload, localStorage, {
mergeDefaults: true,
})
const url = computed(() => upload.value.url)
const method = computed(() => upload.value.method)
const headers = computed(() => upload.value.headers)
const bodyType = computed(() => upload.value.bodyType)
const fileField = computed(() => upload.value.fileField)
const extraBody = computed(() => upload.value.extraBody)
const responseUrlPath = computed(() => upload.value.responseUrlPath)
function set<K extends keyof UploadStore>(key: K, value: UploadStore[K]) {
upload.value[key] = value
}
function get<K extends keyof UploadStore>(key: K) {
return upload.value[key]
}
return {
upload: readonly(upload),
url,
method,
headers,
bodyType,
fileField,
responseUrlPath,
extraBody,
get,
set,
setUrl: (value: string) => set('url', value),
setMethod: (value: RequestMethod) => set('method', value),
setHeaders: (value: Record<string, string>) => set('headers', value),
setBodyType: (value: BodyType) => set('bodyType', value),
setFileField: (value: string) => set('fileField', value),
setResponseUrlPath: (value: string) => set('responseUrlPath', value),
setExtraBody: (value: Record<string, unknown>) => set('extraBody', value),
addHeader: (key: string, value: string) => {
upload.value.headers = { ...upload.value.headers, [key]: value }
},
removeHeader: (key: string) => {
const { [key]: _, ...rest } = upload.value.headers
upload.value.headers = rest
},
reset: () => {
upload.value = { ...defaultUpload }
},
}
}
export type { UploadStore }
export { defaultUpload, useUploadStore }