@autonomys/auto-drive 1.2.0 → 1.2.1

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.
@@ -50,6 +50,7 @@ export declare const apiCalls: {
50
50
  getObjectMetadata: (api: import("..").AutoDriveApi, query: import("../../utils").ArgsWithoutPagination<{
51
51
  cid: string;
52
52
  }>) => Promise<import("..").ObjectInformation["metadata"]>;
53
+ getMe: (api: import("..").AutoDriveApi) => Promise<import("../models/user").UserInfo>;
53
54
  downloadObject: (api: import("..").AutoDriveApi, query: import("../../utils").ArgsWithoutPagination<{
54
55
  cid: string;
55
56
  }>) => Promise<ReadableStream<Uint8Array>>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/api/calls/index.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,QAAQ;;;;;;;;;;;;gBAMg0B,CAAC;;;;;;qBAAgvC,CAAC;;;;;gBAAkiD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CADzmH,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/api/calls/index.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,QAAQ;;;;;;;;;;;;gBAMg0B,CAAC;;;;;;qBAAgvC,CAAC;;;;;gBAAkiD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CADzmH,CAAA"}
@@ -2,6 +2,7 @@ import { ArgsWithoutPagination, ArgsWithPagination } from '../../utils/types';
2
2
  import { AutoDriveApi } from '../connection';
3
3
  import { PaginatedResult } from '../models/common';
4
4
  import { ObjectInformation, ObjectSummary, Scope } from '../models/objects';
5
+ import { UserInfo } from '../models/user';
5
6
  /**
6
7
  * Retrieves the root objects based on the specified scope.
7
8
  *
@@ -96,4 +97,12 @@ export declare const getObjectOwners: (api: AutoDriveApi, query: ArgsWithoutPagi
96
97
  export declare const getObjectMetadata: (api: AutoDriveApi, query: ArgsWithoutPagination<{
97
98
  cid: string;
98
99
  }>) => Promise<ObjectInformation["metadata"]>;
100
+ /**
101
+ * Get upload and download limits of the user
102
+ *
103
+ * @param {AutoDriveApi} api - The API instance used to send requests.
104
+ * @returns {Promise<UserInfo>} - A promise that resolves to the user info.
105
+ * @throws {Error} - Throws an error if the request fails.
106
+ */
107
+ export declare const getMe: (api: AutoDriveApi) => Promise<UserInfo>;
99
108
  //# sourceMappingURL=read.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../../../src/api/calls/read.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC7E,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAClD,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAE3E;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,QACd,YAAY,SACV,kBAAkB,CAAC;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC,KAC1C,OAAO,CAAC,eAAe,CAAC,aAAa,CAAC,CAaxC,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,QACrB,YAAY,SACV,kBAAkB,KACxB,OAAO,CAAC,eAAe,CAAC,aAAa,CAAC,CAaxC,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,QAChB,YAAY,SACV,kBAAkB,KACxB,OAAO,CAAC,eAAe,CAAC,aAAa,CAAC,CAaxC,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,QACf,YAAY,SACV,qBAAqB,CAAC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,KAC5C,OAAO,CAAC,iBAAiB,CAU3B,CAAA;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,q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V,kBAAkB,KACxB,OAAO,CAAC,eAAe,CAAC,aAAa,CAAC,CAaxC,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,QACf,YAAY,SACV,qBAAqB,CAAC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,KAC5C,OAAO,CAAC,iBAAiB,CAU3B,CAAA;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,qBAAqB,QAC3B,YAAY,SACV,qBAAqB,CAAC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,KAC5C,OAAO,CAAC,iBAAiB,CAAC,cAAc,CAAC,CAU3C,CAAA;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,eAAe,QACrB,YAAY,SACV,qBAAqB,CAAC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,KAC5C,OAAO,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAUrC,CAAA;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,iBAAiB,QACvB,YAAY,SACV,qBAAqB,CAAC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,KAC5C,OAAO,CAAC,iBAAiB,CAAC,UAAU,CAAC,CAUvC,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,QAAe,YAAY,KAAG,OAAO,CAAC,QAAQ,CAS/D,CAAA"}
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getObjectMetadata = exports.getObjectOwners = exports.getObjectUploadStatus = exports.getObject = exports.getDeleted = exports.getSharedWithMe = exports.getRoots = void 0;
12
+ exports.getMe = exports.getObjectMetadata = exports.getObjectOwners = exports.getObjectUploadStatus = exports.getObject = exports.getDeleted = exports.getSharedWithMe = exports.getRoots = void 0;
13
13
  /**
14
14
  * Retrieves the root objects based on the specified scope.
15
15
  *
@@ -157,3 +157,20 @@ const getObjectMetadata = (api, query) => __awaiter(void 0, void 0, void 0, func
157
157
  return response.json();
158
158
  });
159
159
  exports.getObjectMetadata = getObjectMetadata;
160
+ /**
161
+ * Get upload and download limits of the user
162
+ *
163
+ * @param {AutoDriveApi} api - The API instance used to send requests.
164
+ * @returns {Promise<UserInfo>} - A promise that resolves to the user info.
165
+ * @throws {Error} - Throws an error if the request fails.
166
+ */
167
+ const getMe = (api) => __awaiter(void 0, void 0, void 0, function* () {
168
+ const response = yield api.sendRequest('@me', {
169
+ method: 'GET',
170
+ });
171
+ if (!response.ok) {
172
+ throw new Error(`Failed to get limits: ${response.statusText}`);
173
+ }
174
+ return response.json();
175
+ });
176
+ exports.getMe = getMe;
@@ -0,0 +1,28 @@
1
+ export type SubscriptionGranularity = 'monthly';
2
+ export type SubscriptionInfo = {
3
+ id: string;
4
+ organizationId: string;
5
+ uploadLimit: number;
6
+ downloadLimit: number;
7
+ granularity: SubscriptionGranularity;
8
+ pendingUploadCredits: number;
9
+ pendingDownloadCredits: number;
10
+ };
11
+ export declare enum UserRole {
12
+ User = "User",
13
+ Admin = "Admin"
14
+ }
15
+ export type User = {
16
+ oauthProvider: string;
17
+ oauthUserId: string;
18
+ role: UserRole;
19
+ downloadCredits: number;
20
+ uploadCredits: number;
21
+ publicId: string;
22
+ onboarded: true;
23
+ };
24
+ export type UserInfo = {
25
+ user: User;
26
+ subscription: SubscriptionInfo;
27
+ };
28
+ //# sourceMappingURL=user.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../../src/api/models/user.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,uBAAuB,GAAG,SAAS,CAAA;AAE/C,MAAM,MAAM,gBAAgB,GAAG;IAC7B,EAAE,EAAE,MAAM,CAAA;IACV,cAAc,EAAE,MAAM,CAAA;IACtB,WAAW,EAAE,MAAM,CAAA;IACnB,aAAa,EAAE,MAAM,CAAA;IACrB,WAAW,EAAE,uBAAuB,CAAA;IACpC,oBAAoB,EAAE,MAAM,CAAA;IAC5B,sBAAsB,EAAE,MAAM,CAAA;CAC/B,CAAA;AAED,oBAAY,QAAQ;IAClB,IAAI,SAAS;IACb,KAAK,UAAU;CAChB;AAED,MAAM,MAAM,IAAI,GAAG;IACjB,aAAa,EAAE,MAAM,CAAA;IACrB,WAAW,EAAE,MAAM,CAAA;IACnB,IAAI,EAAE,QAAQ,CAAA;IACd,eAAe,EAAE,MAAM,CAAA;IACvB,aAAa,EAAE,MAAM,CAAA;IACrB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,IAAI,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,IAAI,CAAA;IACV,YAAY,EAAE,gBAAgB,CAAA;CAC/B,CAAA"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserRole = void 0;
4
+ var UserRole;
5
+ (function (UserRole) {
6
+ UserRole["User"] = "User";
7
+ UserRole["Admin"] = "Admin";
8
+ })(UserRole || (exports.UserRole = UserRole = {}));
@@ -1,5 +1,6 @@
1
1
  import { AutoDriveApi } from './connection';
2
2
  import { GenericFile, GenericFileWithinFolder } from './models/file';
3
+ import { SubscriptionInfo } from './models/user';
3
4
  export type UploadFileOptions = {
4
5
  password?: string;
5
6
  compression?: boolean;
@@ -37,10 +38,26 @@ export declare const uploadFileFromInput: (api: AutoDriveApi, file: File, option
37
38
  * @param {string} [options.password] - The password for encryption (optional).
38
39
  * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
39
40
  * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
40
- * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
41
+ * @returns {Promise<string>} - The CID of the uploaded file.
41
42
  * @throws {Error} - Throws an error if the upload fails at any stage.
42
43
  */
43
44
  export declare const uploadFile: (api: AutoDriveApi, file: GenericFile, options?: UploadFileOptions, uploadChunkSize?: number) => Promise<string>;
45
+ /**
46
+ * Uploads an object as a JSON file to the server.
47
+ *
48
+ * This function serializes the provided object to a JSON string,
49
+ * and then uploads the JSON string as a file to the server.
50
+ *
51
+ * @param {AutoDriveApi} api - The API instance used to send requests.
52
+ * @param {File | GenericFile} file - The file to be uploaded, which can be a File or a GenericFile.
53
+ * @param {UploadFileOptions} options - Options for the upload process.
54
+ * @param {string} [options.password] - The password for encryption (optional).
55
+ * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
56
+ * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
57
+ * @returns {Promise<string>} - The CID of the uploaded file.
58
+ * @throws {Error} - Throws an error if the upload fails at any stage.
59
+ */
60
+ export declare const uploadObjectAsJSON: (api: AutoDriveApi, object: unknown, name?: string | undefined, options?: UploadFileOptions, uploadChunkSize?: number) => Promise<string>;
44
61
  /**
45
62
  * Uploads an entire folder to the server.
46
63
  *
@@ -81,4 +98,9 @@ export declare const uploadFileWithinFolderUpload: (api: AutoDriveApi, uploadId:
81
98
  * @returns {Promise<ReadableStream<Uint8Array>>} A promise that resolves to a ReadableStream of the downloaded file.
82
99
  */
83
100
  export declare const downloadFile: (api: AutoDriveApi, cid: string, password?: string) => Promise<AsyncIterable<Buffer>>;
101
+ export declare const getPendingCredits: (api: AutoDriveApi) => Promise<{
102
+ upload: number;
103
+ download: number;
104
+ }>;
105
+ export declare const getSubscriptionInfo: (api: AutoDriveApi) => Promise<SubscriptionInfo>;
84
106
  //# sourceMappingURL=wrappers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wrappers.d.ts","sourceRoot":"","sources":["../../src/api/wrappers.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAGpE,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,W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AAED,eAAO,MAAM,iBAAiB,QACvB,YAAY,KAChB,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAM9C,CAAA;AAED,eAAO,MAAM,mBAAmB,QAAe,YAAY,KAAG,OAAO,CAAC,gBAAgB,CAIrF,CAAA"}
@@ -19,7 +19,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
19
19
  return (mod && mod.__esModule) ? mod : { "default": mod };
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.downloadFile = exports.uploadFileWithinFolderUpload = exports.uploadFolderFromInput = exports.uploadFile = exports.uploadFileFromInput = void 0;
22
+ exports.getSubscriptionInfo = exports.getPendingCredits = exports.downloadFile = exports.uploadFileWithinFolderUpload = exports.uploadFolderFromInput = exports.uploadObjectAsJSON = exports.uploadFile = exports.uploadFileFromInput = void 0;
23
23
  const mime_types_1 = __importDefault(require("mime-types"));
24
24
  const async_1 = require("../utils/async");
25
25
  const misc_1 = require("../utils/misc");
@@ -131,7 +131,7 @@ exports.uploadFileFromInput = uploadFileFromInput;
131
131
  * @param {string} [options.password] - The password for encryption (optional).
132
132
  * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
133
133
  * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
134
- * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
134
+ * @returns {Promise<string>} - The CID of the uploaded file.
135
135
  * @throws {Error} - Throws an error if the upload fails at any stage.
136
136
  */
137
137
  const uploadFile = (api_1, file_1, ...args_1) => __awaiter(void 0, [api_1, file_1, ...args_1], void 0, function* (api, file, options = {}, uploadChunkSize) {
@@ -175,6 +175,36 @@ const uploadFile = (api_1, file_1, ...args_1) => __awaiter(void 0, [api_1, file_
175
175
  return result.cid;
176
176
  });
177
177
  exports.uploadFile = uploadFile;
178
+ /**
179
+ * Uploads an object as a JSON file to the server.
180
+ *
181
+ * This function serializes the provided object to a JSON string,
182
+ * and then uploads the JSON string as a file to the server.
183
+ *
184
+ * @param {AutoDriveApi} api - The API instance used to send requests.
185
+ * @param {File | GenericFile} file - The file to be uploaded, which can be a File or a GenericFile.
186
+ * @param {UploadFileOptions} options - Options for the upload process.
187
+ * @param {string} [options.password] - The password for encryption (optional).
188
+ * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
189
+ * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
190
+ * @returns {Promise<string>} - The CID of the uploaded file.
191
+ * @throws {Error} - Throws an error if the upload fails at any stage.
192
+ */
193
+ const uploadObjectAsJSON = (api_1, object_1, name_1, ...args_1) => __awaiter(void 0, [api_1, object_1, name_1, ...args_1], void 0, function* (api, object, name, options = {}, uploadChunkSize) {
194
+ try {
195
+ const json = Buffer.from(JSON.stringify(object));
196
+ return (0, exports.uploadFile)(api, {
197
+ read: () => (0, async_1.bufferToIterable)(json),
198
+ name: name || 'object.json',
199
+ mimeType: 'application/json',
200
+ size: json.length,
201
+ }, options, uploadChunkSize);
202
+ }
203
+ catch (e) {
204
+ throw new Error('Failed to serialize object to JSON');
205
+ }
206
+ });
207
+ exports.uploadObjectAsJSON = uploadObjectAsJSON;
178
208
  /**
179
209
  * Uploads an entire folder to the server.
180
210
  *
@@ -285,3 +315,16 @@ const downloadFile = (api, cid, password) => __awaiter(void 0, void 0, void 0, f
285
315
  return iterable;
286
316
  });
287
317
  exports.downloadFile = downloadFile;
318
+ const getPendingCredits = (api) => __awaiter(void 0, void 0, void 0, function* () {
319
+ const me = yield index_1.apiCalls.getMe(api);
320
+ return {
321
+ upload: me.subscription.pendingUploadCredits,
322
+ download: me.subscription.pendingDownloadCredits,
323
+ };
324
+ });
325
+ exports.getPendingCredits = getPendingCredits;
326
+ const getSubscriptionInfo = (api) => __awaiter(void 0, void 0, void 0, function* () {
327
+ const me = yield index_1.apiCalls.getMe(api);
328
+ return me.subscription;
329
+ });
330
+ exports.getSubscriptionInfo = getSubscriptionInfo;
@@ -2,4 +2,5 @@ import { Buffer } from 'buffer';
2
2
  export declare const asyncByChunk: (asyncIterable: AsyncIterable<Buffer>, chunkSize: number) => AsyncGenerator<Buffer<ArrayBuffer>, void, unknown>;
3
3
  export declare const asyncFromStream: (stream: ReadableStream<Uint8Array>) => AsyncIterable<Buffer>;
4
4
  export declare const fileToIterable: (file: File | Blob) => AsyncIterable<Buffer>;
5
+ export declare const bufferToIterable: (buffer: Buffer) => AsyncIterable<Buffer>;
5
6
  //# sourceMappingURL=async.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/utils/async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,eAAO,MAAM,YAAY,kBACR,aAAa,CAAC,MAAM,CAAC,aACzB,MAAM,uDAelB,CAAA;AAED,eAAO,MAAM,eAAe,WAClB,cAAc,CAAC,UAAU,CAAC,KACjC,aAAa,CAAC,MAAM,CAOtB,CAAA;AAED,eAAO,MAAM,cAAc,SAA0B,IAAI,GAAG,IAAI,KAAG,aAAa,CAAC,MAAM,CAKtF,CAAA"}
1
+ {"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/utils/async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,eAAO,MAAM,YAAY,kBACR,aAAa,CAAC,MAAM,CAAC,aACzB,MAAM,uDAelB,CAAA;AAED,eAAO,MAAM,eAAe,WAClB,cAAc,CAAC,UAAU,CAAC,KACjC,aAAa,CAAC,MAAM,CAOtB,CAAA;AAED,eAAO,MAAM,cAAc,SAA0B,IAAI,GAAG,IAAI,KAAG,aAAa,CAAC,MAAM,CAKtF,CAAA;AAED,eAAO,MAAM,gBAAgB,WAA4B,MAAM,KAAG,aAAa,CAAC,MAAM,CAErF,CAAA"}
@@ -20,7 +20,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
20
20
  function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.fileToIterable = exports.asyncFromStream = exports.asyncByChunk = void 0;
23
+ exports.bufferToIterable = exports.fileToIterable = exports.asyncFromStream = exports.asyncByChunk = void 0;
24
24
  const buffer_1 = require("buffer");
25
25
  const asyncByChunk = function (asyncIterable, chunkSize) {
26
26
  return __asyncGenerator(this, arguments, function* () {
@@ -71,3 +71,9 @@ const fileToIterable = function (file) {
71
71
  });
72
72
  };
73
73
  exports.fileToIterable = fileToIterable;
74
+ const bufferToIterable = function (buffer) {
75
+ return __asyncGenerator(this, arguments, function* () {
76
+ yield yield __await(buffer);
77
+ });
78
+ };
79
+ exports.bufferToIterable = bufferToIterable;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@autonomys/auto-drive",
3
3
  "packageManager": "yarn@4.2.2",
4
- "version": "1.2.0",
4
+ "version": "1.2.1",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -42,12 +42,12 @@
42
42
  "typescript": "^5.6.3"
43
43
  },
44
44
  "dependencies": {
45
- "@autonomys/auto-dag-data": "^1.2.0",
45
+ "@autonomys/auto-dag-data": "^1.2.1",
46
46
  "jszip": "^3.10.1",
47
47
  "mime-types": "^2.1.35",
48
48
  "process": "^0.11.10",
49
49
  "stream": "^0.0.3",
50
50
  "zod": "^3.23.8"
51
51
  },
52
- "gitHead": "4e00ac77a400bf1ca76b0f9034045f67c815a9cb"
52
+ "gitHead": "72285a024adcc7681ec59fabd1f061a6e40aeb09"
53
53
  }
@@ -2,6 +2,7 @@ import { ArgsWithoutPagination, ArgsWithPagination } from '../../utils/types'
2
2
  import { AutoDriveApi } from '../connection'
3
3
  import { PaginatedResult } from '../models/common'
4
4
  import { ObjectInformation, ObjectSummary, Scope } from '../models/objects'
5
+ import { UserInfo } from '../models/user'
5
6
 
6
7
  /**
7
8
  * Retrieves the root objects based on the specified scope.
@@ -193,3 +194,21 @@ export const getObjectMetadata = async (
193
194
 
194
195
  return response.json()
195
196
  }
197
+
198
+ /**
199
+ * Get upload and download limits of the user
200
+ *
201
+ * @param {AutoDriveApi} api - The API instance used to send requests.
202
+ * @returns {Promise<UserInfo>} - A promise that resolves to the user info.
203
+ * @throws {Error} - Throws an error if the request fails.
204
+ */
205
+ export const getMe = async (api: AutoDriveApi): Promise<UserInfo> => {
206
+ const response = await api.sendRequest('@me', {
207
+ method: 'GET',
208
+ })
209
+ if (!response.ok) {
210
+ throw new Error(`Failed to get limits: ${response.statusText}`)
211
+ }
212
+
213
+ return response.json()
214
+ }
@@ -0,0 +1,31 @@
1
+ export type SubscriptionGranularity = 'monthly'
2
+
3
+ export type SubscriptionInfo = {
4
+ id: string
5
+ organizationId: string
6
+ uploadLimit: number
7
+ downloadLimit: number
8
+ granularity: SubscriptionGranularity
9
+ pendingUploadCredits: number
10
+ pendingDownloadCredits: number
11
+ }
12
+
13
+ export enum UserRole {
14
+ User = 'User',
15
+ Admin = 'Admin',
16
+ }
17
+
18
+ export type User = {
19
+ oauthProvider: string
20
+ oauthUserId: string
21
+ role: UserRole
22
+ downloadCredits: number
23
+ uploadCredits: number
24
+ publicId: string
25
+ onboarded: true
26
+ }
27
+
28
+ export type UserInfo = {
29
+ user: User
30
+ subscription: SubscriptionInfo
31
+ }
@@ -1,10 +1,11 @@
1
1
  import mime from 'mime-types'
2
- import { asyncByChunk, asyncFromStream, fileToIterable } from '../utils/async'
2
+ import { asyncByChunk, asyncFromStream, bufferToIterable, fileToIterable } from '../utils/async'
3
3
  import { progressToPercentage } from '../utils/misc'
4
4
  import { apiCalls } from './calls/index'
5
5
  import { AutoDriveApi } from './connection'
6
6
  import { GenericFile, GenericFileWithinFolder } from './models/file'
7
7
  import { constructFromInput, constructZipBlobFromTreeAndPaths } from './models/folderTree'
8
+ import { SubscriptionInfo } from './models/user'
8
9
 
9
10
  export type UploadFileOptions = {
10
11
  password?: string
@@ -125,7 +126,7 @@ export const uploadFileFromInput = (
125
126
  * @param {string} [options.password] - The password for encryption (optional).
126
127
  * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
127
128
  * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
128
- * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
129
+ * @returns {Promise<string>} - The CID of the uploaded file.
129
130
  * @throws {Error} - Throws an error if the upload fails at any stage.
130
131
  */
131
132
  export const uploadFile = async (
@@ -182,6 +183,46 @@ export const uploadFile = async (
182
183
  return result.cid
183
184
  }
184
185
 
186
+ /**
187
+ * Uploads an object as a JSON file to the server.
188
+ *
189
+ * This function serializes the provided object to a JSON string,
190
+ * and then uploads the JSON string as a file to the server.
191
+ *
192
+ * @param {AutoDriveApi} api - The API instance used to send requests.
193
+ * @param {File | GenericFile} file - The file to be uploaded, which can be a File or a GenericFile.
194
+ * @param {UploadFileOptions} options - Options for the upload process.
195
+ * @param {string} [options.password] - The password for encryption (optional).
196
+ * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
197
+ * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
198
+ * @returns {Promise<string>} - The CID of the uploaded file.
199
+ * @throws {Error} - Throws an error if the upload fails at any stage.
200
+ */
201
+ export const uploadObjectAsJSON = async (
202
+ api: AutoDriveApi,
203
+ object: unknown,
204
+ name?: string | undefined,
205
+ options: UploadFileOptions = {},
206
+ uploadChunkSize?: number,
207
+ ): Promise<string> => {
208
+ try {
209
+ const json = Buffer.from(JSON.stringify(object))
210
+ return uploadFile(
211
+ api,
212
+ {
213
+ read: () => bufferToIterable(json),
214
+ name: name || 'object.json',
215
+ mimeType: 'application/json',
216
+ size: json.length,
217
+ },
218
+ options,
219
+ uploadChunkSize,
220
+ )
221
+ } catch (e) {
222
+ throw new Error('Failed to serialize object to JSON')
223
+ }
224
+ }
225
+
185
226
  /**
186
227
  * Uploads an entire folder to the server.
187
228
  *
@@ -335,3 +376,19 @@ export const downloadFile = async (
335
376
 
336
377
  return iterable
337
378
  }
379
+
380
+ export const getPendingCredits = async (
381
+ api: AutoDriveApi,
382
+ ): Promise<{ upload: number; download: number }> => {
383
+ const me = await apiCalls.getMe(api)
384
+ return {
385
+ upload: me.subscription.pendingUploadCredits,
386
+ download: me.subscription.pendingDownloadCredits,
387
+ }
388
+ }
389
+
390
+ export const getSubscriptionInfo = async (api: AutoDriveApi): Promise<SubscriptionInfo> => {
391
+ const me = await apiCalls.getMe(api)
392
+
393
+ return me.subscription
394
+ }
@@ -36,3 +36,7 @@ export const fileToIterable = async function* (file: File | Blob): AsyncIterable
36
36
  yield Buffer.from(await file.slice(i, i + chunkSize).arrayBuffer())
37
37
  }
38
38
  }
39
+
40
+ export const bufferToIterable = async function* (buffer: Buffer): AsyncIterable<Buffer> {
41
+ yield buffer
42
+ }