diff --git a/src/config.ts b/src/config.ts index 6171f8c..31350ff 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,12 +2,16 @@ export interface NoraConfig { group?: string; email: string; - password: string; + password?: string; + sso?: string; } -export const apiEndpoint = 'https://api.smart-nora.eu/client/'; +export const API_ENDPOINT = 'https://api.smart-nora.eu'; -export const firebaseConfig = { +const { name, version } = require('../package.json'); +export const USER_AGENT = `${name}/${version}`; + +export const FIREBASE_CONFIG = { apiKey: 'AIzaSyCE4ogvmNJG8Vvkzf1wfWKhjzCALlLGLsw', authDomain: 'nora-firebase.firebaseapp.com', databaseURL: 'https://nora-firebase-default-rtdb.europe-west1.firebasedatabase.app', diff --git a/src/context.ts b/src/context.ts index de61308..fcb3d97 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,7 +1,7 @@ import { deleteApp, initializeApp } from 'firebase/app'; import { getAuth, signInWithEmailAndPassword } from 'firebase/auth'; import { getDatabase, Database, ref, DatabaseReference, child, update, onValue, remove } from 'firebase/database'; -import { firebaseConfig } from './config'; +import { FIREBASE_CONFIG } from './config'; interface ContextConfiguration { email: string; @@ -42,7 +42,7 @@ class FirebaseContextStorage { async open() { await this.close(); - const app = initializeApp(firebaseConfig, 'app-context'); + const app = initializeApp(FIREBASE_CONFIG, 'app-context'); const auth = getAuth(app); const { user } = await signInWithEmailAndPassword(auth, this.config.email, this.config.password); this.db = getDatabase(app); diff --git a/src/index.ts b/src/index.ts index 8d62ca0..8dc57e3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,7 @@ import { Device } from '@andrei-tatar/nora-firebase-common'; -import { - concat, connectable, EMPTY, MonoTypeOperatorFunction, - Observable, of, ReplaySubject, Subscription, timer -} from 'rxjs'; +import { concat, EMPTY, MonoTypeOperatorFunction, of, ReplaySubject, timer } from 'rxjs'; import { filter, map, scan, share, switchMap } from 'rxjs/operators'; +import type { Response as NodeFetchResponse } from 'node-fetch'; export interface NodeMessage extends Record { payload: any; @@ -44,7 +42,8 @@ export interface Logger { export interface ConfigNode { email: string; - password: string; + password?: string; + sso?: string; group?: string; valid: boolean; localExecution: boolean; @@ -126,3 +125,20 @@ export function singleton(): MonoTypeOperatorFunction { }) ); } + +export function shouldRetryRequest(response: NodeFetchResponse) { + if (response.status === 429) { + return true; + } + + const status = Math.floor(response.status / 100); + return status !== 2 && status !== 4; +} + +export class HttpError extends Error { + constructor( + public readonly statusCode: number, + public readonly content: string) { + super(`HTTP response (${statusCode} ${content})`); + } +} diff --git a/src/nodes/nora-config.html b/src/nodes/nora-config.html index 3c1e4b2..25718b6 100644 --- a/src/nodes/nora-config.html +++ b/src/nodes/nora-config.html @@ -1,51 +1,85 @@