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 {
changeCurrentSubscription,
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();
export const { generateCustomerPortalUrl } = polar.checkoutApi();

View File

@@ -9,7 +9,7 @@ import { CheckoutLink, CustomerPortalLink } from "../../src/react";
export default function TodoList() {
const user = useQuery(api.example.getCurrentUser);
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 completeTodo = useMutation(api.example.completeTodo);
const deleteTodo = useMutation(api.example.deleteTodo);

View File

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

View File

@@ -1,11 +1,11 @@
{
"name": "@erquhart/convex-polar",
"description": "A Polar component for Convex.",
"repository": "github:get-convex/polar",
"homepage": "https://github.com/get-convex/polar#readme",
"repository": "github:erquhart/convex-polar",
"homepage": "https://github.com/erquhart/convex-polar#readme",
"bugs": {
"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",
"license": "Apache-2.0",

View File

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