Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cookie support #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/util/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

export let BEARER = "";
export let COOKIE = "";

/**
* Sets the Bearer Token to provide to robotevents. This is useful if you have
Expand All @@ -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 != "";
}
10 changes: 8 additions & 2 deletions src/util/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,6 +70,9 @@ async function doRequest<T = unknown>(url: URL): Promise<T> {
if (BEARER) {
headers["Authorization"] = `Bearer ${BEARER}`;
}
if (COOKIE) {
headers["Cookie"] = COOKIE;
}

// Make the initial request
const response = await fetch(url.href, {
Expand All @@ -85,7 +88,10 @@ async function doRequest<T = unknown>(url: URL): Promise<T> {

// 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();
Expand Down