correctly add search params

This commit is contained in:
Jacob Nguyen
2023-12-10 17:01:53 -06:00
parent 3917433117
commit a439f2a7e9
5 changed files with 46 additions and 31 deletions

View File

@@ -16,30 +16,31 @@ type GuildEdit = paths["/applications/{application_id}/guilds/{guild_id}/command
type GuildDelete = paths["/applications/{application_id}/guilds/{guild_id}/commands/{command_id}"]["delete"]
type GuildPut = paths["/applications/{application_id}/guilds/{guild_id}/commands"]["put"]
type AnyRoute =
| GlobalGetAll
| GlobalGet
| GlobalPost
| GlobalEdit
| GlobalPut
| GlobalDelete
| GuildPost
| GuildGet
| GuildEdit
| GuildDelete
| GuildPut;
//We create a discriminating union to ensure Responses are typed correctly
//type AnyRoute =
// | GlobalGetAll & { type: "global/get-all" }
// | GlobalGet & { type: "global/get" }
// | GlobalPost & { type: "global/post" }
// | GlobalEdit & { type: "global/edit" }
// | GlobalPut & { type: "global/put" }
// | GlobalDelete & { type: "global/delete" }
// | GuildPost & { type: "global/post" }
// | GuildGet & { type: "global/x" }
// | GuildEdit & { type: "global/get" }
// | GuildDelete & { type: "global/delete" }
// | GuildPut & { type: "global/put" };
interface RoutesOptions {
"global/get-all": [];
"global/get": [GlobalGet["parameters"]["path"] & { application_id?: never }];
"global/get-all": [GlobalGetAll['parameters']];
"global/get": [GlobalGet["parameters"]["path"] & { application_id?: never }];
"global/post": [{ body: GlobalPost["requestBody"]["content"]['application/json'] }
& GlobalPost["parameters"]["path"]
& { application_id?: never }];
"global/edit": [{ body: GlobalEdit["requestBody"]["content"]['application/json']}
& GlobalEdit["parameters"]["path"]
& { application_id?: never }];
"global/put": [{ body: GlobalPut["requestBody"]["content"]['application/json']}
"global/put": [{ body: GlobalPut["requestBody"]["content"]['application/json']}
& GlobalPut["parameters"]['path']
& { application_id?: never } ];
"global/delete": [];
@@ -55,12 +56,17 @@ interface RoutesOptions {
& { application_id?: never }];
}
interface TypedResponse<T extends keyof RoutesOptions> extends Response {
body?: { } & AnyRoute['responses']
}
//interface TypedResponse<T extends keyof RoutesOptions> extends Response {
// /**
// * Please do not use `.type` in runtime. It is merely a way to get accurate typings
// * It does not exist in runtime.
// */
// json(): ({ type: T } & AnyRoute)['responses']
//}
interface Send {
<T extends keyof RoutesOptions>(command : T, ...opts: RoutesOptions[T]): Promise<Response>
<T extends keyof RoutesOptions>
(command : T, ...opts: RoutesOptions[T]): Promise<Response>
}
export default function (token: string, appid: string): Send;

View File

@@ -9,6 +9,5 @@
{:poster {:target :node-library
:output-to "dist/index.js"
:exports-var core.poster/poster
:build-hooks
[(dev.mv/hook 1 2 3)]
:build-hooks [(dev.mv/hook 1 2 3)]
}}}

View File

@@ -16,7 +16,7 @@
:guild/put ["PUT" "/applications/{application.id}/guilds/{guild.id}/commands"]
})
(defn request-init [spec]
(defn- request-init [spec]
(let [[method url] (routes spec)]
[url (fn [body headers]
#js { "method" method
@@ -24,7 +24,7 @@
"body" (.stringify js/JSON body )})
]))
(defn keyword->str [ky]
(defn- keyword->str [ky]
(subs (str ky) 1))
(def actions (into {}

View File

@@ -2,20 +2,28 @@
(:require [clojure.string :refer [replace]]
[core.actions :refer [actions]]))
(def base-url "https://discord.com/api/v10/applications")
(def base-url "https://discord.com/api/v10")
(defn processed-url [remaining-url opts]
(defn- inject [remaining-url opts]
(-> (str base-url remaining-url)
(replace #"\{application\.id\}" (.-app_id ^js opts))
(replace #"\{guild\.id\}" (.-guild_id ^js opts))
(replace #"\{command\.id\}" (.-command_id ^js opts))))
(defn ?params [^js query]
(new js/URLSearchParams query))
(defn poster [token, appid]
(let [header #js{ "Content-Type" "application/json"
"Authorization" (str "Bot " token) }]
(fn [action opts]
(let [[url mkrequest] (actions action)
full-url (processed-url url #js{"app_id" appid
"guild_id" (.-guild_id ^js opts)
"command_id" (.-command_id ^js opts)})]
(js/fetch full-url (mkrequest (.-body ^js opts ) header))))))
options #js{"app_id" appid
"guild_id" (.-guild_id ^js opts)
"command_id" (.-command_id ^js opts)}
url (new js/URL (inject url options))]
(set! (.-search url) (?params (.-query ^js opts)))
(js/fetch url (mkrequest (.-body ^js opts) header))))))

View File

@@ -3,8 +3,10 @@ import poster from '../dist/index.js';
const send = poster("token", "appid");
const req = await send("global/get-all");
const req = await send("global/get-all", {
});
console.log(req);
console.log(await req.json());