-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.ts
51 lines (43 loc) · 1.17 KB
/
vote.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
import config from "src/utils/config";
import type { NextApiRequest, NextApiResponse } from 'next'
var aws4 = require('aws4')
type ResponseData = {
[key: string]: string
}
export default async function handler(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const method = req.method;
const body = JSON.parse(req.body);
const host = config.Api.ENDPOINT_GATEWAY;
const path = '/dev/vote';
const service = 'execute-api';
const headers = {
'x-api-key': `${config.Api.APIKEY}`
}
var opts = {
host,
path,
service,
method,
headers,
body: JSON.stringify(body)
}
var signedRequest = aws4.sign(opts, {
accessKeyId: body.credentials.AccessKeyId,
secretAccessKey: body.credentials.SecretAccessKey,
sessionToken: body.credentials.SessionToken
})
switch (method) {
case 'POST':
try {
const response = await fetch(`${config.Api.ENDPOINT}/vote`, {
headers: signedRequest.headers,
method,
body: JSON.stringify(body)
});
const data = await response.json();
return res.status(200).json(data)
} catch (error) {
console.error(error);
}
}
}