@yaagoub/federation-tools 0.0.1

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,63 @@
1
+ # FederationTools
2
+
3
+ This project was generated using [Angular CLI](https://github.com/angular/angular-cli) version 20.3.0.
4
+
5
+ ## Code scaffolding
6
+
7
+ Angular CLI includes powerful code scaffolding tools. To generate a new component, run:
8
+
9
+ ```bash
10
+ ng generate component component-name
11
+ ```
12
+
13
+ For a complete list of available schematics (such as `components`, `directives`, or `pipes`), run:
14
+
15
+ ```bash
16
+ ng generate --help
17
+ ```
18
+
19
+ ## Building
20
+
21
+ To build the library, run:
22
+
23
+ ```bash
24
+ ng build federation-tools
25
+ ```
26
+
27
+ This command will compile your project, and the build artifacts will be placed in the `dist/` directory.
28
+
29
+ ### Publishing the Library
30
+
31
+ Once the project is built, you can publish your library by following these steps:
32
+
33
+ 1. Navigate to the `dist` directory:
34
+ ```bash
35
+ cd dist/federation-tools
36
+ ```
37
+
38
+ 2. Run the `npm publish` command to publish your library to the npm registry:
39
+ ```bash
40
+ npm publish
41
+ ```
42
+
43
+ ## Running unit tests
44
+
45
+ To execute unit tests with the [Karma](https://karma-runner.github.io) test runner, use the following command:
46
+
47
+ ```bash
48
+ ng test
49
+ ```
50
+
51
+ ## Running end-to-end tests
52
+
53
+ For end-to-end (e2e) testing, run:
54
+
55
+ ```bash
56
+ ng e2e
57
+ ```
58
+
59
+ Angular CLI does not come with an end-to-end testing framework by default. You can choose one that suits your needs.
60
+
61
+ ## Additional Resources
62
+
63
+ For more information on using the Angular CLI, including detailed command references, visit the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
@@ -0,0 +1,84 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, InjectionToken, inject, makeEnvironmentProviders, provideAppInitializer } from '@angular/core';
3
+ import { HttpClient } from '@angular/common/http';
4
+ import { firstValueFrom } from 'rxjs';
5
+
6
+ class Federation {
7
+ getShellOrigin() {
8
+ return window.location.origin;
9
+ }
10
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: Federation, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
11
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: Federation, providedIn: 'root' });
12
+ }
13
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: Federation, decorators: [{
14
+ type: Injectable,
15
+ args: [{
16
+ providedIn: 'root'
17
+ }]
18
+ }] });
19
+
20
+ /**
21
+ * Holds all loaded env objects:
22
+ * - key: remoteName (from env-manifest.json) + 'shell'
23
+ * - value: parsed env.json for that remote
24
+ */
25
+ const APP_CONFIG_ENV = new InjectionToken('env-settings');
26
+ class Env {
27
+ http = inject(HttpClient);
28
+ federation = inject(Federation);
29
+ envMap = inject(APP_CONFIG_ENV);
30
+ getManifestUrl() {
31
+ return this.federation.getShellOrigin() + '/env-manifest.json';
32
+ }
33
+ getShellEnvUrl() {
34
+ return this.federation.getShellOrigin() + '/env.json';
35
+ }
36
+ getRemoteEnvUrl(origin) {
37
+ return origin.replace(/\/+$/, '') + '/env.json';
38
+ }
39
+ async load() {
40
+ try {
41
+ const envManifest = await firstValueFrom(this.http.get(this.getManifestUrl()));
42
+ const remoteNames = Object.keys(envManifest);
43
+ const remoteLoads = remoteNames.map(async (remoteName) => {
44
+ const remoteOrigin = envManifest[remoteName];
45
+ const env = await firstValueFrom(this.http.get(this.getRemoteEnvUrl(remoteOrigin)));
46
+ this.envMap.set(remoteName, env);
47
+ });
48
+ await Promise.all(remoteLoads);
49
+ const shellEnv = await firstValueFrom(this.http.get(this.getShellEnvUrl()));
50
+ this.envMap.set('shell', shellEnv);
51
+ }
52
+ catch (e) {
53
+ throw new Error(`Failed to load environment configuration; cannot continue. ${e?.message ?? e}`);
54
+ }
55
+ }
56
+ get(remoteName = 'shell') {
57
+ const v = this.envMap.get(remoteName);
58
+ if (!v) {
59
+ throw new Error(`Env for "${remoteName}" not loaded. Did you register provideAppInitializer(() => env.load()) ?`);
60
+ }
61
+ return v;
62
+ }
63
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: Env, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
64
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: Env, providedIn: 'root' });
65
+ }
66
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: Env, decorators: [{
67
+ type: Injectable,
68
+ args: [{ providedIn: 'root' }]
69
+ }] });
70
+
71
+ function provideFederationEnv() {
72
+ return makeEnvironmentProviders([
73
+ Env, Federation,
74
+ { provide: APP_CONFIG_ENV, useValue: new Map() },
75
+ provideAppInitializer(() => inject(Env).load()),
76
+ ]);
77
+ }
78
+
79
+ /**
80
+ * Generated bundle index. Do not edit.
81
+ */
82
+
83
+ export { provideFederationEnv };
84
+ //# sourceMappingURL=yaagoub-federation-tools.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yaagoub-federation-tools.mjs","sources":["../../../projects/federation-tools/src/federation/federation.service.ts","../../../projects/federation-tools/src/env/env.service.ts","../../../projects/federation-tools/src/providers/provide.ts","../../../projects/federation-tools/src/yaagoub-federation-tools.ts"],"sourcesContent":["import { Injectable } from \"@angular/core\";\r\n\r\n@Injectable({\r\n providedIn: 'root'\r\n})\r\nexport class Federation {\r\n getShellOrigin(): string {\r\n return window.location.origin;\r\n }\r\n}\r\n","import { inject, Injectable } from '@angular/core';\r\nimport { HttpClient } from '@angular/common/http';\r\nimport { firstValueFrom } from 'rxjs';\r\nimport { InjectionToken } from '@angular/core';\r\nimport { Federation } from '../federation/federation.service';\r\n\r\nexport type EnvMap = Map<string, Record<string, any>>;\r\n\r\n/**\r\n * Holds all loaded env objects:\r\n * - key: remoteName (from env-manifest.json) + 'shell'\r\n * - value: parsed env.json for that remote\r\n */\r\nexport const APP_CONFIG_ENV = new InjectionToken<EnvMap>('env-settings');\r\n\r\n@Injectable({ providedIn: 'root' })\r\nexport class Env {\r\n private http = inject(HttpClient);\r\n private federation = inject(Federation);\r\n private envMap: EnvMap = inject(APP_CONFIG_ENV);\r\n\r\n private getManifestUrl(): string {\r\n return this.federation.getShellOrigin() + '/env-manifest.json';\r\n }\r\n\r\n private getShellEnvUrl(): string {\r\n return this.federation.getShellOrigin() + '/env.json';\r\n }\r\n\r\n private getRemoteEnvUrl(origin: string): string {\r\n return origin.replace(/\\/+$/, '') + '/env.json';\r\n }\r\n\r\n async load(): Promise<void> {\r\n try {\r\n const envManifest = await firstValueFrom(\r\n this.http.get<Record<string, string>>(this.getManifestUrl())\r\n );\r\n\r\n const remoteNames = Object.keys(envManifest);\r\n\r\n const remoteLoads = remoteNames.map(async (remoteName) => {\r\n const remoteOrigin = envManifest[remoteName];\r\n const env = await firstValueFrom(\r\n this.http.get<Record<string, any>>(this.getRemoteEnvUrl(remoteOrigin))\r\n );\r\n this.envMap.set(remoteName, env);\r\n });\r\n\r\n await Promise.all(remoteLoads);\r\n\r\n const shellEnv = await firstValueFrom(\r\n this.http.get<Record<string, any>>(this.getShellEnvUrl())\r\n );\r\n this.envMap.set('shell', shellEnv);\r\n\r\n } catch (e: any) {\r\n throw new Error(\r\n `Failed to load environment configuration; cannot continue. ${e?.message ?? e}`\r\n );\r\n }\r\n }\r\n\r\n get(remoteName = 'shell'): Record<string, any> {\r\n const v = this.envMap.get(remoteName);\r\n if (!v) {\r\n throw new Error(\r\n `Env for \"${remoteName}\" not loaded. Did you register provideAppInitializer(() => env.load()) ?`\r\n );\r\n }\r\n return v;\r\n }\r\n}\r\n","import { EnvironmentProviders, inject, makeEnvironmentProviders, provideAppInitializer } from '@angular/core';\nimport { APP_CONFIG_ENV, Env, EnvMap } from '../env/env.service';\nimport { Federation } from '../federation/federation.service';\n\nexport function provideFederationEnv(): EnvironmentProviders {\n return makeEnvironmentProviders([\n Env, Federation,\n { provide: APP_CONFIG_ENV, useValue: new Map() as EnvMap },\n provideAppInitializer(() => inject(Env).load()),\n ]);\n}","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;MAKa,UAAU,CAAA;IACrB,cAAc,GAAA;AACZ,QAAA,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM;IAC/B;wGAHW,UAAU,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAAV,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,UAAU,cAFT,MAAM,EAAA,CAAA;;4FAEP,UAAU,EAAA,UAAA,EAAA,CAAA;kBAHtB,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE;AACb,iBAAA;;;ACID;;;;AAIG;AACI,MAAM,cAAc,GAAG,IAAI,cAAc,CAAS,cAAc,CAAC;MAG3D,GAAG,CAAA;AACN,IAAA,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC;AACzB,IAAA,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;AAC/B,IAAA,MAAM,GAAW,MAAM,CAAC,cAAc,CAAC;IAEvC,cAAc,GAAA;QACpB,OAAO,IAAI,CAAC,UAAU,CAAC,cAAc,EAAE,GAAG,oBAAoB;IAChE;IAEQ,cAAc,GAAA;QACpB,OAAO,IAAI,CAAC,UAAU,CAAC,cAAc,EAAE,GAAG,WAAW;IACvD;AAEQ,IAAA,eAAe,CAAC,MAAc,EAAA;QACpC,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,WAAW;IACjD;AAEA,IAAA,MA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,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAAH,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,GAAG,cADU,MAAM,EAAA,CAAA;;4FACnB,GAAG,EAAA,UAAA,EAAA,CAAA;kBADf,UAAU;mBAAC,EAAE,UAAU,EAAE,MAAM,EAAE;;;SCXlB,oBAAoB,GAAA;AAClC,IAAA,OAAO,wBAAwB,CAAC;AAC9B,QAAA,GAAG,EAAE,UAAU;QACf,EAAE,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,IAAI,GAAG,EAAY,EAAE;QAC1D,qBAAqB,CAAC,MAAM,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AAChD,KAAA,CAAC;AACJ;;ACVA;;AAEG;;;;"}
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { EnvironmentProviders } from '@angular/core';
2
+
3
+ declare function provideFederationEnv(): EnvironmentProviders;
4
+
5
+ export { provideFederationEnv };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@yaagoub/federation-tools",
3
+ "version": "0.0.1",
4
+ "peerDependencies": {
5
+ "@angular/common": ">=15.0.0",
6
+ "@angular/core": ">=15.0.0"
7
+ },
8
+ "dependencies": {
9
+ "tslib": "^2.3.0"
10
+ },
11
+ "sideEffects": false,
12
+ "module": "fesm2022/yaagoub-federation-tools.mjs",
13
+ "typings": "index.d.ts",
14
+ "exports": {
15
+ "./package.json": {
16
+ "default": "./package.json"
17
+ },
18
+ ".": {
19
+ "types": "./index.d.ts",
20
+ "default": "./fesm2022/yaagoub-federation-tools.mjs"
21
+ }
22
+ }
23
+ }