@energyweb/node-red-contrib-green-proof-worker
Version:
## Peer dependencies
68 lines (67 loc) • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadAllGraphqlPages = exports.partition = exports.diff = exports.groupBy = void 0;
const groupBy = (array, getKey, getValue) => {
return array.reduce((result, currentItem) => {
const key = getKey(currentItem);
const value = getValue(currentItem);
result[key] || (result[key] = value);
return result;
}, {});
};
exports.groupBy = groupBy;
const diff = (array, otherArray) => {
return array.filter(item1 => !otherArray.includes(item1));
};
exports.diff = diff;
const partition = (array, condition) => {
const positive = [];
const negative = [];
array.forEach(item => {
const checkResult = condition(item);
if (checkResult) {
positive.push(item);
}
else {
negative.push(item);
}
});
return [positive, negative];
};
exports.partition = partition;
const downloadAllGraphqlPages = async (params) => {
const results = [];
const limit = 50;
let offset = 0;
let iterations = 0;
while (true) {
iterations += 1;
if (iterations > 100) {
throw new Error('Downloading pages from GraphQL exceeded 100 iterations');
}
const queryVariables = {
...params.variables,
limit,
offset,
};
const queryResult = await fetch(params.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: params.query,
variables: queryVariables,
})
}).then(r => r.json()).then(r => params.mapResponse(r));
if (queryResult.length === 0) {
break;
}
else {
results.push(...queryResult);
offset += limit;
}
}
return results;
};
exports.downloadAllGraphqlPages = downloadAllGraphqlPages;