mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 14:14:06 +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:
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 };
|
||||
Reference in New Issue
Block a user