cordo 2.18.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordo",
3
- "version": "2.18.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",
@@ -24,7 +24,7 @@
24
24
  "lint": "eslint --ext .ts --ignore-path .gitignore src/**/*.ts",
25
25
  "tunnel": "cloudflared tunnel --config ~/.cloudflared/config.yml run fsb",
26
26
  "dev": "tsc && node --trace-warnings ./build/index.js",
27
- "test": "echo \"Error: no tests specified\" && exit 0",
27
+ "test": "bun test",
28
28
  "prepub": "npm run lint && npm run build && npm run test && npm pub"
29
29
  },
30
30
  "repository": {
@@ -38,25 +38,38 @@
38
38
  },
39
39
  "homepage": "https://github.com/maanex/cordo#readme",
40
40
  "dependencies": {
41
- "@types/bun": "^1.4.3",
42
- "axios": "^0.21.4",
43
41
  "defu": "^6.1.7",
44
- "discord-interactions": "^2.4.1",
45
- "type-fest": "^4.41.0"
42
+ "discord-api-types": "^0.38.57",
43
+ "discord-interactions": "^4.4.0",
44
+ "type-fest": "^5.10.0"
45
+ },
46
+ "peerDependencies": {
47
+ "discord.js": "^14.0.0",
48
+ "express": "^4.0.0 || ^5.0.0",
49
+ "hono": "^4.0.0"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "discord.js": {
53
+ "optional": true
54
+ },
55
+ "express": {
56
+ "optional": true
57
+ },
58
+ "hono": {
59
+ "optional": true
60
+ }
46
61
  },
47
62
  "devDependencies": {
48
- "@types/axios": "^0.14.4",
63
+ "@types/bun": "^1.4.4",
49
64
  "@types/express": "^4.17.25",
50
- "@types/node": "^17.0.45",
65
+ "@types/node": "^22.13.0",
51
66
  "@typescript-eslint/eslint-plugin": "^4.33.0",
52
67
  "@typescript-eslint/parser": "^4.33.0",
53
- "discord-api-types": "^0.38.57",
54
68
  "discord.js": "^14.27.0",
55
69
  "eslint": "^7.32.0",
56
70
  "eslint-config-maanex": "^1.3.3",
57
71
  "express": "^4.22.3",
58
- "hono": "^4.13.13",
59
- "reflect-metadata": "^0.1.14",
72
+ "hono": "^4.13.14",
60
73
  "typescript": "^5.9.3",
61
74
  "vitepress": "^1.6.4",
62
75
  "vue": "^3.5.44"
@@ -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['retry-after'] || res.data?.retry_after
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))
@@ -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 { AxiosResponse } from 'axios'
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<AxiosResponse>,
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
 
@@ -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: Method, url: string, body?: Record<string, any>) {
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
- return axios({
64
- method,
65
- url,
66
- baseURL: config.upstream.baseUrl,
67
- headers: config.client.token ? { Authorization: `Bot ${config.client.token}` } : undefined,
68
- data: body,
69
- validateStatus: null
70
- })
71
- .then(res => Hooks.callHook('onAfterRespond', res))
72
- .catch(res => Hooks.callHook('onNetworkError', res))
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'
@@ -1,4 +1,4 @@
1
- import { ApplicationCommandOptionType, ApplicationCommandType, InteractionType } from 'discord.js'
1
+ import { ApplicationCommandOptionType, ApplicationCommandType, InteractionType } from 'discord-api-types/v10'
2
2
  import { MissingContextError } from '../../errors/builtin/missing-context'
3
3
  import { FunctCompiler } from '../../functions/compiler'
4
4
  import { Hooks } from '../hooks'
@@ -1,12 +1,12 @@
1
- import { verifyKey } from "discord-interactions"
2
- import { Hono } from "hono"
3
- import { Cordo } from "../core"
4
- import { PluginConfigurationError } from "../errors"
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
 
package/tsconfig.json CHANGED
@@ -32,7 +32,11 @@
32
32
  "@core/*": [ "./src/core/*" ],
33
33
  "@plugin/*": [ "./src/plugin/*" ],
34
34
  "@lib/*": [ "./src/lib/*" ]
35
- }
35
+ },
36
+
37
+ "types": [
38
+ "bun"
39
+ ]
36
40
  },
37
41
  "exclude": [
38
42
  "test"