Add spec for Networking (#24892)

Summary:
Part of #24875, adds a spec for Networking. Since `sendRequest` methods are different for both platforms, I had to create 2 spec files as Flow would merge their definitions even when I added `Platform.OS` check

## Changelog

[General] [Added] - TM spec for Networking
Pull Request resolved: https://github.com/facebook/react-native/pull/24892

Reviewed By: RSNara

Differential Revision: D15543067

Pulled By: fkgozali

fbshipit-source-id: 2b91114dfa45e7899bbb139656a30a6fd52e31db
This commit is contained in:
Michał Pierzchała
2019-05-29 18:33:28 -07:00
committed by Facebook Github Bot
parent 08efb1d73b
commit e8037cb942
4 changed files with 89 additions and 15 deletions
@@ -0,0 +1,38 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';
type Header = [string, string];
export interface Spec extends TurboModule {
+sendRequest: (
method: string,
url: string,
requestId: number,
headers: Array<Header>,
data: Object,
responseType: Object, // TODO: Use stricter type.
useIncrementalUpdates: boolean,
timeout: number,
withCredentials: boolean,
) => void;
+abortRequest: (requestId: number) => void;
+clearCookies: (callback: (result: boolean) => mixed) => void;
// RCTEventEmitter
+addListener: (eventName: string) => void;
+removeListeners: (count: number) => void;
}
export default TurboModuleRegistry.getEnforcing<Spec>('Networking');
+38
View File
@@ -0,0 +1,38 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';
export interface Spec extends TurboModule {
+sendRequest: (
query: {|
method: string,
url: string,
data: Object,
headers: Object,
responseType: Object, // TODO: Use stricter type.
incrementalUpdates: boolean,
timeout: number,
withCredentials: boolean,
|},
callback: (requestId: number) => mixed,
) => void;
+abortRequest: (requestId: number) => void;
+clearCookies: (callback: (result: boolean) => mixed) => void;
// RCTEventEmitter
+addListener: (eventName: string) => void;
+removeListeners: (count: number) => void;
}
export default TurboModuleRegistry.getEnforcing<Spec>('Networking');
+6 -7
View File
@@ -13,8 +13,7 @@
// Do not require the native RCTNetworking module directly! Use this wrapper module instead.
// It will add the necessary requestId, so that you don't have to generate it yourself.
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
const RCTNetworkingNative = require('../BatchedBridge/NativeModules')
.Networking;
import NativeNetworkingAndroid from './NativeNetworkingAndroid';
const convertRequestBody = require('./convertRequestBody');
import type {RequestBody} from './convertRequestBody';
@@ -42,7 +41,7 @@ function generateRequestId(): number {
*/
class RCTNetworking extends NativeEventEmitter {
constructor() {
super(RCTNetworkingNative);
super(NativeNetworkingAndroid);
}
sendRequest(
@@ -54,7 +53,7 @@ class RCTNetworking extends NativeEventEmitter {
responseType: 'text' | 'base64',
incrementalUpdates: boolean,
timeout: number,
callback: (requestId: number) => any,
callback: (requestId: number) => mixed,
withCredentials: boolean,
) {
const body = convertRequestBody(data);
@@ -65,7 +64,7 @@ class RCTNetworking extends NativeEventEmitter {
}));
}
const requestId = generateRequestId();
RCTNetworkingNative.sendRequest(
NativeNetworkingAndroid.sendRequest(
method,
url,
requestId,
@@ -80,11 +79,11 @@ class RCTNetworking extends NativeEventEmitter {
}
abortRequest(requestId: number) {
RCTNetworkingNative.abortRequest(requestId);
NativeNetworkingAndroid.abortRequest(requestId);
}
clearCookies(callback: (result: boolean) => any) {
RCTNetworkingNative.clearCookies(callback);
NativeNetworkingAndroid.clearCookies(callback);
}
}
+7 -8
View File
@@ -11,8 +11,7 @@
'use strict';
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
const RCTNetworkingNative = require('../BatchedBridge/NativeModules')
.Networking;
import NativeNetworkingIOS from './NativeNetworkingIOS';
const convertRequestBody = require('./convertRequestBody');
import type {RequestBody} from './convertRequestBody';
@@ -21,7 +20,7 @@ import type {NativeResponseType} from './XMLHttpRequest';
class RCTNetworking extends NativeEventEmitter {
constructor() {
super(RCTNetworkingNative);
super(NativeNetworkingIOS);
}
sendRequest(
@@ -33,11 +32,11 @@ class RCTNetworking extends NativeEventEmitter {
responseType: NativeResponseType,
incrementalUpdates: boolean,
timeout: number,
callback: (requestId: number) => any,
callback: (requestId: number) => mixed,
withCredentials: boolean,
) {
const body = convertRequestBody(data);
RCTNetworkingNative.sendRequest(
NativeNetworkingIOS.sendRequest(
{
method,
url,
@@ -53,11 +52,11 @@ class RCTNetworking extends NativeEventEmitter {
}
abortRequest(requestId: number) {
RCTNetworkingNative.abortRequest(requestId);
NativeNetworkingIOS.abortRequest(requestId);
}
clearCookies(callback: (result: boolean) => any) {
RCTNetworkingNative.clearCookies(callback);
clearCookies(callback: (result: boolean) => mixed) {
NativeNetworkingIOS.clearCookies(callback);
}
}