Socket
Socket
Sign inDemoInstall

extract-files

Package Overview
Dependencies
0
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    extract-files

Clones a value, recursively extracting File, Blob and ReactNativeFile instances with their object paths, replacing them with null. FileList instances are treated as File instance arrays.


Version published
Weekly downloads
4M
decreased by-5.77%
Maintainers
1
Install size
26.3 kB
Created
Weekly downloads
 

Package description

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

Changelog

Source

9.0.0

Major

  • Updated Node.js support to ^10.17.0 || ^12.0.0 || >= 13.7.0.
  • Updated dev dependencies, some of which require newer Node.js versions than previously supported.
  • Renamed the lib directory to public; existing deep import or require paths must be updated.
  • Removed the package module field.

Patch

  • Removed Node.js v13 and added v14 to the versions tested in GitHub Actions.
  • Simplified the GitHub Actions CI config with the npm install-test command.
  • Improved JSDoc code examples.
  • Updated EditorConfig.
  • No longer transpile tests with Babel, or test ESM.
  • Simplified the Babel config and scripts, ensuring .js files are parsed as scripts.
  • Removed unnecessary .js file extensions from require paths.
  • Documented all the ways to import and require the public API.
  • Tweaked the readme setup instructions.

Readme

Source

extract-files

npm version CI status

Clones a value, recursively extracting File, Blob and ReactNativeFile instances with their object paths, replacing them with null. FileList instances are treated as File instance arrays.

Used by GraphQL multipart request spec client implementations such as graphql-react and apollo-upload-client.

Setup

Install with npm:

npm install extract-files

See the extractFiles documentation to get started.

Support

API

Table of contents

class ReactNativeFile

Used to mark a React Native File substitute in an object tree for extractFiles. It’s too risky to assume all objects with uri, type and name properties are files to extract.

ParameterTypeDescription
fileReactNativeFileSubstituteA React Native File substitute.
Examples

Ways to import.

import { ReactNativeFile } from 'extract-files';
import ReactNativeFile from 'extract-files/public/ReactNativeFile.js';

Ways to require.

const { ReactNativeFile } = require('extract-files');
const ReactNativeFile = require('extract-files/public/ReactNativeFile');

An extractable file in React Native.

import { ReactNativeFile } from 'extract-files';

const file = new ReactNativeFile({
  uri: uriFromCameraRoll,
  name: 'a.jpg',
  type: 'image/jpeg',
});

function extractFiles

Clones a value, recursively extracting File, Blob and ReactNativeFile instances with their object paths, replacing them with null. FileList instances are treated as File instance arrays.

ParameterTypeDescription
value*Value (typically an object tree) to extract files from.
pathObjectPath? = ''Prefix for object paths for extracted files.
isExtractableFileExtractableFileMatcher? = isExtractableFileThe function used to identify extractable files.

Returns: ExtractFilesResult — Result.

Examples

Ways to import.

import { extractFiles } from 'extract-files';
import extractFiles from 'extract-files/public/extractFiles.js';

Ways to require.

const { extractFiles } = require('extract-files');
const extractFiles = require('extract-files/public/extractFiles');

Extract files from an object.

For the following:

import { extractFiles } from 'extract-files';

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, 'prefix');

value remains the same.

clone is:

{
  "a": null,
  "b": [null, null]
}

files is a Map instance containing:

KeyValue
file1['prefix.a', 'prefix.b.0']
file2['prefix.b.1']

function isExtractableFile

Checks if a value is an extractable file.

Type: ExtractableFileMatcher

ParameterTypeDescription
value*Value to check.

Returns: boolean — Is the value an extractable file.

Examples

Ways to import.

import { isExtractableFile } from 'extract-files';
import isExtractableFile from 'extract-files/public/isExtractableFile.js';

Ways to require.

const { isExtractableFile } = require('extract-files');
const isExtractableFile = require('extract-files/public/isExtractableFile');

type ExtractableFile

An extractable file.

Type: File | Blob | ReactNativeFile


type ExtractableFileMatcher

A function that checks if a value is an extractable file.

Type: Function

ParameterTypeDescription
value*Value to check.

Returns: boolean — Is the value an extractable file.

See
Examples

How to check for the default exactable files, as well as a custom type of file.

import { isExtractableFile } from 'extract-files';

const isExtractableFileEnhanced = (value) =>
  isExtractableFile(value) ||
  (typeof CustomFile !== 'undefined' && value instanceof CustomFile);

type ExtractFilesResult

What extractFiles returns.

Type: object

PropertyTypeDescription
clone*Clone of the original input value with files recursively replaced with null.
filesMap<ExtractableFile, Array<ObjectPath>>Extracted files and their locations within the original value.

type ObjectPath

String notation for the path to a node in an object tree.

Type: string

See
Examples

Object path is property a, array index 0, object property b.

a.0.b

type ReactNativeFileSubstitute

A React Native File substitute for when using FormData.

Type: object

PropertyTypeDescription
uristringFilesystem path.
namestring?File name.
typestring?File content type.
See
Examples

A camera roll file.

const fileSubstitute = {
  uri: uriFromCameraRoll,
  name: 'a.jpg',
  type: 'image/jpeg',
};

Keywords

FAQs

Last updated on 22 Jul 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc