Skip to content

Commit 121dd25

Browse files
authored
Merge pull request #315 from HarperDB/feature/document-plugins-ts-support
Document plugins TS support
2 parents e15db16 + 34e432a commit 121dd25

File tree

1 file changed

+64
-54
lines changed

1 file changed

+64
-54
lines changed

docs/reference/components/plugins.md

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,17 @@ In this example, the entry handler method passed to `handleEntry` will manage th
161161

162162
This example is heavily simplified, but it demonstrates how the different key parts of `scope` can be used together to provide a performant and reactive application experience.
163163

164-
## Function: `handleApplication(scope: Scope): void | Promise<void>`
164+
## API
165+
166+
### TypeScript support
167+
168+
The classes and types referenced below are all exported by the `harperdb` package. Just import the ones you need like this:
169+
170+
```typescript
171+
import { Scope, type Config } from 'harperdb';
172+
```
173+
174+
### Function: `handleApplication(scope: Scope): void | Promise<void>`
165175

166176
Parameters:
167177

@@ -171,29 +181,29 @@ Returns: `void | Promise<void>`
171181

172182
This is the only method a plugin module must export. It can be async and is awaited by the component loader. The `scope` argument provides access to the relative application's configuration, resources, and other APIs.
173183

174-
## Class: `Scope`
184+
### Class: `Scope`
175185

176186
- Extends [`EventEmitter`](https://nodejs.org/docs/latest/api/events.html#class-eventemitter)
177187

178-
### Event: `'close'`
188+
#### Event: `'close'`
179189

180190
Emitted after the scope is closed via the `close()` method.
181191

182-
### Event: `'error'`
192+
#### Event: `'error'`
183193

184194
- `error` - `unknown` - The error that occurred.
185195

186-
### Event: `'ready'`
196+
#### Event: `'ready'`
187197

188198
Emitted when the Scope is ready to be used after loading the associated config file. It is awaited by the component loader, so it is not necessary to await it within the `handleApplication()` method.
189199

190-
### `scope.close()`
200+
#### `scope.close()`
191201

192202
Returns: `this` - The current `Scope` instance.
193203

194204
Closes all associated entry handlers, the associated `scope.options` instance, emits the `'close'` event, and then removes all other listeners on the instance.
195205

196-
### `scope.handleEntry([files][, handler])`
206+
#### `scope.handleEntry([files][, handler])`
197207

198208
Parameters:
199209

@@ -247,23 +257,23 @@ customPlugin:
247257
248258
Then the default `EntryHandler` instances would be created to handle all entries within the `web` directory.
249259

250-
### `scope.requestRestart()`
260+
#### `scope.requestRestart()`
251261

252262
Returns: `void`
253263

254264
Request a Harper restart. This **does not** restart the instance immediately, but rather indicates to the user that a restart is required. This should be called when the plugin cannot handle the entry event and wants to indicate to the user that the Harper instance should be restarted.
255265

256266
This method is called automatically by the `scope` instance if the user has not defined an `scope.options.on('change')` handler or if an event handler exists and is missing a necessary handler method.
257267

258-
### `scope.resources`
268+
#### `scope.resources`
259269

260270
Returns: `Map<string, Resource>` - A map of the currently loaded [Resource](../globals#resource) instances.
261271

262-
### `scope.server`
272+
#### `scope.server`
263273

264274
Returns: `server` - A reference to the [server](../globals#server) global API.
265275

266-
### `scope.options`
276+
#### `scope.options`
267277

268278
Returns: [`OptionsWatcher`](#class-optionswatcher) - An instance of the `OptionsWatcher` class that provides access to the application's configuration options. Emits `'change'` events when the respective plugin part of the component's config file is modified.
269279

@@ -293,39 +303,39 @@ Then modifying the `files` option in the `config.yaml` to `bar.js` would log the
293303
Files option changed to: bar.js
294304
```
295305

296-
### `scope.logger`
306+
#### `scope.logger`
297307

298308
Returns: `logger` - A scoped instance of the [`logger`](../globals#logger) class that provides logging capabilities for the plugin.
299309

300310
It is recommended to use this instead of the `logger` global.
301311

302-
### `scope.name`
312+
#### `scope.name`
303313

304314
Returns: `string` - The name of the plugin as configured in the `config.yaml` file. This is the key under which the plugin is configured.
305315

306-
### `scope.directory`
316+
#### `scope.directory`
307317

308318
Returns: `string` - The directory of the application. This is the root directory of the component where the `config.yaml` file is located.
309319

310-
## Interface: `FilesOption`
320+
### Interface: `FilesOption`
311321

312322
- `string` | `string[]` | [`FilesOptionObject`](#interface-filesoptionobject)
313323

314-
## Interface: `FilesOptionObject`
324+
### Interface: `FilesOptionObject`
315325

316326
- `source` - `string` | `string[]` - _required_ - The glob pattern string or array of strings.
317327
- `ignore` - `string` | `string[]` - _optional_ - An array of glob patterns to exclude from matches. This is an alternative way to use negative patterns. Defaults to `[]`.
318328

319-
## Interface: `FileAndURLPathConfig`
329+
### Interface: `FileAndURLPathConfig`
320330

321331
- `files` - [`FilesOption`](#interface-filesoption) - _required_ - A glob pattern string, array of glob pattern strings, or a more expressive glob options object determining the set of files and directories to be resolved for the plugin.
322332
- `urlPath` - `string` - _optional_ - A base URL path to prepend to the resolved `files` entries.
323333

324-
## Class: `OptionsWatcher`
334+
### Class: `OptionsWatcher`
325335

326336
- Extends [`EventEmitter`](https://nodejs.org/docs/latest/api/events.html#class-eventemitter)
327337

328-
### Event: `'change'`
338+
#### Event: `'change'`
329339

330340
- `key` - `string[]` - The key of the changed option split into parts (e.g. `foo.bar` becomes `['foo', 'bar']`).
331341
- `value` - [`ConfigValue`](#interface-configvalue) - The new value of the option.
@@ -354,31 +364,31 @@ scope.options.on('change', (key, value, config) => {
354364
});
355365
```
356366

357-
### Event: `'close'`
367+
#### Event: `'close'`
358368

359369
Emitted when the `OptionsWatcher` is closed via the `close()` method. The watcher is not usable after this event is emitted.
360370

361-
### Event: `'error'`
371+
#### Event: `'error'`
362372

363373
- `error` - `unknown` - The error that occurred.
364374

365-
### Event: `'ready'`
375+
#### Event: `'ready'`
366376

367377
- `config` - [`ConfigValue`](#interface-configvalue) | `undefined` - The configuration object of the plugin, if present.
368378

369379
This event can be emitted multiple times. It is first emitted upon the initial load, but will also be emitted after restoring a configuration file or configuration object after a `'remove'` event.
370380

371-
### Event: `'remove'`
381+
#### Event: `'remove'`
372382

373383
The configuration was removed. This can happen if the configuration file was deleted, the configuration object within the file is deleted, or if the configuration file fails to parse. Once restored, the `'ready'` event will be emitted again.
374384

375-
### `options.close()`
385+
#### `options.close()`
376386

377387
Returns: `this` - The current `OptionsWatcher` instance.
378388

379389
Closes the options watcher, removing all listeners and preventing any further events from being emitted. The watcher is not usable after this method is called.
380390

381-
### `options.get(key)`
391+
#### `options.get(key)`
382392

383393
Parameters:
384394

@@ -388,37 +398,37 @@ Returns: [`ConfigValue`](#interface-configvalue) | `undefined`
388398

389399
If the config is defined it will attempt to retrieve the value of the option at the specified key. If the key does not exist, it will return `undefined`.
390400

391-
### `options.getAll()`
401+
#### `options.getAll()`
392402

393403
Returns: [`ConfigValue`](#interface-configvalue) | `undefined`
394404

395405
Returns the entire configuration object for the plugin. If the config is not defined, it will return `undefined`.
396406

397-
### `options.getRoot()`
407+
#### `options.getRoot()`
398408

399409
Returns: [`Config`](#interface-config) | `undefined`
400410

401411
Returns the root configuration object of the application. This is the entire configuration object, basically the parsed form of the `config.yaml`. If the config is not defined, it will return `undefined`.
402412

403-
### Interface: `Config`
413+
#### Interface: `Config`
404414

405415
- `[key: string]` [`ConfigValue`](#interface-configvalue)
406416

407417
An object representing the `config.yaml` file configuration.
408418

409-
### Interface: `ConfigValue`
419+
#### Interface: `ConfigValue`
410420

411421
- `string` | `number` | `boolean` | `null` | `undefined` | `ConfigValue[]` | [`Config`](#interface-config)
412422

413423
Any valid configuration value type. Essentially, the primitive types, an array of those types, or an object comprised of values of those types.
414424

415-
## Class: `EntryHandler`
425+
### Class: `EntryHandler`
416426

417427
Extends: [`EventEmitter`](https://nodejs.org/docs/latest/api/events.html#class-eventemitter)
418428

419429
Created by calling [`scope.handleEntry()`](#scopehandleentry) method.
420430

421-
### Event: `'all'`
431+
#### Event: `'all'`
422432

423433
- `entry` - [`FileEntry`](#interface-fileentry) | [`DirectoryEntry`](#interface-directoryentry) - The entry that was added, changed, or removed.
424434

@@ -450,67 +460,67 @@ async function handleApplication(scope) {
450460
}
451461
```
452462

453-
### Event: `'add'`
463+
#### Event: `'add'`
454464

455465
- `entry` - [`AddFileEvent`](#interface-addfileevent) - The file entry that was added.
456466

457467
The `'add'` event is emitted when a file is created (or the watcher sees it for the first time). The event handler receives an `AddFileEvent` object that contains the file contents, URL path, absolute path, and other metadata.
458468

459-
### Event: `'addDir'`
469+
#### Event: `'addDir'`
460470

461471
- `entry` - [`AddDirectoryEvent`](#interface-adddirectoryevent) - The directory entry that was added.
462472

463473
The `'addDir'` event is emitted when a directory is created (or the watcher sees it for the first time). The event handler receives an `AddDirectoryEvent` object that contains the URL path and absolute path of the directory.
464474

465-
### Event: `'change'`
475+
#### Event: `'change'`
466476

467477
- `entry` - [`ChangeFileEvent`](#interface-changefileevent) - The file entry that was changed.
468478

469479
The `'change'` event is emitted when a file is modified. The event handler receives a `ChangeFileEvent` object that contains the updated file contents, URL path, absolute path, and other metadata.
470480

471-
### Event: `'close'`
481+
#### Event: `'close'`
472482

473483
Emitted when the entry handler is closed via the [`entryHandler.close()`](#entryhandlerclose) method.
474484

475-
### Event: `'error'`
485+
#### Event: `'error'`
476486

477487
- `error` - `unknown` - The error that occurred.
478488

479-
### Event: `'ready'`
489+
#### Event: `'ready'`
480490

481491
Emitted when the entry handler is ready to be used. This is not automatically awaited by the component loader, but also is not required. Calling `scope.handleEntry()` is perfectly sufficient. This is generally useful if you need to do something _after_ the entry handler is absolutely watching and handling entries.
482492

483-
### Event: `'unlink'`
493+
#### Event: `'unlink'`
484494

485495
- `entry` - [`UnlinkFileEvent`](#interface-unlinkfileevent) - The file entry that was deleted.
486496

487497
The `'unlink'` event is emitted when a file is deleted. The event handler receives an `UnlinkFileEvent` object that contains the URL path and absolute path of the deleted file.
488498

489-
### Event: `'unlinkDir'`
499+
#### Event: `'unlinkDir'`
490500

491501
- `entry` - [`UnlinkDirectoryEvent`](#interface-unlinkdirectoryevent) - The directory entry that was deleted.
492502

493503
The `'unlinkDir'` event is emitted when a directory is deleted. The event handler receives an `UnlinkDirectoryEvent` object that contains the URL path and absolute path of the deleted directory.
494504

495-
### `entryHandler.name`
505+
#### `entryHandler.name`
496506

497507
Returns: `string` - The name of the plugin as configured in the `config.yaml` file. This is the key under which the plugin is configured.
498508

499509
The name of the plugin.
500510

501-
### `entryHandler.directory`
511+
#### `entryHandler.directory`
502512

503513
Returns: `string`
504514

505515
The directory of the application. This is the root directory of the component where the `config.yaml` file is located.
506516

507-
### `entryHandler.close()`
517+
#### `entryHandler.close()`
508518

509519
Returns: `this` - The current `EntryHandler` instance.
510520

511521
Closes the entry handler, removing all listeners and preventing any further events from being emitted. The handler can be started again using the [`entryHandler.update()`](#entryhandlerupdateconfig) method.
512522

513-
### `entryHandler.update(config)`
523+
#### `entryHandler.update(config)`
514524

515525
Parameters:
516526

@@ -520,7 +530,7 @@ This method will update an existing entry handler to watch new entries. It will
520530

521531
This method returns a promise associated with the ready event of the updated handler.
522532

523-
### Interface: `BaseEntry`
533+
#### Interface: `BaseEntry`
524534

525535
- `stats` - [`fs.Stats`](https://nodejs.org/docs/latest/api/fs.html#class-fsstats) | `undefined` - The file system stats for the entry.
526536
- `urlPath` - `string` - The recommended URL path of the entry.
@@ -532,7 +542,7 @@ The `urlPath` is resolved based on the configured pattern (`files:` option) comb
532542

533543
The `absolutePath` is the file system path for the entry.
534544

535-
### Interface: `FileEntry`
545+
#### Interface: `FileEntry`
536546

537547
Extends [`BaseEntry`](#interface-baseentry)
538548

@@ -542,7 +552,7 @@ A specific extension of the `BaseEntry` interface representing a file entry. We
542552

543553
There is no `DirectoryEntry` since there is no other important metadata aside from the `BaseEntry` properties. If a user wants the contents of a directory, they should adjust the pattern to resolve files instead.
544554

545-
### Interface: `EntryEvent`
555+
#### Interface: `EntryEvent`
546556

547557
Extends [`BaseEntry`](#interface-baseentry)
548558

@@ -551,7 +561,7 @@ Extends [`BaseEntry`](#interface-baseentry)
551561

552562
A general interface representing the entry handle event objects.
553563

554-
### Interface: `AddFileEvent`
564+
#### Interface: `AddFileEvent`
555565

556566
Extends [`EntryEvent`](#interface-entryevent), [FileEntry](#interface-fileentry)
557567

@@ -560,7 +570,7 @@ Extends [`EntryEvent`](#interface-entryevent), [FileEntry](#interface-fileentry)
560570

561571
Event object emitted when a file is created (or the watcher sees it for the first time).
562572

563-
### Interface: `ChangeFileEvent`
573+
#### Interface: `ChangeFileEvent`
564574

565575
Extends [`EntryEvent`](#interface-entryevent), [FileEntry](#interface-fileentry)
566576

@@ -569,7 +579,7 @@ Extends [`EntryEvent`](#interface-entryevent), [FileEntry](#interface-fileentry)
569579

570580
Event object emitted when a file is modified.
571581

572-
### Interface: `UnlinkFileEvent`
582+
#### Interface: `UnlinkFileEvent`
573583

574584
Extends [`EntryEvent`](#interface-entryevent), [FileEntry](#interface-fileentry)
575585

@@ -578,13 +588,13 @@ Extends [`EntryEvent`](#interface-entryevent), [FileEntry](#interface-fileentry)
578588

579589
Event object emitted when a file is deleted.
580590

581-
### Interface: `FileEntryEvent`
591+
#### Interface: `FileEntryEvent`
582592

583593
- `AddFileEvent` | `ChangeFileEvent` | `UnlinkFileEvent`
584594

585595
A union type representing the file entry events. These events are emitted when a file is created, modified, or deleted. The `FileEntry` interface provides the file contents and other metadata.
586596

587-
### Interface: `AddDirectoryEvent`
597+
#### Interface: `AddDirectoryEvent`
588598

589599
Extends [`EntryEvent`](#interface-entryevent)
590600

@@ -593,7 +603,7 @@ Extends [`EntryEvent`](#interface-entryevent)
593603

594604
Event object emitted when a directory is created (or the watcher sees it for the first time).
595605

596-
### Interface: `UnlinkDirectoryEvent`
606+
#### Interface: `UnlinkDirectoryEvent`
597607

598608
Extends [`EntryEvent`](#interface-entryevent)
599609

@@ -602,13 +612,13 @@ Extends [`EntryEvent`](#interface-entryevent)
602612

603613
Event object emitted when a directory is deleted.
604614

605-
### Interface: `DirectoryEntryEvent`
615+
#### Interface: `DirectoryEntryEvent`
606616

607617
- `AddDirectoryEvent` | `UnlinkDirectoryEvent`
608618

609619
A union type representing the directory entry events. There are no change events for directories since they are not modified in the same way as files.
610620

611-
### Function: `onEntryEventHandler(entryEvent: FileEntryEvent | DirectoryEntryEvent): void`
621+
#### Function: `onEntryEventHandler(entryEvent: FileEntryEvent | DirectoryEntryEvent): void`
612622

613623
Parameters:
614624

0 commit comments

Comments
 (0)