Merge pull request #5 from sern-handler/poster/refactor

poster/refactor
This commit is contained in:
Jacob Nguyen
2024-04-07 15:25:10 -05:00
committed by GitHub
11 changed files with 105 additions and 1527 deletions

View File

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

View File

@@ -2,16 +2,14 @@
"name": "@sern/poster",
"version": "1.1.0",
"description": "Post discord application commands",
"main": "dist/index.js",
"scripts": {
"poster:debug": "shadow-cljs compile poster",
"poster:prod": "shadow-cljs release poster",
"gen-discord-types": "npx openapi-typescript https://raw.githubusercontent.com/discord/discord-api-spec/main/specs/openapi.json --output ./dts/discord.d.ts",
"repl": "shadow-cljs node-repl"
"exports": {
"." : "./dist/poster.mjs"
},
"devDependencies": {
"shadow-cljs": "^2.8.52",
"source-map-support": "^0.5.13"
"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/discord.d.ts",
"repl": "npx squint repl"
},
"keywords": [],
"author": "",
@@ -19,5 +17,8 @@
"publishConfig": {
"access": "public",
"tag": "beta"
},
"dependencies": {
"squint-cljs": "latest"
}
}

View File

@@ -0,0 +1,3 @@
{:paths ["src"]
:output-dir "dist/"
:copy-resources #{ :d.ts }}

View File

@@ -1,4 +1,4 @@
(ns core.actions)
(ns actions)
(def routes {
@@ -22,14 +22,11 @@
[url (fn [body headers]
#js { "method" method
"headers" headers
"body" (.stringify js/JSON body )})
]))
"body" (js/JSON.stringify body)}) ]))
(defn- keyword->str [ky]
(subs (str ky) 1))
(def actions (into {}
(map (fn [[k v]] [(keyword->str k) (request-init v)]))
(map (fn [[k v]] [k (request-init v)]))
routes))

View File

@@ -1,41 +0,0 @@
(ns core.poster
(:require [clojure.string :refer [replace]]
[shadow.cljs.modern :refer (js-await)]
[core.actions :refer [actions]]))
(def base "https://discord.com/api/v10")
(defn- inject [remaining-url opts]
(-> (str base 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 fetch-application [headers]
#_(println (str base (actions "application/me")))
(-> (js/fetch (str base (first (actions "application/me"))) #js{ "headers" headers })
(.then (fn [res] (.json res)))
(.then (fn [json] (.-id json)))))
(defn poster [token, appid]
(let [header #js{ "Content-Type" "application/json"
"Authorization" (str "Bot " token) } ]
(js-await [appid (fetch-application header)]
(fn [action opts]
(let [[url mkrequest] (actions action)
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))
(defn is4XX? [^js response]
(not (.-ok response)))

View File

@@ -1,10 +0,0 @@
(ns dev.mv)
(require '[clojure.java.io :as io])
(defn hook
{:shadow.build/stage :compile-finish}
[build-state & args]
(do
(when (not (.exists (io/file "./dist"))) (.mkdir (io/file "./dist")))
(spit "./dist/discord.d.ts" (slurp "./dts/discord.d.ts"))
(spit "./dist/index.d.ts" (slurp "./dts/index.d.ts")))
build-state)

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/index.js';
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");

1497
yarn.lock

File diff suppressed because it is too large Load Diff