Socialify

Folder ..

Viewing stats.svg.ts
45 lines (39 loc) • 989.0 B

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { NextRequest } from 'next/server'
import { badgen } from 'badgen'
import statsEndpoint from './stats'

const statsSvgEndpoint = async (req: NextRequest) => {
  let totalCount = 0

  try {
    const apiResponse = await (await statsEndpoint(req)).json()
    if (apiResponse.total_count) {
      totalCount = apiResponse.total_count
    }
  } catch (ex) {
    console.error(ex)
  }

  const svg = totalCount
    ? badgen({
        subject: '',
        status: `${totalCount} repositories`,
        color: 'black',
        style: 'flat'
      })
    : badgen({
        subject: '',
        status: `thousands of repositories`,
        color: 'black',
        style: 'flat'
      })

  return new Response(svg, {
    status: 200,
    headers: {
      'content-type': 'image/svg+xml',
      'cache-control':
        'public, immutable, no-transform, max-age=60, s-maxage=86400'
    }
  })
}

export const config = {
  runtime: 'experimental-edge'
}

export default statsSvgEndpoint