From 5257650f8a8e4d6d7c433362dc78054bdab495c2 Mon Sep 17 00:00:00 2001 From: Carlos Lostao Date: Tue, 24 Dec 2024 11:43:15 +0100 Subject: [PATCH 1/2] feat: get my limits --- packages/auto-drive/src/api/calls/read.ts | 19 +++++++++++++ packages/auto-drive/src/api/models/user.ts | 31 ++++++++++++++++++++++ packages/auto-drive/src/api/wrappers.ts | 12 +++++++++ 3 files changed, 62 insertions(+) create mode 100644 packages/auto-drive/src/api/models/user.ts diff --git a/packages/auto-drive/src/api/calls/read.ts b/packages/auto-drive/src/api/calls/read.ts index c49852b..6ff8139 100644 --- a/packages/auto-drive/src/api/calls/read.ts +++ b/packages/auto-drive/src/api/calls/read.ts @@ -2,6 +2,7 @@ import { ArgsWithoutPagination, ArgsWithPagination } from '../../utils/types' import { AutoDriveApi } from '../connection' import { PaginatedResult } from '../models/common' import { ObjectInformation, ObjectSummary, Scope } from '../models/objects' +import { UserInfo } from '../models/user' /** * Retrieves the root objects based on the specified scope. @@ -193,3 +194,21 @@ export const getObjectMetadata = async ( return response.json() } + +/** + * Get upload and download limits of the user + * + * @param {AutoDriveApi} api - The API instance used to send requests. + * @returns {Promise} - A promise that resolves to the user info. + * @throws {Error} - Throws an error if the request fails. + */ +export const getMe = async (api: AutoDriveApi): Promise => { + const response = await api.sendRequest('@me', { + method: 'GET', + }) + if (!response.ok) { + throw new Error(`Failed to get limits: ${response.statusText}`) + } + + return response.json() +} diff --git a/packages/auto-drive/src/api/models/user.ts b/packages/auto-drive/src/api/models/user.ts new file mode 100644 index 0000000..3af8a02 --- /dev/null +++ b/packages/auto-drive/src/api/models/user.ts @@ -0,0 +1,31 @@ +export type SubscriptionGranularity = 'monthly' + +export type SubscriptionInfo = { + id: string + organizationId: string + uploadLimit: number + downloadLimit: number + granularity: SubscriptionGranularity + pendingUploadCredits: number + pendingDownloadCredits: number +} + +export enum UserRole { + User = 'User', + Admin = 'Admin', +} + +export type User = { + oauthProvider: string + oauthUserId: string + role: UserRole + downloadCredits: number + uploadCredits: number + publicId: string + onboarded: true +} + +export type UserInfo = { + user: User + subscription: SubscriptionInfo +} diff --git a/packages/auto-drive/src/api/wrappers.ts b/packages/auto-drive/src/api/wrappers.ts index d312caf..ee57e1c 100644 --- a/packages/auto-drive/src/api/wrappers.ts +++ b/packages/auto-drive/src/api/wrappers.ts @@ -2,6 +2,7 @@ import mime from 'mime-types' import { asyncByChunk, asyncFromStream, bufferToIterable, fileToIterable } from '../utils/async' import { progressToPercentage } from '../utils/misc' import { apiCalls } from './calls/index' +import { getMe } from './calls/read' import { AutoDriveApi } from './connection' import { GenericFile, GenericFileWithinFolder } from './models/file' import { constructFromInput, constructZipBlobFromTreeAndPaths } from './models/folderTree' @@ -375,3 +376,14 @@ export const downloadFile = async ( return iterable } + +export const getLimits = async ( + api: AutoDriveApi, +): Promise<{ upload: number; download: number }> => { + const me = await apiCalls.getMe(api) + + return { + upload: me.subscription.uploadLimit, + download: me.subscription.downloadLimit, + } +} From ca2993dca52e6ede28340cd7988963170ad9dd4f Mon Sep 17 00:00:00 2001 From: Carlos Lostao Date: Tue, 24 Dec 2024 12:05:51 +0100 Subject: [PATCH 2/2] feat: add get subcription info --- packages/auto-drive/src/api/wrappers.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/auto-drive/src/api/wrappers.ts b/packages/auto-drive/src/api/wrappers.ts index ee57e1c..a345ba3 100644 --- a/packages/auto-drive/src/api/wrappers.ts +++ b/packages/auto-drive/src/api/wrappers.ts @@ -2,10 +2,10 @@ import mime from 'mime-types' import { asyncByChunk, asyncFromStream, bufferToIterable, fileToIterable } from '../utils/async' import { progressToPercentage } from '../utils/misc' import { apiCalls } from './calls/index' -import { getMe } from './calls/read' import { AutoDriveApi } from './connection' import { GenericFile, GenericFileWithinFolder } from './models/file' import { constructFromInput, constructZipBlobFromTreeAndPaths } from './models/folderTree' +import { SubscriptionInfo } from './models/user' export type UploadFileOptions = { password?: string @@ -377,13 +377,18 @@ export const downloadFile = async ( return iterable } -export const getLimits = async ( +export const getPendingCredits = async ( api: AutoDriveApi, ): Promise<{ upload: number; download: number }> => { const me = await apiCalls.getMe(api) - return { - upload: me.subscription.uploadLimit, - download: me.subscription.downloadLimit, + upload: me.subscription.pendingUploadCredits, + download: me.subscription.pendingDownloadCredits, } } + +export const getSubscriptionInfo = async (api: AutoDriveApi): Promise => { + const me = await apiCalls.getMe(api) + + return me.subscription +}