mirror of
https://github.com/LukeHagar/polar.git
synced 2025-12-09 20:57:43 +00:00
add example functions
This commit is contained in:
2
example/convex/_generated/api.d.ts
vendored
2
example/convex/_generated/api.d.ts
vendored
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
|
||||
import type * as example from "../example.js";
|
||||
import type * as http from "../http.js";
|
||||
|
||||
import type {
|
||||
ApiFromModules,
|
||||
@@ -25,6 +26,7 @@ import type {
|
||||
*/
|
||||
declare const fullApi: ApiFromModules<{
|
||||
example: typeof example;
|
||||
http: typeof http;
|
||||
}>;
|
||||
declare const fullApiWithMounts: typeof fullApi;
|
||||
|
||||
|
||||
@@ -1,59 +1,56 @@
|
||||
import { components } from "./_generated/api";
|
||||
import { Polar } from "@convex-dev/polar";
|
||||
import { v } from "convex/values";
|
||||
import { WebhookSubscriptionCreatedPayload$inboundSchema } from "@polar-sh/sdk/models/components";
|
||||
import { query, internalMutation } from "./_generated/server";
|
||||
import { components } from "./_generated/api";
|
||||
import { Id } from "./_generated/dataModel";
|
||||
|
||||
const polar = new Polar(components.polar);
|
||||
const polarComponent = new Polar(components.polar);
|
||||
|
||||
/*
|
||||
export const addOne = mutation({
|
||||
export const listProducts = query({
|
||||
args: {},
|
||||
handler: async (ctx, _args) => {
|
||||
await numUsers.inc(ctx);
|
||||
handler: async (ctx) => {
|
||||
return polarComponent.listProducts(ctx, {
|
||||
includeArchived: false,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export const getCount = query({
|
||||
args: {},
|
||||
handler: async (ctx, _args) => {
|
||||
return await numUsers.count(ctx);
|
||||
export const getUserSubscriptions = query({
|
||||
args: {
|
||||
userId: v.string(),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
return polarComponent.listUserSubscriptions(ctx, args.userId);
|
||||
},
|
||||
});
|
||||
|
||||
export const usingClient = internalMutation({
|
||||
args: {},
|
||||
handler: async (ctx, _args) => {
|
||||
await polar.add(ctx, "accomplishments");
|
||||
await polar.add(ctx, "beans", 2);
|
||||
const count = await polar.count(ctx, "beans");
|
||||
return count;
|
||||
},
|
||||
});
|
||||
|
||||
export const usingFunctions = internalMutation({
|
||||
args: {},
|
||||
handler: async (ctx, _args) => {
|
||||
await numUsers.inc(ctx);
|
||||
await numUsers.inc(ctx);
|
||||
await numUsers.dec(ctx);
|
||||
return numUsers.count(ctx);
|
||||
},
|
||||
});
|
||||
|
||||
export const directCall = internalMutation({
|
||||
args: {},
|
||||
handler: async (ctx, _args) => {
|
||||
await ctx.runMutation(components.polar.lib.add, {
|
||||
name: "pennies",
|
||||
count: 250,
|
||||
});
|
||||
await ctx.runMutation(components.polar.lib.add, {
|
||||
name: "beans",
|
||||
count: 3,
|
||||
shards: 100,
|
||||
});
|
||||
const count = await ctx.runQuery(components.polar.lib.count, {
|
||||
name: "beans",
|
||||
});
|
||||
return count;
|
||||
},
|
||||
});
|
||||
/**
|
||||
* This function is called when a Polar webhook is received.
|
||||
*
|
||||
* The payload is provided as received from Polar, and the webhook signature is
|
||||
* already verified before this function is called.
|
||||
*/
|
||||
export const polarEventCallback = internalMutation({
|
||||
args: {
|
||||
payload: v.any(),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
switch (args.payload.type) {
|
||||
// When creating a subscription, pass the user's id from your system into
|
||||
// the metadata field. The same metadata will be passed back in the
|
||||
// webhook, allowing you to add the user's Polar ID to the record in
|
||||
// your database.
|
||||
case "subscription.created": {
|
||||
const payload = WebhookSubscriptionCreatedPayload$inboundSchema.parse(
|
||||
args.payload
|
||||
);
|
||||
const userId = payload.data.metadata.userId;
|
||||
await ctx.db.patch(userId as Id<"users">, {
|
||||
polarId: payload.data.userId,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
14
example/convex/http.ts
Normal file
14
example/convex/http.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Polar as PolarComponent } from "@convex-dev/polar";
|
||||
import { httpRouter } from "convex/server";
|
||||
import { components, internal } from "./_generated/api";
|
||||
|
||||
const http = httpRouter();
|
||||
|
||||
const polarComponent = new PolarComponent(components.polar);
|
||||
|
||||
polarComponent.registerRoutes(http, {
|
||||
path: "/events/polar",
|
||||
eventCallback: internal.example.polarEventCallback,
|
||||
});
|
||||
|
||||
export default http;
|
||||
@@ -1,7 +1,9 @@
|
||||
import { defineSchema } from "convex/server";
|
||||
import { defineSchema, defineTable } from "convex/server";
|
||||
import { v } from "convex/values";
|
||||
|
||||
export default defineSchema(
|
||||
{
|
||||
// Any tables used by the example app go here.
|
||||
},
|
||||
);
|
||||
export default defineSchema({
|
||||
users: defineTable({
|
||||
email: v.string(),
|
||||
polarId: v.string(),
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
import { defineSchema, defineTable } from "convex/server";
|
||||
import { v } from "convex/values";
|
||||
|
||||
export const INTERVALS = {
|
||||
MONTH: "month",
|
||||
YEAR: "year",
|
||||
} as const;
|
||||
export const intervalValidator = v.union(
|
||||
v.literal(INTERVALS.MONTH),
|
||||
v.literal(INTERVALS.YEAR)
|
||||
);
|
||||
|
||||
export default defineSchema(
|
||||
{
|
||||
benefits: defineTable({
|
||||
|
||||
Reference in New Issue
Block a user