subscription.ts56 lines, 1.4 KB
| 1 | import { apiClient } from "./client"; |
| 2 | import { |
| 3 | ApiResponse, |
| 4 | CheckoutSessionRequest, |
| 5 | CheckoutSessionResponse, |
| 6 | SubscriptionPlan, |
| 7 | UserSubscriptionResponse, |
| 8 | } from "./types"; |
| 9 | |
| 10 | /** |
| 11 | * Get all available subscription plans |
| 12 | */ |
| 13 | export const getSubscriptionPlans = async (): Promise<SubscriptionPlan[]> => { |
| 14 | const response = await apiClient.get<ApiResponse<SubscriptionPlan[]>>( |
| 15 | "/subscriptions/plans" |
| 16 | ); |
| 17 | return response.data.data; |
| 18 | }; |
| 19 | |
| 20 | /** |
| 21 | * Get the current user's subscription |
| 22 | */ |
| 23 | export const getUserSubscription = |
| 24 | async (): Promise<UserSubscriptionResponse> => { |
| 25 | try { |
| 26 | const response = await apiClient.get< |
| 27 | ApiResponse<UserSubscriptionResponse> |
| 28 | >("/subscriptions/user"); |
| 29 | return response.data.data; |
| 30 | } catch (error) { |
| 31 | throw error; |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Create a checkout session for a subscription plan |
| 37 | */ |
| 38 | export const createCheckoutSession = async ( |
| 39 | data: CheckoutSessionRequest |
| 40 | ): Promise<CheckoutSessionResponse> => { |
| 41 | const response = await apiClient.post<ApiResponse<CheckoutSessionResponse>>( |
| 42 | "/subscriptions/checkout", |
| 43 | data |
| 44 | ); |
| 45 | return response.data.data; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Cancel the current user's subscription |
| 50 | */ |
| 51 | export const cancelSubscription = async (): Promise<{ message: string }> => { |
| 52 | const response = await apiClient.post<ApiResponse<{ message: string }>>( |
| 53 | "/subscriptions/cancel" |
| 54 | ); |
| 55 | return response.data.data; |
| 56 | }; |