@semapps/semantic-data-provider 1.1.2 → 1.1.4

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/dist/index.cjs.js CHANGED
@@ -131,7 +131,6 @@ var $0edd1f2d07c8231f$export$2e2bcd8739ae039 = $0edd1f2d07c8231f$var$getUploadsC
131
131
 
132
132
 
133
133
  const $6fcb30f76390d142$var$isFile = (o)=>o?.rawFile && o.rawFile instanceof File;
134
- const $6fcb30f76390d142$var$isFileToDelete = (o)=>o?.fileToDelete !== undefined && o?.fileToDelete !== null;
135
134
  const $6fcb30f76390d142$export$a5575dbeeffdad98 = async (rawFile, config, serverKey)=>{
136
135
  const uploadsContainerUri = (0, $0edd1f2d07c8231f$export$2e2bcd8739ae039)(config, serverKey);
137
136
  if (!uploadsContainerUri) throw new Error("You must define an container with binaryResources in one of the server's configuration");
@@ -145,16 +144,10 @@ const $6fcb30f76390d142$export$a5575dbeeffdad98 = async (rawFile, config, server
145
144
  if (response.status === 201) return response.headers.get('Location');
146
145
  return null;
147
146
  };
148
- const $6fcb30f76390d142$var$deleteFiles = async (filesToDelete, config)=>{
149
- return Promise.all(filesToDelete.map((file)=>config.httpClient(file, {
150
- method: 'DELETE'
151
- })));
152
- };
153
147
  /*
154
148
  * Look for raw files in the record data.
155
149
  * If there are any, upload them and replace the file by its URL.
156
150
  */ const $6fcb30f76390d142$var$uploadAllFiles = async (record, config, serverKey)=>{
157
- const filesToDelete = [];
158
151
  const updatedRecord = {
159
152
  ...record
160
153
  };
@@ -162,30 +155,16 @@ const $6fcb30f76390d142$var$deleteFiles = async (filesToDelete, config)=>{
162
155
  const value = record[property];
163
156
  if (Array.isArray(value)) for(let i = 0; i < value.length; i++){
164
157
  const itemValue = value[i];
165
- if ($6fcb30f76390d142$var$isFile(itemValue)) {
166
- if ($6fcb30f76390d142$var$isFileToDelete(itemValue)) filesToDelete.push(itemValue.fileToDelete);
167
- updatedRecord[property][i] = await $6fcb30f76390d142$export$a5575dbeeffdad98(itemValue.rawFile, config, serverKey);
168
- } else if ($6fcb30f76390d142$var$isFileToDelete(itemValue)) {
169
- filesToDelete.push(itemValue.fileToDelete);
170
- updatedRecord[property][i] = null;
171
- }
172
- }
173
- else if ($6fcb30f76390d142$var$isFile(value)) {
174
- if ($6fcb30f76390d142$var$isFileToDelete(value)) filesToDelete.push(value.fileToDelete);
175
- updatedRecord[property] = await $6fcb30f76390d142$export$a5575dbeeffdad98(value.rawFile, config, serverKey);
176
- } else if ($6fcb30f76390d142$var$isFileToDelete(value)) {
177
- filesToDelete.push(value.fileToDelete);
178
- updatedRecord[property] = null;
158
+ if ($6fcb30f76390d142$var$isFile(itemValue)) updatedRecord[property][i] = await $6fcb30f76390d142$export$a5575dbeeffdad98(itemValue.rawFile, config, serverKey);
179
159
  }
160
+ else if ($6fcb30f76390d142$var$isFile(value)) updatedRecord[property] = await $6fcb30f76390d142$export$a5575dbeeffdad98(value.rawFile, config, serverKey);
180
161
  }
181
162
  return {
182
- updatedRecord: updatedRecord,
183
- filesToDelete: filesToDelete
163
+ updatedRecord: updatedRecord
184
164
  };
185
165
  };
186
166
  var $6fcb30f76390d142$export$2e2bcd8739ae039 = {
187
- upload: $6fcb30f76390d142$var$uploadAllFiles,
188
- delete: $6fcb30f76390d142$var$deleteFiles
167
+ upload: $6fcb30f76390d142$var$uploadAllFiles
189
168
  };
190
169
 
191
170
 
@@ -321,13 +300,11 @@ const $907cbc087f6529e2$var$createMethod = (config)=>async (resourceId, params)=
321
300
  var $907cbc087f6529e2$export$2e2bcd8739ae039 = $907cbc087f6529e2$var$createMethod;
322
301
 
323
302
 
324
-
325
303
  const $566b5adde94810fa$var$deleteMethod = (config)=>async (resourceId, params)=>{
326
304
  const { httpClient: httpClient } = config;
327
305
  await httpClient(`${params.id}`, {
328
306
  method: 'DELETE'
329
307
  });
330
- if (params.meta?.filesToDelete) await (0, $6fcb30f76390d142$export$2e2bcd8739ae039).delete(params.meta.filesToDelete, config);
331
308
  return {
332
309
  data: {
333
310
  id: params.id
@@ -382,6 +359,19 @@ const $e6fbab1f303bdb93$var$arrayOf = (value)=>{
382
359
  var $e6fbab1f303bdb93$export$2e2bcd8739ae039 = $e6fbab1f303bdb93$var$arrayOf;
383
360
 
384
361
 
362
+
363
+ // Fetch the selected resources of the provided containers
364
+ // Filter out resources that are provided (can avoid loading a resource twice)
365
+ const $448b8598c73a447a$var$fetchSelectedResources = async (containers, excludedResourcesUris, config)=>{
366
+ let selectedResourcesUris = containers.filter((c)=>c.selectedResources).map((c)=>c.selectedResources).flat();
367
+ // Filter out resources which are already included in the SPARQL query results
368
+ selectedResourcesUris = selectedResourcesUris.filter((uri)=>!excludedResourcesUris.includes(uri));
369
+ const selectedResources = await Promise.all(selectedResourcesUris.map((resourceUri)=>(0, $3db7a4510a668a04$export$2e2bcd8739ae039)(resourceUri, config)));
370
+ return selectedResources;
371
+ };
372
+ var $448b8598c73a447a$export$2e2bcd8739ae039 = $448b8598c73a447a$var$fetchSelectedResources;
373
+
374
+
385
375
  const $8c999cc29c8d6a6c$var$isValidLDPContainer = (container)=>{
386
376
  const resourceType = container.type || container['@type'];
387
377
  return Array.isArray(resourceType) ? resourceType.includes('ldp:Container') : resourceType === 'ldp:Container';
@@ -389,23 +379,24 @@ const $8c999cc29c8d6a6c$var$isValidLDPContainer = (container)=>{
389
379
  const $8c999cc29c8d6a6c$var$isObject = (val)=>{
390
380
  return val != null && typeof val === 'object' && !Array.isArray(val);
391
381
  };
392
- const $8c999cc29c8d6a6c$var$fetchContainers = async (containers, params, { httpClient: httpClient, jsonContext: jsonContext })=>{
393
- const fetchPromises = containers.map((container)=>httpClient(container.uri).then(async ({ json: json })=>{
394
- const jsonResponse = json;
395
- // If container's context is different, compact it to have an uniform result
396
- // TODO deep compare if the context is an object
397
- if (jsonResponse['@context'] !== jsonContext) return (0, ($parcel$interopDefault($bkNnK$jsonld))).compact(jsonResponse, jsonContext);
398
- return jsonResponse;
399
- }).then((json)=>{
400
- if (!$8c999cc29c8d6a6c$var$isValidLDPContainer(json)) throw new Error(`${container.uri} is not a LDP container`);
401
- return (0, $e6fbab1f303bdb93$export$2e2bcd8739ae039)(json['ldp:contains']).map((resource)=>({
402
- '@context': json['@context'],
403
- ...resource
404
- }));
405
- }));
382
+ const $8c999cc29c8d6a6c$var$fetchContainers = async (containers, params, config)=>{
383
+ const { httpClient: httpClient, jsonContext: jsonContext } = config;
406
384
  // Fetch simultaneously all containers
407
- const results = await Promise.all(fetchPromises);
385
+ const results = await Promise.all(containers.filter((c)=>!c.selectedResources).map(async (container)=>{
386
+ let { json: json } = await httpClient(container.uri);
387
+ // If container's context is different, compact it to have an uniform result
388
+ // TODO deep compare if the context is an object
389
+ if (json['@context'] !== jsonContext) json = await (0, ($parcel$interopDefault($bkNnK$jsonld))).compact(json, jsonContext);
390
+ if (!$8c999cc29c8d6a6c$var$isValidLDPContainer(json)) throw new Error(`${container.uri} is not a LDP container`);
391
+ return (0, $e6fbab1f303bdb93$export$2e2bcd8739ae039)(json['ldp:contains']).map((resource)=>({
392
+ '@context': json['@context'],
393
+ ...resource
394
+ }));
395
+ }));
408
396
  let resources = results.flat();
397
+ // Append selected resources (if any)
398
+ const selectedResources = await (0, $448b8598c73a447a$export$2e2bcd8739ae039)(containers, resources.map((r)=>r.id), config);
399
+ resources = resources.concat(selectedResources);
409
400
  resources = resources.map((resource)=>{
410
401
  resource.id = resource.id || resource['@id'];
411
402
  return resource;
@@ -805,6 +796,7 @@ const $33c37185da3771a9$var$buildSparqlQuery = ({ containersUris: containersUris
805
796
  var $33c37185da3771a9$export$2e2bcd8739ae039 = $33c37185da3771a9$var$buildSparqlQuery;
806
797
 
807
798
 
799
+
808
800
  const $1e7a94d745f8597b$var$compare = (a, b)=>{
809
801
  switch(typeof a){
810
802
  case 'string':
@@ -816,16 +808,19 @@ const $1e7a94d745f8597b$var$compare = (a, b)=>{
816
808
  }
817
809
  };
818
810
  const $1e7a94d745f8597b$var$fetchSparqlEndpoints = async (containers, resourceId, params, config)=>{
819
- const { dataServers: dataServers, resources: resources, httpClient: httpClient, jsonContext: jsonContext, ontologies: ontologies } = config;
820
- const dataModel = resources[resourceId];
821
- const serversToQuery = containers.reduce((acc, cur)=>{
811
+ const { dataServers: dataServers, httpClient: httpClient, jsonContext: jsonContext, ontologies: ontologies } = config;
812
+ const dataModel = config.resources[resourceId];
813
+ // Find servers to query with SPARQL
814
+ // (Ignore containers with selected resources, they will be fetched below)
815
+ const serversToQuery = containers.filter((c)=>!c.selectedResources).reduce((acc, cur)=>{
822
816
  if (!acc.includes(cur.server)) acc.push(cur.server);
823
817
  return acc;
824
818
  }, []);
825
- const sparqlQueryPromises = serversToQuery.map((serverKey)=>new Promise((resolve, reject)=>{
819
+ // Run simultaneous SPARQL queries
820
+ const results = await Promise.all(serversToQuery.map((serverKey)=>new Promise((resolve, reject)=>{
826
821
  const blankNodes = params.filter?.blankNodes || dataModel.list?.blankNodes;
827
822
  const sparqlQuery = (0, $33c37185da3771a9$export$2e2bcd8739ae039)({
828
- containersUris: containers.filter((c)=>c.server === serverKey).map((c)=>c.uri),
823
+ containersUris: containers.filter((c)=>c.server === serverKey && !c.selectedResources).map((c)=>c.uri),
829
824
  params: params,
830
825
  dataModel: dataModel,
831
826
  ontologies: ontologies
@@ -865,17 +860,18 @@ const $1e7a94d745f8597b$var$fetchSparqlEndpoints = async (containers, resourceId
865
860
  ...resource
866
861
  })) || []);
867
862
  }).catch((e)=>reject(e));
868
- }));
869
- // Run simultaneous SPARQL queries
870
- let results = await Promise.all(sparqlQueryPromises);
871
- if (results.length === 0) return {
863
+ })));
864
+ // Merge results from all SPARQL servers
865
+ let resources = results.flat();
866
+ // Append selected resources to SPARQL query results
867
+ const selectedResources = await (0, $448b8598c73a447a$export$2e2bcd8739ae039)(containers, resources.map((r)=>r.id), config);
868
+ resources = resources.concat(selectedResources);
869
+ if (resources.length === 0) return {
872
870
  data: [],
873
871
  total: 0
874
872
  };
875
- // Merge all results in one array
876
- results = [].concat(...results);
877
873
  // Add id in addition to @id, as this is what React-Admin expects
878
- let returnData = results.map((item)=>{
874
+ let returnData = resources.map((item)=>{
879
875
  item.id = item.id || item['@id'];
880
876
  return item;
881
877
  });
@@ -1024,7 +1020,7 @@ const $ceaafb56f75454f0$var$updateMethod = (config)=>async (resourceId, params)=
1024
1020
  const { httpClient: httpClient, jsonContext: jsonContext, dataServers: dataServers } = config;
1025
1021
  const serverKey = (0, $59a07b932dae8600$export$2e2bcd8739ae039)(params.id, dataServers);
1026
1022
  // Upload files, if there are any
1027
- const { updatedRecord: updatedRecord, filesToDelete: filesToDelete } = await (0, $6fcb30f76390d142$export$2e2bcd8739ae039).upload(params.data, config, serverKey);
1023
+ const { updatedRecord: updatedRecord } = await (0, $6fcb30f76390d142$export$2e2bcd8739ae039).upload(params.data, config, serverKey);
1028
1024
  params.data = updatedRecord;
1029
1025
  await httpClient(`${params.id}`, {
1030
1026
  method: 'PUT',
@@ -1033,8 +1029,6 @@ const $ceaafb56f75454f0$var$updateMethod = (config)=>async (resourceId, params)=
1033
1029
  ...params.data
1034
1030
  })
1035
1031
  });
1036
- // Delete files only if update is successful
1037
- await (0, $6fcb30f76390d142$export$2e2bcd8739ae039).delete(filesToDelete, config);
1038
1032
  return {
1039
1033
  data: params.data
1040
1034
  };
@@ -1297,7 +1291,7 @@ const $89358cee13a17a31$var$configureUserStorage = ()=>({
1297
1291
  const newConfig = {
1298
1292
  ...config
1299
1293
  };
1300
- newConfig.dataServers.user = {
1294
+ newConfig.dataServers[webId] = {
1301
1295
  pod: true,
1302
1296
  default: true,
1303
1297
  authServer: true,
@@ -1325,6 +1319,7 @@ var $89358cee13a17a31$export$2e2bcd8739ae039 = $89358cee13a17a31$var$configureUs
1325
1319
 
1326
1320
 
1327
1321
 
1322
+
1328
1323
  const $37dc42f6e1c3b4af$var$getContainerFromDataRegistration = async (dataRegistrationUri, config)=>{
1329
1324
  const { json: dataRegistration } = await config.httpClient(dataRegistrationUri, {
1330
1325
  headers: new Headers({
@@ -1358,7 +1353,8 @@ const $37dc42f6e1c3b4af$var$getContainerFromDataRegistration = async (dataRegist
1358
1353
  '@container': '@language'
1359
1354
  }
1360
1355
  });
1361
- const { baseUrl: baseUrl } = config.dataServers.user;
1356
+ const userStorage = (0, $8f44b7c15b8b8e1d$export$2e2bcd8739ae039)('default', config.dataServers);
1357
+ const { baseUrl: baseUrl } = config.dataServers[userStorage];
1362
1358
  const containerPath = dataRegistration.id.replace(baseUrl, '');
1363
1359
  const container = {
1364
1360
  path: containerPath,
@@ -1384,7 +1380,9 @@ var $37dc42f6e1c3b4af$export$2e2bcd8739ae039 = $37dc42f6e1c3b4af$var$getContaine
1384
1380
  * Return a function that look if an app (clientId) is registered with an user (webId)
1385
1381
  * If not, it redirects to the endpoint provided by the user's authorization agent
1386
1382
  * See https://solid.github.io/data-interoperability-panel/specification/#authorization-agent
1387
- */ const $c512de108ef5d674$var$fetchAppRegistration = ()=>({
1383
+ */ const $c512de108ef5d674$var$fetchAppRegistration = (pluginConfig = {})=>{
1384
+ const { includeSelectedResources: includeSelectedResources = true } = pluginConfig;
1385
+ return {
1388
1386
  transformConfig: async (config)=>{
1389
1387
  const token = localStorage.getItem('token');
1390
1388
  // If the user is logged in
@@ -1406,15 +1404,27 @@ var $37dc42f6e1c3b4af$export$2e2bcd8739ae039 = $37dc42f6e1c3b4af$var$getContaine
1406
1404
  const newConfig = {
1407
1405
  ...config
1408
1406
  };
1409
- // Load data grants concurrently to improve performances
1407
+ // Load access grants concurrently to improve performances
1410
1408
  const results = await Promise.all((0, $e6fbab1f303bdb93$export$2e2bcd8739ae039)(appRegistration['interop:hasAccessGrant']).map(async (accessGrantUri)=>{
1411
1409
  const { json: accessGrant } = await config.httpClient(accessGrantUri);
1412
- return Promise.all((0, $e6fbab1f303bdb93$export$2e2bcd8739ae039)(accessGrant['interop:hasDataGrant']).map(async (dataGrantUri)=>{
1413
- const { json: dataGrant } = await config.httpClient(dataGrantUri);
1414
- return (0, $37dc42f6e1c3b4af$export$2e2bcd8739ae039)(dataGrant['interop:hasDataRegistration'], config);
1415
- }));
1410
+ const container = await (0, $37dc42f6e1c3b4af$export$2e2bcd8739ae039)(accessGrant['interop:hasDataRegistration'], config);
1411
+ container.server = accessGrant['interop:dataOwner'];
1412
+ if (accessGrant['interop:scopeOfGrant'] === 'interop:AllFromRegistry') return container;
1413
+ else if (accessGrant['interop:scopeOfGrant'] === 'interop:SelectedFromRegistry') {
1414
+ if (!includeSelectedResources) return undefined;
1415
+ container.selectedResources = (0, $e6fbab1f303bdb93$export$2e2bcd8739ae039)(accessGrant['interop:hasDataInstance']);
1416
+ return container;
1417
+ }
1416
1418
  }));
1417
- newConfig.dataServers.user.containers = results.flat();
1419
+ // Put data shared by other users in other servers (storages)
1420
+ for (const container of results.flat().filter((i)=>i !== undefined))if (!newConfig.dataServers[container.server]) newConfig.dataServers[container.server] = {
1421
+ pod: true,
1422
+ baseUrl: `${container.server}/data`,
1423
+ containers: [
1424
+ container
1425
+ ]
1426
+ };
1427
+ else newConfig.dataServers[container.server].containers.push(container);
1418
1428
  return newConfig;
1419
1429
  }
1420
1430
  }
@@ -1422,7 +1432,8 @@ var $37dc42f6e1c3b4af$export$2e2bcd8739ae039 = $37dc42f6e1c3b4af$var$getContaine
1422
1432
  }
1423
1433
  return config;
1424
1434
  }
1425
- });
1435
+ };
1436
+ };
1426
1437
  var $c512de108ef5d674$export$2e2bcd8739ae039 = $c512de108ef5d674$var$fetchAppRegistration;
1427
1438
 
1428
1439
 
@@ -1433,9 +1444,9 @@ const $cd772adda3024172$var$fetchDataRegistry = ()=>({
1433
1444
  const token = localStorage.getItem('token');
1434
1445
  // If the user is logged in
1435
1446
  if (token) {
1436
- if (!config.dataServers.user) throw new Error(`You must configure the user storage first with the configureUserStorage plugin`);
1437
1447
  const payload = (0, ($parcel$interopDefault($bkNnK$jwtdecode)))(token);
1438
1448
  const webId = payload.webId || payload.webid; // Currently we must deal with both formats
1449
+ if (!config.dataServers[webId]) throw new Error(`You must configure the user storage first with the configureUserStorage plugin`);
1439
1450
  const { json: user } = await config.httpClient(webId);
1440
1451
  const { json: registrySet } = await config.httpClient(user['interop:hasRegistrySet']);
1441
1452
  const { json: dataRegistry } = await config.httpClient(registrySet['interop:hasDataRegistry']);
@@ -1446,7 +1457,7 @@ const $cd772adda3024172$var$fetchDataRegistry = ()=>({
1446
1457
  const newConfig = {
1447
1458
  ...config
1448
1459
  };
1449
- newConfig.dataServers.user.containers?.push(...results.flat());
1460
+ newConfig.dataServers[webId].containers?.push(...results.flat());
1450
1461
  return newConfig;
1451
1462
  }
1452
1463
  }
@@ -1466,9 +1477,9 @@ const $69d4da9beaa62ac6$var$fetchTypeIndexes = ()=>({
1466
1477
  const token = localStorage.getItem('token');
1467
1478
  // If the user is logged in
1468
1479
  if (token) {
1469
- if (!config.dataServers.user) throw new Error(`You must configure the user storage first with the configureUserStorage plugin`);
1470
1480
  const payload = (0, ($parcel$interopDefault($bkNnK$jwtdecode)))(token);
1471
1481
  const webId = payload.webId || payload.webid; // Currently we must deal with both formats
1482
+ if (!config.dataServers[webId]) throw new Error(`You must configure the user storage first with the configureUserStorage plugin`);
1472
1483
  const { json: user } = await config.httpClient(webId);
1473
1484
  const typeRegistrations = {
1474
1485
  public: [],
@@ -1497,17 +1508,17 @@ const $69d4da9beaa62ac6$var$fetchTypeIndexes = ()=>({
1497
1508
  separateNumbers: true
1498
1509
  })
1499
1510
  },
1500
- path: typeRegistration['solid:instanceContainer'].replace(newConfig.dataServers.user.baseUrl, ''),
1511
+ path: typeRegistration['solid:instanceContainer'].replace(newConfig.dataServers[webId].baseUrl, ''),
1501
1512
  types: await (0, $9ab033d1ec46b5da$export$2e2bcd8739ae039)(types, user['@context']),
1502
1513
  private: mode === 'private'
1503
1514
  };
1504
- const containerIndex = newConfig.dataServers.user.containers.findIndex((c)=>c.path === container.path);
1515
+ const containerIndex = newConfig.dataServers[webId].containers.findIndex((c)=>c.path === container.path);
1505
1516
  if (containerIndex !== -1) // If a container with this URI already exist, add type registration information if they are not set
1506
- newConfig.dataServers.user.containers[containerIndex] = {
1517
+ newConfig.dataServers[webId].containers[containerIndex] = {
1507
1518
  ...container,
1508
- ...newConfig.dataServers.user.containers[containerIndex]
1519
+ ...newConfig.dataServers[webId].containers[containerIndex]
1509
1520
  };
1510
- else newConfig.dataServers.user.containers.push(container);
1521
+ else newConfig.dataServers[webId].containers.push(container);
1511
1522
  }
1512
1523
  return newConfig;
1513
1524
  }