cli-release-version.ts 975 B

123456789101112131415161718192021222324252627282930313233
  1. import { NextApiRequest, NextApiResponse } from 'next'
  2. type GitHubRepositoryRelease = {
  3. id: number
  4. url: string
  5. name: string
  6. tag_name: string
  7. published_at: string
  8. }
  9. const current = process.env.CURRENT_CLI_VERSION ? `v${process.env.CURRENT_CLI_VERSION}` : undefined
  10. const handler = async (_req: NextApiRequest, res: NextApiResponse) => {
  11. try {
  12. const { tag_name: latest, published_at }: GitHubRepositoryRelease = await fetch(
  13. 'https://api.github.com/repos/briven/cli/releases/latest'
  14. ).then((res) => res.json())
  15. const data: GitHubRepositoryRelease[] = await fetch(
  16. 'https://api.github.com/repos/briven/cli/releases?per_page=1'
  17. )
  18. .then((res) => res.json())
  19. // Ignore errors fetching beta release version
  20. .catch(() => [])
  21. const beta = data[0]?.tag_name
  22. return res.status(200).json({ current, latest, beta, published_at })
  23. } catch {
  24. return res.status(200).json({ current })
  25. }
  26. }
  27. export default handler