@flatfile/listener 0.3.3 → 0.3.5

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/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
+ import { Event, RecordsWithLinks } from '@flatfile/api/api';
1
2
  import { Flatfile } from '@flatfile/api';
2
3
 
3
4
  declare class AuthenticatedClient {
4
5
  _accessToken?: string;
5
6
  _apiUrl?: string;
6
7
  constructor(accessToken?: string, apiUrl?: string);
7
- fetch(url: string): Promise<any>;
8
+ fetch(url: string, options?: any): Promise<any>;
8
9
  /**
9
10
  *
10
11
  * @deprecated use @flatfile/cross-env-config instead
@@ -42,11 +43,12 @@ declare class FlatfileEvent extends AuthenticatedClient {
42
43
  readonly topic: string;
43
44
  readonly domain: string;
44
45
  readonly target: string;
46
+ readonly origin: object;
45
47
  readonly action: string;
46
48
  readonly context: any;
47
49
  readonly payload: any;
48
50
  readonly cache: EventCache;
49
- constructor(src: Flatfile.Event, accessToken?: string, apiUrl?: string);
51
+ constructor(src: Event, accessToken?: string, apiUrl?: string);
50
52
  /**
51
53
  * Should return either event body if expanded already or fetch data from the
52
54
  * signed callback URL
@@ -56,6 +58,7 @@ declare class FlatfileEvent extends AuthenticatedClient {
56
58
  get data(): Promise<any>;
57
59
  private afterAllCallbacks;
58
60
  afterAll<T>(callback: () => T, cacheKey?: string): void;
61
+ update(records: RecordsWithLinks): Promise<void>;
59
62
  }
60
63
  type EventCallback = (evt: FlatfileEvent) => void;
61
64
 
package/dist/index.js CHANGED
@@ -113,6 +113,7 @@ var CrossEnvConfig = class {
113
113
  if (typeof process === "object" && typeof process.env === "object") {
114
114
  values.push(process.env[prop]);
115
115
  }
116
+ values.push(...this.checkForBrowserVariables(prop));
116
117
  const foundValue = values.find((v) => v !== void 0);
117
118
  if (foundValue !== void 0) {
118
119
  return foundValue;
@@ -123,6 +124,28 @@ var CrossEnvConfig = class {
123
124
  }
124
125
  return void 0;
125
126
  }
127
+ /**
128
+ * Internal function for checking for stored variables in a browser-like environment
129
+ *
130
+ * @param prop
131
+ * @private
132
+ */
133
+ static checkForBrowserVariables(prop) {
134
+ let values = [];
135
+ if (typeof window === "object") {
136
+ const windowValue = window[`CROSSENV_${prop}`];
137
+ if (windowValue !== void 0) {
138
+ values.push(windowValue);
139
+ }
140
+ if (typeof sessionStorage === "object") {
141
+ const storedValue = sessionStorage.getItem(`CROSSENV_${prop}`);
142
+ if (storedValue !== null) {
143
+ values.push(storedValue);
144
+ }
145
+ }
146
+ }
147
+ return values;
148
+ }
126
149
  };
127
150
  CrossEnvConfig._overrides = /* @__PURE__ */ new Map();
128
151
  CrossEnvConfig._aliases = /* @__PURE__ */ new Map();
@@ -136,14 +159,25 @@ var AuthenticatedClient = class {
136
159
  this._accessToken = accessToken || bearerToken || "...";
137
160
  this._apiUrl = apiUrl || FLATFILE_API_URL;
138
161
  }
139
- fetch(url) {
162
+ async fetch(url, options) {
140
163
  const headers = {
141
164
  Authorization: `Bearer ${this._accessToken}`,
142
165
  "x-disable-hooks": "true"
143
166
  };
144
- const fetchUrl = this._apiUrl + "/" + url;
145
- return import_axios.default.get(fetchUrl, { headers }).then((resp) => resp.data.data).catch((err) => {
167
+ const axiosInstance = import_axios.default.create({
168
+ headers
146
169
  });
170
+ const fetchUrl = this._apiUrl + "/" + url;
171
+ const config = {
172
+ url: fetchUrl,
173
+ method: "GET",
174
+ ...options
175
+ };
176
+ try {
177
+ const resp = await axiosInstance(config);
178
+ return resp.data.data;
179
+ } catch (err) {
180
+ }
147
181
  }
148
182
  /**
149
183
  *
@@ -215,6 +249,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
215
249
  this.context = src.context;
216
250
  this.payload = src.payload;
217
251
  this.target = src.target || "";
252
+ this.origin = src.origin || {};
218
253
  this.action = src.context?.actionName || "";
219
254
  }
220
255
  /**
@@ -236,6 +271,20 @@ var FlatfileEvent = class extends AuthenticatedClient {
236
271
  this.afterAllCallbacks.set(key, callback);
237
272
  }
238
273
  }
274
+ async update(records) {
275
+ if (!this.src.dataUrl) {
276
+ throw new Error("Cannot set data on an event without a dataUrl");
277
+ }
278
+ const prepRecords = records.map((record) => {
279
+ record.messages?.map((message) => {
280
+ delete message.source;
281
+ });
282
+ });
283
+ await this.fetch(this.src.dataUrl, {
284
+ method: "POST",
285
+ data: JSON.stringify(prepRecords)
286
+ });
287
+ }
239
288
  };
240
289
 
241
290
  // ../../node_modules/wildcard-match/build/index.es.mjs
@@ -458,7 +507,9 @@ var EventHandler = class extends AuthenticatedClient {
458
507
  }
459
508
  }
460
509
  await this.trigger(event, true);
461
- await event.afterAllCallbacks.forEach(async (cb) => cb(event));
510
+ for (const [_key, cb] of event.afterAllCallbacks) {
511
+ await cb(event);
512
+ }
462
513
  event.cache.delete();
463
514
  }
464
515
  /**
package/dist/index.mjs CHANGED
@@ -70,6 +70,7 @@ var CrossEnvConfig = class {
70
70
  if (typeof process === "object" && typeof process.env === "object") {
71
71
  values.push(process.env[prop]);
72
72
  }
73
+ values.push(...this.checkForBrowserVariables(prop));
73
74
  const foundValue = values.find((v) => v !== void 0);
74
75
  if (foundValue !== void 0) {
75
76
  return foundValue;
@@ -80,6 +81,28 @@ var CrossEnvConfig = class {
80
81
  }
81
82
  return void 0;
82
83
  }
84
+ /**
85
+ * Internal function for checking for stored variables in a browser-like environment
86
+ *
87
+ * @param prop
88
+ * @private
89
+ */
90
+ static checkForBrowserVariables(prop) {
91
+ let values = [];
92
+ if (typeof window === "object") {
93
+ const windowValue = window[`CROSSENV_${prop}`];
94
+ if (windowValue !== void 0) {
95
+ values.push(windowValue);
96
+ }
97
+ if (typeof sessionStorage === "object") {
98
+ const storedValue = sessionStorage.getItem(`CROSSENV_${prop}`);
99
+ if (storedValue !== null) {
100
+ values.push(storedValue);
101
+ }
102
+ }
103
+ }
104
+ return values;
105
+ }
83
106
  };
84
107
  CrossEnvConfig._overrides = /* @__PURE__ */ new Map();
85
108
  CrossEnvConfig._aliases = /* @__PURE__ */ new Map();
@@ -93,14 +116,25 @@ var AuthenticatedClient = class {
93
116
  this._accessToken = accessToken || bearerToken || "...";
94
117
  this._apiUrl = apiUrl || FLATFILE_API_URL;
95
118
  }
96
- fetch(url) {
119
+ async fetch(url, options) {
97
120
  const headers = {
98
121
  Authorization: `Bearer ${this._accessToken}`,
99
122
  "x-disable-hooks": "true"
100
123
  };
101
- const fetchUrl = this._apiUrl + "/" + url;
102
- return axios.get(fetchUrl, { headers }).then((resp) => resp.data.data).catch((err) => {
124
+ const axiosInstance = axios.create({
125
+ headers
103
126
  });
127
+ const fetchUrl = this._apiUrl + "/" + url;
128
+ const config = {
129
+ url: fetchUrl,
130
+ method: "GET",
131
+ ...options
132
+ };
133
+ try {
134
+ const resp = await axiosInstance(config);
135
+ return resp.data.data;
136
+ } catch (err) {
137
+ }
104
138
  }
105
139
  /**
106
140
  *
@@ -172,6 +206,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
172
206
  this.context = src.context;
173
207
  this.payload = src.payload;
174
208
  this.target = src.target || "";
209
+ this.origin = src.origin || {};
175
210
  this.action = src.context?.actionName || "";
176
211
  }
177
212
  /**
@@ -193,6 +228,20 @@ var FlatfileEvent = class extends AuthenticatedClient {
193
228
  this.afterAllCallbacks.set(key, callback);
194
229
  }
195
230
  }
231
+ async update(records) {
232
+ if (!this.src.dataUrl) {
233
+ throw new Error("Cannot set data on an event without a dataUrl");
234
+ }
235
+ const prepRecords = records.map((record) => {
236
+ record.messages?.map((message) => {
237
+ delete message.source;
238
+ });
239
+ });
240
+ await this.fetch(this.src.dataUrl, {
241
+ method: "POST",
242
+ data: JSON.stringify(prepRecords)
243
+ });
244
+ }
196
245
  };
197
246
 
198
247
  // ../../node_modules/wildcard-match/build/index.es.mjs
@@ -415,7 +464,9 @@ var EventHandler = class extends AuthenticatedClient {
415
464
  }
416
465
  }
417
466
  await this.trigger(event, true);
418
- await event.afterAllCallbacks.forEach(async (cb) => cb(event));
467
+ for (const [_key, cb] of event.afterAllCallbacks) {
468
+ await cb(event);
469
+ }
419
470
  event.cache.delete();
420
471
  }
421
472
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flatfile/listener",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "A PubSub Listener for configuring and using Flatfile",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",