works now i think

This commit is contained in:
Jacob Nguyen
2023-11-20 15:29:45 -06:00
parent 79f629f193
commit 8b29923482
3 changed files with 37 additions and 46 deletions

View File

@@ -1,40 +1,29 @@
(ns core.actions
(:require [cljs.spec.alpha :as s]))
(ns core.actions)
(defn create-validator [schema]
{ :valid #(s/valid? schema %)
:explain #(s/explain schema %) })
#_(s/def ::http-verb (s/enum "POST" "PATCH" "PUT" "GET"))
(s/def :global/put some?)
(def routes {
; :GlobalGetAll "/applications/{application.id}/commands"
; :GlobalGet "/applications/{application.id}/commands/commands/{command.id}"
; :GlobalPost "/applications/{application.id}/commands"
; :GlobalEdit "/applications/{application.id}/commands/{command.id}"
; :GlobalDelete "/applications/{application.id}/commands/{command.id}"
:global/put #js["GET" "/applications/{application.id}/commands"]
; :GuildGetAll "/applications/{application.id}/guilds/{guild.id}/commands"
; :GuildPost "/applications/{application.id}/guilds/{guild.id}/commands"
; :GuildGet "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}"
; :GuildEdit "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}"
; :GuildDelete "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}"
; :GuildPut "/applications/{application.id}/guilds/{guild.id}/commands"
; :GlobalGetAll "/applications/{application.id}/commands"
; :GlobalGet "/applications/{application.id}/commands/commands/{command.id}"
; :GlobalPost "/applications/{application.id}/commands"
; :GlobalEdit "/applications/{application.id}/commands/{command.id}"
; :GlobalDelete "/applications/{application.id}/commands/{command.id}"
:global/put ["PUT" "/applications/{application.id}/commands"]
; :GuildGetAll "/applications/{application.id}/guilds/{guild.id}/commands"
; :GuildPost "/applications/{application.id}/guilds/{guild.id}/commands"
; :GuildGet "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}"
; :GuildEdit "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}"
; :GuildDelete "/applications/{application.id}/guilds/{guild.id}/commands/{command.id}"
; :GuildPut "/applications/{application.id}/guilds/{guild.id}/commands"
})
(defn parseurl
[appid]
)
(defn request-init [spec]
(fn [body]
#js { "method" (first (routes 'spec))
"body" body }))
(let [[method url] (routes spec)]
[ url (fn [body headers]
#js { "method" method
"headers" headers
"body" (.stringify js/JSON body )})
]))
(def validators {
"global/put" [(request-init :global/put)
(create-validator :global/put)]
(def actions{
"global/put" (request-init :global/put)
})

View File

@@ -1,20 +1,21 @@
(ns core.poster
(:use [core.actions :only [validators]]))
(:require [clojure.string :refer [replace]]
[core.actions :refer [actions]]))
(def base-url (new js/URL "https://discord.com/api/v10/applications/"))
(def base-url "https://discord.com/api/v10/applications")
(def excluded-keys #js { "command" "absPath" })
(defn make-global [appid token]
"makes a url which posts to global"
(new js/URL (str appid "/commands") base-url))
(defn processed-url [remaining-url opts]
(-> (str base-url remaining-url)
(replace #"\{application\.id\}" (.-app_id opts))
(replace #"\{guild\.id\}" (.-guild_id opts))
(replace #"\{command\.id\}" (.-command_id opts))))
(defn poster [token, appid]
(let [header #js{ "Content-Type" "application/json"
"Authorization" (str "Bot " token) }]
(fn [action body]
(let [ [request-init validator] (validators action)
{ is-valid? :valid explain :explain } validator]
(if (is-valid? body)
(js/fetch base-url (request-init body))
(explain body))))))
(fn [action opts]
(let [ [url mkrequest] (actions action)
full-url (processed-url url #js { "app_id" appid
"guild_id" (.-guild_id opts)
"command_id" (.-command_id opts) })]
(js/fetch full-url (mkrequest (.-body opts ) header))))))

View File

@@ -3,4 +3,5 @@ import poster from '../dist/index.js';
const client = poster("token", "appid");
const req = await client("global/put", { });
const req = await client("global/put", { body : [] });