Skip to content

Commit

Permalink
Add spec for Networking (facebook#24892)
Browse files Browse the repository at this point in the history
Summary:
Part of facebook#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: facebook#24892

Reviewed By: RSNara

Differential Revision: D15543067

Pulled By: fkgozali

fbshipit-source-id: 2b91114dfa45e7899bbb139656a30a6fd52e31db
  • Loading branch information
thymikee authored and M-i-k-e-l committed Mar 10, 2020
1 parent 5638f3f commit 7c86114
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 15 deletions.
38 changes: 38 additions & 0 deletions Libraries/Network/NativeNetworkingAndroid.js
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions Libraries/Network/NativeNetworkingIOS.js
Original file line number Diff line number Diff line change
@@ -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');
13 changes: 6 additions & 7 deletions Libraries/Network/RCTNetworking.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -42,7 +41,7 @@ function generateRequestId(): number {
*/
class RCTNetworking extends NativeEventEmitter {
constructor() {
super(RCTNetworkingNative);
super(NativeNetworkingAndroid);
}

sendRequest(
Expand All @@ -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);
Expand All @@ -65,7 +64,7 @@ class RCTNetworking extends NativeEventEmitter {
}));
}
const requestId = generateRequestId();
RCTNetworkingNative.sendRequest(
NativeNetworkingAndroid.sendRequest(
method,
url,
requestId,
Expand All @@ -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);
}
}

Expand Down
15 changes: 7 additions & 8 deletions Libraries/Network/RCTNetworking.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +20,7 @@ import type {NativeResponseType} from './XMLHttpRequest';

class RCTNetworking extends NativeEventEmitter {
constructor() {
super(RCTNetworkingNative);
super(NativeNetworkingIOS);
}

sendRequest(
Expand All @@ -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,
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 7c86114

Please sign in to comment.