Skip to content

Commit e8037cb

Browse files
thymikeefacebook-github-bot
authored andcommitted
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: #24892 Reviewed By: RSNara Differential Revision: D15543067 Pulled By: fkgozali fbshipit-source-id: 2b91114dfa45e7899bbb139656a30a6fd52e31db
1 parent 08efb1d commit e8037cb

File tree

4 files changed

+89
-15
lines changed

4 files changed

+89
-15
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {TurboModule} from 'RCTExport';
14+
import * as TurboModuleRegistry from 'TurboModuleRegistry';
15+
16+
type Header = [string, string];
17+
18+
export interface Spec extends TurboModule {
19+
+sendRequest: (
20+
method: string,
21+
url: string,
22+
requestId: number,
23+
headers: Array<Header>,
24+
data: Object,
25+
responseType: Object, // TODO: Use stricter type.
26+
useIncrementalUpdates: boolean,
27+
timeout: number,
28+
withCredentials: boolean,
29+
) => void;
30+
+abortRequest: (requestId: number) => void;
31+
+clearCookies: (callback: (result: boolean) => mixed) => void;
32+
33+
// RCTEventEmitter
34+
+addListener: (eventName: string) => void;
35+
+removeListeners: (count: number) => void;
36+
}
37+
38+
export default TurboModuleRegistry.getEnforcing<Spec>('Networking');
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {TurboModule} from 'RCTExport';
14+
import * as TurboModuleRegistry from 'TurboModuleRegistry';
15+
16+
export interface Spec extends TurboModule {
17+
+sendRequest: (
18+
query: {|
19+
method: string,
20+
url: string,
21+
data: Object,
22+
headers: Object,
23+
responseType: Object, // TODO: Use stricter type.
24+
incrementalUpdates: boolean,
25+
timeout: number,
26+
withCredentials: boolean,
27+
|},
28+
callback: (requestId: number) => mixed,
29+
) => void;
30+
+abortRequest: (requestId: number) => void;
31+
+clearCookies: (callback: (result: boolean) => mixed) => void;
32+
33+
// RCTEventEmitter
34+
+addListener: (eventName: string) => void;
35+
+removeListeners: (count: number) => void;
36+
}
37+
38+
export default TurboModuleRegistry.getEnforcing<Spec>('Networking');

Libraries/Network/RCTNetworking.android.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
// Do not require the native RCTNetworking module directly! Use this wrapper module instead.
1414
// It will add the necessary requestId, so that you don't have to generate it yourself.
1515
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
16-
const RCTNetworkingNative = require('../BatchedBridge/NativeModules')
17-
.Networking;
16+
import NativeNetworkingAndroid from './NativeNetworkingAndroid';
1817
const convertRequestBody = require('./convertRequestBody');
1918

2019
import type {RequestBody} from './convertRequestBody';
@@ -42,7 +41,7 @@ function generateRequestId(): number {
4241
*/
4342
class RCTNetworking extends NativeEventEmitter {
4443
constructor() {
45-
super(RCTNetworkingNative);
44+
super(NativeNetworkingAndroid);
4645
}
4746

4847
sendRequest(
@@ -54,7 +53,7 @@ class RCTNetworking extends NativeEventEmitter {
5453
responseType: 'text' | 'base64',
5554
incrementalUpdates: boolean,
5655
timeout: number,
57-
callback: (requestId: number) => any,
56+
callback: (requestId: number) => mixed,
5857
withCredentials: boolean,
5958
) {
6059
const body = convertRequestBody(data);
@@ -65,7 +64,7 @@ class RCTNetworking extends NativeEventEmitter {
6564
}));
6665
}
6766
const requestId = generateRequestId();
68-
RCTNetworkingNative.sendRequest(
67+
NativeNetworkingAndroid.sendRequest(
6968
method,
7069
url,
7170
requestId,
@@ -80,11 +79,11 @@ class RCTNetworking extends NativeEventEmitter {
8079
}
8180

8281
abortRequest(requestId: number) {
83-
RCTNetworkingNative.abortRequest(requestId);
82+
NativeNetworkingAndroid.abortRequest(requestId);
8483
}
8584

8685
clearCookies(callback: (result: boolean) => any) {
87-
RCTNetworkingNative.clearCookies(callback);
86+
NativeNetworkingAndroid.clearCookies(callback);
8887
}
8988
}
9089

Libraries/Network/RCTNetworking.ios.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
'use strict';
1212

1313
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
14-
const RCTNetworkingNative = require('../BatchedBridge/NativeModules')
15-
.Networking;
14+
import NativeNetworkingIOS from './NativeNetworkingIOS';
1615
const convertRequestBody = require('./convertRequestBody');
1716

1817
import type {RequestBody} from './convertRequestBody';
@@ -21,7 +20,7 @@ import type {NativeResponseType} from './XMLHttpRequest';
2120

2221
class RCTNetworking extends NativeEventEmitter {
2322
constructor() {
24-
super(RCTNetworkingNative);
23+
super(NativeNetworkingIOS);
2524
}
2625

2726
sendRequest(
@@ -33,11 +32,11 @@ class RCTNetworking extends NativeEventEmitter {
3332
responseType: NativeResponseType,
3433
incrementalUpdates: boolean,
3534
timeout: number,
36-
callback: (requestId: number) => any,
35+
callback: (requestId: number) => mixed,
3736
withCredentials: boolean,
3837
) {
3938
const body = convertRequestBody(data);
40-
RCTNetworkingNative.sendRequest(
39+
NativeNetworkingIOS.sendRequest(
4140
{
4241
method,
4342
url,
@@ -53,11 +52,11 @@ class RCTNetworking extends NativeEventEmitter {
5352
}
5453

5554
abortRequest(requestId: number) {
56-
RCTNetworkingNative.abortRequest(requestId);
55+
NativeNetworkingIOS.abortRequest(requestId);
5756
}
5857

59-
clearCookies(callback: (result: boolean) => any) {
60-
RCTNetworkingNative.clearCookies(callback);
58+
clearCookies(callback: (result: boolean) => mixed) {
59+
NativeNetworkingIOS.clearCookies(callback);
6160
}
6261
}
6362

0 commit comments

Comments
 (0)