diff --git a/jest.config.js b/jest.config.js index 710baf7..8824c11 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,10 +1,3 @@ module.exports = { preset: 'ts-jest', - - // TODO: Remove when `network-information-types` become obsolete - transformIgnorePatterns: ['/node_modules/(!network-information-types/)'], - moduleNameMapper: { - 'network-information-types': - 'network-information-types/dist-types/index.d.ts', - }, }; diff --git a/packages/web-api-hooks/package.json b/packages/web-api-hooks/package.json index cb0bf2d..593d26f 100644 --- a/packages/web-api-hooks/package.json +++ b/packages/web-api-hooks/package.json @@ -20,8 +20,7 @@ "doc": "documentation readme src --section Reference --config documentation.yml --markdown-toc-max-depth 3 --parse-extension ts && prettier --write README.md" }, "dependencies": { - "@types/resize-observer-browser": "^0.1.2", - "network-information-types": "^0.1.0" + "@types/resize-observer-browser": "^0.1.2" }, "devDependencies": { "typescript": "^3.8.3" diff --git a/packages/web-api-hooks/src/experimental-types/NetworkInformation.bundled.ts b/packages/web-api-hooks/src/experimental-types/NetworkInformation.bundled.ts new file mode 100644 index 0000000..f3ce5ad --- /dev/null +++ b/packages/web-api-hooks/src/experimental-types/NetworkInformation.bundled.ts @@ -0,0 +1,42 @@ +// Source: https://github.com/lacolaco/network-information-types/blob/master/index.d.ts + +// W3C Spec Draft http://wicg.github.io/netinfo/ +// Edition: Draft Community Group Report 20 February 2019 + +// http://wicg.github.io/netinfo/#connection-types +type ConnectionType = + | 'bluetooth' + | 'cellular' + | 'ethernet' + | 'mixed' + | 'none' + | 'other' + | 'unknown' + | 'wifi' + | 'wimax'; + +// http://wicg.github.io/netinfo/#effectiveconnectiontype-enum +type EffectiveConnectionType = '2g' | '3g' | '4g' | 'slow-2g'; + +// http://wicg.github.io/netinfo/#dom-megabit +type Megabit = number; +// http://wicg.github.io/netinfo/#dom-millisecond +type Millisecond = number; + +// http://wicg.github.io/netinfo/#networkinformation-interface +export interface NetworkInformation extends EventTarget { + // http://wicg.github.io/netinfo/#type-attribute + readonly type?: ConnectionType; + // http://wicg.github.io/netinfo/#effectivetype-attribute + readonly effectiveType?: EffectiveConnectionType; + // http://wicg.github.io/netinfo/#downlinkmax-attribute + readonly downlinkMax?: Megabit; + // http://wicg.github.io/netinfo/#downlink-attribute + readonly downlink?: Megabit; + // http://wicg.github.io/netinfo/#rtt-attribute + readonly rtt?: Millisecond; + // http://wicg.github.io/netinfo/#savedata-attribute + readonly saveData?: boolean; + // http://wicg.github.io/netinfo/#handling-changes-to-the-underlying-connection + onchange?: EventListener; +} diff --git a/packages/web-api-hooks/src/experimental-types/NetworkInformation.d.ts b/packages/web-api-hooks/src/experimental-types/NetworkInformation.d.ts new file mode 100644 index 0000000..f3f1ed4 --- /dev/null +++ b/packages/web-api-hooks/src/experimental-types/NetworkInformation.d.ts @@ -0,0 +1,53 @@ +// Source: https://github.com/lacolaco/network-information-types/blob/master/index.d.ts + +// W3C Spec Draft http://wicg.github.io/netinfo/ +// Edition: Draft Community Group Report 20 February 2019 + +// http://wicg.github.io/netinfo/#connection-types +type ConnectionType = + | 'bluetooth' + | 'cellular' + | 'ethernet' + | 'mixed' + | 'none' + | 'other' + | 'unknown' + | 'wifi' + | 'wimax'; + +// http://wicg.github.io/netinfo/#effectiveconnectiontype-enum +type EffectiveConnectionType = '2g' | '3g' | '4g' | 'slow-2g'; + +// http://wicg.github.io/netinfo/#dom-megabit +type Megabit = number; +// http://wicg.github.io/netinfo/#dom-millisecond +type Millisecond = number; + +// http://wicg.github.io/netinfo/#networkinformation-interface +interface NetworkInformation extends EventTarget { + // http://wicg.github.io/netinfo/#type-attribute + readonly type?: ConnectionType; + // http://wicg.github.io/netinfo/#effectivetype-attribute + readonly effectiveType?: EffectiveConnectionType; + // http://wicg.github.io/netinfo/#downlinkmax-attribute + readonly downlinkMax?: Megabit; + // http://wicg.github.io/netinfo/#downlink-attribute + readonly downlink?: Megabit; + // http://wicg.github.io/netinfo/#rtt-attribute + readonly rtt?: Millisecond; + // http://wicg.github.io/netinfo/#savedata-attribute + readonly saveData?: boolean; + // http://wicg.github.io/netinfo/#handling-changes-to-the-underlying-connection + onchange?: EventListener; +} + +// http://wicg.github.io/netinfo/#navigatornetworkinformation-interface +/* eslint-disable @typescript-eslint/no-empty-interface */ +declare interface Navigator extends NavigatorNetworkInformation {} +declare interface WorkerNavigator extends NavigatorNetworkInformation {} +/* eslint-enable @typescript-eslint/no-empty-interface */ + +// http://wicg.github.io/netinfo/#navigatornetworkinformation-interface +declare interface NavigatorNetworkInformation { + readonly connection?: NetworkInformation; +} diff --git a/packages/web-api-hooks/src/useNetworkInformation.ts b/packages/web-api-hooks/src/useNetworkInformation.ts index 27d57d9..1a25eaa 100644 --- a/packages/web-api-hooks/src/useNetworkInformation.ts +++ b/packages/web-api-hooks/src/useNetworkInformation.ts @@ -1,8 +1,6 @@ -// eslint-disable-next-line import/no-unresolved -import 'network-information-types'; - import { useEffect, useState } from 'react'; +import { NetworkInformation } from './experimental-types/NetworkInformation.bundled'; import { canUseDOM, managedEventListener } from './utils'; /** diff --git a/yarn.lock b/yarn.lock index e99a4f4..fa082b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8208,11 +8208,6 @@ neo-async@^2.6.0: resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c" integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw== -network-information-types@^0.1.0: - version "0.1.0" - resolved "https://registry.yarnpkg.com/network-information-types/-/network-information-types-0.1.0.tgz#5c880bebc2a9e6b25eb00ed23854d13ff3a7fe3a" - integrity sha512-cRUCYZoRHTMjYcgk5MbwqM0h0Za34panRxAJKY8n+mQ+NLMuRIw7aKzmaZqkC/cte7bnRcdfTwFA27GgN62EtQ== - nice-try@^1.0.4: version "1.0.5" resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"