UNPKG

4.86 kBJavaScriptView Raw
1/*
2Copyright 2016 CREATE-NET
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17const d = require("debug")("raptorjs:index")
18const models = require("./lib/model/models")
19const EventEmitter = require("eventemitter3")
20
21const BASE_URL = "http://raptor.local"
22
23const VERSION = require("./package.json").version
24
25/**
26 * Raptor SDK wrapper
27 *
28 * @author Luca Capra <lcapra@fbk.eu>
29 * @copyright FBK/CREATE-NET
30 * @license Apache-2.0
31 *
32 * @constructor
33 * @param {Object} config configuration object
34 */
35class Raptor extends EventEmitter {
36
37 constructor(config) {
38
39 super()
40
41 this.version = VERSION
42
43 this.config = {}
44 this.isBrowser = (typeof window !== "undefined")
45
46 this.permissions = require("./lib/permissions")
47 this.routes = require("./lib/routes")
48 this.models = models
49
50 this.defaultConfig = {
51 domain: null,
52 token: null,
53 username: null,
54 password: null,
55 url: null,
56 // full url of the MQTT broker
57 mqttUrl: null,
58 debug: false,
59 // oauth2
60 clientId: null,
61 clientSecret: null,
62 scopes: null,
63 }
64
65
66 if(typeof config === "string") {
67 config = {
68 token: config
69 }
70 }
71
72 this.setConfig(config)
73 }
74
75 getConfig() {
76 return this.config
77 }
78
79 setConfig(cfg) {
80
81 this.config = Object.assign({}, this.defaultConfig, cfg)
82
83 if (!this.config.url) {
84 let baseUrl = BASE_URL
85 // set default to current page
86 if (this.isBrowser) {
87 baseUrl = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":"+window.location.port : "")
88 }
89 this.config.url = baseUrl
90 }
91
92 //allow domain alias
93 this.config.domain = this.config.domain || this.config.app || this.config.appId
94
95 // reset oauth2 url
96 if(this.config.clientId) {
97 this.config = Object.assign(this.config, {
98 accessTokenUri: this.config.accessTokenUri || `${this.config.url}${this.routes.accessTokenUri}`,
99 authorizationUri: this.config.authorizationUri || `${this.config.url}${this.routes.authorizationUri}`,
100 redirectUri: this.config.redirectUri || `${this.config.url}${this.routes.redirectUri}`,
101 })
102 }
103
104 this.Auth().reset()
105 d("Client configuration: %j", this.config)
106 }
107
108 getClient() {
109 if(!this.client) {
110 const Client = require("./lib/Client")
111 this.client = new Client(this)
112 }
113 return this.client
114 }
115
116 Auth() {
117 if(!this.auth) {
118 const Auth = require("./lib/Auth")
119 this.auth = new Auth(this)
120 }
121 return this.auth
122 }
123
124 Admin() {
125 if(!this.admin) {
126 const Admin = require("./lib/Admin")
127 this.admin = new Admin(this)
128 }
129 return this.admin
130 }
131
132 App() {
133 if(!this.app) {
134 const App = require("./lib/App")
135 this.app = new App(this)
136 }
137 return this.app
138 }
139
140 Profile() {
141 if(!this.profile) {
142 const Profile = require("./lib/Profile")
143 this.profile = new Profile(this)
144 }
145 return this.profile
146 }
147
148 Inventory() {
149 if(!this.inventory) {
150 const Inventory = require("./lib/Inventory")
151 this.inventory = new Inventory(this)
152 }
153 return this.inventory
154 }
155
156 Workflow() {
157 if(!this.workflow) {
158 const Workflow = require("./lib/Workflow")
159 this.workflow = new Workflow(this)
160 }
161 return this.workflow
162 }
163
164 Stream() {
165 if(!this.stream) {
166 const Stream = require("./lib/Stream")
167 this.stream = new Stream(this)
168 }
169 return this.stream
170 }
171
172 Action() {
173 if(!this.action) {
174 const Action = require("./lib/Action")
175 this.action = new Action(this)
176 }
177 return this.action
178 }
179
180 Tree() {
181 if(!this.tree) {
182 const Tree = require("./lib/Tree")
183 this.tree = new Tree(this)
184 }
185 return this.tree
186 }
187
188}
189
190module.exports = Raptor
191module.exports.models = models