@ztimson/utils 0.18.2 → 0.19.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.
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Available methods:
3
+ * * - All/Wildcard
4
+ * n - None
5
+ * c - Create
6
+ * r - Read
7
+ * u - Update
8
+ * d - Delete
9
+ * x - Execute
10
+ */
11
+ export type Method = '*' | 'n' | 'c' | 'r' | 'u' | 'd' | 'x';
12
+ /**
13
+ * Shorthand for creating PathedEvent from a string
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const event: PathedEvent = PE`users/system:*`;
18
+ * ```
19
+ *
20
+ * @param {TemplateStringsArray} str String that will be parsed into PathedEvent
21
+ * @param {string} args
22
+ * @return {PathEvent} PathedEvent object
23
+ */
24
+ export declare function PE(str: TemplateStringsArray, ...args: string[]): PathEvent;
25
+ /**
26
+ * Shorthand for creating PathedEvent strings, ensures paths are correct
27
+ *
28
+ * @param {TemplateStringsArray} str
29
+ * @param {string} args
30
+ * @return {string}
31
+ * @constructor
32
+ */
33
+ export declare function PES(str: TemplateStringsArray, ...args: any[]): string;
34
+ /**
35
+ * A pathed event broken down into its core components for easy processing
36
+ * PathedEvent Structure: `module/path/name:property:method`
37
+ * Example: `users/system:crud` or `storage/some/path/file.txt:r`
38
+ */
39
+ export declare class PathEvent {
40
+ /** First directory in path */
41
+ module: string;
42
+ /** Entire path, including the module & name */
43
+ fullPath: string;
44
+ /** Path including the name, excluding the module */
45
+ path: string;
46
+ /** Last sagment of path */
47
+ name: string;
48
+ /** List of methods */
49
+ methods: Method[];
50
+ /** All/Wildcard specified */
51
+ all: boolean;
52
+ /** None specified */
53
+ none: boolean;
54
+ /** Create method specified */
55
+ create: boolean;
56
+ /** Read method specified */
57
+ read: boolean;
58
+ /** Update method specified */
59
+ update: boolean;
60
+ /** Delete method specified */
61
+ delete: boolean;
62
+ constructor(pathedEvent: string | PathEvent);
63
+ /**
64
+ * Combine multiple pathed events into one parsed object. Longest path takes precedent, but all subsequent methods are
65
+ * combined until a "none" is reached
66
+ *
67
+ * @param {string | PathEvent} paths PathedEvents as strings or pre-parsed
68
+ * @return {PathEvent} Final combined permission
69
+ */
70
+ static combine(paths: (string | PathEvent)[]): PathEvent;
71
+ /**
72
+ * Squash 2 sets of paths & return true if any overlap is found
73
+ *
74
+ * @param {string | PathEvent | (string | PathEvent)[]} target Array of PathedEvents as strings or pre-parsed
75
+ * @param has Target must have at least one of these path
76
+ * @return {boolean} Whether there is any overlap
77
+ */
78
+ static has(target: string | PathEvent | (string | PathEvent)[], ...has: (string | PathEvent)[]): boolean;
79
+ /**
80
+ * Squash 2 sets of paths & return true if the target has all paths
81
+ *
82
+ * @param {string | PathEvent | (string | PathEvent)[]} target Array of PathedEvents as strings or pre-parsed
83
+ * @param has Target must have all these paths
84
+ * @return {boolean} Whether there is any overlap
85
+ */
86
+ static hasAll(target: string | PathEvent | (string | PathEvent)[], ...has: (string | PathEvent)[]): boolean;
87
+ /**
88
+ * Same as `has` but raises an error if there is no overlap
89
+ *
90
+ * @param {string | string[]} target Array of PathedEvents as strings or pre-parsed
91
+ * @param has Target must have at least one of these path
92
+ */
93
+ static hasFatal(target: string | PathEvent | (string | PathEvent)[], ...has: (string | PathEvent)[]): void;
94
+ /**
95
+ * Same as `hasAll` but raises an error if the target is missing any paths
96
+ *
97
+ * @param {string | string[]} target Array of PathedEvents as strings or pre-parsed
98
+ * @param has Target must have all these paths
99
+ */
100
+ static hasAllFatal(target: string | PathEvent | (string | PathEvent)[], ...has: (string | PathEvent)[]): void;
101
+ /**
102
+ * Create pathed event string from its components
103
+ *
104
+ * @param {string | string[]} path Event path
105
+ * @param {Method} methods Event method
106
+ * @return {string} String representation of PathedEvent
107
+ */
108
+ static toString(path: string | string[], methods: Method | Method[]): string;
109
+ /**
110
+ * Create pathed event string from its components
111
+ *
112
+ * @return {string} String representation of PathedEvent
113
+ */
114
+ toString(): string;
115
+ }
116
+ export type PathListener = (event: PathEvent, ...args: any[]) => any;
117
+ export type PathUnsubscribe = () => void;
118
+ export interface IPathedEventEmitter {
119
+ emit(event: string, ...args: any[]): void;
120
+ off(listener: PathListener): void;
121
+ on(event: string, listener: PathListener): PathUnsubscribe;
122
+ once(event: string, listener?: PathListener): Promise<any>;
123
+ relayEvents(emitter: PathEventEmitter): void;
124
+ }
125
+ /**
126
+ * Event emitter that uses paths allowing listeners to listen to different combinations of modules, paths & methods
127
+ */
128
+ export declare class PathEventEmitter implements IPathedEventEmitter {
129
+ private listeners;
130
+ emit(event: string | PathEvent, ...args: any[]): void;
131
+ off(listener: PathListener): void;
132
+ on(event: string | string[], listener: PathListener): PathUnsubscribe;
133
+ once(event: string | string[], listener?: PathListener): Promise<any>;
134
+ relayEvents(emitter: IPathedEventEmitter): void;
135
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ztimson/utils",
3
- "version": "0.18.2",
3
+ "version": "0.19.1",
4
4
  "description": "Utility library",
5
5
  "author": "Zak Timson",
6
6
  "license": "MIT",