Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-native): support direct uploads through FormData #307

Merged
merged 4 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"./lib/tools/getFormData.node.js": "./lib/tools/getFormData.browser.js",
"./lib/tools/sockets.node.js": "./lib/tools/sockets.browser.js"
},
"react-native": {
"./lib/request/request.node.js": "./lib/request/request.browser.js",
"./lib/tools/getFormData.node.js": "./lib/tools/getFormData.react-native.js",
"./lib/tools/sockets.node.js": "./lib/tools/sockets.browser.js"
},
"scripts": {
"check-env-vars": "node ./checkvars.js",
"mock:start": "ts-node --project ./mock-server/tsconfig.mock.json ./mock-server/server.ts --silent",
Expand Down Expand Up @@ -52,9 +57,9 @@
"@types/koa": "2.11.4",
"@types/node": "12.12.67",
"@types/promise": "7.1.30",
"@types/ws": "7.2.7",
"@typescript-eslint/eslint-plugin": "2.34.0",
"@typescript-eslint/parser": "2.34.0",
"@types/ws": "7.2.7",
"chalk": "4.1.0",
"data-uri-to-buffer": "3.0.1",
"dataurl-to-blob": "0.0.1",
Expand Down
18 changes: 10 additions & 8 deletions src/tools/buildFormData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getFormData from './getFormData.node'
import getFormData, { transformFile } from './getFormData.node'

import * as NodeFormData from 'form-data'
import { BrowserFile, NodeFile } from '../request/types'
Expand All @@ -9,24 +9,26 @@ import { BrowserFile, NodeFile } from '../request/types'
* in browsers.
*/

type FileTuple = ['file', BrowserFile | NodeFile, string]
type FileTriple = ['file', BrowserFile | NodeFile, string]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo?
FileTripple => FileTuple?

isFileTriple(tuple)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FilippovAM

This is not typo, triple is 3-tuple. I thought it was better to call this type triple, but now I think it's better to left it as it was. So I reverted it back.

type BaseType = string | number | void
type FormDataTuple = [string, BaseType | BaseType[]]

const isFileTriple = (tuple: FormDataTuple | FileTriple): tuple is FileTriple =>
tuple[0] === 'file'

function buildFormData(
body: (FormDataTuple | FileTuple)[]
body: (FormDataTuple | FileTriple)[]
): FormData | NodeFormData {
const formData = getFormData()

const isTriple = (tuple: FormDataTuple | FileTuple): tuple is FileTuple =>
tuple[0] === 'file'

for (const tuple of body) {
if (Array.isArray(tuple[1])) {
// refactor this
tuple[1].forEach(val => val && formData.append(tuple[0] + '[]', `${val}`))
} else if (isTriple(tuple)) {
formData.append(tuple[0], tuple[1] as Blob, tuple[2])
} else if (isFileTriple(tuple)) {
const name = tuple[2]
const file = transformFile(tuple[1], name) as Blob // lgtm[js/superfluous-trailing-arguments]
formData.append(tuple[0], file, name)
} else if (tuple[1] != null) {
formData.append(tuple[0], `${tuple[1]}`)
}
Expand Down
4 changes: 4 additions & 0 deletions src/tools/getFormData.browser.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import { FileTransformer } from './types'
import { identity } from './identity'

export const transformFile: FileTransformer = identity
export default (): FormData => new FormData()
3 changes: 3 additions & 0 deletions src/tools/getFormData.node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as NodeFormData from 'form-data'
import { FileTransformer } from './types'
import { identity } from './identity'

export const transformFile: FileTransformer = identity
export default (): NodeFormData | FormData => new NodeFormData()
16 changes: 16 additions & 0 deletions src/tools/getFormData.react-native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BrowserFile, NodeFile } from '../request/types'
import { FileTransformer, ReactNativeAsset } from './types'

export const transformFile: FileTransformer = (
file: BrowserFile | NodeFile,
name: string
): ReactNativeAsset => {
if (!file) {
return file
}
const uri = URL.createObjectURL(file)
const type = (file as BrowserFile).type
return { uri, name, type }
}

export default (): FormData => new FormData()
3 changes: 3 additions & 0 deletions src/tools/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function identity<T>(obj: T): T {
return obj
}
8 changes: 8 additions & 0 deletions src/tools/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BrowserFile, NodeFile } from '../request/types'

export type ReactNativeAsset = { name?: string; type?: string; uri: string }

export type FileTransformer = (
file: NodeFile | BrowserFile,
name: string
) => NodeFile | BrowserFile | ReactNativeAsset