@intellegens/cornerstone-client 0.0.9999-alpha-7 → 0.0.9999-alpha-8

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
@@ -7,19 +7,19 @@ This project includes services for easy communication from a Cornerstone client
7
7
  1. Install dependencies:
8
8
 
9
9
  ```bash
10
- npm install
10
+ pnpm install
11
11
  ```
12
12
 
13
13
  2. Build the project:
14
14
 
15
15
  ```bash
16
- npm run build
16
+ pnpm run build
17
17
  ```
18
18
 
19
19
  3. Run Client demo HTTP server:
20
20
 
21
21
  ```bash
22
- npm run demo
22
+ pnpm run demo
23
23
  ```
24
24
 
25
25
  4. Open
@@ -1 +1,2 @@
1
1
  export * from './dto';
2
+ export * from './policy';
@@ -1 +1,2 @@
1
1
  export * from './dto';
2
+ export * from './policy';
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Interface for policies that can be checked.
3
+ *
4
+ * @interface IPolicy<TParams extends unknown[]>
5
+ * @template TParams - The type of the parameters that will be passed to the policy check function.
6
+ */
7
+ export interface IPolicy<TParams extends unknown[]> {
8
+ /**
9
+ * Checks if the policy is valid for the given parameters.
10
+ *
11
+ * @param params - Parameters to pass to the policy check function.
12
+ * @return A promise that resolves if the policy is valid, rejects otherwise.
13
+ */
14
+ check(...params: TParams): Promise<boolean>;
15
+ }
16
+ /**
17
+ * Base class for policies. It implements the IPolicy interface.
18
+ *
19
+ * @class PolicyBase
20
+ * @template TParams - The type of the parameters that will be passed to the policy check function.
21
+ */
22
+ export declare class PolicyBase<TParams extends unknown[]> implements IPolicy<TParams> {
23
+ /**
24
+ * Checks if the policy is valid for the given parameters.
25
+ *
26
+ * @return A promise that resolves if the policy is valid, rejects otherwise.
27
+ * @throws {Error} Error if not implemented.
28
+ */
29
+ check(): Promise<boolean>;
30
+ }
31
+ /**
32
+ * Class for policies that can be checked.
33
+ *
34
+ * @class Policy
35
+ * @template TParams - The type of the parameters that will be passed to the policy check function.
36
+ */
37
+ export declare class Policy<TParams extends unknown[]> extends PolicyBase<TParams> {
38
+ private readonly _verificationFn;
39
+ /**
40
+ * Creates a new policy.
41
+ *
42
+ * @param verificationFn - The function that will be used to verify the policy.
43
+ */
44
+ constructor(verificationFn: (...params: TParams) => Promise<boolean>);
45
+ /**
46
+ * Checks if the policy is valid for the given parameters.
47
+ *
48
+ * @param params - Parameters to pass to the policy check function.
49
+ * @return A promise that resolves if the policy is valid, rejects otherwise.
50
+ */
51
+ check(...params: TParams): Promise<boolean>;
52
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Base class for policies. It implements the IPolicy interface.
3
+ *
4
+ * @class PolicyBase
5
+ * @template TParams - The type of the parameters that will be passed to the policy check function.
6
+ */
7
+ export class PolicyBase {
8
+ /**
9
+ * Checks if the policy is valid for the given parameters.
10
+ *
11
+ * @return A promise that resolves if the policy is valid, rejects otherwise.
12
+ * @throws {Error} Error if not implemented.
13
+ */
14
+ async check() {
15
+ throw new Error('Not implemented');
16
+ }
17
+ }
18
+ /**
19
+ * Class for policies that can be checked.
20
+ *
21
+ * @class Policy
22
+ * @template TParams - The type of the parameters that will be passed to the policy check function.
23
+ */
24
+ export class Policy extends PolicyBase {
25
+ _verificationFn;
26
+ /**
27
+ * Creates a new policy.
28
+ *
29
+ * @param verificationFn - The function that will be used to verify the policy.
30
+ */
31
+ constructor(verificationFn) {
32
+ super();
33
+ this._verificationFn = verificationFn;
34
+ }
35
+ /**
36
+ * Checks if the policy is valid for the given parameters.
37
+ *
38
+ * @param params - Parameters to pass to the policy check function.
39
+ * @return A promise that resolves if the policy is valid, rejects otherwise.
40
+ */
41
+ async check(...params) {
42
+ return this._verificationFn(...params);
43
+ }
44
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@intellegens/cornerstone-client",
3
- "version": "0.0.9999-alpha-7",
3
+ "version": "0.0.9999-alpha-8",
4
4
  "private": false,
5
5
  "publishable": true,
6
- "main": "./index.js",
7
- "types": "./index.d.ts",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
8
  "type": "module",
9
9
  "author": "Intellegens",
10
10
  "license": "MIT",
@@ -14,32 +14,32 @@
14
14
  "cornerstone"
15
15
  ],
16
16
  "scripts": {
17
- "clean": "npx --yes rimraf '{dist}'",
17
+ "clean": "pnpx rimraf '{dist}'",
18
18
  "test": "jest",
19
19
  "coverage": "jest --coverage",
20
- "build": "npm run clean && tsc && tsc-alias",
21
- "start": "npm run build && npx vite build && npx tsx ./demo"
20
+ "build": "pnpm run clean && tsc && tsc-alias",
21
+ "start": "pnpm run build && pnpx vite build && pnpx tsx ./demo"
22
22
  },
23
23
  "devDependencies": {
24
- "@eslint/js": "^9.21.0",
25
- "@types/express": "^5.0.0",
26
- "@types/jest": "^29.5.12",
27
- "@types/node": "^22.13.9",
28
- "eslint": "^9.21.0",
29
- "eslint-config-prettier": "^10.0.1",
30
- "express": "^4.21.2",
31
- "globals": "^16.0.0",
32
- "jest": "^29.7.0",
33
- "jest-environment-jsdom": "^29.7.0",
34
- "prettier": "^3.5.2",
35
- "ts-jest": "^29.1.2",
36
- "tsc-alias": "^1.8.11",
37
- "typescript-eslint": "^8.26.0",
38
- "vite": "6.3.4",
39
- "zod": "^3.24.2"
24
+ "@eslint/js": "catalog:",
25
+ "@types/express": "catalog:",
26
+ "@types/jest": "catalog:",
27
+ "@types/node": "catalog:",
28
+ "eslint": "catalog:",
29
+ "eslint-config-prettier": "catalog:",
30
+ "express": "catalog:",
31
+ "globals": "catalog:",
32
+ "jest": "catalog:",
33
+ "jest-environment-jsdom": "catalog:",
34
+ "prettier": "catalog:",
35
+ "ts-jest": "catalog:",
36
+ "tsc-alias": "catalog:",
37
+ "typescript-eslint": "catalog:",
38
+ "vite": "catalog:",
39
+ "zod": "catalog:"
40
40
  },
41
41
  "dependencies": {
42
42
  "http-proxy-middleware": "^3.0.3",
43
- "typescript": "^5.4.5"
43
+ "typescript": "catalog:"
44
44
  }
45
45
  }
@@ -60,4 +60,8 @@ export declare class AuthService<TKey, TUser extends UserDto<TKey> = UserDto<TKe
60
60
  * to update local authentication state to unauthenticated
61
61
  */
62
62
  handleNoAuthApiResponse(): void;
63
+ /**
64
+ * True if a user is currently loaded (post whoAmI/signIn)
65
+ */
66
+ isAuthenticated(): boolean;
63
67
  }
@@ -150,4 +150,11 @@ export class AuthService {
150
150
  handleNoAuthApiResponse() {
151
151
  this.user = undefined;
152
152
  }
153
+ // #region Client-side authorization helpers
154
+ /**
155
+ * True if a user is currently loaded (post whoAmI/signIn)
156
+ */
157
+ isAuthenticated() {
158
+ return !!this.user;
159
+ }
153
160
  }
@@ -0,0 +1,17 @@
1
+ import { IPolicy } from '../../data';
2
+ /**
3
+ * Checks if a policy is valid for the given parameters.
4
+ *
5
+ * @param policy Policy to check, either an instance or a class.
6
+ * @param params Parameters to pass to the policy check function.
7
+ * @returns A promise that resolves if the policy is valid, rejects otherwise.
8
+ */
9
+ export declare function check<TParams extends unknown[]>(policy: IPolicy<TParams> | (new () => IPolicy<TParams>), ...params: TParams): Promise<boolean>;
10
+ /**
11
+ * Composes multiple policies into a single policy.
12
+ *
13
+ * @param composition Composition method, either 'AND' or 'OR'.
14
+ * @param policies Policies to compose.
15
+ * @returns A new policy that represents the composition of the policies.
16
+ */
17
+ export declare function compose<TParams extends unknown[]>(composition: 'AND' | 'OR', ...policies: IPolicy<TParams>[]): IPolicy<TParams>;
@@ -0,0 +1,45 @@
1
+ import { PolicyBase } from '../../data';
2
+ /**
3
+ * Checks if a policy is valid for the given parameters.
4
+ *
5
+ * @param policy Policy to check, either an instance or a class.
6
+ * @param params Parameters to pass to the policy check function.
7
+ * @returns A promise that resolves if the policy is valid, rejects otherwise.
8
+ */
9
+ export async function check(policy, ...params) {
10
+ // If passed an instance of a policy
11
+ if (policy instanceof PolicyBase) {
12
+ return policy.check(...params);
13
+ }
14
+ // If passed a policy class
15
+ else {
16
+ return new policy().check(...params);
17
+ }
18
+ }
19
+ /**
20
+ * Composes multiple policies into a single policy.
21
+ *
22
+ * @param composition Composition method, either 'AND' or 'OR'.
23
+ * @param policies Policies to compose.
24
+ * @returns A new policy that represents the composition of the policies.
25
+ */
26
+ export function compose(composition, ...policies) {
27
+ return {
28
+ check: async (...params) => {
29
+ // Check all policies
30
+ if (composition === 'AND') {
31
+ const results = await Promise.allSettled(policies.map(policy => policy.check(...params)));
32
+ return results.every(result => result);
33
+ }
34
+ // Check any policies, exit as soon as any are true
35
+ else if (composition === 'OR') {
36
+ const results = await Promise.allSettled(policies.map(policy => policy.check(...params)));
37
+ return results.some(result => result);
38
+ }
39
+ // Invalid composition
40
+ else {
41
+ throw new Error('Invalid composition');
42
+ }
43
+ },
44
+ };
45
+ }