mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
editorconfig
This commit is contained in:
68
api/axios.js
68
api/axios.js
@@ -5,53 +5,53 @@ const { baseAPIURL, accountsAPIURL } = require("../constants");
|
||||
const logger = require("../utils/logger")(module);
|
||||
|
||||
const authInstance = axios.create({
|
||||
baseURL: accountsAPIURL,
|
||||
timeout: 20000,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Authorization": "Basic " + (Buffer.from(process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET).toString("base64"))
|
||||
},
|
||||
baseURL: accountsAPIURL,
|
||||
timeout: 20000,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Authorization": "Basic " + (Buffer.from(process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET).toString("base64"))
|
||||
},
|
||||
});
|
||||
|
||||
const uncappedAxiosInstance = axios.create({
|
||||
baseURL: baseAPIURL,
|
||||
timeout: 20000,
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
baseURL: baseAPIURL,
|
||||
timeout: 20000,
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
});
|
||||
|
||||
const axiosInstance = rateLimit(uncappedAxiosInstance, {
|
||||
maxRequests: 10,
|
||||
perMilliseconds: 5000,
|
||||
maxRequests: 10,
|
||||
perMilliseconds: 5000,
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.request.use(config => {
|
||||
logger.http("API call", {
|
||||
url: config.url,
|
||||
method: config.method,
|
||||
params: config.params ?? {},
|
||||
headers: Object.keys(config.headers),
|
||||
});
|
||||
return config;
|
||||
logger.http("API call", {
|
||||
url: config.url,
|
||||
method: config.method,
|
||||
params: config.params ?? {},
|
||||
headers: Object.keys(config.headers),
|
||||
});
|
||||
return config;
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
logger.warn("AxiosError", {
|
||||
error: {
|
||||
name: error.name,
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
},
|
||||
req: error.config,
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
(response) => response,
|
||||
(error) => {
|
||||
logger.warn("AxiosError", {
|
||||
error: {
|
||||
name: error.name,
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
},
|
||||
req: error.config,
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
module.exports = {
|
||||
authInstance,
|
||||
axiosInstance
|
||||
authInstance,
|
||||
axiosInstance
|
||||
};
|
||||
|
||||
226
api/spotify.js
226
api/spotify.js
@@ -18,147 +18,147 @@ const logPrefix = "Spotify API: ";
|
||||
* @param {boolean} inlineData true if data is to be placed inside config
|
||||
*/
|
||||
const singleRequest = async (req, res, method, path, config = {}, data = null, inlineData = false) => {
|
||||
let resp;
|
||||
config.headers = { ...config.headers, ...req.sessHeaders };
|
||||
try {
|
||||
if (!data || (data && inlineData)) {
|
||||
if (data)
|
||||
config.data = data ?? null;
|
||||
resp = await axiosInstance[method.toLowerCase()](path, config);
|
||||
} else
|
||||
resp = await axiosInstance[method.toLowerCase()](path, data, config);
|
||||
let resp;
|
||||
config.headers = { ...config.headers, ...req.sessHeaders };
|
||||
try {
|
||||
if (!data || (data && inlineData)) {
|
||||
if (data)
|
||||
config.data = data ?? null;
|
||||
resp = await axiosInstance[method.toLowerCase()](path, config);
|
||||
} else
|
||||
resp = await axiosInstance[method.toLowerCase()](path, data, config);
|
||||
|
||||
logger.debug(logPrefix + "Successful response received.");
|
||||
return resp;
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
// Non 2XX response received
|
||||
let logMsg;
|
||||
if (error.response.status >= 400 && error.response.status < 600) {
|
||||
res.status(error.response.status).send(error.response.data);
|
||||
logMsg = "" + error.response.status
|
||||
}
|
||||
else {
|
||||
res.sendStatus(error.response.status);
|
||||
logMsg = "???";
|
||||
}
|
||||
logger.warn(logPrefix + logMsg, {
|
||||
response: {
|
||||
data: error.response.data,
|
||||
status: error.response.status,
|
||||
}
|
||||
});
|
||||
} else if (error.request) {
|
||||
// No response received
|
||||
res.status(504).send({ message: "No response from Spotify" });
|
||||
logger.error(logPrefix + "No response", { error });
|
||||
} else {
|
||||
// Something happened in setting up the request that triggered an Error
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error(logPrefix + "Request failed?", { error });
|
||||
}
|
||||
logger.debug(logPrefix + "Successful response received.");
|
||||
return resp;
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
// Non 2XX response received
|
||||
let logMsg;
|
||||
if (error.response.status >= 400 && error.response.status < 600) {
|
||||
res.status(error.response.status).send(error.response.data);
|
||||
logMsg = "" + error.response.status
|
||||
}
|
||||
else {
|
||||
res.sendStatus(error.response.status);
|
||||
logMsg = "???";
|
||||
}
|
||||
logger.warn(logPrefix + logMsg, {
|
||||
response: {
|
||||
data: error.response.data,
|
||||
status: error.response.status,
|
||||
}
|
||||
});
|
||||
} else if (error.request) {
|
||||
// No response received
|
||||
res.status(504).send({ message: "No response from Spotify" });
|
||||
logger.error(logPrefix + "No response", { error });
|
||||
} else {
|
||||
// Something happened in setting up the request that triggered an Error
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error(logPrefix + "Request failed?", { error });
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
const getUserProfile = async (req, res) => {
|
||||
const response = await singleRequest(req, res,
|
||||
"GET", "/me",
|
||||
{ headers: { Authorization: `Bearer ${req.session.accessToken}` } }
|
||||
);
|
||||
return res.headersSent ? null : response.data;
|
||||
const response = await singleRequest(req, res,
|
||||
"GET", "/me",
|
||||
{ headers: { Authorization: `Bearer ${req.session.accessToken}` } }
|
||||
);
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
const getUserPlaylistsFirstPage = async (req, res) => {
|
||||
const response = await singleRequest(req, res,
|
||||
"GET",
|
||||
`/users/${req.session.user.id}/playlists`,
|
||||
{
|
||||
params: {
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
},
|
||||
});
|
||||
return res.headersSent ? null : response.data;
|
||||
const response = await singleRequest(req, res,
|
||||
"GET",
|
||||
`/users/${req.session.user.id}/playlists`,
|
||||
{
|
||||
params: {
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
},
|
||||
});
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
const getUserPlaylistsNextPage = async (req, res, nextURL) => {
|
||||
const response = await singleRequest(
|
||||
req, res, "GET", nextURL);
|
||||
return res.headersSent ? null : response.data;
|
||||
const response = await singleRequest(
|
||||
req, res, "GET", nextURL);
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
const getPlaylistDetailsFirstPage = async (req, res, initialFields, playlistID) => {
|
||||
const response = await singleRequest(req, res,
|
||||
"GET",
|
||||
`/playlists/${playlistID}/`,
|
||||
{
|
||||
params: {
|
||||
fields: initialFields
|
||||
},
|
||||
});
|
||||
return res.headersSent ? null : response.data;
|
||||
const response = await singleRequest(req, res,
|
||||
"GET",
|
||||
`/playlists/${playlistID}/`,
|
||||
{
|
||||
params: {
|
||||
fields: initialFields
|
||||
},
|
||||
});
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
const getPlaylistDetailsNextPage = async (req, res, nextURL) => {
|
||||
const response = await singleRequest(
|
||||
req, res, "GET", nextURL);
|
||||
return res.headersSent ? null : response.data;
|
||||
const response = await singleRequest(
|
||||
req, res, "GET", nextURL);
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
const addItemsToPlaylist = async (req, res, nextBatch, playlistID) => {
|
||||
const response = await singleRequest(req, res,
|
||||
"POST",
|
||||
`/playlists/${playlistID}/tracks`,
|
||||
{},
|
||||
{ uris: nextBatch }, false
|
||||
)
|
||||
return res.headersSent ? null : response.data;
|
||||
const response = await singleRequest(req, res,
|
||||
"POST",
|
||||
`/playlists/${playlistID}/tracks`,
|
||||
{},
|
||||
{ uris: nextBatch }, false
|
||||
)
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
const removeItemsFromPlaylist = async (req, res, nextBatch, playlistID, snapshotID) => {
|
||||
// API doesn't document this kind of deletion via the 'positions' field
|
||||
// but see here: https://github.com/spotipy-dev/spotipy/issues/95#issuecomment-2263634801
|
||||
const response = await singleRequest(req, res,
|
||||
"DELETE",
|
||||
`/playlists/${playlistID}/tracks`,
|
||||
{},
|
||||
// axios delete method doesn't have separate arg for body so hv to put it in config
|
||||
{ positions: nextBatch, snapshot_id: snapshotID }, true
|
||||
);
|
||||
return res.headersSent ? null : response.data;
|
||||
// API doesn't document this kind of deletion via the 'positions' field
|
||||
// but see here: https://github.com/spotipy-dev/spotipy/issues/95#issuecomment-2263634801
|
||||
const response = await singleRequest(req, res,
|
||||
"DELETE",
|
||||
`/playlists/${playlistID}/tracks`,
|
||||
{},
|
||||
// axios delete method doesn't have separate arg for body so hv to put it in config
|
||||
{ positions: nextBatch, snapshot_id: snapshotID }, true
|
||||
);
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
const checkPlaylistEditable = async (req, res, playlistID, userID) => {
|
||||
let checkFields = ["collaborative", "owner(id)"];
|
||||
let checkFields = ["collaborative", "owner(id)"];
|
||||
|
||||
const checkFromData = await getPlaylistDetailsFirstPage(req, res, checkFields.join(), playlistID);
|
||||
if (res.headersSent) return false;
|
||||
const checkFromData = await getPlaylistDetailsFirstPage(req, res, checkFields.join(), playlistID);
|
||||
if (res.headersSent) return false;
|
||||
|
||||
// https://web.archive.org/web/20241226081630/https://developer.spotify.com/documentation/web-api/concepts/playlists#:~:text=A%20playlist%20can%20also%20be%20made%20collaborative
|
||||
// playlist is editable if it's collaborative (and thus private) or owned by the user
|
||||
if (checkFromData.collaborative !== true &&
|
||||
checkFromData.owner.id !== userID) {
|
||||
res.status(403).send({
|
||||
message: "You cannot edit this playlist, you must be the owner/the playlist must be collaborative",
|
||||
playlistID: playlistID
|
||||
});
|
||||
logger.info("user cannot edit target playlist", { playlistID: playlistID });
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
// https://web.archive.org/web/20241226081630/https://developer.spotify.com/documentation/web-api/concepts/playlists#:~:text=A%20playlist%20can%20also%20be%20made%20collaborative
|
||||
// playlist is editable if it's collaborative (and thus private) or owned by the user
|
||||
if (checkFromData.collaborative !== true &&
|
||||
checkFromData.owner.id !== userID) {
|
||||
res.status(403).send({
|
||||
message: "You cannot edit this playlist, you must be the owner/the playlist must be collaborative",
|
||||
playlistID: playlistID
|
||||
});
|
||||
logger.info("user cannot edit target playlist", { playlistID: playlistID });
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
singleRequest,
|
||||
getUserProfile,
|
||||
getUserPlaylistsFirstPage,
|
||||
getUserPlaylistsNextPage,
|
||||
getPlaylistDetailsFirstPage,
|
||||
getPlaylistDetailsNextPage,
|
||||
addItemsToPlaylist,
|
||||
removeItemsFromPlaylist,
|
||||
checkPlaylistEditable,
|
||||
}
|
||||
singleRequest,
|
||||
getUserProfile,
|
||||
getUserPlaylistsFirstPage,
|
||||
getUserPlaylistsNextPage,
|
||||
getPlaylistDetailsFirstPage,
|
||||
getPlaylistDetailsNextPage,
|
||||
addItemsToPlaylist,
|
||||
removeItemsFromPlaylist,
|
||||
checkPlaylistEditable,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user