Skip to content

Commit

Permalink
Add method for getting credit limits
Browse files Browse the repository at this point in the history
Add get limits method
  • Loading branch information
clostao authored Dec 24, 2024
2 parents c1f6bcd + ca2993d commit e93699e
Show file tree
Hide file tree
Showing 3 changed files with 67 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
}
17 changes: 17 additions & 0 deletions packages/auto-drive/src/api/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { apiCalls } from './calls/index'
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
Expand Down Expand Up @@ -375,3 +376,19 @@ export const downloadFile = async (

return iterable
}

export const getPendingCredits = async (
api: AutoDriveApi,
): Promise<{ upload: number; download: number }> => {
const me = await apiCalls.getMe(api)
return {
upload: me.subscription.pendingUploadCredits,
download: me.subscription.pendingDownloadCredits,
}
}

export const getSubscriptionInfo = async (api: AutoDriveApi): Promise<SubscriptionInfo> => {
const me = await apiCalls.getMe(api)

return me.subscription
}

0 comments on commit e93699e

Please sign in to comment.