react-native/Libraries/Network
Adam Comella fc38fe1736 DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g
Summary:
This change intends to fix 2 issues with the NetInfo API:
  - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android.
  - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g.

The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes:
  - The `fetch` method is deprecated in favor of `getConnection`
  - The `change` event is deprecated in favor of the `connectionchange` event.
  - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values.
    - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same.
    - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did).

`getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change.

Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`.

**Test Plan (required)**

Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app.

Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/14618

Differential Revision: D5459593

Pulled By: shergin

fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-24 15:50:53 -07:00
..
RCTNetwork.xcodeproj Enable -Wimplicit-retain-self en sync warning config for all projects 2017-03-23 15:01:34 -07:00
__tests__ remove disableAutomock from jest tests (new default) @bypass-lint 2017-06-13 15:04:09 -07:00
FormData.js Network/FormData typeof object allowing null 2017-02-10 13:30:48 -08:00
NetInfo.js DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g 2017-07-24 15:50:53 -07:00
RCTDataRequestHandler.h Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTDataRequestHandler.m Fix retain cyles in RCTNetworkTask when used with RCTFileRequestHandler and RCTDataRequestHandler 2015-11-04 07:16:26 -08:00
RCTFileRequestHandler.h Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTFileRequestHandler.m Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTHTTPRequestHandler.h Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTHTTPRequestHandler.mm Added Cookie Header to XML and Websocket request 2017-05-24 09:47:37 -07:00
RCTNetInfo.h Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTNetInfo.m DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g 2017-07-24 15:50:53 -07:00
RCTNetworkTask.h Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTNetworkTask.m Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTNetworking.android.js Fix RCTNetworking error message 2017-07-11 09:10:20 -07:00
RCTNetworking.h Move all header imports to "<React/..>" 2016-11-23 07:58:39 -08:00
RCTNetworking.ios.js Fix RCTNetworking error message 2017-07-11 09:10:20 -07:00
RCTNetworking.mm Added Cookie Header to XML and Websocket request 2017-05-24 09:47:37 -07:00
XHRInterceptor.js Reorganize core JS files 2016-10-11 10:14:28 -07:00
XMLHttpRequest.js XMLHttpRequest withCredentials defaults to "true" 2017-05-25 11:01:14 -07:00
convertRequestBody.js XHR: support typed arrays for request payloads 2017-01-20 18:43:27 -08:00
fetch.js Removing @nolint from flow files 2017-07-12 14:09:54 -07:00