From c66e1dafd84c87981de11c516e7ab34bd63b7847 Mon Sep 17 00:00:00 2001 From: ikatkov Date: Wed, 14 Feb 2018 20:20:44 +0200 Subject: [PATCH 1/2] v1.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 043da2a..0d608e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-absinthe-upload-link", - "version": "1.0.3", + "version": "1.0.4", "description": "A network interface for Apollo that enables file-uploading to Absinthe back ends.", "main": "lib/index.js", "repository": "https://github.com/bytewitchcraft/apollo-absinthe-upload-link", From 6a2bd1ba13109a8408ee05a665f55a26747ce335 Mon Sep 17 00:00:00 2001 From: ikatkov Date: Thu, 31 May 2018 21:10:31 +0300 Subject: [PATCH 2/2] added react native support deleted jest files from publish --- .babelrc | 1 + README.md | 26 ++++++++++++++++++++++++- package.json | 5 +++-- src/index.js | 2 ++ src/validators.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++- yarn.lock | 13 +++++++++++++ 6 files changed, 92 insertions(+), 4 deletions(-) diff --git a/.babelrc b/.babelrc index 91f35b2..fb29bd8 100644 --- a/.babelrc +++ b/.babelrc @@ -1,6 +1,7 @@ { "presets": ["env"], "plugins": [ + "transform-class-properties", "transform-object-rest-spread" ] } diff --git a/README.md b/README.md index ae283c3..447d8a6 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,30 @@ const client = new ApolloClient({ ### Usage with React Native -Not tested. Contributing is welcome. +Substitute [`File`](https://developer.mozilla.org/en/docs/Web/API/File) with [`ReactNativeFile`](https://github.com/bytewitchcraft/apollo-absinthe-upload-link/blob/master/src/validators.js): + +```js +import { ReactNativeFile } from 'apollo-absinthe-upload-link' + +const file = new ReactNativeFile({ + uri: '…', + type: 'image/jpeg', + name: 'photo.jpg' +}) + +const files = ReactNativeFile.list([ + { + uri: '…', + type: 'image/jpeg', + name: 'photo-1.jpg' + }, + { + uri: '…', + type: 'image/jpeg', + name: 'photo-2.jpg' + } +]) +``` ## License @@ -50,3 +73,4 @@ MIT (see LICENSE) ## Acknowledgements * @labtwentyfive's [`apollo-absinthe-upload-client`](https://github.com/labtwentyfive/apollo-absinthe-upload-client) +* @jaydenseric [`apollo-upload-client`](https://github.com/jaydenseric/apollo-upload-client) diff --git a/package.json b/package.json index 0d608e5..37b89e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-absinthe-upload-link", - "version": "1.0.4", + "version": "1.1.0", "description": "A network interface for Apollo that enables file-uploading to Absinthe back ends.", "main": "lib/index.js", "repository": "https://github.com/bytewitchcraft/apollo-absinthe-upload-link", @@ -8,7 +8,7 @@ "license": "MIT", "scripts": { "test": "jest", - "compile": "yarn rimraf lib/* && yarn babel src -d lib", + "compile": "yarn rimraf lib/* && yarn babel src -d lib && yarn rimraf lib/__tests__ && yarn rimraf lib/__mocks__", "prepublish": "npm run compile" }, "dependencies": { @@ -24,6 +24,7 @@ "babel-core": "^6.26.0", "babel-eslint": "^8.2.1", "babel-jest": "^22.2.0", + "babel-plugin-transform-class-properties": "^6.24.1", "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-preset-env": "^1.6.1", "eslint": "^4.14.0", diff --git a/src/index.js b/src/index.js index 2c620a1..8758c1f 100644 --- a/src/index.js +++ b/src/index.js @@ -31,3 +31,5 @@ export const createUploadMiddleware = ({ uri, headers }) => export const createLink = opts => concat(createUploadMiddleware(opts), new HttpLink(opts)) + +export { ReactNativeFile } from './validators' diff --git a/src/validators.js b/src/validators.js index 532a2f1..8905404 100644 --- a/src/validators.js +++ b/src/validators.js @@ -3,4 +3,51 @@ export const isFileList = value => typeof FileList !== 'undefined' && value instanceof FileList export const isUploadFile = value => - typeof File !== 'undefined' && value instanceof File + (typeof File !== 'undefined' && value instanceof File) || + value instanceof ReactNativeFile + +/** + * A React Native FormData file object. + * @see {@link https://github.com/facebook/react-native/blob/v0.45.1/Libraries/Network/FormData.js#L34} + * @typedef {Object} ReactNativeFileObject + * @property {String} uri - File system path. + * @property {String} [type] - File content type. + * @property {String} [name] - File name. + */ + +/** + * A React Native file. + */ +export class ReactNativeFile { + /** + * Constructs a new file. + * @param {ReactNativeFileObject} file + * @example + * const file = new ReactNativeFile({ + * uri: uriFromCameraRoll, + * type: 'image/jpeg', + * name: 'photo.jpg' + * }) + */ + constructor({ uri, type, name }) { + this.uri = uri + this.type = type + this.name = name + } + + /** + * Creates an array of file instances. + * @param {ReactNativeFileObject[]} files + * @example + * const files = ReactNativeFile.list([{ + * uri: uriFromCameraRoll1, + * type: 'image/jpeg', + * name: 'photo-1.jpg' + * }, { + * uri: uriFromCameraRoll2, + * type: 'image/jpeg', + * name: 'photo-2.jpg' + * }]) + */ + static list = files => files.map(file => new ReactNativeFile(file)) +} diff --git a/yarn.lock b/yarn.lock index 2e3f195..91b6102 100644 --- a/yarn.lock +++ b/yarn.lock @@ -525,6 +525,10 @@ babel-plugin-syntax-async-functions@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95" +babel-plugin-syntax-class-properties@^6.8.0: + version "6.13.0" + resolved "https://registry.yarnpkg.com/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz#d7eb23b79a317f8543962c505b827c7d6cac27de" + babel-plugin-syntax-exponentiation-operator@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de" @@ -545,6 +549,15 @@ babel-plugin-transform-async-to-generator@^6.22.0: babel-plugin-syntax-async-functions "^6.8.0" babel-runtime "^6.22.0" +babel-plugin-transform-class-properties@^6.24.1: + version "6.24.1" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-class-properties/-/babel-plugin-transform-class-properties-6.24.1.tgz#6a79763ea61d33d36f37b611aa9def81a81b46ac" + dependencies: + babel-helper-function-name "^6.24.1" + babel-plugin-syntax-class-properties "^6.8.0" + babel-runtime "^6.22.0" + babel-template "^6.24.1" + babel-plugin-transform-es2015-arrow-functions@^6.22.0: version "6.22.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-arrow-functions/-/babel-plugin-transform-es2015-arrow-functions-6.22.0.tgz#452692cb711d5f79dc7f85e440ce41b9f244d221"