Skip to content

Commit

Permalink
[http] Provide proxies for HTTP methods
Browse files Browse the repository at this point in the history
This prevents these methods not working when destructured from the top-level of
a file.
  • Loading branch information
redstonekasi authored and yellowsink committed May 6, 2024
1 parent 5ce6571 commit 2dd1e80
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
28 changes: 24 additions & 4 deletions packages/shelter/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
import { after, instead } from "spitroast";
import { DiscordHTTP, HTTPRequest, HTTPResponse } from "./types";
import { DiscordHTTP, HTTPApi, HTTPRequest, HTTPResponse } from "./types";

const methods = ["get", "post", "put", "patch", "del"];

let resolve: () => void;
export let ready = new Promise<void>((res) => (resolve = res));

export let discordHttp: DiscordHTTP;

const api: HTTPApi = {
intercept,
ready,
get _raw() {
return discordHttp;
},
};

for (const fun of methods) {
api[fun] = (...args: any[]) => {
if (discordHttp === undefined) throw new Error("HTTP method used before API was ready");
return discordHttp[fun](...args);
};
}

export default api;

const unpatch = after("bind", Function.prototype, function (args, res) {
if (args.length !== 2 || args[0] !== null || args[1] !== "get") return;
unpatch();
return function (...args) {
// I don't know why, but for the first call `this` is Window
if (this !== window) {
this.get = res;
discordHttp = this;
Object.assign(api, discordHttp);
resolve();
this.get = res;
}
return res(...args);
};
Expand All @@ -22,7 +42,7 @@ const unpatch = after("bind", Function.prototype, function (args, res) {
export let unpatchHttpHandlers;
function patchHttpHandlers() {
if (unpatchHttpHandlers) return;
const patches = ["get", "post", "put", "patch", "del"].map((fun) =>
const patches = methods.map((fun) =>
instead(fun, discordHttp, async (args, original) => {
let req = args[0];
if (typeof req === "string") {
Expand Down
3 changes: 0 additions & 3 deletions packages/shelter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ export interface DiscordHTTP {
put: HTTPFunction;
patch: HTTPFunction;
del: HTTPFunction;
getAPIBaseURL: string;
V6OrEarlierAPIError: Error;
V8APIError: Error;
}

export type HTTPApi = {
Expand Down
16 changes: 2 additions & 14 deletions packages/shelter/src/windowApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as plugins from "./plugins";
import { registerSection } from "./settings";
import * as storage from "./storage";
import { observe } from "./observer";
import { discordHttp, intercept, ready } from "./http";
import http from "./http";
import { HTTPApi } from "./types";
import { constants } from "./constants";

Expand All @@ -22,7 +22,6 @@ function without<T extends Record<string, any>, TK extends string>(object: T, ..
return cloned as Omit<T, TK>;
}

let http;
const windowApi = async (unloads) => {
const dispatcher = await flux.getDispatcher();

Expand All @@ -38,18 +37,7 @@ const windowApi = async (unloads) => {
"modifiedSym",
"initDispatchLogger",
),
get http(): HTTPApi {
if (discordHttp === undefined) {
return { intercept, ready };
}

return (http ??= {
...discordHttp,
_raw: discordHttp,
intercept,
ready,
});
},
http,
constants,
patcher: without(patcher, "unpatchAll"),
solid,
Expand Down

0 comments on commit 2dd1e80

Please sign in to comment.