Skip to content

Commit

Permalink
fix(astro): bypass durable object for local dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonianb committed Aug 2, 2024
1 parent dd60d32 commit 3a27e01
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions packages/codius-astro/src/components/AppBilling.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,42 @@ import CardField from "@/components/CardField.astro"
import { Label } from "@/components/ui/label"
import { CostTooltip } from "@/components/CostTooltip"
import { getWorkerTotalCost, nanoUSDToString } from "@/lib/utils"
import type { WorkerBilling } from "billing-durable-object"
interface Props {
appId: string
}
const { appId } = Astro.props
const billingId: DurableObjectId =
Astro.locals.runtime.env.BILLING_DURABLE_OBJECT.idFromName(appId)
const billing = Astro.locals.runtime.env.BILLING_DURABLE_OBJECT.get(billingId)
const workerBilling = await billing.getWorkerBilling()
const getWorkerBilling = async (): Promise<WorkerBilling> => {
// Workaround for:
// Cannot access `BillingDurableObject#getWorkerBilling` as Durable Object RPC is not yet supported between multiple `wrangler dev` sessions.
if (process.env.NODE_ENV === "development") {
return {
requests: {
total: 0n,
totalAllowed: 1000000n,
},
funding: {
totalNanoUSD: 1_000_000_000n,
},
pricing: {
unitPriceNanoUSD: 300_000_000n,
requestsPerUnit: 1_000_000n,
includedRequests: 100n,
},
}
} else {
const billingId: DurableObjectId =
Astro.locals.runtime.env.BILLING_DURABLE_OBJECT.idFromName(appId)
const billing =
Astro.locals.runtime.env.BILLING_DURABLE_OBJECT.get(billingId)
return await billing.getWorkerBilling()
}
}
const workerBilling = await getWorkerBilling()
const totalCost = getWorkerTotalCost(workerBilling)
---
Expand Down

0 comments on commit 3a27e01

Please sign in to comment.