Closed ninthsun91 closed 2 months ago
Confirmed this. Using fastify with multipart is not working for me either. Here is my error
[Nest] 36944 - 04/28/2024, 12:44:08 AM ERROR [ExceptionsHandler] File is not defined
ReferenceError: File is not defined
at string (project/node_modules/.pnpm/typia@6.0.3_typescript@5.4.2/node_modules/typia/src/functional/$FormDataReader/$FormDataReader.ts:43:20)
at decode (project/dist/modules/example/example.controller.js:124:26)
at assert (project/dist/modules/example/example.controller.js:150:31)
at project/node_modules/.pnpm/@nestia+core@3.0.5_@nestia+fetcher@3.0.5_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_33ze35cl3fyddlgbtw33ly2oz4/node_modules/@nestia/core/src/decorators/internal/validate_request_form_data.ts:32:14
at project/node_modules/.pnpm/@nestia+core@3.0.5_@nestia+fetcher@3.0.5_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_33ze35cl3fyddlgbtw33ly2oz4/node_modules/@nestia/core/src/decorators/TypedFormData.ts:101:33
at step (project/node_modules/.pnpm/@nestia+core@3.0.5_@nestia+fetcher@3.0.5_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_33ze35cl3fyddlgbtw33ly2oz4/node_modules/@nestia/core/lib/decorators/TypedFormData.js:44:23)
at Object.next (project/node_modules/.pnpm/@nestia+core@3.0.5_@nestia+fetcher@3.0.5_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_33ze35cl3fyddlgbtw33ly2oz4/node_modules/@nestia/core/lib/decorators/TypedFormData.js:25:53)
at fulfilled (project/node_modules/.pnpm/@nestia+core@3.0.5_@nestia+fetcher@3.0.5_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_33ze35cl3fyddlgbtw33ly2oz4/node_modules/@nestia/core/lib/decorators/TypedFormData.js:16:58)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
Currently my workaround is just creating a param decorator to assert the request body
export function FormData(assert: (data: unknown) => void): ParameterDecorator {
return createParamDecorator((_: any, ctx: ExecutionContext) => {
const req = ctx.switchToHttp().getRequest() as FastifyRequest
try {
assert(req.body)
return req.body
} catch (error) {
if (error instanceof TypeGuardError) throw new BadRequestException({
path: error.path,
reason: `Error on ${error.method}(): invalid type on ${error.path}, expect to be ${error.expected}`,
expected: error.expected,
message: "Request body data is not following the promised type."
})
}
})()
}
Usage
export interface FormDataExample {
foo: string
}
@Post('form-data')
@Upload('image') // wrapper decorator using fastify multer
formData(
@FormData(data => typia.assert<FormDataExample>(data))
payload: FormDataExample,
@UploadedFile() file: File,
) {
return {
...file,
...payload
}
}
@ninthsun91 @thoriqadillah Thanks for bug reporting.
Now, upgrade to v3.1.2, then this bug be fixed.
After updating , I still got the same error @samchon
[Nest] 27665 - 05/05/2024, 7:53:25 AM ERROR [ExceptionsHandler] File is not defined
ReferenceError: File is not defined
at string (project/node_modules/.pnpm/typia@6.0.3_typescript@5.4.2/node_modules/typia/src/functional/$FormDataReader/$FormDataReader.ts:43:20)
at decode (project/dist/modules/example/example.controller.js:435:26)
at assert (project/dist/modules/example/example.controller.js:461:31)
at project/node_modules/.pnpm/@nestia+core@3.1.2_@nestia+fetcher@3.1.2_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_z7bpgokcv72kwtpynnfygo76ru/node_modules/@nestia/core/src/decorators/internal/validate_request_form_data.ts:32:14
at project/node_modules/.pnpm/@nestia+core@3.1.2_@nestia+fetcher@3.1.2_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_z7bpgokcv72kwtpynnfygo76ru/node_modules/@nestia/core/src/decorators/TypedFormData.ts:101:33
at Generator.next (<anonymous>)
at fulfilled (project/node_modules/.pnpm/@nestia+core@3.1.2_@nestia+fetcher@3.1.2_@nestjs+common@10.3.8_@nestjs+core@10.3.8_reflect-me_z7bpgokcv72kwtpynnfygo76ru/node_modules/@nestia/core/lib/decorators/TypedFormData.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
What i did:
@nestia/sdk
, @nestia/core
, and @nestia/fetcher
to 3.1.2 and then pnpm i
. Did not worknpx nestia setup --manager pnpm
and tried the endpoint again, still no luckpnpm.lock
and node_modules
. After that, run the pnpm i
and then npx nestia setup --manager pnpm
and tried the endpoint again, still no luckexport interface FormDataExample {
foo: string
}
@TypedRoute.Post('form-data')
@Upload('image')
formData(
@TypedFormData.Body() payload: FormDataExample,
@UploadedFile() file: File,
) {
return {
...file,
...payload
}
}
Check your node version. File is not defined means that your NodeJS does not have the File class.
Upgrading to version 20.12.2 (LTS) solves the problem. Thanks
Hi, thanks for updating fastify supports with
@TypedFormData
.However, I don't think this is working well. Nestia is throwing error while validating request body.
Here's the minimal reproduction. You can also clone the code from here.
I confirmed that the same request works with nestjs express platform or vanilla fastify with
@fastify/multipart
plugin registered. So, I hardly doubt the request is not valid for fastify.