Socialify

Folder ..

Viewing stats.ts
42 lines (37 loc) • 1022.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
import type { NextRequest } from 'next/server'

const statsEndpoint = async (req: NextRequest) => {
  const response = await fetch(
    `https://api.github.com/search/code?per_page=1&q=${encodeURIComponent(
      'socialify.git.ci'
    )}`,
    {
      method: 'GET',
      headers: {
        Accept: 'application/vnd.github.v3+json',
        Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
        'content-type': 'application/json'
      }
    }
  )

  if (!response.ok) {
    return new Response(await response.text(), {
      status: response.status,
      headers: {
        'cache-control': 'public, max-age=0'
      }
    })
  }

  const json = await response.json()
  return new Response(JSON.stringify({ total_count: json.total_count }), {
    status: 200,
    headers: {
      'content-type': 'application/json',
      'cache-control':
        'public, immutable, no-transform, max-age=60, s-maxage=86400'
    }
  })
}

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

export default statsEndpoint