mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 11:03:49 +00:00
refactor: product page data fetching
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { hasProductAccess } from '$lib/utils/supabase/admin';
|
||||
import type { ProductInfo } from '$lib/types/Products/ProductInfo';
|
||||
|
||||
export const load = async ({ params, locals: { safeGetSession }, fetch }) => {
|
||||
const { session } = await safeGetSession();
|
||||
@@ -8,7 +9,7 @@ export const load = async ({ params, locals: { safeGetSession }, fetch }) => {
|
||||
}
|
||||
|
||||
const productResponse = await fetch(`/products/${params.productId}`);
|
||||
const product = await productResponse.json();
|
||||
const { product, isSubscription, price }: ProductInfo = await productResponse.json();
|
||||
|
||||
// Create the item object
|
||||
const item = {
|
||||
@@ -17,17 +18,13 @@ export const load = async ({ params, locals: { safeGetSession }, fetch }) => {
|
||||
categoryDescription: product.description,
|
||||
imgAlt: product.description,
|
||||
imgSrc: product.images[0],
|
||||
price: 0,
|
||||
priceId: product.default_price,
|
||||
quantity: 1,
|
||||
isSubscription: product.isSubscription
|
||||
isSubscription: isSubscription,
|
||||
price: (price.unit_amount ? price.unit_amount / 100 : NaN),
|
||||
interval: price.recurring ? price.recurring.interval : null,
|
||||
}
|
||||
|
||||
// Fetch the price of the product
|
||||
const priceResponse = await fetch(`/products/prices/${product.default_price}`);
|
||||
const price = await priceResponse.json();
|
||||
// Set the price of the product
|
||||
item.price = price.unit_amount / 100;
|
||||
|
||||
|
||||
// Check if the user has access to the 'EXAMPLE PRODUCT' product
|
||||
const hasPurchasedProduct = await hasProductAccess(session.user.id, params.productId);
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
||||
import Stripe from 'stripe';
|
||||
import { type ProductInfo } from '$lib/types/Products/ProductInfo';
|
||||
|
||||
export const GET = async ({ params }) => {
|
||||
const { productId } = params;
|
||||
const product = await stripeClient.products.retrieve(productId);
|
||||
|
||||
const price = await stripeClient.prices.retrieve(product.default_price);
|
||||
const price = await stripeClient.prices.retrieve(product.default_price as string);
|
||||
|
||||
const isSubscription = price.type === ('recurring' as Stripe.Price.type);
|
||||
product.isSubscription = isSubscription;
|
||||
return json(product);
|
||||
const isSubscription = price.type === ('recurring' as Stripe.Price.Type);
|
||||
|
||||
const productInfo: ProductInfo = {
|
||||
product,
|
||||
price,
|
||||
isSubscription
|
||||
}
|
||||
|
||||
return json(productInfo);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user