mirror of
https://github.com/sern-handler/tools
synced 2026-06-06 01:16:59 +00:00
move to squint
This commit is contained in:
1
packages/poster/.gitignore
vendored
1
packages/poster/.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
dist/
|
||||
.shadow-cljs
|
||||
src/*.d.ts
|
||||
|
||||
@@ -2,16 +2,15 @@
|
||||
"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/core/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/resources/discord.d.ts",
|
||||
"repl": "npx squint repl"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
@@ -19,5 +18,8 @@
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"tag": "beta"
|
||||
},
|
||||
"dependencies": {
|
||||
"squint-cljs": "latest"
|
||||
}
|
||||
}
|
||||
|
||||
3
packages/poster/squint.edn
Normal file
3
packages/poster/squint.edn
Normal file
@@ -0,0 +1,3 @@
|
||||
{:paths ["src"]
|
||||
:output-dir "dist/"
|
||||
:copy-resources #{ :d.ts }}
|
||||
@@ -25,11 +25,9 @@
|
||||
"body" (.stringify js/JSON 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))
|
||||
|
||||
|
||||
|
||||
@@ -1,41 +1,45 @@
|
||||
(ns core.poster
|
||||
(:require [clojure.string :refer [replace]]
|
||||
[shadow.cljs.modern :refer (js-await)]
|
||||
(:require [clojure.string :as s]
|
||||
[core.actions :refer [actions]]))
|
||||
|
||||
(def base "https://discord.com/api/v10")
|
||||
(def ^:private 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))))
|
||||
(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]
|
||||
#_(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]
|
||||
(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) } ]
|
||||
(js-await [appid (fetch-application header)]
|
||||
(fn [action opts]
|
||||
(let [[url mkrequest] (actions action)
|
||||
"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))]
|
||||
url (new js/URL (inject url options)) ]
|
||||
(set! (.-search url) (?params (.-query ^js opts)))
|
||||
(js/fetch url (mkrequest (.-body ^js opts) header)))))))
|
||||
(js/fetch url (mkrequest (.-body ^js opts) header))))))
|
||||
|
||||
(defn isOk? [^js response]
|
||||
(defn- isOk? [^js response]
|
||||
(.-ok response))
|
||||
|
||||
(defn is4XX? [^js response]
|
||||
(not (.-ok response)))
|
||||
|
||||
|
||||
(def default {
|
||||
:client poster
|
||||
:isOk isOk?
|
||||
:is4XX (complement isOk?)
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
@@ -1,4 +1,4 @@
|
||||
import poster from '../dist/index.js';
|
||||
import poster from '../dist/core/poster.mjs';
|
||||
|
||||
const send = await poster.client("");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user