Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add HTTP headers through reachabilityHeaders param to NetInfoConfiguration #673

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export type NetInfoSubscription = () => void;
export interface NetInfoConfiguration {
reachabilityUrl: string;
reachabilityMethod?: NetInfoMethodType;
reachabilityHeaders?: HeadersInit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @webraptor 👋 - this HeadersInit appears undefined, as noted in #693 - what should it be? Seem like it should be whatever valid internet headers are, perhaps { [index: string]: string; } or similar? What do you think?

reachabilityTest: (response: Response) => Promise<boolean>;
reachabilityLongTimeout: number;
reachabilityShortTimeout: number;
Expand Down