-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add a genric type for "file" fields (same as "json" ?) #65
Comments
Well, I have "solved" it with some shameless duck typing: /**
* process form data to append files
* @param data form data to process
* @returns FormData object with files
*/
function processFiles(data:Object) {
const formData = new FormData()
Object.entries(data).forEach(([key, value]) => {
if (value?.[0]?.file instanceof File) {
value.forEach((file) => {
formData.append(key, file.file)
})
} else {
formData.append(key, value as string)
}
})
return formData
} |
I haven't used file fields in pocketbase so I'll need to do more investigation. One thing worth mentioning is that there should be a Do all file create requests have the type |
For PocketBase upset me by not having an upsert method (pun intended). But I should be able to write a generic one as you provide mapping for Collection/type/Id/string... Does
|
I managed to convert "FormKit field input" to what React people do: type FormKitS<T> = FormKitSchemaFormKit &
(
| {
name: keyof T
}
| {
ignore: true
}
)
const nvlMaison = ref<MaisonResponse>()
const schema: FormKitS<MaisonRecord>[] = [
/* ... */
{
$formkit: 'file',
ignore: true,
label: 'Images',
accept: '.png,.jpg,.jpeg,.bmp,.gif,.tiff',
multiple: 'true',
onchange: (e: Event) => {
const target = e.target as HTMLInputElement
const files = target.files
if (files) {
const images = Array.from(files)
nvlMaison.value = {
...nvlMaison.value,
// @ts-ignore Impossible to assign type `File[]` to type `string[]`
images
}
}
}
}
] And get rid of the function processFiles(data: Object) {
const formData = new FormData()
Object.entries(data).forEach(([key, value]) => {
if (value?.[0] instanceof File) {
value.forEach((file) => {
formData.append(key, file)
})
} else {
formData.append(key, value as string)
}
})
return formData
} I will ask FormKit if they are willing to optionally return |
Hey @ppierre do the latest changes to the pocketbase SDK help this issue? See the notes for v0.17 You can now send the file as an object |
@patmood thanks for the tip. It still exists a discrepancy between:
|
After reflection, For my code I should make a custom field component who accept |
Thanks for such a nice and straight forward tool.
I was testing file upload with PocketBase/Vue/FormKit. I managed to strongly type my form.
But while processing my form, I had to "patch" the type:
FormKit sends me the
files
property of the form field. Not thestring[]
who would exist only after PocketBase had processed the images.My idea was of generating for
file
fields, a generic type with a default value:Somehow like
json
fields.Or someone can find a better solution where the alternate type is fixed or specified only once for all
file
's fields ?I am suggesting in reason for this frequent use case. Feel free to close the issue : the current situation is very usable thanks to your nice generated types 👍
The text was updated successfully, but these errors were encountered: