diff --git a/packages/formidable/src/ExtendedFormData.ts b/packages/formidable/src/ExtendedFormData.ts index 7ed336c..91b448c 100644 --- a/packages/formidable/src/ExtendedFormData.ts +++ b/packages/formidable/src/ExtendedFormData.ts @@ -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(); #validateFileType(value: FormDataValue) { - if (typeof value != 'string' && !(value instanceof VolatileFile)) { + if (typeof value != 'string' && !ExtendedFormData.isFormidableFile(value)) { return String(value); } return value; @@ -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 { @@ -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; + } } diff --git a/packages/formidable/src/index.ts b/packages/formidable/src/index.ts index d0ff0f2..2595aec 100644 --- a/packages/formidable/src/index.ts +++ b/packages/formidable/src/index.ts @@ -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 }; @@ -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, +};