Skip to content

Instantly share code, notes, and snippets.

@Marenthyu
Created November 13, 2020 23:01
Show Gist options
  • Save Marenthyu/2cf3f8ce49edbb1372562901300462df to your computer and use it in GitHub Desktop.
Save Marenthyu/2cf3f8ce49edbb1372562901300462df to your computer and use it in GitHub Desktop.
simple node Proxy for node because JAVA doesn't do PATCH requests
let http = require('http');
let url = require('url');
let got = require('got');
http.createServer((req, res) => {
handler(req, res).then().catch();
}).listen(2950);
async function handler(req, res) {
console.log("Got request");
let body = '';
req.on('data', (data) => {
body += data;
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6) {
req.connection.destroy();
res.writeHead(400, 'Too much data');
res.end();
}
});
req.on('end', () => {
secondhandler(req, res, body).then().catch();
});
}
async function secondhandler(req, res, body) {
let q = url.parse(req.url, true);
let response = await got({
method: 'PATCH',
url: `https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions?broadcaster_id=${
encodeURIComponent(q.query['broadcaster_id'])}&id=${
encodeURIComponent(q.query['id'])}&reward_id=${
encodeURIComponent(q.query['reward_id'])}`,
headers: {
"Authorization": req.headers["authorization"],
"Client-ID": req.headers['client-id']
},
responseType: "json",
json: JSON.parse(body)
}).catch((e) => {
console.log(JSON.stringify(e));
res.writeHead(e.response.statusCode, "IDFK", e.response.headers);
res.end(e.response.body.toString());
});
console.log(JSON.stringify(response.body));
res.writeHead(response.statusCode, "IDFK", response.headers);
res.end(response.body.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment