@flatfile/listener 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/.eslintrc.js ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['@flatfile/eslint-config-platform-sdk'],
4
+ }
@@ -0,0 +1,17 @@
1
+
2
+ > @flatfile/listener@0.0.1 build
3
+ > tsup src/index.ts --format esm,cjs --dts
4
+
5
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v6.6.3
8
+ CLI Target: es6
9
+ ESM Build start
10
+ CJS Build start
11
+ CJS dist/index.js 209.83 KB
12
+ CJS ⚡️ Build success in 347ms
13
+ ESM dist/index.mjs 208.05 KB
14
+ ESM ⚡️ Build success in 354ms
15
+ DTS Build start
16
+ DTS ⚡️ Build success in 3645ms
17
+ DTS dist/index.d.ts 6.30 KB
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @flatfile/listener
2
+
3
+ ## 0.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 4b918a2: Adds the new @flatfile/listener package
@@ -0,0 +1,230 @@
1
+ import { DefaultApi, Event } from '@flatfile/api';
2
+ import { Agent } from '@flatfile/configure';
3
+
4
+ declare class AuthenticatedClient {
5
+ private _api?;
6
+ get api(): DefaultApi;
7
+ }
8
+
9
+ declare class FlatfileEvent extends AuthenticatedClient {
10
+ private readonly src;
11
+ /**
12
+ * Event ID from the API
13
+ *
14
+ * @example us0_ev_82hgidh9skd
15
+ * @readonly
16
+ *
17
+ */
18
+ readonly id?: string;
19
+ /**
20
+ * Topic the event was produced on
21
+ *
22
+ * @example workbook:created
23
+ * @readonly
24
+ */
25
+ readonly topic: string;
26
+ readonly domain: string;
27
+ readonly target: string;
28
+ readonly context: any;
29
+ readonly payload: any;
30
+ constructor(src: Event);
31
+ /**
32
+ * Should return either event body if expanded already or fetch data from the
33
+ * signed callback URL
34
+ *
35
+ * @todo this should work with the included callback URL
36
+ */
37
+ get data(): Promise<any>;
38
+ }
39
+ type EventCallback = (evt: FlatfileEvent) => void;
40
+
41
+ /**
42
+ * EventHandler is a Flatfile flavored implementation of EventTarget
43
+ */
44
+ declare class EventHandler extends AuthenticatedClient {
45
+ /**
46
+ * Apply a filter to the values of an event
47
+ */
48
+ readonly filterQuery?: EventFilter;
49
+ /**
50
+ * Cache of registered listeners on this instance
51
+ * @private
52
+ */
53
+ private listeners;
54
+ constructor(filter?: EventFilter);
55
+ /**
56
+ * Cache of registered child nodes for this listener. These nodes will
57
+ * only receive events that pass the parent filter.
58
+ *
59
+ * @private
60
+ */
61
+ private nodes;
62
+ /**
63
+ * Register a subscriber for events that match this path
64
+ */
65
+ on(query: Arrayable<string>, callback: EventCallback): this;
66
+ on(query: Arrayable<string>, filter: EventFilter, callback: EventCallback): this;
67
+ /**
68
+ * Add child nodes to send this event to as well
69
+ *
70
+ * @param node
71
+ */
72
+ addNode(node: EventHandler): this;
73
+ /**
74
+ * Dispatch an event and resolve the promise once it has completed (or
75
+ * errored
76
+ *
77
+ * @todo - is there a right order in which to resolve event listeners?
78
+ * Should it matter?
79
+ *
80
+ * @param event
81
+ */
82
+ dispatchEvent(event: FlatfileEvent | Event | any): Promise<void>;
83
+ /**
84
+ * @deprecated legacy shim for receiving events from the VM layer
85
+ * @alias dispatchEvent
86
+ * @param event
87
+ */
88
+ routeEvent(event: Event): Promise<void>;
89
+ /**
90
+ * Actually trigger the event listeners on this particular target
91
+ *
92
+ * @note It is safer for now to run this in series to avoid IO locks and
93
+ * potential race conditions and uncaught errors
94
+ *
95
+ * @param event
96
+ */
97
+ trigger(event: FlatfileEvent): Promise<void>;
98
+ /**
99
+ * Get any listeners from this target subscribing to this event
100
+ *
101
+ * @param event
102
+ */
103
+ getListeners(event: FlatfileEvent): EventCallback[];
104
+ /**
105
+ * Attach more event listeners using a callback function. Used most
106
+ * frequently for plugins.
107
+ *
108
+ * @param fn
109
+ */
110
+ use(fn: (handler: this) => void): this;
111
+ /**
112
+ * Filter an event out based on glob filter object
113
+ *
114
+ * @param event
115
+ * @param filter
116
+ */
117
+ matchEvent(event: FlatfileEvent, filter: EventFilter | undefined): boolean;
118
+ }
119
+ type EventFilter = Record<string, Arrayable<string>>;
120
+ type Arrayable<T> = T | Array<T>;
121
+
122
+ declare abstract class EventDriver {
123
+ _handler?: EventHandler;
124
+ get handler(): EventHandler;
125
+ /**
126
+ * Mount an event handler
127
+ *
128
+ * @param handler
129
+ */
130
+ mountEventHandler(handler: EventHandler): this;
131
+ /**
132
+ * Dispatch an event
133
+ *
134
+ * @param e
135
+ */
136
+ dispatchEvent(e: any): this;
137
+ }
138
+
139
+ /**
140
+ * The Client
141
+ *
142
+ * The Flatfile PubSub Client is just a simple event subscriber. It can
143
+ * receive events from any PubSub driver. The default drivers are:
144
+ *
145
+ * - Webhook (for simply processing events sent to URL)
146
+ * - Websocket (for subscribing real time on an HTTP2 connection)
147
+ * - Serverless (for stateless invocations via AWS Lambda or similar)
148
+ *
149
+ * Once an event is received, it is routed to any awaiting listeners which
150
+ * are added with `addEventListener()` or its alias `on()`.
151
+ *
152
+ * Flatfile events follow a standard structure and event listeners can use
153
+ * any of the following syntaxes to react to events within Flatfile.
154
+ *
155
+ * // listen to an event
156
+ * addEventListener('entity:topic')
157
+ *
158
+ * // listen to an event on a specific namespace
159
+ * addEventListener('entity:topic@namespace')
160
+ *
161
+ * // listen to a specific context on a namespace
162
+ * addEventListener('entity:topic@namespace?context=us_sp_89234oihsdo')
163
+ *
164
+ * // filter by any
165
+ * addEventListener('entity:topic@namespace?')
166
+ *
167
+ */
168
+
169
+ declare class Client extends EventHandler {
170
+ /**
171
+ * Subscribe to events only within a certain namespace.
172
+ *
173
+ * @param namespace
174
+ * @param cb
175
+ */
176
+ namespace(namespace: string | string[], cb?: SubFn): Client;
177
+ /**
178
+ * Filter by namespace
179
+ *
180
+ * @param filter
181
+ * @param cb
182
+ */
183
+ filter(filter: EventFilter, cb?: SubFn): Client;
184
+ setupFunctions: Array<(...args: any[]) => void>;
185
+ beforeMount(cb: SubFn): void;
186
+ /**
187
+ * Start subscribing to events
188
+ *
189
+ * @param cb
190
+ */
191
+ static create(cb: SubFn): Client;
192
+ /**
193
+ * Mount this client using an acceptable Event Driver
194
+ */
195
+ mount(driver: EventDriver): this;
196
+ }
197
+ type SubFn = (client: Client) => void;
198
+
199
+ declare class PollingEventDriver extends EventDriver {
200
+ private options;
201
+ constructor(options: {
202
+ interval: number;
203
+ environmentId: string;
204
+ });
205
+ start(): void;
206
+ shutdown(): void;
207
+ }
208
+
209
+ /**
210
+ * Flatfile's Virtual Machine is stateless / serverless. So when a new event
211
+ * is handled, it will just call `handle(event)`.
212
+ */
213
+ declare class FlatfileVirtualMachine extends EventDriver {
214
+ /**
215
+ * This method is triggered from within the Flatfile Core VM Runner. This
216
+ * EventDriver does not have to listen for events because this method will
217
+ * be invoked as necessary.
218
+ *
219
+ * @param event
220
+ */
221
+ handle(event: FlatfileEvent): void;
222
+ }
223
+
224
+ type IConfigureOptions = {
225
+ environmentId?: string;
226
+ createSpaces?: boolean;
227
+ };
228
+ declare const useConfigure: (agent: Agent, options?: IConfigureOptions) => (client: Client) => Promise<void>;
229
+
230
+ export { Arrayable, AuthenticatedClient, Client, EventCallback, EventFilter, EventHandler, FlatfileEvent, FlatfileVirtualMachine, PollingEventDriver, useConfigure };