cordo 2.4.2 → 2.5.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.4.2",
3
+ "version": "2.5.0",
4
4
  "description": "A framework for handling complex discord api interactions",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -3,6 +3,7 @@ import type { Method } from "axios"
3
3
  import axios from "axios"
4
4
  import { FunctCompiler } from "../functions/compiler"
5
5
  import { FunctInternals } from "../functions/funct"
6
+ import { MissingContextError } from "../errors/builtin/missing-context"
6
7
  import { InteractionInternals, type CordoInteraction } from "./interaction"
7
8
  import { CordoMagic } from "./magic"
8
9
  import type { LockfileInternals } from "./files/lockfile"
@@ -46,6 +47,8 @@ export namespace CordoGateway {
46
47
 
47
48
  function apiRequest(method: Method, url: string, body?: Record<string, any>) {
48
49
  const config = CordoMagic.getConfig()
50
+ if (!config)
51
+ throw new MissingContextError('Could not make API request, no config found in context.')
49
52
  return axios({
50
53
  method,
51
54
  url,
@@ -88,6 +91,8 @@ export namespace CordoGateway {
88
91
  return null
89
92
 
90
93
  const config = CordoMagic.getConfig()
94
+ if (!config)
95
+ throw new MissingContextError('Could not respond to interaction, no config found in context.')
91
96
  if (!config.client.id) {
92
97
  console.warn(`No client id provided in config. Cannot respond to interactions.`)
93
98
  return null
@@ -108,7 +113,10 @@ export namespace CordoGateway {
108
113
  i = await Hooks.callHook('onBeforeHandle', i)
109
114
  if (!i) return
110
115
 
111
- const deferAfter = CordoMagic.getConfig().upstream.autoDeferMs
116
+ const config = CordoMagic.getConfig()
117
+ if (!config)
118
+ throw new MissingContextError('Could not handle interaction, no config found in context.')
119
+ const deferAfter = config.upstream.autoDeferMs
112
120
  if (deferAfter) {
113
121
  setTimeout(() => {
114
122
  if (!InteractionInternals.get(i).answered)
package/src/core/hooks.ts CHANGED
@@ -12,10 +12,17 @@ export namespace Hooks {
12
12
  hookName: Name,
13
13
  value: Value,
14
14
  context?: Context,
15
- config?: CordoConfig
15
+ config?: NonNullable<CordoConfig>
16
16
  ): ReturnType<NonNullable<CordoConfig['hooks'][Name]>> {
17
- if (!config)
18
- config = CordoMagic.getConfig()
17
+ if (!config) {
18
+ const contextConfig = CordoMagic.getConfig()
19
+ if (!contextConfig) {
20
+ console.warn(`Calling hook '${hookName}' failed, no config provided and no config found in context.`)
21
+ return value as any
22
+ }
23
+
24
+ config = contextConfig
25
+ }
19
26
 
20
27
  const hook = config.hooks[hookName]
21
28
  if (!hook)
@@ -35,7 +42,7 @@ export namespace Hooks {
35
42
  }
36
43
 
37
44
  export function isDefined(name: keyof CordoConfig['hooks']) {
38
- return !!CordoMagic.getConfig().hooks[name]
45
+ return !!CordoMagic.getConfig()?.hooks[name]
39
46
  }
40
47
 
41
48
  }
package/src/core/index.ts CHANGED
@@ -5,6 +5,8 @@ import { ConfigInternals, type CordoConfig, type ParsedCordoConfig } from './fil
5
5
  import { LockfileInternals } from './files/lockfile'
6
6
  import { CordoGateway } from './gateway'
7
7
  import { RoutingFilesystem } from './routing/filesystem'
8
+ import { CordoMagic } from './magic'
9
+ import type { CordoInteraction } from './interaction'
8
10
 
9
11
  export { type DynamicTypes } from './dynamic-types'
10
12
  export { type CordoConfig, defineCordoConfig } from './files/config'
@@ -76,3 +78,25 @@ export const Cordo = {
76
78
  respondToRawInteraction: CordoGateway.respondTo,
77
79
  }
78
80
  Object.freeze(Cordo)
81
+
82
+
83
+ export namespace Extend {
84
+
85
+ export function runInCordoContext(
86
+ fn: () => any,
87
+ ctx?: {
88
+ invoker?: CordoInteraction
89
+ lockfile?: LockfileInternals.ParsedLockfile
90
+ config?: ParsedCordoConfig
91
+ }
92
+ ) {
93
+ CordoMagic.Internals.runWithCtx(fn, {
94
+ invoker: ctx?.invoker ?? null,
95
+ lockfile: ctx?.lockfile ?? null,
96
+ config: ctx?.config ?? null,
97
+ cwd: '',
98
+ idCounter: 0
99
+ })
100
+ }
101
+
102
+ }
package/src/core/magic.ts CHANGED
@@ -7,9 +7,9 @@ import type { ParsedCordoConfig } from './files/config'
7
7
 
8
8
  type Context = {
9
9
  cwd: string
10
- lockfile: LockfileInternals.ParsedLockfile
11
- config: ParsedCordoConfig
12
- invoker: CordoInteraction
10
+ lockfile: LockfileInternals.ParsedLockfile | null
11
+ config: ParsedCordoConfig | null
12
+ invoker: CordoInteraction | null
13
13
  idCounter: number
14
14
  }
15
15
 
@@ -1,3 +1,4 @@
1
+ import { MissingContextError } from "../../errors/builtin/missing-context"
1
2
  import { Hooks } from "../hooks"
2
3
  import type { CordoInteraction } from "../interaction"
3
4
  import { CordoMagic } from "../magic"
@@ -60,6 +61,13 @@ export namespace RoutingResolve {
60
61
  const currentRoute = CordoMagic.getCwd()
61
62
  const invoker = CordoMagic.getInvoker()
62
63
 
64
+ if (!lockfile)
65
+ throw new MissingContextError('getRouteFromPath failed, no lockfile found in context.')
66
+ if (!currentRoute)
67
+ throw new MissingContextError('getRouteFromPath failed, no current route found in context.')
68
+ if (!invoker)
69
+ throw new MissingContextError('getRouteFromPath failed, no invoker found in context.')
70
+
63
71
  let startingPoint = currentRoute
64
72
  if (startingPoint.endsWith(DefaultFileName))
65
73
  startingPoint = startingPoint.slice(0, -DefaultFileName.length)
@@ -139,7 +147,7 @@ export namespace RoutingResolve {
139
147
  }
140
148
 
141
149
  export function getRouteFromId(routeId: string) {
142
- return CordoMagic.getLockfile().$runtime.routeImpls.get(routeId)
150
+ return CordoMagic.getLockfile()?.$runtime.routeImpls.get(routeId)
143
151
  }
144
152
 
145
153
  }
@@ -0,0 +1,13 @@
1
+ import { CordoError } from "../cordo-error"
2
+
3
+
4
+ export class MissingContextError extends CordoError {
5
+
6
+ constructor(public description: string) {
7
+ super(
8
+ 'Missing context',
9
+ description
10
+ )
11
+ }
12
+
13
+ }
@@ -8,7 +8,10 @@ import { CordoError } from "./cordo-error"
8
8
  export namespace HandleErrors {
9
9
 
10
10
  function findApplicableBoundaries(startingPath: string) {
11
- const errorBounds = CordoMagic.getLockfile().$runtime.errorBoundaries
11
+ const errorBounds = CordoMagic.getLockfile()?.$runtime.errorBoundaries
12
+ if (!errorBounds)
13
+ return []
14
+
12
15
  const routeFilePath = RoutingResolve.getRouteFromPath(startingPath, false).routeFilePath!
13
16
 
14
17
  const options = errorBounds.map(b => ({
@@ -1,6 +1,7 @@
1
1
  import { LockfileInternals } from "../core/files/lockfile"
2
2
  import { CordoMagic } from "../core/magic"
3
3
  import { RoutingResolve } from "../core/routing/resolve"
4
+ import { MissingContextError } from "../errors/builtin/missing-context"
4
5
  import { LibIds } from "../lib/ids"
5
6
  import { LibUtils } from "../lib/utils"
6
7
  import { FunctInternals, type CordoFunct } from "./funct"
@@ -58,7 +59,10 @@ export namespace FunctCompiler {
58
59
  }
59
60
 
60
61
  // encode arguments
61
- const lut = CordoMagic.getLockfile().lut
62
+ const lut = CordoMagic.getLockfile()?.lut
63
+ if (!lut)
64
+ throw new MissingContextError('toCustomId failed, no lockfile found in context.')
65
+
62
66
  let argusStr = ''
63
67
  let counter = -1
64
68
  for (const arg of LibUtils.iterate(argus, extraValues)) {
@@ -91,7 +95,7 @@ export namespace FunctCompiler {
91
95
  return arg.slice(1)
92
96
 
93
97
  if (arg[0] === LutArgumentIndicator)
94
- return CordoMagic.getLockfile().lut[LibIds.parse(arg.slice(1))] ?? ''
98
+ return CordoMagic.getLockfile()?.lut[LibIds.parse(arg.slice(1))] ?? ''
95
99
 
96
100
  if (arg[0] === ReferenceArgumentIndicator)
97
101
  return parsedArguments[LibIds.parseSingle(arg[1])] ?? ''
@@ -41,7 +41,7 @@ export function goto(
41
41
  ? FunctInternals.readFunct(path).path
42
42
  : path
43
43
 
44
- const flags = parseFlags(opts, FunctInternals.readFunct(path as any)?.flags ?? CordoMagic.getConfig().functDefaultFlags.gotoBits)
44
+ const flags = parseFlags(opts, FunctInternals.readFunct(path as any)?.flags ?? CordoMagic.getConfig()?.functDefaultFlags.gotoBits ?? 0)
45
45
 
46
46
  return FunctInternals.createFunct({
47
47
  type: 'goto',
@@ -58,7 +58,7 @@ export function run(
58
58
  ? FunctInternals.readFunct(path).path
59
59
  : path
60
60
 
61
- const flags = parseFlags(opts, FunctInternals.readFunct(path as any)?.flags ?? CordoMagic.getConfig().functDefaultFlags.runBits)
61
+ const flags = parseFlags(opts, FunctInternals.readFunct(path as any)?.flags ?? CordoMagic.getConfig()?.functDefaultFlags.runBits ?? 0)
62
62
 
63
63
  return FunctInternals.createFunct({
64
64
  type: 'run',