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.
@@ -16,7 +16,12 @@ export declare type BaseMutationHookArgs = {
16
16
  mutation: any;
17
17
  isServiceCall: true;
18
18
  });
19
- declare type StartHookArgs<T = RequestOptions | MutateOptions, A = void> = {
19
+ export declare type BaseServiceHookArgs = {
20
+ credentials: CustomerCredentials;
21
+ method: `${keyof typeof services}.${string}`;
22
+ requestOptions: any;
23
+ };
24
+ declare type StartHookArgs<T = RequestOptions | MutateOptions | any, A = void> = {
20
25
  cancel: A extends void ? () => void : (args?: A) => void;
21
26
  editOptions: (options: Partial<T>) => void;
22
27
  };
@@ -30,6 +35,7 @@ declare type EndHookArgs<T = services.IGoogleAdsRow[] | services.MutateGoogleAds
30
35
  declare type HookArgs = StartHookArgs | ErrorHookArgs | EndHookArgs;
31
36
  declare type RequestHook<H extends HookArgs> = (a: BaseRequestHookArgs & H) => void;
32
37
  declare type MutationHook<H extends HookArgs> = (a: BaseMutationHookArgs & H) => void;
38
+ declare type ServiceHook<H extends HookArgs> = (a: BaseServiceHookArgs & H) => void;
33
39
  export declare type OnQueryStart = RequestHook<StartHookArgs<RequestOptions, any>>;
34
40
  export declare type OnQueryError = RequestHook<ErrorHookArgs>;
35
41
  export declare type OnQueryEnd = RequestHook<EndHookArgs<services.IGoogleAdsRow[]>>;
@@ -38,6 +44,9 @@ export declare type OnStreamError = RequestHook<ErrorHookArgs>;
38
44
  export declare type OnMutationStart = MutationHook<StartHookArgs<MutateOptions, any>>;
39
45
  export declare type OnMutationError = MutationHook<ErrorHookArgs>;
40
46
  export declare type OnMutationEnd = MutationHook<EndHookArgs<services.MutateGoogleAdsResponse>>;
47
+ export declare type OnServiceStart = ServiceHook<StartHookArgs<any, any>>;
48
+ export declare type OnServiceError = ServiceHook<ErrorHookArgs>;
49
+ export declare type OnServiceEnd = ServiceHook<EndHookArgs<any>>;
41
50
  export interface Hooks {
42
51
  /**
43
52
  * @description Hook called before execution of a query in the `query` and `report` methods
@@ -98,7 +107,7 @@ export interface Hooks {
98
107
  onMutationStart?: OnMutationStart;
99
108
  /**
100
109
  * @description Hook called upon a mutation throwing an error
101
- * @params `{ credentials, mutations error }`
110
+ * @params `{ credentials, mutations, error }`
102
111
  * @param credentials customer id, login customer id, linked customer id
103
112
  * @param mutations
104
113
  * @param error google ads error
@@ -108,11 +117,37 @@ export interface Hooks {
108
117
  * @description Hook called after successful execution of a mutation
109
118
  * @params `{ credentials, mutations, response, resolve }`
110
119
  * @param credentials customer id, login customer id, linked customer id
111
- * @mutations
120
+ * @param mutations
112
121
  * @param response results of the mutation
113
122
  * @param resolve utility function for returning an alternative value from the mutation
114
123
  */
115
124
  onMutationEnd?: OnMutationEnd;
125
+ /**
126
+ * @description Hook called before execution of a service
127
+ * @params `{ credentials, method, requestOptions }`
128
+ * @param credentials customer id, login customer id, linked customer id
129
+ * @param method
130
+ * @param cancel utility function for cancelling the service. if an argument is provided then the service will return this argument
131
+ * @param editOptions utility function for editing the service options. any service option keys that are passed will be changed
132
+ */
133
+ onServiceStart?: OnServiceStart;
134
+ /**
135
+ * @description Hook called upon a service throwing an error
136
+ * @params `{ credentials, method, error }`
137
+ * @param credentials customer id, login customer id, linked customer id
138
+ * @param method
139
+ * @param error google ads error
140
+ */
141
+ onServiceError?: OnServiceError;
142
+ /**
143
+ * @description Hook called after successful execution of a service
144
+ * @params `{ credentials, method, response, resolve }`
145
+ * @param credentials customer id, login customer id, linked customer id
146
+ * @param method
147
+ * @param response results of the service
148
+ * @param resolve utility function for returning an alternative value from the service
149
+ */
150
+ onServiceEnd?: OnServiceEnd;
116
151
  }
117
152
  export declare type HookedCancellation = {
118
153
  cancelled: boolean;
@@ -4,5 +4,5 @@ export { fromMicros, toMicros } from "./utils";
4
4
  export { parse } from "./parser";
5
5
  export * as ResourceNames from "./protos/autogen/resourceNames";
6
6
  export { CustomerOptions, ReportOptions, MutateOperation, Constraint, Constraints, } from "./types";
7
- export { Hooks, OnQueryStart, OnQueryError, OnQueryEnd, OnStreamStart, OnStreamError, OnMutationStart, OnMutationError, OnMutationEnd, } from "./hooks";
7
+ export { Hooks, OnQueryStart, OnQueryError, OnQueryEnd, OnStreamStart, OnStreamError, OnMutationStart, OnMutationError, OnMutationEnd, OnServiceStart, OnServiceError, OnServiceEnd, } from "./hooks";
8
8
  export { Customer } from "./customer";
@@ -1,19 +1,12 @@
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
  var __importDefault = (this && this.__importDefault) || function (mod) {
10
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
11
4
  };
12
5
  Object.defineProperty(exports, "__esModule", { value: true });
13
6
  exports.parseRows = exports.getReportOptionFields = exports.getGAQLFields = exports.parse = exports.ParsingError = void 0;
14
- var long_1 = __importDefault(require("long"));
15
- var protos_1 = require("./protos");
16
- var utils_1 = require("./utils");
7
+ const long_1 = __importDefault(require("long"));
8
+ const protos_1 = require("./protos");
9
+ const utils_1 = require("./utils");
17
10
  exports.ParsingError = {
18
11
  NO_REPORT_OPTIONS_OR_GAQL_QUERY: "Must provided reportOptions or gaqlString to parse results.",
19
12
  NO_FIELDS_IN_GAQL_QUERY: "GAQL Query must contain at least one attribute, metric or segment.",
@@ -25,8 +18,7 @@ exports.ParsingError = {
25
18
  const parsedResults = parse({ results, reportOptions })
26
19
  const parsedResults = parse({ results, gaqlString })
27
20
  */
28
- function parse(_a) {
29
- var results = _a.results, reportOptions = _a.reportOptions, gaqlString = _a.gaqlString;
21
+ function parse({ results, reportOptions, gaqlString, }) {
30
22
  if (results.length === 0) {
31
23
  return results;
32
24
  }
@@ -34,27 +26,25 @@ function parse(_a) {
34
26
  typeof gaqlString === "undefined") {
35
27
  throw new Error(exports.ParsingError.NO_REPORT_OPTIONS_OR_GAQL_QUERY);
36
28
  }
37
- var queryFields = reportOptions
29
+ const queryFields = reportOptions
38
30
  ? getReportOptionFields(reportOptions)
39
31
  : getGAQLFields(gaqlString);
40
32
  // Add in all relevant resource_name fields, which are always returned by API
41
- var entities = queryFields.map(function (field) { return field.split(".")[0]; });
42
- var resourceNameFields = protos_1.fields.resourceNames.filter(function (resourceNameField) {
43
- return entities.includes(resourceNameField.split(".")[0]);
44
- });
45
- var allFields = __spreadArrays(queryFields, resourceNameFields);
33
+ const entities = queryFields.map((field) => field.split(".")[0]);
34
+ const resourceNameFields = protos_1.fields.resourceNames.filter((resourceNameField) => entities.includes(resourceNameField.split(".")[0]));
35
+ const allFields = [...queryFields, ...resourceNameFields];
46
36
  return parseRows(results, allFields);
47
37
  }
48
38
  exports.parse = parse;
49
39
  // This function assumes that a gaql query is of the format "select * * * from * ...".
50
40
  // Queries that are not in this format should have thrown an error when called.
51
41
  function getGAQLFields(gaqlString) {
52
- var normalisedQuery = utils_1.normaliseQuery(gaqlString);
53
- var fields = normalisedQuery
42
+ const normalisedQuery = utils_1.normaliseQuery(gaqlString);
43
+ const fields = normalisedQuery
54
44
  .toLowerCase()
55
45
  .replace(/(^\s*select)|( from .*)|(\s+)/g, "")
56
46
  .split(",")
57
- .filter(function (field) { return field.length > 0; });
47
+ .filter((field) => field.length > 0);
58
48
  if (!fields.length) {
59
49
  throw new Error(exports.ParsingError.NO_FIELDS_IN_GAQL_QUERY);
60
50
  }
@@ -62,7 +52,11 @@ function getGAQLFields(gaqlString) {
62
52
  }
63
53
  exports.getGAQLFields = getGAQLFields;
64
54
  function getReportOptionFields(reportOptions) {
65
- var fields = __spreadArrays((reportOptions.attributes || []), (reportOptions.metrics || []), (reportOptions.segments || []));
55
+ const fields = [
56
+ ...(reportOptions.attributes || []),
57
+ ...(reportOptions.metrics || []),
58
+ ...(reportOptions.segments || []),
59
+ ];
66
60
  if (!fields.length) {
67
61
  throw new Error(exports.ParsingError.NO_FIELDS_IN_REPORT_OPTIONS);
68
62
  }
@@ -70,24 +64,23 @@ function getReportOptionFields(reportOptions) {
70
64
  }
71
65
  exports.getReportOptionFields = getReportOptionFields;
72
66
  function parseRows(rows, fields) {
73
- var fieldsPreSplit = {};
67
+ const fieldsPreSplit = {};
74
68
  // pre-split all the field strings for performance reasons (increases speed by ~5x for large number of rows)
75
- for (var _i = 0, fields_1 = fields; _i < fields_1.length; _i++) {
76
- var field = fields_1[_i];
69
+ for (const field of fields) {
77
70
  fieldsPreSplit[field] = field.split(".");
78
71
  }
79
- var newRows = [];
80
- for (var r = 0; r < rows.length; r++) {
81
- var newRow = {};
82
- var originalRow = protos_1.services.GoogleAdsRow.fromObject(rows[r]);
83
- for (var split in fieldsPreSplit) {
72
+ const newRows = [];
73
+ for (let r = 0; r < rows.length; r++) {
74
+ const newRow = {};
75
+ const originalRow = protos_1.services.GoogleAdsRow.fromObject(rows[r]);
76
+ for (const split in fieldsPreSplit) {
84
77
  // @ts-expect-error These are the best we can do for these types
85
- var _a = fieldsPreSplit[split], parent_1 = _a[0], children = _a.slice(1);
78
+ const [parent, ...children] = fieldsPreSplit[split];
86
79
  // Ignore null fields (unspecified resource names)
87
- if (!originalRow[parent_1]) {
80
+ if (!originalRow[parent]) {
88
81
  continue;
89
82
  }
90
- newRow[parent_1] = parseNestedValues(newRow[parent_1], originalRow[parent_1], parent_1, children);
83
+ newRow[parent] = parseNestedValues(newRow[parent], originalRow[parent], parent, children);
91
84
  }
92
85
  newRows.push(newRow);
93
86
  }
@@ -97,12 +90,12 @@ exports.parseRows = parseRows;
97
90
  function parseNestedValues(row, data, field, paths) {
98
91
  if (!data)
99
92
  return null;
100
- var parentField = paths[0], childFields = paths.slice(1);
93
+ const [parentField, ...childFields] = paths;
101
94
  if (!row)
102
95
  row = {};
103
96
  if (childFields.length === 0) {
104
- var rawVal = data[parentField];
105
- var parsedVal = long_1.default.isLong(rawVal)
97
+ const rawVal = data[parentField];
98
+ const parsedVal = long_1.default.isLong(rawVal)
106
99
  ? new long_1.default(rawVal.low, rawVal.high, rawVal.unsigned).toNumber()
107
100
  : rawVal;
108
101
  row[parentField] = parsedVal;