Skip to content

Instantly share code, notes, and snippets.

@remy
Created August 16, 2021 21:49
Show Gist options
  • Save remy/0aee145a71044f07e822c36894ef52bb to your computer and use it in GitHub Desktop.
Save remy/0aee145a71044f07e822c36894ef52bb to your computer and use it in GitHub Desktop.
var http = require("http");
var httpProxy = require("http-proxy");
var modifyResponse = require("node-http-proxy-json");
// Create a proxy server
var proxy = httpProxy.createProxyServer({
target: "https://api.spotify.com",
});
function hasShow(item) {
if (item.target.uri.startsWith("spotify:show:")) {
return false;
}
if (item.target.uri.startsWith("spotify:episode:")) {
return false;
}
return true;
}
// Listen for the `proxyRes` event on `proxy`.
proxy.on("proxyRes", function (proxyRes, req, res) {
modifyResponse(res, proxyRes, function (body) {
console.log("inbound");
console.log(req.url);
if (body) {
// return body;
// modify some information
if (body.body) {
body.body[0].text.title = "Waasssssup?";
const newBody = body.body.map((item) => {
if (item.children) {
item.children = item.children.filter(hasShow);
if (item.children.length === 0) {
return false;
}
}
return item;
});
const stripped = [];
for (let i = 0; i < body.body.length; i++) {
const item = body.body[i];
const newItem = newBody[i];
if (!newItem) {
continue;
}
if (item.text && item.text.title) {
console.log(item.text.title);
const t = item.text.title.toLowerCase();
if (t.includes("podcast") || t.includes("show")) {
i++; // skip the next block too
continue;
}
}
stripped.push(item);
}
body.body = stripped;
}
console.log(body);
}
return body; // return value can be a promise
});
});
// Create your server and then proxies the request
var server = http
.createServer(function (req, res) {
proxy.web(req, res);
})
.listen(6666);
{
"name": "transform-proxy",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"http-proxy": "^1.18.1",
"node-http-proxy-json": "^0.1.9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment