Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polar-init",
"version": "0.2.2",
"version": "0.2.3",
"license": "Apache-2.0",
"bin": "bin/cli.js",
"type": "module",
Expand All @@ -14,24 +14,26 @@
"check": "biome check --write ./src",
"copy:templates": "mkdir -p dist/templates && cp -r templates dist"
},
"files": ["dist", "bin"],
"files": [
"dist",
"bin"
],
"dependencies": {
"@inkjs/ui": "^2.0.0",
"@polar-sh/sdk": "^0.13.3",
"@polar-sh/sdk": "^0.17.1",
"@types/cross-spawn": "^6.0.6",
"cross-spawn": "^7.0.3",
"ink": "^4.4.1",
"ink-link": "^4.1.0",
"listr": "^0.14.3",
"meow": "^11.0.0",
"next": "^15.0.1",
"nuxt": "^3.0.0",
"open": "^10.1.0",
"prompts": "^2.4.2",
"react": "^18.2.0",
"standardwebhooks": "1.0.0",
"tsup": "^8.3.0",
"zod": "^3.23.8",
"nuxt": "^3.0.0"
"zod": "^3.23.8"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
Expand All @@ -56,7 +58,9 @@
"ts": "module",
"tsx": "module"
},
"nodeArguments": ["--loader=ts-node/esm"]
"nodeArguments": [
"--loader=ts-node/esm"
]
},
"xo": {
"extends": "xo-react",
Expand Down
18 changes: 10 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions src/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,8 @@ const cli = meow(
}

const baseDependencies = ["@polar-sh/sdk"];
const webhooksDependencies = ["standardwebhooks"];

await installMessage(
installDependencies(
shouldCopyWebhooks
? [...baseDependencies, ...webhooksDependencies]
: baseDependencies,
),
);

await installMessage(installDependencies(baseDependencies));

let envVar = {};

Expand Down
6 changes: 1 addition & 5 deletions src/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ export const resolveOrganization = async (
slug: string,
): Promise<Organization> => {
// Get list of organizations user is member of
const userOrganizations = (
await api.organizations.list({
isMember: true,
})
).result.items;
const userOrganizations = (await api.organizations.list({})).result.items;

// Find organization matching slug if it exists
const matchingOrg = userOrganizations.find((org) => org.slug === slug);
Expand Down
8 changes: 5 additions & 3 deletions src/product.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { Polar } from "@polar-sh/sdk";
import type { Organization } from "@polar-sh/sdk/models/components";
import type { ProductsCreateProductCreate } from "@polar-sh/sdk/models/operations";
import type {
Organization,
ProductCreate,
} from "@polar-sh/sdk/models/components";
import type { benefitPrompt } from "./prompts/benefit.js";

export const createProduct = async (
api: Polar,
organization: Organization,
productCreate: ProductsCreateProductCreate,
productCreate: ProductCreate,
benefit: Awaited<ReturnType<typeof benefitPrompt>>,
) => {
const product = await api.products.create({
Expand Down
2 changes: 1 addition & 1 deletion src/ui/success.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const successMessage = (
{framework === "next" && (
<Text color="cyanBright">
{">"}{" "}
<Link url="https://docs.polar.sh/guides/nextjs">
<Link url="https://docs.polar.sh/documentation/integration-guides/nextjs">
Continue to the Polar Next.js Guide
</Link>
</Text>
Expand Down
43 changes: 17 additions & 26 deletions templates/next/api/webhook/polar/route.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
import { Webhook } from "standardwebhooks";

import type {
WebhookCheckoutCreatedPayload,
WebhookCheckoutUpdatedPayload,
WebhookSubscriptionActivePayload,
WebhookSubscriptionCanceledPayload,
WebhookSubscriptionCreatedPayload,
WebhookSubscriptionRevokedPayload,
WebhookSubscriptionUpdatedPayload,
} from "@polar-sh/sdk/models/components";
import {
validateEvent,
WebhookVerificationError,
} from "@polar-sh/sdk/webhooks";
import { type NextRequest, NextResponse } from "next/server";

type WebhookEvent =
| WebhookCheckoutCreatedPayload
| WebhookCheckoutUpdatedPayload
| WebhookSubscriptionCreatedPayload
| WebhookSubscriptionActivePayload
| WebhookSubscriptionCanceledPayload
| WebhookSubscriptionUpdatedPayload
| WebhookSubscriptionRevokedPayload;

export async function POST(request: NextRequest) {
const requestBody = await request.text();

const webhookHeaders = {
"webhook-id": request.headers.get("webhook-id") ?? "",
"webhook-timestamp": request.headers.get("webhook-timestamp") ?? "",
"webhook-signature": request.headers.get("webhook-signature") ?? "",
};

const webhookSecret = Buffer.from(process.env.POLAR_WEBHOOK_SECRET!).toString(
"base64",
);
const wh = new Webhook(webhookSecret);
const webhookPayload = wh.verify(requestBody, webhookHeaders) as WebhookEvent;
let webhookPayload: ReturnType<typeof validateEvent>;
try {
webhookPayload = validateEvent(
requestBody,
webhookHeaders,
process.env.POLAR_WEBHOOK_SECRET ?? "",
);
} catch (error) {
if (error instanceof WebhookVerificationError) {
return new NextResponse("", { status: 403 });
}
throw error;
}

console.log("Incoming Webhook", webhookPayload.type);

Expand Down
41 changes: 18 additions & 23 deletions templates/nuxt/server/api/polar/webhook/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import { Webhook } from "standardwebhooks";

import type {
WebhookCheckoutCreatedPayload,
WebhookCheckoutUpdatedPayload,
WebhookSubscriptionActivePayload,
WebhookSubscriptionCanceledPayload,
WebhookSubscriptionCreatedPayload,
WebhookSubscriptionRevokedPayload,
WebhookSubscriptionUpdatedPayload,
} from "@polar-sh/sdk/models/components";
import {
validateEvent,
WebhookVerificationError,
} from "@polar-sh/sdk/webhooks";
import { defineEventHandler, readBody } from "h3";

type WebhookEvent =
| WebhookCheckoutCreatedPayload
| WebhookCheckoutUpdatedPayload
| WebhookSubscriptionCreatedPayload
| WebhookSubscriptionActivePayload
| WebhookSubscriptionCanceledPayload
| WebhookSubscriptionUpdatedPayload
| WebhookSubscriptionRevokedPayload;

export default defineEventHandler(async (event) => {
const { polarWebhookSecret } = useRuntimeConfig(event);

Expand All @@ -31,9 +15,20 @@ export default defineEventHandler(async (event) => {
"webhook-signature": getHeader(event, "webhook-signature") ?? "",
};

const webhookSecret = Buffer.from(polarWebhookSecret).toString("base64");
const wh = new Webhook(webhookSecret);
const webhookPayload = wh.verify(requestBody, webhookHeaders) as WebhookEvent;
let webhookPayload: ReturnType<typeof validateEvent>;
try {
webhookPayload = validateEvent(
requestBody,
webhookHeaders,
process.env.POLAR_WEBHOOK_SECRET ?? "",
);
} catch (error) {
if (error instanceof WebhookVerificationError) {
setResponseStatus(event, 403);
return {};
}
throw error;
}

console.log("Incoming Webhook", webhookPayload.type);

Expand Down