-
Notifications
You must be signed in to change notification settings - Fork 369
chore(clerk-js,shared): Expose experimental useSubscription
#6317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c3c11f0
9e5b3f8
238ebd1
791a6d7
dab66c5
2b36c62
ca0ecb6
c8242f7
08b3196
c0c8c89
8c524ac
e3610b2
8e1c9e6
c0bbd77
b8af083
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
'@clerk/types': minor | ||
--- | ||
|
||
[Billing Beta] Introduce top level subscription. | ||
|
||
Updated `CommerceSubscriptionJSON` to describe the top level subscription and renamed the existing type to `CommerceSubscriptionItemJSON`. | ||
Deprecated `billing.getSubscriptions()` in favour of `billing.getSubscription`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
'@clerk/shared': minor | ||
--- | ||
|
||
[Billing Beta] Replace `useSubscriptionItems` with `useSubscription`. |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,8 @@ | ||||||||||||||||
import type { | ||||||||||||||||
CancelSubscriptionParams, | ||||||||||||||||
CommerceMoney, | ||||||||||||||||
CommerceSubscriptionItemJSON, | ||||||||||||||||
CommerceSubscriptionItemResource, | ||||||||||||||||
CommerceSubscriptionJSON, | ||||||||||||||||
CommerceSubscriptionPlanPeriod, | ||||||||||||||||
CommerceSubscriptionResource, | ||||||||||||||||
|
@@ -14,6 +16,46 @@ import { commerceMoneyFromJSON } from '../../utils'; | |||||||||||||||
import { BaseResource, CommercePlan, DeletedObject } from './internal'; | ||||||||||||||||
|
||||||||||||||||
export class CommerceSubscription extends BaseResource implements CommerceSubscriptionResource { | ||||||||||||||||
id!: string; | ||||||||||||||||
status!: Extract<CommerceSubscriptionStatus, 'active' | 'past_due'>; | ||||||||||||||||
activeAt!: Date; | ||||||||||||||||
createdAt!: Date; | ||||||||||||||||
pastDueAt!: Date | null; | ||||||||||||||||
updatedAt!: Date | null; | ||||||||||||||||
nextPayment: { | ||||||||||||||||
amount: CommerceMoney; | ||||||||||||||||
date: Date; | ||||||||||||||||
} | null = null; | ||||||||||||||||
subscriptionItems!: CommerceSubscriptionItemResource[]; | ||||||||||||||||
|
||||||||||||||||
constructor(data: CommerceSubscriptionJSON) { | ||||||||||||||||
super(); | ||||||||||||||||
this.fromJSON(data); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
protected fromJSON(data: CommerceSubscriptionJSON | null): this { | ||||||||||||||||
if (!data) { | ||||||||||||||||
return this; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
this.id = data.id; | ||||||||||||||||
this.status = data.status; | ||||||||||||||||
this.createdAt = unixEpochToDate(data.created_at); | ||||||||||||||||
this.updatedAt = data.updated_at ? unixEpochToDate(data.updated_at) : null; | ||||||||||||||||
this.activeAt = unixEpochToDate(data.active_at); | ||||||||||||||||
this.pastDueAt = data.past_due_at ? unixEpochToDate(data.past_due_at) : null; | ||||||||||||||||
this.nextPayment = data.next_payment | ||||||||||||||||
? { | ||||||||||||||||
amount: commerceMoneyFromJSON(data.next_payment.amount), | ||||||||||||||||
date: unixEpochToDate(data.next_payment.date), | ||||||||||||||||
} | ||||||||||||||||
: null; | ||||||||||||||||
this.subscriptionItems = (data.subscription_items || []).map(item => new CommerceSubscriptionItem(item)); | ||||||||||||||||
return this; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
export class CommerceSubscriptionItem extends BaseResource implements CommerceSubscriptionItemResource { | ||||||||||||||||
id!: string; | ||||||||||||||||
paymentSourceId!: string; | ||||||||||||||||
plan!: CommercePlan; | ||||||||||||||||
|
@@ -27,17 +69,18 @@ export class CommerceSubscription extends BaseResource implements CommerceSubscr | |||||||||||||||
periodStart!: number; | ||||||||||||||||
periodEnd!: number; | ||||||||||||||||
canceledAt!: number | null; | ||||||||||||||||
//TODO(@COMMERCE): Why can this be undefined ? | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainAddress TODO comment about undefined amount The TODO raises a valid concern about why 🏁 Script executed: #!/bin/bash
# Search for usage of CommerceSubscriptionItem to understand when amount might be undefined
ast-grep --pattern 'amount' | rg -B5 -A5 "CommerceSubscriptionItem"
# Check if there are any subscription items without amount in the codebase
rg -A10 "amount.*undefined|amount.*null" --type ts Length of output: 140215 🏁 Script executed: #!/bin/bash
# Display the first 200 lines of CommerceSubscription.ts to inspect the class signature and constructor
sed -n '1,200p' packages/clerk-js/src/core/resources/CommerceSubscription.ts Length of output: 3948 🏁 Script executed: #!/bin/bash
# List immediate subdirectories under packages
ls -1 packages Length of output: 234 🏁 Script executed: #!/bin/bash
# List files in the types package
ls -1 packages/types
# Search for CommerceSubscriptionItemJSON in the types definitions
rg -n "export interface CommerceSubscriptionItemJSON" -n packages/types Length of output: 325 🏁 Script executed: #!/bin/bash
# Show CommerceSubscriptionItemJSON interface definition
sed -n '760,840p' packages/types/src/json.ts Length of output: 2839 Remove TODO and document optional amount The export class CommerceSubscriptionItem extends BaseResource implements CommerceSubscriptionItemResource {
// …
- //TODO(@COMMERCE): Why can this be undefined ?
+ // `amount` is optional per API spec (e.g., free‐plan or credit‐only items)
amount?: CommerceMoney;
// …
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||
amount?: CommerceMoney; | ||||||||||||||||
credit?: { | ||||||||||||||||
amount: CommerceMoney; | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
constructor(data: CommerceSubscriptionJSON) { | ||||||||||||||||
constructor(data: CommerceSubscriptionItemJSON) { | ||||||||||||||||
super(); | ||||||||||||||||
this.fromJSON(data); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
protected fromJSON(data: CommerceSubscriptionJSON | null): this { | ||||||||||||||||
protected fromJSON(data: CommerceSubscriptionItemJSON | null): this { | ||||||||||||||||
if (!data) { | ||||||||||||||||
return this; | ||||||||||||||||
} | ||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.