@semapps/semantic-data-provider 1.1.1 → 1.1.3

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
@@ -920,7 +897,7 @@ const $95cbc03f25caf72a$var$getListMethod = (config)=>async (resourceId, params)
920
897
  if (!dataModel) throw new Error(`Resource ${resourceId} is not mapped in resources file`);
921
898
  let containers = [];
922
899
  if (!params.filter?._servers && dataModel.list?.containers) {
923
- if (Array.isArray(dataModel.list?.containers)) throw new Error(`The list.containers property of ${resourceId} dataModel must be of type object ({ serverKey: [containerUri] })`);
900
+ if (!Array.isArray(dataModel.list?.containers)) throw new Error(`The list.containers property of ${resourceId} dataModel must be of type array`);
924
901
  // If containers are set explicitly, use them
925
902
  containers = (0, $37c161736d0d7276$export$2e2bcd8739ae039)(dataModel.list.containers, dataServers);
926
903
  } else if (dataModel.shapeTreeUri) containers = (0, $1d94774735aa9ea2$export$2e2bcd8739ae039)(dataModel.shapeTreeUri, params?.filter?._servers || dataModel.list?.servers, dataServers);
@@ -1024,7 +1001,7 @@ const $ceaafb56f75454f0$var$updateMethod = (config)=>async (resourceId, params)=
1024
1001
  const { httpClient: httpClient, jsonContext: jsonContext, dataServers: dataServers } = config;
1025
1002
  const serverKey = (0, $59a07b932dae8600$export$2e2bcd8739ae039)(params.id, dataServers);
1026
1003
  // Upload files, if there are any
1027
- const { updatedRecord: updatedRecord, filesToDelete: filesToDelete } = await (0, $6fcb30f76390d142$export$2e2bcd8739ae039).upload(params.data, config, serverKey);
1004
+ const { updatedRecord: updatedRecord } = await (0, $6fcb30f76390d142$export$2e2bcd8739ae039).upload(params.data, config, serverKey);
1028
1005
  params.data = updatedRecord;
1029
1006
  await httpClient(`${params.id}`, {
1030
1007
  method: 'PUT',
@@ -1033,8 +1010,6 @@ const $ceaafb56f75454f0$var$updateMethod = (config)=>async (resourceId, params)=
1033
1010
  ...params.data
1034
1011
  })
1035
1012
  });
1036
- // Delete files only if update is successful
1037
- await (0, $6fcb30f76390d142$export$2e2bcd8739ae039).delete(filesToDelete, config);
1038
1013
  return {
1039
1014
  data: params.data
1040
1015
  };
@@ -1101,16 +1076,16 @@ var $341dff85fe619d85$export$2e2bcd8739ae039 = $341dff85fe619d85$var$httpClient;
1101
1076
 
1102
1077
 
1103
1078
 
1104
- const $9ab033d1ec46b5da$var$isURL = (value)=>(typeof value === 'string' || value instanceof String) && value.startsWith('http');
1079
+ const $9ab033d1ec46b5da$var$isURI = (value)=>(typeof value === 'string' || value instanceof String) && (value.startsWith('http') || value.startsWith('urn:'));
1105
1080
  const $9ab033d1ec46b5da$var$expandTypes = async (types, context)=>{
1106
1081
  // If types are already full URIs, return them immediately
1107
- if (types.every((type)=>$9ab033d1ec46b5da$var$isURL(type))) return types;
1082
+ if (types.every((type)=>$9ab033d1ec46b5da$var$isURI(type))) return types;
1108
1083
  const result = await (0, ($parcel$interopDefault($bkNnK$jsonld))).expand({
1109
1084
  '@context': context,
1110
1085
  '@type': types
1111
1086
  });
1112
1087
  const expandedTypes = (0, $e6fbab1f303bdb93$export$2e2bcd8739ae039)(result[0]['@type']);
1113
- if (!expandedTypes.every((type)=>$9ab033d1ec46b5da$var$isURL(type))) throw new Error(`
1088
+ if (!expandedTypes.every((type)=>$9ab033d1ec46b5da$var$isURI(type))) throw new Error(`
1114
1089
  Could not expand all types (${expandedTypes.join(', ')}).
1115
1090
  Is an ontology missing or not registered yet on the local context ?
1116
1091
  `);
@@ -1220,6 +1195,8 @@ var $fcf4eee3b18e8350$export$2e2bcd8739ae039 = $fcf4eee3b18e8350$var$getOntologi
1220
1195
  config.dataServers ??= {};
1221
1196
  // Configure httpClient with initial data servers, so that plugins may use it
1222
1197
  config.httpClient = (0, $341dff85fe619d85$export$2e2bcd8739ae039)(config.dataServers);
1198
+ // Useful for debugging.
1199
+ document.httpClient = config.httpClient;
1223
1200
  for (const plugin of config.plugins)if (plugin.transformConfig) config = await plugin.transformConfig(config);
1224
1201
  // Configure again httpClient with possibly updated data servers
1225
1202
  config.httpClient = (0, $341dff85fe619d85$export$2e2bcd8739ae039)(config.dataServers);
@@ -1553,16 +1530,22 @@ const $1395e306228d41f2$var$fetchVoidEndpoints = ()=>({
1553
1530
  const expandedTypes = await (0, $9ab033d1ec46b5da$export$2e2bcd8739ae039)([
1554
1531
  type
1555
1532
  ], result.context);
1556
- const containerIndex = newConfig.dataServers[result.key].containers.findIndex((c)=>c.types?.some((t)=>expandedTypes.includes(t)));
1557
- if (containerIndex && containerIndex !== -1) // If a container with this type already exist, overwrite path and types
1558
- newConfig.dataServers[result.key].containers[containerIndex] = {
1559
- ...newConfig.dataServers[result.key].containers[containerIndex],
1533
+ const containerIndex = newConfig.dataServers[result.key].containers.findIndex((c)=>c.path === path);
1534
+ if (containerIndex !== -1) {
1535
+ // If a container with this path already exist, merge types
1536
+ const mergedTypes = [
1537
+ ...newConfig.dataServers[result.key].containers[containerIndex].types,
1538
+ ...expandedTypes
1539
+ ].filter((v, i, a)=>a.indexOf(v) === i);
1540
+ newConfig.dataServers[result.key].containers[containerIndex] = {
1541
+ ...newConfig.dataServers[result.key].containers[containerIndex],
1542
+ types: mergedTypes,
1543
+ binaryResources: mergedTypes.includes('http://semapps.org/ns/core#File')
1544
+ };
1545
+ } else newConfig.dataServers[result.key].containers.push({
1560
1546
  path: path,
1561
- types: expandedTypes
1562
- };
1563
- else newConfig.dataServers[result.key].containers.push({
1564
- path: path,
1565
- types: expandedTypes
1547
+ types: expandedTypes,
1548
+ binaryResources: expandedTypes.includes('http://semapps.org/ns/core#File')
1566
1549
  });
1567
1550
  }
1568
1551
  }
@@ -1725,7 +1708,6 @@ var $d3746ce11bc56f3b$export$2e2bcd8739ae039 = $d3746ce11bc56f3b$var$useContaine
1725
1708
 
1726
1709
 
1727
1710
 
1728
-
1729
1711
  const $ff3623bf1421ebcc$var$findCreateContainerWithTypes = (types, createServerKey, dataServers)=>{
1730
1712
  if (!dataServers[createServerKey].containers) throw new Error(`Data server ${createServerKey} has no declared containers`);
1731
1713
  const matchingContainers = dataServers[createServerKey].containers.filter((container)=>container.types?.some((t)=>types.includes(t)));
@@ -1744,11 +1726,8 @@ const $32d32215b4e4729f$var$useGetCreateContainerUri = ()=>{
1744
1726
  const getCreateContainerUri = (0, $bkNnK$react.useCallback)((resourceId)=>{
1745
1727
  if (!dataModels || !dataServers || !dataModels[resourceId]) return undefined;
1746
1728
  const dataModel = dataModels[resourceId];
1747
- if (dataModel.create?.container) {
1748
- const [serverKey, path] = Object.entries(dataModel.create.container)[0];
1749
- if (!serverKey || !dataServers[serverKey]) throw new Error(`Wrong key for the dataModel.create.container config of resource ${resourceId}`);
1750
- return (0, ($parcel$interopDefault($bkNnK$urljoin)))(dataServers[serverKey].baseUrl, path);
1751
- } else if (dataModel.create?.server) return (0, $ff3623bf1421ebcc$export$2e2bcd8739ae039)(dataModel.types, dataModel.create?.server, dataServers);
1729
+ if (dataModel.create?.container) return dataModel.create.container;
1730
+ else if (dataModel.create?.server) return (0, $ff3623bf1421ebcc$export$2e2bcd8739ae039)(dataModel.types, dataModel.create.server, dataServers);
1752
1731
  else {
1753
1732
  const defaultServerKey = (0, $8f44b7c15b8b8e1d$export$2e2bcd8739ae039)('default', dataServers);
1754
1733
  if (!defaultServerKey) throw new Error(`No default dataServer found. You can set explicitly one setting the "default" attribute to true`);