UNPKG

kitten-cloud-function

Version:

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

145 lines (144 loc) 4.63 kB
import { LOWER_CASE_LETTER, NUMBER_CHAR, randomString } from "../utils/other"; import { CodemaoAxios, CodemaoLocalStorage } from "./codemao-environment"; /** * https://api.codemao.cn/coconut/clouddb/currentTime */ export async function getCurrentTime() { return CodemaoAxios({ method: "GET", url: "https://api.codemao.cn/coconut/clouddb/currentTime" }); } let timeDifference = null; /** * 获取本地时间与 {@link getCurrentTime} 的差异 */ export async function getTimeDifference() { if (timeDifference == null) { timeDifference = Math.round(Date.now() / 1000) - await getCurrentTime(); } return timeDifference; } /** * 获取通过 {@link getTimeDifference} 校准过的时间戳 */ export async function getCalibratedTimestamp() { return Math.round(Date.now() / 1000) - await getTimeDifference(); } export function getSignUUID() { var _a; let signUUID = (_a = CodemaoLocalStorage.getItem("sign_uuid")) !== null && _a !== void 0 ? _a : randomString(8, NUMBER_CHAR.concat(LOWER_CASE_LETTER)); CodemaoLocalStorage.setItem("sign_uuid", signUUID); return signUUID; } export function getClientID() { return getSignUUID(); } export 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 import("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 用户信息 */ export async function getUserProfile(authorization) { const headers = authorization == null ? {} : { Cookie: `Authorization=${authorization}` }; return 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 */ export function getThisUserDetail(authorization) { const headers = authorization == null ? {} : { Cookie: `Authorization=${authorization}` }; return CodemaoAxios({ method: "GET", url: "https://api.codemao.cn/web/users/details", withCredentials: true, headers }); } /** * https://api.codemao.cn/api/user/info/detail/${userID} */ export async function getUserDetail(userID) { return (await 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} */ export function getUserHonor(userID) { return 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} */ export function getWorkInfo(workID) { return CodemaoAxios({ method: "GET", url: `https://api.codemao.cn/creation-tools/v1/works/${workID}` }); } /** * https://api.codemao.cn/api/work/info/${workID} */ export async function getWorkDetail(workID) { return (await CodemaoAxios({ method: "GET", url: `https://api.codemao.cn/api/work/info/${workID}` })).workDetail; } /** * https://api.codemao.cn/creation-tools/v1/works/${workID}/source/public */ export function getNemoWorkPublicResource(workID) { return 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} */ export function getKittenWorkPublicResource(workID) { return 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} */ export async function getKittenNWorkPublicResource(workID) { return CodemaoAxios(await setXCreationToolsDeviceAuth({ method: "GET", url: `https://api-creation.codemao.cn/neko/community/player/published-work-detail/${workID}` })); }