Skip to content
Merged
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
11 changes: 5 additions & 6 deletions nextjs/generation-based-subscription/bun.lock

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "e1c4a87d-52d4-4112-8d76-80aa1b288c9e",
"id": "66597033-970b-4b5a-b9fc-d2f242c71df4",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
Expand Down Expand Up @@ -104,15 +104,7 @@
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"accounts_provider_id_account_id_unique": {
"name": "accounts_provider_id_account_id_unique",
"columns": [
"provider_id",
"account_id"
]
}
},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "7",
"when": 1762366685322,
"tag": "0000_dizzy_micromax",
"when": 1768190947104,
"tag": "0000_open_maria_hill",
"breakpoints": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion nextjs/generation-based-subscription/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"db:studio": "drizzle-kit studio --config=drizzle.config.ts"
},
"dependencies": {
"@flowglad/nextjs": "0.15.0",
"@flowglad/nextjs": "0.16.2",
"@radix-ui/react-dropdown-menu": "^2.1.16",
"@radix-ui/react-progress": "^1.1.8",
"@radix-ui/react-slot": "^1.2.3",
Expand Down
138 changes: 0 additions & 138 deletions nextjs/generation-based-subscription/src/app/api/usage-events/route.ts

This file was deleted.

52 changes: 22 additions & 30 deletions nextjs/generation-based-subscription/src/app/home-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,26 +165,24 @@ export function HomeClient() {
setGenerateError(null);

try {
// Generate a unique transaction ID for idempotency
const transactionId = `fast_image_${Date.now()}_${Math.random().toString(36).substring(7)}`;
if (!billing.createUsageEvent) {
throw new Error('createUsageEvent is not available');
}

// Random amount between 3-5
const amount = Math.floor(Math.random() * 3) + 3;

const response = await fetch('/api/usage-events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
usageMeterSlug: 'fast_generations',
amount,
transactionId,
}),
const result = await billing.createUsageEvent({
usageMeterSlug: 'fast_generations',
amount,
});

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to create usage event');
if ('error' in result) {
const errorMsg = result.error.json?.error ?? result.error.json?.message;
throw new Error(
(typeof errorMsg === 'string' ? errorMsg : null) ||
'Failed to create usage event'
);
}

// Cycle through mock images
Expand Down Expand Up @@ -217,26 +215,20 @@ export function HomeClient() {
setHdVideoError(null);

try {
// Generate a unique transaction ID for idempotency
const transactionId = `hd_video_${Date.now()}_${Math.random().toString(36).substring(7)}`;
// Random amount between 1-3 minutes
const amount = Math.floor(Math.random() * 3) + 1;

const response = await fetch('/api/usage-events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
usageMeterSlug: 'hd_video_minutes',
amount,
transactionId,
}),
const result = await billing.createUsageEvent({
usageMeterSlug: 'hd_video_minutes',
amount,
});

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to create usage event');
if ('error' in result) {
const errorMsg = result.error.json?.error ?? result.error.json?.message;
throw new Error(
(typeof errorMsg === 'string' ? errorMsg : null) ||
'Failed to create usage event'
);
}
Comment on lines +221 to 232

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing guard for createUsageEvent availability.

handleGenerateFastImage guards against billing.createUsageEvent being undefined (lines 168-170), but this handler does not. For consistency and to prevent potential runtime errors, add the same guard here.

Proposed fix
 const handleGenerateHDVideo = async () => {
   if (!hasHDVideoMinutesAccess || hdVideoMinutesRemaining === 0) {
     return;
   }

   setIsGeneratingHDVideo(true);
   setHdVideoError(null);

   try {
+    if (!billing.createUsageEvent) {
+      throw new Error('createUsageEvent is not available');
+    }
+
     // Generate a unique transaction ID for idempotency
     const transactionId = `hd_video_${Date.now()}_${Math.random().toString(36).substring(7)}`;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const result = await billing.createUsageEvent({
usageMeterSlug: 'hd_video_minutes',
amount,
transactionId,
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to create usage event');
if ('error' in result) {
throw new Error(
(result.error.json?.error as string) || 'Failed to create usage event'
);
}
const handleGenerateHDVideo = async () => {
if (!hasHDVideoMinutesAccess || hdVideoMinutesRemaining === 0) {
return;
}
setIsGeneratingHDVideo(true);
setHdVideoError(null);
try {
if (!billing.createUsageEvent) {
throw new Error('createUsageEvent is not available');
}
// Generate a unique transaction ID for idempotency
const transactionId = `hd_video_${Date.now()}_${Math.random().toString(36).substring(7)}`;
const result = await billing.createUsageEvent({
usageMeterSlug: 'hd_video_minutes',
amount,
transactionId,
});
if ('error' in result) {
throw new Error(
(result.error.json?.error as string) || 'Failed to create usage event'
);
}
🤖 Prompt for AI Agents
In @nextjs/generation-based-subscription/src/app/home-client.tsx around lines
221 - 231, The call to billing.createUsageEvent in this handler lacks the same
undefined guard used in handleGenerateFastImage; before invoking
billing.createUsageEvent (and before using amount/transactionId), check that
billing.createUsageEvent is a function and return or throw early if it's
undefined, mirroring the guard in handleGenerateFastImage so you avoid runtime
errors when createUsageEvent is not provided.


// Cycle through mock video GIFs
Expand Down
10 changes: 5 additions & 5 deletions nextjs/pay-as-you-go/bun.lock

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

2 changes: 1 addition & 1 deletion nextjs/pay-as-you-go/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"db:studio": "drizzle-kit studio --config=drizzle.config.ts"
},
"dependencies": {
"@flowglad/nextjs": "0.15.0",
"@flowglad/nextjs": "0.16.2",
"@radix-ui/react-dropdown-menu": "^2.1.16",
"@radix-ui/react-progress": "^1.1.8",
"@radix-ui/react-scroll-area": "^1.2.10",
Expand Down
Loading