@cooperation/vc-storage 1.0.17 → 1.0.18

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.
@@ -59,8 +59,7 @@ export class GoogleDriveStorage {
59
59
  headers: {}, // Add additional headers if required
60
60
  url,
61
61
  });
62
- console.log(`Content fetched for file ID: ${fileId}`);
63
- return response; // This could be text, JSON, or binary, depending on the file type
62
+ return response;
64
63
  }
65
64
  catch (error) {
66
65
  console.error(`Error fetching content for file ID: ${fileId}:`, error.message);
@@ -133,18 +132,6 @@ export class GoogleDriveStorage {
133
132
  body: formData,
134
133
  url: `https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,parents`,
135
134
  });
136
- // Set the file permission to "Anyone with the link" can view
137
- const permissionUrl = `https://www.googleapis.com/drive/v3/files/${file.id}/permissions`;
138
- const permissionData = {
139
- role: 'reader',
140
- type: 'anyone', // Public access
141
- };
142
- await this.fetcher({
143
- method: 'POST',
144
- url: permissionUrl,
145
- headers: {},
146
- body: JSON.stringify(permissionData),
147
- });
148
135
  return file;
149
136
  }
150
137
  catch (error) {
@@ -161,21 +148,6 @@ export class GoogleDriveStorage {
161
148
  const metadataUrl = `https://www.googleapis.com/drive/v3/files/${id}?fields=id,name`;
162
149
  const dataUrl = `https://www.googleapis.com/drive/v3/files/${id}?alt=media`;
163
150
  try {
164
- // Initial "touch" request to ensure file accessibility for the current user
165
- const touchResponse = await fetch(metadataUrl, {
166
- method: 'GET',
167
- headers: {
168
- Authorization: `Bearer ${this.accessToken}`,
169
- },
170
- });
171
- if (!touchResponse.ok) {
172
- const errorData = await touchResponse.json();
173
- console.error(`Failed to "touch" file for accessibility with ID ${id}:`, errorData);
174
- return null;
175
- }
176
- // Fetch file metadata to get the name
177
- const metadata = await touchResponse.json();
178
- const fileName = metadata.name;
179
151
  // Fetch actual file data
180
152
  const dataResponse = await fetch(dataUrl, {
181
153
  method: 'GET',
@@ -194,9 +166,6 @@ export class GoogleDriveStorage {
194
166
  if (contentType?.includes('application/json')) {
195
167
  fileData = await dataResponse.json();
196
168
  }
197
- else if (contentType?.includes('text') || contentType?.includes('image/svg+xml')) {
198
- fileData = await dataResponse.text(); // Fetch SVG files as text for easy manipulation
199
- }
200
169
  else if (contentType?.includes('image') ||
201
170
  contentType?.includes('video') ||
202
171
  contentType?.includes('audio') ||
@@ -209,10 +178,9 @@ export class GoogleDriveStorage {
209
178
  fileData = await dataResponse.blob();
210
179
  }
211
180
  else {
212
- fileData = await dataResponse.arrayBuffer(); // Fallback for other binary types
181
+ fileData = await dataResponse.arrayBuffer();
213
182
  }
214
- // Return file ID, name, and data
215
- return { id: metadata.id, name: fileName, data: fileData };
183
+ return { data: fileData };
216
184
  }
217
185
  catch (error) {
218
186
  console.error(`Error retrieving file with ID ${id}:`, error.message);
@@ -261,10 +229,12 @@ export class GoogleDriveStorage {
261
229
  // Retrieve all 'VC.json' files from each 'VC-timestamp' subfolder
262
230
  const fileContents = await Promise.all(vcSubfolders.map(async (folder) => {
263
231
  const files = await this.findFilesUnderFolder(folder.id);
232
+ console.log('🚀 ~ GoogleDriveStorage ~ vcSubfolders.map ~ files:', files);
264
233
  return Promise.all(files.map(async (file) => {
265
234
  return await this.retrieve(file.id);
266
235
  }));
267
236
  }));
237
+ console.log('🚀 ~ GoogleDriveStorage ~ getAllFilesByType ~ fileContents:', fileContents);
268
238
  return fileContents;
269
239
  }
270
240
  // Step 3: Generic handling for other types
@@ -327,8 +297,6 @@ export class GoogleDriveStorage {
327
297
  async findFilesUnderFolder(folderId) {
328
298
  if (!folderId)
329
299
  throw new Error('Folder ID is required');
330
- console.log('🚀 ~ GoogleDriveStorage ~ findFilesUnderFolder ~ folderId', folderId);
331
- // Fetch files under the folder
332
300
  const files = await this.searchFiles(`'${folderId}' in parents`);
333
301
  if (files.length === 0) {
334
302
  console.log('No files found in the folder.');
@@ -338,7 +306,7 @@ export class GoogleDriveStorage {
338
306
  const filesWithContent = await Promise.all(files.map(async (file) => {
339
307
  try {
340
308
  const content = await this.getFileContent(file.id);
341
- return { ...file, content }; // Merge file metadata with its content
309
+ return { ...file, content };
342
310
  }
343
311
  catch (error) {
344
312
  console.error(`Error fetching content for file "${file.name}" (ID: ${file.id}):`, error);
@@ -348,22 +316,24 @@ export class GoogleDriveStorage {
348
316
  return filesWithContent;
349
317
  }
350
318
  async updateFileData(fileId, data) {
351
- const fileMetadata = {
352
- name: data.fileName,
353
- mimeType: data.mimeType,
354
- };
355
- let uploadUrl = `https://www.googleapis.com/upload/drive/v3/files/${fileId}?uploadType=multipart`;
356
- const formData = new FormData();
357
- formData.append('metadata', new Blob([JSON.stringify(fileMetadata)], { type: 'application/json' }));
358
- formData.append('file', new Blob([data.body], { type: fileMetadata.mimeType }));
359
- const updatedFile = await this.fetcher({
360
- method: 'PATCH',
361
- headers: {},
362
- body: JSON.stringify(formData),
363
- url: `${uploadUrl}&fields=id,parents`,
364
- });
365
- console.log('File updated:', updatedFile);
366
- return updatedFile;
319
+ try {
320
+ const updateUrl = `https://www.googleapis.com/drive/v3/files/${fileId}`;
321
+ const updatedFile = await this.fetcher({
322
+ method: 'PATCH',
323
+ headers: {
324
+ 'Content-Type': 'application/json',
325
+ },
326
+ body: JSON.stringify({
327
+ name: data.fileName,
328
+ }),
329
+ url: updateUrl,
330
+ });
331
+ console.log('✅ File renamed successfully:', updatedFile);
332
+ return updatedFile;
333
+ }
334
+ catch (error) {
335
+ throw error;
336
+ }
367
337
  }
368
338
  async getFileParents(fileId) {
369
339
  console.log('🚀 ~ GoogleDriveStorage ~ getFileParents ~ fileId', fileId);
@@ -427,21 +397,33 @@ export class GoogleDriveStorage {
427
397
  }
428
398
  }
429
399
  async update(fileId, data) {
430
- const fileMetadata = {
431
- name: data.fileName,
432
- mimeType: data.mimeType,
400
+ console.log('🚀 ~ GoogleDriveStorage ~ update ~ data:', data);
401
+ console.log('🚀 ~ GoogleDriveStorage ~ update ~ fileId:', fileId);
402
+ // ✅ Ensure JSON file type
403
+ const metadata = {
404
+ name: data.fileName || 'resume.json',
405
+ mimeType: 'application/json',
433
406
  };
434
- let uploadUrl = `https://www.googleapis.com/upload/drive/v3/files/${fileId}?uploadType=multipart`;
407
+ const uploadUrl = `https://www.googleapis.com/upload/drive/v3/files/${fileId}?uploadType=multipart`;
408
+ // ✅ Create multipart request to update Google Drive JSON file
435
409
  const formData = new FormData();
436
- formData.append('metadata', new Blob([JSON.stringify(fileMetadata)], { type: 'application/json' }));
437
- formData.append('file', new Blob([data.body], { type: fileMetadata.mimeType }));
438
- const updatedFile = await this.fetcher({
439
- method: 'PATCH',
440
- headers: {},
441
- body: JSON.stringify(formData),
442
- url: `${uploadUrl}&fields=id,parents`,
443
- });
444
- console.log('File updated:', updatedFile);
445
- return updatedFile;
410
+ formData.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
411
+ formData.append('file', new Blob([JSON.stringify(data.body)], { type: 'application/json' }) // ✅ Ensure JSON format
412
+ );
413
+ console.log('🚀 ~ GoogleDriveStorage ~ update ~ FormData:', formData);
414
+ try {
415
+ const response = await this.fetcher({
416
+ method: 'PATCH',
417
+ headers: {}, // ✅ No Content-Type needed, let FormData set it
418
+ body: formData, // ✅ Sends JSON file properly
419
+ url: `${uploadUrl}&fields=id,name,mimeType`,
420
+ });
421
+ console.log('✅ File updated successfully:', response);
422
+ return response;
423
+ }
424
+ catch (error) {
425
+ console.error('❌ Error updating Google Drive file:', error);
426
+ throw error;
427
+ }
446
428
  }
447
429
  }
@@ -1,4 +1,3 @@
1
- import { DataToSaveI } from '../../types';
2
1
  interface FileContent {
3
2
  name: string;
4
3
  content: any;
@@ -44,9 +43,7 @@ export declare class GoogleDriveStorage {
44
43
  * @returns file content
45
44
  */
46
45
  retrieve(id: string): Promise<{
47
- name: string;
48
46
  data: any;
49
- id: string;
50
47
  } | null>;
51
48
  /**
52
49
  * Get folder by folderId, if folderId == null you will have them all
@@ -69,7 +66,9 @@ export declare class GoogleDriveStorage {
69
66
  updateFileName(fileId: string, newFileName: string): Promise<any>;
70
67
  findFileByName(name: string): Promise<any>;
71
68
  findFilesUnderFolder(folderId: string): Promise<any[]>;
72
- updateFileData(fileId: string, data: DataToSaveI): Promise<any>;
69
+ updateFileData(fileId: string, data: {
70
+ fileName: string;
71
+ }): Promise<any>;
73
72
  getFileParents(fileId: string): Promise<any>;
74
73
  updateRelationsFile({ relationsFileId, recommendationFileId }: {
75
74
  relationsFileId: string;
@@ -10,7 +10,9 @@ export declare const getVCWithRecommendations: ({ vcId, storage }: {
10
10
  vcId: string;
11
11
  storage: GoogleDriveStorage;
12
12
  }) => Promise<{
13
- vc: any;
13
+ vc: {
14
+ data: any;
15
+ };
14
16
  recommendations: any[];
15
17
  relationsFileId: any;
16
18
  }>;
@@ -3,15 +3,14 @@ export const getVCWithRecommendations = async ({ vcId, storage }) => {
3
3
  const files = await storage.findFilesUnderFolder(vcFolderId);
4
4
  const relationsFile = files.find((f) => f.name === 'RELATIONS');
5
5
  const relationsContent = await storage.retrieve(relationsFile.id);
6
- const relationsData = JSON.parse(relationsContent.data.body);
6
+ const relationsData = relationsContent.data;
7
7
  const [vcFileId, recommendationIds] = [relationsData.vc_id, relationsData.recommendations];
8
8
  const vc = await storage.retrieve(vcFileId);
9
- const vcData = JSON.parse(vc.data.body);
10
9
  const recommendations = await Promise.all(recommendationIds.map(async (rec) => {
11
10
  const recFile = await storage.retrieve(rec);
12
11
  return recFile;
13
12
  }));
14
- return { vc: vcData, recommendations, relationsFileId: relationsFile.id };
13
+ return { vc: vc, recommendations, relationsFileId: relationsFile.id };
15
14
  };
16
15
  /**
17
16
  * Save data to Google Drive in the specified folder type.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cooperation/vc-storage",
3
3
  "type": "module",
4
- "version": "1.0.17",
4
+ "version": "1.0.18",
5
5
  "description": "Sign and store your verifiable credentials.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/types/index.d.ts",