pangea-server 3.3.76 → 3.3.77

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.
@@ -55,73 +55,48 @@ function hasInstancesAndTotalCount(obj) {
55
55
  return typeof obj === 'object' && obj !== null && 'instances' in obj && 'totalCount' in obj;
56
56
  }
57
57
  async function setFilesUrls(data) {
58
- const entries = [];
58
+ const targets = [];
59
59
  const visited = new WeakSet();
60
- function matchesSingular(key) {
61
- const k = key.toLowerCase();
62
- return k === 'image' || k === 'video' || k.endsWith('image') || k.endsWith('video');
63
- }
64
- function matchesPlural(key) {
65
- const k = key.toLowerCase();
66
- return k === 'images' || k === 'videos' || k.endsWith('images') || k.endsWith('videos');
60
+ function isTargetKey(key) {
61
+ return key.toLowerCase().includes('image') || key.toLowerCase().includes('video');
67
62
  }
68
63
  function collect(node) {
69
64
  if (!node)
70
65
  return;
66
+ if (Array.isArray(node)) {
67
+ node.forEach(collect);
68
+ return;
69
+ }
71
70
  if (typeof node !== 'object')
72
71
  return;
73
72
  if (visited.has(node))
74
73
  return;
75
74
  visited.add(node);
76
- if (Array.isArray(node)) {
77
- node.forEach(collect);
78
- return;
79
- }
80
- const payload = node.dataValues ? node.dataValues : node;
81
- if (payload !== node) {
82
- if (visited.has(payload))
83
- return;
84
- visited.add(payload);
85
- }
86
- for (const [key, value] of Object.entries(payload)) {
87
- if (typeof value === 'string' && value.length > 0) {
88
- if (matchesSingular(key)) {
89
- entries.push({ node: payload, field: key, type: 'singular' });
75
+ for (const [key, value] of Object.entries(node)) {
76
+ if (isTargetKey(key)) {
77
+ if (typeof value === 'string' && value.length) {
78
+ targets.push({ node, key, type: 'single' });
90
79
  }
91
- }
92
- else if (Array.isArray(value) && value.every((v) => typeof v === 'string')) {
93
- if (matchesPlural(key)) {
94
- entries.push({ node: payload, field: key, type: 'plural' });
80
+ else if (Array.isArray(value) && value.length && value.every((v) => typeof v === 'string')) {
81
+ targets.push({ node, key, type: 'array' });
95
82
  }
96
83
  }
97
- if (typeof value === 'object' && value !== null) {
98
- collect(value);
99
- }
84
+ collect(value);
100
85
  }
101
86
  }
102
87
  collect(data);
103
- if (!entries.length)
88
+ if (!targets.length)
104
89
  return;
105
- const allFileKeys = new Set();
106
- for (const { node, field, type } of entries) {
107
- if (type === 'singular') {
108
- allFileKeys.add(node[field]);
109
- }
110
- else {
111
- for (const v of node[field])
112
- allFileKeys.add(v);
113
- }
114
- }
115
- const uniqueKeys = Array.from(allFileKeys);
116
- const urls = await Promise.all(uniqueKeys.map((key) => helpers_1.FileStorage.GenerateDownloadUrl(key)));
117
- const urlMap = new Map();
118
- uniqueKeys.forEach((key, i) => urlMap.set(key, urls[i]));
119
- for (const { node, field, type } of entries) {
120
- if (type === 'singular') {
121
- node[`${field}Url`] = urlMap.get(node[field]) || null;
90
+ const allFiles = Array.from(new Set(targets.flatMap((t) => (t.type === 'single' ? [t.node[t.key]] : t.node[t.key]))));
91
+ const urls = await Promise.all(allFiles.map((file) => helpers_1.FileStorage.GenerateDownloadUrl(file)));
92
+ const map = new Map();
93
+ allFiles.forEach((file, i) => map.set(file, urls[i]));
94
+ targets.forEach((t) => {
95
+ if (t.type === 'single') {
96
+ t.node[`${t.key}Url`] = map.get(t.node[t.key]) || null;
122
97
  }
123
98
  else {
124
- node[`${field}Urls`] = node[field].map((v) => urlMap.get(v) || null);
99
+ t.node[`${t.key}Urls`] = t.node[t.key].map((file) => map.get(file) || null);
125
100
  }
126
- }
101
+ });
127
102
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "3.3.76",
4
+ "version": "3.3.77",
5
5
  "files": [
6
6
  "dist"
7
7
  ],