configurapi 1.7.0 → 2.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.
Files changed (66) hide show
  1. package/dist/configurapi.d.ts +21 -0
  2. package/dist/configurapi.js +33 -0
  3. package/dist/entities/config.d.ts +20 -0
  4. package/dist/entities/config.js +149 -0
  5. package/dist/entities/errorResponse.d.ts +9 -0
  6. package/dist/entities/errorResponse.js +31 -0
  7. package/dist/entities/event.d.ts +22 -0
  8. package/dist/entities/event.js +154 -0
  9. package/dist/entities/identity.d.ts +17 -0
  10. package/{src → dist/entities}/identity.js +35 -67
  11. package/dist/entities/policy.d.ts +13 -0
  12. package/dist/entities/policy.js +34 -0
  13. package/dist/entities/policyHandlerLoader.d.ts +11 -0
  14. package/dist/entities/policyHandlerLoader.js +66 -0
  15. package/dist/entities/request.d.ts +14 -0
  16. package/{src → dist/entities}/request.js +8 -6
  17. package/dist/entities/response.d.ts +7 -0
  18. package/dist/entities/response.js +14 -0
  19. package/dist/entities/route.d.ts +15 -0
  20. package/dist/entities/route.js +38 -0
  21. package/dist/entities/service.d.ts +20 -0
  22. package/dist/entities/service.js +118 -0
  23. package/dist/interfaces/iClaim.d.ts +6 -0
  24. package/dist/interfaces/iClaim.js +2 -0
  25. package/dist/interfaces/iConfig.d.ts +5 -0
  26. package/dist/interfaces/iConfig.js +2 -0
  27. package/dist/interfaces/iErrorResponse.d.ts +5 -0
  28. package/dist/interfaces/iErrorResponse.js +2 -0
  29. package/dist/interfaces/iEvent.d.ts +16 -0
  30. package/dist/interfaces/iEvent.js +2 -0
  31. package/dist/interfaces/iIdentity.d.ts +11 -0
  32. package/dist/interfaces/iIdentity.js +2 -0
  33. package/dist/interfaces/iPolicy.d.ts +12 -0
  34. package/dist/interfaces/iPolicy.js +2 -0
  35. package/dist/interfaces/iPolicyHandlerLoader.d.ts +7 -0
  36. package/dist/interfaces/iPolicyHandlerLoader.js +2 -0
  37. package/dist/interfaces/iRequest.d.ts +12 -0
  38. package/dist/interfaces/iRequest.js +2 -0
  39. package/dist/interfaces/iResponse.d.ts +5 -0
  40. package/dist/interfaces/iResponse.js +2 -0
  41. package/dist/interfaces/iRoute.d.ts +13 -0
  42. package/dist/interfaces/iRoute.js +2 -0
  43. package/dist/interfaces/iService.d.ts +6 -0
  44. package/dist/interfaces/iService.js +2 -0
  45. package/dist/interfaces/iServiceListener.d.ts +3 -0
  46. package/dist/interfaces/iServiceListener.js +2 -0
  47. package/dist/interfaces/iYamlConfig.d.ts +7 -0
  48. package/dist/interfaces/iYamlConfig.js +2 -0
  49. package/dist/interfaces/logLevel.d.ts +11 -0
  50. package/dist/interfaces/logLevel.js +26 -0
  51. package/dist/interfaces/result.d.ts +4 -0
  52. package/dist/interfaces/result.js +8 -0
  53. package/package.json +19 -8
  54. package/src/config.js +0 -227
  55. package/src/configurapi.d.ts +0 -161
  56. package/src/configurapi.js +0 -11
  57. package/src/dict.js +0 -40
  58. package/src/errorResponse.js +0 -39
  59. package/src/event.js +0 -199
  60. package/src/logLevel.js +0 -7
  61. package/src/policy/policy.js +0 -44
  62. package/src/policy/policyHandlerLoader.js +0 -96
  63. package/src/response.js +0 -9
  64. package/src/result.js +0 -4
  65. package/src/route.js +0 -61
  66. package/src/service.js +0 -204
@@ -0,0 +1,21 @@
1
+ export * from './entities/config';
2
+ export * from './entities/errorResponse';
3
+ export * from './entities/event';
4
+ export * from './entities/identity';
5
+ export * from './entities/policy';
6
+ export * from './entities/policyHandlerLoader';
7
+ export * from './entities/response';
8
+ export * from './entities/route';
9
+ export * from './entities/service';
10
+ export * from './entities/request';
11
+ export * from './interfaces/iClaim';
12
+ export * from './interfaces/iEvent';
13
+ export * from './interfaces/iIdentity';
14
+ export * from './interfaces/iRequest';
15
+ export * from './interfaces/iResponse';
16
+ export * from './interfaces/iService';
17
+ export * from './interfaces/iServiceListener';
18
+ export * from './interfaces/logLevel';
19
+ export * from './interfaces/result';
20
+ export * from './interfaces/iRoute';
21
+ export * from './interfaces/iPolicy';
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./entities/config"), exports);
14
+ __exportStar(require("./entities/errorResponse"), exports);
15
+ __exportStar(require("./entities/event"), exports);
16
+ __exportStar(require("./entities/identity"), exports);
17
+ __exportStar(require("./entities/policy"), exports);
18
+ __exportStar(require("./entities/policyHandlerLoader"), exports);
19
+ __exportStar(require("./entities/response"), exports);
20
+ __exportStar(require("./entities/route"), exports);
21
+ __exportStar(require("./entities/service"), exports);
22
+ __exportStar(require("./entities/request"), exports);
23
+ __exportStar(require("./interfaces/iClaim"), exports);
24
+ __exportStar(require("./interfaces/iEvent"), exports);
25
+ __exportStar(require("./interfaces/iIdentity"), exports);
26
+ __exportStar(require("./interfaces/iRequest"), exports);
27
+ __exportStar(require("./interfaces/iResponse"), exports);
28
+ __exportStar(require("./interfaces/iService"), exports);
29
+ __exportStar(require("./interfaces/iServiceListener"), exports);
30
+ __exportStar(require("./interfaces/logLevel"), exports);
31
+ __exportStar(require("./interfaces/result"), exports);
32
+ __exportStar(require("./interfaces/iRoute"), exports);
33
+ __exportStar(require("./interfaces/iPolicy"), exports);
@@ -0,0 +1,20 @@
1
+ import { IConfig } from '../interfaces/iConfig';
2
+ import { IPolicy } from '../interfaces/iPolicy';
3
+ import { IRoute } from '../interfaces/iRoute';
4
+ import { IYamlApi, IYamlConfig } from '../interfaces/iYamlConfig';
5
+ export declare class Config implements IConfig {
6
+ modules: string[];
7
+ events: Map<String, IRoute>;
8
+ constructor(modules?: string[], events?: Map<String, IRoute>);
9
+ static getPathParentDir(path: any): string;
10
+ static load(path: string): IConfig;
11
+ static parse(content: string, parentDirectory?: string): IConfig;
12
+ static parseImport(document: object, parentDirectory?: string): string[];
13
+ static parseApi(document: Partial<IYamlConfig>): Map<String, IRoute>;
14
+ static parseRoutes(api: IYamlApi): Map<String, IRoute>;
15
+ static parseRoute(route: IRoute, routeIndex: number): IRoute;
16
+ static parsePolicies(route: IRoute, routeName: string): IPolicy[];
17
+ static parsePolicy(policy: IPolicy, routeName: string, policyIndex: number): IPolicy;
18
+ static parsePolicyName(policy: IPolicy): string;
19
+ static parseParameter(policy: IPolicy, policyName: string): any[];
20
+ }
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Config = void 0;
4
+ const fs_1 = require("fs");
5
+ const js_yaml_1 = require("js-yaml");
6
+ const NestedError = require("nested-error-stacks");
7
+ const path_1 = require("path");
8
+ const logLevel_1 = require("../interfaces/logLevel");
9
+ const policy_1 = require("./policy");
10
+ const route_1 = require("./route");
11
+ class Config {
12
+ constructor(modules = [], events = new Map()) {
13
+ this.modules = modules;
14
+ this.events = events;
15
+ }
16
+ static getPathParentDir(path) {
17
+ if (!path_1.isAbsolute(path)) {
18
+ path = path_1.resolve(process.cwd(), path);
19
+ }
20
+ return path_1.parse(path).dir;
21
+ }
22
+ static load(path) {
23
+ let content = "";
24
+ try {
25
+ content = fs_1.readFileSync(path, 'utf8');
26
+ }
27
+ catch (error) {
28
+ throw new NestedError(`Failed to load '${path}'`, error);
29
+ }
30
+ return this.parse(content, this.getPathParentDir(path));
31
+ }
32
+ static parse(content, parentDirectory) {
33
+ let document = js_yaml_1.load(content);
34
+ if (document instanceof Object) {
35
+ return new Config(this.parseImport(document, parentDirectory), this.parseApi(document));
36
+ }
37
+ else {
38
+ throw new Error("The specified config file is not an invalid yaml.");
39
+ }
40
+ }
41
+ static parseImport(document, parentDirectory) {
42
+ let result = [];
43
+ //Check for an optional "import"
44
+ if ('import' in document) {
45
+ if (typeof document['import'][Symbol.iterator] !== 'function') {
46
+ throw new Error(`Could not load 'import.${document['import']}' in the config document`);
47
+ }
48
+ for (let module of document['import']) {
49
+ if (module.startsWith('.')) {
50
+ module = path_1.resolve(parentDirectory, module);
51
+ }
52
+ result.push(module);
53
+ }
54
+ }
55
+ return result;
56
+ }
57
+ static parseApi(document) {
58
+ if ('api' in document) {
59
+ return this.parseRoutes(document.api);
60
+ }
61
+ else {
62
+ throw new Error('Could not find \'api\' in the config document');
63
+ }
64
+ }
65
+ static parseRoutes(api) {
66
+ if ((api instanceof Object) && 'events' in api) {
67
+ let result = new Map();
68
+ if (typeof api.events[Symbol.iterator] !== 'function') {
69
+ throw new Error(`Events could not be empty in the config document.`);
70
+ }
71
+ let routeIndex = 0;
72
+ for (let r of api.events) {
73
+ let route = this.parseRoute(r, routeIndex);
74
+ if (route.enabled) {
75
+ result.set(route.name, route);
76
+ }
77
+ }
78
+ return result;
79
+ }
80
+ else {
81
+ throw new Error('Could not find \'api.events\' in the config document');
82
+ }
83
+ }
84
+ static parseRoute(route, routeIndex) {
85
+ let result = new route_1.Route();
86
+ if (route instanceof Object && 'name' in route) {
87
+ result.name = route.name;
88
+ result.logLevel = logLevel_1.LogLevel[Object.keys(logLevel_1.LogLevel).find(key => key.toLowerCase() === (route.logLevel || logLevel_1.LogLevel.Trace).toLowerCase())];
89
+ if (route.when) {
90
+ if (process.env.NODE_ENV === undefined) {
91
+ result.enabled = false;
92
+ }
93
+ else {
94
+ result.enabled = process.env.NODE_ENV === route.when;
95
+ }
96
+ }
97
+ result.policies = this.parsePolicies(route, result.name);
98
+ return result;
99
+ }
100
+ else {
101
+ throw new Error(`Could not find 'api.events[${routeIndex}].name' in the config document`);
102
+ }
103
+ }
104
+ static parsePolicies(route, routeName) {
105
+ let result = [];
106
+ if (route instanceof Object && 'policies' in route) {
107
+ let policyIndex = 0;
108
+ if (typeof route.policies[Symbol.iterator] !== 'function') {
109
+ throw new Error(`Policies 'api.events[${routeName}].policies' could not be empty in the config document.`);
110
+ }
111
+ for (let policy of route.policies) {
112
+ result.push(this.parsePolicy(policy, routeName, policyIndex));
113
+ }
114
+ return result;
115
+ }
116
+ else {
117
+ throw new Error(`Could not find 'api.events[${routeName}].policies' in the config document`);
118
+ }
119
+ }
120
+ static parsePolicy(policy, routeName, policyIndex) {
121
+ let policyName = this.parsePolicyName(policy);
122
+ if (!policyName) {
123
+ throw new Error(`Could not find 'api.events[${routeName}].policies[${policyIndex}]' in the config document`);
124
+ }
125
+ return new policy_1.Policy({
126
+ name: policyName,
127
+ parameters: this.parseParameter(policy, policyName),
128
+ logLevel: policy.logLevel || logLevel_1.LogLevel.Trace
129
+ });
130
+ }
131
+ static parsePolicyName(policy) {
132
+ if (typeof policy == 'string') {
133
+ return policy;
134
+ }
135
+ for (var key in policy) {
136
+ if (policy.hasOwnProperty(key)) {
137
+ return key;
138
+ }
139
+ return undefined;
140
+ }
141
+ }
142
+ static parseParameter(policy, policyName) {
143
+ if (policy[policyName] instanceof Array) {
144
+ return policy[policyName];
145
+ }
146
+ return policy.parameters || [];
147
+ }
148
+ }
149
+ exports.Config = Config;
@@ -0,0 +1,9 @@
1
+ import { IErrorResponse } from "../interfaces/iErrorResponse";
2
+ import { Response } from "./response";
3
+ export declare class ErrorResponse extends Response implements IErrorResponse {
4
+ error: Error | ErrorResponse | string;
5
+ message: Error | ErrorResponse | any;
6
+ details: string;
7
+ constructor(message: Error | ErrorResponse | any, statusCode?: number, details?: string);
8
+ static isErrorResponse(obj: any): boolean;
9
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorResponse = void 0;
4
+ const response_1 = require("./response");
5
+ class ErrorResponse extends response_1.Response {
6
+ constructor(message, statusCode = 500, details = '') {
7
+ super('', statusCode, { 'Content-Type': 'application/json' });
8
+ this.error = message;
9
+ this.message = message;
10
+ this.details = details;
11
+ if (message instanceof ErrorResponse) {
12
+ let e = message;
13
+ this.message = e.message;
14
+ this.details = e.details;
15
+ this.statusCode = e.statusCode;
16
+ }
17
+ else if (message instanceof Error) {
18
+ this.message = message.message;
19
+ this.details = message.stack;
20
+ }
21
+ else {
22
+ this.message = message;
23
+ this.details = details;
24
+ }
25
+ this.body = { 'statusCode': this.statusCode, 'message': this.message, 'details': this.details };
26
+ }
27
+ static isErrorResponse(obj) {
28
+ return typeof obj === 'object' && 'statusCode' in obj && 'error' in obj && 'message' in obj && 'details' in obj && 'body' in obj;
29
+ }
30
+ }
31
+ exports.ErrorResponse = ErrorResponse;
@@ -0,0 +1,22 @@
1
+ import { IIdentity } from '../configurapi';
2
+ import { IEvent } from '../interfaces/iEvent';
3
+ import { IRequest } from '../interfaces/iRequest';
4
+ import { IResponse } from '../interfaces/iResponse';
5
+ export declare class Event implements IEvent {
6
+ #private;
7
+ id: string;
8
+ params: Record<string, any>;
9
+ name: string;
10
+ request: Partial<IRequest>;
11
+ response: Partial<IResponse>;
12
+ method: string;
13
+ version: string;
14
+ correlationId: string;
15
+ identity: IIdentity;
16
+ payload: string | Record<string, any> | undefined;
17
+ constructor(request?: Partial<IRequest>);
18
+ objectToString(o: any, s: any): string;
19
+ resolve(obj: any): any;
20
+ resolveStr(str: any): any;
21
+ toString(): string;
22
+ }
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
3
+ if (!privateMap.has(receiver)) {
4
+ throw new TypeError("attempted to set private field on non-instance");
5
+ }
6
+ privateMap.set(receiver, value);
7
+ return value;
8
+ };
9
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
10
+ if (!privateMap.has(receiver)) {
11
+ throw new TypeError("attempted to get private field on non-instance");
12
+ }
13
+ return privateMap.get(receiver);
14
+ };
15
+ var _versionRegExp;
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Event = void 0;
18
+ const inflection_1 = require("inflection");
19
+ const uuid_1 = require("uuid");
20
+ const response_1 = require("./response");
21
+ class Event {
22
+ constructor(request) {
23
+ _versionRegExp.set(this, void 0);
24
+ this.id = uuid_1.v4();
25
+ if ('query' in request && 'correlationid' in request.query) {
26
+ this.correlationId = request.query['correlationid'].toString();
27
+ }
28
+ else if ('headers' in request && 'correlation-id' in request.headers) {
29
+ this.correlationId = request.headers['correlation-id'];
30
+ }
31
+ else {
32
+ this.correlationId = this.id;
33
+ }
34
+ __classPrivateFieldSet(this, _versionRegExp, new RegExp("^v\\d+(\\.\\d+)*$"));
35
+ this.params = {};
36
+ this.name = request.name || "";
37
+ this.request = request;
38
+ this.response = new response_1.Response();
39
+ this.identity = undefined;
40
+ this.payload = this.request.payload;
41
+ this.method = request.method;
42
+ let segments = request.path.split("/").filter(Boolean);
43
+ if (segments.length === 0)
44
+ return;
45
+ //Extract version, if any.
46
+ if (__classPrivateFieldGet(this, _versionRegExp).test(segments[0])) {
47
+ this.version = segments[0];
48
+ segments = segments.slice(1);
49
+ }
50
+ //Extract resource-resourceId pair.
51
+ for (let i = 0, end = segments.length; i < end; i += 2) {
52
+ let resource;
53
+ let resourceId = '';
54
+ if (i < end) {
55
+ resource = segments[i];
56
+ }
57
+ if (i + 1 < end) {
58
+ resourceId = segments[i + 1];
59
+ }
60
+ if (resource.length === 0)
61
+ break;
62
+ let singularizedResource = inflection_1.singularize(resource);
63
+ //Change 'get' to 'list'
64
+ if (!resourceId && this.method == 'get') {
65
+ this.method = 'list';
66
+ }
67
+ else {
68
+ resource = singularizedResource;
69
+ }
70
+ this.params[singularizedResource] = resourceId;
71
+ this.name += `_${resource}`;
72
+ }
73
+ for (let key in request.query) {
74
+ this.params[key] = request.query[key];
75
+ }
76
+ this.name = this.method + (this.version ? `_${this.version}` : "") + this.name;
77
+ }
78
+ objectToString(o, s) {
79
+ s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
80
+ s = s.replace(/^\./, ''); // strip a leading dot
81
+ var a = s.split('.');
82
+ for (var i = 0, n = a.length; i < n; ++i) {
83
+ var k = a[i];
84
+ if (k in o) {
85
+ o = o[k];
86
+ }
87
+ else {
88
+ return;
89
+ }
90
+ }
91
+ return o;
92
+ }
93
+ resolve(obj) {
94
+ if (typeof obj === 'string') {
95
+ return this.resolveStr(obj);
96
+ }
97
+ else if (typeof obj === 'object') {
98
+ for (let property in obj) {
99
+ obj[property] = this.resolve(obj[property]);
100
+ }
101
+ return obj;
102
+ }
103
+ else {
104
+ return obj;
105
+ }
106
+ }
107
+ resolveStr(str) {
108
+ let regex = /\$\([^\)]*\)/g;
109
+ let matches = str.match(regex);
110
+ if (!matches) {
111
+ return str;
112
+ }
113
+ for (let match of matches) {
114
+ let key = match.substr(2, match.length - 3);
115
+ let value = undefined;
116
+ if (key.startsWith('$env.')) {
117
+ //handle environment variables
118
+ let variableName = key.substr(5);
119
+ value = process.env[variableName];
120
+ }
121
+ else if (key.startsWith('$event.payload.') && this.payload) {
122
+ //handle environment variables
123
+ let path = key.substr(15);
124
+ value = this.objectToString(this.payload, path);
125
+ }
126
+ else if (key === '$event.payload' && this.payload) {
127
+ value = this.payload;
128
+ }
129
+ else if (this.params[key] !== undefined) {
130
+ value = this.params[key];
131
+ }
132
+ else if (key.indexOf('.') !== -1 || key.indexOf('[') !== -1) {
133
+ //handle resolving object using string path such as obj.prop.subprop.list[0]
134
+ let index = key.indexOf('.');
135
+ let objSection = key.substr(0, index);
136
+ let propSection = key.substr(index + 1);
137
+ value = this.params[objSection];
138
+ if (typeof value === 'object') {
139
+ value = this.objectToString(value, propSection);
140
+ }
141
+ }
142
+ if (value === undefined) {
143
+ value = match;
144
+ }
145
+ str = str.replace(match, value);
146
+ }
147
+ return str;
148
+ }
149
+ toString() {
150
+ return `${this.name}(${Object.values(this.params)})`;
151
+ }
152
+ }
153
+ exports.Event = Event;
154
+ _versionRegExp = new WeakMap();
@@ -0,0 +1,17 @@
1
+ import { IClaim } from '../interfaces/iClaim';
2
+ import { IIdentity } from '../interfaces/iIdentity';
3
+ export declare class Identity implements IIdentity {
4
+ private _claims;
5
+ private _claimsTable;
6
+ id: string;
7
+ issuer: string;
8
+ name: string;
9
+ email: string;
10
+ get claims(): Partial<IClaim>[];
11
+ set claims(value: Partial<IClaim>[]);
12
+ getClaimId(type: string, value: string, scope: string): string;
13
+ constructor(identity?: Partial<IIdentity>);
14
+ hasClaim(type: string, valueOrValues: string | string[], scopeOrScopes?: string | string[]): boolean;
15
+ getClaim(type: string, value: string, scope?: string): Partial<IClaim>;
16
+ getClaims(type: string, value: string, scope?: string): IClaim[];
17
+ }
@@ -1,100 +1,68 @@
1
- let Dictionary = require('./dict');
2
-
3
- module.exports = class Identity
4
- {
5
- get claims()
6
- {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Identity = void 0;
4
+ class Identity {
5
+ constructor(identity) {
6
+ this._claims = [];
7
+ this._claimsTable = new Map();
8
+ this.id = identity?.id;
9
+ this.claims = identity?.claims || [];
10
+ this.issuer = identity?.issuer;
11
+ this.name = identity?.name;
12
+ this.email = identity?.email;
13
+ }
14
+ get claims() {
7
15
  return this._claims;
8
16
  }
9
-
10
- set claims(value)
11
- {
17
+ set claims(value) {
12
18
  this._claims = value;
13
-
14
19
  this._claimsTable.clear();
15
-
16
- for(let claim of this._claims)
17
- {
20
+ for (let claim of this._claims) {
18
21
  let scopes = Array.isArray(claim.scope) ? claim.scope : [claim.scope];
19
-
20
- for(let scope of scopes)
21
- {
22
+ for (let scope of scopes) {
22
23
  this._claimsTable.set(this.getClaimId(claim.type, claim.value, scope), claim);
23
24
  }
24
25
  }
25
26
  }
26
-
27
- getClaimId(type, value, scope)
28
- {
27
+ getClaimId(type, value, scope) {
29
28
  return `${scope || ''}::${type || ''}::${value || ''}`;
30
29
  }
31
-
32
- constructor(id, claims = [])
33
- {
34
- this._claims = [];
35
- this._claimsTable = new Dictionary();
36
-
37
- this.id = id;
38
- this.claims = claims;
39
- this.issuer = undefined;
40
- this.name = undefined;
41
- this.email = undefined;
42
- }
43
-
44
- hasClaim(type, valueOrValues, scopeOrScopes)
45
- {
46
- if(!this.claims) return false;
47
-
30
+ hasClaim(type, valueOrValues, scopeOrScopes) {
31
+ if (!this.claims)
32
+ return false;
48
33
  let values = Array.isArray(valueOrValues) ? [...new Set(valueOrValues)] : [valueOrValues];
49
-
50
34
  let scopes = Array.isArray(scopeOrScopes) ? [...new Set(scopeOrScopes)] : [scopeOrScopes];
51
-
52
- for(let value of values)
53
- {
54
- for(let scope of scopes)
55
- {
56
- if(this._claimsTable.has(this.getClaimId(type, value, scope)))
57
- {
35
+ for (let value of values) {
36
+ for (let scope of scopes) {
37
+ if (this._claimsTable.has(this.getClaimId(type, value, scope))) {
58
38
  return true;
59
39
  }
60
40
  }
61
41
  }
62
-
63
42
  return false;
64
43
  }
65
-
66
- getClaim(type, value, scope)
67
- {
68
- if(!this._claimsTable.has(this.getClaimId(type, value, scope))) return undefined;
69
-
44
+ getClaim(type, value, scope) {
45
+ if (!this._claimsTable.has(this.getClaimId(type, value, scope)))
46
+ return undefined;
70
47
  return this._claimsTable.get(this.getClaimId(type, value, scope));
71
48
  }
72
-
73
- getClaims(type, value, scope)
74
- {
75
- if(!this.claims) return [];
76
-
49
+ getClaims(type, value, scope) {
50
+ if (!this.claims)
51
+ return [];
77
52
  let result = [];
78
-
79
53
  let matchingTypeValueClaims = this.claims.filter((c) => c.type === type && c.value === value);
80
-
81
- for(let claim of matchingTypeValueClaims)
82
- {
83
- if(claim.scope === scope)
84
- {
54
+ for (let claim of matchingTypeValueClaims) {
55
+ if (claim.scope === scope) {
85
56
  result.push(claim);
86
57
  continue;
87
58
  }
88
-
89
59
  //For arrays
90
60
  let scopes = Array.isArray(claim.scope) ? claim.scope : [claim.scope];
91
-
92
- if(scopes.includes(scope))
93
- {
61
+ if (scopes.includes(scope)) {
94
62
  result.push(claim);
95
63
  }
96
64
  }
97
-
98
65
  return result;
99
66
  }
100
- };
67
+ }
68
+ exports.Identity = Identity;
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ import { IEvent, Result } from '../configurapi';
4
+ import { IPolicy } from '../interfaces/iPolicy';
5
+ import { LogLevel } from '../interfaces/logLevel';
6
+ export declare class Policy extends EventEmitter implements IPolicy {
7
+ name: string;
8
+ parameters: any[];
9
+ logLevel: LogLevel;
10
+ handler: Function;
11
+ constructor(policy?: Partial<IPolicy>);
12
+ process(event: IEvent): Promise<Result>;
13
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Policy = void 0;
4
+ const events_1 = require("events");
5
+ const configurapi_1 = require("../configurapi");
6
+ const logLevel_1 = require("../interfaces/logLevel");
7
+ class Policy extends events_1.EventEmitter {
8
+ constructor(policy) {
9
+ super();
10
+ this.name = policy?.name || "";
11
+ this.parameters = policy?.parameters || [];
12
+ this.logLevel = policy?.logLevel || logLevel_1.LogLevel.Trace;
13
+ this.handler = policy?.handler || undefined;
14
+ }
15
+ async process(event) {
16
+ let state = configurapi_1.Result.Continue;
17
+ let context = {
18
+ 'continue': () => state = configurapi_1.Result.Continue,
19
+ 'complete': () => state = configurapi_1.Result.Completed,
20
+ 'catch': (error) => { throw error; },
21
+ 'emit': (level, message) => this.emit(level, `${event.id} - ${event.correlationId} - ${message}`)
22
+ };
23
+ let handlerParams = this.parameters instanceof Array ? this.parameters.slice(0) : [];
24
+ handlerParams.unshift(event);
25
+ try {
26
+ await this.handler.apply(context, handlerParams);
27
+ return state;
28
+ }
29
+ catch (error) {
30
+ throw error;
31
+ }
32
+ }
33
+ }
34
+ exports.Policy = Policy;
@@ -0,0 +1,11 @@
1
+ import { FunctionType, IPolicyHandlerLoader } from '../interfaces/iPolicyHandlerLoader';
2
+ export declare class PolicyHandlerLoader implements IPolicyHandlerLoader {
3
+ handlers: Map<String, FunctionType>;
4
+ constructor(handlers?: Map<String, FunctionType>);
5
+ get(handlerName: string): Function;
6
+ load(moduleOrFileName: string): void;
7
+ loadCustomHandlers(directory: string): void;
8
+ private resolve;
9
+ loadHandler(obj: any): void;
10
+ private isHandler;
11
+ }