Skip to content

Installation-only-plans prototype #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 5 additions & 4 deletions app/v1/installations/[installationId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const GET = withAuth(async (claims) => {
(plan) => plan.id === installation.billingPlanId
);
return Response.json({
metadata: installation.metadata,
billingPlan: {
...billingPlan,
scope: "installation",
Expand All @@ -72,10 +73,10 @@ export const PATCH = withAuth(async (claims, request) => {
return new Response(null, { status: 400 });
}

await updateInstallation(
claims.installation_id,
requestBody.data.billingPlanId
);
await updateInstallation(claims.installation_id, {
billingPlanId: requestBody.data.billingPlanId,
metadata: requestBody.data.metadata,
});

return new Response(null, { status: 204 });
});
7 changes: 7 additions & 0 deletions app/v1/installations/new/plans/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getNewInstallationtBillingPlans } from "@/lib/partner";
import { withAuth } from "@/lib/vercel/auth";

export const GET = withAuth(async () => {
const billingPlans = await getNewInstallationtBillingPlans();
return Response.json(billingPlans);
});
25 changes: 22 additions & 3 deletions lib/partner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,18 @@ export async function installIntegration(

export async function updateInstallation(
installationId: string,
billingPlanId: string
{
billingPlanId,
metadata,
}: { billingPlanId: string; metadata?: Record<string, unknown> }
): Promise<void> {
const installation = await getInstallation(installationId);
const pipeline = kv.pipeline();
await pipeline.set(installationId, { ...installation, billingPlanId });
await pipeline.set(installationId, {
...installation,
billingPlanId,
metadata: metadata !== undefined ? metadata : installation.metadata,
});
await pipeline.exec();
}

Expand Down Expand Up @@ -140,7 +147,9 @@ export async function provisionResource(
serializeResource(resource)
);
await kv.lpush(`${installationId}:resources`, resource.id);
await updateInstallation(installationId, request.billingPlanId);
await updateInstallation(installationId, {
billingPlanId: request.billingPlanId,
});

return {
...resource,
Expand Down Expand Up @@ -421,6 +430,14 @@ export async function getInstallationtBillingPlans(
};
}

export async function getNewInstallationtBillingPlans(
_experimental_metadata?: Record<string, unknown>
): Promise<GetBillingPlansResponse> {
return {
plans: billingPlans,
};
}

export async function getProductBillingPlans(
_productId: string,
installationId: string,
Expand All @@ -446,13 +463,15 @@ export async function getInstallation(installationId: string): Promise<
InstallIntegrationRequest & {
type: "marketplace" | "external";
billingPlanId: string;
metadata?: Record<string, unknown>;
deletedAt?: number;
}
> {
const installation = await kv.get<
InstallIntegrationRequest & {
type: "marketplace" | "external";
billingPlanId: string;
metadata?: Record<string, unknown>;
deletedAt?: number;
}
>(installationId);
Expand Down
2 changes: 1 addition & 1 deletion lib/vercel/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function verifyToken(token: string): Promise<OidcClaims> {
);

if (claims.aud !== env.INTEGRATION_CLIENT_ID) {
throw new AuthError("Invalid audience");
throw new AuthError(`Invalid audience: ${claims.aud}`);
}

if (claims.iss !== "https://marketplace.vercel.com") {
Expand Down
7 changes: 5 additions & 2 deletions lib/vercel/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const usageTypeSchema = z.enum(["total", "interval", "rate"]);

export type UsageType = z.infer<typeof usageTypeSchema>;

const metadataSchema = z.record(z.unknown());

// Account and Installation

export const installIntegrationRequestSchema = z.object({
Expand All @@ -30,10 +32,13 @@ export const installIntegrationRequestSchema = z.object({
access_token: z.string().min(1),
token_type: z.string().min(1),
}),
billingPlanId: z.string().optional(),
metadata: metadataSchema.optional(),
});

export const updateInstallationRequestSchema = z.object({
billingPlanId: z.string(),
metadata: metadataSchema.optional(),
});

export type InstallIntegrationRequest = z.infer<
Expand Down Expand Up @@ -169,8 +174,6 @@ export type ProvisionPurchaseResponse = z.infer<

// Product

const metadataSchema = z.record(z.unknown());

const notificationSchema = z.object({
level: z.enum(["info", "warn", "error"]),
title: z.string().max(100),
Expand Down