Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwidlund committed Oct 30, 2024
1 parent 625ed09 commit 4295e7e
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 218 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
with:
version: latest
- name: Run Biome
run: biome ci .
run: biome ci ./src
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node --no-warnings

import '../dist/cli.js';
import "../dist/cli.js";
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
"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",
Expand Down Expand Up @@ -59,9 +56,7 @@
"ts": "module",
"tsx": "module"
},
"nodeArguments": [
"--loader=ts-node/esm"
]
"nodeArguments": ["--loader=ts-node/esm"]
},
"xo": {
"extends": "xo-react",
Expand Down
2 changes: 1 addition & 1 deletion templates/next/api/webhook/polar/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ export async function POST(request: NextRequest) {
}

return NextResponse.json({ received: true });
}
}
2 changes: 1 addition & 1 deletion templates/next/checkout/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { polar } from "../../polar";
import { type NextRequest, NextResponse } from "next/server";
import { polar } from "../../polar";

export async function GET(req: NextRequest) {
const url = new URL(req.url);
Expand Down
109 changes: 57 additions & 52 deletions templates/next/products/page.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,69 @@
import Link from "next/link";
import { polar } from "../../polar";
import type { Product } from "@polar-sh/sdk/models/components";
import Link from "next/link";
import { useMemo } from "react";
import { polar } from "../../polar";

interface ProductCardProps {
product: Product
product: Product;
}

export const ProductCard = ({ product }: ProductCardProps) => {
// Handling just a single price for now
// Remember to handle multiple prices for products if you support monthly & yearly pricing plans
const firstPrice = product.prices[0]
// Handling just a single price for now
// Remember to handle multiple prices for products if you support monthly & yearly pricing plans
const firstPrice = product.prices[0];

const price = useMemo(() => {
switch(firstPrice.amountType) {
case 'fixed':
// The Polar API returns prices in cents - Convert to dollars for display
return `$${firstPrice.priceAmount / 100}`
case 'free':
return 'Free'
default:
return 'Pay what you want'
}
}, [firstPrice])
const price = useMemo(() => {
switch (firstPrice.amountType) {
case "fixed":
// The Polar API returns prices in cents - Convert to dollars for display
return `$${firstPrice.priceAmount / 100}`;
case "free":
return "Free";
default:
return "Pay what you want";
}
}, [firstPrice]);

return (
<div className="flex flex-col gap-y-24 justify-between p-12 rounded-3xl bg-neutral-950 h-full border border-neutral-900">
<div className="flex flex-col gap-y-8">
<h1 className="text-3xl">{product.name}</h1>
<p className="text-neutral-400">{product.description}</p>
<ul>
{product.benefits.map((benefit) => (
<li key={benefit.id} className="flex flex-row gap-x-2 items-center">
{benefit.description}
</li>
))}
</ul>
</div>
<div className="flex flex-row gap-x-4 justify-between items-center">
<Link className="h-8 flex flex-row items-center justify-center rounded-full bg-white text-black font-medium px-4" href={`/checkout?priceId=${firstPrice.id}`}>Buy</Link>
<span className="text-neutral-500">{price}</span>
</div>
</div>
)
}
return (
<div className="flex flex-col gap-y-24 justify-between p-12 rounded-3xl bg-neutral-950 h-full border border-neutral-900">
<div className="flex flex-col gap-y-8">
<h1 className="text-3xl">{product.name}</h1>
<p className="text-neutral-400">{product.description}</p>
<ul>
{product.benefits.map((benefit) => (
<li key={benefit.id} className="flex flex-row gap-x-2 items-center">
{benefit.description}
</li>
))}
</ul>
</div>
<div className="flex flex-row gap-x-4 justify-between items-center">
<Link
className="h-8 flex flex-row items-center justify-center rounded-full bg-white text-black font-medium px-4"
href={`/checkout?priceId=${firstPrice.id}`}
>
Buy
</Link>
<span className="text-neutral-500">{price}</span>
</div>
</div>
);
};

export default async function Page() {
const { result } = await polar.products.list({
organizationId: process.env.POLAR_ORGANIZATION_ID!,
isArchived: false // Only fetch products which are published
})
const { result } = await polar.products.list({
organizationId: process.env.POLAR_ORGANIZATION_ID!,
isArchived: false, // Only fetch products which are published
});

return (
<div className="flex flex-col gap-y-32">
<h1 className="text-5xl">Products</h1>
<div className="grid grid-cols-4 gap-12">
{result.items.map((product) => (
<ProductCard key={product.id} product={product} />
))}
</div>
</div>
);
}
return (
<div className="flex flex-col gap-y-32">
<h1 className="text-5xl">Products</h1>
<div className="grid grid-cols-4 gap-12">
{result.items.map((product) => (
<ProductCard key={product.id} product={product} />
))}
</div>
</div>
);
}
4 changes: 2 additions & 2 deletions templates/nuxt/app/pages/checkout/confirmation/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
const route = useRoute()
const checkoutId = route.query.checkout_id
const route = useRoute();
const checkoutId = route.query.checkout_id;
</script>

<template>
Expand Down
37 changes: 18 additions & 19 deletions templates/nuxt/app/pages/products/index.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
<script setup lang="ts">
import type { Product } from '@polar-sh/sdk/models/components'
import type { ProductPrice } from '@polar-sh/sdk/src/models/components/productprice'
import type { Product } from "@polar-sh/sdk/models/components";
import type { ProductPrice } from "@polar-sh/sdk/src/models/components/productprice";
const products = ref<Product[]>([])
const products = ref<Product[]>([]);
// This could be a global store (such a pinia) if your app has one
const fetchPolarProducts = async () => {
return await useRequestFetch()<Product[]>(`/api/polar/products`)
}
return await useRequestFetch()<Product[]>(`/api/polar/products`);
};
const { data } = await useAsyncData(
'fetchPolarProducts',
() => fetchPolarProducts()
)
products.value = data.value ?? []
const { data } = await useAsyncData("fetchPolarProducts", () =>
fetchPolarProducts(),
);
products.value = data.value ?? [];
const formatPrice = (price: ProductPrice) => {
switch (price.amountType) {
case 'fixed':
return `$${price.priceAmount / 100}`
case 'free':
return 'Free'
default:
return 'Pay what you want'
}
}
switch (price.amountType) {
case "fixed":
return `$${price.priceAmount / 100}`;
case "free":
return "Free";
default:
return "Pay what you want";
}
};
</script>

<template>
Expand Down
19 changes: 11 additions & 8 deletions templates/nuxt/app/utils/polar.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Polar } from '@polar-sh/sdk'
import type { ServerList } from '@polar-sh/sdk/src/lib/config'
import { Polar } from "@polar-sh/sdk";
import type { ServerList } from "@polar-sh/sdk/src/lib/config";

export const usePolar = (accessToken: string, server: keyof typeof ServerList) => {
return new Polar({
accessToken,
server,
})
}
export const usePolar = (
accessToken: string,
server: keyof typeof ServerList,
) => {
return new Polar({
accessToken,
server,
});
};
57 changes: 29 additions & 28 deletions templates/nuxt/server/api/checkout/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import { defineEventHandler, sendRedirect } from 'h3'
import type { ServerList } from '@polar-sh/sdk/src/lib/config'
import { usePolar } from '~/utils/polar'
import type { ServerList } from "@polar-sh/sdk/src/lib/config";
import { defineEventHandler, sendRedirect } from "h3";
import { usePolar } from "~/utils/polar";

export default defineEventHandler(async (event) => {
const { priceId } = getQuery(event)
if (!priceId) throw Error('MISSING_PRICE_ID_QUERY_PARAM')
const productPriceId = priceId.toString()
const { priceId } = getQuery(event);
if (!priceId) throw Error("MISSING_PRICE_ID_QUERY_PARAM");
const productPriceId = priceId.toString();

const url = getRequestURL(event)
url.pathname = '/checkout/confirmation?checkout_id={CHECKOUT_ID}'
const successUrl = url.toString()
const url = getRequestURL(event);
url.pathname = "/checkout/confirmation?checkout_id={CHECKOUT_ID}";
const successUrl = url.toString();

const {
polarAccessToken, polarServer,
} = useRuntimeConfig(event)
const { polarAccessToken, polarServer } = useRuntimeConfig(event);

const polar = usePolar(polarAccessToken, polarServer as keyof typeof ServerList)
const polar = usePolar(
polarAccessToken,
polarServer as keyof typeof ServerList,
);

try {
const result = await polar.checkouts.custom.create({
productPriceId,
successUrl,
})
try {
const result = await polar.checkouts.custom.create({
productPriceId,
successUrl,
});

return sendRedirect(event, result.url)
} catch (error) {
console.error(error)
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error',
fatal: true,
})
}
})
return sendRedirect(event, result.url);
} catch (error) {
console.error(error);
throw createError({
statusCode: 500,
statusMessage: "Internal Server Error",
fatal: true,
});
}
});
30 changes: 16 additions & 14 deletions templates/nuxt/server/api/polar/products/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { ServerList } from '@polar-sh/sdk/src/lib/config'
import { usePolar } from '~/utils/polar'
import type { ServerList } from "@polar-sh/sdk/src/lib/config";
import { usePolar } from "~/utils/polar";

export default defineEventHandler({
handler: async (event) => {
const {
polarAccessToken, polarServer, polarOrganizationId,
} = useRuntimeConfig(event)
handler: async (event) => {
const { polarAccessToken, polarServer, polarOrganizationId } =
useRuntimeConfig(event);

const polar = usePolar(polarAccessToken, polarServer as keyof typeof ServerList)
const polar = usePolar(
polarAccessToken,
polarServer as keyof typeof ServerList,
);

const { result } = await polar.products.list({
organizationId: polarOrganizationId,
isArchived: false, // Only fetch products which are published
})
return result.items
},
})
const { result } = await polar.products.list({
organizationId: polarOrganizationId,
isArchived: false, // Only fetch products which are published
});
return result.items;
},
});
Loading

0 comments on commit 4295e7e

Please sign in to comment.