@adobe/spacecat-shared-brand-client 1.0.1 → 1.1.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,17 @@
1
+ # [@adobe/spacecat-shared-brand-client-v1.1.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-brand-client-v1.0.2...@adobe/spacecat-shared-brand-client-v1.1.0) (2025-03-20)
2
+
3
+
4
+ ### Features
5
+
6
+ * ims user validate ([#668](https://github.com/adobe/spacecat-shared/issues/668)) ([b3db9ab](https://github.com/adobe/spacecat-shared/commit/b3db9abb154277376ff34213c556e3b7491e696b))
7
+
8
+ # [@adobe/spacecat-shared-brand-client-v1.0.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-brand-client-v1.0.1...@adobe/spacecat-shared-brand-client-v1.0.2) (2025-03-06)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * error handling and dependencies ([#650](https://github.com/adobe/spacecat-shared/issues/650)) ([3dc4e03](https://github.com/adobe/spacecat-shared/commit/3dc4e03b045b324ea01aff82e177fd7567822f51))
14
+
1
15
  # [@adobe/spacecat-shared-brand-client-v1.0.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-brand-client-v1.0.0...@adobe/spacecat-shared-brand-client-v1.0.1) (2025-03-06)
2
16
 
3
17
 
package/README.md CHANGED
@@ -23,7 +23,7 @@ const config = {
23
23
  apiBaseUrl: '<API_BASE_URL>',
24
24
  };
25
25
 
26
- const client = new BrandClient(config, console);
26
+ const client = new BrandClient(config, log);
27
27
  ```
28
28
 
29
29
  ### Creating an instance from Helix UniversalContext
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-brand-client",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Shared modules of the Spacecat Services - Brand Client",
5
5
  "type": "module",
6
6
  "engines": {
@@ -36,12 +36,12 @@
36
36
  "dependencies": {
37
37
  "@adobe/helix-universal": "5.0.8",
38
38
  "@adobe/spacecat-shared-ims-client": "1.5.6",
39
- "@adobe/spacecat-shared-utils": "https://gitpkg.now.sh/adobe/spacecat-shared/packages/spacecat-shared-utils?3982de9615518c35225d04d338c4ab6a9b492e83"
39
+ "@adobe/spacecat-shared-utils": "1.34.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "chai": "5.2.0",
43
43
  "chai-as-promised": "8.0.1",
44
- "mocha": "10.3.0",
44
+ "mocha": "11.1.0",
45
45
  "mocha-multi-reporters": "1.5.1",
46
46
  "nock": "14.0.1",
47
47
  "sinon": "19.0.2",
package/src/index.js CHANGED
@@ -14,6 +14,10 @@ import {
14
14
  } from '@adobe/spacecat-shared-utils';
15
15
  import { ImsClient } from '@adobe/spacecat-shared-ims-client';
16
16
 
17
+ const HTTP_BAD_REQUEST = 400;
18
+ const HTTP_NOT_FOUND = 404;
19
+ const HTTP_INTERNAL_SERVER_ERROR = 500;
20
+
17
21
  const PUBLISHED_BRANDS_FILTER = 'roles=BRAND&itemFilter=publishedBrands';
18
22
  const API_GET_BRANDS = `/api/v1/libraries?${PUBLISHED_BRANDS_FILTER}`;
19
23
  const API_GET_BRAND_GUIDELINES = (brandId) => `/api/v1/libraries/${brandId}?selector=details`;
@@ -32,18 +36,24 @@ export default class BrandClient {
32
36
  }
33
37
 
34
38
  constructor({ apiBaseUrl, apiKey }, log) {
39
+ this.log = log;
35
40
  if (!isValidUrl(apiBaseUrl)) {
36
- throw new Error(`Invalid Brand API Base URL: ${apiBaseUrl}`);
41
+ throw this.#createError(`Invalid Brand API Base URL: ${apiBaseUrl}`, HTTP_BAD_REQUEST);
37
42
  }
38
43
  if (!hasText(apiKey)) {
39
- throw new Error(`Invalid Brand API Key: ${apiKey}`);
44
+ throw this.#createError(`Invalid Brand API Key: ${apiKey}`, HTTP_BAD_REQUEST);
40
45
  }
41
46
  this.apiBaseUrl = apiBaseUrl;
42
47
  this.apiKey = apiKey;
43
- this.log = log;
44
48
  this.serviceAccessToken = null;
45
49
  }
46
50
 
51
+ #createError(message, status) {
52
+ const error = Object.assign(new Error(message), { status });
53
+ this.log.error(error.message);
54
+ return error;
55
+ }
56
+
47
57
  // eslint-disable-next-line class-methods-use-this
48
58
  #mapToBrand(library) {
49
59
  let createdAt = '';
@@ -69,11 +79,11 @@ export default class BrandClient {
69
79
 
70
80
  async getBrandsForOrganization(imsOrgId, imsAccessToken) {
71
81
  if (!isValidIMSOrgId(imsOrgId)) {
72
- throw new Error(`Invalid IMS Org ID: ${imsOrgId}`);
82
+ throw this.#createError(`Invalid IMS Org ID: ${imsOrgId}`, HTTP_BAD_REQUEST);
73
83
  }
74
84
 
75
85
  if (!hasText(imsAccessToken)) {
76
- throw new Error(`Invalid IMS Access Token: ${imsAccessToken}`);
86
+ throw this.#createError(`Invalid IMS Access Token: ${imsAccessToken}`, HTTP_BAD_REQUEST);
77
87
  }
78
88
  const headers = {
79
89
  'Content-Type': 'application/json',
@@ -85,7 +95,7 @@ export default class BrandClient {
85
95
  });
86
96
 
87
97
  if (!response.ok) {
88
- throw new Error(`Error getting brands for organization ${imsOrgId}: ${response.statusText}`);
98
+ throw this.#createError(`Error getting brands for organization ${imsOrgId}: ${response.statusText}`, response.status);
89
99
  }
90
100
  try {
91
101
  const result = await response.json();
@@ -93,8 +103,7 @@ export default class BrandClient {
93
103
  (library) => library.org_id === imsOrgId,
94
104
  )?.map(this.#mapToBrand);
95
105
  } catch (e) {
96
- this.log.error(`Error getting brands for organization ${imsOrgId} with imsAccessToken. ${e.message}`);
97
- throw new Error(`Error getting brands for organization ${imsOrgId} with imsAccessToken. ${e.message}`);
106
+ throw this.#createError(`Error getting brands for organization ${imsOrgId} with imsAccessToken. ${e.message}`, HTTP_INTERNAL_SERVER_ERROR);
98
107
  }
99
108
  }
100
109
 
@@ -123,16 +132,16 @@ export default class BrandClient {
123
132
 
124
133
  async getBrandGuidelines(brandId, imsOrgId, imsConfig = {}) {
125
134
  if (!hasText(brandId)) {
126
- throw new Error(`Invalid brand ID: ${brandId}`);
135
+ throw this.#createError(`Invalid brand ID: ${brandId}`, HTTP_BAD_REQUEST);
127
136
  }
128
137
  if (!isValidIMSOrgId(imsOrgId)) {
129
- throw new Error(`Invalid IMS Org ID: ${imsOrgId}`);
138
+ throw this.#createError(`Invalid IMS Org ID: ${imsOrgId}`, HTTP_BAD_REQUEST);
130
139
  }
131
140
  const {
132
141
  host, clientId, clientCode, clientSecret,
133
142
  } = imsConfig;
134
143
  if (!hasText(host) || !hasText(clientId) || !hasText(clientCode) || !hasText(clientSecret)) {
135
- throw new Error(`Invalid IMS Config: ${JSON.stringify(imsConfig)}`);
144
+ throw this.#createError(`Invalid IMS Config: ${JSON.stringify(imsConfig)}`, HTTP_BAD_REQUEST);
136
145
  }
137
146
  const imsAccessToken = await this.#getImsAccessToken(imsConfig);
138
147
  const headers = {
@@ -144,12 +153,12 @@ export default class BrandClient {
144
153
  headers,
145
154
  });
146
155
  if (!response.ok) {
147
- throw new Error(`Error getting brand guidelines for brand ${brandId}: ${response.status}`);
156
+ throw this.#createError(`Error getting brand guidelines for brand ${brandId}: ${response.status}`, response.status);
148
157
  }
149
158
  try {
150
159
  const result = await response.json();
151
160
  if (result.org_id !== imsOrgId) {
152
- throw new Error(`Brand ${brandId} not found for org ${imsOrgId}`);
161
+ throw this.#createError(`Brand ${brandId} not found for org ${imsOrgId}`, HTTP_NOT_FOUND);
153
162
  }
154
163
  const brandGuidelines = this.#mapToBrand(result);
155
164
  const guidelines = result.details?.['brand#copyGuidelines'];
@@ -162,8 +171,7 @@ export default class BrandClient {
162
171
  }
163
172
  return brandGuidelines;
164
173
  } catch (e) {
165
- this.log.error(`Error getting brand guidelines for brand ${brandId}: ${e.message}`);
166
- throw new Error(`Error getting brand guidelines for brand ${brandId}: ${e.message}`);
174
+ throw this.#createError(`Error getting brand guidelines for brand ${brandId}: ${e.message}`, HTTP_INTERNAL_SERVER_ERROR);
167
175
  }
168
176
  }
169
177
  }