Skip to content

Commit

Permalink
Merge pull request #8 from bytewitchcraft/feature/react-native-support
Browse files Browse the repository at this point in the history
Feature/react native support
  • Loading branch information
ihorkatkov authored May 31, 2018
2 parents 2f0e87a + 5cab64f commit 72e4bbd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"presets": ["env"],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
}
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "apollo-absinthe-upload-link",
"version": "1.0.3",
"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",
"author": "Ihor Katkov",
"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": {
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export const createUploadMiddleware = ({ uri, headers }) =>

export const createLink = opts =>
concat(createUploadMiddleware(opts), new HttpLink(opts))

export { ReactNativeFile } from './validators'
49 changes: 48 additions & 1 deletion src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 72e4bbd

Please sign in to comment.