Skip to content

Commit 3007deb

Browse files
brkalowwobsoriano
authored andcommitted
chore(clerk-js): Remove Clerk.commerce (#5846)
1 parent a75ee9a commit 3007deb

File tree

23 files changed

+198
-151
lines changed

23 files changed

+198
-151
lines changed

.changeset/proud-donuts-shop.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

.changeset/witty-doors-hear.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
'@clerk/types': minor
55
---
66

7-
Expose stable commerce stable apis under `Clerk.commerce`
7+
Expose Clerk Billing APIs.
88

99
## Render the pricing table component
1010
- `Clerk.mountPricingTable`
1111
- `Clerk.unmountPricingTable`
1212

13-
## Commerce namespace
14-
- `Clerk.commerce.initializePaymentSource()`
15-
- `Clerk.commerce.addPaymentSource()`
16-
- `Clerk.commerce.getPaymentSources()`
17-
- `Clerk.commerce.billing`
18-
- `Clerk.commerce.billing.getPlans()`
19-
- `Clerk.commerce.billing.getSubscriptions()`
20-
- `Clerk.commerce.billing.getInvoices()`
21-
- `Clerk.commerce.billing.startCheckout()`
13+
## Manage payment methods
14+
- `Clerk.[user|organization].initializePaymentSource()`
15+
- `Clerk.[user|organization].addPaymentSource()`
16+
- `Clerk.[user|organization].getPaymentSources()`
17+
18+
## Billing namespace
19+
- `Clerk.billing`
20+
- `Clerk.billing.getPlans()`
21+
- `Clerk.billing.getSubscriptions()`
22+
- `Clerk.billing.getInvoices()`
23+
- `Clerk.billing.startCheckout()`

packages/clerk-js/src/core/clerk.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030
ClerkOptions,
3131
ClientJSONSnapshot,
3232
ClientResource,
33-
CommerceNamespace,
33+
CommerceBillingNamespace,
3434
CreateOrganizationParams,
3535
CreateOrganizationProps,
3636
CredentialReturn,
@@ -133,7 +133,7 @@ import { eventBus, events } from './events';
133133
import type { FapiClient, FapiRequestCallback } from './fapiClient';
134134
import { createFapiClient } from './fapiClient';
135135
import { createClientFromJwt } from './jwt-client';
136-
import { Commerce } from './modules/commerce';
136+
import { CommerceBilling } from './modules/commerce';
137137
import {
138138
ApiKey,
139139
BaseResource,
@@ -190,7 +190,7 @@ export class Clerk implements ClerkInterface {
190190
version: __PKG_VERSION__,
191191
environment: process.env.NODE_ENV || 'production',
192192
};
193-
private static _commerce: CommerceNamespace;
193+
private static _billing: CommerceBillingNamespace;
194194

195195
public client: ClientResource | undefined;
196196
public session: SignedInSessionResource | null | undefined;
@@ -319,11 +319,11 @@ export class Clerk implements ClerkInterface {
319319
return this.#options.standardBrowser || false;
320320
}
321321

322-
get commerce(): CommerceNamespace {
323-
if (!Clerk._commerce) {
324-
Clerk._commerce = new Commerce();
322+
get billing(): CommerceBillingNamespace {
323+
if (!Clerk._billing) {
324+
Clerk._billing = new CommerceBilling();
325325
}
326-
return Clerk._commerce;
326+
return Clerk._billing;
327327
}
328328

329329
public __internal_getOption<K extends keyof ClerkOptions>(key: K): ClerkOptions[K] {

packages/clerk-js/src/core/modules/commerce/Commerce.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './Commerce';
21
export * from './CommerceBilling';
2+
export * from './payment-source-methods';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type {
2+
AddPaymentSourceParams,
3+
ClerkPaginatedResponse,
4+
CommerceInitializedPaymentSourceJSON,
5+
CommercePaymentSourceJSON,
6+
GetPaymentSourcesParams,
7+
InitializePaymentSourceParams,
8+
} from '@clerk/types';
9+
10+
import { convertPageToOffsetSearchParams } from '../../../utils/convertPageToOffsetSearchParams';
11+
import { BaseResource, CommerceInitializedPaymentSource, CommercePaymentSource } from '../../resources/internal';
12+
13+
export const initializePaymentSource = async (params: InitializePaymentSourceParams) => {
14+
const { orgId, ...rest } = params;
15+
const json = (
16+
await BaseResource._fetch({
17+
path: orgId
18+
? `/organizations/${orgId}/commerce/payment_sources/initialize`
19+
: `/me/commerce/payment_sources/initialize`,
20+
method: 'POST',
21+
body: rest as any,
22+
})
23+
)?.response as unknown as CommerceInitializedPaymentSourceJSON;
24+
return new CommerceInitializedPaymentSource(json);
25+
};
26+
27+
export const addPaymentSource = async (params: AddPaymentSourceParams) => {
28+
const { orgId, ...rest } = params;
29+
30+
const json = (
31+
await BaseResource._fetch({
32+
path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`,
33+
method: 'POST',
34+
body: rest as any,
35+
})
36+
)?.response as unknown as CommercePaymentSourceJSON;
37+
return new CommercePaymentSource(json);
38+
};
39+
40+
export const getPaymentSources = async (params: GetPaymentSourcesParams) => {
41+
const { orgId, ...rest } = params;
42+
43+
return await BaseResource._fetch({
44+
path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`,
45+
method: 'GET',
46+
search: convertPageToOffsetSearchParams(rest),
47+
}).then(res => {
48+
const { data: paymentSources, total_count } =
49+
res?.response as unknown as ClerkPaginatedResponse<CommercePaymentSourceJSON>;
50+
return {
51+
total_count,
52+
data: paymentSources.map(paymentSource => new CommercePaymentSource(paymentSource)),
53+
};
54+
});
55+
};

packages/clerk-js/src/core/resources/Organization.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type {
3131

3232
import { convertPageToOffsetSearchParams } from '../../utils/convertPageToOffsetSearchParams';
3333
import { unixEpochToDate } from '../../utils/date';
34+
import { addPaymentSource, getPaymentSources, initializePaymentSource } from '../modules/commerce';
3435
import { BaseResource, CommerceSubscription, OrganizationInvitation, OrganizationMembership } from './internal';
3536
import { OrganizationDomain } from './OrganizationDomain';
3637
import { OrganizationMembershipRequest } from './OrganizationMembershipRequest';
@@ -282,6 +283,27 @@ export class Organization extends BaseResource implements OrganizationResource {
282283
}).then(res => new Organization(res?.response as OrganizationJSON));
283284
};
284285

286+
initializePaymentSource: typeof initializePaymentSource = params => {
287+
return initializePaymentSource({
288+
...params,
289+
orgId: this.id,
290+
});
291+
};
292+
293+
addPaymentSource: typeof addPaymentSource = params => {
294+
return addPaymentSource({
295+
...params,
296+
orgId: this.id,
297+
});
298+
};
299+
300+
getPaymentSources: typeof getPaymentSources = params => {
301+
return getPaymentSources({
302+
...params,
303+
orgId: this.id,
304+
});
305+
};
306+
285307
protected fromJSON(data: OrganizationJSON | OrganizationJSONSnapshot | null): this {
286308
if (!data) {
287309
return this;

packages/clerk-js/src/core/resources/User.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { unixEpochToDate } from '../../utils/date';
3636
import { normalizeUnsafeMetadata } from '../../utils/resourceParams';
3737
import { getFullName } from '../../utils/user';
3838
import { eventBus, events } from '../events';
39+
import { addPaymentSource, getPaymentSources, initializePaymentSource } from '../modules/commerce';
3940
import { BackupCode } from './BackupCode';
4041
import {
4142
BaseResource,
@@ -290,6 +291,18 @@ export class User extends BaseResource implements UserResource {
290291
return new DeletedObject(json);
291292
};
292293

294+
initializePaymentSource: typeof initializePaymentSource = params => {
295+
return initializePaymentSource(params);
296+
};
297+
298+
addPaymentSource: typeof addPaymentSource = params => {
299+
return addPaymentSource(params);
300+
};
301+
302+
getPaymentSources: typeof getPaymentSources = params => {
303+
return getPaymentSources(params);
304+
};
305+
293306
get verifiedExternalAccounts() {
294307
return this.externalAccounts.filter(externalAccount => externalAccount.verification?.status == 'verified');
295308
}

packages/clerk-js/src/core/resources/__tests__/__snapshots__/Organization.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`Organization has the same initial properties 1`] = `
44
Organization {
55
"addMember": [Function],
6+
"addPaymentSource": [Function],
67
"adminDeleteEnabled": true,
78
"createDomain": [Function],
89
"createdAt": 1970-01-01T00:00:12.345Z,
@@ -12,11 +13,13 @@ Organization {
1213
"getInvitations": [Function],
1314
"getMembershipRequests": [Function],
1415
"getMemberships": [Function],
16+
"getPaymentSources": [Function],
1517
"getRoles": [Function],
1618
"getSubscriptions": [Function],
1719
"hasImage": true,
1820
"id": "test_id",
1921
"imageUrl": "https://img.clerk.com/eyJ0eXBlIjoiZGVmYXVsdCIsImlpZCI6Imluc18xbHlXRFppb2JyNjAwQUtVZVFEb1NsckVtb00iLCJyaWQiOiJ1c2VyXzJKbElJQTN2VXNjWXh1N2VUMnhINmFrTGgxOCIsImluaXRpYWxzIjoiREsifQ?width=160",
22+
"initializePaymentSource": [Function],
2023
"inviteMember": [Function],
2124
"inviteMembers": [Function],
2225
"maxAllowedMemberships": 3,

packages/clerk-js/src/core/resources/__tests__/__snapshots__/OrganizationMembership.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ OrganizationMembership {
77
"id": "test_id",
88
"organization": Organization {
99
"addMember": [Function],
10+
"addPaymentSource": [Function],
1011
"adminDeleteEnabled": true,
1112
"createDomain": [Function],
1213
"createdAt": 1970-01-01T00:00:12.345Z,
@@ -16,11 +17,13 @@ OrganizationMembership {
1617
"getInvitations": [Function],
1718
"getMembershipRequests": [Function],
1819
"getMemberships": [Function],
20+
"getPaymentSources": [Function],
1921
"getRoles": [Function],
2022
"getSubscriptions": [Function],
2123
"hasImage": true,
2224
"id": "test_org_id",
2325
"imageUrl": "https://img.clerk.com/eyJ0eXBlIjoiZGVmYXVsdCIsImlpZCI6Imluc18xbHlXRFppb2JyNjAwQUtVZVFEb1NsckVtb00iLCJyaWQiOiJ1c2VyXzJKbElJQTN2VXNjWXh1N2VUMnhINmFrTGgxOCIsImluaXRpYWxzIjoiREsifQ?width=160",
26+
"initializePaymentSource": [Function],
2427
"inviteMember": [Function],
2528
"inviteMembers": [Function],
2629
"maxAllowedMemberships": 3,

0 commit comments

Comments
 (0)