|
| 1 | +--- |
| 2 | +title: Action |
| 3 | +description: The root class for every Hercules action |
| 4 | +--- |
| 5 | + |
| 6 | +import { TypeTable } from 'fumadocs-ui/components/type-table'; |
| 7 | + |
| 8 | +`Action` is the entry point of every Hercules action. It manages the gRPC connection to Aquila, holds all registered |
| 9 | +functions, events, and data types, and emits lifecycle events you can subscribe to. |
| 10 | + |
| 11 | +```ts |
| 12 | +import { Action } from '@code0-tech/hercules'; |
| 13 | +``` |
| 14 | + |
| 15 | +## Constructor |
| 16 | + |
| 17 | +```ts |
| 18 | +new Action( |
| 19 | + identifier: string, |
| 20 | + version: string, |
| 21 | + aquilaUrl: string | undefined, |
| 22 | + author: string, |
| 23 | + icon: string, |
| 24 | + documentation: string, |
| 25 | + name: Translation[], |
| 26 | + configurationDefinitions?: ConfigurationDefinition[] |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +<TypeTable |
| 31 | + type={{ |
| 32 | + identifier: { |
| 33 | + description: 'Unique identifier for this action.', |
| 34 | + type: 'string', |
| 35 | + required: true, |
| 36 | + }, |
| 37 | + version: { |
| 38 | + description: 'Semver version string.', |
| 39 | + type: 'string', |
| 40 | + required: true, |
| 41 | + }, |
| 42 | + aquilaUrl: { |
| 43 | + description: 'Default gRPC endpoint for Aquila. Can be overridden in connect().', |
| 44 | + type: 'string | undefined', |
| 45 | + required: true, |
| 46 | + }, |
| 47 | + author: { |
| 48 | + description: 'Author or organisation name.', |
| 49 | + type: 'string', |
| 50 | + required: true, |
| 51 | + }, |
| 52 | + icon: { |
| 53 | + description: 'Icon identifier (e.g. "tabler:bolt").', |
| 54 | + type: 'string', |
| 55 | + required: true, |
| 56 | + }, |
| 57 | + documentation: { |
| 58 | + description: 'Short documentation string for this action.', |
| 59 | + type: 'string', |
| 60 | + required: true, |
| 61 | + }, |
| 62 | + name: { |
| 63 | + description: 'Localised display names.', |
| 64 | + type: 'Translation[]', |
| 65 | + required: true, |
| 66 | + }, |
| 67 | + configurationDefinitions: { |
| 68 | + description: 'Per-project configuration fields shown in the platform UI.', |
| 69 | + type: 'ConfigurationDefinition[]', |
| 70 | + default: '[]', |
| 71 | + }, |
| 72 | + }} |
| 73 | +/> |
| 74 | + |
| 75 | +### Example |
| 76 | + |
| 77 | +```ts |
| 78 | +const action = new Action( |
| 79 | + 'my-action', |
| 80 | + '1.0.0', |
| 81 | + 'aquila.example.com:8081', |
| 82 | + 'my-org', |
| 83 | + 'tabler:bolt', |
| 84 | + 'Does something useful', |
| 85 | + [{ code: 'en-US', content: 'My Action' }], |
| 86 | + [ |
| 87 | + { |
| 88 | + identifier: 'API_KEY', |
| 89 | + type: 'string', |
| 90 | + name: [{ code: 'en-US', content: 'API Key' }], |
| 91 | + }, |
| 92 | + ], |
| 93 | +); |
| 94 | +``` |
| 95 | + |
| 96 | +## Properties |
| 97 | + |
| 98 | +### Managers |
| 99 | + |
| 100 | +Each manager is a key-value store backed by a `Map`. See [BaseManager](./base-manager.mdx) for available methods. |
| 101 | + |
| 102 | +<TypeTable |
| 103 | + type={{ |
| 104 | + configs: { |
| 105 | + description: 'Stores per-project configurations keyed by project ID. Updated automatically when Aquila sends module configurations.', |
| 106 | + type: 'ConfigManager', |
| 107 | + }, |
| 108 | + functions: { |
| 109 | + description: 'Stores registered public-facing function definitions keyed by runtimeName.', |
| 110 | + type: 'FunctionManager', |
| 111 | + }, |
| 112 | + runtimeFunctions: { |
| 113 | + description: 'Stores registered runtime function definitions keyed by runtimeName.', |
| 114 | + type: 'RuntimeFunctionManager', |
| 115 | + }, |
| 116 | + dataTypes: { |
| 117 | + description: 'Stores registered data type definitions keyed by identifier.', |
| 118 | + type: 'DataTypeManager', |
| 119 | + }, |
| 120 | + events: { |
| 121 | + description: 'Stores registered public-facing event definitions keyed by identifier.', |
| 122 | + type: 'EventManager', |
| 123 | + }, |
| 124 | + runtimeEvents: { |
| 125 | + description: 'Stores registered runtime event definitions keyed by identifier.', |
| 126 | + type: 'RuntimeEventManager', |
| 127 | + }, |
| 128 | + }} |
| 129 | +/> |
| 130 | + |
| 131 | +### Getters |
| 132 | + |
| 133 | +<TypeTable |
| 134 | + type={{ |
| 135 | + identifier: { |
| 136 | + description: 'The action identifier passed to the constructor.', |
| 137 | + type: 'string', |
| 138 | + }, |
| 139 | + version: { |
| 140 | + description: 'The version string passed to the constructor.', |
| 141 | + type: 'string', |
| 142 | + }, |
| 143 | + stream: { |
| 144 | + description: 'The active gRPC duplex stream. undefined until connect() resolves.', |
| 145 | + type: 'DuplexStreamingCall | undefined', |
| 146 | + }, |
| 147 | + }} |
| 148 | +/> |
| 149 | + |
| 150 | +## Methods |
| 151 | + |
| 152 | +### `registerFunction` |
| 153 | + |
| 154 | +```ts |
| 155 | +action.registerFunction<T extends RuntimeFunctionClass>(klass: FunctionClass<T>): void |
| 156 | +``` |
| 157 | +
|
| 158 | +Registers a public-facing function definition. The class must extend a `RuntimeFunctionClass`. |
| 159 | +
|
| 160 | +```ts |
| 161 | +action.registerFunction(FibonacciFunction); |
| 162 | +``` |
| 163 | +
|
| 164 | +### `registerRuntimeFunction` |
| 165 | +
|
| 166 | +```ts |
| 167 | +action.registerRuntimeFunction(klass: RuntimeFunctionClass): void |
| 168 | +``` |
| 169 | +
|
| 170 | +Registers a runtime function implementation. Unless `@OmitRuntimeFunction()` is applied, a matching `FunctionProps` |
| 171 | +entry is also created automatically. |
| 172 | +
|
| 173 | +```ts |
| 174 | +action.registerRuntimeFunction(FibonacciRuntimeFunction); |
| 175 | +``` |
| 176 | +
|
| 177 | +### `registerDataTypeClass` |
| 178 | +
|
| 179 | +```ts |
| 180 | +action.registerDataTypeClass(klass: DataTypeClass): void |
| 181 | +``` |
| 182 | +
|
| 183 | +Registers a custom data type. |
| 184 | +
|
| 185 | +### `registerEventClass` |
| 186 | +
|
| 187 | +```ts |
| 188 | +action.registerEventClass(klass: EventClass): void |
| 189 | +``` |
| 190 | +
|
| 191 | +Registers a user-facing event (flow type). |
| 192 | +
|
| 193 | +### `registerRuntimeEventClass` |
| 194 | +
|
| 195 | +```ts |
| 196 | +action.registerRuntimeEventClass(klass: RuntimeEventClass): void |
| 197 | +``` |
| 198 | +
|
| 199 | +Registers a runtime event definition. Unless `@OmitEvent()` is applied, a matching `EventModel` entry is also created |
| 200 | +automatically. |
| 201 | +
|
| 202 | +### `connect` |
| 203 | +
|
| 204 | +```ts |
| 205 | +action.connect(authToken: string, aquilaUrl?: string, grpcOptions?: GrpcOptions): Promise<void> |
| 206 | +``` |
| 207 | +
|
| 208 | +Opens the gRPC connection to Aquila, sends the module descriptor, and starts processing the incoming stream. |
| 209 | +
|
| 210 | +<TypeTable |
| 211 | + type={{ |
| 212 | + authToken: { |
| 213 | + description: 'Authentication token for Aquila.', |
| 214 | + type: 'string', |
| 215 | + required: true, |
| 216 | + }, |
| 217 | + aquilaUrl: { |
| 218 | + description: 'Override the URL set in the constructor.', |
| 219 | + type: 'string', |
| 220 | + }, |
| 221 | + grpcOptions: { |
| 222 | + description: 'Additional gRPC transport options.', |
| 223 | + type: 'GrpcOptions', |
| 224 | + }, |
| 225 | + }} |
| 226 | +/> |
| 227 | +
|
| 228 | +```ts |
| 229 | +await action.connect(process.env.AUTH_TOKEN!); |
| 230 | +``` |
| 231 | +
|
| 232 | +### `fire` |
| 233 | +
|
| 234 | +```ts |
| 235 | +action.fire( |
| 236 | + eventClass: EventClass | RuntimeEventClass, |
| 237 | + projectId: number | bigint, |
| 238 | + payload: PlainValue |
| 239 | +): Promise<void> |
| 240 | +``` |
| 241 | +
|
| 242 | +Dispatches an event to Aquila for a specific project. Throws if `connect()` has not been called or if the class is |
| 243 | +missing an `@Identifier` decorator. |
| 244 | +
|
| 245 | +<TypeTable |
| 246 | + type={{ |
| 247 | + eventClass: { |
| 248 | + description: 'The EventClass or RuntimeEventClass to fire. Must have an @Identifier decorator.', |
| 249 | + type: 'EventClass | RuntimeEventClass', |
| 250 | + required: true, |
| 251 | + }, |
| 252 | + projectId: { |
| 253 | + description: 'ID of the project this event belongs to.', |
| 254 | + type: 'number | bigint', |
| 255 | + required: true, |
| 256 | + }, |
| 257 | + payload: { |
| 258 | + description: 'Event payload matching the event signature.', |
| 259 | + type: 'PlainValue', |
| 260 | + required: true, |
| 261 | + }, |
| 262 | + }} |
| 263 | +/> |
| 264 | +
|
| 265 | +```ts |
| 266 | +await action.fire(UserCreatedRuntimeEvent, projectId, { userId: 42 }); |
| 267 | +``` |
| 268 | +
|
| 269 | +All manager properties on `Action` extend [`BaseManager`](./base-manager.mdx). |
| 270 | +
|
| 271 | +`Action` extends Node.js `EventEmitter` — see [`CodeZeroEvent`](./code-zero-event.mdx) for all available events. |
0 commit comments