@adobe/helix-google-support 1.4.19 → 1.5.2
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 +21 -0
- package/package.json +3 -3
- package/src/GoogleClient.d.ts +26 -9
- package/src/GoogleClient.js +39 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## [1.5.2](https://github.com/adobe/helix-google-support/compare/v1.5.1...v1.5.2) (2022-07-16)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **deps:** update dependency @adobe/helix-onedrive-support to v8.1.2 ([ad929d0](https://github.com/adobe/helix-google-support/commit/ad929d093788b4bb0ef241717e21a2b980ab90b9))
|
|
7
|
+
|
|
8
|
+
## [1.5.1](https://github.com/adobe/helix-google-support/compare/v1.5.0...v1.5.1) (2022-07-16)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **deps:** update dependency lru-cache to v7.13.1 ([e01766b](https://github.com/adobe/helix-google-support/commit/e01766b4d9db6a409aa8de680d7eabeee234309d))
|
|
14
|
+
|
|
15
|
+
# [1.5.0](https://github.com/adobe/helix-google-support/compare/v1.4.19...v1.5.0) (2022-07-12)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* 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)
|
|
21
|
+
|
|
1
22
|
## [1.4.19](https://github.com/adobe/helix-google-support/compare/v1.4.18...v1.4.19) (2022-07-09)
|
|
2
23
|
|
|
3
24
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/helix-google-support",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "Helix Google Support",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"reporter-options": "configFile=.mocha-multi.json"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@adobe/helix-onedrive-support": "8.1.
|
|
34
|
+
"@adobe/helix-onedrive-support": "8.1.2",
|
|
35
35
|
"googleapis": "105.0.0",
|
|
36
|
-
"lru-cache": "7.
|
|
36
|
+
"lru-cache": "7.13.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@adobe/eslint-config-helix": "1.3.2",
|
package/src/GoogleClient.d.ts
CHANGED
|
@@ -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
|
|
83
|
-
* @
|
|
97
|
+
* @param {string} [type] optional file mimetype filter.
|
|
98
|
+
* @return {Promise<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,14 +113,15 @@ 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
|
|
107
123
|
* @param {string} fileId
|
|
108
|
-
* @returns {string} file data
|
|
124
|
+
* @returns {Promise<string>} file data
|
|
109
125
|
*/
|
|
110
126
|
getFile(fileId:string):Promise<string>;
|
|
111
127
|
|
|
@@ -116,14 +132,15 @@ 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
|
-
* @returns {object} document
|
|
143
|
+
* @returns {Promise<object>} document
|
|
127
144
|
*/
|
|
128
145
|
getDocument(documentId:string):Promise<object>;
|
|
129
146
|
|
package/src/GoogleClient.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
292
|
-
* @
|
|
303
|
+
* @param {string} [type] optional file mimetype
|
|
304
|
+
* @return {Promise<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;
|
|
@@ -430,7 +443,7 @@ export class GoogleClient {
|
|
|
430
443
|
/**
|
|
431
444
|
* Returns an (uncached) file directly via the google api
|
|
432
445
|
* @param {string} fileId
|
|
433
|
-
* @returns {string} file data
|
|
446
|
+
* @returns {Promise<string>} file data
|
|
434
447
|
*/
|
|
435
448
|
async getFile(fileId) {
|
|
436
449
|
try {
|
|
@@ -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,9 +493,9 @@ 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
|
-
* @returns {object} document
|
|
498
|
+
* @returns {Promise<object>} document
|
|
485
499
|
*/
|
|
486
500
|
async getDocument(documentId) {
|
|
487
501
|
const docs = google.docs({
|
|
@@ -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
|
+
});
|