@accelerate/authorization-angular 0.0.0-stage → 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.
@@ -0,0 +1,37 @@
1
+ import { EnvironmentProviders, InjectionToken } from "@angular/core";
2
+ export interface AuthorizationSnapshot {
3
+ userId: string;
4
+ roles: readonly string[];
5
+ permissions: readonly string[];
6
+ }
7
+ export interface AuthorizationConfig {
8
+ endpoint?: string;
9
+ loader?: () => Promise<AuthorizationSnapshot>;
10
+ }
11
+ export type AuthorizationStatus = "idle" | "loading" | "ready" | "error";
12
+ export declare const AUTHORIZATION_CONFIG: InjectionToken<AuthorizationConfig>;
13
+ export declare class AuthorizationService {
14
+ private readonly config;
15
+ private readonly http;
16
+ private readonly state;
17
+ private requestVersion;
18
+ readonly status: import("@angular/core").Signal<AuthorizationStatus>;
19
+ readonly userId: import("@angular/core").Signal<string | null>;
20
+ readonly permissions: import("@angular/core").Signal<readonly string[]>;
21
+ readonly roles: import("@angular/core").Signal<readonly string[]>;
22
+ can(permission: string): import("@angular/core").Signal<boolean>;
23
+ canAny(permissions: readonly string[]): import("@angular/core").Signal<boolean>;
24
+ canAll(permissions: readonly string[]): import("@angular/core").Signal<boolean>;
25
+ refresh(): Promise<void>;
26
+ private loadFromEndpoint;
27
+ reset(): void;
28
+ }
29
+ export declare function provideAuthorization(config: AuthorizationConfig): EnvironmentProviders;
30
+ export declare class AuthCanDirective {
31
+ readonly authCan: import("@angular/core").InputSignal<string>;
32
+ private readonly auth;
33
+ private readonly template;
34
+ private readonly view;
35
+ private visible;
36
+ constructor();
37
+ }
package/dist/index.js ADDED
@@ -0,0 +1,93 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { computed, Directive, effect, inject, Injectable, InjectionToken, input, makeEnvironmentProviders, signal, TemplateRef, ViewContainerRef } from "@angular/core";
8
+ import { HttpClient } from "@angular/common/http";
9
+ import { firstValueFrom } from "rxjs";
10
+ export const AUTHORIZATION_CONFIG = new InjectionToken("ACCELERATE_AUTHORIZATION_CONFIG");
11
+ let AuthorizationService = class AuthorizationService {
12
+ constructor() {
13
+ this.config = inject(AUTHORIZATION_CONFIG);
14
+ this.http = inject(HttpClient, { optional: true });
15
+ this.state = signal({
16
+ status: "idle", userId: null, permissions: [], roles: []
17
+ });
18
+ this.requestVersion = 0;
19
+ this.status = computed(() => this.state().status);
20
+ this.userId = computed(() => this.state().userId);
21
+ this.permissions = computed(() => this.state().permissions);
22
+ this.roles = computed(() => this.state().roles);
23
+ }
24
+ can(permission) { return computed(() => this.status() === "ready" && this.permissions().includes(permission)); }
25
+ canAny(permissions) { return computed(() => this.status() === "ready" && permissions.some(p => this.permissions().includes(p))); }
26
+ canAll(permissions) { return computed(() => this.status() === "ready" && permissions.every(p => this.permissions().includes(p))); }
27
+ async refresh() {
28
+ const version = ++this.requestVersion;
29
+ this.state.set({ status: "loading", userId: null, permissions: [], roles: [] });
30
+ try {
31
+ const snapshot = this.config.loader
32
+ ? await this.config.loader()
33
+ : await this.loadFromEndpoint();
34
+ if (version !== this.requestVersion)
35
+ return;
36
+ if (!snapshot || typeof snapshot.userId !== "string" || !Array.isArray(snapshot.roles) || !Array.isArray(snapshot.permissions)) {
37
+ throw new Error("Invalid authorization snapshot");
38
+ }
39
+ this.state.set({ status: "ready", userId: snapshot.userId, permissions: [...snapshot.permissions], roles: [...snapshot.roles] });
40
+ }
41
+ catch (error) {
42
+ if (version !== this.requestVersion)
43
+ return;
44
+ this.state.set({ status: "error", userId: null, permissions: [], roles: [] });
45
+ throw error;
46
+ }
47
+ }
48
+ loadFromEndpoint() {
49
+ if (!this.http)
50
+ throw new Error("provideHttpClient() is required when using an authorization endpoint");
51
+ return firstValueFrom(this.http.get(this.config.endpoint));
52
+ }
53
+ reset() {
54
+ ++this.requestVersion;
55
+ this.state.set({ status: "idle", userId: null, permissions: [], roles: [] });
56
+ }
57
+ };
58
+ AuthorizationService = __decorate([
59
+ Injectable()
60
+ ], AuthorizationService);
61
+ export { AuthorizationService };
62
+ export function provideAuthorization(config) {
63
+ if (!config.loader && !config.endpoint)
64
+ throw new Error("Authorization requires endpoint or loader");
65
+ return makeEnvironmentProviders([
66
+ { provide: AUTHORIZATION_CONFIG, useValue: config },
67
+ AuthorizationService
68
+ ]);
69
+ }
70
+ let AuthCanDirective = class AuthCanDirective {
71
+ constructor() {
72
+ this.authCan = input.required();
73
+ this.auth = inject(AuthorizationService);
74
+ this.template = inject((TemplateRef));
75
+ this.view = inject(ViewContainerRef);
76
+ this.visible = false;
77
+ effect(() => {
78
+ const allowed = this.auth.status() === "ready" && this.auth.permissions().includes(this.authCan());
79
+ if (allowed && !this.visible) {
80
+ this.view.createEmbeddedView(this.template);
81
+ this.visible = true;
82
+ }
83
+ else if (!allowed && this.visible) {
84
+ this.view.clear();
85
+ this.visible = false;
86
+ }
87
+ });
88
+ }
89
+ };
90
+ AuthCanDirective = __decorate([
91
+ Directive({ selector: "[authCan]", standalone: true })
92
+ ], AuthCanDirective);
93
+ export { AuthCanDirective };
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@accelerate/authorization-angular",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "peerDependencies": {
18
+ "@angular/core": ">=17",
19
+ "@angular/common": ">=17",
20
+ "rxjs": "^7.4.0"
21
+ },
22
+ "scripts": {
23
+ "build": "tsc -p tsconfig.json",
24
+ "typecheck": "tsc -p tsconfig.json --noEmit"
25
+ },
26
+ "devDependencies": {
27
+ "@angular/core": "^19.2.0",
28
+ "@angular/common": "^19.2.0",
29
+ "rxjs": "^7.8.1"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/archubbuck/devkit.git",
34
+ "directory": "packages/authorization-angular"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public",
38
+ "registry": "https://registry.npmjs.org/"
39
+ }
40
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.