UNPKG

eightsleep

Version:
206 lines 9.52 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const simple_api_client_1 = __importStar(require("simple-api-client")); const validateOauth_1 = __importDefault(require("./validateOauth")); const validateSession_1 = __importDefault(require("./validateSession")); const baseerr_1 = __importDefault(require("baseerr")); const EightSleepAppApi_1 = require("./EightSleepAppApi"); const cross_fetch_1 = __importDefault(require("cross-fetch")); const memoize_concurrent_1 = __importDefault(require("memoize-concurrent")); const fast_safe_stringify_1 = __importDefault(require("fast-safe-stringify")); const validateDevice_1 = __importDefault(require("./validateDevice")); const validateUser_1 = __importDefault(require("./validateUser")); var EightSleepAppApi_2 = require("./EightSleepAppApi"); Object.defineProperty(exports, "Sides", { enumerable: true, get: function () { return EightSleepAppApi_2.Sides; } }); var simple_api_client_2 = require("simple-api-client"); Object.defineProperty(exports, "NetworkError", { enumerable: true, get: function () { return simple_api_client_2.NetworkError; } }); Object.defineProperty(exports, "StatusCodeError", { enumerable: true, get: function () { return simple_api_client_2.StatusCodeError; } }); Object.defineProperty(exports, "InvalidResponseError", { enumerable: true, get: function () { return simple_api_client_2.InvalidResponseError; } }); simple_api_client_1.setFetch(cross_fetch_1.default); const defaultInit = { headers: { 'user-agent': 'Eight%20Sleep/1000018 CFNetwork/1237 Darwin/20.4.0', 'accept-language': 'en-us', 'accept-encoding': 'gzip, deflate, br', }, backoff: { statusCodes: /^5..$/, timeouts: [100, 200], }, throttle: { statusCodes: [429], timeout: 60 * 1000, }, }; class EightSleepClientApi extends simple_api_client_1.default { constructor({ email, password, oauthClient, oauthSession, session, }) { super('https://client-api.8slp.net/v1', (path, init) => __awaiter(this, void 0, void 0, function* () { var _a; // @ts-ignore const session = ((_a = init === null || init === void 0 ? void 0 : init.json) === null || _a === void 0 ? void 0 : _a.email) == null ? yield this.login() : null; const headers = Object.assign(Object.assign({}, defaultInit.headers), init === null || init === void 0 ? void 0 : init.headers); if (session) { Object.assign(headers, { 'user-id': session.userId, 'session-token': session.token, }); } return Object.assign(Object.assign(Object.assign({}, init), defaultInit), { headers }); })); this.login = memoize_concurrent_1.default(() => __awaiter(this, void 0, void 0, function* () { if (this.session != null) return yield this.refreshSession(); const { email, password } = this.auth; const json = yield this.post('login', 200, { json: { email, password, }, }); this.session = validateSession_1.default(json.session); return this.session; }), { cacheKey: () => 'all', }); this.oauth = memoize_concurrent_1.default(() => __awaiter(this, void 0, void 0, function* () { if (this.oauthClient == null) { throw new baseerr_1.default('missing oauth client info'); } if (this.oauthSession != null) return yield this.refreshOauth(); const json = yield this.post('users/oauth-token', { json: { client_id: this.oauthClient.id, client_secret: this.oauthClient.secret, }, }); this.oauthSession = validateOauth_1.default(json); return this.oauthSession; })); this.getUser = memoize_concurrent_1.default((id, query) => __awaiter(this, void 0, void 0, function* () { const json = yield this.get(`users/${id}`, 200, { query, }); // @ts-ignore if (query === null || query === void 0 ? void 0 : query.filter) return json.result; return validateUser_1.default(json.user); }), { cacheKey: ([id, query]) => `${id}:${fast_safe_stringify_1.default(query !== null && query !== void 0 ? query : {})}`, }); this.getDevice = memoize_concurrent_1.default((id, query) => __awaiter(this, void 0, void 0, function* () { const json = yield this.get(`devices/${id}`, 200, { query, }); // @ts-ignore if (query === null || query === void 0 ? void 0 : query.filter) return json.result; return validateDevice_1.default(json.result); }), { cacheKey: ([id, query]) => `${id}:${fast_safe_stringify_1.default(query !== null && query !== void 0 ? query : {})}`, }); this.auth = { email, password }; this.oauthClient = oauthClient; if (oauthClient) { this.appApiClient = new EightSleepAppApi_1.EightSleepAppApi({ clientApi: this, defaultInit, }); } this.oauthSession = oauthSession; this.session = session; } toJSON() { return Object.assign(Object.assign({}, this.auth), { oauthClient: this.oauthClient, oauthSession: this.oauthSession, session: this.session }); } refreshSession() { return __awaiter(this, void 0, void 0, function* () { if (this.auth == null || this.session == null) { // no auth or session throw new baseerr_1.default('unauthorized', { login: this.auth, session: this.session, }); } if (this.session.expirationDate.valueOf() < Date.now() - 100) { // session is expired, login again delete this.session; return yield this.login(); } return this.session; }); } getAppApiClient() { if (!this.appApiClient) { throw new baseerr_1.default('appApiClient is missing (client should be instantiated with oauthClient)'); } return this.appApiClient; } refreshOauth() { return __awaiter(this, void 0, void 0, function* () { if (this.oauthClient == null) { throw new baseerr_1.default('missing oauth client info'); } if (this.oauthSession == null) { // no oauth session to refresh throw new baseerr_1.default('unauthorized'); } if (this.oauthSession.expiresIn.valueOf() < Date.now() - 100) { // oauth session is expired, login again delete this.oauthSession; return yield this.oauth(); } return this.oauthSession; }); } getMe(query) { return __awaiter(this, void 0, void 0, function* () { return this.getUser('me', query); }); } updateMe(json, query) { return __awaiter(this, void 0, void 0, function* () { const resJSON = yield this.put('users/me', 200, { json, query, }); // @ts-ignore if (query === null || query === void 0 ? void 0 : query.filter) return resJSON.result; return validateUser_1.default(resJSON.user); }); } } exports.default = EightSleepClientApi; //# sourceMappingURL=index.js.map