UNPKG

kitten-cloud-function

Version:

用于编程猫源码云功能(云变量、云列表等)的客户端工具

185 lines (184 loc) 6.69 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.getCurrentTime = getCurrentTime; exports.getTimeDifference = getTimeDifference; exports.getCalibratedTimestamp = getCalibratedTimestamp; exports.getSignUUID = getSignUUID; exports.getClientID = getClientID; exports.setXCreationToolsDeviceAuth = setXCreationToolsDeviceAuth; exports.getUserProfile = getUserProfile; exports.getThisUserDetail = getThisUserDetail; exports.getUserDetail = getUserDetail; exports.getUserHonor = getUserHonor; exports.getWorkInfo = getWorkInfo; exports.getWorkDetail = getWorkDetail; exports.getNemoWorkPublicResource = getNemoWorkPublicResource; exports.getKittenWorkPublicResource = getKittenWorkPublicResource; exports.getKittenNWorkPublicResource = getKittenNWorkPublicResource; const other_1 = require("../utils/other"); const codemao_environment_1 = require("./codemao-environment"); /** * https://api.codemao.cn/coconut/clouddb/currentTime */ async function getCurrentTime() { return (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: "https://api.codemao.cn/coconut/clouddb/currentTime" }); } let timeDifference = null; /** * 获取本地时间与 {@link getCurrentTime} 的差异 */ async function getTimeDifference() { if (timeDifference == null) { timeDifference = Math.round(Date.now() / 1000) - await getCurrentTime(); } return timeDifference; } /** * 获取通过 {@link getTimeDifference} 校准过的时间戳 */ async function getCalibratedTimestamp() { return Math.round(Date.now() / 1000) - await getTimeDifference(); } function getSignUUID() { var _a; let signUUID = (_a = codemao_environment_1.CodemaoLocalStorage.getItem("sign_uuid")) !== null && _a !== void 0 ? _a : (0, other_1.randomString)(8, other_1.NUMBER_CHAR.concat(other_1.LOWER_CASE_LETTER)); codemao_environment_1.CodemaoLocalStorage.setItem("sign_uuid", signUUID); return signUUID; } function getClientID() { return getSignUUID(); } async function setXCreationToolsDeviceAuth(argument) { var _a; let timestamp = await getCalibratedTimestamp(); let clientID = getClientID(); (_a = argument.headers) !== null && _a !== void 0 ? _a : (argument.headers = {}); argument.headers["X-Creation-Tools-Device-Auth"] = JSON.stringify({ sign: (await Promise.resolve().then(() => __importStar(require("crypto-js")))).SHA256("pBlYqXbJDu" + timestamp + clientID).toString().toLocaleUpperCase(), timestamp, client_id: clientID }); return argument; } /** * https://api.codemao.cn/tiger/v3/web/accounts/profile * * @param authorization 用户凭证,留空则使用默认 Cookie * * @returns 用户信息 */ async function getUserProfile(authorization) { const headers = authorization == null ? {} : { Cookie: `Authorization=${authorization}` }; return (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: "https://api.codemao.cn/tiger/v3/web/accounts/profile", withCredentials: true, headers }); } /** * https://api.codemao.cn/web/users/details * * 用户被封号时该 API 不可用。 * * @param authorization 用户凭证,留空则使用默认 Cookie */ function getThisUserDetail(authorization) { const headers = authorization == null ? {} : { Cookie: `Authorization=${authorization}` }; return (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: "https://api.codemao.cn/web/users/details", withCredentials: true, headers }); } /** * https://api.codemao.cn/api/user/info/detail/${userID} */ async function getUserDetail(userID) { return (await (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: `https://api.codemao.cn/api/user/info/detail/${userID}`, withCredentials: true })).userInfo; } /** * https://api.codemao.cn/creation-tools/v1/user/center/honor?user_id=${userID} */ function getUserHonor(userID) { return (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: `https://api.codemao.cn/creation-tools/v1/user/center/honor?user_id=${userID}` }); } /** * https://api.codemao.cn/creation-tools/v1/works/${workID} */ function getWorkInfo(workID) { return (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: `https://api.codemao.cn/creation-tools/v1/works/${workID}` }); } /** * https://api.codemao.cn/api/work/info/${workID} */ async function getWorkDetail(workID) { return (await (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: `https://api.codemao.cn/api/work/info/${workID}` })).workDetail; } /** * https://api.codemao.cn/creation-tools/v1/works/${workID}/source/public */ function getNemoWorkPublicResource(workID) { return (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: `https://api.codemao.cn/creation-tools/v1/works/${workID}/source/public` }); } /** * https://api-creation.codemao.cn/kitten/r2/work/player/load/${workID} */ function getKittenWorkPublicResource(workID) { return (0, codemao_environment_1.CodemaoAxios)({ method: "GET", url: `https://api-creation.codemao.cn/kitten/r2/work/player/load/${workID}` }); } /** * https://api-creation.codemao.cn/neko/community/player/published-work-detail/${workID} */ async function getKittenNWorkPublicResource(workID) { return (0, codemao_environment_1.CodemaoAxios)(await setXCreationToolsDeviceAuth({ method: "GET", url: `https://api-creation.codemao.cn/neko/community/player/published-work-detail/${workID}` })); }