@google-cloud/nodejs-common 2.0.16-alpha → 2.0.16-beta
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/package.json +1 -1
- package/src/apis/google_ads_api.js +17 -6
- package/src/components/utils.js +34 -0
package/package.json
CHANGED
|
@@ -95,7 +95,9 @@ const {
|
|
|
95
95
|
getLogger,
|
|
96
96
|
BatchResult,
|
|
97
97
|
extractObject,
|
|
98
|
+
changeNamingFromSnakeToLowerCamel,
|
|
98
99
|
changeObjectNamingFromSnakeToLowerCamel,
|
|
100
|
+
changeObjectNamingFromLowerCamelToSnake,
|
|
99
101
|
} = require('../components/utils.js');
|
|
100
102
|
|
|
101
103
|
/** @type {!ReadonlyArray<string>} */
|
|
@@ -438,23 +440,31 @@ class GoogleAdsApi {
|
|
|
438
440
|
* `results` in JSON format strings with the delimit of a line breaker.
|
|
439
441
|
* @param {string} customerId
|
|
440
442
|
* @param {string} loginCustomerId Login customer account ID (Mcc Account id).
|
|
441
|
-
* @param {string} query
|
|
443
|
+
* @param {string} query A Google Ads Query string.
|
|
444
|
+
* @param {boolean} outputSnake Output JSON objects in snake_case.
|
|
442
445
|
* @return {!Promise<string>}
|
|
443
446
|
*/
|
|
444
|
-
async cleanedStreamReport(customerId, loginCustomerId, query
|
|
447
|
+
async cleanedStreamReport(customerId, loginCustomerId, query,
|
|
448
|
+
outputSnake = false) {
|
|
445
449
|
const cleanReportStream = new Transform({
|
|
446
450
|
writableObjectMode: true,
|
|
447
451
|
transform(chunk, encoding, callback) {
|
|
448
452
|
const { fieldMask: { paths } } = chunk;
|
|
449
|
-
const
|
|
453
|
+
const camelPaths = paths.map((path) => {
|
|
454
|
+
return path.split('.').map(changeNamingFromSnakeToLowerCamel).join('.');
|
|
455
|
+
});
|
|
456
|
+
const extractor = extractObject(camelPaths);
|
|
457
|
+
const results = outputSnake
|
|
458
|
+
? chunk.results.map(extractor).map(changeObjectNamingFromLowerCamelToSnake)
|
|
459
|
+
: chunk.results.map(extractor);
|
|
450
460
|
// Add a line break after each chunk to keep files in proper format.
|
|
451
|
-
const data =
|
|
452
|
-
+ '\n';
|
|
461
|
+
const data = results.map(JSON.stringify).join('\n') + '\n';
|
|
453
462
|
callback(null, data);
|
|
454
463
|
}
|
|
455
464
|
});
|
|
456
465
|
const stream = await this.streamReport(customerId, loginCustomerId, query);
|
|
457
|
-
return stream.
|
|
466
|
+
return stream.on('error', (error) => cleanReportStream.emit('error', error))
|
|
467
|
+
.pipe(cleanReportStream);
|
|
458
468
|
}
|
|
459
469
|
|
|
460
470
|
/**
|
|
@@ -1536,6 +1546,7 @@ const debugGoogleAdsFailure = (failures, request) => {
|
|
|
1536
1546
|
}
|
|
1537
1547
|
|
|
1538
1548
|
module.exports = {
|
|
1549
|
+
GoogleAdsField,
|
|
1539
1550
|
ConversionConfig,
|
|
1540
1551
|
CustomerMatchRecord,
|
|
1541
1552
|
CustomerMatchConfig,
|
package/src/components/utils.js
CHANGED
|
@@ -539,6 +539,16 @@ const changeNamingFromSnakeToLowerCamel = (name) => {
|
|
|
539
539
|
(initial) => initial.substring(1).toUpperCase());
|
|
540
540
|
};
|
|
541
541
|
|
|
542
|
+
/**
|
|
543
|
+
* For more details, see:
|
|
544
|
+
* https://developers.google.com/google-ads/api/docs/rest/design/json-mappings
|
|
545
|
+
* @param {string} name Identifiers.
|
|
546
|
+
* @return {string}
|
|
547
|
+
*/
|
|
548
|
+
const changeNamingFromLowerCamelToSnake = (name) => {
|
|
549
|
+
return name.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
550
|
+
};
|
|
551
|
+
|
|
542
552
|
/**
|
|
543
553
|
* Converts a JSON object which has snake naming convention to lower camel
|
|
544
554
|
* naming.
|
|
@@ -561,6 +571,28 @@ const changeObjectNamingFromSnakeToLowerCamel = (obj) => {
|
|
|
561
571
|
}
|
|
562
572
|
}
|
|
563
573
|
|
|
574
|
+
/**
|
|
575
|
+
* Converts a JSON object which has lower camel naming convention to snake
|
|
576
|
+
* naming.
|
|
577
|
+
*
|
|
578
|
+
* @param {object} obj
|
|
579
|
+
* @return {object}
|
|
580
|
+
*/
|
|
581
|
+
const changeObjectNamingFromLowerCamelToSnake = (obj) => {
|
|
582
|
+
if (Array.isArray(obj)) {
|
|
583
|
+
return obj.map(changeObjectNamingFromLowerCamelToSnake);
|
|
584
|
+
} else if (typeof obj === 'object') {
|
|
585
|
+
const newObj = {};
|
|
586
|
+
Object.keys(obj).forEach((key) => {
|
|
587
|
+
newObj[changeNamingFromLowerCamelToSnake(key)] =
|
|
588
|
+
changeObjectNamingFromLowerCamelToSnake(obj[key]);
|
|
589
|
+
});
|
|
590
|
+
return newObj;
|
|
591
|
+
} else {
|
|
592
|
+
return obj;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
564
596
|
/**
|
|
565
597
|
* Returns the response data for a HTTP request. It will retry the specific
|
|
566
598
|
* times if there was errors happened.
|
|
@@ -606,6 +638,8 @@ module.exports = {
|
|
|
606
638
|
getObjectByPath,
|
|
607
639
|
changeNamingFromSnakeToUpperCamel,
|
|
608
640
|
changeNamingFromSnakeToLowerCamel,
|
|
641
|
+
changeNamingFromLowerCamelToSnake,
|
|
609
642
|
changeObjectNamingFromSnakeToLowerCamel,
|
|
643
|
+
changeObjectNamingFromLowerCamelToSnake,
|
|
610
644
|
requestWithRetry,
|
|
611
645
|
};
|