Skip to content

Commit 0cbf067

Browse files
authored
feat: add optional reachabilityHeaders param to NetInfoConfiguration (react-native-netinfo#673)
1 parent fe41ff3 commit 0cbf067

File tree

5 files changed

+5
-0
lines changed

5 files changed

+5
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ The configuration options for the library.
224224
| Property | Type | Default | Description
225225
| ---------------------------- | --------------------------------- | ----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
226226
| `reachabilityUrl` | `string` | `https://clients3.google.com/generate_204` | The URL to call to test if the internet is reachable. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`.
227+
| `reachabilityHeaders` | `object` | `{}` | A HTTP headers object, an object literal, or an array of two-item arrays to set request's headers, to use during the reachabilityUrl URL call to test if the internet is reachable. Defaults to `{}`. |
227228
| `reachabilityMethod` | `NetInfoMethodType` | `HEAD` | The HTTP request method to use to call reachabilityUrl URL to call to test if the internet is reachable. Defaults to `HEAD`. `GET` is also available |
228229
| `reachabilityTest` | `(response: Response) => boolean` | `Promise.resolve(response.status === 204)` | A function which is passed the `Response` from calling the reachability URL. It should return `true` if the response indicates that the internet is reachable. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |
229230
| `reachabilityShortTimeout` | `number` | 5 seconds | The number of milliseconds between internet reachability checks when the internet was not previously detected. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |

src/internal/defaultConfiguration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Types from './types';
33
const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
44
reachabilityUrl: 'https://clients3.google.com/generate_204',
55
reachabilityMethod: 'HEAD',
6+
reachabilityHeaders: {},
67
reachabilityTest: (response: Response): Promise<boolean> =>
78
Promise.resolve(response.status === 204),
89
reachabilityShortTimeout: 5 * 1000, // 5s

src/internal/defaultConfiguration.web.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Types from './types';
33
const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
44
reachabilityUrl: '/',
55
reachabilityMethod: "HEAD",
6+
reachabilityHeaders: {},
67
reachabilityTest: (response: Response): Promise<boolean> =>
78
Promise.resolve(response.status === 200),
89
reachabilityShortTimeout: 5 * 1000, // 5s

src/internal/internetReachability.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default class InternetReachability {
6969

7070
private _checkInternetReachability = (): InternetReachabilityCheckHandler => {
7171
const responsePromise = fetch(this._configuration.reachabilityUrl, {
72+
headers: this._configuration.reachabilityHeaders,
7273
method: this._configuration.reachabilityMethod,
7374
cache: 'no-cache',
7475
});

src/internal/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export type NetInfoSubscription = () => void;
117117
export interface NetInfoConfiguration {
118118
reachabilityUrl: string;
119119
reachabilityMethod?: NetInfoMethodType;
120+
reachabilityHeaders?: HeadersInit;
120121
reachabilityTest: (response: Response) => Promise<boolean>;
121122
reachabilityLongTimeout: number;
122123
reachabilityShortTimeout: number;

0 commit comments

Comments
 (0)