mirror of
https://github.com/sern-handler/tools
synced 2026-06-06 01:16:59 +00:00
merge
This commit is contained in:
2
packages/poster/.gitignore
vendored
2
packages/poster/.gitignore
vendored
@@ -1,3 +1,3 @@
|
||||
dist/
|
||||
.shadow-cljs
|
||||
src/*.d.ts
|
||||
src/discord.d.ts
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 {}
|
||||
@@ -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?)
|
||||
})
|
||||
43
packages/poster/src/poster.cljs
Normal file
43
packages/poster/src/poster.cljs
Normal 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?) })
|
||||
@@ -1,4 +1,4 @@
|
||||
declare module 'index.js';
|
||||
declare module 'poster.*';
|
||||
|
||||
import type { paths } from './discord.d.ts'
|
||||
|
||||
@@ -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", {
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user