gdrivekit 1.0.2 → 1.0.4

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.
Files changed (3) hide show
  1. package/README.md +27 -0
  2. package/dist/index.js +148 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -148,6 +148,14 @@ main();
148
148
 
149
149
  ---
150
150
 
151
+ ## Json Operation
152
+ | Method | Description |
153
+ | ----------------------- | ----------------------------------------- |
154
+ | `readJsonFileData()` | Read JSON file content |
155
+ | `addJsonKeyValue()` | Add a new key-value pair to a JSON file |
156
+ | `updateJsonFieldAndValues()` | Update an existing field in a JSON file |
157
+ | `deleteJsonFieldAndKeys()` | Delete a field from a JSON file |
158
+
151
159
  ### ⚡ Example: Upload a File
152
160
 
153
161
  ```ts
@@ -171,6 +179,25 @@ if (files?.data?.files?.length) {
171
179
 
172
180
  ---
173
181
 
182
+ ### ⚡ Example: Add a Key-Value Pair to a JSON File
183
+
184
+ ```ts
185
+ const fileId = "1234567890abcdef1234567890abcdef";
186
+ const addResult = await operations.addJsonKeyValue(
187
+ fileId,
188
+ "alive",
189
+ true
190
+ );
191
+
192
+ if (addResult.success) {
193
+ console.log("✅ Added new key-value pair successfully");
194
+ } else {
195
+ console.error("❌ Failed:", addResult.error);
196
+ }
197
+ ```
198
+
199
+ ---
200
+
174
201
  ### 🧑‍💻 Author
175
202
 
176
203
  **Vikash Khati**
package/dist/index.js CHANGED
@@ -778704,6 +778704,7 @@ var exports_operations = {};
778704
778704
  __export(exports_operations, {
778705
778705
  uploadMultipleFiles: () => uploadMultipleFiles,
778706
778706
  uploadFile: () => uploadFile,
778707
+ updateJsonFieldAndValues: () => updateJsonFieldAndValues,
778707
778708
  updateFile: () => updateFile,
778708
778709
  searchStarredFiles: () => searchStarredFiles,
778709
778710
  searchSharedFiles: () => searchSharedFiles,
@@ -778713,6 +778714,8 @@ __export(exports_operations, {
778713
778714
  searchByExactName: () => searchByExactName,
778714
778715
  searchByContent: () => searchByContent,
778715
778716
  renameFile: () => renameFile,
778717
+ readJsonFileData: () => readJsonFileData,
778718
+ readFileData: () => readFileData,
778716
778719
  moveFileByName: () => moveFileByName,
778717
778720
  moveFile: () => moveFile,
778718
778721
  listRecentFiles: () => listRecentFiles,
@@ -778729,10 +778732,12 @@ __export(exports_operations, {
778729
778732
  downloadMultipleFiles: () => downloadMultipleFiles,
778730
778733
  downloadFile: () => downloadFile,
778731
778734
  deleteMultipleFiles: () => deleteMultipleFiles,
778735
+ deleteJsonFieldAndKeys: () => deleteJsonFieldAndKeys,
778732
778736
  deleteFolder: () => deleteFolder,
778733
778737
  deleteFile: () => deleteFile,
778734
778738
  createFolder: () => createFolder,
778735
- copyFile: () => copyFile
778739
+ copyFile: () => copyFile,
778740
+ addJsonKeyValue: () => addJsonKeyValue
778736
778741
  });
778737
778742
 
778738
778743
  // drivers/services.ts
@@ -778752,6 +778757,7 @@ var MIME_TYPES = {
778752
778757
  POWERPOINT: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
778753
778758
  TEXT: "text/plain",
778754
778759
  CSV: "text/csv",
778760
+ JSON: "application/json",
778755
778761
  JPEG: "image/jpeg",
778756
778762
  PNG: "image/png",
778757
778763
  GIF: "image/gif",
@@ -778765,11 +778771,19 @@ var MIME_TYPES = {
778765
778771
  };
778766
778772
 
778767
778773
  // operations.ts
778774
+ async function readFileData(fileId) {
778775
+ const response = await driveService.readFileData(fileId);
778776
+ if (response.success && response.data) {
778777
+ return typeof response.data === "string" ? response.data : response.data.toString();
778778
+ }
778779
+ throw new Error(response.error || "Failed to read file data");
778780
+ }
778768
778781
  async function uploadFile(filePath, options) {
778769
778782
  return await driveService.uploadFile(filePath, {
778770
778783
  name: options?.fileName,
778771
778784
  parents: options?.folderId ? [options.folderId] : undefined,
778772
- description: options?.description
778785
+ description: options?.description,
778786
+ mimeType: options?.mimeType
778773
778787
  });
778774
778788
  }
778775
778789
  async function downloadFile(fileId, savePath) {
@@ -778792,7 +778806,9 @@ async function moveFile(fileId, newFolderId) {
778792
778806
  if (!file.success || !file.data) {
778793
778807
  return { success: false, error: "File not found" };
778794
778808
  }
778795
- return await driveService.updateFileMetadata(fileId, { parents: [newFolderId] });
778809
+ return await driveService.updateFileMetadata(fileId, {
778810
+ parents: [newFolderId]
778811
+ });
778796
778812
  }
778797
778813
  async function moveFileByName(fileName, folderName) {
778798
778814
  const fileResult = await getFileIdByName(fileName);
@@ -778808,6 +778824,130 @@ async function moveFileByName(fileName, folderName) {
778808
778824
  async function copyFile(fileId, newName) {
778809
778825
  return await driveService.copyFile(fileId, newName ? { name: newName } : undefined);
778810
778826
  }
778827
+ async function readJsonFileData(fileId) {
778828
+ const content = await readFileData(fileId);
778829
+ try {
778830
+ return { success: true, data: JSON.parse(content) };
778831
+ } catch (error) {
778832
+ return { success: false, error: "Failed to parse JSON file content" };
778833
+ }
778834
+ }
778835
+ async function addJsonKeyValue(fileId, key, value) {
778836
+ try {
778837
+ const readResponse = await driveService.readFileData(fileId, true);
778838
+ if (!readResponse.success || !readResponse.data) {
778839
+ throw new Error(readResponse.error || "Failed to read JSON file data");
778840
+ }
778841
+ let jsonData;
778842
+ try {
778843
+ jsonData = JSON.parse(readResponse.data);
778844
+ } catch {
778845
+ throw new Error("Invalid JSON format in file");
778846
+ }
778847
+ const parts = key.split(".");
778848
+ let current = jsonData;
778849
+ for (let i2 = 0;i2 < parts.length - 1; i2++) {
778850
+ const part = parts[i2];
778851
+ if (!current[part] || typeof current[part] !== "object") {
778852
+ current[part] = {};
778853
+ }
778854
+ current = current[part];
778855
+ }
778856
+ current[parts.at(-1)] = value;
778857
+ const updateResponse = await driveService.updateJsonContent(fileId, jsonData);
778858
+ if (!updateResponse.success) {
778859
+ throw new Error(updateResponse.error || "Failed to update file");
778860
+ }
778861
+ return { success: true, data: updateResponse.data };
778862
+ } catch (error) {
778863
+ console.error("❌ Error adding key-value pair:", error.message);
778864
+ return { success: false, error: error.message };
778865
+ }
778866
+ }
778867
+ async function deleteJsonFieldAndKeys(fileId, key) {
778868
+ try {
778869
+ const readResponse = await driveService.readFileData(fileId, true);
778870
+ if (!readResponse.success || !readResponse.data) {
778871
+ throw new Error(readResponse.error || "Failed to read JSON file data");
778872
+ }
778873
+ let jsonData;
778874
+ try {
778875
+ jsonData = JSON.parse(readResponse.data);
778876
+ } catch {
778877
+ throw new Error("Invalid JSON format in file");
778878
+ }
778879
+ const parts = key.split(".");
778880
+ let current = jsonData;
778881
+ for (let i2 = 0;i2 < parts.length - 1; i2++) {
778882
+ const part = parts[i2];
778883
+ if (!current[part] || typeof current[part] !== "object") {
778884
+ throw new Error(`Key path '${key}' does not exist in JSON`);
778885
+ }
778886
+ current = current[part];
778887
+ }
778888
+ const lastKey = parts.at(-1);
778889
+ if (!(lastKey in current)) {
778890
+ throw new Error(`Key '${lastKey}' does not exist`);
778891
+ }
778892
+ delete current[lastKey];
778893
+ const updateResponse = await driveService.updateJsonContent(fileId, jsonData);
778894
+ if (!updateResponse.success) {
778895
+ throw new Error(updateResponse.error || "Failed to update file");
778896
+ }
778897
+ return { success: true, data: updateResponse.data };
778898
+ } catch (error) {
778899
+ console.error("❌ Error deleting key-value pair:", error.message);
778900
+ return { success: false, error: error.message };
778901
+ }
778902
+ }
778903
+ async function updateJsonFieldAndValues(fileId, keyPath, newKey, newValue) {
778904
+ try {
778905
+ const readResponse = await driveService.readFileData(fileId, true);
778906
+ if (!readResponse.success || !readResponse.data) {
778907
+ throw new Error(readResponse.error || "Failed to read JSON file data");
778908
+ }
778909
+ let jsonData;
778910
+ try {
778911
+ jsonData = JSON.parse(readResponse.data);
778912
+ } catch {
778913
+ throw new Error("Invalid JSON format in file");
778914
+ }
778915
+ const parts = keyPath.split(".");
778916
+ let current = jsonData;
778917
+ for (let i2 = 0;i2 < parts.length - 1; i2++) {
778918
+ const part = parts[i2];
778919
+ if (!current[part] || typeof current[part] !== "object") {
778920
+ throw new Error(`Key path '${keyPath}' does not exist in JSON`);
778921
+ }
778922
+ current = current[part];
778923
+ }
778924
+ const oldKey = parts.at(-1);
778925
+ if (!(oldKey in current)) {
778926
+ throw new Error(`Key '${oldKey}' does not exist`);
778927
+ }
778928
+ const existingValue = current[oldKey];
778929
+ const finalValue = newValue !== undefined ? newValue : existingValue;
778930
+ const finalKey = newKey && newKey !== oldKey ? newKey : oldKey;
778931
+ const rebuilt = {};
778932
+ for (const k of Object.keys(current)) {
778933
+ if (k === oldKey) {
778934
+ rebuilt[finalKey] = finalValue;
778935
+ } else {
778936
+ rebuilt[k] = current[k];
778937
+ }
778938
+ }
778939
+ Object.keys(current).forEach((k) => delete current[k]);
778940
+ Object.assign(current, rebuilt);
778941
+ const updateResponse = await driveService.updateJsonContent(fileId, jsonData);
778942
+ if (!updateResponse.success) {
778943
+ throw new Error(updateResponse.error || "Failed to update file");
778944
+ }
778945
+ return { success: true, data: updateResponse.data };
778946
+ } catch (error) {
778947
+ console.error("❌ Error updating JSON key-value:", error.message);
778948
+ return { success: false, error: error.message };
778949
+ }
778950
+ }
778811
778951
  async function createFolder(folderName, parentFolderId) {
778812
778952
  return await driveService.createFolder(folderName, parentFolderId);
778813
778953
  }
@@ -778949,6 +779089,7 @@ async function getFileIdByName(fileName) {
778949
779089
  return { success: false, error: "File not found" };
778950
779090
  }
778951
779091
  var driveOperations = {
779092
+ readFileData,
778952
779093
  uploadFile,
778953
779094
  downloadFile,
778954
779095
  deleteFile,
@@ -778958,6 +779099,10 @@ var driveOperations = {
778958
779099
  moveFile,
778959
779100
  moveFileByName,
778960
779101
  copyFile,
779102
+ readJsonFileData,
779103
+ addJsonKeyValue,
779104
+ updateJsonFieldAndValues,
779105
+ deleteJsonFieldAndKeys,
778961
779106
  createFolder,
778962
779107
  deleteFolder,
778963
779108
  listAllFolders,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdrivekit",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "A lightweight Google Drive toolkit for File Management. Handle Google Drive operations easily — upload, download, and other file operations in a few lines of code.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",