Skip to content

Commit

Permalink
feat: add support for reachabilityMethod to specify GET or HEAD (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainJeff committed Jun 28, 2022
1 parent 9ee3309 commit 3f5badd
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ The configuration options for the library.

| Property | Type | Default | Description
| ---------------------------- | --------------------------------- | ----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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`. |
| `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`.
| `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 |
| `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`. |
| `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`. |
| `reachabilityLongTimeout` | `number` | 60 seconds | The number of milliseconds between internet reachability checks when the internet was previously detected. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |
Expand Down
1 change: 1 addition & 0 deletions src/internal/defaultConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Types from './types';

const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
reachabilityUrl: 'https://clients3.google.com/generate_204',
reachabilityMethod: 'HEAD',
reachabilityTest: (response: Response): Promise<boolean> =>
Promise.resolve(response.status === 204),
reachabilityShortTimeout: 5 * 1000, // 5s
Expand Down
1 change: 1 addition & 0 deletions src/internal/defaultConfiguration.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Types from './types';

const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
reachabilityUrl: '/',
reachabilityMethod: "HEAD",
reachabilityTest: (response: Response): Promise<boolean> =>
Promise.resolve(response.status === 200),
reachabilityShortTimeout: 5 * 1000, // 5s
Expand Down
4 changes: 2 additions & 2 deletions src/internal/internetReachability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @format
*/

import * as Types from './types';
import * as PrivateTypes from './privateTypes';
import * as Types from './types';

interface InternetReachabilityCheckHandler {
promise: Promise<void>;
Expand Down Expand Up @@ -69,7 +69,7 @@ export default class InternetReachability {

private _checkInternetReachability = (): InternetReachabilityCheckHandler => {
const responsePromise = fetch(this._configuration.reachabilityUrl, {
method: 'HEAD',
method: this._configuration.reachabilityMethod,
cache: 'no-cache',
});

Expand Down
3 changes: 3 additions & 0 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export enum NetInfoStateType {
other = 'other',
}

export type NetInfoMethodType = 'HEAD' | 'GET';

export enum NetInfoCellularGeneration {
'2g' = '2g',
'3g' = '3g',
Expand Down Expand Up @@ -109,6 +111,7 @@ export type NetInfoSubscription = () => void;

export interface NetInfoConfiguration {
reachabilityUrl: string;
reachabilityMethod?: NetInfoMethodType;
reachabilityTest: (response: Response) => Promise<boolean>;
reachabilityLongTimeout: number;
reachabilityShortTimeout: number;
Expand Down

0 comments on commit 3f5badd

Please sign in to comment.