Skip to content

Commit

Permalink
feat: add optional reachabilityHeaders param to NetInfoConfiguration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
webraptor committed Nov 1, 2023
1 parent fe41ff3 commit 0cbf067
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -224,6 +224,7 @@ 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`.
| `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 `{}`. |
| `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`. |
Expand Down
1 change: 1 addition & 0 deletions src/internal/defaultConfiguration.ts
Expand Up @@ -3,6 +3,7 @@ import * as Types from './types';
const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
reachabilityUrl: 'https://clients3.google.com/generate_204',
reachabilityMethod: 'HEAD',
reachabilityHeaders: {},
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
Expand Up @@ -3,6 +3,7 @@ import * as Types from './types';
const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
reachabilityUrl: '/',
reachabilityMethod: "HEAD",
reachabilityHeaders: {},
reachabilityTest: (response: Response): Promise<boolean> =>
Promise.resolve(response.status === 200),
reachabilityShortTimeout: 5 * 1000, // 5s
Expand Down
1 change: 1 addition & 0 deletions src/internal/internetReachability.ts
Expand Up @@ -69,6 +69,7 @@ export default class InternetReachability {

private _checkInternetReachability = (): InternetReachabilityCheckHandler => {
const responsePromise = fetch(this._configuration.reachabilityUrl, {
headers: this._configuration.reachabilityHeaders,
method: this._configuration.reachabilityMethod,
cache: 'no-cache',
});
Expand Down
1 change: 1 addition & 0 deletions src/internal/types.ts
Expand Up @@ -117,6 +117,7 @@ export type NetInfoSubscription = () => void;
export interface NetInfoConfiguration {
reachabilityUrl: string;
reachabilityMethod?: NetInfoMethodType;
reachabilityHeaders?: HeadersInit;
reachabilityTest: (response: Response) => Promise<boolean>;
reachabilityLongTimeout: number;
reachabilityShortTimeout: number;
Expand Down

0 comments on commit 0cbf067

Please sign in to comment.