@@ -20,22 +20,22 @@ import nodePath from 'node:path';
20
20
* @param {string } endpoint Endpoint name.
21
21
* @param {{}[] } data An array from JSON response to map and sort.
22
22
* @param {Callback } callback Callbacks for mapping and sorting arrays.
23
+ * @param {string } fileName Name of snapshot response file.
23
24
* @returns {RSnapshot } Aggregated file data to write in the collection directory.
24
25
*/
25
- const aggregateJson = ( name , endpoint , data , callback ) => {
26
+ const aggregateJson = ( name , endpoint , data , callback , fileName ) => {
26
27
const dirName = import . meta. dirname ,
27
- fileName = new Date ( ) . toISOString ( ) + '.json' ,
28
28
path = `../collection/${ name } /${ endpoint . toLowerCase ( ) } ` ,
29
29
filePath = nodePath . join ( dirName , path ) ,
30
30
filePathFull = nodePath . join ( filePath , fileName ) ;
31
+
31
32
let array , fileData ;
32
33
33
34
if ( endpoint === 'CURRENCY_ALL' ) {
34
35
const currencies = data . map ( callback . currencies ) ;
35
36
36
37
array = currencies . sort ( callback . sort ) ;
37
- }
38
- if ( endpoint === 'CURRENCY_NETWORK_ALL' ) {
38
+ } else if ( endpoint === 'CURRENCY_NETWORK_ALL' ) {
39
39
const networks = data . map ( callback . networks ) ,
40
40
arrayDupes = networks . reduce ( ( accum , chain ) => accum . concat ( chain ) , [ ] ) ,
41
41
arrayUnique = arrayDupes . filter (
@@ -44,6 +44,7 @@ const aggregateJson = (name, endpoint, data, callback) => {
44
44
45
45
array = arrayUnique . sort ( ( a , b ) => a . chain . localeCompare ( b . chain ) ) ;
46
46
}
47
+
47
48
fileData = JSON . stringify ( array , null , 2 ) ;
48
49
nodeFs . mkdirSync ( filePath , { recursive : true } ) ;
49
50
nodeFs . writeFileSync ( filePathFull , fileData ) ;
@@ -58,9 +59,10 @@ const aggregateJson = (name, endpoint, data, callback) => {
58
59
* @param {string } endpoint Endpoint name.
59
60
* @param {{}[] } data An array from JSON response to map.
60
61
* @param {Callback } callback Callbacks for mapping and sorting arrays.
62
+ * @param {string } fileName Name of snapshot response file.
61
63
* @returns {RSnapshot } Aggregated file data to write in the collection directory.
62
64
*/
63
- const responseAggregate = ( api , endpoint , data , callback ) => {
65
+ const responseAggregate = ( api , endpoint , data , callback , fileName ) => {
64
66
const { options } = global . apiTools ,
65
67
{ prefs } = api ,
66
68
isEnabled = prefs . enabled . includes ( 'aggregate' ) ,
@@ -69,14 +71,14 @@ const responseAggregate = (api, endpoint, data, callback) => {
69
71
70
72
if ( typeof aggregate === 'boolean' ) {
71
73
if ( aggregate ) {
72
- const { fileData, fileName } = aggregateJson ( api . name , endpoint , data , callback ) ;
74
+ const { fileData } = aggregateJson ( api . name , endpoint , data , callback , fileName ) ;
73
75
74
76
return { fileData, fileName } ;
75
77
}
76
78
} else {
77
79
if ( isEnabled ) {
78
80
if ( isAggregate ) {
79
- const { fileData, fileName } = aggregateJson ( api . name , endpoint , data , callback ) ;
81
+ const { fileData } = aggregateJson ( api . name , endpoint , data , callback , fileName ) ;
80
82
81
83
return { fileData, fileName } ;
82
84
} else console . info ( `Aggregate: endpoint "${ endpoint } " is not enabled is settings.` ) ;
0 commit comments