@enonic-types/global 0.1.0 → 7.11.0-B1

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/README.md CHANGED
@@ -23,7 +23,7 @@ Add the corresponding types to your `tsconfig.json` file that is used for applic
23
23
  }
24
24
  ```
25
25
 
26
- > The paths to the `node_mosules` assume that your `tsconfig.json` is placed under your project root.
26
+ > The paths to the `node_modules` assume that your `tsconfig.json` is placed under your project root.
27
27
 
28
28
  After that, all the global XP variables will be typed.
29
29
 
@@ -36,7 +36,8 @@ libraries are also added to your `tsconfig.json`.
36
36
 
37
37
  ### Require
38
38
 
39
- To add support for type resolution for the custom libraries via `require`, you can redeclare the `XpLibraries` interface in global scope, wich will lead to declaration merging:
39
+ To add support for type resolution for the custom libraries via `require`, you can redeclare the `XpLibraries` interface in global scope,
40
+ which will lead to declaration merging:
40
41
 
41
42
  ```ts
42
43
  declare global {
@@ -46,9 +47,10 @@ declare global {
46
47
  }
47
48
  ```
48
49
 
49
- __Other imports__
50
+ #### Other imports
50
51
 
51
52
  If you want to use custom import functions, like `__non_webpack_require__` with Webpack, just use global `XpRequire` type for this:
53
+
52
54
  ```ts
53
55
  declare const __non_webpack_require__: XpRequire;
54
56
  ```
@@ -63,7 +65,7 @@ interface SomeHelper {
63
65
  }
64
66
  ```
65
67
 
66
- __Option 1__
68
+ #### Option 1
67
69
 
68
70
  You can pass the type argument explicitly. This option is a bit cleaner.
69
71
 
@@ -71,7 +73,7 @@ You can pass the type argument explicitly. This option is a bit cleaner.
71
73
  const helper = __.newBean<SomeHelper>('com.me.project.SomeHelper');
72
74
  ```
73
75
 
74
- __Option 2__
76
+ #### Option 2
75
77
 
76
78
  Or you can map the bean name to bean interface. It may be a preferable way to do it, if the bean is used across multiple files:
77
79
 
package/global.d.ts CHANGED
@@ -1,18 +1,203 @@
1
+ interface XpLibraries {}
2
+
3
+ interface XpBeans {}
4
+
5
+ /**
6
+ * The globally available app object holds information about the contextual application.
7
+ * @example
8
+ * var nameVersion = app.name + ' v' + app.version;
9
+ *
10
+ * @global
11
+ * @namespace
12
+ */
13
+ declare const app: {
14
+ /**
15
+ * The name of the application.
16
+ *
17
+ * @type string
18
+ */
19
+ name: string;
20
+ /**
21
+ * Version of the application.
22
+ *
23
+ * @type string
24
+ */
25
+ version: string;
26
+ /**
27
+ * Values from the application’s configuration file.
28
+ * This can be set using $XP_HOME/config/<app.name>.cfg.
29
+ * Every time the configuration is changed the app is restarted.
30
+ *
31
+ * @type Object
32
+ */
33
+ config: Record<string, string | undefined>;
34
+ };
35
+
1
36
  /**
2
- * This project don't include XP libraries definitions and need this interface
3
- * to properly map imports via require.
37
+ * Logging functions.
4
38
  *
5
- * Warning: this interface is empty. If you want type definitions for various
6
- * libraries, you need to redefine this interface in your library in global
7
- * path and add a new property with library path.
39
+ * @example
40
+ * // Log with simple message
41
+ * log.debug('My log message');
8
42
  *
9
43
  * @example
10
- * declare global {
11
- * interface XpLibraries {
12
- * '/lib/xp/event': typeof import('./event');
13
- * }
14
- * }
15
- */
16
- interface XpLibraries {}
44
+ * // Log with placeholders
45
+ * log.info('My %s message with %s', 'log', 'placeholders');
46
+ *
47
+ * @example
48
+ * // Log a JSON object
49
+ * log.warning('My JSON: %s', {a: 1});
50
+ *
51
+ * @example
52
+ * // Log JSON object using string
53
+ * log.error('My JSON: %s', JSON.stringify({a: 1}, null, 2));
54
+ *
55
+ * @global
56
+ * @namespace
57
+ */
58
+ declare const log: {
59
+ /**
60
+ * Log debug message.
61
+ *
62
+ * @param {Array} args... logging arguments.
63
+ */
64
+ debug: (...args: unknown[]) => void;
17
65
 
18
- interface XpBeans {}
66
+ /**
67
+ * Log info message.
68
+ *
69
+ * @param {Array} args... logging arguments.
70
+ */
71
+ info: (...args: unknown[]) => void;
72
+
73
+ /**
74
+ * Log warning message.
75
+ *
76
+ * @param {Array} args... logging arguments.
77
+ */
78
+ warning: (...args: unknown[]) => void;
79
+
80
+ /**
81
+ * Log error message.
82
+ *
83
+ * @param {Array} args... logging arguments.
84
+ */
85
+ error: (...args: unknown[]) => void;
86
+ };
87
+
88
+ declare interface ScriptValue {
89
+ isArray(): boolean;
90
+
91
+ isObject(): boolean;
92
+
93
+ isValue(): boolean;
94
+
95
+ isFunction(): boolean;
96
+
97
+ getValue(): unknown;
98
+
99
+ getKeys(): string[];
100
+
101
+ hasMember(key: string): boolean;
102
+
103
+ getMember(key: string): ScriptValue;
104
+
105
+ getArray(): ScriptValue[];
106
+
107
+ getMap(): Record<string, unknown>;
108
+
109
+ getList(): object[];
110
+ }
111
+
112
+ type NewBean = <T = unknown, Bean extends keyof XpBeans | string = string>(bean: Bean) =>
113
+ Bean extends keyof XpBeans ? XpBeans[Bean] : T;
114
+
115
+ /**
116
+ * Javascript to Java bridge functions.
117
+ *
118
+ * @example
119
+ * var bean = __.newBean('com.enonic.xp.MyJavaUtils');
120
+ *
121
+ * @example
122
+ * return __.toNativeObject(bean.findArray(arrayName));
123
+ *
124
+ * @global
125
+ * @namespace
126
+ */
127
+ declare const __: {
128
+ /**
129
+ * Creates a new JavaScript bean that wraps the given Java class and makes its methods available to be called from JavaScript.
130
+ */
131
+ newBean: NewBean;
132
+ /**
133
+ * Converts arrays or complex Java objects to JSON.
134
+ * @param value Value to convert
135
+ */
136
+ toNativeObject: <T = unknown>(value: T) => T;
137
+ /**
138
+ * Converts JSON to a Java Map structure that can be used as parameters to a Java method on a bean created with newBean.
139
+ * @param value Value to convert
140
+ */
141
+ toScriptValue: <T = object>(value: T) => ScriptValue;
142
+ /**
143
+ * Add a disposer that is called when the app is stopped.
144
+ * @param callback Function to call
145
+ */
146
+ disposer: (callback: (...args: unknown[]) => unknown) => void;
147
+ /**
148
+ * Converts a JavaScript variable that is undefined to a Java <code>null</code> object.
149
+ * If the JavaScript variable is defined, it is returned as is.
150
+ * @param value Value to convert
151
+ */
152
+ nullOrValue: <T = object>(value: T) => T | null | undefined;
153
+
154
+ /**
155
+ * Doc registerMock.
156
+ *
157
+ * @param name Name of mock.
158
+ * @param value Value to register.
159
+ */
160
+ registerMock: (name: string, value: object) => void
161
+ };
162
+
163
+ declare type XpRequire = <Key extends keyof XpLibraries | string = string>(path: Key) =>
164
+ Key extends keyof XpLibraries ? XpLibraries[Key] : unknown;
165
+
166
+ /**
167
+ * This globally available function will load a JavaScript file and return the exports as objects.
168
+ * The function implements parts of the `CommonJS Modules Specification`.
169
+ *
170
+ * @example
171
+ * // Require relative to this
172
+ * var other = require('./other.js');
173
+ *
174
+ * @example
175
+ * // Require absolute
176
+ * var other = require('/path/to/other.js');
177
+ *
178
+ * @example
179
+ * // Require without .js extension
180
+ * var other = require('./other');
181
+ *
182
+ * @param {string} path Path for javascript file (relative or absolute and .js ending is optional).
183
+ * @returns {object} Exports from loaded javascript.
184
+ * @global
185
+ */
186
+ declare const require: XpRequire;
187
+
188
+ /**
189
+ * Resolves a path to another file. Can use relative or absolute path.
190
+ *
191
+ * @example
192
+ * // Resolve relative to this
193
+ * var path = resolve('./other.html');
194
+ *
195
+ * @example
196
+ * // Reslove absolute
197
+ * var path = resolve('/path/to/other.html');
198
+ *
199
+ * @param {string} path Path to resolve.
200
+ * @returns {*} Reference to an object.
201
+ * @global
202
+ */
203
+ declare const resolve: (path: string) => object;
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "@enonic-types/global",
3
- "version": "0.1.0",
4
- "description": "Type definitions for XP JavaScript global object properties.",
5
- "types": "index.d.ts",
3
+ "version": "7.11.0-B1",
4
+ "description": "Type definitions for global.",
5
+ "types": "global.d.ts",
6
6
  "files": [
7
- "index.d.ts",
8
7
  "global.d.ts"
9
8
  ],
10
9
  "repository": {
@@ -15,6 +14,7 @@
15
14
  "enonic",
16
15
  "enonic-xp",
17
16
  "global",
17
+ "common",
18
18
  "types",
19
19
  "typescript"
20
20
  ],
package/index.d.ts DELETED
@@ -1,54 +0,0 @@
1
- /// <reference path='global.d.ts' />
2
-
3
- declare const app: {
4
- name: string;
5
- version: string;
6
- config: Record<string, string | undefined>;
7
- };
8
-
9
- declare const log: {
10
- debug: (...args: unknown[]) => void;
11
- info: (...args: unknown[]) => void;
12
- warning: (...args: unknown[]) => void;
13
- error: (...args: unknown[]) => void;
14
- };
15
-
16
- declare interface ScriptValue {
17
- isArray(): boolean;
18
-
19
- isObject(): boolean;
20
-
21
- isValue(): boolean;
22
-
23
- isFunction(): boolean;
24
-
25
- getValue(): unknown;
26
-
27
- getKeys(): string[];
28
-
29
- hasMember(key: string): boolean;
30
-
31
- getMember(key: string): ScriptValue;
32
-
33
- getArray(): ScriptValue[];
34
-
35
- getMap(): Record<String, unknown>;
36
-
37
- getList(): object[];
38
- }
39
-
40
- type NewBean = <T = unknown, Bean extends keyof XpBeans | string = string>(bean: Bean) =>
41
- Bean extends keyof XpBeans ? XpBeans[Bean] : T;
42
-
43
- declare const __: {
44
- newBean: NewBean;
45
- toNativeObject: <T = unknown>(beanResult: T) => T;
46
- toScriptValue: <T = object>(value: T) => ScriptValue;
47
- disposer: (callback: (...args: unknown[]) => unknown) => void;
48
- nullOrValue: <T = object>(value: T) => T | null | undefined;
49
- };
50
-
51
- declare type XpRequire = <Key extends keyof XpLibraries | string = string>(path: Key) =>
52
- Key extends keyof XpLibraries ? XpLibraries[Key] : unknown;
53
-
54
- declare const require: XpRequire;