UNPKG

@abl-solutions/wifi-connect

Version:

The WiFi Connect React Native SDK enables the use of WiFi WPA2-Enterprise networks on mobile devices that are managed and operated by abl solutions GmbH

204 lines (165 loc) 7.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WifiConnectServiceImpl = void 0; var _reactNative = require("react-native"); var _http = require("./http"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class WifiConnectServiceImpl { constructor(nativeWifiConnect, wifiConnectionOptions) { this.nativeWifiConnect = nativeWifiConnect; _defineProperty(this, "options", void 0); _defineProperty(this, "legalTerms", null); _defineProperty(this, "permissionRejectedListener", null); _defineProperty(this, "SSID", null); _defineProperty(this, "isConnectedToWifi", () => this.SSID ? this.nativeWifiConnect.isConnectedToWifi(this.SSID) : Promise.resolve(false)); this.throwIfInvalidOptions(wifiConnectionOptions); this.options = this.mergeWithDefaultOptions(wifiConnectionOptions); } async connectToWifi(deviceId, user) { var _this$options$android; const endpoint = `${this.options.wifiApiEndpoint}/api/v1/devices/${deviceId}`; var response = await fetch(endpoint, { method: 'PUT', headers: { 'Authorization': `Bearer ${this.options.accessToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ email: user.email, preferredLocale: user.preferredLocale }) }); const body = await response.json(); (0, _http.throwOnInvliadResponse)(response.status, body); this.SSID = body.wifiCredentials.authorizedNetwork.ssid; const args = { caCertificate: body.wifiCredentials.authorizedNetwork.caCertificate, domainSuffix: body.wifiCredentials.authorizedNetwork.wpa2Domain, ignoreNetworkErrorOnSimulator: this.options.ignoreNetworkErrorOnSimulator, password: body.wifiCredentials.password, ssid: body.wifiCredentials.authorizedNetwork.ssid, username: body.wifiCredentials.username, timeSpanToWaitForPermissionDialogConfirmationInSeconds: (_this$options$android = this.options.android.timeSpanToWaitForPermissionDialogConfirmationInSeconds) !== null && _this$options$android !== void 0 ? _this$options$android : 10, isHiddenSSID: this.options.isHiddenSSID }; await this.nativeWifiConnect.connectToWifi(args); } async getLatestLegalTerms() { if (this.legalTerms) { return this.legalTerms; } const endpoint = `${this.options.wifiApiEndpoint}/api/v1/legal-terms`; var response = await fetch(endpoint, { method: 'GET', headers: { Authorization: `Bearer ${this.options.accessToken}` } }); if (response.status !== 200) { throw new Error(`Could not load legal terms. Received status ${response.status} from API.`); } const body = await response.json(); this.legalTerms = body; return body; } async legalTermsAccepted() { const legalTerms = await this.getLatestLegalTerms(); const endpoint = `${this.options.wifiApiEndpoint}/api/v1/legal-terms/acceptedVersion`; var response = await fetch(endpoint, { method: 'GET', headers: { Authorization: `Bearer ${this.options.accessToken}` } }); // The API will return a 404 if the current user did not accept any legal terms version at all. if (response.status === 404) { return false; } if (response.status !== 200) { throw new Error(`Could not load the currently accepted version. Received status ${response.status} from API.`); } const body = await response.json(); return body.version === legalTerms.version; } async legalTermsAcceptedVersion() { const endpoint = `${this.options.wifiApiEndpoint}/api/v1/legal-terms/acceptedVersion`; var response = await fetch(endpoint, { method: 'GET', headers: { Authorization: `Bearer ${this.options.accessToken}` } }); // The API will return a 404 if the current user did not accept any legal terms version at all. if (response.status === 404) { return 'undefined'; } if (response.status !== 200) { throw new Error(`Could not load the currently accepted version. Received status ${response.status} from API.`); } const body = await response.json(); return body.version; } async acceptLegalTerms(legalTermsVersion) { const endpoint = `${this.options.wifiApiEndpoint}/api/v1/legal-terms/${legalTermsVersion}/accept`; var response = await fetch(endpoint, { method: 'PUT', headers: { Authorization: `Bearer ${this.options.accessToken}` } }); if (response.status !== 200 && response.status !== 202) { throw new Error(`Accepting the legal terms failed. Received status ${response.status} from API.`); } } async deleteWifiConfiguration(deviceId) { const endpoint = `${this.options.wifiApiEndpoint}/api/v1/devices/${deviceId}`; var response = await fetch(endpoint, { method: 'DELETE', headers: { Authorization: `Bearer ${this.options.accessToken}` } }); (0, _http.throwOnInvliadResponse)(response.status, null, true); const args = { ignoreNetworkErrorOnSimulator: this.options.ignoreNetworkErrorOnSimulator }; await this.nativeWifiConnect.deleteConfiguration(args); } async isWifiConfigured() { return await this.nativeWifiConnect.isWifiConfigured(); } registerOnPermissionRejectedListener(callback) { const eventEmitter = new _reactNative.NativeEventEmitter(this.nativeWifiConnect); this.permissionRejectedListener = eventEmitter.addListener('PermissionRejected', () => callback()); } unregisterOnPermissionRejectedListener() { const listener = this.permissionRejectedListener; if (listener) { listener.remove(); this.permissionRejectedListener = null; } } mergeWithDefaultOptions(options) { var _options$ignoreNetwor, _options$android; return { accessToken: options.accessToken, ignoreNetworkErrorOnSimulator: (_options$ignoreNetwor = options.ignoreNetworkErrorOnSimulator) !== null && _options$ignoreNetwor !== void 0 ? _options$ignoreNetwor : false, wifiApiEndpoint: (options === null || options === void 0 ? void 0 : options.wifiApiEndpoint) || 'https://api.wifi.connectivity.abl-solutions.io', android: (_options$android = options.android) !== null && _options$android !== void 0 ? _options$android : {}, isHiddenSSID: (options === null || options === void 0 ? void 0 : options.isHiddenSSID) || false }; } throwIfInvalidOptions(options) { if (!options) { throw new Error('WifiConnectOptions must be not null.'); } if (!options.accessToken) { throw new Error('WifiConnectOptions.accessToken must be not null.'); } if (options.accessToken === '') { throw new Error('WifiConnectOptions.accessToken must be not empty.'); } } } exports.WifiConnectServiceImpl = WifiConnectServiceImpl; //# sourceMappingURL=wifi-connect.js.map