mosquito-transport-js 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mosquito-transport-js",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Javascript web sdk for mosquito-transport (https://github.com/deflexable/mosquito-transport)",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -37,7 +37,7 @@
37
37
  "lodash": "^4.17.21",
38
38
  "set-large-timeout": "^1.0.1",
39
39
  "socket.io-client": "^4.6.2",
40
- "subscription-listener": "^1.1.1",
40
+ "subscription-listener": "^1.1.2",
41
41
  "tweetnacl": "^1.0.3"
42
42
  },
43
43
  "devDependencies": {
@@ -1,7 +1,7 @@
1
1
  import { ServerReachableListener, StoreReadyListener } from "./listeners";
2
2
  import { CACHE_PROTOCOL, CACHE_STORAGE_PATH, DEFAULT_CACHE_PASSWORD } from "./values";
3
3
  import { CacheStore, Scoped } from "./variables";
4
- import { decryptString, encryptString, niceTry, serializeE2E } from "./peripherals";
4
+ import { decryptString, encryptString, serializeE2E } from "./peripherals";
5
5
 
6
6
  export const updateCacheStore = () => {
7
7
  try { window } catch (_) { return; }
@@ -5,6 +5,7 @@ export const Scoped = {
5
5
  IS_TOKEN_READY: {},
6
6
  InitializedProject: {},
7
7
  ReleaseCacheData: undefined,
8
+ IsStoreReady: false,
8
9
  AuthJWTToken: {},
9
10
  cacheStorageReducer: undefined,
10
11
  TokenRefreshTimer: {},
package/src/index.d.ts CHANGED
@@ -290,6 +290,7 @@ interface DocumentFind {
290
290
  $or?: any[];
291
291
  $text?: {
292
292
  $search: string;
293
+ $field: string;
293
294
  $language?: string;
294
295
  $caseSensitive?: boolean;
295
296
  $diacriticSensitive?: boolean;
@@ -319,8 +320,11 @@ interface MTAuth {
319
320
  listenVerifiedStatus: (callback?: (verified?: boolean) => void, onError?: (error?: ErrorResponse) => void) => () => void;
320
321
  listenAuthToken: (callback: (token: string) => void) => () => void;
321
322
  getAuthToken: () => Promise<string>;
322
- listenAuth: (callback: (auth: AuthData) => void) => () => void;
323
- getAuth: () => Promise<AuthData>;
323
+ getRefreshToken: () => Promise<string>;
324
+ getRefreshTokenData: () => Promise<RefreshTokenData>;
325
+ parseToken: () => string;
326
+ listenAuth: (callback: (auth: TokenEventData) => void) => () => void;
327
+ getAuth: () => Promise<TokenEventData>;
324
328
  signOut: () => Promise<void>;
325
329
  forceRefreshToken: () => Promise<string>;
326
330
  }
@@ -341,14 +345,35 @@ interface AuthData {
341
345
  signupMethod: 'google' | 'apple' | 'custom' | 'github' | 'twitter' | 'facebook' | string;
342
346
  currentAuthMethod: 'google' | 'apple' | 'custom' | 'github' | 'twitter' | 'facebook' | string;
343
347
  joinedOn: number;
344
- encryptionKey: string;
345
348
  uid: string;
346
349
  claims: Object;
347
350
  emailVerified: boolean;
351
+ tokenID: string;
352
+ disabled: boolean;
353
+ entityOf: string;
348
354
  profile: {
349
355
  photo: string;
350
356
  name: string;
351
- }
357
+ },
358
+ exp: number;
359
+ aud: string;
360
+ iss: string;
361
+ sub: string;
362
+ }
363
+
364
+ interface RefreshTokenData {
365
+ uid: string;
366
+ tokenID: string;
367
+ isRefreshToken: true;
368
+ }
369
+
370
+ interface TokenEventData extends AuthData {
371
+ tokenManager: TokenManager | null;
372
+ }
373
+
374
+ interface TokenManager {
375
+ refreshToken: string;
376
+ accessToken: string;
352
377
  }
353
378
 
354
379
  declare type Base64String = string;
package/src/index.js CHANGED
@@ -300,26 +300,23 @@ export class MosquitoTransport {
300
300
  const validateReleaseCacheProp = (prop) => {
301
301
  const cacheList = [...Object.values(CACHE_PROTOCOL)];
302
302
 
303
- if (prop?.io) {
304
- Object.entries(prop).forEach(([k, v]) => {
305
- if (k === 'input' || k === 'output') {
306
- if (typeof v !== 'function')
307
- throw `Invalid value supplied to ${k}, expected a function but got "${v}"`;
308
- } else if (k === 'cachePassword') {
309
- if (typeof v !== 'string' || v.trim().length <= 0)
310
- throw `Invalid value supplied to cachePassword, value must be a string and greater than 0 characters`;
311
- } else throw `Unexpected property named ${k}`;
312
- });
313
- } else {
314
- Object.entries(prop).forEach(([k, v]) => {
315
- if (k === 'cachePassword') {
316
- if (typeof v !== 'string' || v.trim().length <= 0)
317
- throw `Invalid value supplied to cachePassword, value must be a string and greater than 0 characters`;
318
- } else if (k === 'cacheProtocol') {
319
- if (!cacheList.includes(`${v}`)) throw `unknown value supplied to ${k}, expected any of ${cacheList}`;
320
- } else throw `Unexpected property named ${k}`;
321
- });
322
- }
303
+ Object.entries(prop).forEach(([k, v]) => {
304
+ if (k === 'cachePassword') {
305
+ if (typeof v !== 'string' || v.trim().length <= 0)
306
+ throw `Invalid value supplied to cachePassword, value must be a string and greater than 0 characters`;
307
+ } else if (k === 'cacheProtocol') {
308
+ if (!cacheList.includes(`${v}`)) throw `unknown value supplied to ${k}, expected any of ${cacheList}`;
309
+ } else if (k === 'io') {
310
+ Object.entries(v).forEach(([k, v]) => {
311
+ if (k === 'input' || k === 'output') {
312
+ if (typeof v !== 'function')
313
+ throw `Invalid value supplied to "io.${k}", expected a function but got "${v}"`;
314
+ } else throw `Unexpected property named "io.${k}"`;
315
+ });
316
+ } else throw `Unexpected property named ${k}`;
317
+ });
318
+
319
+ if (!prop?.io && !prop?.cacheProtocol) throw 'You need to provide either "io" or "cacheProtocol"';
323
320
  }
324
321
 
325
322
  const validator = {
@@ -2,8 +2,8 @@ import setLargeTimeout from "set-large-timeout";
2
2
  import { doSignOut } from ".";
3
3
  import EngineApi from "../../helpers/EngineApi";
4
4
  import { AuthTokenListener, TokenRefreshListener } from "../../helpers/listeners";
5
- import { decodeBinary, deserializeE2E, listenReachableServer, simplifyCaughtError } from "../../helpers/peripherals";
6
- import { awaitReachableServer, awaitStore, buildFetchInterface, simplifyError, updateCacheStore } from "../../helpers/utils";
5
+ import { decodeBinary, deserializeE2E, listenReachableServer } from "../../helpers/peripherals";
6
+ import { awaitStore, buildFetchInterface, simplifyError, updateCacheStore } from "../../helpers/utils";
7
7
  import { CacheStore, Scoped } from "../../helpers/variables";
8
8
 
9
9
  export const listenToken = (callback, projectUrl) =>
@@ -15,6 +15,7 @@ export const listenToken = (callback, projectUrl) =>
15
15
  export const injectFreshToken = async (config, { token, refreshToken }) => {
16
16
  const { projectUrl } = config;
17
17
 
18
+ await awaitStore();
18
19
  CacheStore.AuthStore[projectUrl] = { token, refreshToken };
19
20
  Scoped.AuthJWTToken[projectUrl] = token;
20
21
  updateCacheStore();
@@ -105,6 +105,13 @@ export class MTAuth {
105
105
 
106
106
  listenAuthToken = (callback) => listenToken(callback, this.builder.projectUrl);
107
107
 
108
+ getRefreshToken = async () => {
109
+ await awaitStore();
110
+ return CacheStore.AuthStore[this.builder.projectUrl]?.refreshToken;
111
+ }
112
+
113
+ parseToken = (token) => parseToken(token);
114
+
108
115
  getAuthToken = () => new Promise(resolve => {
109
116
  const l = listenToken(t => {
110
117
  l();