UNPKG

yandex-music

Version:

Creative and progressive Node.js framework for applications that interact with yandex music

140 lines (139 loc) 6.29 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.baseClient = exports.Requset = exports.Languages = void 0; const axios_1 = __importStar(require("axios")); const url_1 = require("url"); const exceptions_1 = require("../exceptions"); var Languages; (function (Languages) { Languages["en"] = "en"; Languages["uz"] = "uz"; Languages["uk"] = "uk"; Languages["us"] = "us"; Languages["ru"] = "ru"; Languages["kk"] = "kk"; Languages["hy"] = "hy"; })(Languages = exports.Languages || (exports.Languages = {})); class Requset { constructor(client, url) { this.client = client; this.url = url; this._headers = new Map(); this.axios = axios_1.default.create({ baseURL: this.url }); this._headers.set("X-Yandex-Music-Client", "YandexMusicAndroid/23020251"); this._headers.set("User-Agent", "Yandex-Music-API"); } /** * @returns {Map<string, any>} Headers from the request client class */ get headers() { return this._headers; } /** * Add language header for each requset. * @param {Languages} lang Avlibale variants: en/uz/uk/us/ru/kk/hy. * @returns {void} Void functions that set header. */ setLanguage(lang = "ru") { this._headers.set("Accept-Language", lang); } /** * Add token header for each requset. * @returns {void} Void functions that set header. */ setAuthorization() { var _a; this._headers.set("Authorization", `OAuth ${(_a = this.client) === null || _a === void 0 ? void 0 : _a.token}`); } /** * Throws the necessary exceptions, returns a response. Passes custom arguments to the request. * * @param {"GET" | "POST"} type HTTP request method ("GET" | "POST"). * @param {string} url Url for the request. * @param {data} data Search and additional date for the post request. * @param {data} params Search and additional params for the all requests. * @returns {Promise<T>} Data from the yandex music api. * * @throws UnauthorizedError, when token is invalid, or a long wait for a direct link to the file. * @throws BadRequestError, when the request is incorrect. * @throws NotFoundError, when the page is not found * @throws NetworkError, when there are problems with the network */ async wrapper(type, url, data = null, params = null) { var _a, _b, _c, _d, _e, _f, _g; let response; if (params) for (const [key, value] of Object.entries(params)) params[key] = value.toString(); const config = { headers: Object.fromEntries(this._headers.entries()), params: params ? new url_1.URLSearchParams(params) : null, }; try { if (type === "GET") response = await this.axios.get(url, config); else if (type === "POST") response = await this.axios.post(url, data, config); else if (type === "DIRECT_LINK") return (await axios_1.default.get(url, config))['data']; } catch (e) { if (e instanceof axios_1.AxiosError) { const { message } = e; const code = (_a = e.response) === null || _a === void 0 ? void 0 : _a.status; const response = `${((_c = (_b = e.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.error) ? ": " + ((_f = (_e = (_d = e.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.error) === null || _f === void 0 ? void 0 : _f.message) : ""}`; const error = `${message}${response}`; if (code === 401 || code === 403) throw new exceptions_1.UnauthorizedError(message); else if (code === 400) throw new exceptions_1.BadRequestError(error); else if (code === 404) throw new exceptions_1.NotFoundError(message); else if (code === 409 || code === 413) throw new exceptions_1.NetworkError(message); else if (code === 502) throw new exceptions_1.NetworkError(message); else throw new exceptions_1.NetworkError(`${message} (${(_g = e.response) === null || _g === void 0 ? void 0 : _g.status}): ${response}`); } else throw new exceptions_1.YandexMusicError(`Unknown HTTP error. ${e}`); } return response["data"]["result"]; } async get(url, params = null) { return await this.wrapper("GET", url, null, params); } async post(url, data = null, params = null) { return await this.wrapper("POST", url, data, params); } async directLink(url, params) { return await this.wrapper("DIRECT_LINK", url, null, params); } } exports.Requset = Requset; const baseClient = (url) => axios_1.default.create({ baseURL: url }); exports.baseClient = baseClient;