UNPKG

6.79 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>JSDoc: Source: index.js</title>
6
7 <script src="scripts/prettify/prettify.js"> </script>
8 <script src="scripts/prettify/lang-css.js"> </script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14</head>
15
16<body>
17
18<div id="main">
19
20 <h1 class="page-title">Source: index.js</h1>
21
22
23
24
25
26
27 <section>
28 <article>
29 <pre class="prettyprint source linenums"><code>/*
30Copyright 2016 CREATE-NET
31
32Licensed under the Apache License, Version 2.0 (the "License");
33you may not use this file except in compliance with the License.
34You may obtain a copy of the License at
35
36http://www.apache.org/licenses/LICENSE-2.0
37
38Unless required by applicable law or agreed to in writing, software
39distributed under the License is distributed on an "AS IS" BASIS,
40WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41See the License for the specific language governing permissions and
42limitations under the License.
43*/
44
45var d = require("debug")("raptorjs:index");
46
47var Promise = require("bluebird");
48var _ = require("lodash");
49var Client = require('./lib/client');
50var ServiceObject = require("./lib/model/ServiceObject");
51
52/**
53 * Raptor SDK wrapper
54 *
55 * @author Luca Capra &lt;lcapra@fbk.eu>
56 * @copyright CREATE-NET
57 * @license Apache-2.0
58 *
59 * @constructor
60 * @param {Object} config configuration object
61 */
62var Raptor = function (config) {
63
64 var instance = this;
65
66 // keep reference to Promise object
67 this.Promise = Promise
68
69 var defaultConfig = {
70 apiKey: null,
71 username: null,
72 password: null,
73 url: "https://api.raptorbox.eu",
74 debug: false
75 };
76
77 config = config || {};
78
79 if(typeof config === 'string') {
80 config = {
81 apiKey: config
82 };
83 }
84
85 this.config = {};
86
87 _.each(defaultConfig, function(val, key) {
88 instance.config[key] =
89 (config[key] === undefined) ? val : config[key];
90 });
91
92 if(instance.config.apiKey) {
93 var apikeyPrefix = "Bearer ";
94 if(instance.config.apiKey.substr(0, apikeyPrefix.length) !== apikeyPrefix ) {
95 instance.config.apiKey = apikeyPrefix + instance.config.apiKey;
96 }
97 }
98
99 this.isBrowser = (typeof window !== 'undefined');
100
101 if(this.isBrowser) {
102 d("Running in browser");
103 }
104
105 d("Client configuration: %j", this.config);
106
107 /**
108 * Return a API client instance
109 */
110 this.client = new Client(this);
111
112 // add Promise reference
113 this.Promise = Promise;
114
115 this.newObject = function (data) {
116 var obj = new ServiceObject(data, instance);
117 return obj;
118 };
119
120 /**
121 * Create a Service Object from an object or a WebObject
122 *
123 * @param {Object} wo ServiceObject compatible definition object or WebObject
124 *
125 * @return {Promise} Promise for the future ServiceObject created
126 * */
127 this.create = function (data) {
128 var obj = instance.newObject(data);
129 return obj.create();
130 };
131
132 /**
133 * Delete a Service Object by id
134 *
135 * @param {String} objectId ServiceObject id
136 *
137 * @return {Promise} Promise for the future result of the operation
138 * */
139 this.delete = function (id) {
140 var obj = instance.newObject({
141 id: id
142 });
143 return obj.delete();
144 };
145
146 /**
147 * @param {String} id ServiceObject id
148 *
149 * @return {Promise} A promise with the created SO
150 */
151 this.load = function (id) {
152 var obj = instance.newObject({
153 id: id,
154 name: "unknown"
155 });
156 return obj.load();
157 };
158
159 /**
160 * Retrieve all the Service Objects from a given user (identified by the Authorization header).
161 *
162 * @return {Promise}
163 */
164 this.list = function () {
165 return instance.client.get('/').then(function (data) {
166 var json = (typeof data === 'string') ? JSON.parse(data) : data;
167 return Promise.resolve(json);
168 });
169 };
170
171 /**
172 * Search for Service Objects.
173 * Example parameters:
174 * 1) free-form query: { query: "some params" }
175 * 2) field params:
176 * {
177 * name: "Object Name",
178 * description: "optional description"
179 * customFields: {
180 * param1: "value"
181 * }
182 * }
183 *
184 *
185 * @params Object search parameters
186 * @return {Promise}
187 */
188 this.find = this.search = function (params) {
189 if(typeof params === 'string') {
190 params = { search: params };
191 }
192 return instance.client.post('/search', params)
193 .then(function (data) {
194 var json = (typeof data === 'string') ? JSON.parse(data) : data;
195 return Promise.resolve(json);
196 });
197 };
198
199 this.fromJSON = function (json) {
200 if(typeof json === 'string') json = JSON.parse(json);
201 return instance.newObject(json);
202 };
203
204 // Auth related API
205 var _authBasePath = this.config.authBasePath === undefined ? '/auth' : this.config.authBasePath;
206 this.authBasePath = function(u) {
207 return _authBasePath + u;
208 }
209
210 this.auth = require('./lib/auth/index')(this);
211
212 this.getUser = function(info) {
213 return instance.auth.getUser(info)
214 };
215
216 this.currentUser = function() {
217 return instance.auth.currentUser() || null;
218 };
219
220 this.setUser = function(user) {
221 return instance.auth.setUser(user)
222 };
223
224 this.setToken = function(token) {
225 return instance.auth.setToken(token)
226 };
227
228};
229
230module.exports = Raptor;
231module.exports.permissions = {
232 CREATE: "create",
233 WRITE: "write",
234 UPDATE: "update",
235 DELETE: "delete",
236 ADMINISTRATION: "administration",
237 PUSH: "push",
238 PULL: "pull",
239 SUBSCRIBE: "subscribe",
240 EXECUTE: "execute",
241 LIST: "list",
242}
243module.exports.RecordSet = require("./lib/model/RecordSet")
244module.exports.ResultSet = require("./lib/model/ResultSet")
245module.exports.Promise = Promise
246</code></pre>
247 </article>
248 </section>
249
250
251
252
253</div>
254
255<nav>
256 <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Action.html">Action</a></li><li><a href="Channel.html">Channel</a></li><li><a href="Client.html">Client</a></li><li><a href="Raptor.html">Raptor</a></li><li><a href="RecordSet.html">RecordSet</a></li><li><a href="ResultSet.html">ResultSet</a></li><li><a href="ServiceObject.html">ServiceObject</a></li><li><a href="Stream.html">Stream</a></li><li><a href="User.html">User</a></li></ul><h3>Global</h3><ul><li><a href="global.html#Container">Container</a></li></ul>
257</nav>
258
259<br class="clear">
260
261<footer>
262 Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Wed Nov 02 2016 11:15:06 GMT+0100 (CET)
263</footer>
264
265<script> prettyPrint(); </script>
266<script src="scripts/linenumber.js"> </script>
267</body>
268</html>