Skip to content

Commit

Permalink
feat(options): Allow a custom options object as paramater
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Briani committed Jun 3, 2018
1 parent c19152b commit d11586e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,64 @@
[![Build passing](https://img.shields.io/travis/TheWorm/isomorphic-is-online.svg)](https://travis-ci.org/TheWorm/isomorphic-is-online)
[![Code coverage](https://img.shields.io/codecov/c/github/theworm/isomorphic-is-online.svg)](https://codecov.io/github/theworm/isomorphic-is-online)
[![Release version](https://img.shields.io/github/release/theworm/isomorphic-is-online.svg)](https://www.npmjs.com/package/isomorphic-is-online)

A simple utility you can use to know if you are online or not.

It relies on [cross-fetch](https://github.com/lquixada/cross-fetch), thus it aims to work on Node, React Native and Web (with browserify / webpack) environments.

## Installation

At the command-line terminal:

```bash
npm install --save isomorphic-is-online
```

Then in your code:

```javascript
// Using ES6 modules with Babel or TypeScript
import isOnline from "isomorphic-is-online";

// Using CommonJS modules
const isOnline = require("isomorphic-is-online").default;
```

## Usage

```javascript
import isOnline from "isomorphic-is-online";

console.log("Are you able to reach the internet?");
isOnline().then(isOnline => {
console.log("Response:", isOnline);
});
```

`isOnline` returns a promise which resolves to `true` if a connection can be established and resolves to `false` if it cannot or the network requests time out.

| para

## Default and custom options

An optional object can be passed to `isOnline` to customize some options:

| option | type | default | description |
| ------- | ---------------- | ------------- | -------------------------------------------------- |
| urls | array of strings | ["//1.1.1.1"] | List of urls to request to check connection status |
| timeout | number | 3000 | Milliseconds to wait before timing out the request |

```javascript
const options = {
urls: ["https://apple.com", "//1.1.1.1"],
timeout: 5000
};

isOnline(options).then(isOnline => {
if (isOnline) {
hackTheInternet();
} else {
beQuiet();
}
});
```
21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@ import fetch from "cross-fetch";

const isString = s => typeof s === "string" || s instanceof String;

const isOnline = async (customUrls: Array<string>, timeout: number = 3000) => {
let urls = ["//1.1.1.1"];
if (Array.isArray(customUrls) && customUrls.every(isString)) {
urls = customUrls;
type options = {
urls: Array<string>,
timeout: number
};

const defaultOptions: options = {
urls: ["//1.1.1.1"],
timeout: 3000
};

const isOnline = async (options: options) => {
let { urls, timeout } = options;
if (!Array.isArray(urls) || !urls.every(isString)) {
urls = defaultOptions.urls;
}
if (!timeout || typeof timeout !== "number") {
timeout = defaultOptions.timeout;
}
const onlinePromise = new Promise((resolve, reject) => {
let failedUrls = 0;
Expand Down

0 comments on commit d11586e

Please sign in to comment.