What is extract-files?
The extract-files package is designed to extract files from a JavaScript object tree so they can be uploaded via a multipart request. It is commonly used in applications that need to handle file uploads, especially in the context of GraphQL operations.
What are extract-files's main functionalities?
Extract files from an object
This feature allows you to extract files from an object, which is useful when preparing files for upload. The `extractFiles` function takes an object and a path to traverse within the object to find files. It returns an object with the extracted files and a clone of the original object with files replaced by null.
{"operation": "const { extractFiles } = require('extract-files');\nconst file = new File(['content'], 'file.txt', { type: 'text/plain' });\nconst operation = { variables: { file } };\nconst { files, clone } = extractFiles(operation, 'variables');"}
Support for FileList and Map objects
The package can handle `FileList` objects, which are typically obtained from file input elements, and `Map` objects. It can extract files from these complex structures, making it versatile for various file upload scenarios.
{"operation": "const { extractFiles } = require('extract-files');\nconst fileList = document.querySelector('input[type=file]').files;\nconst operation = { variables: { files: fileList } };\nconst { files, clone } = extractFiles(operation, 'variables');"}
Other packages similar to extract-files
form-data
The form-data package allows you to create `multipart/form-data` streams to submit files and values via HTTP. It can be used to simulate a form submission with file uploads, similar to extract-files, but it is more focused on constructing the form data itself rather than extracting files from an existing object structure.
busboy
Busboy is a Node.js module for parsing incoming HTML form data, including file uploads. It differs from extract-files in that it is used on the server side to process file uploads, whereas extract-files is typically used on the client side to prepare files for upload.
multer
Multer is a Node.js middleware for handling `multipart/form-data`, primarily used for uploading files. It is similar to busboy but is designed to be used with Express applications. Unlike extract-files, multer is not about extracting files from an object but rather about handling file uploads on the server side.
A function to recursively extract files and their object paths within a value, replacing them with null
in a deep clone without mutating the original value. FileList
instances are treated as File
instance arrays. Files are typically File
and Blob
instances.
Used by GraphQL multipart request spec client implementations such as graphql-react
and apollo-upload-client
.
Installation
For Node.js, to install extract-files
with npm, run:
npm install extract-files
For Deno and browsers, an example import map:
{
"imports": {
"extract-files/": "https://unpkg.com/extract-files@13.0.0/",
"is-plain-obj": "https://unpkg.com/is-plain-obj@4.1.0/index.js",
"is-plain-obj/": "https://unpkg.com/is-plain-obj@4.1.0/"
}
}
See the function extractFiles
to get started.
Requirements
Supported runtime environments:
Non Deno projects must configure TypeScript to use types from the ECMAScript modules that have a // @ts-check
comment:
Exports
The npm package extract-files
features optimal JavaScript module design. It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via the package.json
field exports
: