@adobe/spacecat-shared-brand-client 1.0.1 → 1.0.2
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 +7 -0
- package/README.md +1 -1
- package/package.json +2 -2
- package/src/index.js +23 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [@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)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* error handling and dependencies ([#650](https://github.com/adobe/spacecat-shared/issues/650)) ([3dc4e03](https://github.com/adobe/spacecat-shared/commit/3dc4e03b045b324ea01aff82e177fd7567822f51))
|
|
7
|
+
|
|
1
8
|
# [@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
9
|
|
|
3
10
|
|
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/spacecat-shared-brand-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Shared modules of the Spacecat Services - Brand Client",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -36,7 +36,7 @@
|
|
|
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": "
|
|
39
|
+
"@adobe/spacecat-shared-utils": "1.34.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"chai": "5.2.0",
|
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
|
|
41
|
+
throw this.#createError(`Invalid Brand API Base URL: ${apiBaseUrl}`, HTTP_BAD_REQUEST);
|
|
37
42
|
}
|
|
38
43
|
if (!hasText(apiKey)) {
|
|
39
|
-
throw
|
|
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
|
|
82
|
+
throw this.#createError(`Invalid IMS Org ID: ${imsOrgId}`, HTTP_BAD_REQUEST);
|
|
73
83
|
}
|
|
74
84
|
|
|
75
85
|
if (!hasText(imsAccessToken)) {
|
|
76
|
-
throw
|
|
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
|
|
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
|
|
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
|
|
135
|
+
throw this.#createError(`Invalid brand ID: ${brandId}`, HTTP_BAD_REQUEST);
|
|
127
136
|
}
|
|
128
137
|
if (!isValidIMSOrgId(imsOrgId)) {
|
|
129
|
-
throw
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
}
|