@enonic-types/lib-context 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 +81 -0
  2. package/context.d.ts +73 -0
  3. package/package.json +36 -0
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Enonic XP lib-context TS types
2
+
3
+ > TypeScript definitions for `lib-context` library of Enonic XP
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i --save-dev @enonic/lib-context
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
+
17
+ ```json
18
+ {
19
+ "compilerOptions": {
20
+ "types": [
21
+ "@enonic-types/lib-context"
22
+ ]
23
+ }
24
+ }
25
+ ```
26
+
27
+ ### Require and custom imports
28
+
29
+ To make `require` work out of box, you must install and add the `@enonic-types/global` types. Aside from providing definitions for XP global
30
+ objects, e.g. `log`, `app`, `__`, etc, requiring library by the default path will return typed object.
31
+
32
+ `tsconfig.json`
33
+
34
+ ```diff
35
+ {
36
+ "compilerOptions": {
37
+ "types": [
38
+ + "@enonic-types/global"
39
+ "@enonic-types/lib-context"
40
+ ]
41
+ }
42
+ }
43
+ ```
44
+
45
+ `example.ts`
46
+
47
+ ```ts
48
+ const {get, run} = require('/lib/xp/context');
49
+ ```
50
+
51
+ More detailed explanation on how it works and how to type custom import function can be
52
+ found [here](https://github.com/enonic/xp/tree/master/modules/lib/typescript/README.md).
53
+
54
+ ### ES6-style import
55
+
56
+ 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
57
+ types mapping to your configuration.
58
+
59
+ `tsconfig.json`
60
+
61
+ ```diff
62
+ {
63
+ "compilerOptions": {
64
+ "types": [
65
+ "@enonic-types/lib-context"
66
+ ]
67
+ + "baseUrl": "./",
68
+ + "paths": {
69
+ + "/lib/xp/context": ["node_modules/@enonic-types/lib-context"],
70
+ + }
71
+ }
72
+ }
73
+ ```
74
+
75
+ `example.ts`
76
+
77
+ ```ts
78
+ import {get, run} from '/lib/xp/context';
79
+ ```
80
+
81
+ Setting `baseUrl` and `paths` will allow the `tsc` to keep the valid paths in the resulting JavaScript files.
package/context.d.ts ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Built-in context functions.
3
+ *
4
+ * @example
5
+ * var contextLib = require('/lib/xp/context');
6
+ *
7
+ * @module context
8
+ */
9
+ declare global {
10
+ interface XpLibraries {
11
+ '/lib/xp/context': typeof import('./context');
12
+ }
13
+ }
14
+ export interface Principal {
15
+ type: string;
16
+ key: string;
17
+ displayName: string;
18
+ modifiedTime: string;
19
+ }
20
+ export interface User extends Principal {
21
+ disabled?: boolean;
22
+ email?: string;
23
+ login?: string;
24
+ idProvider: string;
25
+ profile?: Record<string, unknown>;
26
+ }
27
+ export interface AuthInfo {
28
+ user?: User | null;
29
+ principals?: string[] | null;
30
+ }
31
+ export declare type ContextAttributes = Record<string, number | string | boolean | Record<string, unknown>>;
32
+ export interface Context {
33
+ branch: string;
34
+ repository: string;
35
+ authInfo?: AuthInfo;
36
+ attributes: ContextAttributes;
37
+ }
38
+ export interface ContextUserParams {
39
+ login: string;
40
+ idProvider?: string;
41
+ }
42
+ export interface ContextParams {
43
+ repository?: string;
44
+ branch?: string;
45
+ user?: ContextUserParams;
46
+ principals?: string[];
47
+ attributes?: ContextAttributes;
48
+ }
49
+ /**
50
+ * Runs a function within a specified context.
51
+ *
52
+ * @example-ref examples/context/run.js
53
+ *
54
+ * @param {object} context JSON parameters.
55
+ * @param {string} [context.repository] Repository to execute the callback in. Default is the current repository set in portal.
56
+ * @param {string} [context.branch] Name of the branch to execute the callback in. Default is the current branch set in portal.
57
+ * @param {object} [context.user] User to execute the callback with. Default is the current user.
58
+ * @param {string} context.user.login Login of the user.
59
+ * @param {string} [context.user.idProvider] Id provider containing the user. By default, all the id providers will be used.
60
+ * @param {array} [context.principals] Additional principals to execute the callback with.
61
+ * @param {object} [context.attributes] Additional Context attributes.
62
+ * @param {function} callback Function to execute.
63
+ * @returns {object} Result of the function execution.
64
+ */
65
+ export declare function run<T>(context: ContextParams, callback: () => T): T;
66
+ /**
67
+ * Returns the current context.
68
+ *
69
+ * @example-ref examples/context/get.js
70
+ *
71
+ * @returns {object} Return the current context as JSON object.
72
+ */
73
+ export declare function get(): Context;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@enonic-types/lib-context",
3
+ "version": "0.1.0",
4
+ "description": "Type definitions for lib-context.",
5
+ "types": "context.d.ts",
6
+ "files": [
7
+ "context.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-context",
21
+ "context",
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
+ }