Skip to content

Commit 8273b92

Browse files
committed
Added code documentation
1 parent 495baab commit 8273b92

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

src/solid.ts

+62-25
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ function unHookGetItemURL (rs): void {
6969
* @param {XMLHttpRequestBodyInit} body - Request body
7070
* @returns {Blob} Blob equivalent of the body
7171
*
72-
*
7372
* @private
7473
*/
7574
function requestBodyToBlob(body: XMLHttpRequestBodyInit): Blob {
@@ -104,29 +103,23 @@ function requestBodyToBlob(body: XMLHttpRequestBodyInit): Blob {
104103
/**
105104
* @class Solid
106105
*
107-
* To use this backend, you need to specify the authURL like so:
106+
* To use this backend, you need to specify the authURL before calling connect like so:
108107
*
109108
* @example
110-
* remoteStorage.setAuthURL('https://login.example.com');
109+
* solid.setAuthURL('https://login.example.com');
110+
* solid.connect();
111111
*
112-
* In order to set the Solid options for the widget you have to specify the valid options like so:
113-
*
114-
* @example
115-
* remoteStorage.setApiKeys({
116-
* solid: {
117-
* providers: [
118-
* {
119-
* name: "provider name",
120-
* authURL: "auth URL"
121-
* }
122-
* ],
123-
* allowAnyProvider: true|false
124-
* }
125-
* });
112+
* If connect is successful a list of available pods for the Solid account is retrieved and
113+
* a `pod-not-selected` event is fired. After receiving this event you have to call getPodURLs
114+
* to get the list of available pods and set one of them to be used by calling setPodURL. After
115+
* setting the pod URL the `connected` event is fired.
116+
*
117+
* You can find a list of running solid servers on the solid project website here:
118+
* https://solidproject.org/for-developers#hosted-pod-services
126119
**/
127120
class Solid extends RemoteBase implements Remote, ConfigObserver {
128121
authURL: string;
129-
podURLs: string[] = [];
122+
podURLs: string[] = null;
130123
selectedPodURL: string;
131124
sessionProperties: object;
132125
configStorage: ConfigStorage;
@@ -135,12 +128,13 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
135128
constructor(remoteStorage) {
136129
super(remoteStorage);
137130
this.online = true;
138-
this.storageApi = 'draft-dejong-remotestorage-19';
139131
this.addEvents(['connected', 'not-connected', 'pod-not-selected']);
140132

133+
// We use a custom ConfigStore to store the solid session in a rs friendly manner to
134+
// make configuration and disconnect work.
141135
this.configStorage = new ConfigStorage(this);
142136
this.session = new Session({
143-
secureStorage: new InMemoryStorage(),
137+
secureStorage: new InMemoryStorage(), // Inrupt prefers InMemoryStorage for tokens. We respect that.
144138
insecureStorage: this.configStorage
145139
}, 'any');
146140

@@ -154,6 +148,15 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
154148
}
155149
}
156150

151+
/**
152+
* Solid Session storage state changed.
153+
*
154+
* This function is called by the ConfigStore that we provided to Session as an insecure storage.
155+
*
156+
* @param {string} config - The entire Session configuration object serialized into a string
157+
*
158+
* @private
159+
*/
157160
onConfigChanged(config: string): void {
158161
if (config) {
159162
const sessionConfig = JSON.parse(config);
@@ -178,6 +181,7 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
178181
}
179182
}
180183

184+
this.podURLs = null;
181185
localStorage.removeItem(SETTINGS_KEY);
182186
}
183187

@@ -236,6 +240,7 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
236240
const handleError = function() {
237241
this.connected = false;
238242
this.sessionProperties = null;
243+
this.podURLs = null;
239244
if (hasLocalStorage) {
240245
localStorage.removeItem(SETTINGS_KEY);
241246
}
@@ -252,20 +257,42 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
252257

253258
/**
254259
* Set the auth URL
260+
*
255261
* @param {string} authURL - Auth URL
262+
*
263+
* @public
256264
*/
257265
setAuthURL(authURL: string): void {
258266
this.authURL = authURL;
259267
}
260268

261269
/**
270+
* Get a list of pod URLs for this Solid account.
271+
*
272+
* If the Solid Session is not connected, this function returns null.
262273
*
263274
* @returns Get the list of pod URLs
275+
*
276+
* @public
264277
*/
265278
getPodURLs(): string[] {
266279
return this.podURLs;
267280
}
268281

282+
/**
283+
* Set the pod URL to use as the storage.
284+
*
285+
* Pod URL must be one of the URLs provided by the getPodURLs function. This function does
286+
* not validate this constraint.
287+
*
288+
* If the Solid Session is connected and the pod URL is updated to be null, a
289+
* `pod-not-selected` event will be fired. If Session is connected and the pod URL is set,
290+
* a `connected` event will be fired.
291+
*
292+
* @param {string} podURL - URL of the pod to be used as storage
293+
*
294+
* @public
295+
*/
269296
setPodURL(podURL: string): void {
270297
if (this.selectedPodURL === podURL) {
271298
return;
@@ -299,12 +326,21 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
299326
}
300327
}
301328

302-
getPodURL(): string|null {
329+
/**
330+
* Get the pod URL that is being used as the storage.
331+
*
332+
* @returns {string} The in-use pod URL or null
333+
*
334+
* @public
335+
*/
336+
getPodURL(): string {
303337
return this.selectedPodURL;
304338
}
305339

306340
/**
307341
* Initiate the authorization flow's OAuth dance.
342+
*
343+
* @public
308344
*/
309345
connect (): void {
310346
this.rs.setBackend('solid');
@@ -324,10 +360,12 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
324360
/**
325361
* Get the connected Solid session
326362
*
327-
* @returns {Session} that is being used by this instance
363+
* @returns {Session} that is being used by this instance or null if Session is not connected
364+
*
365+
* @public
328366
*/
329367
getSession(): Session {
330-
return (this.session.info && this.session.info.isLoggedIn)?this.session:undefined;
368+
return (this.session.info && this.session.info.isLoggedIn)?this.session:null;
331369
}
332370

333371
/**
@@ -336,7 +374,6 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
336374
* @param {string} path - Path of the resource
337375
* @returns {string} Full URL of the resource on the pod
338376
*
339-
*
340377
* @private
341378
*/
342379
getFileURL(path: string): string {
@@ -388,7 +425,7 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
388425
statusCode: 200,
389426
body: listing,
390427
contentType: 'application/json; charset=UTF-8',
391-
// revision: ?
428+
// revision: ? Skipping ETag
392429
} as RemoteResponse);
393430
}).catch(error => {
394431
if (error instanceof FetchError) {

0 commit comments

Comments
 (0)