@adobe/spacecat-shared-rum-api-client 2.5.4 → 2.6.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-rum-api-client-v2.6.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.5.4...@adobe/spacecat-shared-rum-api-client-v2.6.0) (2024-08-02)
2
+
3
+
4
+ ### Features
5
+
6
+ * multi-query support for rum-client ([#311](https://github.com/adobe/spacecat-shared/issues/311)) ([c3ac6a2](https://github.com/adobe/spacecat-shared/commit/c3ac6a20396874d0abffdcdcd50406e9718a426b))
7
+
1
8
  # [@adobe/spacecat-shared-rum-api-client-v2.5.4](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.5.3...@adobe/spacecat-shared-rum-api-client-v2.5.4) (2024-08-01)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-rum-api-client",
3
- "version": "2.5.4",
3
+ "version": "2.6.0",
4
4
  "description": "Shared modules of the Spacecat Services - Rum API client",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.d.ts CHANGED
@@ -21,26 +21,40 @@ export interface RUMAPIOptions {
21
21
 
22
22
  export default class RUMAPIClient {
23
23
  /**
24
- * Static factory method to create an instance of RUMAPIClient.
25
- * @param {UniversalContext} context - An object containing the AWS Lambda context information
26
- * @returns An instance of RUMAPIClient.
27
- * @remarks This method is designed to create a new instance from an AWS Lambda context.
28
- * The created instance is stored in the Lambda context, and subsequent calls to
29
- * this method will return the singleton instance if previously created.
30
- */
24
+ * Static factory method to create an instance of RUMAPIClient.
25
+ * @param {UniversalContext} context - An object containing the AWS Lambda context information
26
+ * @returns An instance of RUMAPIClient.
27
+ * @remarks This method is designed to create a new instance from an AWS Lambda context.
28
+ * The created instance is stored in the Lambda context, and subsequent calls to
29
+ * this method will return the singleton instance if previously created.
30
+ */
31
31
  static createFrom(context: UniversalContext): RUMAPIClient;
32
32
 
33
33
  /**
34
- * Constructor for creating an instance of RUMAPIClient.
35
- */
34
+ * Constructor for creating an instance of RUMAPIClient.
35
+ */
36
36
  constructor();
37
37
 
38
38
  /**
39
- * Asynchronous method to run queries against RUM Bundler API.
40
- * @param {string} query - Name of the query to run.
41
- * @param {RUMAPIOptions} opts - A object containing options for query to run.
42
- * @returns A Promise resolving to an object with the query results.
43
- * @remarks See the README.md for the available queries.
44
- */
39
+ * Asynchronous method to run queries against RUM Bundler API.
40
+ * @param {string} query - Name of the query to run.
41
+ * @param {RUMAPIOptions} opts - A object containing options for query to run.
42
+ * @returns A Promise resolving to an object with the query results.
43
+ * @remarks See the README.md for the available queries.
44
+ */
45
45
  query(query: string, opts?: RUMAPIOptions): Promise<object>;
46
+
47
+ /**
48
+ * Asynchronous method to run multiple queries against the data fetched from RUM Bundler API.
49
+ *
50
+ * This method makes a single call to the RUM Bundler API to fetch the raw data, then applies
51
+ * all the requested queries to this raw data. The results are returned in an object where each
52
+ * key corresponds to a query name and each value contains the result of that query.
53
+ *
54
+ * @param {string[]} queries - An array of query names to execute.
55
+ * @param {RUMAPIOptions} [opts] - Optional object containing options for the queries.
56
+ * @returns {Promise<object>} A Promise that resolves to an object where each key is the name
57
+ * of a query, and each value is the result of that query.
58
+ */
59
+ queryMulti(queries: string[], opts?: RUMAPIOptions): Promise<object[]>;
46
60
  }
package/src/index.js CHANGED
@@ -49,4 +49,41 @@ export default class RUMAPIClient {
49
49
  throw new Error(`Query '${query}' failed. Opts: ${JSON.stringify(opts)}. Reason: ${e.message}`);
50
50
  }
51
51
  }
52
+
53
+ // eslint-disable-next-line class-methods-use-this
54
+ async queryMulti(queries, opts) {
55
+ const queryHandlers = [];
56
+ const allCheckpoints = new Set();
57
+
58
+ for (const query of queries) {
59
+ const { handler, checkpoints = [] } = HANDLERS[query] || {};
60
+
61
+ if (!handler) {
62
+ throw new Error(`Unknown query: ${query}`);
63
+ }
64
+
65
+ queryHandlers.push({ query, handler });
66
+ checkpoints.forEach((checkpoint) => allCheckpoints.add(checkpoint));
67
+ }
68
+
69
+ try {
70
+ // Fetch bundles with deduplicated checkpoints
71
+ const bundles = await fetchBundles({
72
+ ...opts,
73
+ checkpoints: [...allCheckpoints],
74
+ });
75
+
76
+ const results = {};
77
+
78
+ // Execute each query handler sequentially
79
+ for (const { query, handler } of queryHandlers) {
80
+ // eslint-disable-next-line no-await-in-loop
81
+ results[query] = await handler(bundles);
82
+ }
83
+
84
+ return results;
85
+ } catch (e) {
86
+ throw new Error(`Multi query failed. Queries: ${JSON.stringify(queries)}, Opts: ${JSON.stringify(opts)}. Reason: ${e.message}`);
87
+ }
88
+ }
52
89
  }