1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const d = require("debug")("raptorjs:index")
|
18 | const models = require("./lib/model/models")
|
19 | const EventEmitter = require("eventemitter3")
|
20 |
|
21 | const BASE_URL = "http://raptor.local"
|
22 |
|
23 | const VERSION = require("./package.json").version
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | class 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 |
|
57 | mqttUrl: null,
|
58 | debug: false,
|
59 |
|
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 |
|
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 |
|
93 | this.config.domain = this.config.domain || this.config.app || this.config.appId
|
94 |
|
95 |
|
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 | Diagnostics() {
|
189 | if(!this.diagnostics) {
|
190 | const Diagnostics = require("./lib/Diagnostics")
|
191 | this.diagnostics = new Diagnostics(this)
|
192 | }
|
193 | return this.diagnostics
|
194 | }
|
195 | }
|
196 |
|
197 | module.exports = Raptor
|
198 | module.exports.models = models
|