@gudhub/core 1.2.1 → 1.2.3

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.
@@ -10,6 +10,12 @@ export class Auth {
10
10
  return user;
11
11
  }
12
12
 
13
+ async loginWithToken(token) {
14
+ const user = await this.loginWithTokenApi(token);
15
+ this.storage.updateUser(user);
16
+ return user;
17
+ }
18
+
13
19
  async logout(token) {
14
20
  const response = await this.logoutApi(token);
15
21
  return response;
@@ -58,6 +64,18 @@ export class Auth {
58
64
  }
59
65
  }
60
66
 
67
+ async loginWithTokenApi(token) {
68
+ try {
69
+ const user = await this.req.axiosRequest({
70
+ method: 'POST',
71
+ url: `${this.req.root}/auth/login?accesstoken=${token}`
72
+ });
73
+ return user;
74
+ } catch (error) {
75
+ console.log(error);
76
+ }
77
+ }
78
+
61
79
  async updateTokenApi(auth_key) {
62
80
  try {
63
81
  const user = await this.req.axiosRequest({
@@ -1,4 +1,4 @@
1
- import should from "should";
1
+
2
2
  import {GudHub} from './../gudhub.js';
3
3
 
4
4
  describe("AUTHORIZATION", async function() {
@@ -22,6 +22,16 @@ describe("AUTHORIZATION", async function() {
22
22
  user.fullname.should.equal('Vasya Pupkin');
23
23
  })
24
24
 
25
+ it("Authentication with access token", async function () {
26
+ const gudhub = new GudHub();
27
+
28
+ const token = 'accesstoken';
29
+
30
+ const user = await gudhub.loginWithToken(token);
31
+
32
+ user.hasOwnProperty('fullname').should.equal(true);
33
+ })
34
+
25
35
  it("Signing up user", async function () {
26
36
  const gudhub = new GudHub();
27
37
  let credentials = {
@@ -0,0 +1,19 @@
1
+ export class ChunkAPI {
2
+ constructor(req) {
3
+ this.req = req;
4
+ }
5
+
6
+ async getChunk(app_id, chunk_id) {
7
+ return this.req.get({
8
+ url: `${this.req.root}/api/get-items-chunk/${app_id}/${chunk_id}`,
9
+ method: 'GET'
10
+ });
11
+ }
12
+
13
+ async getLastChunk(app_id) {
14
+ return this.req.axiosRequest({
15
+ url: `${this.req.root}/api/get-last-items-chunk/${app_id}`,
16
+ method: 'GET'
17
+ });
18
+ }
19
+ }
@@ -0,0 +1,101 @@
1
+ import { ChunkDataService, HttpDataService } from "./httpDataService.js";
2
+
3
+
4
+ //this should be global in project
5
+ // class IndexedDBFacade extends CacheService {
6
+
7
+ // }
8
+
9
+
10
+
11
+ class ChunkCachedDataService extends ChunkDataService {
12
+ dataService;
13
+ }
14
+
15
+
16
+ export class IndexedDBChunkCacheService extends ChunkCachedDataService {
17
+ constructor(req) {
18
+ super();
19
+
20
+ this.dataService = new HttpDataService(req);
21
+ }
22
+
23
+
24
+ OBJECT_STORE = "chunks";
25
+ DatabaseName = "gudhubapi-data";
26
+ DatabaseVersion = 1;
27
+
28
+
29
+ //TODO use IndexedDBFacade here
30
+ // indexDBAccess = new IndexedDBFacade;
31
+
32
+
33
+ async putChunk(id, data) {
34
+ try {
35
+ const db = await this.openDatabase();
36
+
37
+ const transaction = db.transaction(this.OBJECT_STORE, "readwrite");
38
+ const store = transaction.objectStore(this.OBJECT_STORE);
39
+
40
+ store.put(data, id);
41
+ } catch (error) {
42
+
43
+ }
44
+ }
45
+
46
+
47
+ async getChunk(app_id, id) {
48
+ try {
49
+ const db = await this.openDatabase();
50
+ const transaction = db.transaction(this.OBJECT_STORE, "readonly");
51
+ const store = transaction.objectStore(this.OBJECT_STORE);
52
+
53
+ const storeRequest = store.get(id);
54
+
55
+ return await new Promise((resolve, reject) => {
56
+ storeRequest.onsuccess = (e) => {
57
+
58
+ let cachedData = e.target.result;
59
+
60
+ if (
61
+ !cachedData
62
+ ) {
63
+ reject();
64
+ }
65
+
66
+ if (
67
+ cachedData
68
+ ) {
69
+
70
+ resolve(cachedData);
71
+ }
72
+ };
73
+
74
+ storeRequest.onerror = reject
75
+ });
76
+ } catch(e) {
77
+ let res = await this.dataService.getChunk(app_id, id);
78
+
79
+ await this.putChunk(id, res);
80
+
81
+
82
+ return res;
83
+ }
84
+ }
85
+
86
+ async openDatabase() {
87
+ return new Promise((resolve, reject) => {
88
+ const request = indexedDB.open(this.DatabaseName, this.DatabaseVersion);
89
+ request.onupgradeneeded = event => {
90
+ const db = event.target.result;
91
+ db.createObjectStore(this.OBJECT_STORE);
92
+ };
93
+ request.onsuccess = event => {
94
+ resolve(event.target.result);
95
+ };
96
+ request.onerror = event => {
97
+ reject(event.target.error);
98
+ };
99
+ });
100
+ }
101
+ }
@@ -0,0 +1,13 @@
1
+ import { IS_WEB } from "../../consts.js";
2
+ import { IndexedDBChunkCacheService } from "./IndexedDBCacheService.js";
3
+ import { HttpDataService } from "./httpDataService.js";
4
+
5
+ let exportClass;
6
+
7
+ if (IS_WEB) {
8
+ exportClass = IndexedDBChunkCacheService
9
+ } else {
10
+ exportClass = HttpDataService
11
+ }
12
+
13
+ export { exportClass as DataService };
@@ -0,0 +1,21 @@
1
+ import { ChunkAPI } from "../ChunkApi.js";
2
+
3
+ export class ChunkDataService {}
4
+
5
+
6
+ export class HttpDataService extends ChunkDataService {
7
+ constructor(req) {
8
+ super();
9
+
10
+ this.chunkApi = new ChunkAPI(req);
11
+ }
12
+
13
+ async getChunk(app_id, id) {
14
+ return this.chunkApi.getChunk(app_id, id);
15
+ }
16
+
17
+
18
+ putChunk() {
19
+ //do nothing
20
+ }
21
+ }
@@ -1,5 +1,5 @@
1
1
  import { AppDataService } from "../AppDataService.js";
2
- import { ChunkCacheDataService, ChunkDataService } from "../ChunkDataService.js";
2
+ import { ChunkDataService } from "../ChunkDataService.js"; // removed "ChunkCacheDataService" from imported because it was causing an error "{ ChunkCacheDataService } not found in exported names" in "gudhub-node-server"
3
3
  import { AppHttpService } from "../httpService/AppHttpService.js";
4
4
  import { objectAssignWithOverride } from "../utils.js";
5
5
  import { IndexedDBService } from "./IndexedDBService.js";
@@ -1,4 +1,4 @@
1
- import { ChunkCacheDataService, ChunkDataService } from "../ChunkDataService.js";
1
+ import { ChunkDataService } from "../ChunkDataService.js"; // removed "ChunkCacheDataService" from imported because it was causing an error "{ ChunkCacheDataService } not found in exported names" in "gudhub-node-server"
2
2
  import { ChunkHttpService } from "../httpService/ChunkHttpService.js";
3
3
  import { objectAssignWithOverride } from "../utils.js";
4
4
  import { IndexedDBService } from "./IndexedDBService.js";
package/GUDHUB/gudhub.js CHANGED
@@ -32,7 +32,9 @@ export class GudHub {
32
32
  swLink: "",
33
33
  async_modules_path,
34
34
  file_server_url,
35
- automation_modules_path
35
+ automation_modules_path,
36
+ accesstoken: this.accesstoken,
37
+ expirydate: this.expirydate
36
38
  }
37
39
  ) {
38
40
  this.config = options;
@@ -45,7 +47,15 @@ export class GudHub {
45
47
  this.auth = new Auth(this.req, this.storage);
46
48
  this.sharing = new Sharing(this, this.req);
47
49
  this.groupSharing = new GroupSharing(this, this.req, this.pipeService);
48
- if (authKey) this.storage.setUser({ auth_key: authKey });
50
+
51
+ // Login with access token - pass to setUser access token and after it user can make requests with access token
52
+ // This method created for optimize GudHub initialization without auth key
53
+ if (authKey) {
54
+ this.storage.setUser({ auth_key: authKey })
55
+ } else if(options.accesstoken && options.expirydate) {
56
+ this.storage.setUser({accesstoken: options.accesstoken, expirydate: options.expirydate})
57
+ }
58
+
49
59
  this.req.init(this.auth.getToken.bind(this.auth));
50
60
 
51
61
  this.ws = new WebSocketApi(options.wss_url, this.auth);
@@ -445,6 +455,10 @@ export class GudHub {
445
455
  return this.auth.login(data);
446
456
  }
447
457
 
458
+ loginWithToken(token) {
459
+ return this.auth.loginWithToken(token);
460
+ }
461
+
448
462
  logout(token) {
449
463
  this.appProcessor.clearAppProcessor();
450
464
  return this.auth.logout(token);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {