Socialify

Folder ..

Viewing graphql.ts
49 lines (42 loc) • 1.1 KB

 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
46
47
48
49
import type { NextRequest } from 'next/server'

const API_ENDPOINT = 'https://api.github.com/graphql'

const graphQLEndpoint = async (req: NextRequest) => {
  if (req.method !== 'POST') {
    return new Response('Method Not Allowed', {
      status: 405,
      headers: {
        'cache-control': 'max-age=0, public'
      }
    })
  }

  const response = await fetch(API_ENDPOINT, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
      'content-type': 'application/json'
    },
    body: req.body
  })

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

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

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

export default graphQLEndpoint