mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
MASSIVE commit
- moved to typescript - axios rate limitmodule is busted, removed for now, do something else for that - sequelize-typescript - dotenv, not dotenv-flow - removed playlist details route types for API ton of minor fixes and improvements
This commit is contained in:
53
api/axios.js
53
api/axios.js
@@ -1,53 +0,0 @@
|
||||
import axios from "axios";
|
||||
import rateLimit from "axios-rate-limit";
|
||||
|
||||
import { baseAPIURL, accountsAPIURL } from "../constants.js";
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
|
||||
export 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"))
|
||||
},
|
||||
});
|
||||
|
||||
const uncappedAxiosInstance = axios.create({
|
||||
baseURL: baseAPIURL,
|
||||
timeout: 20000,
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
});
|
||||
|
||||
export const axiosInstance = rateLimit(uncappedAxiosInstance, {
|
||||
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;
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
);
|
||||
54
api/axios.ts
Normal file
54
api/axios.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
// TODO: rate limit module is busted (CJS types), do something for rate limiting
|
||||
import axios, { type AxiosInstance } from "axios";
|
||||
import { baseAPIURL, accountsAPIURL } from "../constants.ts";
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
const authInstance: AxiosInstance = 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"),
|
||||
},
|
||||
});
|
||||
|
||||
const axiosInstance: AxiosInstance = axios.create({
|
||||
baseURL: baseAPIURL,
|
||||
timeout: 20000,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
);
|
||||
|
||||
export { authInstance, axiosInstance };
|
||||
153
api/spotify.js
153
api/spotify.js
@@ -1,153 +0,0 @@
|
||||
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
|
||||
import * as typedefs from "../typedefs.js";
|
||||
|
||||
import { axiosInstance } from "./axios.js";
|
||||
|
||||
const logPrefix = "Spotify API: ";
|
||||
|
||||
/**
|
||||
* Spotify API - one-off request handler
|
||||
* @param {typedefs.Req} req convenient auto-placing headers from middleware (not a good approach?)
|
||||
* @param {typedefs.Res} res handle failure responses here itself (not a good approach?)
|
||||
* @param {import("axios").Method} method HTTP method
|
||||
* @param {string} path request path
|
||||
* @param {import("axios").AxiosRequestConfig} config request params, headers, etc.
|
||||
* @param {any} data request body
|
||||
* @param {boolean} inlineData true if data is to be placed inside config
|
||||
*/
|
||||
export 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);
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
|
||||
export const getUserPlaylistsNextPage = async (req, res, nextURL) => {
|
||||
const response = await singleRequest(
|
||||
req, res, "GET", nextURL);
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
|
||||
export const getPlaylistDetailsNextPage = async (req, res, nextURL) => {
|
||||
const response = await singleRequest(
|
||||
req, res, "GET", nextURL);
|
||||
return res.headersSent ? null : response.data;
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
|
||||
export const checkPlaylistEditable = async (req, res, playlistID, userID) => {
|
||||
let checkFields = ["collaborative", "owner(id)"];
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
286
api/spotify.ts
Normal file
286
api/spotify.ts
Normal file
@@ -0,0 +1,286 @@
|
||||
import { axiosInstance } from "./axios.ts";
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
|
||||
import { type AxiosResponse, type AxiosRequestConfig } from "axios";
|
||||
import type {
|
||||
AddItemsToPlaylist,
|
||||
EndpointHandlerBaseArgs,
|
||||
GetCurrentUsersPlaylists,
|
||||
GetCurrentUsersProfile,
|
||||
GetPlaylist,
|
||||
GetPlaylistItems,
|
||||
RemovePlaylistItems,
|
||||
Req,
|
||||
Res,
|
||||
} from "spotify_manager/index.d.ts";
|
||||
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
const logPrefix = "Spotify API: ";
|
||||
enum allowedMethods {
|
||||
Get = "get",
|
||||
Post = "post",
|
||||
Put = "put",
|
||||
Delete = "delete",
|
||||
}
|
||||
|
||||
/**
|
||||
* Spotify API - one-off request handler
|
||||
* @param req convenient auto-placing headers from middleware (not a good approach?)
|
||||
* @param res handle failure responses here itself (not a good approach?)
|
||||
* @param method HTTP method
|
||||
* @param path request path
|
||||
* @param config request params, headers, etc.
|
||||
* @param data request body
|
||||
* @param inlineData true if `data` is to be placed inside config (say, axios' delete method)
|
||||
*/
|
||||
const singleRequest = async <RespDataType>(
|
||||
req: Req,
|
||||
res: Res,
|
||||
method: allowedMethods,
|
||||
path: string,
|
||||
config: AxiosRequestConfig = {},
|
||||
data: any = null,
|
||||
inlineData: boolean = false
|
||||
): Promise<AxiosResponse<RespDataType, any> | null> => {
|
||||
let resp: AxiosResponse<RespDataType, any>;
|
||||
config.headers = { ...config.headers, ...req.session.authHeaders };
|
||||
try {
|
||||
if (!data || inlineData) {
|
||||
if (data) config.data = data ?? null;
|
||||
resp = await axiosInstance[method](path, config);
|
||||
} else {
|
||||
resp = await axiosInstance[method](path, data, config);
|
||||
}
|
||||
logger.debug(logPrefix + "Successful response received.");
|
||||
return resp;
|
||||
} catch (error: any) {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
interface GetCurrentUsersProfileArgs extends EndpointHandlerBaseArgs {}
|
||||
const getCurrentUsersProfile: (
|
||||
opts: GetCurrentUsersProfileArgs
|
||||
) => Promise<GetCurrentUsersProfile | null> = async ({ req, res }) => {
|
||||
const response = await singleRequest<GetCurrentUsersProfile>(
|
||||
req,
|
||||
res,
|
||||
allowedMethods.Get,
|
||||
"/me",
|
||||
{
|
||||
headers: { Authorization: `Bearer ${req.session.accessToken}` },
|
||||
}
|
||||
);
|
||||
return response ? response.data : null;
|
||||
};
|
||||
|
||||
interface GetCurrentUsersPlaylistsFirstPageArgs
|
||||
extends EndpointHandlerBaseArgs {}
|
||||
const getCurrentUsersPlaylistsFirstPage: (
|
||||
opts: GetCurrentUsersPlaylistsFirstPageArgs
|
||||
) => Promise<GetCurrentUsersPlaylists | null> = async ({ req, res }) => {
|
||||
const response = await singleRequest<GetCurrentUsersPlaylists>(
|
||||
req,
|
||||
res,
|
||||
allowedMethods.Get,
|
||||
`/me/playlists`,
|
||||
{
|
||||
params: {
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
},
|
||||
}
|
||||
);
|
||||
return response?.data ?? null;
|
||||
};
|
||||
|
||||
interface GetCurrentUsersPlaylistsNextPageArgs extends EndpointHandlerBaseArgs {
|
||||
nextURL: string;
|
||||
}
|
||||
const getCurrentUsersPlaylistsNextPage: (
|
||||
opts: GetCurrentUsersPlaylistsNextPageArgs
|
||||
) => Promise<GetCurrentUsersPlaylists | null> = async ({
|
||||
req,
|
||||
res,
|
||||
nextURL,
|
||||
}) => {
|
||||
const response = await singleRequest<GetCurrentUsersPlaylists>(
|
||||
req,
|
||||
res,
|
||||
allowedMethods.Get,
|
||||
nextURL
|
||||
);
|
||||
return response?.data ?? null;
|
||||
};
|
||||
|
||||
interface GetPlaylistDetailsFirstPageArgs extends EndpointHandlerBaseArgs {
|
||||
initialFields: string;
|
||||
playlistID: string;
|
||||
}
|
||||
const getPlaylistDetailsFirstPage: (
|
||||
opts: GetPlaylistDetailsFirstPageArgs
|
||||
) => Promise<GetPlaylist | null> = async ({
|
||||
req,
|
||||
res,
|
||||
initialFields,
|
||||
playlistID,
|
||||
}) => {
|
||||
const response = await singleRequest<GetPlaylist>(
|
||||
req,
|
||||
res,
|
||||
allowedMethods.Get,
|
||||
`/playlists/${playlistID}/`,
|
||||
{
|
||||
params: {
|
||||
fields: initialFields,
|
||||
},
|
||||
}
|
||||
);
|
||||
return response?.data ?? null;
|
||||
};
|
||||
|
||||
interface GetPlaylistDetailsNextPageArgs extends EndpointHandlerBaseArgs {
|
||||
nextURL: string;
|
||||
}
|
||||
const getPlaylistDetailsNextPage: (
|
||||
opts: GetPlaylistDetailsNextPageArgs
|
||||
) => Promise<GetPlaylistItems | null> = async ({ req, res, nextURL }) => {
|
||||
const response = await singleRequest<GetPlaylistItems>(
|
||||
req,
|
||||
res,
|
||||
allowedMethods.Get,
|
||||
nextURL
|
||||
);
|
||||
return response?.data ?? null;
|
||||
};
|
||||
|
||||
interface AddItemsToPlaylistArgs extends EndpointHandlerBaseArgs {
|
||||
nextBatch: string[];
|
||||
playlistID: string;
|
||||
}
|
||||
const addItemsToPlaylist: (
|
||||
opts: AddItemsToPlaylistArgs
|
||||
) => Promise<AddItemsToPlaylist | null> = async ({
|
||||
req,
|
||||
res,
|
||||
nextBatch,
|
||||
playlistID,
|
||||
}) => {
|
||||
const response = await singleRequest<AddItemsToPlaylist>(
|
||||
req,
|
||||
res,
|
||||
allowedMethods.Post,
|
||||
`/playlists/${playlistID}/tracks`,
|
||||
{},
|
||||
{ uris: nextBatch },
|
||||
false
|
||||
);
|
||||
return response?.data ?? null;
|
||||
};
|
||||
|
||||
interface RemovePlaylistItemsArgs extends EndpointHandlerBaseArgs {
|
||||
nextBatch: string[] | number[]; // see note below
|
||||
playlistID: string;
|
||||
snapshotID: string;
|
||||
}
|
||||
const removePlaylistItems: (
|
||||
opts: RemovePlaylistItemsArgs
|
||||
) => Promise<RemovePlaylistItems | null> = 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<RemovePlaylistItems>(
|
||||
req,
|
||||
res,
|
||||
allowedMethods.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 response?.data ?? null;
|
||||
};
|
||||
|
||||
// ---------
|
||||
// non-endpoints, i.e. convenience wrappers
|
||||
// ---------
|
||||
|
||||
interface CheckPlaylistEditableArgs extends EndpointHandlerBaseArgs {
|
||||
playlistID: string;
|
||||
userID: string;
|
||||
}
|
||||
const checkPlaylistEditable: (
|
||||
opts: CheckPlaylistEditableArgs
|
||||
) => Promise<boolean> = async ({ req, res, playlistID, userID }) => {
|
||||
let checkFields = ["collaborative", "owner(id)"];
|
||||
|
||||
const checkFromData = await getPlaylistDetailsFirstPage({
|
||||
req,
|
||||
res,
|
||||
initialFields: checkFields.join(),
|
||||
playlistID,
|
||||
});
|
||||
if (!checkFromData) 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,
|
||||
});
|
||||
logger.info("user cannot edit target playlist", { playlistID });
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
singleRequest,
|
||||
getCurrentUsersProfile,
|
||||
getCurrentUsersPlaylistsFirstPage,
|
||||
getCurrentUsersPlaylistsNextPage,
|
||||
getPlaylistDetailsFirstPage,
|
||||
getPlaylistDetailsNextPage,
|
||||
addItemsToPlaylist,
|
||||
removePlaylistItems,
|
||||
checkPlaylistEditable,
|
||||
};
|
||||
Reference in New Issue
Block a user