@enonic-types/lib-event 0.1.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.
Files changed (3) hide show
  1. package/README.md +73 -0
  2. package/event.d.ts +84 -0
  3. package/package.json +36 -0
package/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Enonic XP lib-event TS types
2
+
3
+ > TypeScript definitions for `lib-event` library of Enonic XP
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i --save-dev @enonic/lib-event
9
+ ```
10
+
11
+ ## Use
12
+
13
+ Add the corresponding types to your `tsconfig.json` file that is used for application's server-side TypeScript code.
14
+
15
+ `tsconfig.json`
16
+ ```json
17
+ {
18
+ "compilerOptions": {
19
+ "types": [
20
+ "@enonic-types/lib-event"
21
+ ]
22
+ }
23
+ }
24
+ ```
25
+
26
+ ### Require and custom imports
27
+
28
+ To make `require` work out of box, you must install and add the `@enonic-types/global` types. Aside from providing definitions for XP global objects, e.g. `log`, `app`, `__`, etc, requiring library by the default path will return typed object.
29
+
30
+ `tsconfig.json`
31
+ ```diff
32
+ {
33
+ "compilerOptions": {
34
+ "types": [
35
+ + "@enonic-types/global"
36
+ "@enonic-types/lib-event"
37
+ ]
38
+ }
39
+ }
40
+ ```
41
+
42
+ `example.ts`
43
+ ```ts
44
+ const {listener} = require('/lib/xp/event');
45
+ ```
46
+
47
+ More detailed explanation on how it works and how to type custom import function can be found [here](https://github.com/enonic/xp/tree/master/modules/lib/typescript/README.md).
48
+
49
+ ### ES6-style import
50
+
51
+ If you are planning to use `import` in your code and transpile it with the default `tsc` TypeScript compiler, you'll need to add proper types mapping to your configuration.
52
+
53
+ `tsconfig.json`
54
+ ```diff
55
+ {
56
+ "compilerOptions": {
57
+ "types": [
58
+ "@enonic-types/lib-event"
59
+ ]
60
+ + "baseUrl": "./",
61
+ + "paths": {
62
+ + "/lib/xp/event": ["node_modules/@enonic-types/lib-event"],
63
+ + }
64
+ }
65
+ }
66
+ ```
67
+
68
+ `example.ts`
69
+ ```ts
70
+ import {listener, EnonicEvent} from '/lib/xp/event';
71
+ ```
72
+
73
+ Setting `baseUrl` and `paths` will allow the `tsc` to keep the valid paths in the resulting JavaScript files.
package/event.d.ts ADDED
@@ -0,0 +1,84 @@
1
+ declare global {
2
+ interface XpLibraries {
3
+ '/lib/xp/event': typeof import('./event');
4
+ }
5
+ }
6
+ export interface ListenerParams<EventData extends object = EnonicEventData> {
7
+ /**
8
+ * Event type pattern
9
+ */
10
+ type: EnonicEventTypes;
11
+ /**
12
+ * Callback event listener
13
+ */
14
+ callback: (event: EnonicEvent<EventData>) => void;
15
+ /**
16
+ * Local events only (default to false)
17
+ */
18
+ localOnly?: boolean;
19
+ }
20
+ export interface SendParams {
21
+ /**
22
+ * Event type
23
+ */
24
+ type: EnonicEventTypes;
25
+ /**
26
+ * `true` if it should be distributed in cluster
27
+ */
28
+ distributed?: boolean;
29
+ /**
30
+ * Additional data for event.
31
+ */
32
+ data?: object;
33
+ }
34
+ export interface EnonicEvent<EventData extends object = EnonicEventData> {
35
+ readonly type: EnonicEventTypes;
36
+ readonly timestamp: number;
37
+ readonly localOrigin: boolean;
38
+ readonly distributed: boolean;
39
+ readonly data: EventData;
40
+ }
41
+ export interface EnonicEventData {
42
+ readonly nodes: ReadonlyArray<EnonicEventDataNode>;
43
+ readonly state?: string;
44
+ }
45
+ export interface EnonicEventDataNode {
46
+ readonly id: string;
47
+ readonly path: string;
48
+ readonly branch: string;
49
+ readonly repo: string;
50
+ readonly newPath?: string;
51
+ }
52
+ declare type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);
53
+ export declare type EnonicEventTypes = LiteralUnion<string>;
54
+ /**
55
+ * Event functions.
56
+ *
57
+ * @example
58
+ * var eventLib = require('/lib/xp/event');
59
+ *
60
+ * @module event
61
+ */
62
+ /**
63
+ * This function adds a event listener.
64
+ *
65
+ * @example-ref examples/event/listener.js
66
+ *
67
+ * @param {object} params Listener parameters.
68
+ * @param {string} params.type Event type pattern.
69
+ * @param {function} params.callback Callback event listener.
70
+ * @param {boolean} params.localOnly Local events only (default to false).
71
+ */
72
+ export declare function listener<EventData extends object = EnonicEventData>(params: ListenerParams<EventData>): void;
73
+ /**
74
+ * This function sends a custom event. All custom events are prefixed 'custom.'.
75
+ *
76
+ * @example-ref examples/event/send.js
77
+ *
78
+ * @param {object} event Event to send.
79
+ * @param {string} event.type Event type.
80
+ * @param {boolean} event.distributed True if it should be distributed in cluster.
81
+ * @param {object} event.data Additional data for event.
82
+ */
83
+ export declare function send(event: SendParams): void;
84
+ export {};
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@enonic-types/lib-event",
3
+ "version": "0.1.0",
4
+ "description": "Type definitions for lib-event.",
5
+ "types": "event.d.ts",
6
+ "files": [
7
+ "event.d.ts"
8
+ ],
9
+ "scripts": {
10
+ "pre": "npm run --prefix ../ pre",
11
+ "packlist": "npx npm-packlist"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/enonic/xp.git#master"
16
+ },
17
+ "keywords": [
18
+ "enonic",
19
+ "enonic-xp",
20
+ "lib-event",
21
+ "event",
22
+ "types",
23
+ "typescript"
24
+ ],
25
+ "contributors": [
26
+ "Mikita Taukachou <edloidas@gmail.com> (https://edloidas.github.io/)"
27
+ ],
28
+ "license": "Apache-2.0",
29
+ "bugs": {
30
+ "url": "https://github.com/enonic/xp/issues"
31
+ },
32
+ "homepage": "https://github.com/enonic/xp/tree/master#readme",
33
+ "publishConfig": {
34
+ "access": "public"
35
+ }
36
+ }