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
.
To install with npm, run:
npm install extract-files
See the documentation for the function extractFiles
to get started.
- Node.js:
^12.22.0 || ^14.17.0 || >= 16.0.0
- Browsers:
> 0.5%, not OperaMini all, not IE > 0, not dead
These ECMAScript modules are published to npm and exported via the package.json
exports
field:
Function extractFiles
— Recursively extracts 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.
Extractable
:any
— Extractable file type.
value
:unknown
— Value to extract files from. Typically an object tree.isExtractable
:(value: unknown) => value is Extractable
— Matches extractable files. TypicallyisExtractableFile
.path
?
:ObjectPath
— Prefix for object paths for extracted files. Defaults to""
.
Extraction
<Extractable
> — Extraction result.
Extracting files from an object.
For the following:
import extractFiles from "extract-files/extractFiles.mjs";
import isExtractableFile from "extract-files/isExtractableFile.mjs";
const file1 = new File(["1"], "1.txt", { type: "text/plain" });
const file2 = new File(["2"], "2.txt", { type: "text/plain" });
const value = {
a: file1,
b: [file1, file2],
};
const { clone, files } = extractFiles(value, isExtractableFile, "prefix");
value
remains the same.
clone
is:
{
"a": null,
"b": [null, null]
}
files
is a Map
instance containing:
Key | Value |
---|---|
file1 |
["prefix.a", "prefix.b.0"] |
file2 |
["prefix.b.1"] |
object
— An extraction result.
Extractable
?
:any
— Extractable file type. Defaults tounknown
.
clone
:unknown
— Clone of the original value with files recursively replaced withnull
.files
:Map
<Extractable
,Array
<ObjectPath
>> — Extracted files and their object paths within the original value.
string
— String notation for the path to a node in an object tree.
An object path for object property a
, array index 0
, object property b
:
a.0.b
Function isExtractableFile
— Checks if a value is an extractable file.
value
:unknown
— Value to check.
value is
ExtractableFile
— Is the value an extractable file.