google-ads-api 10.0.0 → 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.
@@ -1,151 +1,62 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- var __assign = (this && this.__assign) || function () {
16
- __assign = Object.assign || function(t) {
17
- for (var s, i = 1, n = arguments.length; i < n; i++) {
18
- s = arguments[i];
19
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
- t[p] = s[p];
21
- }
22
- return t;
23
- };
24
- return __assign.apply(this, arguments);
25
- };
26
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
- return new (P || (P = Promise))(function (resolve, reject) {
29
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
- step((generator = generator.apply(thisArg, _arguments || [])).next());
33
- });
34
- };
35
- var __generator = (this && this.__generator) || function (thisArg, body) {
36
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
37
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
38
- function verb(n) { return function (v) { return step([n, v]); }; }
39
- function step(op) {
40
- if (f) throw new TypeError("Generator is already executing.");
41
- while (_) try {
42
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
43
- if (y = 0, t) op = [op[0] & 2, t.value];
44
- switch (op[0]) {
45
- case 0: case 1: t = op; break;
46
- case 4: _.label++; return { value: op[1], done: false };
47
- case 5: _.label++; y = op[1]; op = [0]; continue;
48
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
49
- default:
50
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
51
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
52
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
53
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
54
- if (t[2]) _.ops.pop();
55
- _.trys.pop(); continue;
56
- }
57
- op = body.call(thisArg, _);
58
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
59
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
60
- }
61
- };
62
- var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
63
- var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
64
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
65
- var g = generator.apply(thisArg, _arguments || []), i, q = [];
66
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
67
- function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
68
- function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
69
- function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
70
- function fulfill(value) { resume("next", value); }
71
- function reject(value) { resume("throw", value); }
72
- function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
73
- };
74
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
75
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
76
4
  };
77
5
  Object.defineProperty(exports, "__esModule", { value: true });
78
6
  exports.Customer = void 0;
79
- var parser_1 = require("./parser");
80
- var serviceFactory_1 = __importDefault(require("./protos/autogen/serviceFactory"));
81
- var query_1 = require("./query");
82
- var utils_1 = require("./utils");
83
- var Customer = /** @class */ (function (_super) {
84
- __extends(Customer, _super);
85
- function Customer(clientOptions, customerOptions, hooks) {
86
- return _super.call(this, clientOptions, customerOptions, hooks !== null && hooks !== void 0 ? hooks : {}) || this;
7
+ const parser_1 = require("./parser");
8
+ const serviceFactory_1 = __importDefault(require("./protos/autogen/serviceFactory"));
9
+ const query_1 = require("./query");
10
+ const utils_1 = require("./utils");
11
+ class Customer extends serviceFactory_1.default {
12
+ constructor(clientOptions, customerOptions, hooks) {
13
+ super(clientOptions, customerOptions, hooks !== null && hooks !== void 0 ? hooks : {});
87
14
  }
88
15
  /**
89
16
  @description Single query using a raw GAQL string.
90
17
  @hooks onQueryStart, onQueryError, onQueryEnd
91
18
  */
92
- Customer.prototype.query = function (gaqlQuery, requestOptions) {
93
- if (requestOptions === void 0) { requestOptions = {}; }
94
- return __awaiter(this, void 0, void 0, function () {
95
- var response;
96
- return __generator(this, function (_a) {
97
- switch (_a.label) {
98
- case 0: return [4 /*yield*/, this.querier(gaqlQuery, requestOptions)];
99
- case 1:
100
- response = (_a.sent()).response;
101
- return [2 /*return*/, response];
102
- }
103
- });
104
- });
105
- };
19
+ async query(gaqlQuery, requestOptions = {}) {
20
+ const { response } = await this.querier(gaqlQuery, requestOptions);
21
+ return response;
22
+ }
23
+ /**
24
+ @description Stream query using a raw GAQL string. If a generic type is provided, it must be the type of a single row.
25
+ If a summary row is requested then this will be the last emitted row of the stream.
26
+ @hooks onStreamStart, onStreamError
27
+ @example
28
+ const stream = queryStream<T>(gaqlQuery)
29
+ for await (const row of stream) { ... }
30
+ */
31
+ async *queryStream(gaqlQuery, requestOptions = {}) {
32
+ const stream = this.streamer(gaqlQuery, requestOptions);
33
+ for await (const row of stream) {
34
+ yield row;
35
+ }
36
+ }
106
37
  /**
107
38
  @description Single query using ReportOptions.
108
39
  If a summary row is requested then this will be the first row of the results.
109
40
  @hooks onQueryStart, onQueryError, onQueryEnd
110
41
  */
111
- Customer.prototype.report = function (options) {
112
- return __awaiter(this, void 0, void 0, function () {
113
- var _a, gaqlQuery, requestOptions, response;
114
- return __generator(this, function (_b) {
115
- switch (_b.label) {
116
- case 0:
117
- _a = query_1.buildQuery(options), gaqlQuery = _a.gaqlQuery, requestOptions = _a.requestOptions;
118
- return [4 /*yield*/, this.querier(gaqlQuery, requestOptions, options)];
119
- case 1:
120
- response = (_b.sent()).response;
121
- return [2 /*return*/, response];
122
- }
123
- });
124
- });
125
- };
42
+ async report(options) {
43
+ const { gaqlQuery, requestOptions } = query_1.buildQuery(options);
44
+ const { response } = await this.querier(gaqlQuery, requestOptions, options);
45
+ return response;
46
+ }
126
47
  /**
127
48
  @description Get the total row count of a report.
128
49
  @hooks none
129
50
  */
130
- Customer.prototype.reportCount = function (options) {
131
- return __awaiter(this, void 0, void 0, function () {
132
- var _a, gaqlQuery, requestOptions, useHooks, totalResultsCount;
133
- return __generator(this, function (_b) {
134
- switch (_b.label) {
135
- case 0:
136
- options.limit = 1; // must get at least one row
137
- _a = query_1.buildQuery(options), gaqlQuery = _a.gaqlQuery, requestOptions = _a.requestOptions;
138
- // @ts-expect-error we do not allow this field in reportOptions, however it is still a valid request option
139
- requestOptions.return_total_results_count = true;
140
- useHooks = false;
141
- return [4 /*yield*/, this.querier(gaqlQuery, requestOptions, options, useHooks)];
142
- case 1:
143
- totalResultsCount = (_b.sent()).totalResultsCount;
144
- return [2 /*return*/, totalResultsCount];
145
- }
146
- });
147
- });
148
- };
51
+ async reportCount(options) {
52
+ options.limit = 1; // must get at least one row
53
+ const { gaqlQuery, requestOptions } = query_1.buildQuery(options);
54
+ // @ts-expect-error we do not allow this field in reportOptions, however it is still a valid request option
55
+ requestOptions.return_total_results_count = true;
56
+ const useHooks = false; // to avoid cacheing conflicts
57
+ const { totalResultsCount } = await this.querier(gaqlQuery, requestOptions, options, useHooks);
58
+ return totalResultsCount;
59
+ }
149
60
  /**
150
61
  @description Stream query using ReportOptions. If a generic type is provided, it must be the type of a single row.
151
62
  If a summary row is requested then this will be the last emitted row of the stream.
@@ -154,98 +65,13 @@ var Customer = /** @class */ (function (_super) {
154
65
  const stream = reportStream<T>(reportOptions)
155
66
  for await (const row of stream) { ... }
156
67
  */
157
- Customer.prototype.reportStream = function (reportOptions) {
158
- return __asyncGenerator(this, arguments, function reportStream_1() {
159
- var _a, gaqlQuery, requestOptions, baseHookArguments, queryStart_1, _b, service, request, stream, streamFinished, accumulator, nextChunk, item, searchError_1, googleAdsError;
160
- var _this = this;
161
- return __generator(this, function (_c) {
162
- switch (_c.label) {
163
- case 0:
164
- _a = query_1.buildQuery(reportOptions), gaqlQuery = _a.gaqlQuery, requestOptions = _a.requestOptions;
165
- baseHookArguments = {
166
- credentials: this.credentials,
167
- query: gaqlQuery,
168
- reportOptions: reportOptions,
169
- };
170
- if (!this.hooks.onStreamStart) return [3 /*break*/, 3];
171
- queryStart_1 = { cancelled: false };
172
- return [4 /*yield*/, __await(this.hooks.onStreamStart(__assign(__assign({}, baseHookArguments), { cancel: function () {
173
- queryStart_1.cancelled = true;
174
- }, editOptions: function (options) {
175
- Object.entries(options).forEach(function (_a) {
176
- var key = _a[0], val = _a[1];
177
- // @ts-expect-error
178
- requestOptions[key] = val;
179
- });
180
- } })))];
181
- case 1:
182
- _c.sent();
183
- if (!queryStart_1.cancelled) return [3 /*break*/, 3];
184
- return [4 /*yield*/, __await(void 0)];
185
- case 2: return [2 /*return*/, _c.sent()];
186
- case 3:
187
- _b = this.buildSearchStreamRequestAndService(gaqlQuery, requestOptions), service = _b.service, request = _b.request;
188
- stream = service.searchStream(request, {
189
- otherArgs: { headers: this.callHeaders },
190
- });
191
- streamFinished = false;
192
- accumulator = [];
193
- nextChunk = utils_1.createNextChunkArrivedPromise();
194
- stream.on("data", function (chunk) {
195
- var results = chunk.summary_row ? [chunk.summary_row] : chunk.results;
196
- var parsedResponse = _this.clientOptions.disable_parsing
197
- ? results
198
- : parser_1.parse({ results: results, reportOptions: reportOptions });
199
- accumulator.push.apply(accumulator, parsedResponse);
200
- nextChunk.resolve();
201
- nextChunk = utils_1.createNextChunkArrivedPromise();
202
- });
203
- stream.on("error", function (searchError) {
204
- nextChunk.reject(searchError);
205
- });
206
- stream.on("end", function () {
207
- streamFinished = true;
208
- nextChunk.resolve();
209
- });
210
- _c.label = 4;
211
- case 4:
212
- _c.trys.push([4, 12, 15, 16]);
213
- _c.label = 5;
214
- case 5:
215
- if (!(!streamFinished || accumulator.length)) return [3 /*break*/, 11];
216
- if (!(accumulator.length > 0)) return [3 /*break*/, 8];
217
- item = accumulator.shift();
218
- if (item === undefined) {
219
- throw new Error("UNDEFINED_STREAM_ERROR");
220
- }
221
- return [4 /*yield*/, __await(item)];
222
- case 6: return [4 /*yield*/, _c.sent()];
223
- case 7:
224
- _c.sent();
225
- return [3 /*break*/, 10];
226
- case 8: return [4 /*yield*/, __await(nextChunk.newPromise)];
227
- case 9:
228
- _c.sent();
229
- _c.label = 10;
230
- case 10: return [3 /*break*/, 5];
231
- case 11: return [3 /*break*/, 16];
232
- case 12:
233
- searchError_1 = _c.sent();
234
- googleAdsError = this.getGoogleAdsError(searchError_1);
235
- if (!this.hooks.onStreamError) return [3 /*break*/, 14];
236
- return [4 /*yield*/, __await(this.hooks.onStreamError(__assign(__assign({}, baseHookArguments), { error: googleAdsError })))];
237
- case 13:
238
- _c.sent();
239
- _c.label = 14;
240
- case 14: throw googleAdsError;
241
- case 15:
242
- stream.destroy();
243
- return [7 /*endfinally*/];
244
- case 16: return [2 /*return*/];
245
- }
246
- });
247
- });
248
- };
68
+ async *reportStream(reportOptions) {
69
+ const { gaqlQuery, requestOptions } = query_1.buildQuery(reportOptions);
70
+ const stream = this.streamer(gaqlQuery, requestOptions, reportOptions);
71
+ for await (const row of stream) {
72
+ yield row;
73
+ }
74
+ }
249
75
  /**
250
76
  @description Retreive the raw stream using ReportOptions.
251
77
  @hooks onStreamStart
@@ -255,270 +81,285 @@ var Customer = /** @class */ (function (_super) {
255
81
  stream.on('error', (error) => { ... })
256
82
  stream.on('end', () => { ... })
257
83
  */
258
- Customer.prototype.reportStreamRaw = function (reportOptions) {
259
- return __awaiter(this, void 0, void 0, function () {
260
- var _a, gaqlQuery, requestOptions, baseHookArguments, queryStart, _b, service, request;
261
- return __generator(this, function (_c) {
262
- switch (_c.label) {
263
- case 0:
264
- _a = query_1.buildQuery(reportOptions), gaqlQuery = _a.gaqlQuery, requestOptions = _a.requestOptions;
265
- baseHookArguments = {
266
- credentials: this.credentials,
267
- query: gaqlQuery,
268
- reportOptions: reportOptions,
269
- };
270
- queryStart = { cancelled: false };
271
- if (!this.hooks.onStreamStart) return [3 /*break*/, 2];
272
- return [4 /*yield*/, this.hooks.onStreamStart(__assign(__assign({}, baseHookArguments), { cancel: function () {
273
- queryStart.cancelled = true;
274
- }, editOptions: function (options) {
275
- Object.entries(options).forEach(function (_a) {
276
- var key = _a[0], val = _a[1];
277
- // @ts-ignore
278
- requestOptions[key] = val;
279
- });
280
- } }))];
281
- case 1:
282
- _c.sent();
283
- if (queryStart.cancelled) {
284
- return [2 /*return*/];
285
- }
286
- _c.label = 2;
287
- case 2:
288
- _b = this.buildSearchStreamRequestAndService(gaqlQuery, requestOptions), service = _b.service, request = _b.request;
289
- return [2 /*return*/, service.searchStream(request, {
290
- otherArgs: { headers: this.callHeaders },
291
- })];
292
- }
84
+ async reportStreamRaw(reportOptions) {
85
+ const { gaqlQuery, requestOptions } = query_1.buildQuery(reportOptions);
86
+ const baseHookArguments = {
87
+ credentials: this.credentials,
88
+ query: gaqlQuery,
89
+ reportOptions,
90
+ };
91
+ const queryStart = { cancelled: false };
92
+ if (this.hooks.onStreamStart) {
93
+ await this.hooks.onStreamStart({
94
+ ...baseHookArguments,
95
+ cancel: () => {
96
+ queryStart.cancelled = true;
97
+ },
98
+ editOptions: (options) => {
99
+ Object.entries(options).forEach(([key, val]) => {
100
+ // @ts-ignore
101
+ requestOptions[key] = val;
102
+ });
103
+ },
293
104
  });
105
+ if (queryStart.cancelled) {
106
+ return;
107
+ }
108
+ }
109
+ const { service, request } = this.buildSearchStreamRequestAndService(gaqlQuery, requestOptions);
110
+ return service.searchStream(request, {
111
+ otherArgs: { headers: this.callHeaders },
294
112
  });
295
- };
296
- Customer.prototype.search = function (gaqlQuery, requestOptions) {
297
- return __awaiter(this, void 0, void 0, function () {
298
- var _a, service, request, searchResponse, response, summaryRow, nextPageToken, totalResultsCount;
299
- return __generator(this, function (_b) {
300
- switch (_b.label) {
301
- case 0:
302
- _a = this.buildSearchRequestAndService(gaqlQuery, requestOptions), service = _a.service, request = _a.request;
303
- return [4 /*yield*/, service.search(request, {
304
- otherArgs: { headers: this.callHeaders },
305
- autoPaginate: false,
306
- })];
307
- case 1:
308
- searchResponse = _b.sent();
309
- response = searchResponse[0];
310
- summaryRow = searchResponse[2].summary_row;
311
- nextPageToken = searchResponse[2].next_page_token;
312
- totalResultsCount = searchResponse[2].total_results_count
313
- ? +searchResponse[2].total_results_count
314
- : undefined;
315
- if (summaryRow) {
316
- response.unshift(summaryRow);
317
- }
318
- return [2 /*return*/, { response: response, nextPageToken: nextPageToken, totalResultsCount: totalResultsCount }];
319
- }
320
- });
113
+ }
114
+ async search(gaqlQuery, requestOptions) {
115
+ const { service, request } = this.buildSearchRequestAndService(gaqlQuery, requestOptions);
116
+ const searchResponse = await service.search(request, {
117
+ otherArgs: { headers: this.callHeaders },
118
+ autoPaginate: false,
321
119
  });
322
- };
323
- Customer.prototype.paginatedSearch = function (gaqlQuery, requestOptions, parser) {
324
- return __awaiter(this, void 0, void 0, function () {
325
- var response, nextPageToken, initialSearch, totalResultsCount, nextSearch;
326
- return __generator(this, function (_a) {
327
- switch (_a.label) {
328
- case 0:
329
- response = [];
330
- nextPageToken = undefined;
331
- return [4 /*yield*/, this.search(gaqlQuery, requestOptions)];
332
- case 1:
333
- initialSearch = _a.sent();
334
- totalResultsCount = initialSearch.totalResultsCount;
335
- response.push.apply(response, parser(initialSearch.response));
336
- nextPageToken = initialSearch.nextPageToken;
337
- _a.label = 2;
338
- case 2:
339
- if (!nextPageToken) return [3 /*break*/, 4];
340
- return [4 /*yield*/, this.search(gaqlQuery, __assign(__assign({}, requestOptions), { page_token: nextPageToken }))];
341
- case 3:
342
- nextSearch = _a.sent();
343
- response.push.apply(response, parser(nextSearch.response));
344
- nextPageToken = nextSearch.nextPageToken;
345
- return [3 /*break*/, 2];
346
- case 4: return [2 /*return*/, { response: response, totalResultsCount: totalResultsCount }];
347
- }
120
+ const response = searchResponse[0];
121
+ const summaryRow = searchResponse[2].summary_row;
122
+ const nextPageToken = searchResponse[2].next_page_token;
123
+ const totalResultsCount = searchResponse[2].total_results_count
124
+ ? +searchResponse[2].total_results_count
125
+ : undefined;
126
+ if (summaryRow) {
127
+ response.unshift(summaryRow);
128
+ }
129
+ return { response, nextPageToken, totalResultsCount };
130
+ }
131
+ async paginatedSearch(gaqlQuery, requestOptions, parser) {
132
+ const response = [];
133
+ let nextPageToken = undefined;
134
+ const initialSearch = await this.search(gaqlQuery, requestOptions);
135
+ const totalResultsCount = initialSearch.totalResultsCount;
136
+ response.push(...parser(initialSearch.response));
137
+ nextPageToken = initialSearch.nextPageToken;
138
+ while (nextPageToken) {
139
+ const nextSearch = await this.search(gaqlQuery, {
140
+ ...requestOptions,
141
+ page_token: nextPageToken,
348
142
  });
349
- });
350
- };
351
- Customer.prototype.querier = function (gaqlQuery, requestOptions, reportOptions, useHooks) {
352
- if (requestOptions === void 0) { requestOptions = {}; }
353
- if (useHooks === void 0) { useHooks = true; }
354
- return __awaiter(this, void 0, void 0, function () {
355
- var baseHookArguments, queryCancellation_1, parsingWapper, _a, response, totalResultsCount, queryResolution_1, searchError_2, googleAdsError;
356
- var _this = this;
357
- return __generator(this, function (_b) {
358
- switch (_b.label) {
359
- case 0:
360
- baseHookArguments = {
361
- credentials: this.credentials,
362
- query: gaqlQuery,
363
- reportOptions: reportOptions,
364
- };
365
- if (!(this.hooks.onQueryStart && useHooks)) return [3 /*break*/, 2];
366
- queryCancellation_1 = { cancelled: false };
367
- return [4 /*yield*/, this.hooks.onQueryStart(__assign(__assign({}, baseHookArguments), { cancel: function (res) {
368
- queryCancellation_1.cancelled = true;
369
- queryCancellation_1.res = res;
370
- }, editOptions: function (options) {
371
- Object.entries(options).forEach(function (_a) {
372
- var key = _a[0], val = _a[1];
373
- // @ts-ignore
374
- requestOptions[key] = val;
375
- });
376
- } }))];
377
- case 1:
378
- _b.sent();
379
- if (queryCancellation_1.cancelled) {
380
- return [2 /*return*/, { response: queryCancellation_1.res }];
381
- }
382
- _b.label = 2;
383
- case 2:
384
- _b.trys.push([2, 6, , 9]);
385
- parsingWapper = function (rows) {
386
- return _this.clientOptions.disable_parsing
387
- ? rows
388
- : reportOptions
389
- ? parser_1.parse({ results: rows, reportOptions: reportOptions })
390
- : parser_1.parse({ results: rows, gaqlString: gaqlQuery });
391
- };
392
- return [4 /*yield*/, this.paginatedSearch(gaqlQuery, requestOptions, parsingWapper)];
393
- case 3:
394
- _a = _b.sent(), response = _a.response, totalResultsCount = _a.totalResultsCount;
395
- if (!(this.hooks.onQueryEnd && useHooks)) return [3 /*break*/, 5];
396
- queryResolution_1 = { resolved: false };
397
- return [4 /*yield*/, this.hooks.onQueryEnd(__assign(__assign({}, baseHookArguments), { response: response, resolve: function (res) {
398
- queryResolution_1.resolved = true;
399
- queryResolution_1.res = res;
400
- } }))];
401
- case 4:
402
- _b.sent();
403
- if (queryResolution_1.resolved) {
404
- return [2 /*return*/, { response: queryResolution_1.res, totalResultsCount: totalResultsCount }];
405
- }
406
- _b.label = 5;
407
- case 5: return [2 /*return*/, { response: response, totalResultsCount: totalResultsCount }];
408
- case 6:
409
- searchError_2 = _b.sent();
410
- googleAdsError = this.getGoogleAdsError(searchError_2);
411
- if (!(this.hooks.onQueryError && useHooks)) return [3 /*break*/, 8];
412
- return [4 /*yield*/, this.hooks.onQueryError(__assign(__assign({}, baseHookArguments), { error: googleAdsError }))];
413
- case 7:
414
- _b.sent();
415
- _b.label = 8;
416
- case 8: throw googleAdsError;
417
- case 9: return [2 /*return*/];
143
+ response.push(...parser(nextSearch.response));
144
+ nextPageToken = nextSearch.nextPageToken;
145
+ }
146
+ return { response, totalResultsCount };
147
+ }
148
+ async querier(gaqlQuery, requestOptions = {}, reportOptions, useHooks = true) {
149
+ const baseHookArguments = {
150
+ credentials: this.credentials,
151
+ query: gaqlQuery,
152
+ reportOptions,
153
+ };
154
+ if (this.hooks.onQueryStart && useHooks) {
155
+ const queryCancellation = { cancelled: false };
156
+ await this.hooks.onQueryStart({
157
+ ...baseHookArguments,
158
+ cancel: (res) => {
159
+ queryCancellation.cancelled = true;
160
+ queryCancellation.res = res;
161
+ },
162
+ editOptions: (options) => {
163
+ Object.entries(options).forEach(([key, val]) => {
164
+ // @ts-ignore
165
+ requestOptions[key] = val;
166
+ });
167
+ },
168
+ });
169
+ if (queryCancellation.cancelled) {
170
+ return { response: queryCancellation.res };
171
+ }
172
+ }
173
+ try {
174
+ const parsingWapper = (rows) => {
175
+ return this.clientOptions.disable_parsing
176
+ ? rows
177
+ : reportOptions
178
+ ? parser_1.parse({ results: rows, reportOptions })
179
+ : parser_1.parse({ results: rows, gaqlString: gaqlQuery });
180
+ };
181
+ const { response, totalResultsCount } = await this.paginatedSearch(gaqlQuery, requestOptions, parsingWapper);
182
+ if (this.hooks.onQueryEnd && useHooks) {
183
+ const queryResolution = { resolved: false };
184
+ await this.hooks.onQueryEnd({
185
+ ...baseHookArguments,
186
+ response,
187
+ resolve: (res) => {
188
+ queryResolution.resolved = true;
189
+ queryResolution.res = res;
190
+ },
191
+ });
192
+ if (queryResolution.resolved) {
193
+ return { response: queryResolution.res, totalResultsCount };
418
194
  }
195
+ }
196
+ return { response: response, totalResultsCount };
197
+ }
198
+ catch (searchError) {
199
+ const googleAdsError = this.getGoogleAdsError(searchError);
200
+ if (this.hooks.onQueryError && useHooks) {
201
+ await this.hooks.onQueryError({
202
+ ...baseHookArguments,
203
+ error: googleAdsError,
204
+ });
205
+ }
206
+ throw googleAdsError;
207
+ }
208
+ }
209
+ async *streamer(gaqlQuery, requestOptions = {}, reportOptions) {
210
+ const baseHookArguments = {
211
+ credentials: this.credentials,
212
+ query: gaqlQuery,
213
+ reportOptions,
214
+ };
215
+ if (this.hooks.onStreamStart) {
216
+ const queryStart = { cancelled: false };
217
+ await this.hooks.onStreamStart({
218
+ ...baseHookArguments,
219
+ cancel: () => {
220
+ queryStart.cancelled = true;
221
+ },
222
+ editOptions: (options) => {
223
+ Object.entries(options).forEach(([key, val]) => {
224
+ // @ts-expect-error
225
+ requestOptions[key] = val;
226
+ });
227
+ },
419
228
  });
229
+ if (queryStart.cancelled) {
230
+ return;
231
+ }
232
+ }
233
+ const { service, request } = this.buildSearchStreamRequestAndService(gaqlQuery, requestOptions);
234
+ const stream = service.searchStream(request, {
235
+ otherArgs: { headers: this.callHeaders },
420
236
  });
421
- };
237
+ let streamFinished = false;
238
+ const accumulator = [];
239
+ let nextChunk = utils_1.createNextChunkArrivedPromise();
240
+ stream.on("data", (chunk) => {
241
+ const results = chunk.summary_row ? [chunk.summary_row] : chunk.results;
242
+ const parsedResponse = this.clientOptions.disable_parsing
243
+ ? results
244
+ : reportOptions
245
+ ? parser_1.parse({ results, reportOptions })
246
+ : parser_1.parse({ results, gaqlString: gaqlQuery });
247
+ accumulator.push(...parsedResponse);
248
+ nextChunk.resolve();
249
+ nextChunk = utils_1.createNextChunkArrivedPromise();
250
+ });
251
+ stream.on("error", (searchError) => {
252
+ nextChunk.reject(searchError);
253
+ });
254
+ stream.on("end", () => {
255
+ streamFinished = true;
256
+ nextChunk.resolve();
257
+ });
258
+ try {
259
+ while (!streamFinished || accumulator.length) {
260
+ if (accumulator.length > 0) {
261
+ const item = accumulator.shift();
262
+ if (item === undefined) {
263
+ throw new Error("UNDEFINED_STREAM_ERROR");
264
+ }
265
+ yield item;
266
+ }
267
+ else {
268
+ await nextChunk.newPromise;
269
+ }
270
+ }
271
+ }
272
+ catch (searchError) {
273
+ const googleAdsError = this.getGoogleAdsError(searchError);
274
+ if (this.hooks.onStreamError) {
275
+ await this.hooks.onStreamError({
276
+ ...baseHookArguments,
277
+ error: googleAdsError,
278
+ });
279
+ }
280
+ throw googleAdsError;
281
+ }
282
+ finally {
283
+ stream.destroy();
284
+ }
285
+ }
422
286
  /**
423
287
  * @description Creates, updates, or removes resources. This method supports atomic transactions
424
288
  * with multiple types of resources. For example, you can atomically create a campaign and a
425
289
  * campaign budget, or perform up to thousands of mutates atomically.
426
290
  * @hooks onMutationStart, onMutationError, onMutationEnd
427
291
  */
428
- Customer.prototype.mutateResources = function (mutations, mutateOptions) {
429
- if (mutateOptions === void 0) { mutateOptions = {}; }
430
- return __awaiter(this, void 0, void 0, function () {
431
- var baseHookArguments, mutationCancellation_1, _a, service, request, response, parsedResponse, mutationResolution_1, mutateError_1, googleAdsError;
432
- return __generator(this, function (_b) {
433
- switch (_b.label) {
434
- case 0:
435
- baseHookArguments = {
436
- credentials: this.credentials,
437
- method: "GoogleAdsService.mutate",
438
- mutations: mutations,
439
- isServiceCall: false,
440
- };
441
- if (!this.hooks.onMutationStart) return [3 /*break*/, 2];
442
- mutationCancellation_1 = { cancelled: false };
443
- return [4 /*yield*/, this.hooks.onMutationStart(__assign(__assign({}, baseHookArguments), { cancel: function (res) {
444
- mutationCancellation_1.cancelled = true;
445
- mutationCancellation_1.res = res;
446
- }, editOptions: function (options) {
447
- Object.entries(options).forEach(function (_a) {
448
- var key = _a[0], val = _a[1];
449
- // @ts-ignore
450
- mutateOptions[key] = val;
451
- });
452
- } }))];
453
- case 1:
454
- _b.sent();
455
- if (mutationCancellation_1.cancelled) {
456
- return [2 /*return*/, mutationCancellation_1.res];
457
- }
458
- _b.label = 2;
459
- case 2:
460
- _a = this.buildMutationRequestAndService(mutations, mutateOptions), service = _a.service, request = _a.request;
461
- _b.label = 3;
462
- case 3:
463
- _b.trys.push([3, 7, , 10]);
464
- return [4 /*yield*/, service.mutate(request, {
465
- otherArgs: { headers: this.callHeaders },
466
- })];
467
- case 4:
468
- response = (_b.sent())[0];
469
- parsedResponse = request.partial_failure
470
- ? this.decodePartialFailureError(response)
471
- : response;
472
- if (!this.hooks.onMutationEnd) return [3 /*break*/, 6];
473
- mutationResolution_1 = { resolved: false };
474
- return [4 /*yield*/, this.hooks.onMutationEnd(__assign(__assign({}, baseHookArguments), { response: parsedResponse, resolve: function (res) {
475
- mutationResolution_1.resolved = true;
476
- mutationResolution_1.res = res;
477
- } }))];
478
- case 5:
479
- _b.sent();
480
- if (mutationResolution_1.resolved) {
481
- return [2 /*return*/, mutationResolution_1.res];
482
- }
483
- _b.label = 6;
484
- case 6: return [2 /*return*/, parsedResponse];
485
- case 7:
486
- mutateError_1 = _b.sent();
487
- googleAdsError = this.getGoogleAdsError(mutateError_1);
488
- if (!this.hooks.onMutationError) return [3 /*break*/, 9];
489
- return [4 /*yield*/, this.hooks.onMutationError(__assign(__assign({}, baseHookArguments), { error: googleAdsError }))];
490
- case 8:
491
- _b.sent();
492
- _b.label = 9;
493
- case 9: throw googleAdsError;
494
- case 10: return [2 /*return*/];
495
- }
496
- });
497
- });
498
- };
499
- Object.defineProperty(Customer.prototype, "googleAdsFields", {
500
- get: function () {
501
- var _this = this;
502
- return {
503
- searchGoogleAdsFields: function (request) { return __awaiter(_this, void 0, void 0, function () {
504
- var service;
505
- return __generator(this, function (_a) {
506
- switch (_a.label) {
507
- case 0: return [4 /*yield*/, this.loadService("GoogleAdsFieldServiceClient")];
508
- case 1:
509
- service = _a.sent();
510
- return [2 /*return*/, service.searchGoogleAdsFields(request, {
511
- // @ts-expect-error This method does support call headers
512
- otherArgs: { headers: this.callHeaders },
513
- })];
514
- }
292
+ async mutateResources(mutations, mutateOptions = {}) {
293
+ const baseHookArguments = {
294
+ credentials: this.credentials,
295
+ method: "GoogleAdsService.mutate",
296
+ mutations,
297
+ isServiceCall: false,
298
+ };
299
+ if (this.hooks.onMutationStart) {
300
+ const mutationCancellation = { cancelled: false };
301
+ await this.hooks.onMutationStart({
302
+ ...baseHookArguments,
303
+ cancel: (res) => {
304
+ mutationCancellation.cancelled = true;
305
+ mutationCancellation.res = res;
306
+ },
307
+ editOptions: (options) => {
308
+ Object.entries(options).forEach(([key, val]) => {
309
+ // @ts-ignore
310
+ mutateOptions[key] = val;
515
311
  });
516
- }); },
517
- };
518
- },
519
- enumerable: false,
520
- configurable: true
521
- });
522
- return Customer;
523
- }(serviceFactory_1.default));
312
+ },
313
+ });
314
+ if (mutationCancellation.cancelled) {
315
+ return mutationCancellation.res;
316
+ }
317
+ }
318
+ const { service, request } = this.buildMutationRequestAndService(mutations, mutateOptions);
319
+ try {
320
+ const response = (await service.mutate(request, {
321
+ otherArgs: { headers: this.callHeaders },
322
+ }))[0];
323
+ const parsedResponse = request.partial_failure
324
+ ? this.decodePartialFailureError(response)
325
+ : response;
326
+ if (this.hooks.onMutationEnd) {
327
+ const mutationResolution = { resolved: false };
328
+ await this.hooks.onMutationEnd({
329
+ ...baseHookArguments,
330
+ response: parsedResponse,
331
+ resolve: (res) => {
332
+ mutationResolution.resolved = true;
333
+ mutationResolution.res = res;
334
+ },
335
+ });
336
+ if (mutationResolution.resolved) {
337
+ return mutationResolution.res;
338
+ }
339
+ }
340
+ return parsedResponse;
341
+ }
342
+ catch (mutateError) {
343
+ const googleAdsError = this.getGoogleAdsError(mutateError);
344
+ if (this.hooks.onMutationError) {
345
+ await this.hooks.onMutationError({
346
+ ...baseHookArguments,
347
+ error: googleAdsError,
348
+ });
349
+ }
350
+ throw googleAdsError;
351
+ }
352
+ }
353
+ get googleAdsFields() {
354
+ return {
355
+ searchGoogleAdsFields: async (request) => {
356
+ const service = await this.loadService("GoogleAdsFieldServiceClient");
357
+ return service.searchGoogleAdsFields(request, {
358
+ // @ts-expect-error This method does support call headers
359
+ otherArgs: { headers: this.callHeaders },
360
+ });
361
+ },
362
+ };
363
+ }
364
+ }
524
365
  exports.Customer = Customer;