mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 14:44:08 +00:00
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
// TODO: rate limit module is busted (CJS types), do something for rate limiting
|
|
// bottleneck (https://npmjs.com/package/bottleneck) looks nice
|
|
import axios from "axios";
|
|
import type { AxiosInstance } from "axios";
|
|
import { baseAPIURL, accountsAPIURL } from "../constants.ts";
|
|
import logger from "../utils/logger.ts";
|
|
|
|
const authInstance: AxiosInstance = axios.create({
|
|
baseURL: accountsAPIURL,
|
|
timeout: 20000,
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
Authorization:
|
|
"Basic " +
|
|
Buffer.from(
|
|
process.env["SPOTMGR_CLIENT_ID"] + ":" + process.env["SPOTMGR_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 };
|