Skip to content

Commit

Permalink
fix(formidable): parse files
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Aug 30, 2024
1 parent 19fb7f9 commit 6bb8c34
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
27 changes: 20 additions & 7 deletions packages/formidable/src/ExtendedFormData.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import {VolatileFile} from 'formidable';
import {VolatileFile, PersistentFile} from 'formidable';

export type FormFile = typeof PersistentFile & {
filepath: string;
lastModifiedDate: Date;
mimetype: string;
newFilename: string;
originalFilename: string;
size: number;
}

export type FormDataValue = string | typeof VolatileFile;
export type FormDataValue = string | FormFile;

export default class ExtendedFormData {
#data = new Map<string, FormDataValue[]>();

#validateFileType(value: FormDataValue) {
if (typeof value != 'string' && !(value instanceof VolatileFile)) {
if (typeof value != 'string' && !ExtendedFormData.isFormidableFile(value)) {
return String(value);
}
return value;
Expand Down Expand Up @@ -43,13 +52,13 @@ export default class ExtendedFormData {
return this.getAll(name).filter(value => typeof value == 'string') as string[];
}

getFile(name: string): typeof VolatileFile | null {
getFile(name: string): FormFile | null {
const value = this.get(name);
return value instanceof VolatileFile ? value as typeof VolatileFile : null;
return ExtendedFormData.isFormidableFile(value) ? value as FormFile : null;
}

getAllFiles(name: string): (typeof VolatileFile)[] {
return this.getAll(name).filter(value => value instanceof File) as (typeof VolatileFile)[];
getAllFiles(name: string): (FormFile)[] {
return this.getAll(name).filter(value => ExtendedFormData.isFormidableFile(value)) as FormFile[];
}

has(name: string): boolean {
Expand Down Expand Up @@ -81,4 +90,8 @@ export default class ExtendedFormData {
[Symbol.iterator](): IterableIterator<[string, FormDataValue[]]> {
return this.#data[Symbol.iterator]();
}

static isFormidableFile(object: any) {
return object instanceof VolatileFile || object instanceof PersistentFile;
}
}
20 changes: 14 additions & 6 deletions packages/formidable/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import formidable, {VolatileFile} from 'formidable';
import formidable, {VolatileFile, PersistentFile} from 'formidable';
import {EventEmitter} from 'node:events';
import ExtendedFormData, {FormDataValue} from './ExtendedFormData.js';
import ExtendedFormData, {FormDataValue, FormFile} from './ExtendedFormData.js';

class FormidableRequest extends EventEmitter {
headers: { [key: string]: string };
Expand Down Expand Up @@ -62,9 +62,17 @@ export default async function parseAstroForm(request: Request, options?: formida
return formData;
}

export function isFormidableFile(object: any) {
return object instanceof VolatileFile;
}
const isFormidableFile = ExtendedFormData.isFormidableFile;

export type {
FormFile,
FormDataValue
}

export {VolatileFile, FormDataValue, ExtendedFormData};
export {
parseAstroForm,
VolatileFile,
PersistentFile,
ExtendedFormData,
isFormidableFile,
};

0 comments on commit 6bb8c34

Please sign in to comment.