Skip to content

Commit

Permalink
feat: get my limits
Browse files Browse the repository at this point in the history
  • Loading branch information
clostao committed Dec 24, 2024
1 parent 7580913 commit 5257650
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/auto-drive/src/api/calls/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<UserInfo>} - A promise that resolves to the user info.
* @throws {Error} - Throws an error if the request fails.
*/
export const getMe = async (api: AutoDriveApi): Promise<UserInfo> => {
const response = await api.sendRequest('@me', {
method: 'GET',
})
if (!response.ok) {
throw new Error(`Failed to get limits: ${response.statusText}`)
}

return response.json()
}
31 changes: 31 additions & 0 deletions packages/auto-drive/src/api/models/user.ts
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions packages/auto-drive/src/api/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
}
}

0 comments on commit 5257650

Please sign in to comment.