google-ads-api 19.0.0 → 19.0.1-rest-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/CHANGELOG.md +40 -0
- package/README.md +2 -2
- package/build/src/client.d.ts +1 -0
- package/build/src/client.js +1 -0
- package/build/src/customer.d.ts +8 -2
- package/build/src/customer.js +204 -76
- package/build/src/parserRest.d.ts +4 -0
- package/build/src/parserRest.js +88 -0
- package/build/src/protos/autogen/fields.d.ts +1 -0
- package/build/src/protos/autogen/fields.js +3 -1
- package/build/src/protos/autogen/serviceFactory.js +1 -1
- package/build/src/protos/index.d.ts +1 -1
- package/build/src/protos/index.js +2 -1
- package/build/src/query.d.ts +2 -2
- package/build/src/query.js +1 -1
- package/build/src/service.d.ts +1 -0
- package/build/src/service.js +35 -14
- package/build/src/testUtils.d.ts +38 -0
- package/build/src/testUtils.js +76 -5
- package/build/src/types.d.ts +16 -1
- package/build/src/utils.d.ts +0 -5
- package/build/src/utils.js +0 -15
- package/package.json +15 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
### Upcoming Breaking Changes
|
|
4
|
+
|
|
5
|
+
From v20 onwards this library will use the REST api for report, reportStream, search and searchStream. This will result in minor breaking changes to reporting calls:
|
|
6
|
+
|
|
7
|
+
- Fields with the enum value `0` will now be `undefined` instead
|
|
8
|
+
- Array-fields (such as `final_urls`) with the value `[]` will now be `undefined` instead
|
|
9
|
+
|
|
10
|
+
While these changes are inconvenient, the performance of the REST api is significantly better than the gRPC api, particularly for responses with many rows.
|
|
11
|
+
|
|
12
|
+
To prepare for this change, we recommend you use the install the `19.0.0-rest-beta` version of this library and test your application with it.
|
|
13
|
+
|
|
14
|
+
## 19.0.1
|
|
15
|
+
|
|
16
|
+
- Fix issue with type interface for search_settings not showing any valid fields.
|
|
17
|
+
|
|
18
|
+
## 19.0.0
|
|
19
|
+
|
|
20
|
+
### Version Upgrade
|
|
21
|
+
|
|
22
|
+
- Upgraded google-ads-api version to v19. Refer to Google ads release notes [here](https://developers.google.com/google-ads/api/docs/release-notes) for changes.
|
|
23
|
+
|
|
24
|
+
### Library Changes
|
|
25
|
+
|
|
26
|
+
- We currently do not support `return_total_results_count` on search_settings for SearchGoogleAdsRequest. Please use `reportCount()` instead. This has been reflected in the `RequestOptions` type:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
export type RequestOptions = Omit<
|
|
30
|
+
services.ISearchGoogleAdsRequest,
|
|
31
|
+
"customer_id" | "query" | "search_settings"
|
|
32
|
+
> & {
|
|
33
|
+
search_settings?: Omit<
|
|
34
|
+
services.ISearchSettings,
|
|
35
|
+
"return_total_results_count"
|
|
36
|
+
>;
|
|
37
|
+
};
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
For more information please see the `SearchSettings` reference on the Google document [here](https://developers.google.com/google-ads/api/reference/rpc/v19/SearchSettings).
|
package/README.md
CHANGED
|
@@ -444,7 +444,7 @@ const [summaryRow, ...response] = await customer.report({
|
|
|
444
444
|
entity: "campaign",
|
|
445
445
|
metrics: ["metrics.clicks", "metrics.all_conversions"],
|
|
446
446
|
search_settings: {
|
|
447
|
-
return_summary_row:
|
|
447
|
+
return_summary_row: true,
|
|
448
448
|
},
|
|
449
449
|
});
|
|
450
450
|
```
|
|
@@ -456,7 +456,7 @@ const stream = customer.reportStream({
|
|
|
456
456
|
entity: "campaign",
|
|
457
457
|
metrics: ["metrics.clicks", "metrics.all_conversions"],
|
|
458
458
|
search_settings: {
|
|
459
|
-
return_summary_row:
|
|
459
|
+
return_summary_row: true,
|
|
460
460
|
},
|
|
461
461
|
});
|
|
462
462
|
|
package/build/src/client.d.ts
CHANGED
package/build/src/client.js
CHANGED
package/build/src/customer.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ClientOptions } from "./client";
|
|
|
3
3
|
import { Hooks } from "./hooks";
|
|
4
4
|
import { services } from "./protos";
|
|
5
5
|
import ServiceFactory from "./protos/autogen/serviceFactory";
|
|
6
|
-
import { CustomerOptions, MutateOperation, MutateOptions, ReportOptions, RequestOptions } from "./types";
|
|
6
|
+
import { CustomerOptions, MutateOperation, MutateOptions, ReportOptions, ReportOptionsWithTotalResults, RequestOptions } from "./types";
|
|
7
7
|
export declare class Customer extends ServiceFactory {
|
|
8
8
|
constructor(clientOptions: ClientOptions, customerOptions: CustomerOptions, hooks?: Hooks);
|
|
9
9
|
/**
|
|
@@ -30,7 +30,7 @@ export declare class Customer extends ServiceFactory {
|
|
|
30
30
|
@description Get the total row count of a report.
|
|
31
31
|
@hooks none
|
|
32
32
|
*/
|
|
33
|
-
reportCount(options: Readonly<
|
|
33
|
+
reportCount(options: Readonly<ReportOptionsWithTotalResults>): Promise<number | undefined>;
|
|
34
34
|
/**
|
|
35
35
|
@description Stream query using ReportOptions. If a generic type is provided, it must be the type of a single row.
|
|
36
36
|
If a summary row is requested then this will be the last emitted row of the stream.
|
|
@@ -52,8 +52,10 @@ export declare class Customer extends ServiceFactory {
|
|
|
52
52
|
reportStreamRaw(reportOptions: Readonly<ReportOptions>): Promise<CancellableStream | void>;
|
|
53
53
|
private search;
|
|
54
54
|
private paginatedSearch;
|
|
55
|
+
private useStreamToImitateRegularSearch;
|
|
55
56
|
private querier;
|
|
56
57
|
private streamer;
|
|
58
|
+
private handleStreamError;
|
|
57
59
|
/**
|
|
58
60
|
* @description Creates, updates, or removes resources. This method supports atomic transactions
|
|
59
61
|
* with multiple types of resources. For example, you can atomically create a campaign and a
|
|
@@ -62,4 +64,8 @@ export declare class Customer extends ServiceFactory {
|
|
|
62
64
|
*/
|
|
63
65
|
mutateResources<T>(mutations: MutateOperation<T>[], mutateOptions?: MutateOptions): Promise<services.MutateGoogleAdsResponse>;
|
|
64
66
|
private get googleAdsFields();
|
|
67
|
+
private prepareGoogleAdsServicePostRequestArgs;
|
|
68
|
+
private decamelizeKeysIfNeeded;
|
|
69
|
+
private gaqlQueryStringIncludesLimit;
|
|
70
|
+
private generateTooManyRowsError;
|
|
65
71
|
}
|
package/build/src/customer.js
CHANGED
|
@@ -4,13 +4,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Customer = void 0;
|
|
7
|
-
const
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const stream_chain_1 = require("stream-chain");
|
|
9
|
+
const stream_json_1 = require("stream-json");
|
|
10
|
+
const StreamArray_1 = require("stream-json/streamers/StreamArray");
|
|
11
|
+
const parserRest_1 = require("./parserRest");
|
|
12
|
+
const protos_1 = require("./protos");
|
|
8
13
|
const serviceFactory_1 = __importDefault(require("./protos/autogen/serviceFactory"));
|
|
9
14
|
const query_1 = require("./query");
|
|
10
|
-
const
|
|
15
|
+
const version_1 = require("./version");
|
|
16
|
+
const ROWS_PER_STREAMED_CHUNK = 10_000; // From experience, this is what can be expected from the API.
|
|
11
17
|
class Customer extends serviceFactory_1.default {
|
|
12
18
|
constructor(clientOptions, customerOptions, hooks) {
|
|
13
|
-
super(clientOptions, customerOptions, hooks
|
|
19
|
+
super(clientOptions, customerOptions, hooks ?? {});
|
|
14
20
|
}
|
|
15
21
|
/**
|
|
16
22
|
@description Single query using a raw GAQL string.
|
|
@@ -49,9 +55,10 @@ class Customer extends serviceFactory_1.default {
|
|
|
49
55
|
@hooks none
|
|
50
56
|
*/
|
|
51
57
|
async reportCount(options) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
// must get at least one row
|
|
59
|
+
const { gaqlQuery, requestOptions } = (0, query_1.buildQuery)({ ...options, limit: 1 });
|
|
60
|
+
// We do not allow this field in reportOptions, however it is still a valid request option
|
|
61
|
+
requestOptions.search_settings = { return_total_results_count: true };
|
|
55
62
|
const useHooks = false; // to avoid cacheing conflicts
|
|
56
63
|
const { totalResultsCount } = await this.querier(gaqlQuery, requestOptions, options, useHooks);
|
|
57
64
|
return totalResultsCount;
|
|
@@ -111,39 +118,123 @@ class Customer extends serviceFactory_1.default {
|
|
|
111
118
|
});
|
|
112
119
|
}
|
|
113
120
|
async search(gaqlQuery, requestOptions) {
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
121
|
+
const accessToken = await this.getAccessToken();
|
|
122
|
+
try {
|
|
123
|
+
const rawResponse = await (0, axios_1.default)(this.prepareGoogleAdsServicePostRequestArgs("search", accessToken, {
|
|
124
|
+
data: {
|
|
125
|
+
query: gaqlQuery,
|
|
126
|
+
...requestOptions,
|
|
127
|
+
},
|
|
128
|
+
}));
|
|
129
|
+
const searchResponse = rawResponse.data;
|
|
130
|
+
const results = searchResponse.results ?? [];
|
|
131
|
+
const response = results.map((row) => this.decamelizeKeysIfNeeded(row));
|
|
132
|
+
const summaryRow = this.decamelizeKeysIfNeeded(searchResponse.summaryRow);
|
|
133
|
+
const nextPageToken = searchResponse.nextPageToken;
|
|
134
|
+
const totalResultsCount = searchResponse.totalResultsCount
|
|
135
|
+
? +searchResponse.totalResultsCount
|
|
136
|
+
: undefined;
|
|
137
|
+
return { response, nextPageToken, totalResultsCount, summaryRow };
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
if (e.response?.data.error.details[0]) {
|
|
141
|
+
throw new protos_1.errors.GoogleAdsFailure(this.decamelizeKeysIfNeeded(e.response.data.error.details[0]));
|
|
142
|
+
}
|
|
143
|
+
throw e;
|
|
127
144
|
}
|
|
128
|
-
return { response, nextPageToken, totalResultsCount };
|
|
129
145
|
}
|
|
130
|
-
async paginatedSearch(gaqlQuery, requestOptions
|
|
146
|
+
async paginatedSearch(gaqlQuery, requestOptions) {
|
|
147
|
+
/*
|
|
148
|
+
When possible, use the searchStream method to avoid the overhead of pagination.
|
|
149
|
+
*/
|
|
150
|
+
if (requestOptions.page_size === undefined &&
|
|
151
|
+
!requestOptions.search_settings?.return_summary_row) {
|
|
152
|
+
// If no pagination or summary options are set, we can use the non-paginated search method.
|
|
153
|
+
const { response } = await this.useStreamToImitateRegularSearch(gaqlQuery, requestOptions);
|
|
154
|
+
return { response };
|
|
155
|
+
}
|
|
131
156
|
const response = [];
|
|
132
157
|
let nextPageToken = undefined;
|
|
133
158
|
const initialSearch = await this.search(gaqlQuery, requestOptions);
|
|
134
|
-
|
|
135
|
-
response.
|
|
159
|
+
let totalResultsCount = initialSearch.totalResultsCount;
|
|
160
|
+
// Sometimes (when no results?) the totalResultsCount field is not included in the response.
|
|
161
|
+
// In this case, we set it to 0.
|
|
162
|
+
if (requestOptions.search_settings?.return_total_results_count &&
|
|
163
|
+
initialSearch.totalResultsCount === undefined) {
|
|
164
|
+
totalResultsCount = 0;
|
|
165
|
+
}
|
|
166
|
+
let summaryRow = initialSearch.summaryRow;
|
|
167
|
+
response.push(...initialSearch.response);
|
|
136
168
|
nextPageToken = initialSearch.nextPageToken;
|
|
137
169
|
while (nextPageToken) {
|
|
138
170
|
const nextSearch = await this.search(gaqlQuery, {
|
|
139
171
|
...requestOptions,
|
|
140
172
|
page_token: nextPageToken,
|
|
141
173
|
});
|
|
142
|
-
response.push(...
|
|
174
|
+
response.push(...nextSearch.response);
|
|
143
175
|
nextPageToken = nextSearch.nextPageToken;
|
|
176
|
+
if (nextSearch.summaryRow) {
|
|
177
|
+
summaryRow = nextSearch.summaryRow;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (summaryRow) {
|
|
181
|
+
response.unshift(summaryRow);
|
|
144
182
|
}
|
|
145
183
|
return { response, totalResultsCount };
|
|
146
184
|
}
|
|
185
|
+
// Google's searchStream method is faster than search, but it does not support all features.
|
|
186
|
+
// When report() is called, we use searchStream if possible, otherwise we use paginatedSearch.
|
|
187
|
+
// Note that just like `paginatedSearch`, this method accumulates results in memory. Use
|
|
188
|
+
// `reportStream` for a more memory-efficient alternative (at the cost of more CPU usage).
|
|
189
|
+
async useStreamToImitateRegularSearch(gaqlQuery, requestOptions) {
|
|
190
|
+
const accessToken = await this.getAccessToken();
|
|
191
|
+
try {
|
|
192
|
+
const args = this.prepareGoogleAdsServicePostRequestArgs("searchStream", accessToken, {
|
|
193
|
+
responseType: "stream",
|
|
194
|
+
data: {
|
|
195
|
+
query: gaqlQuery,
|
|
196
|
+
...requestOptions,
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
const response = await (0, axios_1.default)(args);
|
|
200
|
+
const stream = response.data;
|
|
201
|
+
const buffers = [];
|
|
202
|
+
let rowCount = -ROWS_PER_STREAMED_CHUNK;
|
|
203
|
+
for await (const data of stream) {
|
|
204
|
+
if (this.clientOptions.max_reporting_rows &&
|
|
205
|
+
!this.gaqlQueryStringIncludesLimit(gaqlQuery)) {
|
|
206
|
+
// This is a quick-and-dirty way to count rows, but it's good enough for our purposes.
|
|
207
|
+
// We want to avoid using a proper JSON streamer here for performance reasons.
|
|
208
|
+
if (data.toString("utf-8").includes(`results":`)) {
|
|
209
|
+
rowCount += ROWS_PER_STREAMED_CHUNK;
|
|
210
|
+
}
|
|
211
|
+
if (rowCount > this.clientOptions.max_reporting_rows) {
|
|
212
|
+
throw this.generateTooManyRowsError();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
buffers.push(data);
|
|
216
|
+
}
|
|
217
|
+
const asString = Buffer.concat(buffers).toString("utf-8");
|
|
218
|
+
const accumulator = [];
|
|
219
|
+
let foundSummaryRow;
|
|
220
|
+
for (const { results, summaryRow } of JSON.parse(asString)) {
|
|
221
|
+
if (summaryRow) {
|
|
222
|
+
foundSummaryRow = this.decamelizeKeysIfNeeded(summaryRow);
|
|
223
|
+
}
|
|
224
|
+
accumulator.push(...(results ?? []).map((row) => {
|
|
225
|
+
return this.decamelizeKeysIfNeeded(row);
|
|
226
|
+
}));
|
|
227
|
+
if (foundSummaryRow) {
|
|
228
|
+
accumulator.unshift(foundSummaryRow);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return { response: accumulator };
|
|
232
|
+
}
|
|
233
|
+
catch (e) {
|
|
234
|
+
await this.handleStreamError(e);
|
|
235
|
+
throw e; // The line above should always throw.
|
|
236
|
+
}
|
|
237
|
+
}
|
|
147
238
|
async querier(gaqlQuery, requestOptions = {}, reportOptions, useHooks = true) {
|
|
148
239
|
const baseHookArguments = {
|
|
149
240
|
credentials: this.credentials,
|
|
@@ -170,14 +261,7 @@ class Customer extends serviceFactory_1.default {
|
|
|
170
261
|
}
|
|
171
262
|
}
|
|
172
263
|
try {
|
|
173
|
-
const
|
|
174
|
-
return this.clientOptions.disable_parsing
|
|
175
|
-
? rows
|
|
176
|
-
: reportOptions
|
|
177
|
-
? (0, parser_1.parse)({ results: rows, reportOptions })
|
|
178
|
-
: (0, parser_1.parse)({ results: rows, gaqlString: gaqlQuery });
|
|
179
|
-
};
|
|
180
|
-
const { response, totalResultsCount } = await this.paginatedSearch(gaqlQuery, requestOptions, parsingWapper);
|
|
264
|
+
const { response, totalResultsCount } = await this.paginatedSearch(gaqlQuery, requestOptions);
|
|
181
265
|
if (this.hooks.onQueryEnd && useHooks) {
|
|
182
266
|
const queryResolution = { resolved: false };
|
|
183
267
|
await this.hooks.onQueryEnd({
|
|
@@ -229,58 +313,79 @@ class Customer extends serviceFactory_1.default {
|
|
|
229
313
|
return;
|
|
230
314
|
}
|
|
231
315
|
}
|
|
232
|
-
const { service, request } = this.buildSearchStreamRequestAndService(gaqlQuery, requestOptions);
|
|
233
|
-
const stream = service.searchStream(request, {
|
|
234
|
-
otherArgs: { headers: this.callHeaders },
|
|
235
|
-
});
|
|
236
|
-
let streamFinished = false;
|
|
237
|
-
const accumulator = [];
|
|
238
|
-
let nextChunk = (0, utils_1.createNextChunkArrivedPromise)();
|
|
239
|
-
stream.on("data", (chunk) => {
|
|
240
|
-
const results = chunk.summary_row ? [chunk.summary_row] : chunk.results;
|
|
241
|
-
const parsedResponse = this.clientOptions.disable_parsing
|
|
242
|
-
? results
|
|
243
|
-
: reportOptions
|
|
244
|
-
? (0, parser_1.parse)({ results, reportOptions })
|
|
245
|
-
: (0, parser_1.parse)({ results, gaqlString: gaqlQuery });
|
|
246
|
-
accumulator.push(...parsedResponse);
|
|
247
|
-
nextChunk.resolve();
|
|
248
|
-
nextChunk = (0, utils_1.createNextChunkArrivedPromise)();
|
|
249
|
-
});
|
|
250
|
-
stream.on("error", (searchError) => {
|
|
251
|
-
nextChunk.reject(searchError);
|
|
252
|
-
});
|
|
253
|
-
stream.on("end", () => {
|
|
254
|
-
streamFinished = true;
|
|
255
|
-
nextChunk.resolve();
|
|
256
|
-
});
|
|
257
316
|
try {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
317
|
+
const accessToken = await this.getAccessToken();
|
|
318
|
+
const args = this.prepareGoogleAdsServicePostRequestArgs("searchStream", accessToken, {
|
|
319
|
+
responseType: "stream",
|
|
320
|
+
data: {
|
|
321
|
+
query: gaqlQuery,
|
|
322
|
+
...requestOptions,
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
const response = await (0, axios_1.default)(args);
|
|
326
|
+
const stream = response.data;
|
|
327
|
+
// The options below help to make the stream less CPU intensive.
|
|
328
|
+
const parser = new stream_json_1.Parser({
|
|
329
|
+
streamValues: false,
|
|
330
|
+
streamKeys: false,
|
|
331
|
+
packValues: true,
|
|
332
|
+
packKeys: true,
|
|
333
|
+
});
|
|
334
|
+
const pipeline = (0, stream_chain_1.chain)([stream, parser, (0, StreamArray_1.streamArray)()]);
|
|
335
|
+
let count = 0;
|
|
336
|
+
for await (const data of pipeline) {
|
|
337
|
+
const results = data.value.results ?? [data.value.summaryRow];
|
|
338
|
+
count += results.length;
|
|
339
|
+
if (this.clientOptions.max_reporting_rows &&
|
|
340
|
+
count > this.clientOptions.max_reporting_rows &&
|
|
341
|
+
!this.gaqlQueryStringIncludesLimit(gaqlQuery)) {
|
|
342
|
+
throw this.generateTooManyRowsError();
|
|
265
343
|
}
|
|
266
|
-
|
|
267
|
-
|
|
344
|
+
for (const row of results) {
|
|
345
|
+
const parsed = this.decamelizeKeysIfNeeded(row);
|
|
346
|
+
yield parsed;
|
|
268
347
|
}
|
|
269
348
|
}
|
|
349
|
+
return;
|
|
270
350
|
}
|
|
271
|
-
catch (
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
351
|
+
catch (e) {
|
|
352
|
+
try {
|
|
353
|
+
await this.handleStreamError(e);
|
|
354
|
+
}
|
|
355
|
+
catch (_e) {
|
|
356
|
+
if (this.hooks.onStreamError) {
|
|
357
|
+
await this.hooks.onStreamError({
|
|
358
|
+
...baseHookArguments,
|
|
359
|
+
error: _e,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
throw _e;
|
|
278
363
|
}
|
|
279
|
-
throw googleAdsError;
|
|
280
364
|
}
|
|
281
|
-
|
|
282
|
-
|
|
365
|
+
}
|
|
366
|
+
async handleStreamError(e) {
|
|
367
|
+
if (!e?.response?.data) {
|
|
368
|
+
throw e;
|
|
283
369
|
}
|
|
370
|
+
// The error is a stream, so some effort is required to parse it.
|
|
371
|
+
const stream = e.response.data;
|
|
372
|
+
const pipeline = (0, stream_chain_1.chain)([stream, (0, stream_json_1.parser)(), (0, StreamArray_1.streamArray)()]);
|
|
373
|
+
const defaultErrorMessage = "Unknown GoogleAdsFailure";
|
|
374
|
+
let googleAdsFailure = new Error(defaultErrorMessage);
|
|
375
|
+
// Only throw the first error.
|
|
376
|
+
pipeline.once("data", (data) => {
|
|
377
|
+
if (data?.value?.error?.details?.[0]) {
|
|
378
|
+
googleAdsFailure = new protos_1.errors.GoogleAdsFailure(this.decamelizeKeysIfNeeded(data.value.error.details[0]));
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
googleAdsFailure = new Error(data?.value?.error?.message ?? defaultErrorMessage, { cause: data?.value?.error ?? data?.value });
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
// Must always reject.
|
|
385
|
+
await new Promise((_, reject) => {
|
|
386
|
+
pipeline.on("end", () => reject(googleAdsFailure));
|
|
387
|
+
pipeline.on("error", (err) => reject(err));
|
|
388
|
+
});
|
|
284
389
|
}
|
|
285
390
|
/**
|
|
286
391
|
* @description Creates, updates, or removes resources. This method supports atomic transactions
|
|
@@ -360,5 +465,28 @@ class Customer extends serviceFactory_1.default {
|
|
|
360
465
|
},
|
|
361
466
|
};
|
|
362
467
|
}
|
|
468
|
+
prepareGoogleAdsServicePostRequestArgs(functionName, accessToken, extra) {
|
|
469
|
+
return {
|
|
470
|
+
method: "POST",
|
|
471
|
+
url: `https://googleads.googleapis.com/${version_1.googleAdsVersion}/customers/${this.customerOptions.customer_id}/googleAds:${functionName}`,
|
|
472
|
+
headers: {
|
|
473
|
+
Authorization: `Bearer ${accessToken}`,
|
|
474
|
+
...this.callHeaders,
|
|
475
|
+
},
|
|
476
|
+
...extra,
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
decamelizeKeysIfNeeded(input) {
|
|
480
|
+
if (this.clientOptions.disable_parsing) {
|
|
481
|
+
return input;
|
|
482
|
+
}
|
|
483
|
+
return (0, parserRest_1.decamelizeKeys)(input);
|
|
484
|
+
}
|
|
485
|
+
gaqlQueryStringIncludesLimit(gaqlQuery) {
|
|
486
|
+
return gaqlQuery.toLowerCase().includes("limit ");
|
|
487
|
+
}
|
|
488
|
+
generateTooManyRowsError() {
|
|
489
|
+
return new Error(`Exceeded the maximum number of rows set by "max_reporting_rows" (${this.clientOptions.max_reporting_rows}).`);
|
|
490
|
+
}
|
|
363
491
|
}
|
|
364
492
|
exports.Customer = Customer;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* JSON Rest parsing
|
|
4
|
+
*/
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.decamelizeKeys = void 0;
|
|
10
|
+
const map_obj_1 = __importDefault(require("map-obj"));
|
|
11
|
+
const circ_json_1 = require("circ-json");
|
|
12
|
+
const utils_1 = require("./utils");
|
|
13
|
+
const fields_1 = require("./protos/autogen/fields");
|
|
14
|
+
const enums_1 = require("./protos/autogen/enums");
|
|
15
|
+
const fieldDataTypes = (0, circ_json_1.parse)(fields_1.fieldDataTypes);
|
|
16
|
+
const decamelizeCache = new Map();
|
|
17
|
+
const fieldTypeCache = new Map();
|
|
18
|
+
const isObject = (value) => typeof value === "object" && value !== null;
|
|
19
|
+
const decamelizeKeys = (input) => {
|
|
20
|
+
if (!isObject(input)) {
|
|
21
|
+
return input;
|
|
22
|
+
}
|
|
23
|
+
const makeMapper = (parentPath) => (key, value) => {
|
|
24
|
+
key = cachedDecamelize(key);
|
|
25
|
+
if (isObject(value)) {
|
|
26
|
+
const path = parentPath === undefined ? key : `${parentPath}.${key}`;
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
value = (0, map_obj_1.default)(value, makeMapper(path));
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
value = cachedValueParser(key, parentPath, value);
|
|
32
|
+
}
|
|
33
|
+
return [key, value];
|
|
34
|
+
};
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
return (0, map_obj_1.default)(input, makeMapper());
|
|
37
|
+
};
|
|
38
|
+
exports.decamelizeKeys = decamelizeKeys;
|
|
39
|
+
const cachedDecamelize = (key) => {
|
|
40
|
+
const cachedResult = decamelizeCache.get(key);
|
|
41
|
+
if (cachedResult) {
|
|
42
|
+
return cachedResult;
|
|
43
|
+
}
|
|
44
|
+
const newKey = (0, utils_1.toSnakeCase)(key);
|
|
45
|
+
decamelizeCache.set(key, newKey);
|
|
46
|
+
return newKey;
|
|
47
|
+
};
|
|
48
|
+
const cachedValueParser = (key, parentPath, value) => {
|
|
49
|
+
let newValue = value;
|
|
50
|
+
const fullPath = parentPath ? `${parentPath}.${key}` : key;
|
|
51
|
+
const megaDataType = getTypeFromPath(fullPath);
|
|
52
|
+
if (megaDataType === undefined && !fullPath.startsWith("@")) {
|
|
53
|
+
console.warn(`No data type found for ${fullPath}`);
|
|
54
|
+
}
|
|
55
|
+
else if (typeof megaDataType === "object") {
|
|
56
|
+
newValue = megaDataType[value];
|
|
57
|
+
}
|
|
58
|
+
else if (megaDataType === "INT64") {
|
|
59
|
+
newValue = Number(value);
|
|
60
|
+
}
|
|
61
|
+
else if (megaDataType === "ENUM") {
|
|
62
|
+
// Some enums aren't embedded in megaDataType, so we need this fallback.
|
|
63
|
+
// @ts-expect-error typescript doesn't like accessing items in a namespace with a string
|
|
64
|
+
newValue = enums_1.enums[fields_1.fields.enumFields[fullPath]][value]; // e.g. enums['CampaignStatus'][ENABLED] = "2"
|
|
65
|
+
}
|
|
66
|
+
return newValue;
|
|
67
|
+
};
|
|
68
|
+
const getTypeFromPath = (path) => {
|
|
69
|
+
const cachedResult = fieldTypeCache.get(path);
|
|
70
|
+
if (cachedResult) {
|
|
71
|
+
return cachedResult;
|
|
72
|
+
}
|
|
73
|
+
const t = get(fieldDataTypes, path);
|
|
74
|
+
fieldTypeCache.set(path, t);
|
|
75
|
+
return t;
|
|
76
|
+
};
|
|
77
|
+
// Copied from youmightnotneed.com
|
|
78
|
+
const get = (obj, path) => {
|
|
79
|
+
if (!path)
|
|
80
|
+
return undefined;
|
|
81
|
+
// Check if path is string or array. Regex : ensure that we do not have '.' and brackets.
|
|
82
|
+
// Regex explained: https://regexr.com/58j0k
|
|
83
|
+
const pathArray = path.match(/([^[.\]])+/g);
|
|
84
|
+
if (!pathArray)
|
|
85
|
+
return undefined;
|
|
86
|
+
// Find value
|
|
87
|
+
return pathArray.reduce((prevObj, key) => prevObj && prevObj[key], obj);
|
|
88
|
+
};
|