@codeguide/core 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,147 @@
1
+ import { BaseService } from '../base/base-service'
2
+ import {
3
+ CurrentSubscriptionResponse,
4
+ UserSubscriptionsResponse,
5
+ CancelSubscriptionRequest,
6
+ CancelSubscriptionResponse,
7
+ } from '../../types'
8
+
9
+ export class SubscriptionService extends BaseService {
10
+ constructor(config: any) {
11
+ super(config)
12
+ }
13
+
14
+ /**
15
+ * Get the currently active subscription for the authenticated user
16
+ * GET /subscriptions/current
17
+ */
18
+ async getCurrentSubscription(): Promise<CurrentSubscriptionResponse> {
19
+ return this.get<CurrentSubscriptionResponse>('/subscriptions/current')
20
+ }
21
+
22
+ /**
23
+ * Get all subscriptions (active and historical) for the authenticated user
24
+ * GET /subscriptions/
25
+ */
26
+ async getAllSubscriptions(): Promise<UserSubscriptionsResponse> {
27
+ return this.get<UserSubscriptionsResponse>('/subscriptions/')
28
+ }
29
+
30
+ /**
31
+ * Cancel subscription but keep it active until the end of the current billing period
32
+ * POST /subscriptions/{subscription_id}/cancel
33
+ */
34
+ async cancelSubscription(
35
+ subscriptionId: string,
36
+ request: CancelSubscriptionRequest
37
+ ): Promise<CancelSubscriptionResponse> {
38
+ return this.post<CancelSubscriptionResponse>(
39
+ `/subscriptions/${subscriptionId}/cancel`,
40
+ request
41
+ )
42
+ }
43
+
44
+ /**
45
+ * Get subscription details by ID
46
+ * GET /subscriptions/{subscription_id}
47
+ */
48
+ async getSubscriptionById(subscriptionId: string): Promise<{
49
+ status: string
50
+ data: {
51
+ subscription: any
52
+ product: any
53
+ price: any
54
+ }
55
+ }> {
56
+ return this.get(`/subscriptions/${subscriptionId}`)
57
+ }
58
+
59
+ /**
60
+ * Reactivate a canceled subscription (if supported by API)
61
+ * POST /subscriptions/{subscription_id}/reactivate
62
+ */
63
+ async reactivateSubscription(subscriptionId: string): Promise<{
64
+ status: string
65
+ message: string
66
+ data: any
67
+ }> {
68
+ return this.post(`/subscriptions/${subscriptionId}/reactivate`, {})
69
+ }
70
+
71
+ /**
72
+ * Update subscription quantity or other parameters
73
+ * PUT /subscriptions/{subscription_id}
74
+ */
75
+ async updateSubscription(
76
+ subscriptionId: string,
77
+ updates: {
78
+ quantity?: number
79
+ metadata?: Record<string, any>
80
+ }
81
+ ): Promise<{
82
+ status: string
83
+ data: any
84
+ }> {
85
+ return this.put(`/subscriptions/${subscriptionId}`, updates)
86
+ }
87
+
88
+ /**
89
+ * Get subscription usage statistics (if supported by API)
90
+ * GET /subscriptions/{subscription_id}/usage
91
+ */
92
+ async getSubscriptionUsage(subscriptionId: string): Promise<{
93
+ status: string
94
+ data: {
95
+ current_usage: number
96
+ limit: number
97
+ period_start: string
98
+ period_end: string
99
+ usage_breakdown?: Array<{
100
+ feature: string
101
+ used: number
102
+ limit: number
103
+ }>
104
+ }
105
+ }> {
106
+ return this.get(`/subscriptions/${subscriptionId}/usage`)
107
+ }
108
+
109
+ /**
110
+ * Get upcoming invoice for subscription
111
+ * GET /subscriptions/{subscription_id}/upcoming-invoice
112
+ */
113
+ async getUpcomingInvoice(subscriptionId: string): Promise<{
114
+ status: string
115
+ data: {
116
+ amount: number
117
+ currency: string
118
+ date: string
119
+ line_items: Array<{
120
+ description: string
121
+ amount: number
122
+ currency: string
123
+ }>
124
+ }
125
+ }> {
126
+ return this.get(`/subscriptions/${subscriptionId}/upcoming-invoice`)
127
+ }
128
+
129
+ /**
130
+ * Get subscription history and payments
131
+ * GET /subscriptions/{subscription_id}/history
132
+ */
133
+ async getSubscriptionHistory(subscriptionId: string): Promise<{
134
+ status: string
135
+ data: Array<{
136
+ id: string
137
+ type: 'payment' | 'invoice' | 'refund'
138
+ amount: number
139
+ currency: string
140
+ status: string
141
+ created: string
142
+ description?: string
143
+ }>
144
+ }> {
145
+ return this.get(`/subscriptions/${subscriptionId}/history`)
146
+ }
147
+ }
package/types.ts CHANGED
@@ -106,3 +106,135 @@ export interface RevokeApiKeyResponse {
106
106
  message: string
107
107
  revoked_key_id?: string
108
108
  }
109
+
110
+ // Subscription Types
111
+ export interface Subscription {
112
+ id: string
113
+ user_id: string
114
+ status: 'active' | 'canceled' | 'past_due' | 'unpaid' | 'trialing' | 'incomplete' | 'incomplete_expired'
115
+ metadata: Record<string, any>
116
+ price_id: string
117
+ quantity: number
118
+ cancel_at_period_end: boolean
119
+ created: string
120
+ current_period_start: string
121
+ current_period_end: string
122
+ ended_at?: string | null
123
+ cancel_at?: string | null
124
+ canceled_at?: string | null
125
+ trial_start?: string | null
126
+ trial_end?: string | null
127
+ org_id?: string | null
128
+ }
129
+
130
+ export interface Product {
131
+ id: string
132
+ active: boolean
133
+ name: string
134
+ description: string
135
+ image: string
136
+ metadata: Record<string, any>
137
+ marketing_features: string[]
138
+ live_mode: boolean
139
+ is_team_plan: boolean
140
+ }
141
+
142
+ export interface Price {
143
+ id: string
144
+ product_id: string
145
+ active: boolean
146
+ description: string
147
+ unit_amount: number
148
+ currency: string
149
+ type: 'recurring' | 'one_time'
150
+ interval: 'day' | 'week' | 'month' | 'year'
151
+ interval_count: number
152
+ trial_period_days?: number | null
153
+ metadata?: Record<string, any> | null
154
+ }
155
+
156
+ export interface CurrentSubscriptionResponse {
157
+ status: string
158
+ data: {
159
+ subscription: Subscription
160
+ product: Product
161
+ price: Price
162
+ }
163
+ }
164
+
165
+ export interface UserSubscriptionsResponse {
166
+ status: string
167
+ data: Subscription[]
168
+ }
169
+
170
+ export interface CancelSubscriptionRequest {
171
+ cancel_at_period_end: boolean
172
+ }
173
+
174
+ export interface CancelSubscriptionResponse {
175
+ status: string
176
+ message: string
177
+ data: Subscription
178
+ }
179
+
180
+ // Cancellation Funnel Types
181
+ export interface CancellationFunnelInitiateRequest {
182
+ subscription_id: string
183
+ }
184
+
185
+ export interface CancellationFunnelInitiateResponse {
186
+ status: string
187
+ message: string
188
+ data: {
189
+ funnel_id: string
190
+ subscription_id: string
191
+ current_step: 'initiated'
192
+ created_at: string
193
+ available_offers: string[]
194
+ }
195
+ }
196
+
197
+ export interface CancellationFunnelPauseOfferRequest {
198
+ subscription_id: string
199
+ action: 'accepted' | 'declined'
200
+ pause_duration_months?: number
201
+ }
202
+
203
+ export interface CancellationFunnelPauseOfferResponse {
204
+ status: string
205
+ message: string
206
+ data: {
207
+ funnel_id: string
208
+ subscription_id: string
209
+ pause_offer: {
210
+ action: 'accepted' | 'declined'
211
+ pause_duration_months?: number
212
+ pause_start_date?: string
213
+ pause_end_date?: string
214
+ }
215
+ next_step: string
216
+ }
217
+ }
218
+
219
+ export interface CancellationFunnelSurveyRequest {
220
+ subscription_id: string
221
+ reason: string
222
+ feedback?: string
223
+ competitor_name?: string
224
+ }
225
+
226
+ export interface CancellationFunnelSurveyResponse {
227
+ status: string
228
+ message: string
229
+ data: {
230
+ funnel_id: string
231
+ subscription_id: string
232
+ survey_response: {
233
+ reason: string
234
+ feedback?: string
235
+ competitor_name?: string
236
+ submitted_at: string
237
+ }
238
+ next_step: string
239
+ }
240
+ }