@google-cloud/nodejs-common 2.2.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@google-cloud/nodejs-common",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "A NodeJs common library for solutions based on Cloud Functions",
5
5
  "author": "Google Inc.",
6
6
  "license": "Apache-2.0",
@@ -16,28 +16,26 @@
16
16
  },
17
17
  "homepage": "https://github.com/GoogleCloudPlatform/cloud-for-marketing/blob/master/marketing-analytics/activation/common-libs/nodejs-common/README.md",
18
18
  "dependencies": {
19
- "@google-cloud/aiplatform": "^3.17.0",
19
+ "@google-cloud/aiplatform": "^3.25.0",
20
20
  "@google-cloud/automl": "^4.0.1",
21
- "@google-cloud/bigquery": "^7.5.2",
22
- "@google-cloud/datastore": "^8.6.0",
23
- "@google-cloud/firestore": "^7.5.0",
21
+ "@google-cloud/bigquery": "^7.8.0",
22
+ "@google-cloud/datastore": "^9.1.0",
23
+ "@google-cloud/firestore": "^7.9.0",
24
24
  "@google-cloud/logging-winston": "^6.0.0",
25
- "@google-cloud/pubsub": "^4.3.3",
26
- "@google-cloud/storage": "^7.9.0",
27
- "@google-cloud/scheduler": "^4.1.0",
28
- "@google-cloud/secret-manager": "^5.2.0",
29
- "gaxios": "^6.3.0",
25
+ "@google-cloud/pubsub": "^4.5.0",
26
+ "@google-cloud/storage": "^7.12.0",
27
+ "@google-cloud/scheduler": "^4.3.0",
28
+ "@google-cloud/secret-manager": "^5.6.0",
29
+ "gaxios": "^6.7.0",
30
30
  "google-ads-nodejs-client": "16.0.0",
31
- "google-ads-api": "^14.1.0",
32
- "google-ads-node": "^12.0.2",
33
- "google-auth-library": "^9.7.0",
34
- "googleapis": "^134.0.0",
35
- "winston": "^3.13.0",
36
- "@grpc/grpc-js": "^1.10.5",
31
+ "google-auth-library": "^9.11.0",
32
+ "googleapis": "^140.0.1",
33
+ "winston": "^3.13.1",
34
+ "@grpc/grpc-js": "^1.11.1",
37
35
  "lodash": "^4.17.21"
38
36
  },
39
37
  "devDependencies": {
40
- "jasmine": "^5.1.0"
38
+ "jasmine": "^5.2.0"
41
39
  },
42
40
  "scripts": {
43
41
  "test": "node node_modules/jasmine/bin/jasmine"
@@ -16,7 +16,7 @@
16
16
  * @fileoverview A base class for Google Api client library class.
17
17
  */
18
18
  const { google } = require('googleapis');
19
- const { getLogger, getObjectByPath } = require('../../components/utils.js');
19
+ const { getLogger } = require('../../components/utils.js');
20
20
  const { AuthRestfulApi } = require('./auth_restful_api.js');
21
21
 
22
22
  /**
@@ -94,13 +94,13 @@ class DoubleClickBidManager extends GoogleApiClient {
94
94
  * Gets a query metadata.
95
95
  * See https://developers.google.com/bid-manager/reference/rest/v2/queries/get
96
96
  * @param {number} queryId Id of the query.
97
- * @return {!Promise<!QueryMetadata>} Query metadata, see
98
- * https://developers.google.com/bid-manager/reference/rest/v2/queries#QueryMetadata
97
+ * @return {!Promise<!Query>} Query, see
98
+ * https://developers.google.com/bid-manager/reference/rest/v2/queries#Query
99
99
  */
100
100
  async getQuery(queryId) {
101
101
  const doubleclickbidmanager = await this.getApiClient();
102
102
  const response = await doubleclickbidmanager.queries.get({ queryId });
103
- return response.data.metadata;
103
+ return response.data;
104
104
  }
105
105
 
106
106
  /**
@@ -0,0 +1,113 @@
1
+ // Copyright 2024 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ /** @fileoverview Gmail API Client Library. */
16
+
17
+ 'use strict';
18
+
19
+ const { GoogleApiClient } = require('./base/google_api_client.js');
20
+
21
+ const API_SCOPES =
22
+ Object.freeze(['https://www.googleapis.com/auth/gmail.send']);
23
+ const API_VERSION = 'v1';
24
+
25
+ /**
26
+ * @typedef {{
27
+ * from: string|undefined,
28
+ * to: string|Array<string>,
29
+ * cc: string|Array<string>|undefined,
30
+ * bcc: string|Array<string>|undefined,
31
+ * 'reply-to': string|Array<string>|undefined,
32
+ * subject: string,
33
+ * content: string,
34
+ * type: 'text/plain'|'text/html'|undefined,
35
+ * }}
36
+ */
37
+ let EmailOptions;
38
+
39
+ /**
40
+ * Email sending class based on Gmail API.
41
+ */
42
+ class Gmail extends GoogleApiClient {
43
+
44
+ constructor(env = process.env) {
45
+ super(env);
46
+ this.googleApi = 'gmail';
47
+ }
48
+
49
+ /** @override */
50
+ getScope() {
51
+ return API_SCOPES;
52
+ }
53
+
54
+ /** @override */
55
+ getVersion() {
56
+ return API_VERSION;
57
+ }
58
+
59
+ /**
60
+ * Generates a string for an email in RFC 2822 format.
61
+ * If 'from' is different from the OAuth token's owner account, it would be
62
+ * ignored.
63
+ * @param {!EmailOptions} options
64
+ * @return {string}
65
+ * @private
66
+ */
67
+ getRawEmail_(options = {}) {
68
+ const message = [];
69
+ ['from', 'to', 'cc', 'bcc', 'reply-to', 'subject'].forEach((key) => {
70
+ if (options[key]) {
71
+ const value = Array.isArray(options[key])
72
+ ? options[key].join(',') : options[key];
73
+ message.push(`${key}: ${value}`);
74
+ }
75
+ });
76
+ if (message.length === 0)
77
+ throw new Error(`Can not get email from ${options}`);
78
+ message.push('\n');
79
+ message.push(options.content);
80
+ return message.join('\n');
81
+ }
82
+
83
+ /**
84
+ * Sends a simple email (no attachment).
85
+ * @see https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send
86
+ * @param {!EmailOptions} options
87
+ * @return {!Message}
88
+ */
89
+ async sendSimpleEmail(options) {
90
+ const email = this.getRawEmail_(options);
91
+
92
+ const gmail = await this.getApiClient();
93
+ const message = {
94
+ userId: 'me',
95
+ requestBody: { raw: Buffer.from(email).toString('base64') }
96
+ };
97
+ try {
98
+ const response = await gmail.users.messages.send(message);
99
+ return { responseStatus: response.status };
100
+ } catch (error) {
101
+ this.logger.error('Result of sending an email', JSON.stringify(error));
102
+ return { responseStatus: error.status };
103
+ }
104
+ }
105
+
106
+ }
107
+
108
+ module.exports = {
109
+ Gmail,
110
+ EmailOptions,
111
+ API_SCOPES,
112
+ API_VERSION,
113
+ };
package/src/apis/index.js CHANGED
@@ -99,18 +99,6 @@ exports.doubleclickbidmanager = require('./doubleclick_bidmanager.js');
99
99
  */
100
100
  exports.bigquery = require('./bigquery.js');
101
101
 
102
- /**
103
- * APIs integration class for Google Ads.
104
- * @const {{
105
- * GoogleAds:!GoogleAds,
106
- * ConversionConfig:!ConversionConfig,
107
- * CustomerMatchConfig: !CustomerMatchConfig,
108
- * CustomerMatchRecord: !CustomerMatchRecord,
109
- * ReportQueryConfig:!ReportQueryConfig,
110
- * }}
111
- */
112
- exports.googleads = require('./google_ads.js');
113
-
114
102
  /**
115
103
  * APIs integration class for Google Ads with Google API library.
116
104
  * @const {{
@@ -146,9 +134,7 @@ exports.measurementprotocolga4 = require('./measurement_protocol_ga4.js');
146
134
  /**
147
135
  * APIs integration class for YouTube.
148
136
  * @const {{
149
- * YouTube:!YouTube,
150
- * ListChannelsConfig: !ListChannelsConfig,
151
- * ListVideosConfig: !ListVideosConfig,
137
+ * YouTube:!YouTube
152
138
  * }}
153
139
  */
154
140
  exports.youtube = require('./youtube.js');
@@ -162,3 +148,12 @@ exports.youtube = require('./youtube.js');
162
148
  * }}
163
149
  */
164
150
  exports.sendgrid = require('./sendgrid.js');
151
+
152
+ /**
153
+ * APIs integration class for Gmail.
154
+ * @const {{
155
+ * Gmail:!Gmail,
156
+ * API_VERSION: string,
157
+ * }}
158
+ */
159
+ exports.gmail = require('./gmail.js');
@@ -18,12 +18,33 @@
18
18
 
19
19
  'use strict';
20
20
 
21
+ const { EmailOptions } = require('./gmail.js');
21
22
  const { RestfulApiBase } = require('./base/restful_api_base.js');
22
23
  const { getLogger } = require('../components/utils.js');
23
24
 
24
25
  const API_VERSION = 'v3';
25
26
  const API_ENDPOINT = 'https://api.sendgrid.com';
26
27
 
28
+ /**
29
+ * Returns a SendGrid email object based on a given RFC 2822 `mailbox` string.
30
+ * For `mailbox` @see https://www.rfc-editor.org/rfc/rfc2822#page-15
31
+ * @param {string} mailbox
32
+ * @return {{name: string, email: string}} A SendGrid email object.
33
+ */
34
+ function getSendGridEmail(mailbox) {
35
+ if (typeof mailbox === 'string') {
36
+ if (mailbox.indexOf('<') > -1) {
37
+ const name = mailbox.substring(0, mailbox.indexOf('<')).trim();
38
+ const email =
39
+ mailbox.substring(mailbox.indexOf('<') + 1, mailbox.indexOf('>'));
40
+ return { name, email };
41
+ } else {
42
+ return { email: mailbox.trim() };
43
+ }
44
+ }
45
+ return mailbox;
46
+ }
47
+
27
48
  /**
28
49
  * SendGrid API access class.
29
50
  * @see https://docs.sendgrid.com/api-reference/how-to-use-the-sendgrid-v3-api/authentication
@@ -48,11 +69,51 @@ class SendGrid extends RestfulApiBase {
48
69
  });
49
70
  }
50
71
 
72
+ /**
73
+ * Generates an email object for Sendgrid.
74
+ * @param {!EmailOptions} options
75
+ * @return {object}
76
+ * @see https://www.twilio.com/docs/sendgrid/api-reference/mail-send/mail-send#reques
77
+ * @private
78
+ */
79
+ getEmail_(options = {}) {
80
+ const {
81
+ from,
82
+ 'reply-to': reply_to,
83
+ subject,
84
+ content,
85
+ type = 'text/plain',
86
+ } = options;
87
+ const recipientObject = {};
88
+ ['to', 'cc', 'bcc'].forEach((key) => {
89
+ if (options[key]) {
90
+ recipientObject[key] = [options[key]].flat().map(getSendGridEmail);
91
+ }
92
+ });
93
+ const email = {
94
+ subject,
95
+ content: [{ type, value: content }],
96
+ personalizations: [recipientObject],
97
+ };
98
+ if (from) email.from = getSendGridEmail(from);
99
+ //@see https://www.twilio.com/docs/sendgrid/api-reference/mail-send/mail-send#reques
100
+ if (reply_to) {
101
+ if (Array.isArray(reply_to)) {
102
+ email.reply_to_list = reply_to.map(getSendGridEmail);
103
+ } else {
104
+ email.reply_to = getSendGridEmail(reply_to);
105
+ }
106
+ }
107
+ return email;
108
+ }
109
+
51
110
  /**
52
111
  * Sends an email.
112
+ * @param {!EmailOptions} options
53
113
  * @see https://docs.sendgrid.com/api-reference/mail-send/mail-send
54
114
  */
55
- async sendMail(email) {
115
+ async sendMail(options) {
116
+ const email = this.getEmail_(options);
56
117
  const response = await this.request('mail/send', 'POST', email);
57
118
  return response;
58
119
  }
@@ -109,21 +109,73 @@ class Spreadsheets extends GoogleApiClient {
109
109
  }
110
110
 
111
111
  /**
112
- * Clears the content of the Sheet.
113
- * see:
114
- * https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear
112
+ * Returns whether the sheet with speicified name exists.
113
+ * @param {string} sheetName
114
+ * @return {boolean}
115
+ */
116
+ async hasSheet(sheetName) {
117
+ const sheets = await this.getApiClient();
118
+ const spreadsheet = await sheets.spreadsheets.get(
119
+ { spreadsheetId: this.spreadsheetId });
120
+ return spreadsheet.data.sheets.some(
121
+ ({ properties: { title } }) => title === sheetName);
122
+ }
123
+
124
+ /**
125
+ * Creates a sheet with the gvien name.
126
+ *
127
+ * @param {string} sheetName
128
+ */
129
+ async createSheet(sheetName) {
130
+ const sheets = await this.getApiClient();
131
+ await sheets.spreadsheets.batchUpdate({
132
+ spreadsheetId: this.spreadsheetId,
133
+ requestBody: {
134
+ requests: [{ addSheet: { properties: { title: sheetName } } }],
135
+ }
136
+ });
137
+ }
138
+
139
+ /**
140
+ * Deletes a sheet with the gvien name.
141
+ *
142
+ * @param {string} sheetName
143
+ */
144
+ async deleteSheet(sheetName) {
145
+ if (await this.hasSheet(sheetName)) {
146
+ const sheetId = await this.getSheetId(sheetName);
147
+ const sheets = await this.getApiClient();
148
+ await sheets.spreadsheets.batchUpdate({
149
+ spreadsheetId: this.spreadsheetId,
150
+ requestBody: {
151
+ requests: [{ deleteSheet: { sheetId } }],
152
+ }
153
+ });
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Clears the content of the Sheet. If the Sheet doesn't exist, then creates
159
+ * an empty sheet with the sheetName..
160
+ * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear
115
161
  * @param {string} sheetName Name of the Sheet.
116
162
  */
117
163
  async clearSheet(sheetName) {
118
- const request = {
119
- spreadsheetId: this.spreadsheetId,
120
- range: sheetName,
121
- };
122
164
  try {
123
- const sheets = await this.getApiClient();
124
- const response = await sheets.spreadsheets.values.clear(request);
125
- const data = response.data;
126
- this.logger.debug(`Clear sheet[${sheetName}}]: `, data);
165
+ const hasSheet = await this.hasSheet(sheetName);
166
+ if (!hasSheet) {
167
+ await this.createSheet(sheetName);
168
+ this.logger.debug(`Create sheet[${sheetName}]`);
169
+ } else {
170
+ const sheets = await this.getApiClient();
171
+ const request = {
172
+ spreadsheetId: this.spreadsheetId,
173
+ range: sheetName,
174
+ };
175
+ const response = await sheets.spreadsheets.values.clear(request);
176
+ const data = response.data;
177
+ this.logger.debug(`Clear sheet[${sheetName}]: `, data);
178
+ }
127
179
  } catch (error) {
128
180
  this.logger.error(error.toString());
129
181
  throw error;