@adobe/helix-google-support 1.1.0 → 1.2.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 +7 -0
- package/package.json +1 -1
- package/src/GoogleClient.d.ts +140 -0
- package/src/GoogleClient.js +77 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.2.0](https://github.com/adobe/helix-google-support/compare/v1.1.0...v1.2.0) (2022-04-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add get file|doc from path ([#9](https://github.com/adobe/helix-google-support/issues/9)) ([4a2e927](https://github.com/adobe/helix-google-support/commit/4a2e9272f4328e936e65b7144d04e516ac602625))
|
|
7
|
+
|
|
1
8
|
# [1.1.0](https://github.com/adobe/helix-google-support/compare/v1.0.1...v1.1.0) (2022-04-28)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
declare interface ICachePlugin {}
|
|
13
|
+
|
|
14
|
+
declare interface DriveItemInfo {}
|
|
15
|
+
|
|
16
|
+
declare interface GoogleClientOptions {
|
|
17
|
+
log:Console;
|
|
18
|
+
clientId:string;
|
|
19
|
+
clientSecret:string;
|
|
20
|
+
redirectUri:string;
|
|
21
|
+
cachePlugin?:ICachePlugin;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Google client
|
|
26
|
+
*/
|
|
27
|
+
declare class GoogleClient {
|
|
28
|
+
/**
|
|
29
|
+
* Sets the global item cache options. It internally creates a new LRU
|
|
30
|
+
* with the given options
|
|
31
|
+
*
|
|
32
|
+
* @param opts
|
|
33
|
+
*/
|
|
34
|
+
static setItemCacheOptions(opts:object);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns a url for a google drive id.
|
|
38
|
+
* @param {string} id
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
static id2Url(id:string):string;
|
|
42
|
+
|
|
43
|
+
constructor(opts:GoogleClientOptions);
|
|
44
|
+
|
|
45
|
+
init():Promise<GoogleClient>;
|
|
46
|
+
|
|
47
|
+
generateAuthUrl(...args):Promise<string>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Sets the credentials
|
|
51
|
+
* @param tokens
|
|
52
|
+
* @returns {Promise<void>}
|
|
53
|
+
*/
|
|
54
|
+
setCredentials(tokens:object):Promise<void>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns the token for the given code
|
|
58
|
+
* @param {string} code
|
|
59
|
+
* @returns {Promise<*>}
|
|
60
|
+
*/
|
|
61
|
+
getToken(code:string):Promise<object>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {string} parentId
|
|
65
|
+
* @param {string[]} pathSegments
|
|
66
|
+
* @param {string} parentPath
|
|
67
|
+
* @returns {Promise<DriveItemInfo[]>|null}
|
|
68
|
+
*/
|
|
69
|
+
getUncachedItemsFromSegments(parentId:string, pathSegments:string[], parentPath:string):Promise<DriveItemInfo[]>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {string} parentId
|
|
73
|
+
* @param {string} pathSegments
|
|
74
|
+
* @param {string} parentPath
|
|
75
|
+
* @returns {Promise<DriveItemInfo[]>|null}
|
|
76
|
+
*/
|
|
77
|
+
getDriveItemsFromSegments(parentId:string, pathSegments:string[], parentPath:string):Promise<DriveItemInfo[]>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* returns the items hierarchy for the given path and root id, starting with the given path.
|
|
81
|
+
* @param {string} parentId
|
|
82
|
+
* @param {string} path
|
|
83
|
+
* @return {DriveItemInfo[]}
|
|
84
|
+
*/
|
|
85
|
+
getDriveItemsFromSegments(parentId:string, path:string):Promise<DriveItemInfo[]>;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* returns the items hierarchy for the given item, starting with the given id
|
|
89
|
+
* @param {string} fileId
|
|
90
|
+
* @param {object} roots
|
|
91
|
+
* @returns {Promise<DriveItemInfo[]>}
|
|
92
|
+
*/
|
|
93
|
+
getItemsFromId(fileId:string, roots:object):Promise<DriveItemInfo[]>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Returns the (cached) item for the given path or {@code null} if the item cannot be found.
|
|
97
|
+
* The item will contains a `invalidate()` method which can be used to remove it from the cache.
|
|
98
|
+
*
|
|
99
|
+
* @param {string} parentId
|
|
100
|
+
* @param {string} path
|
|
101
|
+
* @returns {Promise<DriveItemInfo>}
|
|
102
|
+
*/
|
|
103
|
+
getItemFromPath(parentId:string, path:string):Promise<DriveItemInfo>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns an (uncached) file directly via the google api
|
|
107
|
+
* @param {string} fileId
|
|
108
|
+
* @returns {string} file data
|
|
109
|
+
*/
|
|
110
|
+
getFile(fileId:string):Promise<string>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Fetches the file data from the give path. If the file with the internal id could not be
|
|
114
|
+
* fetched, the item cache is invalidated and the operation is retried. this is to support
|
|
115
|
+
* moved items.
|
|
116
|
+
* @param {string} parentId
|
|
117
|
+
* @param {string} path
|
|
118
|
+
* @param {boolean} noRetry {@code true} to avoid retry
|
|
119
|
+
* @returns {Promise<string>|null} The data of the file or {@code null} if the file does not exist
|
|
120
|
+
*/
|
|
121
|
+
getFileFromPath(parentId:string, path:string, noRetry:boolean):Promise<string>;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns an (uncached) document directly via the google api
|
|
125
|
+
* @param {string} documentId
|
|
126
|
+
* @returns {object} document
|
|
127
|
+
*/
|
|
128
|
+
getDocument(documentId:string):Promise<object>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Fetches the document from the give path. If the document with the internal id could not be
|
|
132
|
+
* fetched, the item cache is invalidated and the operation is retried. this is to support
|
|
133
|
+
* moved items.
|
|
134
|
+
* @param {string} parentId
|
|
135
|
+
* @param {string} path
|
|
136
|
+
* @param {boolean} noRetry {@code true} to avoid retry
|
|
137
|
+
* @returns {Promise<object>|null} The document or {@code null} if the document does not exist
|
|
138
|
+
*/
|
|
139
|
+
getDocumentFromPath(parentId:string, path:string, noRetry:boolean):Promise<object>;
|
|
140
|
+
}
|
package/src/GoogleClient.js
CHANGED
|
@@ -150,12 +150,12 @@ export class GoogleClient {
|
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
/**
|
|
153
|
-
* @param {string[]} pathSegments
|
|
154
153
|
* @param {string} parentId
|
|
154
|
+
* @param {string[]} pathSegments
|
|
155
155
|
* @param {string} parentPath
|
|
156
156
|
* @returns {Promise<DriveItemInfo[]>|null}
|
|
157
157
|
*/
|
|
158
|
-
async getUncachedItemsFromSegments(
|
|
158
|
+
async getUncachedItemsFromSegments(parentId, pathSegments, parentPath) {
|
|
159
159
|
const { log, drive } = this;
|
|
160
160
|
const name = pathSegments.shift();
|
|
161
161
|
const [baseName, ext] = splitByExtension(name);
|
|
@@ -226,7 +226,7 @@ export class GoogleClient {
|
|
|
226
226
|
const itemPath = `${parentPath}/${sanitizedName}`;
|
|
227
227
|
const children = pathSegments.length
|
|
228
228
|
// eslint-disable-next-line no-use-before-define
|
|
229
|
-
? await this.getDriveItemsFromSegments(
|
|
229
|
+
? await this.getDriveItemsFromSegments(item.id, pathSegments, itemPath)
|
|
230
230
|
: [];
|
|
231
231
|
|
|
232
232
|
if (!children) {
|
|
@@ -250,18 +250,18 @@ export class GoogleClient {
|
|
|
250
250
|
}
|
|
251
251
|
|
|
252
252
|
/**
|
|
253
|
-
* @param {string} pathSegments
|
|
254
253
|
* @param {string} parentId
|
|
254
|
+
* @param {string} pathSegments
|
|
255
255
|
* @param {string} parentPath
|
|
256
256
|
* @returns {Promise<DriveItemInfo[]>|null}
|
|
257
257
|
*/
|
|
258
|
-
async getDriveItemsFromSegments(
|
|
258
|
+
async getDriveItemsFromSegments(parentId, pathSegments, parentPath) {
|
|
259
259
|
const key = getCacheKey(parentId, pathSegments);
|
|
260
260
|
let items = lru.get(key);
|
|
261
261
|
if (items) {
|
|
262
262
|
return items;
|
|
263
263
|
}
|
|
264
|
-
items = await this.getUncachedItemsFromSegments(
|
|
264
|
+
items = await this.getUncachedItemsFromSegments(parentId, pathSegments, parentPath);
|
|
265
265
|
if (items) {
|
|
266
266
|
lru.set(key, items);
|
|
267
267
|
// add invalidation function as not-enumerable to keep compatible objects
|
|
@@ -278,20 +278,20 @@ export class GoogleClient {
|
|
|
278
278
|
|
|
279
279
|
/**
|
|
280
280
|
* returns the items hierarchy for the given path and root id, starting with the given path.
|
|
281
|
-
* @param
|
|
282
|
-
* @param
|
|
281
|
+
* @param {string} parentId
|
|
282
|
+
* @param {string} path
|
|
283
283
|
* @return {DriveItemInfo[]}
|
|
284
284
|
*/
|
|
285
|
-
async getItemsFromPath(
|
|
285
|
+
async getItemsFromPath(parentId, path) {
|
|
286
286
|
const segs = createPathSegments(path);
|
|
287
|
-
const result = await this.getDriveItemsFromSegments(
|
|
287
|
+
const result = await this.getDriveItemsFromSegments(parentId, segs, '');
|
|
288
288
|
if (!result) {
|
|
289
289
|
return [];
|
|
290
290
|
}
|
|
291
291
|
return [...result, {
|
|
292
292
|
name: '',
|
|
293
293
|
path: '/',
|
|
294
|
-
id:
|
|
294
|
+
id: parentId,
|
|
295
295
|
}];
|
|
296
296
|
}
|
|
297
297
|
|
|
@@ -299,6 +299,7 @@ export class GoogleClient {
|
|
|
299
299
|
* returns the items hierarchy for the given item, starting with the given id
|
|
300
300
|
* @param {string} fileId
|
|
301
301
|
* @param {object} roots
|
|
302
|
+
* @private
|
|
302
303
|
* @returns {Promise<DriveItemInfo[]>}
|
|
303
304
|
*/
|
|
304
305
|
async getItems(fileId, roots) {
|
|
@@ -366,13 +367,13 @@ export class GoogleClient {
|
|
|
366
367
|
* Returns the (cached) item for the given path or {@code null} if the item cannot be found.
|
|
367
368
|
* The item will contains a `invalidate()` method which can be used to remove it from the cache.
|
|
368
369
|
*
|
|
369
|
-
* @param {string}
|
|
370
|
+
* @param {string} parentId
|
|
370
371
|
* @param {string} path
|
|
371
372
|
* @returns {Promise<DriveItemInfo>}
|
|
372
373
|
*/
|
|
373
|
-
async getItemFromPath(
|
|
374
|
+
async getItemFromPath(parentId, path) {
|
|
374
375
|
const segs = createPathSegments(path);
|
|
375
|
-
const items = await this.getDriveItemsFromSegments(
|
|
376
|
+
const items = await this.getDriveItemsFromSegments(parentId, segs, '');
|
|
376
377
|
const item = items?.[0];
|
|
377
378
|
if (!item) {
|
|
378
379
|
return null;
|
|
@@ -400,6 +401,37 @@ export class GoogleClient {
|
|
|
400
401
|
}
|
|
401
402
|
}
|
|
402
403
|
|
|
404
|
+
/**
|
|
405
|
+
* Fetches the file data from the give path. If the file with the internal id could not be
|
|
406
|
+
* fetched, the item cache is invalidated and the operation is retried. this is to support
|
|
407
|
+
* moved items.
|
|
408
|
+
* @param {string} parentId
|
|
409
|
+
* @param {string} path
|
|
410
|
+
* @param {boolean} noRetry {@code true} to avoid retry
|
|
411
|
+
* @returns {Promise<string>|null} The data of the file or {@code null} if the file does not exist
|
|
412
|
+
*/
|
|
413
|
+
async getFileFromPath(parentId, path, noRetry) {
|
|
414
|
+
const item = await this.getItemFromPath(parentId, path);
|
|
415
|
+
if (!item) {
|
|
416
|
+
return null;
|
|
417
|
+
}
|
|
418
|
+
try {
|
|
419
|
+
// noinspection ES6RedundantAwait (need to catch exception)
|
|
420
|
+
return await this.getFile(item.id);
|
|
421
|
+
} catch (e) {
|
|
422
|
+
if (e.statusCode === 404) {
|
|
423
|
+
if (noRetry) {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
this.log.info(`file ${item.id} does not exist - invalidating cache and retry`);
|
|
427
|
+
item.invalidate();
|
|
428
|
+
return this.getFileFromPath(parentId, path, true);
|
|
429
|
+
} else {
|
|
430
|
+
throw e;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
403
435
|
/**
|
|
404
436
|
* Returns an (uncached) document directly via the google api
|
|
405
437
|
* @param {string} documentId
|
|
@@ -420,4 +452,35 @@ export class GoogleClient {
|
|
|
420
452
|
throw e;
|
|
421
453
|
}
|
|
422
454
|
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Fetches the document from the give path. If the document with the internal id could not be
|
|
458
|
+
* fetched, the item cache is invalidated and the operation is retried. this is to support
|
|
459
|
+
* moved items.
|
|
460
|
+
* @param {string} parentId
|
|
461
|
+
* @param {string} path
|
|
462
|
+
* @param {boolean} noRetry {@code true} to avoid retry
|
|
463
|
+
* @returns {Promise<object>|null} The document or {@code null} if the document does not exist
|
|
464
|
+
*/
|
|
465
|
+
async getDocumentFromPath(parentId, path, noRetry) {
|
|
466
|
+
const item = await this.getItemFromPath(parentId, path);
|
|
467
|
+
if (!item) {
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
try {
|
|
471
|
+
// noinspection ES6RedundantAwait (need to catch exception)
|
|
472
|
+
return await this.getDocument(item.id);
|
|
473
|
+
} catch (e) {
|
|
474
|
+
if (e.statusCode === 404) {
|
|
475
|
+
if (noRetry) {
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
this.log.info(`document ${item.id} does not exist - invalidating cache and retry`);
|
|
479
|
+
item.invalidate();
|
|
480
|
+
return this.getDocumentFromPath(parentId, path, true);
|
|
481
|
+
} else {
|
|
482
|
+
throw e;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
423
486
|
}
|