@adobe/spacecat-shared-http-utils 1.2.1 → 1.3.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-http-utils-v1.3.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.3.0...@adobe/spacecat-shared-http-utils-v1.3.1) (2024-05-29)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Refactor createResponse Function to Handle Non-JSON Content-Types ([#244](https://github.com/adobe/spacecat-shared/issues/244)) ([7675f35](https://github.com/adobe/spacecat-shared/commit/7675f35c9ae8644b43b489136da66c21c2d7393b))
7
+
8
+ # [@adobe/spacecat-shared-http-utils-v1.3.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.2.1...@adobe/spacecat-shared-http-utils-v1.3.0) (2024-05-21)
9
+
10
+
11
+ ### Features
12
+
13
+ * introduce google client ([#219](https://github.com/adobe/spacecat-shared/issues/219)) ([71eeb64](https://github.com/adobe/spacecat-shared/commit/71eeb64a7efe2037b5d40f0c62668b9eaf41aeb8))
14
+
1
15
  # [@adobe/spacecat-shared-http-utils-v1.2.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.2.0...@adobe/spacecat-shared-http-utils-v1.2.1) (2024-05-03)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-http-utils",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "Shared modules of the Spacecat Services - HTTP Utils",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.d.ts CHANGED
@@ -22,3 +22,5 @@ export declare function badRequest(message?: string, headers?: object): Response
22
22
  export declare function notFound(message?: string, headers?: object): Response;
23
23
 
24
24
  export declare function internalServerError(message?: string, headers?: object): Response;
25
+
26
+ export declare function found(location: string): Response;
package/src/index.js CHANGED
@@ -12,22 +12,39 @@
12
12
 
13
13
  import { Response } from '@adobe/fetch';
14
14
 
15
+ const HEADER_CONTENT_TYPE = 'content-type';
16
+ const HEADER_ERROR = 'x-error';
17
+
18
+ const CONTENT_TYPE_JSON = 'application/json';
19
+
15
20
  /**
16
- * Creates a response with a JSON body. Defaults to 200 status.
17
- * @param {object} body - JSON body.
18
- * @param {number} status - Optional status code.
19
- * @param {object} headers - Optional headers.
21
+ * Creates a response with a JSON body if the content-type is JSON. Defaults to 200 status.
22
+ * If a header is already defined and has a different content-type, it is handled accordingly.
23
+ * @param {object} body - Response body.
24
+ * @param {number} [status=200] - Optional status code.
25
+ * @param {object} [headers={}] - Optional headers.
20
26
  * @return {Response} Response.
21
27
  */
22
28
  export function createResponse(body, status = 200, headers = {}) {
23
- return new Response(
24
- body === '' ? '' : JSON.stringify(body),
25
- {
26
- headers: { 'content-type': 'application/json; charset=utf-8', ...headers },
27
- status,
28
- },
29
- );
29
+ let responseBody = body;
30
+
31
+ // Check if headers already contain a 'content-type' key
32
+ if (!headers[HEADER_CONTENT_TYPE]) {
33
+ // Set content-type to JSON if not already set
34
+ Object.assign(headers, { [HEADER_CONTENT_TYPE]: `${CONTENT_TYPE_JSON}; charset=utf-8` });
35
+ }
36
+
37
+ // Stringify body if content-type is JSON
38
+ if (headers[HEADER_CONTENT_TYPE].includes(CONTENT_TYPE_JSON)) {
39
+ responseBody = body === '' ? '' : JSON.stringify(body);
40
+ }
41
+
42
+ return new Response(responseBody, {
43
+ headers,
44
+ status,
45
+ });
30
46
  }
47
+
31
48
  export function ok(body = '') {
32
49
  return createResponse(body, 200);
33
50
  }
@@ -40,23 +57,29 @@ export function noContent(headers = {}) {
40
57
  return createResponse('', 204, headers);
41
58
  }
42
59
 
60
+ export function found(location, body = '') {
61
+ return createResponse(body, 302, {
62
+ Location: location,
63
+ });
64
+ }
65
+
43
66
  export function badRequest(message = 'bad request', headers = {}) {
44
67
  return createResponse({ message }, 400, {
45
- 'x-error': message,
68
+ [HEADER_ERROR]: message,
46
69
  ...headers,
47
70
  });
48
71
  }
49
72
 
50
73
  export function notFound(message = 'not found', headers = {}) {
51
74
  return createResponse({ message }, 404, {
52
- 'x-error': message,
75
+ [HEADER_ERROR]: message,
53
76
  ...headers,
54
77
  });
55
78
  }
56
79
 
57
80
  export function internalServerError(message = 'internal server error', headers = {}) {
58
81
  return createResponse({ message }, 500, {
59
- 'x-error': message,
82
+ [HEADER_ERROR]: message,
60
83
  ...headers,
61
84
  });
62
85
  }