Skip to content

Commit 33ae2e7

Browse files
committed
Fixing types and jsdoc
1 parent 78b6ac9 commit 33ae2e7

7 files changed

Lines changed: 188 additions & 94 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<!-- Format based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) -->
44

5-
6-
## [Unreleased](https://github.com/pryv/lib-js/compare/2.4.5...HEAD)
5+
## [3.0.0](https://github.com/pryv/lib-js/compare/2.4.5...3.0.0)
6+
- Removing superagent dependency (breaking change)
7+
- Enhancing test suite
78

89
## [2.4.5](https://github.com/pryv/lib-js/compare/2.3.1...2.4.5)
910
- Enhanced typescripts definition

components/pryv/src/Auth/AuthController.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ const AuthStates = require('./AuthStates');
77
const Messages = require('./LoginMessages');
88

99
/**
10-
* @private
10+
* Controller for authentication flow
11+
* @memberof pryv.Auth
1112
*/
1213
class AuthController {
1314
/**
14-
*
15-
* @param {*} settings
16-
* @param {*} service
15+
* Create an AuthController
16+
* @param {AuthSettings} settings - Authentication settings
17+
* @param {Service} service - Pryv service instance
18+
* @param {CustomLoginButton} loginButton - Login button implementation
1719
*/
1820
constructor (settings, service, loginButton) {
1921
this.settings = settings;
@@ -50,9 +52,8 @@ class AuthController {
5052
}
5153

5254
/**
53-
* async function to call right after instanciating object
54-
*
55-
* @returns {Service}
55+
* Initialize the auth controller. Call this right after instantiation.
56+
* @returns {Promise<Service>} Promise resolving to the Service instance
5657
*/
5758
async init () {
5859
this.serviceInfo = this.service.infoSync();
@@ -87,7 +88,8 @@ class AuthController {
8788
}
8889

8990
/**
90-
* Triggered when button is pressed
91+
* Handle button click - triggers appropriate action based on current state
92+
* @returns {Promise<void>}
9193
*/
9294
async handleClick () {
9395
if (isAuthorized.call(this)) {
@@ -113,11 +115,12 @@ class AuthController {
113115
}
114116

115117
/**
116-
* Used only to retrieve returnUrl in browser environments
117-
*
118-
* @param {*} returnURL
119-
* @param {*} windowLocationForTest
120-
* @param {*} navigatorForTests
118+
* Compute the return URL for authentication redirect.
119+
* Used only in browser environments.
120+
* @param {string} [returnURL] - The return URL setting ('auto#', 'self#', or custom URL)
121+
* @param {string} [windowLocationForTest] - Mock window.location.href for testing
122+
* @param {string|Navigator} [navigatorForTests] - Mock navigator for testing
123+
* @returns {string|boolean} The computed return URL, or false if using popup mode
121124
*/
122125
getReturnURL (
123126
returnURL,
@@ -154,6 +157,10 @@ class AuthController {
154157
}
155158
}
156159

160+
/**
161+
* Start the authentication request and polling process
162+
* @returns {Promise<void>}
163+
*/
157164
async startAuthRequest () {
158165
this.state = await postAccess.call(this);
159166

components/pryv/src/Connection.js

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ class Connection {
5151
}
5252

5353
/**
54-
* get username.
55-
* It's async as in it constructed from access info
56-
* @param {*} arrayOfAPICalls
57-
* @param {*} progress
54+
* Get username for this connection.
55+
* It's async as it's constructed from access info.
56+
* @returns {Promise<string>} Promise resolving to the username
5857
*/
5958
async username () {
6059
const accessInfo = await this.accessInfo();
@@ -67,8 +66,9 @@ class Connection {
6766
}
6867

6968
/**
70-
* get access info
71-
* It's async as it is constructed with get function.
69+
* Get access info for this connection.
70+
* It's async as it is fetched from the API.
71+
* @returns {Promise<AccessInfo>} Promise resolving to the access info
7272
*/
7373
async accessInfo () {
7474
return this.get('access-info', null);
@@ -94,11 +94,12 @@ class Connection {
9494
}
9595

9696
/**
97-
* Make one api Api call
98-
* @param {string} method - methodId
99-
* @param {Object|Array} [params] - the params associated with this methodId
100-
* @param {string} [resultKey] - if given, returns the value or throws an error if not present
101-
* @throws {Error} if .error is present the response
97+
* Make one API call
98+
* @param {string} method - Method ID (e.g., 'events.get', 'streams.create')
99+
* @param {Object|Array} [params={}] - The params associated with this method
100+
* @param {string} [expectedKey] - If given, returns the value of this key or throws an error if not present
101+
* @returns {Promise<Object>} Promise resolving to the API result or the value of expectedKey
102+
* @throws {Error} If .error is present in the response or expectedKey is missing
102103
*/
103104
async apiOne (method, params = {}, expectedKey) {
104105
const result = await this.api([{ method, params }]);
@@ -122,9 +123,10 @@ class Connection {
122123

123124
/**
124125
* Revoke : Delete the accessId
125-
* - Do not thow error if access is already revoked, just return null;
126-
* @param {boolean} [throwOnFail = true] - if set to false do not throw Error on failure
127-
* @param {Connection} [usingConnection] - specify which connection issues the revoke, might be necessary when selfRovke
126+
* - Do not throw error if access is already revoked, just return null;
127+
* @param {boolean} [throwOnFail=true] - if set to false do not throw Error on failure
128+
* @param {Connection} [usingConnection] - specify which connection issues the revoke, might be necessary when selfRevoke
129+
* @returns {Promise<Object|null>} Promise resolving to deletion result or null if already revoked/failed
128130
*/
129131
async revoke (throwOnFail = true, usingConnection) {
130132
usingConnection = usingConnection || this;
@@ -212,33 +214,38 @@ class Connection {
212214
}
213215

214216
/**
215-
* Post to API return results
216-
* @param {(Array | Object)} data
217-
* @param {string} path
218-
* @returns {Promise<Array|Object>} Promise to result.body
217+
* Post to API and return results
218+
* @param {string} path - API path
219+
* @param {(Array | Object)} data - Data to post
220+
* @returns {Promise<Object|Object[]>} Promise to result.body
219221
*/
220222
async post (path, data) {
221223
const now = getTimestamp();
222-
const res = await this.postFetch(path, data);
224+
const res = await this._postFetch(path, data);
223225
this._handleMeta(res.body, now);
224226
return res.body;
225227
}
226228

227229
/**
230+
* @private
228231
* Post object as JSON to API
229-
* @param {Array | Object} data
230-
* @param {string} path
232+
* @param {string} path - API path
233+
* @param {Array | Object} data - Data to post as JSON
234+
* @returns {Promise<{response: Response, body: Object|Object[]}>} Promise to response and body
231235
*/
232-
async postFetch (path, data) {
233-
return this.postFetchRaw(path, JSON.stringify(data), 'application/json');
236+
async _postFetch (path, data) {
237+
return this._postFetchRaw(path, JSON.stringify(data), 'application/json');
234238
}
235239

236240
/**
241+
* @private
237242
* Raw Post to API
238-
* @param {Array | Object} data
239-
* @param {string} path
243+
* @param {string} path - API path
244+
* @param {any} data - Raw data to post
245+
* @param {string} [contentType] - Content-Type header (optional, allows fetch to set it for FormData)
246+
* @returns {Promise<{response: Response, body: Object|Object[]}>} Promise to response and body
240247
*/
241-
async postFetchRaw (path, data, contentType) {
248+
async _postFetchRaw (path, data, contentType) {
242249
const headers = {
243250
Authorization: this.token,
244251
Accept: 'application/json'
@@ -258,23 +265,26 @@ class Connection {
258265
}
259266

260267
/**
261-
* Post to API return results
262-
* @param {Object} queryParams
263-
* @param {string} path
264-
* @returns {Promise<Array|Object>} Promise to result.body
268+
* GET from API and return results
269+
* @param {string} path - API path
270+
* @param {Object} [queryParams] - Query parameters
271+
* @returns {Promise<Object|Object[]>} Promise to result.body
265272
*/
266273
async get (path, queryParams) {
267274
const now = getTimestamp();
268-
const res = await this.getFetchRaw(path, queryParams);
275+
const res = await this._getFetchRaw(path, queryParams);
269276
this._handleMeta(res.body, now);
270277
return res.body;
271278
}
272279

273280
/**
274-
* @param {Object} queryParams
275-
* @param {string} path
281+
* @private
282+
* Raw GET from API
283+
* @param {string} path - API path
284+
* @param {Object} [queryParams={}] - Query parameters
285+
* @returns {Promise<{response: Response, body: Object|Object[]}>} Promise to response and body
276286
*/
277-
async getFetchRaw (path, queryParams = {}) {
287+
async _getFetchRaw (path, queryParams = {}) {
278288
path = path || '';
279289
let queryStr = '';
280290
if (queryParams && Object.keys(queryParams).length > 0) {
@@ -344,7 +354,7 @@ class Connection {
344354
formData.append('file', fileBlob, fileName);
345355

346356
const now = getTimestamp();
347-
const { body } = await this.postFetchRaw('events', formData);
357+
const { body } = await this._postFetchRaw('events', formData);
348358
this._handleMeta(body, now);
349359
return body;
350360
}
@@ -375,7 +385,7 @@ class Connection {
375385
*/
376386
async createEventWithFormData (event, formData) {
377387
formData.append('event', JSON.stringify(event));
378-
const { body } = await this.postFetchRaw('events', formData);
388+
const { body } = await this._postFetchRaw('events', formData);
379389
return body;
380390
}
381391

@@ -390,9 +400,9 @@ class Connection {
390400
}
391401

392402
/**
393-
* API endpoint of this connection
403+
* API endpoint of this connection (includes token if present)
394404
* @readonly
395-
* @property {APIEndpoint} deltaTime
405+
* @property {APIEndpoint} apiEndpoint
396406
*/
397407
get apiEndpoint () {
398408
return utils.buildAPIEndpoint(this);

components/pryv/src/Service.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ class Service {
7070

7171
/**
7272
* Check if a service supports High Frequency Data Sets
73-
* @return true if yes
73+
* @returns {Promise<boolean>} Promise resolving to true if HF is supported
7474
*/
7575
async supportsHF () {
7676
const infos = await this.info();
7777
return (infos.features == null || infos.features.noHF !== true);
7878
}
7979

8080
/**
81-
* Check if a service has username in the hostname or in the path of the api
82-
* @return true if the service does not rely on DNS to find a host related to a username
81+
* Check if a service has username in the hostname or in the path of the API.
82+
* @returns {Promise<boolean>} Promise resolving to true if the service does not rely on DNS to find a host related to a username
8383
*/
8484
async isDnsLess () {
8585
const infos = await this.info();
@@ -134,22 +134,22 @@ class Service {
134134

135135
/**
136136
* Return an API endpoint from a username and token
137-
* @param {string} username
138-
* @param {string} [token]
139-
* @return {APIEndpoint}
137+
* @param {string} username - The username
138+
* @param {string} [token] - Optional authorization token
139+
* @returns {Promise<APIEndpoint>} Promise resolving to the API endpoint URL
140140
*/
141141
async apiEndpointFor (username, token) {
142142
const serviceInfo = await this.info();
143143
return Service.buildAPIEndpoint(serviceInfo, username, token);
144144
}
145145

146146
/**
147-
* Return an API endpoint from a username and token and a ServiceInfo.
148-
* This is method is rarely used. See **apiEndpointFor** as an alternative.
149-
* @param {ServiceInfo} serviceInfo
150-
* @param {string} username
151-
* @param {string} [token]
152-
* @return {APIEndpoint}
147+
* Return an API endpoint from a username, token and ServiceInfo.
148+
* This method is rarely used. See **apiEndpointFor** as an alternative.
149+
* @param {ServiceInfo} serviceInfo - The service info object containing API URL template
150+
* @param {string} username - The username
151+
* @param {string} [token] - Optional authorization token
152+
* @returns {APIEndpoint} The constructed API endpoint URL
153153
*/
154154
static buildAPIEndpoint (serviceInfo, username, token) {
155155
const endpoint = serviceInfo.api.replace('{username}', username);

components/pryv/src/ServiceAssets.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class ServiceAssets {
2525
}
2626

2727
/**
28-
* Load Assets definition
29-
* @param {string} pryvServiceAssetsSourceUrl
30-
* @returns {ServiceAssets}
28+
* Load Assets definition from URL
29+
* @param {string} pryvServiceAssetsSourceUrl - URL to the assets definition JSON
30+
* @returns {Promise<ServiceAssets>} Promise resolving to ServiceAssets instance
3131
*/
3232
static async setup (pryvServiceAssetsSourceUrl) {
3333
const { body } = await utils.fetchGet(pryvServiceAssetsSourceUrl);
@@ -110,6 +110,7 @@ class ServiceAssets {
110110

111111
/**
112112
* Get HTML for Login Button
113+
* @returns {Promise<string>} Promise resolving to HTML string
113114
*/
114115
async loginButtonGetHTML () {
115116
const { text } = await utils.fetchGetText(this.relativeURL(this._assets['lib-js'].buttonSignIn.html));
@@ -118,6 +119,7 @@ class ServiceAssets {
118119

119120
/**
120121
* Get Messages strings for Login Button
122+
* @returns {Promise<Object.<string, string>>} Promise resolving to messages object
121123
*/
122124
async loginButtonGetMessages () {
123125
const { body } = await utils.fetchGet(this.relativeURL(this._assets['lib-js'].buttonSignIn.messages));

0 commit comments

Comments
 (0)