@@ -28,11 +28,22 @@ import {
28
28
} from '../../resources/internal' ;
29
29
30
30
export class Billing implements BillingNamespace {
31
+ static readonly #pathRoot = '/billing' ;
32
+ static path ( subPath : string , param ?: { orgId ?: string } ) : string {
33
+ const { orgId } = param || { } ;
34
+ let prefix = '' ;
35
+ if ( orgId ) {
36
+ prefix = `/organizations/${ orgId } ` ;
37
+ }
38
+ prefix = '/me' ;
39
+ return `${ prefix } ${ Billing . #pathRoot} ${ subPath } ` ;
40
+ }
41
+
31
42
getPlans = async ( params ?: GetPlansParams ) : Promise < ClerkPaginatedResponse < BillingPlanResource > > => {
32
43
const { for : forParam , ...safeParams } = params || { } ;
33
44
const searchParams = { ...safeParams , payer_type : forParam === 'organization' ? 'org' : 'user' } ;
34
45
return await BaseResource . _fetch ( {
35
- path : `/commerce/ plans` ,
46
+ path : Billing . path ( '/ plans' ) ,
36
47
method : 'GET' ,
37
48
search : convertPageToOffsetSearchParams ( searchParams ) ,
38
49
} ) . then ( res => {
@@ -48,15 +59,15 @@ export class Billing implements BillingNamespace {
48
59
// Inconsistent API
49
60
getPlan = async ( params : { id : string } ) : Promise < BillingPlanResource > => {
50
61
const plan = ( await BaseResource . _fetch ( {
51
- path : `/commerce/ plans/${ params . id } `,
62
+ path : Billing . path ( `/ plans/${ params . id } `) ,
52
63
method : 'GET' ,
53
64
} ) ) as unknown as BillingPlanJSON ;
54
65
return new BillingPlan ( plan ) ;
55
66
} ;
56
67
57
68
getSubscription = async ( params : GetSubscriptionParams ) : Promise < BillingSubscriptionResource > => {
58
69
return await BaseResource . _fetch ( {
59
- path : params . orgId ? `/organizations/ ${ params . orgId } /commerce/ subscription` : `/me/commerce/subscription` ,
70
+ path : Billing . path ( `/ subscription`, { orgId : params . orgId } ) ,
60
71
method : 'GET' ,
61
72
} ) . then ( res => new BillingSubscription ( res ?. response as BillingSubscriptionJSON ) ) ;
62
73
} ;
@@ -65,7 +76,7 @@ export class Billing implements BillingNamespace {
65
76
const { orgId, ...rest } = params ;
66
77
67
78
return await BaseResource . _fetch ( {
68
- path : orgId ? `/organizations/ ${ orgId } /commerce/ statements` : `/me/commerce/statements` ,
79
+ path : Billing . path ( `/ statements`, { orgId } ) ,
69
80
method : 'GET' ,
70
81
search : convertPageToOffsetSearchParams ( rest ) ,
71
82
} ) . then ( res => {
@@ -82,9 +93,7 @@ export class Billing implements BillingNamespace {
82
93
getStatement = async ( params : { id : string ; orgId ?: string } ) : Promise < BillingStatementResource > => {
83
94
const statement = (
84
95
await BaseResource . _fetch ( {
85
- path : params . orgId
86
- ? `/organizations/${ params . orgId } /commerce/statements/${ params . id } `
87
- : `/me/commerce/statements/${ params . id } ` ,
96
+ path : Billing . path ( `/statements/${ params . id } ` , { orgId : params . orgId } ) ,
88
97
method : 'GET' ,
89
98
} )
90
99
) ?. response as unknown as BillingStatementJSON ;
@@ -97,7 +106,7 @@ export class Billing implements BillingNamespace {
97
106
const { orgId, ...rest } = params ;
98
107
99
108
return await BaseResource . _fetch ( {
100
- path : orgId ? `/organizations/ ${ orgId } /commerce/ payment_attempts` : `/me/commerce/payment_attempts` ,
109
+ path : Billing . path ( `/ payment_attempts`, { orgId } ) ,
101
110
method : 'GET' ,
102
111
search : convertPageToOffsetSearchParams ( rest ) ,
103
112
} ) . then ( res => {
@@ -112,9 +121,7 @@ export class Billing implements BillingNamespace {
112
121
113
122
getPaymentAttempt = async ( params : { id : string ; orgId ?: string } ) : Promise < BillingPaymentResource > => {
114
123
const paymentAttempt = ( await BaseResource . _fetch ( {
115
- path : params . orgId
116
- ? `/organizations/${ params . orgId } /commerce/payment_attempts/${ params . id } `
117
- : `/me/commerce/payment_attempts/${ params . id } ` ,
124
+ path : Billing . path ( `/payment_attempts/${ params . id } ` , { orgId : params . orgId } ) ,
118
125
method : 'GET' ,
119
126
} ) ) as unknown as BillingPaymentJSON ;
120
127
return new BillingPayment ( paymentAttempt ) ;
@@ -124,7 +131,7 @@ export class Billing implements BillingNamespace {
124
131
const { orgId, ...rest } = params ;
125
132
const json = (
126
133
await BaseResource . _fetch < BillingCheckoutJSON > ( {
127
- path : orgId ? `/organizations/ ${ orgId } /commerce/ checkouts` : `/me/commerce/checkouts` ,
134
+ path : Billing . path ( `/ checkouts`, { orgId } ) ,
128
135
method : 'POST' ,
129
136
body : rest as any ,
130
137
} )
0 commit comments