cordo 2.19.0 → 2.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -4
- package/src/core/files/command.ts +1 -1
- package/src/core/files/config.ts +5 -5
- package/src/core/gateway.ts +58 -13
- package/src/core/index.ts +1 -0
- package/src/plugin/hono.ts +6 -6
- package/tsconfig.json +5 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cordo",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.20.0",
|
|
4
4
|
"description": "A framework for handling complex discord api interactions",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts",
|
|
@@ -38,11 +38,10 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/maanex/cordo#readme",
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"axios": "^1.8.4",
|
|
42
41
|
"defu": "^6.1.7",
|
|
43
42
|
"discord-api-types": "^0.38.57",
|
|
44
|
-
"discord-interactions": "^
|
|
45
|
-
"type-fest": "^
|
|
43
|
+
"discord-interactions": "^4.4.0",
|
|
44
|
+
"type-fest": "^5.10.0"
|
|
46
45
|
},
|
|
47
46
|
"peerDependencies": {
|
|
48
47
|
"discord.js": "^14.0.0",
|
|
@@ -293,7 +293,7 @@ export namespace CommandInternals {
|
|
|
293
293
|
const res = await CordoGateway.bulkUpsertCommands(apiCommands, guild)
|
|
294
294
|
|
|
295
295
|
if (res.status === 429) {
|
|
296
|
-
const retryAfter = res.headers
|
|
296
|
+
const retryAfter = res.headers.get('retry-after') || res.data?.retry_after
|
|
297
297
|
const waitTime = retryAfter ? parseFloat(retryAfter) * 1000 : 5000
|
|
298
298
|
console.warn(`Rate limited (429) when bulk syncing commands. Waiting ${waitTime}ms...`)
|
|
299
299
|
await new Promise(resolve => setTimeout(resolve, waitTime))
|
package/src/core/files/config.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { resolve } from 'node:path'
|
|
|
2
2
|
import defu from 'defu'
|
|
3
3
|
import type { PartialDeep, Promisable } from 'type-fest'
|
|
4
4
|
import type { APIInteraction } from 'discord-api-types/v10'
|
|
5
|
-
import type {
|
|
5
|
+
import type { CordoApiResponse } from '../gateway'
|
|
6
6
|
import type { CordoInteraction } from '../interaction'
|
|
7
7
|
import type { StringComponentType } from '../../components/component'
|
|
8
8
|
import { parseFlags as runParseFlags, type FlagOpts as RunFlagOpts } from '../../functions/impl/run'
|
|
@@ -22,7 +22,7 @@ export type CordoConfig = {
|
|
|
22
22
|
* - Cordo will not write to the lockfile or generate types.
|
|
23
23
|
* - Cordo will not generate custom_ids for components.
|
|
24
24
|
* - Cordo will not be able to route.
|
|
25
|
-
*
|
|
25
|
+
*
|
|
26
26
|
* Use headless mode if you want to use cordo exclusively to render components without using cordo as your framework.
|
|
27
27
|
*/
|
|
28
28
|
headless: boolean
|
|
@@ -61,7 +61,7 @@ export type CordoConfig = {
|
|
|
61
61
|
/** gets called before a response is returned or sent to discord */
|
|
62
62
|
onBeforeRespond: HookFor<Record<string, any> | null, { interaction: CordoInteraction }>
|
|
63
63
|
/** gets called with the response of outgoing api calls. DOES NOT get called on the first response as that's not an outgoing api call */
|
|
64
|
-
onAfterRespond: HookFor<
|
|
64
|
+
onAfterRespond: HookFor<CordoApiResponse>,
|
|
65
65
|
/** gets called when outgoing api calls fail */
|
|
66
66
|
onNetworkError: HookFor<any>,
|
|
67
67
|
|
|
@@ -186,12 +186,12 @@ export namespace ConfigInternals {
|
|
|
186
186
|
|
|
187
187
|
if (!config.headless) {
|
|
188
188
|
config.paths.root = resolveRelativePath(config.paths.root)
|
|
189
|
-
|
|
189
|
+
|
|
190
190
|
config.paths.lockfile = resolveRelativePath(config.paths.lockfile, config.paths.root)
|
|
191
191
|
config.paths.types = config.paths.types
|
|
192
192
|
? resolveRelativePath(config.paths.types, config.paths.root)
|
|
193
193
|
: null
|
|
194
|
-
|
|
194
|
+
|
|
195
195
|
config.paths.routes = resolveRelativePath(config.paths.routes, config.paths.root)
|
|
196
196
|
}
|
|
197
197
|
|
package/src/core/gateway.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { ComponentType, InteractionResponseType, InteractionType, type APIApplicationCommand, type APIInteraction } from 'discord-api-types/v10'
|
|
2
|
-
import type { Method } from 'axios'
|
|
3
|
-
import axios from 'axios'
|
|
4
2
|
import { FunctCompiler } from '../functions/compiler'
|
|
5
3
|
import { FunctInternals } from '../functions/funct'
|
|
6
4
|
import { MissingContextError } from '../errors/builtin/missing-context'
|
|
@@ -13,6 +11,18 @@ import { RoutingResolve } from './routing/resolve'
|
|
|
13
11
|
import { RoutingRespond } from './routing/respond'
|
|
14
12
|
|
|
15
13
|
|
|
14
|
+
export type HttpMethod = 'get' | 'GET' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'delete' | 'DELETE'
|
|
15
|
+
|
|
16
|
+
export type CordoApiResponse<T = any> = {
|
|
17
|
+
status: number
|
|
18
|
+
statusText: string
|
|
19
|
+
ok: boolean
|
|
20
|
+
headers: Headers
|
|
21
|
+
data: T
|
|
22
|
+
raw: Response
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
16
26
|
export namespace CordoGateway {
|
|
17
27
|
|
|
18
28
|
/**
|
|
@@ -56,20 +66,55 @@ export namespace CordoGateway {
|
|
|
56
66
|
httpCallback?.({ type: InteractionResponseType.Pong })
|
|
57
67
|
}
|
|
58
68
|
|
|
59
|
-
function apiRequest(method:
|
|
69
|
+
async function apiRequest<T = any>(method: HttpMethod | string, endpoint: string, body?: Record<string, any>): Promise<CordoApiResponse<T>> {
|
|
60
70
|
const config = CordoMagic.getConfig()
|
|
61
71
|
if (!config)
|
|
62
72
|
throw new MissingContextError('Could not make API request, no config found in context.')
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
|
|
74
|
+
const url = /^https?:\/\//i.test(endpoint)
|
|
75
|
+
? endpoint
|
|
76
|
+
: `${config.upstream.baseUrl.replace(/\/+$/, '')}/${endpoint.replace(/^\/+/, '')}`
|
|
77
|
+
|
|
78
|
+
const upperMethod = method.toUpperCase()
|
|
79
|
+
const hasBody = body != null && ![ 'GET', 'HEAD' ].includes(upperMethod)
|
|
80
|
+
|
|
81
|
+
const headers = new Headers()
|
|
82
|
+
if (config.client.token)
|
|
83
|
+
headers.set('Authorization', `Bot ${config.client.token}`)
|
|
84
|
+
if (hasBody)
|
|
85
|
+
headers.set('Content-Type', 'application/json')
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
const rawRes = await fetch(url, {
|
|
89
|
+
method: upperMethod,
|
|
90
|
+
headers,
|
|
91
|
+
body: hasBody ? JSON.stringify(body) : undefined
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
let data: any = null
|
|
95
|
+
const contentType = rawRes.headers.get('content-type')
|
|
96
|
+
if (contentType?.includes('application/json')) {
|
|
97
|
+
try {
|
|
98
|
+
data = await rawRes.json()
|
|
99
|
+
} catch {}
|
|
100
|
+
} else {
|
|
101
|
+
try {
|
|
102
|
+
data = await rawRes.text() || null
|
|
103
|
+
} catch {}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const res: CordoApiResponse<T> = {
|
|
107
|
+
status: rawRes.status,
|
|
108
|
+
statusText: rawRes.statusText,
|
|
109
|
+
ok: rawRes.ok,
|
|
110
|
+
headers: rawRes.headers,
|
|
111
|
+
data,
|
|
112
|
+
raw: rawRes
|
|
113
|
+
}
|
|
114
|
+
return Hooks.callHook('onAfterRespond', res)
|
|
115
|
+
} catch (err) {
|
|
116
|
+
return Hooks.callHook('onNetworkError', err)
|
|
117
|
+
}
|
|
73
118
|
}
|
|
74
119
|
|
|
75
120
|
/** if the payload is null the interaction will be defered if not done already */
|
package/src/core/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { CordoInteraction } from './interaction'
|
|
|
11
11
|
import { CommandInternals } from './files/command'
|
|
12
12
|
|
|
13
13
|
export { type DynamicTypes } from './dynamic-types'
|
|
14
|
+
export { type CordoApiResponse, type HttpMethod } from './gateway'
|
|
14
15
|
export { type CordoConfig, defineCordoConfig } from './files/config'
|
|
15
16
|
export { type CordoRoute, type RouteRequest, defineCordoRoute, assertCordoRequest } from './files/route'
|
|
16
17
|
export { type CordoCommand, defineCordoCommand } from './files/command'
|
package/src/plugin/hono.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { verifyKey } from
|
|
2
|
-
import { Hono } from
|
|
3
|
-
import { Cordo } from
|
|
4
|
-
import { PluginConfigurationError } from
|
|
1
|
+
import { verifyKey } from 'discord-interactions'
|
|
2
|
+
import { Hono } from 'hono'
|
|
3
|
+
import { Cordo } from '../core'
|
|
4
|
+
import { PluginConfigurationError } from '../errors'
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
export function useWithHono(clientPublicKey: string) {
|
|
8
8
|
if (!clientPublicKey)
|
|
9
|
-
throw new PluginConfigurationError('You must specify a Discord client public key')
|
|
9
|
+
throw new PluginConfigurationError('You must specify a Discord client public key')
|
|
10
10
|
|
|
11
11
|
const app = new Hono()
|
|
12
12
|
|
|
@@ -19,7 +19,7 @@ export function useWithHono(clientPublicKey: string) {
|
|
|
19
19
|
return c.text('Bad Request', 400)
|
|
20
20
|
|
|
21
21
|
const body = await Bun.readableStreamToArrayBuffer(bodyStream)
|
|
22
|
-
const isValid = verifyKey(body, signature, timestamp, clientPublicKey)
|
|
22
|
+
const isValid = await verifyKey(body, signature, timestamp, clientPublicKey)
|
|
23
23
|
if (!isValid)
|
|
24
24
|
return c.text('Bad Request', 401)
|
|
25
25
|
|