Skip to content

Commit edeedbf

Browse files
committed
use standalone functions to reduce bundle size
1 parent 61cbd62 commit edeedbf

File tree

9 files changed

+74
-40
lines changed

9 files changed

+74
-40
lines changed

example/convex/_generated/api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
FilterApi,
1818
FunctionReference,
1919
} from "convex/server";
20+
2021
/**
2122
* A utility for referencing Convex functions in your app's API.
2223
*

example/convex/seed.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Polar } from "@polar-sh/sdk";
1+
import { PolarCore } from "@polar-sh/sdk/core.js";
2+
import { productsCreate } from "@polar-sh/sdk/funcs/productsCreate.js";
3+
import { productsList } from "@polar-sh/sdk/funcs/productsList.js";
24
import { internalAction, internalMutation } from "./_generated/server";
35
import { internal } from "./_generated/api";
46

57
const accessToken = process.env.POLAR_ORGANIZATION_TOKEN;
68

7-
const polar = new Polar({
9+
const polar = new PolarCore({
810
accessToken,
911
server: "sandbox",
1012
});
@@ -36,7 +38,7 @@ const seed = internalAction({
3638
return items.length > 0;
3739
}
3840
}
39-
const result = await polar.products.list({
41+
const result = await productsList(polar, {
4042
isArchived: false,
4143
limit: 1,
4244
});
@@ -52,7 +54,7 @@ const seed = internalAction({
5254
// Create example products. In a real app you would likely create your
5355
// products in the Polar dashboard and reference them by id in your application.
5456
await Promise.all([
55-
polar.products.create({
57+
productsCreate(polar, {
5658
name: PREMIUM_PLAN_NAME,
5759
description: "All the things for one low monthly price.",
5860
recurringInterval: "month",
@@ -63,7 +65,7 @@ const seed = internalAction({
6365
},
6466
],
6567
}),
66-
polar.products.create({
68+
productsCreate(polar, {
6769
name: PREMIUM_PLAN_NAME,
6870
description: "All the things for one low annual price.",
6971
recurringInterval: "year",
@@ -74,7 +76,7 @@ const seed = internalAction({
7476
},
7577
],
7678
}),
77-
polar.products.create({
79+
productsCreate(polar, {
7880
name: PREMIUM_PLUS_PLAN_NAME,
7981
description: "All the things for one low monthly price.",
8082
recurringInterval: "month",
@@ -85,7 +87,7 @@ const seed = internalAction({
8587
},
8688
],
8789
}),
88-
polar.products.create({
90+
productsCreate(polar, {
8991
name: PREMIUM_PLUS_PLAN_NAME,
9092
description: "All the things for one low annual price.",
9193
recurringInterval: "year",

example/package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/App.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ export default function TodoList() {
4141

4242
const getButtonText = (targetProductId: string) => {
4343
if (!user?.subscription) return "Upgrade";
44-
const currentAmount = user.subscription.amount ?? 0;
45-
const targetProduct = Object.values(products ?? {}).find(
46-
(p) => p?.id === targetProductId
47-
);
48-
const targetAmount = targetProduct?.prices[0].priceAmount ?? 0;
49-
if (targetAmount > currentAmount) return "Upgrade";
50-
if (targetAmount < currentAmount) return "Downgrade";
51-
return "Switch";
44+
const isPremium =
45+
user.subscription.productId === premiumMonthly?.id ||
46+
user.subscription.productId === premiumYearly?.id;
47+
const targetIsPremiumPlus =
48+
targetProductId === premiumPlusMonthly?.id ||
49+
targetProductId === premiumPlusYearly?.id;
50+
if (isPremium && targetIsPremiumPlus) {
51+
return "Upgrade";
52+
}
53+
return "Downgrade";
5254
};
5355

5456
const handlePlanChange = async (productId: string) => {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
}
7474
},
7575
"peerDependencies": {
76-
"convex": "^1.19.2 || >=1.17.0 <1.35.0",
76+
"@polar-sh/checkout": ">=0.1.10",
77+
"@polar-sh/sdk": ">=0.32.11",
78+
"convex": "^1.25.4",
7779
"react": "^18 || ^19",
7880
"react-dom": "^18 || ^19"
7981
},
@@ -93,8 +95,6 @@
9395
"types": "./dist/commonjs/client/index.d.ts",
9496
"module": "./dist/esm/client/index.js",
9597
"dependencies": {
96-
"@polar-sh/checkout": "0.1.10",
97-
"@polar-sh/sdk": "0.32.11",
9898
"buffer": "^6.0.3",
9999
"convex-helpers": "^0.1.63",
100100
"remeda": "^2.20.2",

src/client/index.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import "./polyfill";
2-
import { Polar as PolarSdk } from "@polar-sh/sdk";
2+
import { PolarCore } from "@polar-sh/sdk/core.js";
3+
import { customersCreate } from "@polar-sh/sdk/funcs/customersCreate.js";
4+
import { checkoutsCreate } from "@polar-sh/sdk/funcs/checkoutsCreate.js";
5+
import { customerSessionsCreate } from "@polar-sh/sdk/funcs/customerSessionsCreate.js";
6+
import { subscriptionsUpdate } from "@polar-sh/sdk/funcs/subscriptionsUpdate.js";
7+
38
import type { Checkout } from "@polar-sh/sdk/models/components/checkout.js";
49
import type { WebhookProductCreatedPayload } from "@polar-sh/sdk/models/components/webhookproductcreatedpayload.js";
510
import type { WebhookProductUpdatedPayload } from "@polar-sh/sdk/models/components/webhookproductupdatedpayload.js";
@@ -48,7 +53,7 @@ export class Polar<
4853
DataModel extends GenericDataModel = GenericDataModel,
4954
Products extends Record<string, string> = Record<string, string>,
5055
> {
51-
public sdk: PolarSdk;
56+
public polar: PolarCore;
5257
public products: Products;
5358
private organizationToken: string;
5459
private webhookSecret: string;
@@ -77,7 +82,7 @@ export class Polar<
7782
(process.env["POLAR_SERVER"] as "sandbox" | "production") ??
7883
"sandbox";
7984

80-
this.sdk = new PolarSdk({
85+
this.polar = new PolarCore({
8186
accessToken: this.organizationToken,
8287
server: this.server,
8388
});
@@ -116,20 +121,23 @@ export class Polar<
116121
const customerId =
117122
dbCustomer?.id ||
118123
(
119-
await this.sdk.customers.create({
124+
await customersCreate(this.polar, {
120125
email,
121126
metadata: {
122127
userId,
123128
},
124129
})
125-
).id;
130+
).value?.id;
131+
if (!customerId) {
132+
throw new Error("Customer not created");
133+
}
126134
if (!dbCustomer) {
127135
await ctx.runMutation(this.component.lib.insertCustomer, {
128136
id: customerId,
129137
userId,
130138
});
131139
}
132-
return this.sdk.checkouts.create({
140+
const checkout = await checkoutsCreate(this.polar, {
133141
allowDiscountCodes: true,
134142
customerId,
135143
embedOrigin: origin,
@@ -138,6 +146,10 @@ export class Polar<
138146
? { products: productIds }
139147
: { products: productIds }),
140148
});
149+
if (!checkout.value) {
150+
throw new Error("Checkout not created");
151+
}
152+
return checkout.value;
141153
}
142154
async createCustomerPortalSession(
143155
ctx: GenericActionCtx<DataModel>,
@@ -152,11 +164,14 @@ export class Polar<
152164
throw new Error("Customer not found");
153165
}
154166

155-
const session = await this.sdk.customerSessions.create({
167+
const session = await customerSessionsCreate(this.polar, {
156168
customerId: customer.id,
157169
});
170+
if (!session.value) {
171+
throw new Error("Customer session not created");
172+
}
158173

159-
return { url: session.customerPortalUrl };
174+
return { url: session.value.customerPortalUrl };
160175
}
161176
listProducts(
162177
ctx: RunQueryCtx,
@@ -209,12 +224,16 @@ export class Polar<
209224
if (subscription.productId === productId) {
210225
throw new Error("Subscription already on this product");
211226
}
212-
await this.sdk.subscriptions.update({
227+
const updatedSubscription = await subscriptionsUpdate(this.polar, {
213228
id: subscription.id,
214229
subscriptionUpdate: {
215230
productId,
216231
},
217232
});
233+
if (!updatedSubscription.value) {
234+
throw new Error("Subscription not updated");
235+
}
236+
return updatedSubscription.value;
218237
}
219238
async cancelSubscription(
220239
ctx: RunActionCtx,
@@ -228,13 +247,17 @@ export class Polar<
228247
if (subscription.status !== "active") {
229248
throw new Error("Subscription is not active");
230249
}
231-
await this.sdk.subscriptions.update({
250+
const updatedSubscription = await subscriptionsUpdate(this.polar, {
232251
id: subscription.id,
233252
subscriptionUpdate: {
234253
cancelAtPeriodEnd: revokeImmediately ? undefined : true,
235254
revoke: revokeImmediately ? true : undefined,
236255
},
237256
});
257+
if (!updatedSubscription.value) {
258+
throw new Error("Subscription not updated");
259+
}
260+
return updatedSubscription.value;
238261
}
239262
api() {
240263
return {

src/component/_generated/api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
FilterApi,
1717
FunctionReference,
1818
} from "convex/server";
19+
1920
/**
2021
* A utility for referencing Convex functions in your app's API.
2122
*

src/component/lib.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Polar as PolarSdk } from "@polar-sh/sdk";
1+
import { PolarCore } from "@polar-sh/sdk/core";
2+
import { productsList } from "@polar-sh/sdk/funcs/productsList.js";
3+
24
import { v } from "convex/values";
35
import { action, mutation, query } from "./_generated/server";
46
import schema from "./schema";
@@ -278,22 +280,25 @@ export const syncProducts = action({
278280
server: v.union(v.literal("sandbox"), v.literal("production")),
279281
},
280282
handler: async (ctx, args) => {
281-
const sdk = new PolarSdk({
283+
const polar = new PolarCore({
282284
accessToken: args.polarAccessToken,
283285
server: args.server,
284286
});
285287
let page = 1;
286288
let maxPage;
287289
do {
288-
const products = await sdk.products.list({
290+
const products = await productsList(polar, {
289291
page,
290292
limit: 100,
291293
});
294+
if (!products.value) {
295+
throw new Error("Failed to get products");
296+
}
292297
page = page + 1;
293-
maxPage = products.result.pagination.maxPage;
298+
maxPage = products.value.result.pagination.maxPage;
294299
await ctx.runMutation(api.lib.updateProducts, {
295300
polarAccessToken: args.polarAccessToken,
296-
products: products.result.items.map(convertToDatabaseProduct),
301+
products: products.value.result.items.map(convertToDatabaseProduct),
297302
});
298303
} while (maxPage >= page);
299304
},

src/component/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import type {
1212
import { GenericId } from "convex/values";
1313
import type { api } from "./_generated/api";
1414
import type { Doc } from "./_generated/dataModel";
15-
import { Subscription } from "@polar-sh/sdk/models/components/subscription.js";
16-
import { Product } from "@polar-sh/sdk/models/components/product.js";
15+
import type { Subscription } from "@polar-sh/sdk/models/components/subscription.js";
16+
import type { Product } from "@polar-sh/sdk/models/components/product.js";
1717

1818
export const omitSystemFields = <
1919
T extends { _id: string; _creationTime: number } | null | undefined,

0 commit comments

Comments
 (0)