@noeldemartin/solid-utils 0.4.0-next.a79c7155bfd5797638ebe4fcd41739c2831c89fc → 0.4.0-next.bf5431cf569638e6dbc4cfd22c73be975e28f0a8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noeldemartin/solid-utils",
3
- "version": "0.4.0-next.a79c7155bfd5797638ebe4fcd41739c2831c89fc",
3
+ "version": "0.4.0-next.bf5431cf569638e6dbc4cfd22c73be975e28f0a8",
4
4
  "description": "My JavaScript utilities for Solid",
5
5
  "main": "dist/noeldemartin-solid-utils.cjs.js",
6
6
  "module": "dist/noeldemartin-solid-utils.esm.js",
@@ -5,7 +5,7 @@ import UnauthorizedError from '../errors/UnauthorizedError';
5
5
  import type SolidDocument from '../models/SolidDocument';
6
6
 
7
7
  import { fetchSolidDocument } from './io';
8
- import type { Fetch } from './io';
8
+ import type { Fetch, FetchSolidDocumentOptions } from './io';
9
9
 
10
10
  export interface SolidUserProfile {
11
11
  webId: string;
@@ -19,7 +19,7 @@ export interface SolidUserProfile {
19
19
  privateTypeIndexUrl?: string;
20
20
  }
21
21
 
22
- async function fetchExtendedUserProfile(webIdDocument: SolidDocument, fetch?: Fetch): Promise<{
22
+ async function fetchExtendedUserProfile(webIdDocument: SolidDocument, options?: FetchSolidDocumentOptions): Promise<{
23
23
  store: SolidStore;
24
24
  cloaked: boolean;
25
25
  writableProfileUrl: string | null;
@@ -43,7 +43,7 @@ async function fetchExtendedUserProfile(webIdDocument: SolidDocument, fetch?: Fe
43
43
  }
44
44
 
45
45
  try {
46
- const document = await fetchSolidDocument(url, fetch);
46
+ const document = await fetchSolidDocument(url, options);
47
47
 
48
48
  documents[url] = document;
49
49
  store.addQuads(document.getQuads());
@@ -80,22 +80,30 @@ async function fetchExtendedUserProfile(webIdDocument: SolidDocument, fetch?: Fe
80
80
  };
81
81
  }
82
82
 
83
- async function fetchUserProfile(webId: string, fetch?: Fetch): Promise<SolidUserProfile> {
83
+ async function fetchUserProfile(webId: string, options: FetchUserProfileOptions = {}): Promise<SolidUserProfile> {
84
+ const requestOptions: FetchSolidDocumentOptions = {
85
+ fetch: options.fetch,
86
+
87
+ // Needed for CSS v7.1.3.
88
+ // See https://github.com/CommunitySolidServer/CommunitySolidServer/issues/1972
89
+ cache: 'no-store',
90
+ };
91
+
84
92
  const documentUrl = urlRoute(webId);
85
- const document = await fetchSolidDocument(documentUrl, fetch);
93
+ const document = await fetchSolidDocument(documentUrl, requestOptions);
86
94
 
87
95
  if (!document.isPersonalProfile() && !document.contains(webId, 'solid:oidcIssuer')) {
88
96
  throw new Error(`${webId} is not a valid webId.`);
89
97
  }
90
98
 
91
- const { store, writableProfileUrl, cloaked } = await fetchExtendedUserProfile(document, fetch);
99
+ const { store, writableProfileUrl, cloaked } = await fetchExtendedUserProfile(document, options);
92
100
  const storageUrls = store.statements(webId, 'pim:storage').map(storage => storage.object.value);
93
101
  const publicTypeIndex = store.statement(webId, 'solid:publicTypeIndex');
94
102
  const privateTypeIndex = store.statement(webId, 'solid:privateTypeIndex');
95
103
 
96
104
  let parentUrl = urlParentDirectory(documentUrl);
97
105
  while (parentUrl && storageUrls.length === 0) {
98
- const parentDocument = await silenced(fetchSolidDocument(parentUrl, fetch));
106
+ const parentDocument = await silenced(fetchSolidDocument(parentUrl, requestOptions));
99
107
 
100
108
  if (parentDocument?.isStorage()) {
101
109
  storageUrls.push(parentUrl);
@@ -110,6 +118,8 @@ async function fetchUserProfile(webId: string, fetch?: Fetch): Promise<SolidUser
110
118
  throw new Error(`Could not find any storage for ${webId}.`);
111
119
  }
112
120
 
121
+ await options.onLoaded?.(new SolidStore(store.statements(webId)));
122
+
113
123
  return {
114
124
  webId,
115
125
  cloaked,
@@ -129,9 +139,13 @@ async function fetchUserProfile(webId: string, fetch?: Fetch): Promise<SolidUser
129
139
  };
130
140
  }
131
141
 
132
- export interface FetchLoginUserProfileOptions {
133
- required?: boolean;
142
+ export interface FetchUserProfileOptions {
134
143
  fetch?: Fetch;
144
+ onLoaded?(store: SolidStore): Promise<unknown> | unknown;
145
+ }
146
+
147
+ export interface FetchLoginUserProfileOptions extends FetchUserProfileOptions {
148
+ required?: boolean;
135
149
  }
136
150
 
137
151
  export async function fetchLoginUserProfile(
@@ -139,10 +153,10 @@ export async function fetchLoginUserProfile(
139
153
  options: FetchLoginUserProfileOptions = {},
140
154
  ): Promise<SolidUserProfile | null> {
141
155
  if (options.required) {
142
- return fetchUserProfile(loginUrl, options.fetch);
156
+ return fetchUserProfile(loginUrl, options);
143
157
  }
144
158
 
145
- const fetchProfile = silenced(url => fetchUserProfile(url, options.fetch));
159
+ const fetchProfile = silenced(url => fetchUserProfile(url, options));
146
160
 
147
161
  return await fetchProfile(loginUrl)
148
162
  ?? await fetchProfile(loginUrl.replace(/\/$/, '').concat('/profile/card#me'))
@@ -12,7 +12,7 @@ async function mintTypeIndexUrl(user: SolidUserProfile, type: TypeIndexType, fet
12
12
  const storageUrl = user.storageUrls[0];
13
13
  const typeIndexUrl = `${storageUrl}settings/${type}TypeIndex`;
14
14
 
15
- return await solidDocumentExists(typeIndexUrl, fetch)
15
+ return await solidDocumentExists(typeIndexUrl, { fetch })
16
16
  ? `${storageUrl}settings/${type}TypeIndex-${uuid()}`
17
17
  : typeIndexUrl;
18
18
  }
@@ -57,7 +57,7 @@ async function findRegistrations(
57
57
  predicate: string,
58
58
  fetch?: Fetch,
59
59
  ): Promise<string[]> {
60
- const typeIndex = await fetchSolidDocument(typeIndexUrl, fetch);
60
+ const typeIndex = await fetchSolidDocument(typeIndexUrl, { fetch });
61
61
  const types = Array.isArray(type) ? type : [type];
62
62
 
63
63
  return types.map(
package/src/helpers/io.ts CHANGED
@@ -28,13 +28,21 @@ export declare type Fetch = TypedFetch | AnyFetch;
28
28
  const ANONYMOUS_PREFIX = 'anonymous://';
29
29
  const ANONYMOUS_PREFIX_LENGTH = ANONYMOUS_PREFIX.length;
30
30
 
31
- async function fetchRawSolidDocument(url: string, fetch: Fetch): Promise<{ body: string; headers: Headers }> {
32
- const options = {
31
+ async function fetchRawSolidDocument(
32
+ url: string,
33
+ options?: FetchSolidDocumentOptions,
34
+ ): Promise<{ body: string; headers: Headers }> {
35
+ const requestOptions: RequestInit = {
33
36
  headers: { Accept: 'text/turtle' },
34
37
  };
35
38
 
39
+ if (options?.cache) {
40
+ requestOptions.cache = options.cache;
41
+ }
42
+
36
43
  try {
37
- const response = await fetch(url, options);
44
+ const fetch = options?.fetch ?? window.fetch;
45
+ const response = await fetch(url, requestOptions);
38
46
 
39
47
  if (response.status === 404)
40
48
  throw new NotFoundError(url);
@@ -141,6 +149,11 @@ function postprocessSubjects(quads: Quad[]): void {
141
149
  }
142
150
  }
143
151
 
152
+ export interface FetchSolidDocumentOptions {
153
+ fetch?: Fetch;
154
+ cache?: RequestCache;
155
+ }
156
+
144
157
  export interface ParsingOptions {
145
158
  baseIRI: string;
146
159
  normalizeBlankNodes: boolean;
@@ -151,7 +164,6 @@ export interface RDFGraphData {
151
164
  containsRelativeIRIs: boolean;
152
165
  }
153
166
 
154
-
155
167
  export async function createSolidDocument(url: string, body: string, fetch?: Fetch): Promise<SolidDocument> {
156
168
  fetch = fetch ?? window.fetch.bind(window);
157
169
 
@@ -166,16 +178,19 @@ export async function createSolidDocument(url: string, body: string, fetch?: Fet
166
178
  return new SolidDocument(url, statements, new Headers({}));
167
179
  }
168
180
 
169
- export async function fetchSolidDocument(url: string, fetch?: Fetch): Promise<SolidDocument> {
170
- const { body: data, headers } = await fetchRawSolidDocument(url, fetch ?? window.fetch);
181
+ export async function fetchSolidDocument(url: string, options?: FetchSolidDocumentOptions): Promise<SolidDocument> {
182
+ const { body: data, headers } = await fetchRawSolidDocument(url, options);
171
183
  const statements = await turtleToQuads(data, { baseIRI: url });
172
184
 
173
185
  return new SolidDocument(url, statements, headers);
174
186
  }
175
187
 
176
- export async function fetchSolidDocumentIfFound(url: string, fetch?: Fetch): Promise<SolidDocument | null> {
188
+ export async function fetchSolidDocumentIfFound(
189
+ url: string,
190
+ options?: FetchSolidDocumentOptions,
191
+ ): Promise<SolidDocument | null> {
177
192
  try {
178
- const document = await fetchSolidDocument(url, fetch);
193
+ const document = await fetchSolidDocument(url, options);
179
194
 
180
195
  return document;
181
196
  } catch (error) {
@@ -287,9 +302,9 @@ export function quadToTurtle(quad: Quad): string {
287
302
  return writer.quadsToString([quad]).slice(0, -1);
288
303
  }
289
304
 
290
- export async function solidDocumentExists(url: string, fetch?: Fetch): Promise<boolean> {
305
+ export async function solidDocumentExists(url: string, options?: FetchSolidDocumentOptions): Promise<boolean> {
291
306
  try {
292
- const document = await fetchSolidDocument(url, fetch);
307
+ const document = await fetchSolidDocument(url, options);
293
308
 
294
309
  return !document.isEmpty();
295
310
  } catch (error) {
@@ -26,7 +26,7 @@ async function fetchEffectiveACL(
26
26
  ): Promise<SolidDocument> {
27
27
  aclResourceUrl = aclResourceUrl ?? await fetchACLResourceUrl(resourceUrl, fetch);
28
28
 
29
- const aclDocument = await fetchSolidDocumentIfFound(aclResourceUrl ?? '', fetch);
29
+ const aclDocument = await fetchSolidDocumentIfFound(aclResourceUrl ?? '', { fetch });
30
30
 
31
31
  if (!aclDocument) {
32
32
  return fetchEffectiveACL(requireUrlParentDirectory(resourceUrl), fetch);