This commit is contained in:
jacob
2024-05-02 17:03:40 -05:00
9 changed files with 479 additions and 1365 deletions

View File

@@ -1,3 +1,3 @@
dist/
.shadow-cljs
src/*.d.ts
src/discord.d.ts

View File

@@ -1,15 +1,14 @@
{
"name": "@sern/poster",
"version": "1.1.0",
"version": "1.2.1",
"description": "Post discord application commands",
"exports": {
"." : "./dist/core/poster.mjs",
"" : ""
".": "./dist/poster.mjs"
},
"scripts": {
"poster:debug": "npx squint compile",
"poster:prod": "npx squint compile",
"gen-discord-types": "npx openapi-typescript https://raw.githubusercontent.com/discord/discord-api-spec/main/specs/openapi.json --output ./src/resources/discord.d.ts",
"gen-discord-types": "npx openapi-typescript https://raw.githubusercontent.com/discord/discord-api-spec/main/specs/openapi.json --output ./src/discord.d.ts",
"repl": "npx squint repl"
},
"keywords": [],
@@ -17,7 +16,7 @@
"license": "ISC",
"publishConfig": {
"access": "public",
"tag": "beta"
"tag": "latest"
},
"dependencies": {
"squint-cljs": "latest"

View File

@@ -1,4 +1,4 @@
(ns core.actions)
(ns actions)
(def routes {
@@ -22,8 +22,7 @@
[url (fn [body headers]
#js { "method" method
"headers" headers
"body" (.stringify js/JSON body )})
]))
"body" (js/JSON.stringify body)}) ]))
(def actions (into {}

View File

@@ -1,45 +0,0 @@
(ns core.poster
(:require [clojure.string :as s]
[core.actions :refer [actions]]))
(def ^:private base "https://discord.com/api/v10")
(defn- inject [remaining-url opts]
(-> (str base remaining-url)
(s/replace #"\{application\.id\}" (or (.-app_id ^js opts) ""))
(s/replace #"\{guild\.id\}" (or (.-guild_id ^js opts) ""))
(s/replace #"\{command\.id\}" (or (.-command_id ^js opts) ""))))
(defn- ?params [^js query]
(new js/URLSearchParams query))
(defn- ^:async fetch-application [headers]
(js-await (-> (js/fetch (str base (first (get actions "application/me"))) #js{ "headers" headers })
(.then (fn ^:=> [res] (.json res)))
(.then (fn ^:=> [son] (if-let [id (.-id son)]
id (throw (str "Reason " (.-message son))))))
(.catch (fn ^:=> [e] (throw e))))))
(defn- poster [token, appid]
(let [header #js{ "Content-Type" "application/json"
"Authorization" (str "Bot " token) }]
(^:async fn [action opts]
(let [[url mkrequest] (get actions action)
appid (js-await (fetch-application 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))))))
(defn- isOk? [^js response]
(.-ok response))
(def default {
:client poster
:isOk isOk?
:is4XX (complement isOk?)
})

View File

@@ -0,0 +1,43 @@
(ns poster
(:require [clojure.string :as s]
[actions :refer [actions]]))
(def ^:private base "https://discord.com/api/v10")
(defn- inject [remaining-url opts]
(-> (str base remaining-url)
(s/replace #"\{application\.id\}" (or (.-app_id ^js opts) ""))
(s/replace #"\{guild\.id\}" (or (.-guild_id ^js opts) ""))
(s/replace #"\{command\.id\}" (or (.-command_id ^js opts) ""))))
(defn- ?params [^js query]
(new js/URLSearchParams query))
(defn- fetch-application [headers]
(let [url (str base (first (get actions "application/me")))]
(-> (js/fetch url #js{ "headers" headers })
(.then (fn ^:=> [res] (.json res)))
(.then (fn ^:=> [son] (if-let [id (.-id son)]
id
(throw (str "Reason " (.-message son))))))
(.catch (fn ^:=> [e] (throw e))))))
(defn- poster [token]
(let [header { "Content-Type" "application/json"
"Authorization" (str "Bot " token) }]
(^:async fn [action opts]
(let [[url mkrequest] (get actions action)
appid (js-await (fetch-application header))
options {:app_id appid :guild_id (.-guild_id opts) :command_id (.-command_id opts)}
url (new js/URL (inject url options)) ]
(set! (.-search url) (?params (.-query opts)))
(js/fetch url (mkrequest (.-body opts) header))))))
(defn- isOk? [response]
(.-ok response))
(def default { :client poster
:isOk isOk?
:is4XX (complement isOk?) })

View File

@@ -1,4 +1,4 @@
declare module 'index.js';
declare module 'poster.*';
import type { paths } from './discord.d.ts'

View File

@@ -1,6 +1,6 @@
import poster from '../dist/core/poster.mjs';
import poster from '../dist/poster.mjs';
const send = await poster.client("");
const send = await poster.client("token");
const req = await send("global/get-all", {

View File

@@ -1,5 +1,4 @@
import poster from '../dist/index.js';
import type { GlobalGetAll, GlobalPut, TypedResponse } from '../dts/index.js';
import poster, { type GlobalPut, type TypedResponse } from '../dist/poster.js';
const send = await poster.client("token");

1731
yarn.lock

File diff suppressed because it is too large Load Diff