make product config optional

This commit is contained in:
Shawn Erquhart
2025-02-25 17:12:44 -05:00
parent 5d1a4bc695
commit 1db4b9c424
5 changed files with 30 additions and 12 deletions

View File

@@ -29,7 +29,13 @@ export const MAX_PREMIUM_TODOS = 6;
export const { export const {
changeCurrentSubscription, changeCurrentSubscription,
cancelCurrentSubscription, cancelCurrentSubscription,
getProducts,
// If you configure your products by key in the Polar constructor,
// this query provides a keyed object of the products.
getConfiguredProducts,
// This provides all products, useful if you don't configure products by key.
listAllProducts,
} = polar.api(); } = polar.api();
export const { generateCustomerPortalUrl } = polar.checkoutApi(); export const { generateCustomerPortalUrl } = polar.checkoutApi();

View File

@@ -9,7 +9,7 @@ import { CheckoutLink, CustomerPortalLink } from "../../src/react";
export default function TodoList() { export default function TodoList() {
const user = useQuery(api.example.getCurrentUser); const user = useQuery(api.example.getCurrentUser);
const todos = useQuery(api.example.listTodos); const todos = useQuery(api.example.listTodos);
const products = useQuery(api.example.getProducts); const products = useQuery(api.example.getConfiguredProducts);
const insertTodo = useMutation(api.example.insertTodo); const insertTodo = useMutation(api.example.insertTodo);
const completeTodo = useMutation(api.example.completeTodo); const completeTodo = useMutation(api.example.completeTodo);
const deleteTodo = useMutation(api.example.deleteTodo); const deleteTodo = useMutation(api.example.deleteTodo);

View File

@@ -8,7 +8,7 @@ import { ConfirmationModal } from "./ConfirmationModal";
export function UpgradeCTA() { export function UpgradeCTA() {
const user = useQuery(api.example.getCurrentUser); const user = useQuery(api.example.getCurrentUser);
const products = useQuery(api.example.getProducts); const products = useQuery(api.example.getConfiguredProducts);
const changeCurrentSubscription = useAction( const changeCurrentSubscription = useAction(
api.example.changeCurrentSubscription api.example.changeCurrentSubscription
); );

View File

@@ -1,11 +1,11 @@
{ {
"name": "@erquhart/convex-polar", "name": "@erquhart/convex-polar",
"description": "A Polar component for Convex.", "description": "A Polar component for Convex.",
"repository": "github:get-convex/polar", "repository": "github:erquhart/convex-polar",
"homepage": "https://github.com/get-convex/polar#readme", "homepage": "https://github.com/erquhart/convex-polar#readme",
"bugs": { "bugs": {
"email": "support@convex.dev", "email": "support@convex.dev",
"url": "https://github.com/get-convex/polar/issues" "url": "https://github.com/erquhart/convex-polar/issues"
}, },
"version": "0.2.0-alpha.2", "version": "0.2.0-alpha.2",
"license": "Apache-2.0", "license": "Apache-2.0",

View File

@@ -56,14 +56,14 @@ export class Polar<
constructor( constructor(
public component: ComponentApi, public component: ComponentApi,
private config: { private config: {
products: Products; products?: Products;
getUserInfo: (ctx: RunQueryCtx) => Promise<{ getUserInfo: (ctx: RunQueryCtx) => Promise<{
userId: string; userId: string;
email: string; email: string;
}>; }>;
} }
) { ) {
this.products = config.products; this.products = config.products ?? ({} as Products);
this.sdk = new PolarSdk({ this.sdk = new PolarSdk({
accessToken: process.env["POLAR_ORGANIZATION_TOKEN"] ?? "", accessToken: process.env["POLAR_ORGANIZATION_TOKEN"] ?? "",
server: server:
@@ -109,9 +109,6 @@ export class Polar<
userId, userId,
}); });
} }
console.log("customerId", customerId);
console.log("productIds", productIds);
console.log("origin", origin);
return this.sdk.checkouts.create({ return this.sdk.checkouts.create({
allowDiscountCodes: true, allowDiscountCodes: true,
customerId, customerId,
@@ -161,12 +158,19 @@ export class Polar<
if (!subscription) { if (!subscription) {
return null; return null;
} }
const product = await ctx.runQuery(this.component.lib.getProduct, {
id: subscription.productId,
});
if (!product) {
throw new Error("Product not found");
}
const productKey = ( const productKey = (
Object.keys(this.products) as Array<keyof Products> Object.keys(this.products) as Array<keyof Products>
).find((key) => this.products[key] === subscription.productId); ).find((key) => this.products[key] === subscription.productId);
return { return {
...subscription, ...subscription,
productKey, productKey,
product,
}; };
} }
getProduct(ctx: RunQueryCtx, { productId }: { productId: string }) { getProduct(ctx: RunQueryCtx, { productId }: { productId: string }) {
@@ -233,7 +237,7 @@ export class Polar<
}); });
}, },
}), }),
getProducts: queryGeneric({ getConfiguredProducts: queryGeneric({
args: {}, args: {},
handler: async (ctx) => { handler: async (ctx) => {
const products = await this.listProducts(ctx); const products = await this.listProducts(ctx);
@@ -242,6 +246,14 @@ export class Polar<
); );
}, },
}), }),
listAllProducts: queryGeneric({
args: { includeArchived: v.optional(v.boolean()) },
handler: async (ctx, args) => {
return await this.listProducts(ctx, {
includeArchived: args.includeArchived,
});
},
}),
}; };
} }
checkoutApi() { checkoutApi() {