google-ads-api 10.0.2 → 11.0.0
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/README.md +67 -10
- package/build/src/client.js +31 -80
- package/build/src/customer.d.ts +10 -0
- package/build/src/customer.js +313 -472
- package/build/src/hooks.d.ts +38 -3
- package/build/src/index.d.ts +1 -1
- package/build/src/parser.js +29 -36
- package/build/src/protos/autogen/enums.d.ts +410 -300
- package/build/src/protos/autogen/enums.js +699 -583
- package/build/src/protos/autogen/fields.d.ts +167 -151
- package/build/src/protos/autogen/fields.js +9 -1
- package/build/src/protos/autogen/resourceNames.d.ts +16 -0
- package/build/src/protos/autogen/resourceNames.js +177 -157
- package/build/src/protos/autogen/serviceFactory.d.ts +193 -147
- package/build/src/protos/autogen/serviceFactory.js +18420 -17447
- package/build/src/protos/index.d.ts +6 -6
- package/build/src/protos/index.js +7 -7
- package/build/src/query.js +74 -81
- package/build/src/service.js +102 -107
- package/build/src/testUtils.js +47 -52
- package/build/src/utils.js +18 -20
- package/build/src/version.d.ts +1 -1
- package/build/src/version.js +1 -1
- package/package.json +3 -3
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import allProtos, { protos } from "google-ads-node";
|
|
2
|
-
export declare const VERSION: "
|
|
3
|
-
export import common = protos.google.ads.googleads.
|
|
4
|
-
export import errors = protos.google.ads.googleads.
|
|
5
|
-
export import resources = protos.google.ads.googleads.
|
|
6
|
-
export import services = protos.google.ads.googleads.
|
|
7
|
-
export import internalEnums = protos.google.ads.googleads.
|
|
2
|
+
export declare const VERSION: "v11";
|
|
3
|
+
export import common = protos.google.ads.googleads.v11.common;
|
|
4
|
+
export import errors = protos.google.ads.googleads.v11.errors;
|
|
5
|
+
export import resources = protos.google.ads.googleads.v11.resources;
|
|
6
|
+
export import services = protos.google.ads.googleads.v11.services;
|
|
7
|
+
export import internalEnums = protos.google.ads.googleads.v11.enums;
|
|
8
8
|
export { enums } from "./autogen/enums";
|
|
9
9
|
export { fields } from "./autogen/fields";
|
|
10
10
|
export { GoogleAdsServiceClient } from "google-ads-node";
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.longrunning = exports.protobuf = exports.GoogleAdsServiceClient = exports.fields = exports.enums = exports.internalEnums = exports.services = exports.resources = exports.errors = exports.common = exports.VERSION = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const google_ads_node_1 = require("google-ads-node");
|
|
5
|
+
const version_1 = require("../version");
|
|
6
6
|
// "as vN" is required to avoid type issues later on
|
|
7
7
|
exports.VERSION = version_1.googleAdsVersion;
|
|
8
8
|
// -- Expose the googleads generated types --
|
|
9
9
|
// The namespace path is shortened for ease of use
|
|
10
|
-
exports.common = google_ads_node_1.protos.google.ads.googleads.
|
|
11
|
-
exports.errors = google_ads_node_1.protos.google.ads.googleads.
|
|
12
|
-
exports.resources = google_ads_node_1.protos.google.ads.googleads.
|
|
13
|
-
exports.services = google_ads_node_1.protos.google.ads.googleads.
|
|
10
|
+
exports.common = google_ads_node_1.protos.google.ads.googleads.v11.common;
|
|
11
|
+
exports.errors = google_ads_node_1.protos.google.ads.googleads.v11.errors;
|
|
12
|
+
exports.resources = google_ads_node_1.protos.google.ads.googleads.v11.resources;
|
|
13
|
+
exports.services = google_ads_node_1.protos.google.ads.googleads.v11.services;
|
|
14
14
|
// We compile enums ourselves, the Google ones should be used internally only
|
|
15
|
-
exports.internalEnums = google_ads_node_1.protos.google.ads.googleads.
|
|
15
|
+
exports.internalEnums = google_ads_node_1.protos.google.ads.googleads.v11.enums;
|
|
16
16
|
var enums_1 = require("./autogen/enums");
|
|
17
17
|
Object.defineProperty(exports, "enums", { enumerable: true, get: function () { return enums_1.enums; } });
|
|
18
18
|
var fields_1 = require("./autogen/fields");
|
package/build/src/query.js
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
3
|
-
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
4
|
-
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
5
|
-
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
6
|
-
r[k] = a[j];
|
|
7
|
-
return r;
|
|
8
|
-
};
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
3
|
exports.buildQuery = exports.buildRequestOptions = exports.buildOrderClause = exports.buildOrderClauseNew = exports.buildOrderClauseOld = exports.completeOrderly = exports.buildLimitClause = exports.buildWhereClause = exports.extractDateConditions = exports.extractDateConstantConditions = exports.extractConstraintConditions = exports.convertNumericEnumToString = exports.validateConstraintKeyAndValue = exports.buildFromClause = exports.buildSelectClause = exports.QueryError = void 0;
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
const protos_1 = require("./protos");
|
|
5
|
+
const types_1 = require("./types");
|
|
13
6
|
var QueryKeywords;
|
|
14
7
|
(function (QueryKeywords) {
|
|
15
8
|
QueryKeywords["SELECT"] = "SELECT";
|
|
@@ -23,25 +16,18 @@ var QueryKeywords;
|
|
|
23
16
|
exports.QueryError = {
|
|
24
17
|
INVALID_CONSTRAINTS_FORMAT: "Constraints must be an array of objects or a singular object.",
|
|
25
18
|
INVALID_CONSTRAINT_KEY: "A constraint key must have a string value.",
|
|
26
|
-
INVALID_CONSTRAINT_VALUE:
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
19
|
+
INVALID_CONSTRAINT_VALUE: (key, val) =>
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
`The value of the constraint ${key} must be a string, number, boolean, or an array of these types. Here, typeof ${key} is ${typeof val}.`,
|
|
30
22
|
INVALID_CONSTRAINT_OBJECT_FORMAT: "Must specify { key, op, val } or { key: value } when using object-style constraints.",
|
|
31
|
-
INVALID_DATE_CONSTANT_TYPE:
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
INVALID_FROM_DATE_TYPE: function (fromDate) {
|
|
35
|
-
return "From date must be a string. Here, typeof from date is " + typeof fromDate;
|
|
36
|
-
},
|
|
37
|
-
INVALID_TO_DATE_TYPE: function (toDate) {
|
|
38
|
-
return "To date must be a string. Here, typeof to date is " + typeof toDate;
|
|
39
|
-
},
|
|
23
|
+
INVALID_DATE_CONSTANT_TYPE: (dateConstant) => `Date constant must be a string. Here, typeof date constant is ${typeof dateConstant}`,
|
|
24
|
+
INVALID_FROM_DATE_TYPE: (fromDate) => `From date must be a string. Here, typeof from date is ${typeof fromDate}`,
|
|
25
|
+
INVALID_TO_DATE_TYPE: (toDate) => `To date must be a string. Here, typeof to date is ${typeof toDate}`,
|
|
40
26
|
INVALID_LIMIT: "Limit must be a positive integer.",
|
|
41
27
|
INVALID_ORDER: "Order must be an array.",
|
|
42
28
|
INVALID_ORDERLY: "OrderBy arrays must only contain strings.",
|
|
43
29
|
INVALID_ORDERBY: "OrderBy must be a string or an array of strings.",
|
|
44
|
-
INVALID_SORT_ORDER:
|
|
30
|
+
INVALID_SORT_ORDER: `Sort order must be "ASC" or "DESC".`,
|
|
45
31
|
MISSING_FIELDS: 'Must specify at least one field in ("attributes","metrics","segments").',
|
|
46
32
|
MISSING_FROM_DATE: 'Expected start date range is missing - "from_date".',
|
|
47
33
|
UNDEFINED_ENTITY: "The entity of the query must be defined.",
|
|
@@ -50,15 +36,19 @@ function buildSelectClause(attributes, metrics, segments) {
|
|
|
50
36
|
if (!(attributes === null || attributes === void 0 ? void 0 : attributes.length) && !(metrics === null || metrics === void 0 ? void 0 : metrics.length) && !(segments === null || segments === void 0 ? void 0 : segments.length)) {
|
|
51
37
|
throw new Error(exports.QueryError.MISSING_FIELDS);
|
|
52
38
|
}
|
|
53
|
-
|
|
54
|
-
|
|
39
|
+
const selections = [
|
|
40
|
+
...(attributes || []),
|
|
41
|
+
...(metrics || []),
|
|
42
|
+
...(segments || []),
|
|
43
|
+
].join(", ");
|
|
44
|
+
return `${QueryKeywords.SELECT} ${selections}`;
|
|
55
45
|
}
|
|
56
46
|
exports.buildSelectClause = buildSelectClause;
|
|
57
47
|
function buildFromClause(entity) {
|
|
58
48
|
if (typeof entity === "undefined") {
|
|
59
49
|
throw new Error(exports.QueryError.UNDEFINED_ENTITY);
|
|
60
50
|
}
|
|
61
|
-
return
|
|
51
|
+
return ` ${QueryKeywords.FROM} ${entity}`;
|
|
62
52
|
}
|
|
63
53
|
exports.buildFromClause = buildFromClause;
|
|
64
54
|
function validateConstraintKeyAndValue(key, op, val) {
|
|
@@ -67,25 +57,25 @@ function validateConstraintKeyAndValue(key, op, val) {
|
|
|
67
57
|
}
|
|
68
58
|
if (typeof val === "string") {
|
|
69
59
|
if (types_1.dateConstants.includes(val)) {
|
|
70
|
-
return { op
|
|
60
|
+
return { op, val };
|
|
71
61
|
}
|
|
72
62
|
return {
|
|
73
63
|
op: "=",
|
|
74
|
-
val: new RegExp(/^'.*'$|^".*"$/g).test(val) ? val : "
|
|
64
|
+
val: new RegExp(/^'.*'$|^".*"$/g).test(val) ? val : `"${val}"`,
|
|
75
65
|
}; // must start and end in either single or double quotation marks
|
|
76
66
|
}
|
|
77
67
|
if (Array.isArray(val)) {
|
|
78
|
-
|
|
79
|
-
.map(
|
|
68
|
+
const stringifiedValue = val
|
|
69
|
+
.map((v) => {
|
|
80
70
|
if (typeof v === "string") {
|
|
81
|
-
return "
|
|
71
|
+
return `"${v}"`;
|
|
82
72
|
}
|
|
83
73
|
else {
|
|
84
74
|
return convertNumericEnumToString(key, v);
|
|
85
75
|
}
|
|
86
76
|
})
|
|
87
|
-
.join(
|
|
88
|
-
return { op: "IN", val:
|
|
77
|
+
.join(`, `);
|
|
78
|
+
return { op: "IN", val: `(${stringifiedValue})` };
|
|
89
79
|
}
|
|
90
80
|
throw new Error(exports.QueryError.INVALID_CONSTRAINT_VALUE(key, val));
|
|
91
81
|
}
|
|
@@ -94,9 +84,9 @@ function convertNumericEnumToString(key, val) {
|
|
|
94
84
|
// @ts-expect-error key does not always match an enum field
|
|
95
85
|
if (protos_1.fields.enumFields[key] && typeof val === "number") {
|
|
96
86
|
// @ts-expect-error typescript doesn't like accessing items in a namespace with a string
|
|
97
|
-
|
|
87
|
+
const enumStringValue = protos_1.enums[protos_1.fields.enumFields[key]][val]; // e.g. enums['CampaignStatus'][2] = "ENABLED"
|
|
98
88
|
if (enumStringValue) {
|
|
99
|
-
return "
|
|
89
|
+
return `"${enumStringValue}"`;
|
|
100
90
|
}
|
|
101
91
|
}
|
|
102
92
|
return val;
|
|
@@ -107,22 +97,22 @@ function extractConstraintConditions(constraints) {
|
|
|
107
97
|
return [];
|
|
108
98
|
}
|
|
109
99
|
else if (Array.isArray(constraints)) {
|
|
110
|
-
return constraints.map(
|
|
100
|
+
return constraints.map((con) => {
|
|
111
101
|
if (typeof con === "object" && !Array.isArray(con) && con !== null) {
|
|
112
102
|
// @ts-ignore
|
|
113
103
|
if (con.key && con.op && typeof con.val !== "undefined") {
|
|
114
|
-
|
|
104
|
+
const { key, op, val } = con;
|
|
115
105
|
if (typeof key !== "string") {
|
|
116
106
|
throw new Error(exports.QueryError.INVALID_CONSTRAINT_KEY);
|
|
117
107
|
}
|
|
118
|
-
|
|
108
|
+
const validatedValue = validateConstraintKeyAndValue(key, op, val);
|
|
119
109
|
// @ts-ignore
|
|
120
|
-
return key
|
|
110
|
+
return `${key} ${op} ${validatedValue.val}`;
|
|
121
111
|
}
|
|
122
112
|
else if (Object.keys(con).length === 1) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return key
|
|
113
|
+
const [[key, val]] = Object.entries(con);
|
|
114
|
+
const validatedValue = validateConstraintKeyAndValue(key, "=", val);
|
|
115
|
+
return `${key} ${validatedValue.op} ${validatedValue.val}`;
|
|
126
116
|
}
|
|
127
117
|
else {
|
|
128
118
|
throw new Error(exports.QueryError.INVALID_CONSTRAINT_OBJECT_FORMAT);
|
|
@@ -137,10 +127,9 @@ function extractConstraintConditions(constraints) {
|
|
|
137
127
|
});
|
|
138
128
|
}
|
|
139
129
|
else if (typeof constraints === "object" && constraints !== null) {
|
|
140
|
-
return Object.entries(constraints).map(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return key + " " + validatedValue.op + " " + validatedValue.val;
|
|
130
|
+
return Object.entries(constraints).map(([key, val]) => {
|
|
131
|
+
const validatedValue = validateConstraintKeyAndValue(key, "=", val);
|
|
132
|
+
return `${key} ${validatedValue.op} ${validatedValue.val}`;
|
|
144
133
|
});
|
|
145
134
|
}
|
|
146
135
|
else {
|
|
@@ -155,7 +144,7 @@ function extractDateConstantConditions(dateConstant) {
|
|
|
155
144
|
else if (typeof dateConstant !== "string") {
|
|
156
145
|
throw new Error(exports.QueryError.INVALID_DATE_CONSTANT_TYPE(dateConstant));
|
|
157
146
|
}
|
|
158
|
-
return [
|
|
147
|
+
return [`segments.date DURING ${dateConstant}`];
|
|
159
148
|
}
|
|
160
149
|
exports.extractDateConstantConditions = extractDateConstantConditions;
|
|
161
150
|
function extractDateConditions(fromDate, toDate) {
|
|
@@ -166,8 +155,8 @@ function extractDateConditions(fromDate, toDate) {
|
|
|
166
155
|
throw new Error(exports.QueryError.MISSING_FROM_DATE);
|
|
167
156
|
}
|
|
168
157
|
if (typeof fromDate !== "undefined" && typeof toDate === "undefined") {
|
|
169
|
-
|
|
170
|
-
toDate = d.getFullYear()
|
|
158
|
+
const d = new Date();
|
|
159
|
+
toDate = `${d.getFullYear()}-${("0" + (d.getMonth() + 1)).slice(-2)}-${("0" + d.getDate()).slice(-2)}`;
|
|
171
160
|
}
|
|
172
161
|
if (typeof fromDate !== "string") {
|
|
173
162
|
throw new Error(exports.QueryError.INVALID_FROM_DATE_TYPE(fromDate));
|
|
@@ -176,29 +165,33 @@ function extractDateConditions(fromDate, toDate) {
|
|
|
176
165
|
throw new Error(exports.QueryError.INVALID_TO_DATE_TYPE(toDate));
|
|
177
166
|
}
|
|
178
167
|
return [
|
|
179
|
-
|
|
180
|
-
|
|
168
|
+
`segments.date >= "${fromDate}"`,
|
|
169
|
+
`segments.date <= "${toDate}"`,
|
|
181
170
|
];
|
|
182
171
|
}
|
|
183
172
|
exports.extractDateConditions = extractDateConditions;
|
|
184
173
|
function buildWhereClause(constraints, dateConstant, fromDate, toDate) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
174
|
+
const constraintClauses = extractConstraintConditions(constraints);
|
|
175
|
+
const dateConstantClauses = extractDateConstantConditions(dateConstant);
|
|
176
|
+
const dateClauses = extractDateConditions(fromDate, toDate);
|
|
177
|
+
const whereClauses = [
|
|
178
|
+
...constraintClauses,
|
|
179
|
+
...dateConstantClauses,
|
|
180
|
+
...dateClauses,
|
|
181
|
+
].join(` ${QueryKeywords.AND} `);
|
|
189
182
|
return whereClauses.length
|
|
190
|
-
?
|
|
191
|
-
:
|
|
183
|
+
? ` ${QueryKeywords.WHERE} ${whereClauses}`
|
|
184
|
+
: ``;
|
|
192
185
|
}
|
|
193
186
|
exports.buildWhereClause = buildWhereClause;
|
|
194
187
|
function buildLimitClause(limit) {
|
|
195
188
|
if (typeof limit === "undefined") {
|
|
196
|
-
return
|
|
189
|
+
return ``;
|
|
197
190
|
}
|
|
198
191
|
if (typeof limit !== "number" || limit < 1 || !Number.isInteger(limit)) {
|
|
199
192
|
throw new Error(exports.QueryError.INVALID_LIMIT);
|
|
200
193
|
}
|
|
201
|
-
return
|
|
194
|
+
return ` ${QueryKeywords.LIMIT} ${limit}`;
|
|
202
195
|
}
|
|
203
196
|
exports.buildLimitClause = buildLimitClause;
|
|
204
197
|
function completeOrderly(orderly, entity) {
|
|
@@ -211,7 +204,7 @@ function completeOrderly(orderly, entity) {
|
|
|
211
204
|
}
|
|
212
205
|
else if (new RegExp(/^[^.\s]+$/g).test(orderly)) {
|
|
213
206
|
// text without a full stop (e.g. resource_name)
|
|
214
|
-
return entity
|
|
207
|
+
return `${entity}.${orderly}`;
|
|
215
208
|
}
|
|
216
209
|
else {
|
|
217
210
|
throw new Error(exports.QueryError.INVALID_ORDERLY);
|
|
@@ -220,7 +213,7 @@ function completeOrderly(orderly, entity) {
|
|
|
220
213
|
exports.completeOrderly = completeOrderly;
|
|
221
214
|
function buildOrderClauseOld(orderBy, sortOrder, entity) {
|
|
222
215
|
if (typeof orderBy === "undefined") {
|
|
223
|
-
return
|
|
216
|
+
return ``;
|
|
224
217
|
}
|
|
225
218
|
else if (typeof sortOrder === "undefined") {
|
|
226
219
|
sortOrder = "DESC";
|
|
@@ -229,8 +222,8 @@ function buildOrderClauseOld(orderBy, sortOrder, entity) {
|
|
|
229
222
|
throw new Error(exports.QueryError.INVALID_SORT_ORDER);
|
|
230
223
|
}
|
|
231
224
|
if (Array.isArray(orderBy)) {
|
|
232
|
-
|
|
233
|
-
.map(
|
|
225
|
+
const orders = orderBy
|
|
226
|
+
.map((orderly) => {
|
|
234
227
|
if (typeof orderly !== "string") {
|
|
235
228
|
throw new Error(exports.QueryError.INVALID_ORDERLY);
|
|
236
229
|
}
|
|
@@ -239,10 +232,10 @@ function buildOrderClauseOld(orderBy, sortOrder, entity) {
|
|
|
239
232
|
}
|
|
240
233
|
})
|
|
241
234
|
.join(", ");
|
|
242
|
-
return
|
|
235
|
+
return ` ${QueryKeywords.ORDER_BY} ${orders} ${sortOrder}`;
|
|
243
236
|
}
|
|
244
237
|
else if (typeof orderBy === "string") {
|
|
245
|
-
return
|
|
238
|
+
return ` ${QueryKeywords.ORDER_BY} ${completeOrderly(orderBy, entity)} ${sortOrder}`;
|
|
246
239
|
}
|
|
247
240
|
else {
|
|
248
241
|
throw new Error(exports.QueryError.INVALID_ORDERBY);
|
|
@@ -256,14 +249,14 @@ function buildOrderClauseNew(order, entity) {
|
|
|
256
249
|
if (!order.length) {
|
|
257
250
|
return "";
|
|
258
251
|
}
|
|
259
|
-
|
|
260
|
-
.map(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return orderly
|
|
252
|
+
const orders = order
|
|
253
|
+
.map((o) => {
|
|
254
|
+
const orderly = completeOrderly(o.field, entity);
|
|
255
|
+
const sortOrder = o.sort_order ? o.sort_order : "DESC";
|
|
256
|
+
return `${orderly} ${sortOrder}`;
|
|
264
257
|
})
|
|
265
258
|
.join(", ");
|
|
266
|
-
return
|
|
259
|
+
return ` ${QueryKeywords.ORDER_BY} ${orders}`;
|
|
267
260
|
}
|
|
268
261
|
exports.buildOrderClauseNew = buildOrderClauseNew;
|
|
269
262
|
function buildOrderClause(order, orderBy, sortOrder, entity) {
|
|
@@ -276,20 +269,20 @@ function buildOrderClause(order, orderBy, sortOrder, entity) {
|
|
|
276
269
|
}
|
|
277
270
|
exports.buildOrderClause = buildOrderClause;
|
|
278
271
|
function buildRequestOptions(reportOptions) {
|
|
279
|
-
|
|
280
|
-
return { page_size
|
|
272
|
+
const { page_size, page_token, validate_only, summary_row_setting } = reportOptions;
|
|
273
|
+
return { page_size, page_token, validate_only, summary_row_setting };
|
|
281
274
|
}
|
|
282
275
|
exports.buildRequestOptions = buildRequestOptions;
|
|
283
276
|
function buildQuery(reportOptions) {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
277
|
+
const SELECT = buildSelectClause(reportOptions.attributes, reportOptions.metrics, reportOptions.segments);
|
|
278
|
+
const FROM = buildFromClause(reportOptions.entity);
|
|
279
|
+
const WHERE = buildWhereClause(reportOptions.constraints, reportOptions.date_constant, reportOptions.from_date, reportOptions.to_date);
|
|
280
|
+
const LIMIT = buildLimitClause(reportOptions.limit);
|
|
281
|
+
const ORDER = buildOrderClause(reportOptions.order, reportOptions.order_by, reportOptions.sort_order, reportOptions.entity);
|
|
282
|
+
const requestOptions = buildRequestOptions(reportOptions);
|
|
290
283
|
return {
|
|
291
|
-
gaqlQuery:
|
|
292
|
-
requestOptions
|
|
284
|
+
gaqlQuery: `${SELECT}${FROM}${WHERE}${ORDER}${LIMIT}`,
|
|
285
|
+
requestOptions,
|
|
293
286
|
};
|
|
294
287
|
}
|
|
295
288
|
exports.buildQuery = buildQuery;
|
package/build/src/service.js
CHANGED
|
@@ -1,148 +1,140 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.Service = exports.FAILURE_KEY = void 0;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
const google_gax_1 = require("google-gax");
|
|
5
|
+
const google_auth_library_1 = require("google-auth-library");
|
|
6
|
+
const protos_1 = require("./protos");
|
|
7
|
+
const utils_1 = require("./utils");
|
|
8
|
+
const version_1 = require("./version");
|
|
20
9
|
// Make sure to update this version number when upgrading
|
|
21
|
-
exports.FAILURE_KEY =
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
exports.FAILURE_KEY = `google.ads.googleads.${version_1.googleAdsVersion}.errors.googleadsfailure-bin`;
|
|
11
|
+
class Service {
|
|
12
|
+
constructor(clientOptions, customerOptions, hooks) {
|
|
24
13
|
this.clientOptions = clientOptions;
|
|
25
14
|
this.customerOptions = customerOptions;
|
|
26
15
|
this.hooks = hooks !== null && hooks !== void 0 ? hooks : {};
|
|
27
16
|
// @ts-expect-error All fields don't need to be set here
|
|
28
17
|
this.serviceCache = {};
|
|
29
18
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
54
|
-
enumerable: false,
|
|
55
|
-
configurable: true
|
|
56
|
-
});
|
|
57
|
-
Service.prototype.getCredentials = function () {
|
|
58
|
-
var sslCreds = google_gax_1.grpc.credentials.createSsl();
|
|
59
|
-
var authClient = new google_auth_library_1.UserRefreshClient(this.clientOptions.client_id, this.clientOptions.client_secret, this.customerOptions.refresh_token);
|
|
60
|
-
var credentials = google_gax_1.grpc.credentials.combineChannelCredentials(sslCreds, google_gax_1.grpc.credentials.createFromGoogleCredential(authClient));
|
|
19
|
+
get credentials() {
|
|
20
|
+
return {
|
|
21
|
+
customer_id: this.customerOptions.customer_id,
|
|
22
|
+
login_customer_id: this.customerOptions.login_customer_id,
|
|
23
|
+
linked_customer_id: this.customerOptions.linked_customer_id,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
get callHeaders() {
|
|
27
|
+
const headers = {
|
|
28
|
+
"developer-token": this.clientOptions.developer_token,
|
|
29
|
+
};
|
|
30
|
+
if (this.customerOptions.login_customer_id) {
|
|
31
|
+
headers["login-customer-id"] = this.customerOptions.login_customer_id;
|
|
32
|
+
}
|
|
33
|
+
if (this.customerOptions.linked_customer_id) {
|
|
34
|
+
headers["linked-customer-id"] = this.customerOptions.linked_customer_id;
|
|
35
|
+
}
|
|
36
|
+
return headers;
|
|
37
|
+
}
|
|
38
|
+
getCredentials() {
|
|
39
|
+
const sslCreds = google_gax_1.grpc.credentials.createSsl();
|
|
40
|
+
const authClient = new google_auth_library_1.UserRefreshClient(this.clientOptions.client_id, this.clientOptions.client_secret, this.customerOptions.refresh_token);
|
|
41
|
+
const credentials = google_gax_1.grpc.credentials.combineChannelCredentials(sslCreds, google_gax_1.grpc.credentials.createFromGoogleCredential(authClient));
|
|
61
42
|
return credentials;
|
|
62
|
-
}
|
|
63
|
-
|
|
43
|
+
}
|
|
44
|
+
loadService(service) {
|
|
64
45
|
if (this.serviceCache[service]) {
|
|
65
46
|
return this.serviceCache[service];
|
|
66
47
|
}
|
|
67
48
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
68
|
-
|
|
49
|
+
const { [service]: protoService } = require("google-ads-node");
|
|
69
50
|
if (typeof protoService === "undefined") {
|
|
70
|
-
throw new Error(
|
|
51
|
+
throw new Error(`Service "${service}" could not be found`);
|
|
71
52
|
}
|
|
72
|
-
|
|
53
|
+
const client = new protoService({
|
|
73
54
|
sslCreds: this.getCredentials(),
|
|
74
55
|
});
|
|
75
56
|
this.serviceCache[service] = client;
|
|
76
57
|
return client;
|
|
77
|
-
}
|
|
78
|
-
|
|
58
|
+
}
|
|
59
|
+
getGoogleAdsError(error) {
|
|
79
60
|
var _a;
|
|
80
61
|
// @ts-expect-error No type exists for GA query error
|
|
81
62
|
if (typeof ((_a = error === null || error === void 0 ? void 0 : error.metadata) === null || _a === void 0 ? void 0 : _a.internalRepr.get(exports.FAILURE_KEY)) === "undefined") {
|
|
82
63
|
return error;
|
|
83
64
|
}
|
|
84
65
|
// @ts-expect-error No type exists for GA query error
|
|
85
|
-
|
|
66
|
+
const [buffer] = error.metadata.internalRepr.get(exports.FAILURE_KEY);
|
|
86
67
|
return this.decodeGoogleAdsFailureBuffer(buffer);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
68
|
+
}
|
|
69
|
+
decodeGoogleAdsFailureBuffer(buffer) {
|
|
70
|
+
const googleAdsFailure = protos_1.errors.GoogleAdsFailure.decode(buffer);
|
|
90
71
|
return googleAdsFailure;
|
|
91
|
-
}
|
|
92
|
-
|
|
72
|
+
}
|
|
73
|
+
decodePartialFailureError(response) {
|
|
93
74
|
var _a, _b, _c;
|
|
94
75
|
if (typeof ((_a = response) === null || _a === void 0 ? void 0 : _a.partial_failure_error) === "undefined" ||
|
|
95
76
|
!((_b = response) === null || _b === void 0 ? void 0 : _b.partial_failure_error)) {
|
|
96
77
|
return response;
|
|
97
78
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return d.type_url.includes("errors.GoogleAdsFailure");
|
|
101
|
-
})) === null || _c === void 0 ? void 0 : _c.value;
|
|
79
|
+
const { details } = response.partial_failure_error;
|
|
80
|
+
const buffer = (_c = details === null || details === void 0 ? void 0 : details.find((d) => d.type_url.includes("errors.GoogleAdsFailure"))) === null || _c === void 0 ? void 0 : _c.value;
|
|
102
81
|
if (typeof buffer === "undefined") {
|
|
103
82
|
return response;
|
|
104
83
|
}
|
|
105
84
|
// Update the partial failure field with the decoded error details
|
|
106
|
-
return
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
85
|
+
return {
|
|
86
|
+
...response,
|
|
87
|
+
partial_failure_error: this.decodeGoogleAdsFailureBuffer(buffer),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
buildSearchRequestAndService(gaql, options) {
|
|
91
|
+
const service = this.loadService("GoogleAdsServiceClient");
|
|
92
|
+
const request = new protos_1.services.SearchGoogleAdsRequest({
|
|
93
|
+
customer_id: this.customerOptions.customer_id,
|
|
94
|
+
query: gaql,
|
|
95
|
+
...options,
|
|
96
|
+
});
|
|
97
|
+
return { service, request };
|
|
98
|
+
}
|
|
99
|
+
buildSearchStreamRequestAndService(gaql, options) {
|
|
100
|
+
const service = this.loadService("GoogleAdsServiceClient");
|
|
101
|
+
const request = new protos_1.services.SearchGoogleAdsStreamRequest({
|
|
102
|
+
customer_id: this.customerOptions.customer_id,
|
|
103
|
+
query: gaql,
|
|
104
|
+
...options,
|
|
105
|
+
});
|
|
106
|
+
return { service, request };
|
|
107
|
+
}
|
|
108
|
+
buildMutationRequestAndService(mutations, options) {
|
|
109
|
+
const service = this.loadService("GoogleAdsServiceClient");
|
|
110
|
+
const mutateOperations = mutations.map((mutation) => {
|
|
111
|
+
var _a;
|
|
112
|
+
const opKey = utils_1.toSnakeCase(`${mutation.entity}Operation`);
|
|
113
|
+
const operation = {
|
|
114
|
+
[(_a = mutation.operation) !== null && _a !== void 0 ? _a : "create"]: mutation.resource,
|
|
115
|
+
};
|
|
127
116
|
if (mutation.operation === "update") {
|
|
128
117
|
// @ts-expect-error Resource operations should have updateMask defined
|
|
129
118
|
operation.update_mask = utils_1.getFieldMask(mutation.resource);
|
|
130
119
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
120
|
+
const mutateOperation = new protos_1.services.MutateOperation({
|
|
121
|
+
[opKey]: operation,
|
|
122
|
+
});
|
|
134
123
|
return mutateOperation;
|
|
135
124
|
});
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
125
|
+
const request = new protos_1.services.MutateGoogleAdsRequest({
|
|
126
|
+
customer_id: this.customerOptions.customer_id,
|
|
127
|
+
mutate_operations: mutateOperations,
|
|
128
|
+
...options,
|
|
129
|
+
});
|
|
130
|
+
return { service, request };
|
|
131
|
+
}
|
|
132
|
+
buildOperations(type, entities, message) {
|
|
133
|
+
const ops = entities.map((e) => {
|
|
134
|
+
const op = {
|
|
135
|
+
[type]: e,
|
|
136
|
+
operation: type,
|
|
137
|
+
};
|
|
146
138
|
if (type === "update") {
|
|
147
139
|
// @ts-expect-error Field required for updates
|
|
148
140
|
op.update_mask = utils_1.getFieldMask(
|
|
@@ -154,11 +146,14 @@ var Service = /** @class */ (function () {
|
|
|
154
146
|
return op;
|
|
155
147
|
});
|
|
156
148
|
return ops;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
149
|
+
}
|
|
150
|
+
buildRequest(operations, options) {
|
|
151
|
+
const request = {
|
|
152
|
+
customer_id: this.customerOptions.customer_id,
|
|
153
|
+
operations,
|
|
154
|
+
...options,
|
|
155
|
+
};
|
|
160
156
|
return request;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
}());
|
|
157
|
+
}
|
|
158
|
+
}
|
|
164
159
|
exports.Service = Service;
|