ardatan / whatwg-node

Helper packages to create platform agnostic applications and libraries without worrying about the lack of WHATWG support in Node.js
MIT License
155 stars 32 forks source link

PonyfillBlob and nestjs class transformer package #1337

Closed kanopa closed 1 month ago

kanopa commented 3 months ago

Describe the bug

I am working on a NestJS (fastify) project that uses a schema-first approach for GraphQL and I want to implement a file upload feature. For DTO validation, I am using class-validator in combination with class-transformer, and I am applying a global validation pipe via NestJS's ValidationPipe from the @nestjs/common module.

I am encountering an issue when handling file uploads. Specifically, I receive a TypeError: Cannot read properties of undefined (reading 'length') related to PonyfillBlob. This error occurs because class-transformer attempts to create an instance of the class and encounters undefined properties, which leads to this error.

To Reproduce Steps to reproduce the behavior:

I create a demo repository with problem https://github.com/kanopa/yoga-nests-upload-issue

Maybe its better to type blobParts as nullable (private blobParts?: BlobPart[]) and add nullable to condition on 51 line?

if (blobParts?.length === 1 && isBlob(blobParts[0])) {
      return blobParts[0] as PonyfillBlob;
    }
ardatan commented 3 months ago

Thanks for the reproduction but could you give instructions for the reproduction?

kanopa commented 3 months ago

Thanks for the reproduction but could you give instructions for the reproduction?

Of course, i create repo with reproduction. Call mutation filesUpload with files as parameters, you can use any files for your choise :) Server starts on localhost:3000/graphql I prefer using altair gql client for test upload mutations

Screenshot 2024-06-03 at 11 56 10 Screenshot 2024-06-03 at 11 56 23
ardatan commented 1 month ago

Fixed in https://github.com/ardatan/whatwg-node/commit/d26157339f3896509e45cb5c73aa61164d262d56