Skip to content

Commit e93699e

Browse files
authored
Add method for getting credit limits
Add get limits method
2 parents c1f6bcd + ca2993d commit e93699e

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

packages/auto-drive/src/api/calls/read.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ArgsWithoutPagination, ArgsWithPagination } from '../../utils/types'
22
import { AutoDriveApi } from '../connection'
33
import { PaginatedResult } from '../models/common'
44
import { ObjectInformation, ObjectSummary, Scope } from '../models/objects'
5+
import { UserInfo } from '../models/user'
56

67
/**
78
* Retrieves the root objects based on the specified scope.
@@ -193,3 +194,21 @@ export const getObjectMetadata = async (
193194

194195
return response.json()
195196
}
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

packages/auto-drive/src/api/wrappers.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { apiCalls } from './calls/index'
55
import { AutoDriveApi } from './connection'
66
import { GenericFile, GenericFileWithinFolder } from './models/file'
77
import { constructFromInput, constructZipBlobFromTreeAndPaths } from './models/folderTree'
8+
import { SubscriptionInfo } from './models/user'
89

910
export type UploadFileOptions = {
1011
password?: string
@@ -375,3 +376,19 @@ export const downloadFile = async (
375376

376377
return iterable
377378
}
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+
}

0 commit comments

Comments
 (0)