Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
leerob.io/pages/api/gumroad.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
33 lines (27 sloc)
867 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { NextApiRequest, NextApiResponse } from 'next'; | |
import Big from 'big.js'; | |
const getProductSales = async (id) => { | |
const response = await fetch(`https://api.gumroad.com/v2/products/${id}`, { | |
headers: { | |
Authorization: `Bearer ${process.env.GUMROAD_API_KEY}`, | |
'Content-Type': 'application/json' | |
}, | |
method: 'GET' | |
}); | |
const { product } = await response.json(); | |
return new Big(product.sales_usd_cents).div(100); | |
}; | |
export default async function handler( | |
req: NextApiRequest, | |
res: NextApiResponse | |
) { | |
const masteringNextSales = await getProductSales('sDpG'); | |
const react2025Sales = await getProductSales('TifxZ'); | |
res.setHeader( | |
'Cache-Control', | |
'public, s-maxage=1200, stale-while-revalidate=600' | |
); | |
return res.status(200).json({ | |
sales: masteringNextSales.plus(react2025Sales).toFixed(0) | |
}); | |
} |