|
12 | 12 | - [Object field](#object-field) |
13 | 13 | - [Restrictions](#restrictions) |
14 | 14 | - [Json field](#json-field) |
15 | | - |
| 15 | + - [Hooks](#hooks) |
16 | 16 |
|
17 | 17 | Each plugin should be created handle a specific part of the data. |
18 | 18 | Avoid creating a single plugin to handle all the data, as this will make the code harder to maintain and understand. |
@@ -389,3 +389,91 @@ The `json` field is an escape-hatch that allows the user to input any JSON data. |
389 | 389 | ``` |
390 | 390 |
|
391 | 391 |  |
| 392 | + |
| 393 | +## Hooks |
| 394 | + |
| 395 | +> [!WARNING] |
| 396 | +> The hooks are an advanced feature and should be used with caution. |
| 397 | +
|
| 398 | +Hooks are functions that can be added to the plugins to perform actions at specific points in their lifecycle. |
| 399 | + |
| 400 | +Available hooks: |
| 401 | +- `onAfterInit(targetInstance: Plugin, data: any) => void` - Executed after the target plugin's `init` function. Can be a promise. |
| 402 | +- `onAfterEditSchema(targetInstance: Plugin, formData: any, schema: PluginEditSchema) => PluginEditSchema` - Allows changing a plugin's schema. Receives the schema returned by the target plugin's `editSchema` function and the data entered in the form. Should return the modified schema. |
| 403 | + |
| 404 | +Hooks should be registered in the plugin's constructor: |
| 405 | + |
| 406 | +```ts |
| 407 | +import { Plugin } from '@stac-manager/data-core'; |
| 408 | + |
| 409 | +export class PluginName extends Plugin { |
| 410 | + name = 'Plugin Name'; |
| 411 | + |
| 412 | + constructor() { |
| 413 | + super(); |
| 414 | + this.registerHook( |
| 415 | + 'Target plugin name', |
| 416 | + 'onAfterInit', |
| 417 | + (targetInstance: Plugin, data: any) => { |
| 418 | + // Do something. |
| 419 | + } |
| 420 | + ); |
| 421 | + |
| 422 | + this.registerHook( |
| 423 | + 'Target plugin name', |
| 424 | + 'onAfterEditSchema', |
| 425 | + (targetInstance: Plugin, formData: any, schema: PluginEditSchema) => { |
| 426 | + // Do something. |
| 427 | + return schema; |
| 428 | + } |
| 429 | + ); |
| 430 | + } |
| 431 | +} |
| 432 | +``` |
| 433 | + |
| 434 | +**Real world example** |
| 435 | +Whenever an STAC extension plugin is added, it should add the extension to the `stac_extensions` field of the CollectionsCore plugin. This will allow the interface to show the extension as an option in the dropdown. |
| 436 | + |
| 437 | +This can be achieved with th `onAfterEditSchema` hook: |
| 438 | +```ts |
| 439 | +import { Plugin } from '@stac-manager/data-core'; |
| 440 | + |
| 441 | +export class PluginName extends Plugin { |
| 442 | + name = 'Plugin Name'; |
| 443 | + |
| 444 | + constructor() { |
| 445 | + super(); |
| 446 | + this.registerHook( |
| 447 | + 'CollectionsCore', |
| 448 | + 'onAfterEditSchema', |
| 449 | + (targetInstance: Plugin, formData: any, schema: PluginEditSchema) => { |
| 450 | + const stac_extensions = schema.properties |
| 451 | + .stac_extensions as SchemaFieldArray<SchemaFieldString>; |
| 452 | + |
| 453 | + // Set the new extension value in the schema. |
| 454 | + stac_extensions.items.enum!.push([value, label]); |
| 455 | + |
| 456 | + return schema; |
| 457 | + } |
| 458 | + ); |
| 459 | + } |
| 460 | +} |
| 461 | +``` |
| 462 | + |
| 463 | +> [!TIP] |
| 464 | +> Given that this is a common enough use-case there is a helper function (`addStacExtensionOption`) that can be used. |
| 465 | +> ```ts |
| 466 | +>import { Plugin } from '@stac-manager/data-core'; |
| 467 | +>import { addStacExtensionOption } from '@stac-manager/data-plugins'; |
| 468 | +> |
| 469 | +>class PluginName extends Plugin { |
| 470 | +> constructor() { |
| 471 | +> super(); |
| 472 | +> |
| 473 | +> addStacExtensionOption( |
| 474 | +> this, |
| 475 | +> 'Item Assets Definition', |
| 476 | +> 'https://stac-extensions.github.io/item-assets/v1.0.0/schema.json' |
| 477 | +> ); |
| 478 | +> } |
| 479 | +>} |
0 commit comments