google-ads-api 17.0.0-beta.3 → 17.1.0-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/README.md +33 -17
- package/build/src/client.d.ts +1 -0
- package/build/src/client.js +1 -0
- package/build/src/customer.d.ts +21 -0
- package/build/src/customer.js +215 -73
- 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.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/utils.d.ts +0 -5
- package/build/src/utils.js +1 -16
- package/build/src/version.js +0 -18
- package/package.json +16 -6
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
</p>
|
|
8
8
|
<p align="center">
|
|
9
9
|
<a href="https://developers.google.com/google-ads/api/docs/release-notes">
|
|
10
|
-
<img src="https://img.shields.io/badge/google%20ads-
|
|
10
|
+
<img src="https://img.shields.io/badge/google%20ads-v17.1.0-009688.svg?style=flat-square">
|
|
11
11
|
</a>
|
|
12
12
|
<a href="https://www.npmjs.com/package/google-ads-api">
|
|
13
13
|
<img src="https://img.shields.io/npm/v/google-ads-api.svg?style=flat-square">
|
|
@@ -379,32 +379,34 @@ import {
|
|
|
379
379
|
} from "google-ads-api";
|
|
380
380
|
|
|
381
381
|
// Ad Group to which you want to add the keyword
|
|
382
|
-
const adGroupResourceName =
|
|
382
|
+
const adGroupResourceName = "customers/123/adGroups/456";
|
|
383
383
|
|
|
384
|
-
const keyword =
|
|
384
|
+
const keyword = "24 hour locksmith harlem";
|
|
385
385
|
|
|
386
386
|
const operations: MutateOperation<
|
|
387
|
-
resources.IAdGroupCriterion & {
|
|
387
|
+
resources.IAdGroupCriterion & {
|
|
388
|
+
exempt_policy_violation_keys?: google.ads.googleads.v17.common.IPolicyViolationKey[];
|
|
389
|
+
}
|
|
388
390
|
>[] = [
|
|
389
391
|
{
|
|
390
|
-
entity:
|
|
392
|
+
entity: "ad_group_criterion",
|
|
391
393
|
operation: "create",
|
|
392
394
|
resource: {
|
|
393
395
|
// Keyword with policy violation exemptions
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
396
|
+
ad_group: adGroupResourceName,
|
|
397
|
+
keyword: {
|
|
398
|
+
text: "24 hour locksmith harlem",
|
|
399
|
+
match_type: enums.KeywordMatchType.PHRASE,
|
|
400
|
+
},
|
|
401
|
+
status: enums.AdGroupStatus.ENABLED,
|
|
400
402
|
},
|
|
401
403
|
exempt_policy_violation_keys: [
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
404
|
+
{
|
|
405
|
+
policy_name: "LOCAL_SERVICES",
|
|
406
|
+
violating_text: keyword,
|
|
407
|
+
},
|
|
406
408
|
],
|
|
407
|
-
}
|
|
409
|
+
},
|
|
408
410
|
];
|
|
409
411
|
|
|
410
412
|
const result = await customer.mutateResources(operations);
|
|
@@ -683,7 +685,7 @@ try {
|
|
|
683
685
|
// Get the first one and explicitly check for a certain error type
|
|
684
686
|
const [firstError] = err.errors;
|
|
685
687
|
if (
|
|
686
|
-
firstError.error_code ===
|
|
688
|
+
firstError.error_code.query_error ===
|
|
687
689
|
errors.QueryErrorEnum.QueryError.UNRECOGNIZED_FIELD
|
|
688
690
|
) {
|
|
689
691
|
console.log(
|
|
@@ -693,3 +695,17 @@ try {
|
|
|
693
695
|
}
|
|
694
696
|
}
|
|
695
697
|
```
|
|
698
|
+
|
|
699
|
+
# Updating this library to the latest Google Ads API version
|
|
700
|
+
|
|
701
|
+
1. Update google-ads-node & publish to NPM. Check that package for instructions.
|
|
702
|
+
2. Update google-ads-node & google-gax in package.json. google-gax must be the same version as google-ads-node,
|
|
703
|
+
Otherwise you are likely to get a `TypeError: Channel credentials must be a ChannelCredentials object` error.
|
|
704
|
+
3. Update the version in `version.ts`.
|
|
705
|
+
4. Update the versions in `src/protos/index.ts`.
|
|
706
|
+
5. Run `yarn compile` to update the generated files. The `tsc` command might fail -- if so, temporarily add @ts-nocheck to the top of problematic files so that the compile script can continue. After the compile script has run, those @ts-nocheck lines should no longer be required. The compile step depends on some environment variables being set (see scripts/fields.ts), so make sure to set those.
|
|
707
|
+
6. Prettify the generated files so the the diffs are easier to read.
|
|
708
|
+
7. Check that the diffs are expected and that the generated files are correct. New Gads versions will often add new services, fields, and enums.
|
|
709
|
+
8. Test with `yarn test`.
|
|
710
|
+
9. Test by linking to a real project and making some real requests.
|
|
711
|
+
10. Once confident, bump the major version & publish to NPM.
|
package/build/src/client.d.ts
CHANGED
package/build/src/client.js
CHANGED
package/build/src/customer.d.ts
CHANGED
|
@@ -4,6 +4,21 @@ import { Hooks } from "./hooks";
|
|
|
4
4
|
import { services } from "./protos";
|
|
5
5
|
import ServiceFactory from "./protos/autogen/serviceFactory";
|
|
6
6
|
import { CustomerOptions, MutateOperation, MutateOptions, ReportOptions, RequestOptions } from "./types";
|
|
7
|
+
/**
|
|
8
|
+
* TODO:
|
|
9
|
+
* - rename querierRest to streamRest. Add hooks support to it. OK
|
|
10
|
+
* - write querierRest, using pagination. OK
|
|
11
|
+
* - make sure return_total_results_count works
|
|
12
|
+
* - make sure SUMMARY_ROW_SETTING works
|
|
13
|
+
* - adapt query()
|
|
14
|
+
* - adapt queryStream
|
|
15
|
+
* - adapt report()
|
|
16
|
+
* - adapt reportCount()
|
|
17
|
+
* - adapt reportStream()
|
|
18
|
+
* - reportStreamRaw() ?
|
|
19
|
+
* - bonus: use queryStream when the params allow it. Note: do it with native async generator to make it as fast as possible.
|
|
20
|
+
* - bonus: make it possible to cancel a stream or pagination when row count exceeds specified max
|
|
21
|
+
*/
|
|
7
22
|
export declare class Customer extends ServiceFactory {
|
|
8
23
|
constructor(clientOptions: ClientOptions, customerOptions: CustomerOptions, hooks?: Hooks);
|
|
9
24
|
/**
|
|
@@ -52,8 +67,10 @@ export declare class Customer extends ServiceFactory {
|
|
|
52
67
|
reportStreamRaw(reportOptions: Readonly<ReportOptions>): Promise<CancellableStream | void>;
|
|
53
68
|
private search;
|
|
54
69
|
private paginatedSearch;
|
|
70
|
+
private useStreamToImitateRegularSearch;
|
|
55
71
|
private querier;
|
|
56
72
|
private streamer;
|
|
73
|
+
private handleStreamError;
|
|
57
74
|
/**
|
|
58
75
|
* @description Creates, updates, or removes resources. This method supports atomic transactions
|
|
59
76
|
* with multiple types of resources. For example, you can atomically create a campaign and a
|
|
@@ -62,4 +79,8 @@ export declare class Customer extends ServiceFactory {
|
|
|
62
79
|
*/
|
|
63
80
|
mutateResources<T>(mutations: MutateOperation<T>[], mutateOptions?: MutateOptions): Promise<services.MutateGoogleAdsResponse>;
|
|
64
81
|
private get googleAdsFields();
|
|
82
|
+
private prepareGoogleAdsServicePostRequestArgs;
|
|
83
|
+
private decamelizeKeysIfNeeded;
|
|
84
|
+
private gaqlQueryStringIncludesLimit;
|
|
85
|
+
private generateTooManyRowsError;
|
|
65
86
|
}
|
package/build/src/customer.js
CHANGED
|
@@ -4,13 +4,34 @@ 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.
|
|
17
|
+
/**
|
|
18
|
+
* TODO:
|
|
19
|
+
* - rename querierRest to streamRest. Add hooks support to it. OK
|
|
20
|
+
* - write querierRest, using pagination. OK
|
|
21
|
+
* - make sure return_total_results_count works
|
|
22
|
+
* - make sure SUMMARY_ROW_SETTING works
|
|
23
|
+
* - adapt query()
|
|
24
|
+
* - adapt queryStream
|
|
25
|
+
* - adapt report()
|
|
26
|
+
* - adapt reportCount()
|
|
27
|
+
* - adapt reportStream()
|
|
28
|
+
* - reportStreamRaw() ?
|
|
29
|
+
* - bonus: use queryStream when the params allow it. Note: do it with native async generator to make it as fast as possible.
|
|
30
|
+
* - bonus: make it possible to cancel a stream or pagination when row count exceeds specified max
|
|
31
|
+
*/
|
|
11
32
|
class Customer extends serviceFactory_1.default {
|
|
12
33
|
constructor(clientOptions, customerOptions, hooks) {
|
|
13
|
-
super(clientOptions, customerOptions, hooks
|
|
34
|
+
super(clientOptions, customerOptions, hooks ?? {});
|
|
14
35
|
}
|
|
15
36
|
/**
|
|
16
37
|
@description Single query using a raw GAQL string.
|
|
@@ -111,39 +132,123 @@ class Customer extends serviceFactory_1.default {
|
|
|
111
132
|
});
|
|
112
133
|
}
|
|
113
134
|
async search(gaqlQuery, requestOptions) {
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
135
|
+
const accessToken = await this.getAccessToken();
|
|
136
|
+
try {
|
|
137
|
+
const rawResponse = await (0, axios_1.default)(this.prepareGoogleAdsServicePostRequestArgs("search", accessToken, {
|
|
138
|
+
data: {
|
|
139
|
+
query: gaqlQuery,
|
|
140
|
+
...requestOptions,
|
|
141
|
+
},
|
|
142
|
+
}));
|
|
143
|
+
const searchResponse = rawResponse.data;
|
|
144
|
+
const results = searchResponse.results ?? [];
|
|
145
|
+
const response = results.map((row) => this.decamelizeKeysIfNeeded(row));
|
|
146
|
+
const summaryRow = this.decamelizeKeysIfNeeded(searchResponse.summaryRow);
|
|
147
|
+
const nextPageToken = searchResponse.nextPageToken;
|
|
148
|
+
const totalResultsCount = searchResponse.totalResultsCount
|
|
149
|
+
? +searchResponse.totalResultsCount
|
|
150
|
+
: undefined;
|
|
151
|
+
return { response, nextPageToken, totalResultsCount, summaryRow };
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
if (e.response?.data.error.details[0]) {
|
|
155
|
+
throw new protos_1.errors.GoogleAdsFailure(this.decamelizeKeysIfNeeded(e.response.data.error.details[0]));
|
|
156
|
+
}
|
|
157
|
+
throw e;
|
|
127
158
|
}
|
|
128
|
-
return { response, nextPageToken, totalResultsCount };
|
|
129
159
|
}
|
|
130
|
-
async paginatedSearch(gaqlQuery, requestOptions
|
|
160
|
+
async paginatedSearch(gaqlQuery, requestOptions) {
|
|
161
|
+
/*
|
|
162
|
+
When possible, use the searchStream method to avoid the overhead of pagination.
|
|
163
|
+
*/
|
|
164
|
+
if (requestOptions.page_size === undefined &&
|
|
165
|
+
requestOptions.return_total_results_count === undefined) {
|
|
166
|
+
// If no pagination or summary options are set, we can use the non-paginated search method.
|
|
167
|
+
const { response } = await this.useStreamToImitateRegularSearch(gaqlQuery, requestOptions);
|
|
168
|
+
return { response };
|
|
169
|
+
}
|
|
131
170
|
const response = [];
|
|
132
171
|
let nextPageToken = undefined;
|
|
133
172
|
const initialSearch = await this.search(gaqlQuery, requestOptions);
|
|
134
|
-
|
|
135
|
-
response.
|
|
173
|
+
let totalResultsCount = initialSearch.totalResultsCount;
|
|
174
|
+
// Sometimes (when no results?) the totalResultsCount field is not included in the response.
|
|
175
|
+
// In this case, we set it to 0.
|
|
176
|
+
if (requestOptions.return_total_results_count &&
|
|
177
|
+
initialSearch.totalResultsCount === undefined) {
|
|
178
|
+
totalResultsCount = 0;
|
|
179
|
+
}
|
|
180
|
+
let summaryRow = initialSearch.summaryRow;
|
|
181
|
+
response.push(...initialSearch.response);
|
|
136
182
|
nextPageToken = initialSearch.nextPageToken;
|
|
137
183
|
while (nextPageToken) {
|
|
138
184
|
const nextSearch = await this.search(gaqlQuery, {
|
|
139
185
|
...requestOptions,
|
|
140
186
|
page_token: nextPageToken,
|
|
141
187
|
});
|
|
142
|
-
response.push(...
|
|
188
|
+
response.push(...nextSearch.response);
|
|
143
189
|
nextPageToken = nextSearch.nextPageToken;
|
|
190
|
+
if (nextSearch.summaryRow) {
|
|
191
|
+
summaryRow = nextSearch.summaryRow;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (summaryRow) {
|
|
195
|
+
response.unshift(summaryRow);
|
|
144
196
|
}
|
|
145
197
|
return { response, totalResultsCount };
|
|
146
198
|
}
|
|
199
|
+
// Google's searchStream method is faster than search, but it does not support all features.
|
|
200
|
+
// When report() is called, we use searchStream if possible, otherwise we use paginatedSearch.
|
|
201
|
+
// Note that just like `paginatedSearch`, this method accumulates results in memory. Use
|
|
202
|
+
// `reportStream` for a more memory-efficient alternative (at the cost of more CPU usage).
|
|
203
|
+
async useStreamToImitateRegularSearch(gaqlQuery, requestOptions) {
|
|
204
|
+
const accessToken = await this.getAccessToken();
|
|
205
|
+
try {
|
|
206
|
+
const args = this.prepareGoogleAdsServicePostRequestArgs("searchStream", accessToken, {
|
|
207
|
+
responseType: "stream",
|
|
208
|
+
data: {
|
|
209
|
+
query: gaqlQuery,
|
|
210
|
+
...requestOptions,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
const response = await (0, axios_1.default)(args);
|
|
214
|
+
const stream = response.data;
|
|
215
|
+
const buffers = [];
|
|
216
|
+
let rowCount = -ROWS_PER_STREAMED_CHUNK;
|
|
217
|
+
for await (const data of stream) {
|
|
218
|
+
if (this.clientOptions.max_reporting_rows &&
|
|
219
|
+
!this.gaqlQueryStringIncludesLimit(gaqlQuery)) {
|
|
220
|
+
// This is a quick-and-dirty way to count rows, but it's good enough for our purposes.
|
|
221
|
+
// We want to avoid using a proper JSON streamer here for performance reasons.
|
|
222
|
+
if (data.toString("utf-8").includes(`results":`)) {
|
|
223
|
+
rowCount += ROWS_PER_STREAMED_CHUNK;
|
|
224
|
+
}
|
|
225
|
+
if (rowCount > this.clientOptions.max_reporting_rows) {
|
|
226
|
+
throw this.generateTooManyRowsError();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
buffers.push(data);
|
|
230
|
+
}
|
|
231
|
+
const asString = Buffer.concat(buffers).toString("utf-8");
|
|
232
|
+
const accumulator = [];
|
|
233
|
+
let foundSummaryRow;
|
|
234
|
+
for (const { results, summaryRow } of JSON.parse(asString)) {
|
|
235
|
+
if (summaryRow) {
|
|
236
|
+
foundSummaryRow = this.decamelizeKeysIfNeeded(summaryRow);
|
|
237
|
+
}
|
|
238
|
+
accumulator.push(...(results ?? []).map((row) => {
|
|
239
|
+
return this.decamelizeKeysIfNeeded(row);
|
|
240
|
+
}));
|
|
241
|
+
if (foundSummaryRow) {
|
|
242
|
+
accumulator.unshift(foundSummaryRow);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return { response: accumulator };
|
|
246
|
+
}
|
|
247
|
+
catch (e) {
|
|
248
|
+
await this.handleStreamError(e);
|
|
249
|
+
throw e; // The line above should always throw.
|
|
250
|
+
}
|
|
251
|
+
}
|
|
147
252
|
async querier(gaqlQuery, requestOptions = {}, reportOptions, useHooks = true) {
|
|
148
253
|
const baseHookArguments = {
|
|
149
254
|
credentials: this.credentials,
|
|
@@ -170,14 +275,7 @@ class Customer extends serviceFactory_1.default {
|
|
|
170
275
|
}
|
|
171
276
|
}
|
|
172
277
|
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);
|
|
278
|
+
const { response, totalResultsCount } = await this.paginatedSearch(gaqlQuery, requestOptions);
|
|
181
279
|
if (this.hooks.onQueryEnd && useHooks) {
|
|
182
280
|
const queryResolution = { resolved: false };
|
|
183
281
|
await this.hooks.onQueryEnd({
|
|
@@ -229,58 +327,79 @@ class Customer extends serviceFactory_1.default {
|
|
|
229
327
|
return;
|
|
230
328
|
}
|
|
231
329
|
}
|
|
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
330
|
try {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
331
|
+
const accessToken = await this.getAccessToken();
|
|
332
|
+
const args = this.prepareGoogleAdsServicePostRequestArgs("searchStream", accessToken, {
|
|
333
|
+
responseType: "stream",
|
|
334
|
+
data: {
|
|
335
|
+
query: gaqlQuery,
|
|
336
|
+
...requestOptions,
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
const response = await (0, axios_1.default)(args);
|
|
340
|
+
const stream = response.data;
|
|
341
|
+
// The options below help to make the stream less CPU intensive.
|
|
342
|
+
const parser = new stream_json_1.Parser({
|
|
343
|
+
streamValues: false,
|
|
344
|
+
streamKeys: false,
|
|
345
|
+
packValues: true,
|
|
346
|
+
packKeys: true,
|
|
347
|
+
});
|
|
348
|
+
const pipeline = (0, stream_chain_1.chain)([stream, parser, (0, StreamArray_1.streamArray)()]);
|
|
349
|
+
let count = 0;
|
|
350
|
+
for await (const data of pipeline) {
|
|
351
|
+
const results = data.value.results ?? [data.value.summaryRow];
|
|
352
|
+
count += results.length;
|
|
353
|
+
if (this.clientOptions.max_reporting_rows &&
|
|
354
|
+
count > this.clientOptions.max_reporting_rows &&
|
|
355
|
+
!this.gaqlQueryStringIncludesLimit(gaqlQuery)) {
|
|
356
|
+
throw this.generateTooManyRowsError();
|
|
265
357
|
}
|
|
266
|
-
|
|
267
|
-
|
|
358
|
+
for (const row of results) {
|
|
359
|
+
const parsed = this.decamelizeKeysIfNeeded(row);
|
|
360
|
+
yield parsed;
|
|
268
361
|
}
|
|
269
362
|
}
|
|
363
|
+
return;
|
|
270
364
|
}
|
|
271
|
-
catch (
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
365
|
+
catch (e) {
|
|
366
|
+
try {
|
|
367
|
+
await this.handleStreamError(e);
|
|
368
|
+
}
|
|
369
|
+
catch (_e) {
|
|
370
|
+
if (this.hooks.onStreamError) {
|
|
371
|
+
await this.hooks.onStreamError({
|
|
372
|
+
...baseHookArguments,
|
|
373
|
+
error: _e,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
throw _e;
|
|
278
377
|
}
|
|
279
|
-
throw googleAdsError;
|
|
280
378
|
}
|
|
281
|
-
|
|
282
|
-
|
|
379
|
+
}
|
|
380
|
+
async handleStreamError(e) {
|
|
381
|
+
if (!e?.response?.data) {
|
|
382
|
+
throw e;
|
|
283
383
|
}
|
|
384
|
+
// The error is a stream, so some effort is required to parse it.
|
|
385
|
+
const stream = e.response.data;
|
|
386
|
+
const pipeline = (0, stream_chain_1.chain)([stream, (0, stream_json_1.parser)(), (0, StreamArray_1.streamArray)()]);
|
|
387
|
+
const defaultErrorMessage = "Unknown GoogleAdsFailure";
|
|
388
|
+
let googleAdsFailure = new Error(defaultErrorMessage);
|
|
389
|
+
// Only throw the first error.
|
|
390
|
+
pipeline.once("data", (data) => {
|
|
391
|
+
if (data?.value?.error?.details?.[0]) {
|
|
392
|
+
googleAdsFailure = new protos_1.errors.GoogleAdsFailure(this.decamelizeKeysIfNeeded(data.value.error.details[0]));
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
googleAdsFailure = new Error(data?.value?.error?.message ?? defaultErrorMessage, { cause: data?.value?.error ?? data?.value });
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
// Must always reject.
|
|
399
|
+
await new Promise((_, reject) => {
|
|
400
|
+
pipeline.on("end", () => reject(googleAdsFailure));
|
|
401
|
+
pipeline.on("error", (err) => reject(err));
|
|
402
|
+
});
|
|
284
403
|
}
|
|
285
404
|
/**
|
|
286
405
|
* @description Creates, updates, or removes resources. This method supports atomic transactions
|
|
@@ -360,5 +479,28 @@ class Customer extends serviceFactory_1.default {
|
|
|
360
479
|
},
|
|
361
480
|
};
|
|
362
481
|
}
|
|
482
|
+
prepareGoogleAdsServicePostRequestArgs(functionName, accessToken, extra) {
|
|
483
|
+
return {
|
|
484
|
+
method: "POST",
|
|
485
|
+
url: `https://googleads.googleapis.com/${version_1.googleAdsVersion}/customers/${this.customerOptions.customer_id}/googleAds:${functionName}`,
|
|
486
|
+
headers: {
|
|
487
|
+
Authorization: `Bearer ${accessToken}`,
|
|
488
|
+
...this.callHeaders,
|
|
489
|
+
},
|
|
490
|
+
...extra,
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
decamelizeKeysIfNeeded(input) {
|
|
494
|
+
if (this.clientOptions.disable_parsing) {
|
|
495
|
+
return input;
|
|
496
|
+
}
|
|
497
|
+
return (0, parserRest_1.decamelizeKeys)(input);
|
|
498
|
+
}
|
|
499
|
+
gaqlQueryStringIncludesLimit(gaqlQuery) {
|
|
500
|
+
return gaqlQuery.toLowerCase().includes("limit ");
|
|
501
|
+
}
|
|
502
|
+
generateTooManyRowsError() {
|
|
503
|
+
return new Error(`Exceeded the maximum number of rows set by "max_reporting_rows" (${this.clientOptions.max_reporting_rows}).`);
|
|
504
|
+
}
|
|
363
505
|
}
|
|
364
506
|
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
|
+
};
|