This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.ts
73 lines (57 loc) · 1.52 KB
/
function.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Request, Response } from "express"
import { ManagementClient } from "auth0"
import { graphql } from "@octokit/graphql"
export const ghProxy = async (request: Request, response: Response) => {
response.set("Access-Control-Allow-Origin", "*")
if (request.method === "OPTIONS") {
response.set("Access-Control-Allow-Methods", "POST")
response.set("Access-Control-Allow-Headers", "Content-Type")
response.set("Access-Control-Max-Age", "3600")
response.status(204).send("")
return
}
const requestBody = JSON.parse(request.body)
/*
Request the Auth0 Management API access_token
*/
const auth0 = new ManagementClient({
domain: process.env.AUTH0_URL,
clientId: process.env.AUTH0_ID,
clientSecret: process.env.AUTH0_SECRET
})
let user
try {
user = await auth0.getUser({ id: requestBody.user_id })
.catch((reason) => { throw reason })
}
catch (reason) {
response.status(reason.statusCode).json({
reason: reason.message
})
return
}
/*
Use the access_token stored in the retrieved identity to make the GH API
request.
*/
const access_token = user.identities[0].access_token
const { query, variables } = requestBody
let queryResult
try {
queryResult = await graphql(
query,
{
...variables,
headers: {
authorization: `bearer ${access_token}`
}
}
)
}
catch (error) {
response.status(400).json({
...error
})
}
response.status(200).json(queryResult)
}