added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / feathers.service.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17     \r
18 import { Injectable, OnInit } from '@angular/core';\r
19 \r
20 import * as feathers from '@feathersjs/client';\r
21 import * as io from 'socket.io-client';\r
22 import * as socketio from '@feathersjs/socketio-client';\r
23 import * as authentication from '@feathersjs/authentication-client';\r
24 import { Observable, from, interval } from 'rxjs';\r
25 import { now } from 'moment';\r
26 import { AppGlobals } from 'app/app.global';\r
27 import { CookieService } from 'ngx-cookie-service';\r
28 import { Router } from '@angular/router';\r
29 \r
30 \r
31 @Injectable({\r
32   providedIn: 'root'\r
33 })\r
34 export class FeathersService {\r
35   // There are no proper typings available for feathers, due to its plugin-heavy nature\r
36   private _feathers: any;\r
37   public _socket: any;\r
38   public auth: Observable<Object>;\r
39   \r
40   constructor(private route: Router) {\r
41     this._socket = io('/',{\r
42       transports: ['websocket']\r
43     });       // init socket.io\r
44     this._socket.on('connect_error', function(data){\r
45       route.navigateByUrl('/login');\r
46     });\r
47     this._feathers = feathers();                      // init Feathers             // add hooks plugin\r
48     this._feathers.configure(socketio(this._socket, {\r
49         timeout: 100000000\r
50     })); // add socket.io plugin\r
51     this._feathers.configure(authentication({\r
52         storage: window.localStorage,\r
53         storageKey: 'access_token'\r
54     }));\r
55 \r
56     //set observiable for services to check before calling the service\r
57     this.auth = from(this._feathers.authenticate());\r
58     \r
59   }\r
60 \r
61   // expose services\r
62   public service(name: string) {\r
63     return this._feathers.service(name);\r
64   }\r
65 \r
66   public socket(){\r
67     return this._socket;\r
68   }\r
69 \r
70   // expose authentication\r
71   public authenticate(credentials?): Observable<Object> { \r
72     return new Observable(observer => {\r
73       this.auth = from(this._feathers.authenticate(credentials).then(res => {\r
74         observer.next(res);\r
75       }, err => {\r
76         observer.error(err);\r
77         this.route.navigate(['/login'])\r
78       }));\r
79     });\r
80 \r
81   }\r
82 \r
83   // expose logout\r
84   public logout() {\r
85     return this._feathers.logout();\r
86   }\r
87 }\r
88 \r