@adobe/helix-google-support 1.4.17 → 1.5.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/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ # [1.5.0](https://github.com/adobe/helix-google-support/compare/v1.4.19...v1.5.0) (2022-07-12)
2
+
3
+
4
+ ### Features
5
+
6
+ * allow to specify mimetype ([cb5059d](https://github.com/adobe/helix-google-support/commit/cb5059d9dc575b3f26b73191f4a4bb8bd8b02fce)), closes [#47](https://github.com/adobe/helix-google-support/issues/47)
7
+
8
+ ## [1.4.19](https://github.com/adobe/helix-google-support/compare/v1.4.18...v1.4.19) (2022-07-09)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update dependency @adobe/helix-onedrive-support to v8.1.1 ([c8664be](https://github.com/adobe/helix-google-support/commit/c8664be69362f2be01270acabd58453a94b955d1))
14
+
15
+ ## [1.4.18](https://github.com/adobe/helix-google-support/compare/v1.4.17...v1.4.18) (2022-07-05)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * **deps:** update dependency @adobe/helix-onedrive-support to v8.1.0 ([#45](https://github.com/adobe/helix-google-support/issues/45)) ([4fa4cb4](https://github.com/adobe/helix-google-support/commit/4fa4cb42bfe445a8150ba40cb83e1ef752937ef5))
21
+
1
22
  ## [1.4.17](https://github.com/adobe/helix-google-support/compare/v1.4.16...v1.4.17) (2022-07-05)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-google-support",
3
- "version": "1.4.17",
3
+ "version": "1.5.0",
4
4
  "description": "Helix Google Support",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -31,7 +31,7 @@
31
31
  "reporter-options": "configFile=.mocha-multi.json"
32
32
  },
33
33
  "dependencies": {
34
- "@adobe/helix-onedrive-support": "8.0.12",
34
+ "@adobe/helix-onedrive-support": "8.1.1",
35
35
  "googleapis": "105.0.0",
36
36
  "lru-cache": "7.12.0"
37
37
  },
@@ -25,6 +25,18 @@ declare interface GoogleClientOptions {
25
25
  * Google client
26
26
  */
27
27
  declare class GoogleClient {
28
+ /**
29
+ * Mimetype for documents
30
+ * @value 'application/vnd.google-apps.document'
31
+ */
32
+ static TYPE_DOCUMENT:string;
33
+
34
+ /**
35
+ * Mimetype for spreadsheet
36
+ * @value 'application/vnd.google-apps.spreadsheet'
37
+ */
38
+ static TYPE_SPREADSHEET:string;
39
+
28
40
  /**
29
41
  * Sets the global item cache options. It internally creates a new LRU
30
42
  * with the given options
@@ -64,25 +76,28 @@ declare class GoogleClient {
64
76
  * @param {string} parentId
65
77
  * @param {string[]} pathSegments
66
78
  * @param {string} parentPath
79
+ * @param {string} [type] optional file mimetype filter.
67
80
  * @returns {Promise<DriveItemInfo[]>|null}
68
81
  */
69
- getUncachedItemsFromSegments(parentId:string, pathSegments:string[], parentPath:string):Promise<DriveItemInfo[]>;
82
+ getUncachedItemsFromSegments(parentId:string, pathSegments:string[], parentPath:string, type?:string):Promise<DriveItemInfo[]>;
70
83
 
71
84
  /**
72
85
  * @param {string} parentId
73
86
  * @param {string} pathSegments
74
87
  * @param {string} parentPath
88
+ * @param {string} [type] optional file mimetype filter.
75
89
  * @returns {Promise<DriveItemInfo[]>|null}
76
90
  */
77
- getDriveItemsFromSegments(parentId:string, pathSegments:string[], parentPath:string):Promise<DriveItemInfo[]>;
91
+ getDriveItemsFromSegments(parentId:string, pathSegments:string[], parentPath:string, type?:string):Promise<DriveItemInfo[]>;
78
92
 
79
93
  /**
80
94
  * returns the items hierarchy for the given path and root id, starting with the given path.
81
95
  * @param {string} parentId
82
96
  * @param {string} path
97
+ * @param {string} [type] optional file mimetype filter.
83
98
  * @return {DriveItemInfo[]}
84
99
  */
85
- getDriveItemsFromSegments(parentId:string, path:string):Promise<DriveItemInfo[]>;
100
+ getDriveItemsFromSegments(parentId:string, path:string, type?:string):Promise<DriveItemInfo[]>;
86
101
 
87
102
  /**
88
103
  * returns the items hierarchy for the given item, starting with the given id
@@ -98,9 +113,10 @@ declare class GoogleClient {
98
113
  *
99
114
  * @param {string} parentId
100
115
  * @param {string} path
116
+ * @param {string} [type] optional file mimetype filter.
101
117
  * @returns {Promise<DriveItemInfo>}
102
118
  */
103
- getItemFromPath(parentId:string, path:string):Promise<DriveItemInfo>;
119
+ getItemFromPath(parentId:string, path:string, type?:string):Promise<DriveItemInfo>;
104
120
 
105
121
  /**
106
122
  * Returns an (uncached) file directly via the google api
@@ -116,12 +132,13 @@ declare class GoogleClient {
116
132
  * @param {string} parentId
117
133
  * @param {string} path
118
134
  * @param {boolean} noRetry {@code true} to avoid retry
135
+ * @param {string} [type] optional file mimetype filter.
119
136
  * @returns {Promise<string>|null} The data of the file or {@code null} if the file does not exist
120
137
  */
121
- getFileFromPath(parentId:string, path:string, noRetry:boolean):Promise<string>;
138
+ getFileFromPath(parentId:string, path:string, noRetry:boolean, type?:string):Promise<string>;
122
139
 
123
140
  /**
124
- * Returns an (uncached) document directly via the google api
141
+ * Returns an (uncached) document directly via the google docs api
125
142
  * @param {string} documentId
126
143
  * @returns {object} document
127
144
  */
@@ -56,8 +56,8 @@ function createPathSegments(path) {
56
56
  return pathSegments;
57
57
  }
58
58
 
59
- function getCacheKey(parentId, pathSegments) {
60
- return `${parentId}:${pathSegments.join('/')}`;
59
+ function getCacheKey(parentId, pathSegments, type = 'file') {
60
+ return `${parentId}:${pathSegments.join('/')}:${type}`;
61
61
  }
62
62
 
63
63
  /**
@@ -162,21 +162,31 @@ export class GoogleClient {
162
162
  * @param {string} parentId
163
163
  * @param {string[]} pathSegments
164
164
  * @param {string} parentPath
165
+ * @param {string} [type] optional file mimetype
165
166
  * @returns {Promise<DriveItemInfo[]>|null}
166
167
  */
167
- async getUncachedItemsFromSegments(parentId, pathSegments, parentPath) {
168
+ async getUncachedItemsFromSegments(parentId, pathSegments, parentPath, type) {
168
169
  const { log, drive } = this;
169
170
  const name = pathSegments.shift();
170
171
  const [baseName, ext] = splitByExtension(name);
171
172
  const sanitizedName = sanitizeName(baseName);
172
173
 
173
174
  const items = [];
175
+ let mimeTypeFilter;
176
+ // filter for folder if path continues
177
+ if (pathSegments.length) {
178
+ mimeTypeFilter = 'and mimeType = \'application/vnd.google-apps.folder\'';
179
+ } else {
180
+ mimeTypeFilter = type
181
+ ? `and mimeType = '${type}'`
182
+ : 'and mimeType != \'application/vnd.google-apps.folder\'';
183
+ }
184
+
174
185
  const opts = {
175
186
  q: [
176
187
  `'${parentId}' in parents`,
177
188
  'and trashed=false',
178
- // folder if path continues, sheet otherwise
179
- `and mimeType ${pathSegments.length ? '=' : '!='} 'application/vnd.google-apps.folder'`,
189
+ mimeTypeFilter,
180
190
  ].join(' '),
181
191
  fields: 'nextPageToken, files(id, name, mimeType, modifiedTime)',
182
192
  includeItemsFromAllDrives: true,
@@ -235,7 +245,7 @@ export class GoogleClient {
235
245
  const itemPath = `${parentPath}/${sanitizedName}`;
236
246
  const children = pathSegments.length
237
247
  // eslint-disable-next-line no-use-before-define
238
- ? await this.getDriveItemsFromSegments(item.id, pathSegments, itemPath)
248
+ ? await this.getDriveItemsFromSegments(item.id, pathSegments, itemPath, type)
239
249
  : [];
240
250
 
241
251
  if (!children) {
@@ -262,15 +272,16 @@ export class GoogleClient {
262
272
  * @param {string} parentId
263
273
  * @param {string} pathSegments
264
274
  * @param {string} parentPath
275
+ * @param {string} [type] optional file mimetype
265
276
  * @returns {Promise<DriveItemInfo[]>|null}
266
277
  */
267
- async getDriveItemsFromSegments(parentId, pathSegments, parentPath) {
268
- const key = getCacheKey(parentId, pathSegments);
278
+ async getDriveItemsFromSegments(parentId, pathSegments, parentPath, type) {
279
+ const key = getCacheKey(parentId, pathSegments, type);
269
280
  let items = lru.get(key);
270
281
  if (items) {
271
282
  return items;
272
283
  }
273
- items = await this.getUncachedItemsFromSegments(parentId, pathSegments, parentPath);
284
+ items = await this.getUncachedItemsFromSegments(parentId, pathSegments, parentPath, type);
274
285
  if (items) {
275
286
  lru.set(key, items);
276
287
  // add invalidation function as not-enumerable to keep compatible objects
@@ -289,11 +300,12 @@ export class GoogleClient {
289
300
  * returns the items hierarchy for the given path and root id, starting with the given path.
290
301
  * @param {string} parentId
291
302
  * @param {string} path
303
+ * @param {string} [type] optional file mimetype
292
304
  * @return {DriveItemInfo[]}
293
305
  */
294
- async getItemsFromPath(parentId, path) {
306
+ async getItemsFromPath(parentId, path, type) {
295
307
  const segs = createPathSegments(path);
296
- const result = await this.getDriveItemsFromSegments(parentId, segs, '');
308
+ const result = await this.getDriveItemsFromSegments(parentId, segs, '', type);
297
309
  if (!result) {
298
310
  return [];
299
311
  }
@@ -380,9 +392,10 @@ export class GoogleClient {
380
392
  *
381
393
  * @param {string} parentId the parent id or the id of the item itself, if the path is missing
382
394
  * @param {string} [path]
395
+ * @param {string} [type] optional file mimetype
383
396
  * @returns {Promise<DriveItemInfo>}
384
397
  */
385
- async getItemFromPath(parentId, path) {
398
+ async getItemFromPath(parentId, path, type) {
386
399
  if (!path) {
387
400
  try {
388
401
  let item = lru.get(parentId);
@@ -419,7 +432,7 @@ export class GoogleClient {
419
432
  }
420
433
 
421
434
  const segs = createPathSegments(path);
422
- const items = await this.getDriveItemsFromSegments(parentId, segs, '');
435
+ const items = await this.getDriveItemsFromSegments(parentId, segs, '', type);
423
436
  const item = items?.[0];
424
437
  if (!item) {
425
438
  return null;
@@ -454,10 +467,11 @@ export class GoogleClient {
454
467
  * @param {string} parentId
455
468
  * @param {string} path
456
469
  * @param {boolean} noRetry {@code true} to avoid retry
470
+ * @param {string} [type] optional file mimetype
457
471
  * @returns {Promise<string>|null} The data of the file or {@code null} if the file does not exist
458
472
  */
459
- async getFileFromPath(parentId, path, noRetry) {
460
- const item = await this.getItemFromPath(parentId, path);
473
+ async getFileFromPath(parentId, path, noRetry, type) {
474
+ const item = await this.getItemFromPath(parentId, path, type);
461
475
  if (!item) {
462
476
  return null;
463
477
  }
@@ -479,7 +493,7 @@ export class GoogleClient {
479
493
  }
480
494
 
481
495
  /**
482
- * Returns an (uncached) document directly via the google api
496
+ * Returns an (uncached) document directly via the google docs api
483
497
  * @param {string} documentId
484
498
  * @returns {object} document
485
499
  */
@@ -509,7 +523,7 @@ export class GoogleClient {
509
523
  * @returns {Promise<object>|null} The document or {@code null} if the document does not exist
510
524
  */
511
525
  async getDocumentFromPath(parentId, path, noRetry) {
512
- const item = await this.getItemFromPath(parentId, path);
526
+ const item = await this.getItemFromPath(parentId, path, GoogleClient.TYPE_DOCUMENT);
513
527
  if (!item) {
514
528
  return null;
515
529
  }
@@ -530,3 +544,8 @@ export class GoogleClient {
530
544
  }
531
545
  }
532
546
  }
547
+
548
+ Object.assign(GoogleClient, {
549
+ TYPE_DOCUMENT: 'application/vnd.google-apps.document',
550
+ TYPE_SPREADSHEET: 'application/vnd.google-apps.spreadsheet',
551
+ });