File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,11 +38,15 @@ import { FileUpload } from "genql-upload";
3838import fs from " fs" ;
3939
4040async function upload() {
41- // client is setup before
41+ // read stream is valid file input
42+ const file1 = new FileUpload (fs .createReadStream (" ./README.md" ));
43+
44+ // but file can also be Buffer
45+ const file2 = new FileUpload (Buffer .from (/* ... */ ), " filename.txt" );
4246
4347 const response = await client .chain .mutation
4448 .singleUpload ({
45- file: new FileUpload ( fs . createReadStream ( " ./README.md " )),
49+ file: file1 , // file2
4650 })
4751 .get ({
4852 filename: true ,
Original file line number Diff line number Diff line change @@ -2,7 +2,13 @@ import { ReactNativeFile } from "extract-files";
22import type { ReadStream } from 'fs' ;
33
44export class FileUpload extends ReactNativeFile {
5- constructor ( private data : ReadStream ) {
6- super ( { uri : '' , name : String ( data . path ) } )
5+ constructor ( public data : ReadStream | Buffer , filename ?: string ) {
6+ super ( { uri : '' } )
7+
8+ if ( data instanceof Buffer ) {
9+ this . name = filename
10+ } else {
11+ this . name = filename || String ( data . path )
12+ }
713 }
814}
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ export function createFetcher(opts: GenqlUploadOptions) {
4444 // 3. all files not (same index as in map)
4545 let j = 0 ;
4646 for ( const [ file ] of files ) {
47- formData . append ( `${ j ++ } ` , file . data )
47+ formData . append ( `${ j ++ } ` , file . data , file . name ) ;
4848 }
4949
5050 // normal fetch
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ afterAll(async () => {
2626 }
2727} )
2828
29- test ( 'Test using express ' , async ( ) => {
29+ test ( 'Test with createReadStream ' , async ( ) => {
3030
3131 const client = createClient ( {
3232 fetcher : createFetcher ( {
@@ -56,3 +56,36 @@ test('Test using express', async () => {
5656 // expect this to not throw
5757 await fsp . unlink ( 'local-file-output.txt' )
5858} )
59+
60+ test ( 'Test with Buffer' , async ( ) => {
61+
62+ const client = createClient ( {
63+ fetcher : createFetcher ( {
64+ url : serverUrl ,
65+ headers : async ( ) => {
66+ return {
67+ 'X-Test' : 'test'
68+ }
69+ }
70+ } )
71+ } )
72+
73+ const fileBuffer = fs . readFileSync ( './SECURITY.md' )
74+
75+ const f = await client . chain . mutation . singleUpload ( {
76+ file : new FileUpload ( fileBuffer , 'SECURITY.md' )
77+ } ) . get ( {
78+ filename : true ,
79+ mimetype : true ,
80+ headers : true
81+ } )
82+
83+ expect ( f . filename ) . toBe ( 'SECURITY.md' )
84+ expect ( f . mimetype ) . toBe ( 'text/markdown' )
85+
86+ const headers = JSON . parse ( f . headers )
87+ expect ( headers [ 'x-test' ] ) . toBe ( 'test' )
88+
89+ // expect this to not throw
90+ await fsp . unlink ( 'local-file-output.txt' )
91+ } )
You can’t perform that action at this time.
0 commit comments