@@ -49,45 +49,83 @@ function addCommaAfterEachLine(data: string): string {
49
49
return jsonDataWithCommas . join ( '\n' ) ;
50
50
}
51
51
52
- function recursivelyReplaceData ( obj : any , idCounter : number , idMap : Map < string , string > ) {
53
- for ( let key in obj ) {
54
- if ( typeof obj [ key ] === 'string' && isDateLikeString ( obj [ key ] ) ) {
55
- obj [ key ] = `[[ISODateString]]` ;
56
- } else if ( key . includes ( 'timestamp' ) ) {
57
- obj [ key ] = `[[timestamp]]` ;
58
- } else if ( typeof obj [ key ] === 'number' && obj [ key ] > 1000 ) {
59
- obj [ key ] = `[[highNumber]]` ;
60
- } else if ( key === 'if-none-match' ) {
61
- if ( obj [ key ] . startsWith ( 'W/' ) ) {
62
- obj [ key ] = `[[W/entityTagValue]]` ;
63
- } else {
64
- obj [ key ] = `[[entityTagValue]]` ;
52
+ // has to be an object to be able to pass by reference. Otherwise, the counter would not increase
53
+ type Counter = {
54
+ value : number ;
55
+ } ;
56
+
57
+ function recursivelyReplaceData (
58
+ obj : any ,
59
+ idCounter : Counter ,
60
+ idMap : Map < string , string > ,
61
+ filenameCounter : Counter ,
62
+ filenameMap : Map < string , string > ,
63
+ ) {
64
+ if ( Array . isArray ( obj ) ) {
65
+ // some values are arrays with objects in it
66
+ obj . forEach ( ( item , index ) => {
67
+ if ( typeof item === 'object' && item !== null ) {
68
+ recursivelyReplaceData ( item , idCounter , idMap , filenameCounter , filenameMap ) ;
65
69
}
66
- } else if ( key . includes ( '_id' ) ) {
67
- if ( idMap . has ( obj [ key ] ) ) {
68
- // give the same ID replacement to the same value
69
- obj [ key ] = idMap . get ( obj [ key ] ) ;
70
- } else {
71
- const newId = `[[ID${ idCounter ++ } ]]` ;
72
- idMap . set ( obj [ key ] , newId ) ;
73
- obj [ key ] = newId ;
70
+ } ) ;
71
+ } else {
72
+ for ( let key in obj ) {
73
+ if ( typeof obj [ key ] === 'string' && isDateLikeString ( obj [ key ] ) ) {
74
+ obj [ key ] = `[[ISODateString]]` ;
75
+ } else if ( key . includes ( 'timestamp' ) ) {
76
+ obj [ key ] = `[[timestamp]]` ;
77
+ } else if ( key . includes ( 'timestamp' ) ) {
78
+ obj [ key ] = `[[timestamp]]` ;
79
+ } else if ( typeof obj [ key ] === 'number' && obj [ key ] > 1000 ) {
80
+ obj [ key ] = `[[highNumber]]` ;
81
+ } else if ( key === 'if-none-match' ) {
82
+ if ( obj [ key ] . startsWith ( 'W/' ) ) {
83
+ obj [ key ] = `[[W/entityTagValue]]` ;
84
+ } else {
85
+ obj [ key ] = `[[entityTagValue]]` ;
86
+ }
87
+ } else if ( key === 'user-agent' ) {
88
+ obj [ key ] = `[[user-agent]]` ;
89
+ } else if ( key . includes ( '_id' ) ) {
90
+ if ( idMap . has ( obj [ key ] ) ) {
91
+ // give the same ID replacement to the same value
92
+ obj [ key ] = idMap . get ( obj [ key ] ) ;
93
+ } else {
94
+ const newId = `[[ID${ idCounter . value ++ } ]]` ;
95
+ idMap . set ( obj [ key ] , newId ) ;
96
+ obj [ key ] = newId ;
97
+ }
98
+ } else if ( key === 'filename' ) {
99
+ if ( filenameMap . has ( obj [ key ] ) ) {
100
+ // give the same ID replacement to the same value
101
+ obj [ key ] = filenameMap . get ( obj [ key ] ) ;
102
+ } else {
103
+ const newId = `[[FILENAME${ filenameCounter . value ++ } ]]` ;
104
+ filenameMap . set ( obj [ key ] , newId ) ;
105
+ obj [ key ] = newId ;
106
+ }
107
+ }
108
+ // recurse into object or array
109
+ else if ( typeof obj [ key ] === 'object' && obj [ key ] !== null ) {
110
+ recursivelyReplaceData ( obj [ key ] , idCounter , idMap , filenameCounter , filenameMap ) ;
74
111
}
75
- } else if ( typeof obj [ key ] === 'object' && obj [ key ] !== null ) {
76
- recursivelyReplaceData ( obj [ key ] , idCounter , idMap ) ;
77
112
}
78
113
}
79
114
}
80
115
81
116
function replaceDynamicValues ( data : string ) : string [ ] {
82
117
const jsonData = JSON . parse ( data ) ;
83
118
84
- recursivelyReplaceData ( jsonData , 1 , new Map ( ) ) ;
119
+ recursivelyReplaceData ( jsonData , { value : 1 } , new Map ( ) , { value : 1 } , new Map ( ) ) ;
85
120
86
121
// change remaining dynamic values
87
122
jsonData . forEach ( ( item : any ) => {
88
123
if ( item . trace ?. public_key ) {
89
124
item . trace . public_key = '[[publicKey]]' ;
90
125
}
126
+ if ( item . dsn ) {
127
+ item . dsn = '[[dsn]]' ;
128
+ }
91
129
} ) ;
92
130
93
131
return jsonData ;
@@ -103,13 +141,15 @@ async function transformSavedJSON() {
103
141
104
142
const jsonData = addCommaAfterEachLine ( data ) ;
105
143
const transformedJSON = replaceDynamicValues ( jsonData ) ;
144
+
106
145
const objWithReq = transformedJSON [ 2 ] as unknown as { request : { url : string } } ;
146
+ const type = ( transformedJSON [ 1 ] as unknown as { type : string } ) . type ;
107
147
108
148
if ( 'request' in objWithReq ) {
109
149
const url = objWithReq . request . url ;
110
150
const replaceForwardSlashes = ( str : string ) => str . split ( '/' ) . join ( '_' ) ;
111
151
112
- const filepath = `payload-files/${ APP } /${ replaceForwardSlashes ( extractPathFromUrl ( url ) ) } .json` ;
152
+ const filepath = `payload-files/${ APP } /${ replaceForwardSlashes ( extractPathFromUrl ( url ) ) } -- ${ type } .json` ;
113
153
114
154
writeFile ( filepath , JSON . stringify ( transformedJSON , null , 2 ) ) . then ( ( ) => {
115
155
console . log ( `Successfully replaced data and saved file in ${ filepath } ` ) ;
0 commit comments