@flatfile/listener 0.3.13 → 0.3.15

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
@@ -32,7 +32,7 @@ interface GetData extends Function {
32
32
  then<TResult1 = any, TResult2 = any>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
33
33
  }
34
34
  declare class FlatfileEvent extends AuthenticatedClient {
35
- private readonly src;
35
+ readonly src: Event;
36
36
  /**
37
37
  * Event ID from the API
38
38
  *
@@ -56,6 +56,7 @@ declare class FlatfileEvent extends AuthenticatedClient {
56
56
  readonly payload: any;
57
57
  readonly cache: EventCache;
58
58
  readonly namespace: string[];
59
+ readonly createdAt?: Date;
59
60
  /**
60
61
  * Fetch record data from Flatfile API via the event's dataUrl
61
62
  *
@@ -73,6 +74,14 @@ declare class FlatfileEvent extends AuthenticatedClient {
73
74
  private afterAllCallbacks;
74
75
  afterAll<T>(callback: () => T, cacheKey?: string): void;
75
76
  update(records: RecordsWithLinks): Promise<void>;
77
+ /**
78
+ * Fetch the Secrets as indicated by this event context
79
+ *
80
+ * @param key - The name of the secret to fetch
81
+ * @param options - (Optional) environmentId and spaceId to override event context
82
+ *
83
+ * @returns The value of the secret (usually a credential or token)
84
+ */
76
85
  secrets(key: string, options?: {
77
86
  environmentId?: string;
78
87
  spaceId?: string;
package/dist/index.js CHANGED
@@ -152,12 +152,22 @@ CrossEnvConfig._aliases = /* @__PURE__ */ new Map();
152
152
 
153
153
  // src/events/authenticated.client.ts
154
154
  var import_axios = __toESM(require("axios"));
155
+
156
+ // src/utils/helpers.ts
157
+ var ensureSingleTrailingSlash = (url) => {
158
+ while (url.endsWith("/")) {
159
+ url = url.slice(0, -1);
160
+ }
161
+ return url + "/";
162
+ };
163
+
164
+ // src/events/authenticated.client.ts
155
165
  var AuthenticatedClient = class {
156
166
  constructor(accessToken, apiUrl) {
157
167
  const FLATFILE_API_URL = CrossEnvConfig.get("AGENT_INTERNAL_URL") || "http://localhost:3000";
158
168
  const bearerToken = CrossEnvConfig.get("FLATFILE_BEARER_TOKEN");
159
169
  this._accessToken = accessToken || bearerToken || "...";
160
- this._apiUrl = apiUrl || FLATFILE_API_URL;
170
+ this._apiUrl = apiUrl || FLATFILE_API_URL ? ensureSingleTrailingSlash(apiUrl || FLATFILE_API_URL) : void 0;
161
171
  }
162
172
  async fetch(url, options) {
163
173
  const headers = {
@@ -165,9 +175,12 @@ var AuthenticatedClient = class {
165
175
  "x-disable-hooks": "true"
166
176
  };
167
177
  const axiosInstance = import_axios.default.create({
168
- headers
178
+ headers,
179
+ validateStatus: (status) => {
180
+ return status >= 200 && status <= 399;
181
+ }
169
182
  });
170
- const fetchUrl = this._apiUrl + "/" + url;
183
+ const fetchUrl = this._apiUrl + url;
171
184
  const config = {
172
185
  url: fetchUrl,
173
186
  method: "GET",
@@ -253,6 +266,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
253
266
  this.origin = src.origin || {};
254
267
  this.action = src.context?.actionName || "";
255
268
  this.namespace = src.namespaces || [];
269
+ this.createdAt = src.createdAt || void 0;
256
270
  const data = async (options) => this.fetchData(options);
257
271
  data.then = (onfulfilled, onrejected) => {
258
272
  return this.data().then(onfulfilled, onrejected);
@@ -303,14 +317,29 @@ var FlatfileEvent = class extends AuthenticatedClient {
303
317
  data: records
304
318
  });
305
319
  }
320
+ /**
321
+ * Fetch the Secrets as indicated by this event context
322
+ *
323
+ * @param key - The name of the secret to fetch
324
+ * @param options - (Optional) environmentId and spaceId to override event context
325
+ *
326
+ * @returns The value of the secret (usually a credential or token)
327
+ */
306
328
  async secrets(key, options) {
307
- const environmentId = options?.environmentId || this.context.environmentId;
308
- const secrets = await this.cache.init("secrets", async () => {
309
- const secretsResponse = await this.fetch(
310
- `v1/secrets?environmentId=${environmentId}${options?.spaceId ? `&spaceId=${options.spaceId}` : ""}`
311
- );
329
+ const environmentId = options?.environmentId || this.context.environmentId || "";
330
+ const spaceId = options?.spaceId || this.context.spaceId || "";
331
+ if (!environmentId) {
332
+ throw new Error("environmentId is required to fetch secrets");
333
+ }
334
+ let getSecrets = `v1/secrets?environmentId=${environmentId}`;
335
+ if (spaceId) {
336
+ getSecrets += `&spaceId=${spaceId}`;
337
+ }
338
+ const secretCacheKey = `secrets:${environmentId}${spaceId && `:${spaceId}`}`;
339
+ const secrets = await this.cache.init(secretCacheKey, async () => {
340
+ const secretsResponse = await this.fetch(getSecrets);
312
341
  const SecretMap = /* @__PURE__ */ new Map();
313
- secretsResponse.forEach((secret) => {
342
+ secretsResponse?.forEach((secret) => {
314
343
  SecretMap.set(secret.name, secret.value);
315
344
  });
316
345
  return SecretMap;
package/dist/index.mjs CHANGED
@@ -109,12 +109,22 @@ CrossEnvConfig._aliases = /* @__PURE__ */ new Map();
109
109
 
110
110
  // src/events/authenticated.client.ts
111
111
  import axios from "axios";
112
+
113
+ // src/utils/helpers.ts
114
+ var ensureSingleTrailingSlash = (url) => {
115
+ while (url.endsWith("/")) {
116
+ url = url.slice(0, -1);
117
+ }
118
+ return url + "/";
119
+ };
120
+
121
+ // src/events/authenticated.client.ts
112
122
  var AuthenticatedClient = class {
113
123
  constructor(accessToken, apiUrl) {
114
124
  const FLATFILE_API_URL = CrossEnvConfig.get("AGENT_INTERNAL_URL") || "http://localhost:3000";
115
125
  const bearerToken = CrossEnvConfig.get("FLATFILE_BEARER_TOKEN");
116
126
  this._accessToken = accessToken || bearerToken || "...";
117
- this._apiUrl = apiUrl || FLATFILE_API_URL;
127
+ this._apiUrl = apiUrl || FLATFILE_API_URL ? ensureSingleTrailingSlash(apiUrl || FLATFILE_API_URL) : void 0;
118
128
  }
119
129
  async fetch(url, options) {
120
130
  const headers = {
@@ -122,9 +132,12 @@ var AuthenticatedClient = class {
122
132
  "x-disable-hooks": "true"
123
133
  };
124
134
  const axiosInstance = axios.create({
125
- headers
135
+ headers,
136
+ validateStatus: (status) => {
137
+ return status >= 200 && status <= 399;
138
+ }
126
139
  });
127
- const fetchUrl = this._apiUrl + "/" + url;
140
+ const fetchUrl = this._apiUrl + url;
128
141
  const config = {
129
142
  url: fetchUrl,
130
143
  method: "GET",
@@ -210,6 +223,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
210
223
  this.origin = src.origin || {};
211
224
  this.action = src.context?.actionName || "";
212
225
  this.namespace = src.namespaces || [];
226
+ this.createdAt = src.createdAt || void 0;
213
227
  const data = async (options) => this.fetchData(options);
214
228
  data.then = (onfulfilled, onrejected) => {
215
229
  return this.data().then(onfulfilled, onrejected);
@@ -260,14 +274,29 @@ var FlatfileEvent = class extends AuthenticatedClient {
260
274
  data: records
261
275
  });
262
276
  }
277
+ /**
278
+ * Fetch the Secrets as indicated by this event context
279
+ *
280
+ * @param key - The name of the secret to fetch
281
+ * @param options - (Optional) environmentId and spaceId to override event context
282
+ *
283
+ * @returns The value of the secret (usually a credential or token)
284
+ */
263
285
  async secrets(key, options) {
264
- const environmentId = options?.environmentId || this.context.environmentId;
265
- const secrets = await this.cache.init("secrets", async () => {
266
- const secretsResponse = await this.fetch(
267
- `v1/secrets?environmentId=${environmentId}${options?.spaceId ? `&spaceId=${options.spaceId}` : ""}`
268
- );
286
+ const environmentId = options?.environmentId || this.context.environmentId || "";
287
+ const spaceId = options?.spaceId || this.context.spaceId || "";
288
+ if (!environmentId) {
289
+ throw new Error("environmentId is required to fetch secrets");
290
+ }
291
+ let getSecrets = `v1/secrets?environmentId=${environmentId}`;
292
+ if (spaceId) {
293
+ getSecrets += `&spaceId=${spaceId}`;
294
+ }
295
+ const secretCacheKey = `secrets:${environmentId}${spaceId && `:${spaceId}`}`;
296
+ const secrets = await this.cache.init(secretCacheKey, async () => {
297
+ const secretsResponse = await this.fetch(getSecrets);
269
298
  const SecretMap = /* @__PURE__ */ new Map();
270
- secretsResponse.forEach((secret) => {
299
+ secretsResponse?.forEach((secret) => {
271
300
  SecretMap.set(secret.name, secret.value);
272
301
  });
273
302
  return SecretMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flatfile/listener",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
4
4
  "description": "A PubSub Listener for configuring and using Flatfile",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",