@@ -143,7 +143,7 @@ const app = new Elysia()
143
143
)
144
144
. get ( '/formdata' , ( ) =>
145
145
form ( {
146
- image : Bun . file ( './test/kyuukurarin.mp4' )
146
+ image : Bun . file ( './test/public/ kyuukurarin.mp4' )
147
147
} )
148
148
)
149
149
. ws ( '/json-serialization-deserialization' , {
@@ -171,6 +171,22 @@ const app = new Elysia()
171
171
return 'a'
172
172
} )
173
173
. get ( '/id/:id?' , ( { params : { id = 'unknown' } } ) => id )
174
+ . post (
175
+ '/files' ,
176
+ ( { body : { files } } ) => files . map ( ( file ) => file . name ) ,
177
+ {
178
+ body : t . Object ( {
179
+ files : t . Files ( )
180
+ } )
181
+ }
182
+ )
183
+ . post (
184
+ '/file' ,
185
+ ( { body : { file } } ) => file . name , {
186
+ body : t . Object ( {
187
+ file : t . File ( )
188
+ } )
189
+ } )
174
190
175
191
const client = treaty ( app )
176
192
@@ -645,3 +661,122 @@ describe('Treaty2 - Using endpoint URL', () => {
645
661
} )
646
662
} )
647
663
} )
664
+
665
+
666
+ describe ( 'Treaty2 - Using t.File() and t.Files() from server' , async ( ) => {
667
+ const filePath1 = `${ import . meta. dir } /public/aris-yuzu.jpg`
668
+ const filePath2 = `${ import . meta. dir } /public/midori.png`
669
+ const filePath3 = `${ import . meta. dir } /public/kyuukurarin.mp4`
670
+
671
+ const bunFile1 = Bun . file ( filePath1 )
672
+ const bunFile2 = Bun . file ( filePath2 )
673
+ const bunFile3 = Bun . file ( filePath3 )
674
+
675
+ const file1 = new File ( [ await bunFile1 . arrayBuffer ( ) ] , 'cumin.webp' , {
676
+ type : 'image/webp'
677
+ } )
678
+ const file2 = new File ( [ await bunFile2 . arrayBuffer ( ) ] , 'curcuma.jpg' , {
679
+ type : 'image/jpeg'
680
+ } )
681
+ const file3 = new File ( [ await bunFile3 . arrayBuffer ( ) ] , 'kyuukurarin.mp4' , {
682
+ type : 'video/mp4'
683
+ } )
684
+
685
+ const filesForm = new FormData ( )
686
+ filesForm . append ( 'files' , file1 )
687
+ filesForm . append ( 'files' , file2 )
688
+ filesForm . append ( 'files' , file3 )
689
+
690
+ const bunFilesForm = new FormData ( )
691
+ bunFilesForm . append ( 'files' , bunFile1 )
692
+ bunFilesForm . append ( 'files' , bunFile2 )
693
+ bunFilesForm . append ( 'files' , bunFile3 )
694
+
695
+ it ( 'accept a single Bun.file' , async ( ) => {
696
+ const { data : files } = await client . files . post ( {
697
+ files : bunFile1 as unknown as FileList
698
+ } )
699
+
700
+ expect ( files ) . not . toBeNull ( )
701
+ expect ( files ) . not . toBeUndefined ( )
702
+ expect ( files ) . toEqual ( [ bunFile1 . name ! ] )
703
+
704
+ const { data : filesbis } = await client . files . post ( {
705
+ files : [ bunFile1 ] as unknown as FileList
706
+ } )
707
+
708
+ expect ( filesbis ) . not . toBeNull ( )
709
+ expect ( filesbis ) . not . toBeUndefined ( )
710
+ expect ( filesbis ) . toEqual ( [ bunFile1 . name ! ] )
711
+
712
+ const { data : file } = await client . file . post ( {
713
+ file : bunFile1 as unknown as File
714
+ } )
715
+
716
+ expect ( file ) . not . toBeNull ( )
717
+ expect ( file ) . not . toBeUndefined ( )
718
+ expect ( file ) . toEqual ( bunFile1 . name ! )
719
+ } )
720
+
721
+ it ( 'accept a single regular file' , async ( ) => {
722
+ const { data : files } = await client . files . post ( {
723
+ files : file1 as unknown as FileList
724
+ } )
725
+
726
+ expect ( files ) . not . toBeNull ( )
727
+ expect ( files ) . not . toBeUndefined ( )
728
+ expect ( files ) . toEqual ( [ file1 . name ! ] )
729
+
730
+ const { data : filesbis } = await client . files . post ( {
731
+ files : [ file1 ] as unknown as FileList
732
+ } )
733
+
734
+ expect ( filesbis ) . not . toBeNull ( )
735
+ expect ( filesbis ) . not . toBeUndefined ( )
736
+ expect ( filesbis ) . toEqual ( [ file1 . name ! ] )
737
+
738
+ const { data : file } = await client . file . post ( {
739
+ file : file1 as unknown as File
740
+ } )
741
+
742
+ expect ( file ) . not . toBeNull ( )
743
+ expect ( file ) . not . toBeUndefined ( )
744
+ expect ( file ) . toEqual ( file1 . name ! )
745
+ } )
746
+
747
+ it ( 'accept an array of multiple Bun.file' , async ( ) => {
748
+ const { data : files } = await client . files . post ( {
749
+ files : [ bunFile1 , bunFile2 , bunFile3 ] as unknown as FileList
750
+ } )
751
+
752
+ expect ( files ) . not . toBeNull ( )
753
+ expect ( files ) . not . toBeUndefined ( )
754
+ expect ( files ) . toEqual ( [ bunFile1 . name ! , bunFile2 . name ! , bunFile3 . name ! ] )
755
+
756
+ const { data : filesbis } = await client . files . post ( {
757
+ files : bunFilesForm . getAll ( 'files' ) as unknown as FileList
758
+ } )
759
+
760
+ expect ( filesbis ) . not . toBeNull ( )
761
+ expect ( filesbis ) . not . toBeUndefined ( )
762
+ expect ( filesbis ) . toEqual ( [ bunFile1 . name ! , bunFile2 . name ! , bunFile3 . name ! ] )
763
+ } )
764
+
765
+ it ( 'accept an array of multiple regular file' , async ( ) => {
766
+ const { data : files } = await client . files . post ( {
767
+ files : [ file1 , file2 , file3 ] as unknown as FileList
768
+ } )
769
+
770
+ expect ( files ) . not . toBeNull ( )
771
+ expect ( files ) . not . toBeUndefined ( )
772
+ expect ( files ) . toEqual ( [ file1 . name ! , file2 . name ! , file3 . name ! ] )
773
+
774
+ const { data : filesbis } = await client . files . post ( {
775
+ files : filesForm . getAll ( 'files' ) as unknown as FileList
776
+ } )
777
+
778
+ expect ( filesbis ) . not . toBeNull ( )
779
+ expect ( filesbis ) . not . toBeUndefined ( )
780
+ expect ( filesbis ) . toEqual ( [ file1 . name ! , file2 . name ! , file3 . name ! ] )
781
+ } )
782
+ } )
0 commit comments