-
Notifications
You must be signed in to change notification settings - Fork 7
/
submit.js
66 lines (59 loc) · 1.39 KB
/
submit.js
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
require("dotenv").config();
const axios = require("axios");
const headers = {
"content-type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "Content-Type",
};
function preflight() {
return {
statusCode: 204,
headers,
body: {},
};
}
function submit(data) {
const owner = "codegram";
const name = "jamstack-cfp";
return axios.request({
url: `https://api.github.com/repos/${owner}/${name}/dispatches`,
method: "post",
headers: {
Accept: "application/vnd.github.everest-preview+json",
Authorization: `token ${process.env.GH_TOKEN_PERSONAL}`,
},
data: {
event_type: "handle-submission",
client_payload: {
name: data.name,
title: data.title,
description: data.description,
date: new Date().getTime(),
},
},
});
}
export async function handler(request) {
if (request.httpMethod === "OPTIONS") {
return preflight();
}
const body = JSON.parse(request.body);
return submit(body)
.then(() => {
return {
statusCode: 200,
headers,
body: JSON.stringify({
message: `Success`,
}),
};
})
.catch(({ response }) => {
return {
statusCode: response.status || 500,
headers,
body: JSON.stringify(response.data),
};
});
}