@a-vision-software/vue-input-components 1.4.12 → 1.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@a-vision-software/vue-input-components",
3
- "version": "1.4.12",
3
+ "version": "1.4.13",
4
4
  "description": "A collection of reusable Vue 3 input components with TypeScript support",
5
5
  "author": "A-Vision Software",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "https://github.com/a-vision/vue-input-components.git"
9
+ "url": "git+https://github.com/a-vision/vue-input-components.git"
10
10
  },
11
11
  "homepage": "https://a-vision.github.io/vue-input-components/",
12
12
  "keywords": [
@@ -48,7 +48,7 @@
48
48
 
49
49
  <script setup lang="ts">
50
50
  import { ref, watch, onMounted } from 'vue'
51
- import type { FileUploadProps, FileUploadEmits, UploadStatus } from '@/types/fileupload'
51
+ import type { FileUploadProps, FileUploadEmits, UploadStatus, UploadCallbackResponse } from '@/types/fileupload'
52
52
 
53
53
  const props = defineProps<FileUploadProps>()
54
54
  const emit = defineEmits<FileUploadEmits>()
@@ -115,7 +115,7 @@ const handleFileSelect = (e: Event) => {
115
115
  input.value = ''
116
116
  }
117
117
 
118
- const uploadFiles = async () => {
118
+ const uploadFiles = () => {
119
119
  if (!props.uploadUrl) {
120
120
  error.value = 'No upload URL provided'
121
121
  return
@@ -129,15 +129,34 @@ const uploadFiles = async () => {
129
129
  const formData = new FormData()
130
130
  files.value.forEach((file) => {
131
131
  formData.append('files', file)
132
- })
133
-
134
- if (props.uploadData) {
135
- const data = props.uploadData;
136
- formData.append('data', JSON.stringify(data))
132
+ });
133
+
134
+ if (props.uploadCallback) {
135
+ props.uploadCallback(formData)
136
+ .then((response: UploadCallbackResponse) => {
137
+ const { formData, headers } = response;
138
+ doUploadFiles(formData, headers)
139
+ })
140
+ .catch((err) => {
141
+ const errorMessage = err instanceof Error ? err.message : 'Upload failed'
142
+ error.value = errorMessage
143
+ uploadStatus.value = {
144
+ type: 'error',
145
+ message: errorMessage,
146
+ }
147
+ emit('upload-complete', files.value)
148
+ emit('upload-error', uploadStatus.value)
149
+ });
150
+ } else {
151
+ doUploadFiles(formData, {})
137
152
  }
153
+ }
138
154
 
155
+ const doUploadFiles = (formData: FormData, headers: Record<string, string>) => {
139
156
  try {
140
157
  const xhr = new XMLHttpRequest()
158
+ console.log('doUploadFiles:', formData, headers)
159
+
141
160
  xhr.upload.addEventListener('progress', (e) => {
142
161
  if (e.lengthComputable) {
143
162
  uploadProgress.value = (e.loaded / e.total) * 100
@@ -172,7 +191,12 @@ const uploadFiles = async () => {
172
191
  throw new Error('Upload failed')
173
192
  })
174
193
 
175
- xhr.open('POST', props.uploadUrl)
194
+ xhr.open('POST', props.uploadUrl || '')
195
+
196
+ Object.entries(headers).forEach(([key, value]) => {
197
+ xhr.setRequestHeader(key, value)
198
+ })
199
+
176
200
  xhr.send(formData)
177
201
  } catch (err) {
178
202
  const errorMessage = err instanceof Error ? err.message : 'Upload failed'
@@ -20,7 +20,12 @@ interface FileUploadProps {
20
20
  borderColor?: string // Border color.
21
21
  progressColor?: string // Progress color.
22
22
  activeColor?: string // Active color.
23
- uploadData?: Record<string, any> // Data to send with the upload.
23
+ uploadCallback?: (formData: FormData) => Promise<UploadCallbackResponse> // Callback function to handle the upload.
24
+ }
25
+
26
+ interface UploadCallbackResponse {
27
+ formData: FormData
28
+ headers: Record<string, string>
24
29
  }
25
30
 
26
31
  interface UploadStatus {
@@ -37,4 +42,4 @@ interface FileUploadEmits {
37
42
  (e: 'upload-error', status: UploadStatus): void // Emitted when the upload fails.
38
43
  }
39
44
 
40
- export type { FileUploadProps, FileUploadEmits, UploadStatus }
45
+ export type { FileUploadProps, FileUploadEmits, UploadStatus, UploadCallbackResponse }
@@ -24,7 +24,7 @@
24
24
  v-text="imageDisabled ? 'disabled' : 'enabled'"></span>)</h2>
25
25
  <FileUpload icon="image" accept="image/*" @start-upload="handleStartUpload" :disabled="imageDisabled"
26
26
  upload-url="https://httpbin.org/post" :multiple="true" :do-upload="uploadImages"
27
- @upload-complete="showImagesUpload = false" :upload-data="imageUploadData"
27
+ @upload-complete="showImagesUpload = false" :upload-callback="handleUploadCallback"
28
28
  @files-selected="showImagesUpload = true" />
29
29
  <button @click="uploadImages = true" v-if="showImagesUpload">Upload Images</button>
30
30
  </div>
@@ -39,7 +39,7 @@
39
39
  <script setup lang="ts">
40
40
  import { ref } from 'vue'
41
41
  import FileUpload from '@/components/FileUpload.vue'
42
- import type { UploadStatus } from '@/types/fileupload'
42
+ import type { UploadStatus, UploadCallbackResponse } from '@/types/fileupload'
43
43
 
44
44
  const imageDisabled = ref(true);
45
45
  const uploadImages = ref(false);
@@ -50,6 +50,21 @@ const uploadStatus = ref<Record<string, UploadStatus>>({
50
50
  basic: { type: 'success', message: '' },
51
51
  })
52
52
 
53
+ const handleUploadCallback = (formData: FormData): Promise<UploadCallbackResponse> => {
54
+ return new Promise((resolve) => {
55
+ // Add imageUploadData to formData
56
+ Object.entries(imageUploadData.value).forEach(([key, value]) => {
57
+ formData.append(key, value)
58
+ })
59
+ resolve({
60
+ formData,
61
+ headers: {
62
+ 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIqInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvqG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ',
63
+ },
64
+ });
65
+ })
66
+ }
67
+
53
68
  const handleUploadComplete = (files: File[]) => {
54
69
  uploadStatus.value.basic = {
55
70
  type: 'success',