UNPKG

ecoledirecte.js

Version:

Good-looking client for EcoleDirecte's private API.

62 lines (61 loc) 2.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchFile = exports.getCloudFolder = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const v3_1 = require("ecoledirecte-api-types/v3"); const util_1 = require("../util"); async function getCloudFolder(account, folderPath, context = {}) { const body = await (0, util_1.makeRequest)({ method: "POST", path: v3_1.Routes.cloudFolder(account.__raw.typeCompte, account.edId, { verbe: "get", idFolder: folderPath || undefined, }), body: { token: account.token }, guard: true, }, { userId: account.edId, action: "getGrades", ...context }, account); return body; } exports.getCloudFolder = getCloudFolder; const EdHeadersFile = { authority: "api.ecoledirecte.com", // This header, that references a specific version, might cause issues in the future "sec-ch-ua": // eslint-disable-next-line quotes '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"', accept: "application/json, text/plain, */*", "sec-ch-ua-mobile": "?0", "user-agent": util_1.USER_AGENT, "content-type": "application/x-www-form-urlencoded", origin: "https://www.ecoledirecte.com", "sec-fetch-site": "same-site", "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", referer: "https://www.ecoledirecte.com/", "accept-language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7", }; async function fetchFile(fileId, token) { //TODO Bug token usage issues to fix... const urlencoded = new URLSearchParams(); urlencoded.append("data", JSON.stringify({ forceDownload: 0, token: token })); const url = new URL(v3_1.Routes.downloadFile({ verbe: "post", fichierId: encodeURI(fileId), leTypeDeFichier: "CLOUD", }), util_1.Config.get("root")).href; const res = await (0, node_fetch_1.default)(url, { method: "POST", headers: { ...EdHeadersFile, ...util_1.Config.get("addedHeaders") }, body: urlencoded, }); const buf = await res.buffer(); const str = "data:" + res.headers.get("Content-Type") + ";base64," + buf.toString("base64"); return [buf, str]; } exports.fetchFile = fetchFile;