JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1487
  • Score
    100M100P100Q121770F
  • License UNLICENSED

Implementation of @exodus/networking-spec for a React Native context

Package Exports

  • @exodus/networking-mobile
  • @exodus/networking-mobile/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@exodus/networking-mobile) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ci

@exodus/networking-mobile

This repository contains the implementation of the @exodus/networking-spec for React Native contexts.

Installing

yarn add @exodus/networking-mobile

Usage

The following sections will outline how to use the various modules that are part of this package.

@exodus/networking-mobile/http + form

The React Native fetch implementation does not support a spec compliant FormData object. In order to have an API that is similar to what we are used to from the Browser, this module ships with its own FormData implementation.

To use it, import it and add data as you're used to:

import { FormData } from '@exodus/networking-mobile/form'

const formData = new FormData()
formData.append('name', 'Bruce Wayne')

// set is also available
formData.set('name', 'Batman')

// ...and many more: delete, get, getAll, entries, keys, values

As opposed to the Browser implementation, the RN FormData is not just a data container, but also knows how to serialize itself. Therefore the FormData implementation shipping with this module (which follows the Browser spec), cannot directly be used with RN's fetch.

To overcome this limitation, use the HttpClient that is part of this module. It comes with a fetch function that has the same signature as RN's fetch but can handle our FormData.

import { FormData, File } from '@exodus/networking-mobile/form'
import { HttpClient } from '@exodus/networking-mobile/http'
import RNFS from '@exodus/react-native-fs'

const client = new HttpClient(RNFS, 'android', uuidv4)

const formData = new FormData()
formData.set('passport', new File(buffer, 'passport.jpg'))

await client.fetch('https://wayne.enterprises/upload', { method: 'POST', body: formData })

The client transforms FormData by writing files to the temporary folder, and appending the file URI, MIME type, and file name to an RN FormData object that is then passed to RN's fetch. Any temporarily created files are removed when the requests completes - successfully or otherwise.

Please note that the file used above is also provided by this module. It receives an object of type Buffer, a name, and optional properties to set the MIME type and last modified date. The MIME type takes no effect if used in conjunction with the HttpClient, since the client automatically derives the MIME type from the file extension.