@enonic-types/global 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.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Enonic XP global TS types
2
+
3
+ > TypeScript definitions for global variables of Enonic XP
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i --save-dev @enonic-types/global
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
+ ```json
16
+ {
17
+ "compilerOptions": {
18
+ "typeRoots": [
19
+ "node_modules/@types",
20
+ "node_modules/@enonic-types",
21
+ ]
22
+ }
23
+ }
24
+ ```
25
+
26
+ > The paths to the `node_mosules` assume that your `tsconfig.json` is placed under your project root.
27
+
28
+ After that, all the global XP variables will be typed.
29
+
30
+ Adding not `"types": [...]` but `"typeRoots"` will allow to discover types for every other enonic library, that will be installed later.
31
+
32
+ Import functions, such as `require`, will return typed objects if the corresponding types for imported
33
+ libraries are also added to your `tsconfig.json`.
34
+
35
+ ## Configuration
36
+
37
+ ### Require
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:
40
+
41
+ ```ts
42
+ declare global {
43
+ interface XpLibraries {
44
+ '/lib/custom/mylib': typeof import('./mylib');
45
+ }
46
+ }
47
+ ```
48
+
49
+ __Other imports__
50
+
51
+ If you want to use custom import functions, like `__non_webpack_require__` with Webpack, just use global `XpRequire` type for this:
52
+ ```ts
53
+ declare const __non_webpack_require__: XpRequire;
54
+ ```
55
+
56
+ ### Beans
57
+
58
+ To create a new bean, a `__.newBean()` function must be used. Making it return a proper type can be done in two ways. Say you have create an interface for that been somewhere in your project:
59
+
60
+ ```ts
61
+ interface SomeHelper {
62
+ help(text: string): void;
63
+ }
64
+ ```
65
+
66
+ __Option 1__
67
+
68
+ You can pass the type argument explicitly. This option is a bit cleaner.
69
+
70
+ ```ts
71
+ const helper = __.newBean<SomeHelper>('com.me.project.SomeHelper');
72
+ ```
73
+
74
+ __Option 2__
75
+
76
+ 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
+
78
+ ```ts
79
+ declare global {
80
+ interface XpBeans {
81
+ 'com.me.project.SomeHelper': SomeHelper;
82
+ }
83
+ }
84
+
85
+ const helper = __.newBean('com.me.project.SomeHelper');
86
+ ```
package/global.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * This project don't include XP libraries definitions and need this interface
3
+ * to properly map imports via require.
4
+ *
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.
8
+ *
9
+ * @example
10
+ * declare global {
11
+ * interface XpLibraries {
12
+ * '/lib/xp/event': typeof import('./event');
13
+ * }
14
+ * }
15
+ */
16
+ interface XpLibraries {}
17
+
18
+ interface XpBeans {}
package/index.d.ts ADDED
@@ -0,0 +1,54 @@
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;
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
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",
6
+ "files": [
7
+ "index.d.ts",
8
+ "global.d.ts"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/enonic/xp.git#master"
13
+ },
14
+ "keywords": [
15
+ "enonic",
16
+ "enonic-xp",
17
+ "global",
18
+ "types",
19
+ "typescript"
20
+ ],
21
+ "contributors": [
22
+ "Mikita Taukachou <edloidas@gmail.com> (https://edloidas.github.io/)"
23
+ ],
24
+ "license": "Apache-2.0",
25
+ "bugs": {
26
+ "url": "https://github.com/enonic/xp/issues"
27
+ },
28
+ "homepage": "https://github.com/enonic/xp/tree/master#readme",
29
+ "publishConfig": {
30
+ "access": "public"
31
+ }
32
+ }