Skip to content

Commit

Permalink
Add support of custom http methods "HEAD" & "OPTIONS" (only for withN…
Browse files Browse the repository at this point in the history
…etworkConnectivity) (#122)
  • Loading branch information
tscharke authored and rgommezz committed Sep 11, 2018
1 parent 882a157 commit f30a559
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Config = {
withExtraHeadRequest?: boolean = true,
checkConnectionInterval?: number = 0,
checkInBackground?: boolean = false,
httpMethod?: string = 'HEAD',
}
```

Expand All @@ -95,6 +96,8 @@ type Config = {

`checkInBackground`: whether or not to check connectivity when app isn't active. Default is `false`.

`httpMethod`: usage http method to check the internet-access. Supports HEAD or OPTIONS. Default is `HEAD`.

##### Usage
```js
import React from 'react';
Expand Down
4 changes: 3 additions & 1 deletion src/checkInternetAccess.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* @flow */
import makeHttpRequest from './makeHttpRequest';
import type { HTTPMethod } from './types';

export default function checkInternetAccess(
timeout: number = 3000,
url: string = 'http://www.google.com/',
method: HTTPMethod = 'HEAD',
): Promise<boolean> {
return new Promise((resolve: (value: boolean) => void) => {
makeHttpRequest({
method: 'HEAD',
method,
url,
timeout,
})
Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ export type NetworkState = {
isConnected: boolean,
actionQueue: Array<*>,
};

export type HTTPMethod = 'HEAD' | 'OPTIONS';
4 changes: 4 additions & 0 deletions src/withNetworkConnectivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
setupConnectivityCheckInterval,
clearConnectivityCheckInterval,
} from './checkConnectivityInterval';
import type { HTTPMethod } from './types';

type Arguments = {
withRedux?: boolean,
Expand All @@ -19,6 +20,7 @@ type Arguments = {
withExtraHeadRequest?: boolean,
checkConnectionInterval?: number,
checkInBackground?: boolean,
httpMethod?: HTTPMethod,
};

type State = {
Expand All @@ -33,6 +35,7 @@ const withNetworkConnectivity = (
withExtraHeadRequest = true,
checkConnectionInterval = 0,
checkInBackground = false,
httpMethod = 'HEAD',
}: Arguments = {},
) => (WrappedComponent: ReactClass<*>) => {
if (typeof withRedux !== 'boolean') {
Expand Down Expand Up @@ -108,6 +111,7 @@ const withNetworkConnectivity = (
checkInternetAccess(
timeout,
pingServerUrl,
httpMethod,
).then((hasInternetAccess: boolean) => {
this.handleConnectivityChange(hasInternetAccess);
});
Expand Down

0 comments on commit f30a559

Please sign in to comment.