pryv 2.1.8 → 2.3.0

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.
Files changed (46) hide show
  1. package/package.json +12 -45
  2. package/src/Auth/AuthController.js +43 -50
  3. package/src/Auth/AuthStates.js +12 -12
  4. package/src/Auth/LoginMessages.js +17 -14
  5. package/src/Auth/index.js +18 -15
  6. package/src/Browser/CookieUtils.js +37 -27
  7. package/src/Browser/LoginButton.js +42 -37
  8. package/src/Browser/index.js +16 -42
  9. package/src/Connection.js +102 -95
  10. package/src/Service.js +47 -45
  11. package/src/ServiceAssets.js +42 -34
  12. package/src/browser-index-bundle.js +8 -0
  13. package/src/browser-index.js +7 -0
  14. package/src/index.d.ts +833 -0
  15. package/src/index.js +20 -3
  16. package/src/lib/browser-getEventStreamed.js +21 -19
  17. package/src/lib/json-parser.js +23 -24
  18. package/src/utils.js +55 -43
  19. package/test/Browser.AuthController.test.js +19 -21
  20. package/test/Browser.test.js +23 -26
  21. package/test/Connection.test.js +164 -162
  22. package/test/Service.test.js +30 -44
  23. package/test/ServiceAssets.test.js +16 -22
  24. package/test/browser-index.html +26 -0
  25. package/test/utils.test.js +30 -35
  26. package/.jsdoc-conf.json +0 -29
  27. package/.mocharc.js +0 -13
  28. package/LICENSE.md +0 -27
  29. package/README.md +0 -723
  30. package/scripts/setup-environment-dev.sh +0 -28
  31. package/scripts/upload.sh +0 -15
  32. package/src/Pryv.js +0 -19
  33. package/src/index-socket.io-monitor.js +0 -4
  34. package/src/index.html +0 -17
  35. package/test/browser-index.js +0 -11
  36. package/test/browser-tests.html +0 -31
  37. package/test/helpers.js +0 -8
  38. package/test/load-test-account.js +0 -108
  39. package/test/test-data.js +0 -92
  40. package/web-demos/auth-with-redirection.html +0 -72
  41. package/web-demos/auth.html +0 -77
  42. package/web-demos/custom-login-button.html +0 -158
  43. package/web-demos/index.html +0 -186
  44. package/web-demos/service-info.json +0 -13
  45. package/web-demos/stream-examples.html +0 -80
  46. package/webpack.config.js +0 -71
package/src/Service.js CHANGED
@@ -1,25 +1,27 @@
1
-
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
2
5
  const utils = require('./utils.js');
3
6
  // Connection is required at the end of this file to allow circular requires.
4
7
  const Assets = require('./ServiceAssets.js');
5
8
 
6
-
7
9
  /**
8
- * @class Pryv.Service
10
+ * @class pryv.Service
9
11
  * A Pryv.io deployment is a unique "Service", as an example **Pryv Lab** is a service, deployed with the domain name **pryv.me**.
10
- *
11
- * `Pryv.Service` exposes tools to interact with Pryv.io at a "Platform" level.
12
+ *
13
+ * `pryv.Service` exposes tools to interact with Pryv.io at a "Platform" level.
12
14
  *
13
15
  * ##### Initizalization with a service info URL
14
- ```javascript
15
- const service = new Pryv.Service('https://reg.pryv.me/service/info');
16
+ ```js
17
+ const service = new pryv.Service('https://reg.pryv.me/service/info');
16
18
  ```
17
19
 
18
20
  - With the content of a serviceInfo configuration
19
21
 
20
22
  Service information properties can be overriden with specific values. This might be usefull to test new designs on production platforms.
21
23
 
22
- ```javascript
24
+ ```js
23
25
  const serviceInfoUrl = 'https://reg.pryv.me/service/info';
24
26
  const serviceCustomizations = {
25
27
  name: 'Pryv Lab 2',
@@ -27,68 +29,67 @@ const serviceCustomizations = {
27
29
  definitions: 'https://pryv.github.io/assets-pryv.me/index.json'
28
30
  }
29
31
  }
30
- const service = new Pryv.Service(serviceInfoUrl, serviceCustomizations);
31
- ```
32
+ const service = new pryv.Service(serviceInfoUrl, serviceCustomizations);
33
+ ```
32
34
 
33
- * @memberof Pryv
34
- *
35
+ * @memberof pryv
36
+ *
35
37
  * @constructor
36
38
  * @param {string} serviceInfoUrl Url point to /service/info of a Pryv platform see: {@link https://api.pryv.com/reference/#service-info}
37
39
  */
38
40
  class Service {
39
-
40
41
  constructor (serviceInfoUrl, serviceCustomizations) {
41
- this._pryvServiceInfo = null;
42
+ this._serviceInfo = null;
42
43
  this._assets = null;
43
44
  this._polling = false;
44
- this._pryvServiceInfoUrl = serviceInfoUrl;
45
+ this._serviceInfoUrl = serviceInfoUrl;
45
46
  this._pryvServiceCustomizations = serviceCustomizations;
46
47
  }
47
48
 
48
49
  /**
49
50
  * Return service info parameters info known of fetch it if needed.
50
- * Example
51
- * - name of a platform
52
- * `const serviceName = await service.info().name`
53
- * @see PryvServiceInfo For details on available properties.
51
+ * Example
52
+ * - name of a platform
53
+ * `const serviceName = await service.info().name`
54
+ * @see ServiceInfo For details on available properties.
54
55
  * @param {boolean?} forceFetch If true, will force fetching service info.
55
- * @returns {Promise<PryvServiceInfo>} Promise to Service info Object
56
+ * @returns {Promise<ServiceInfo>} Promise to Service info Object
56
57
  */
57
58
  async info (forceFetch) {
58
- if (forceFetch || !this._pryvServiceInfo) {
59
+ if (forceFetch || !this._serviceInfo) {
59
60
  let baseServiceInfo = {};
60
- if (this._pryvServiceInfoUrl) {
61
- const res = await utils.superagent.get(this._pryvServiceInfoUrl).set('Access-Control-Allow-Origin', '*').set('accept', 'json');
61
+ if (this._serviceInfoUrl) {
62
+ const res = await utils.superagent.get(this._serviceInfoUrl).set('Access-Control-Allow-Origin', '*').set('accept', 'json');
62
63
  baseServiceInfo = res.body;
63
64
  }
64
65
  Object.assign(baseServiceInfo, this._pryvServiceCustomizations);
65
66
  this.setServiceInfo(baseServiceInfo);
66
67
  }
67
- return this._pryvServiceInfo;
68
+ return this._serviceInfo;
68
69
  }
69
70
 
70
71
  /**
71
72
  * @private
72
- * @param {PryvServiceInfo} serviceInfo
73
+ * @param {ServiceInfo} serviceInfo
73
74
  */
74
75
  setServiceInfo (serviceInfo) {
75
76
  if (!serviceInfo.name) {
76
77
  throw new Error('Invalid data from service/info');
77
78
  }
78
- // cleanup serviceInfo for eventual url not finishing by "/"
79
+ // cleanup serviceInfo for eventual url not finishing by "/"
79
80
  // code will be obsolete with next version of register
80
81
  ['access', 'api', 'register'].forEach((key) => {
81
82
  if (serviceInfo[key].slice(-1) !== '/') {
82
83
  serviceInfo[key] += '/';
83
84
  }
84
85
  });
85
- this._pryvServiceInfo = serviceInfo;
86
+ this._serviceInfo = serviceInfo;
86
87
  }
87
88
 
88
89
  /**
89
90
  * Return assets property content
90
91
  * @param {boolean?} forceFetch If true, will force fetching service info.
91
- * @returns {Promise<ServiceAssets>} Promise to ServiceAssets
92
+ * @returns {Promise<ServiceAssets>} Promise to ServiceAssets
92
93
  */
93
94
  async assets (forceFetch) {
94
95
  if (!forceFetch && this._assets) {
@@ -106,17 +107,17 @@ class Service {
106
107
 
107
108
  /**
108
109
  * Return service info parameters info known or null if not yet loaded
109
- * @returns {PryvServiceInfo} Service Info definition
110
+ * @returns {ServiceInfo} Service Info definition
110
111
  */
111
112
  infoSync () {
112
- return this._pryvServiceInfo;
113
+ return this._serviceInfo;
113
114
  }
114
115
 
115
116
  /**
116
117
  * Return an API Endpoint from a username and token
117
118
  * @param {string} username
118
119
  * @param {string} [token]
119
- * @return {PryvApiEndpoint}
120
+ * @return {APIEndpoint}
120
121
  */
121
122
  async apiEndpointFor (username, token) {
122
123
  const serviceInfo = await this.info();
@@ -124,25 +125,25 @@ class Service {
124
125
  }
125
126
 
126
127
  /**
127
- * Return an API Endpoint from a username and token and a PryvServiceInfo.
128
+ * Return an API Endpoint from a username and token and a ServiceInfo.
128
129
  * This is method is rarely used. See **apiEndpointFor** as an alternative.
129
- * @param {PryvServiceInfo} serviceInfo
130
+ * @param {ServiceInfo} serviceInfo
130
131
  * @param {string} username
131
132
  * @param {string} [token]
132
- * @return {PryvApiEndpoint}
133
+ * @return {APIEndpoint}
133
134
  */
134
135
  static buildAPIEndpoint (serviceInfo, username, token) {
135
136
  const endpoint = serviceInfo.api.replace('{username}', username);
136
- return utils.buildPryvApiEndpoint({ endpoint: endpoint, token: token });
137
+ return utils.buildAPIEndpoint({ endpoint: endpoint, token: token });
137
138
  }
138
139
 
139
140
  /**
140
- * Issue a "login call on the Service" return a Connection on success
141
+ * Issue a "login call on the Service" return a Connection on success
141
142
  * **! Warning**: the token of the connection will be a "Personal" token that expires
142
143
  * @see https://api.pryv.com/reference-full/#login-user
143
- * @param {string} username
144
- * @param {string} password
145
- * @param {string} appId
144
+ * @param {string} username
145
+ * @param {string} password
146
+ * @param {string} appId
146
147
  * @param {string} [originHeader=service-info.register] Only for Node.js. If not set will use the register value of service info. In browsers this will overridden by current page location.
147
148
  * @throws {Error} on invalid login
148
149
  */
@@ -167,10 +168,11 @@ class Service {
167
168
  this // Pre load Connection with service
168
169
  );
169
170
  } catch (e) {
170
- if (e.response && e.response.body
171
- && e.response.body.error
172
- && e.response.body.error.message) {
173
- throw new Error(e.response.body.error.message)
171
+ if (e.response &&
172
+ e.response.body &&
173
+ e.response.body.error &&
174
+ e.response.body.error.message) {
175
+ throw new Error(e.response.body.error.message);
174
176
  }
175
177
  }
176
178
  }
@@ -183,7 +185,7 @@ const Connection = require('./Connection');
183
185
 
184
186
  /**
185
187
  * Object to handle Pryv Service Informations https://api.pryv.com/reference/#service-info
186
- * @typedef {Object} PryvServiceInfo
188
+ * @typedef {Object} ServiceInfo
187
189
  * @property {string} register The URL of the register service.
188
190
  * @property {string} access The URL of the access page.
189
191
  * @property {string} api The API endpoint format.
@@ -191,7 +193,7 @@ const Connection = require('./Connection');
191
193
  * @property {string} home The URL of the platform's home page.
192
194
  * @property {string} support The email or URL of the support page.
193
195
  * @property {string} terms The terms and conditions, in plain text or the URL displaying them.
194
- * @property {string} eventTypes The URL of the list of validated event types.
196
+ * @property {string} eventTypes The URL of the list of validated event types.
195
197
  * @property {Object} [assets] Holder for service specific Assets (icons, css, ...)
196
198
  * @property {String} [assets.definitions] URL to json object with assets definitions
197
199
  */
@@ -1,10 +1,17 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
1
5
  const utils = require('./utils.js');
6
+
7
+ /* global location */
8
+
2
9
  /**
3
10
  * Holds Pryv Service informations.
4
- *
11
+ *
5
12
  * It's returned by `service.assets()`
6
13
  *
7
- * @memberof Pryv
14
+ * @memberof pryv
8
15
  **/
9
16
  class ServiceAssets {
10
17
  /**
@@ -12,7 +19,7 @@ class ServiceAssets {
12
19
  * @param { object} assets The content of service/info.assets properties.
13
20
  * @param { string } pryvServiceAssetsSourceUrl Url point to assets of the service of a Pryv platform: https://api.pryv.com/reference/#service-info property `assets.src`
14
21
  */
15
- constructor(assets, assetsURL) {
22
+ constructor (assets, assetsURL) {
16
23
  this._assets = assets;
17
24
  this._assetsURL = assetsURL;
18
25
  }
@@ -22,7 +29,7 @@ class ServiceAssets {
22
29
  * @param {string} pryvServiceAssetsSourceUrl
23
30
  * @returns {ServiceAssets}
24
31
  */
25
- static async setup(pryvServiceAssetsSourceUrl) {
32
+ static async setup (pryvServiceAssetsSourceUrl) {
26
33
  const res = await utils.superagent.get(pryvServiceAssetsSourceUrl).set('accept', 'json');
27
34
  return new ServiceAssets(res.body, pryvServiceAssetsSourceUrl);
28
35
  }
@@ -30,9 +37,9 @@ class ServiceAssets {
30
37
  /**
31
38
  * get a value from path separated by `:`
32
39
  * exemple of key `lib-js:buttonSignIn`
33
- * @param {string} [keyPath] if null, will return the all assets
40
+ * @param {string} [keyPath] if null, will return the all assets
34
41
  */
35
- get(keyPath) {
42
+ get (keyPath) {
36
43
  let result = Object.assign({}, this._assets);
37
44
  if (keyPath) {
38
45
  keyPath.split(':').forEach((key) => {
@@ -47,12 +54,12 @@ class ServiceAssets {
47
54
  * get an Url from path separated by `:`
48
55
  * identical to doing assets.relativeURL(assets.get(keyPath))
49
56
  * exemple of key `lib-js:buttonSignIn`
50
- * @param {string} [keyPath] if null, will return the all assets
57
+ * @param {string} [keyPath] if null, will return the all assets
51
58
  */
52
- getUrl(keyPath) {
59
+ getUrl (keyPath) {
53
60
  const url = this.get(keyPath);
54
61
  if (typeof url !== 'string') {
55
- throw new Error(url + ' returned ' + value);
62
+ throw new Error(`Unexpected value for ${keyPath}: ${url}`);
56
63
  }
57
64
  return this.relativeURL(url);
58
65
  }
@@ -60,16 +67,16 @@ class ServiceAssets {
60
67
  /**
61
68
  * get relativeUrl
62
69
  */
63
- relativeURL(url) {
70
+ relativeURL (url) {
64
71
  return relPathToAbs(this._assets.baseUrl || this._assetsURL, url);
65
72
  }
66
73
 
67
- //---------------- Default service ressources
68
-
74
+ // ---------------- Default service resources
75
+
69
76
  /**
70
77
  * Set all defaults Favicon, CSS
71
78
  */
72
- async setAllDefaults() {
79
+ async setAllDefaults () {
73
80
  this.setFavicon();
74
81
  await this.loadCSS();
75
82
  }
@@ -77,8 +84,8 @@ class ServiceAssets {
77
84
  /**
78
85
  * Set service Favicon to Web Page
79
86
  */
80
- setFavicon() {
81
- var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
87
+ setFavicon () {
88
+ const link = document.querySelector("link[rel*='icon']") || document.createElement('link');
82
89
  link.type = 'image/x-icon';
83
90
  link.rel = 'shortcut icon';
84
91
  link.href = this.relativeURL(this._assets.favicon.default.url);
@@ -88,40 +95,41 @@ class ServiceAssets {
88
95
  /**
89
96
  * Set default service CSS
90
97
  */
91
- async loadCSS() {
98
+ async loadCSS () {
92
99
  loadCSS(this.relativeURL(this._assets.css.default.url));
93
100
  }
94
101
 
95
102
  // ---- Login
96
103
 
97
104
  /**
98
- * Load CSS for Login button
99
- */
105
+ * Load CSS for Login button
106
+ */
100
107
  async loginButtonLoadCSS () {
101
108
  loadCSS(this.relativeURL(this._assets['lib-js'].buttonSignIn.css));
102
109
  }
103
110
 
104
111
  /**
105
- * Get HTML for Login Button
106
- */
107
- async loginButtonGetHTML() {
112
+ * Get HTML for Login Button
113
+ */
114
+ async loginButtonGetHTML () {
108
115
  const res = await utils.superagent.get(this.relativeURL(this._assets['lib-js'].buttonSignIn.html)).set('accept', 'html');
109
116
  return res.text;
110
117
  }
111
118
 
112
- /**
113
- * Get Messages strings for Login Button
114
- */
115
- async loginButtonGetMessages() {
119
+ /**
120
+ * Get Messages strings for Login Button
121
+ */
122
+ async loginButtonGetMessages () {
116
123
  const res = await utils.superagent.get(this.relativeURL(this._assets['lib-js'].buttonSignIn.messages)).set('accept', 'json');
117
124
  return res.body;
118
125
  }
119
-
120
126
  }
121
127
 
122
- function loadCSS(url) {
123
- var head = document.getElementsByTagName('head')[0];
124
- var link = document.createElement('link');
128
+ module.exports = ServiceAssets;
129
+
130
+ function loadCSS (url) {
131
+ const head = document.getElementsByTagName('head')[0];
132
+ const link = document.createElement('link');
125
133
  link.id = url;
126
134
  link.rel = 'stylesheet';
127
135
  link.type = 'text/css';
@@ -130,8 +138,11 @@ function loadCSS(url) {
130
138
  head.appendChild(link);
131
139
  }
132
140
 
141
+ /* HACK: disabling linting until code is cleaned up */
142
+ /* eslint-disable */
143
+
133
144
  /*\
134
- |*| Modified version of
145
+ |*| Modified version of
135
146
  |*| :: translate relative paths to absolute paths ::
136
147
  |*|
137
148
  |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
@@ -154,9 +165,6 @@ function relPathToAbs (baseUrlString, sRelPath) {
154
165
  sDir = (sDir + sPath.substring(nStart, nEnd)).replace(new RegExp("(?:\\\/+[^\\\/]*){0," + ((nUpLn - 1) / 3) + "}$"),
155
166
  "/");
156
167
  }
157
- let portStr = baseLocation.port ? ':' + baseLocation.port : '';
168
+ const portStr = baseLocation.port ? ':' + baseLocation.port : '';
158
169
  return baseLocation.protocol + '//' + baseLocation.hostname + portStr + sDir + sPath.substr(nStart);
159
170
  }
160
-
161
-
162
- module.exports = ServiceAssets;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ const pryv = require('./browser-index');
6
+ require('@pryv/socket.io')(pryv);
7
+ require('@pryv/monitor')(pryv);
8
+ module.exports = pryv;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ // make lib available as `Pryv` (with capital P, deprecated) for backward-compatibility with apps importing via <script>
6
+ // TODO: remove deprecated alias with next major version
7
+ module.exports = global.Pryv = require('./index');