@adobe/spacecat-shared-utils 1.20.8 → 1.21.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-utils-v1.21.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.20.8...@adobe/spacecat-shared-utils-v1.21.0) (2024-10-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * tracing fetch ([#409](https://github.com/adobe/spacecat-shared/issues/409)) ([6579a26](https://github.com/adobe/spacecat-shared/commit/6579a26d917db0bb8dc3baa2e19a0dfee8db5fc8))
7
+
1
8
  # [@adobe/spacecat-shared-utils-v1.20.8](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.20.7...@adobe/spacecat-shared-utils-v1.20.8) (2024-10-21)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.20.8",
3
+ "version": "1.21.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
@@ -47,7 +47,7 @@
47
47
  "@adobe/fetch": "4.1.9",
48
48
  "@aws-sdk/client-s3": "3.675.0",
49
49
  "@aws-sdk/client-sqs": "3.675.0",
50
- "aws-xray-sdk": "3.10.1",
51
- "@json2csv/plainjs": "7.0.6"
50
+ "@json2csv/plainjs": "7.0.6",
51
+ "aws-xray-sdk": "3.10.1"
52
52
  }
53
53
  }
@@ -0,0 +1,14 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { context as h2, h1 } from '@adobe/fetch';
13
+
14
+ export const { fetch } = process.env.HELIX_FETCH_FORCE_HTTP1 ? h1() : h2();
package/src/index.d.ts CHANGED
@@ -10,6 +10,8 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import { Request, RequestOptions, Response } from '@adobe/fetch';
14
+
13
15
  /** UTILITY FUNCTIONS */
14
16
  export function arrayEquals<T>(a: T[], b: T[]): boolean;
15
17
 
@@ -159,3 +161,7 @@ export function storeMetrics(content: object, config: object, context: object):
159
161
 
160
162
  export function s3Wrapper(fn: (request: object, context: object) => Promise<Response>):
161
163
  (request: object, context: object) => Promise<Response>;
164
+
165
+ export function fetch(url: string|Request, options?: RequestOptions): Promise<Response>;
166
+
167
+ export function tracingFetch(url: string|Request, options?: RequestOptions): Promise<Response>;
package/src/index.js CHANGED
@@ -53,3 +53,6 @@ export {
53
53
  export { getStoredMetrics, storeMetrics } from './metrics-store.js';
54
54
 
55
55
  export { s3Wrapper } from './s3.js';
56
+
57
+ export { fetch } from './adobe-fetch.js';
58
+ export { tracingFetch } from './tracing-fetch.js';
@@ -0,0 +1,43 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import AWSXRay from 'aws-xray-sdk';
13
+
14
+ import { fetch as adobeFetch } from './adobe-fetch.js';
15
+
16
+ export async function tracingFetch(url, options = {}) {
17
+ const parentSegment = AWSXRay.getSegment();
18
+
19
+ if (!parentSegment) {
20
+ return adobeFetch(url, options);
21
+ }
22
+
23
+ const method = options.method || 'GET';
24
+ const subsegment = parentSegment.addNewSubsegment(`HTTP ${method} ${url}`);
25
+
26
+ try {
27
+ subsegment.addAnnotation('url', url);
28
+ subsegment.addAnnotation('method', method);
29
+
30
+ const response = await adobeFetch(url, options);
31
+
32
+ subsegment.addMetadata('statusCode', response.status);
33
+
34
+ subsegment.close();
35
+
36
+ return response;
37
+ } catch (error) {
38
+ subsegment.addError(error);
39
+ subsegment.close();
40
+
41
+ throw error;
42
+ }
43
+ }