From ef6150b7cf44eacb0b57f1bec58e3d3ca09b8d4d Mon Sep 17 00:00:00 2001 From: GuoLin Date: Wed, 1 May 2024 21:46:50 +0800 Subject: [PATCH] add cookie support --- src/util/authentication.ts | 15 ++++++++++++++- src/util/request.ts | 10 ++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/util/authentication.ts b/src/util/authentication.ts index 2cbc091..74cfe52 100644 --- a/src/util/authentication.ts +++ b/src/util/authentication.ts @@ -3,6 +3,7 @@ */ export let BEARER = ""; +export let COOKIE = ""; /** * Sets the Bearer Token to provide to robotevents. This is useful if you have @@ -14,11 +15,23 @@ export function setBearer(bearer: string) { return (BEARER = bearer); } +/** + * Sets the Cookie to provide to robotevents. + * because access API by bearer will not get the limit info in Response.header. + * but When access API by cookie, they will give the limit info back. + * So We can set the cookie instead of the official Bearer. + * + * @param cookie + */ +export function setCookie(cookie: string) { + return (COOKIE = cookie); +} + /** * Checks if the user agent has been authenticated correctly. If this function * returns true you may not be ok to make requests, as your session cookie may * have been revoked early, however it is generally a good indicator */ export function ok() { - return BEARER !== ""; + return BEARER !== "" || COOKIE != ""; } diff --git a/src/util/request.ts b/src/util/request.ts index 5e53358..75cac3c 100644 --- a/src/util/request.ts +++ b/src/util/request.ts @@ -9,7 +9,7 @@ import fetch from "cross-fetch"; import { ready, updateCurrent } from "./ratelimit"; -import { BEARER } from "./authentication"; +import { BEARER, COOKIE } from "./authentication"; /** * Serializes parameters into a string to be passed to the API @@ -70,6 +70,9 @@ async function doRequest(url: URL): Promise { if (BEARER) { headers["Authorization"] = `Bearer ${BEARER}`; } + if (COOKIE) { + headers["Cookie"] = COOKIE; + } // Make the initial request const response = await fetch(url.href, { @@ -85,7 +88,10 @@ async function doRequest(url: URL): Promise { // If the response errored reject accordingly if (!response.ok) { - return Promise.reject(await response.text()); + const status = response.status; + const statusText = response.statusText; + const text = await response.text(); + return Promise.reject({ status, statusText, text }); } return response.json();