mbd-studio-sdk 2.0.6 → 2.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/StudioConfig.js +127 -0
- package/package.json +2 -1
package/StudioConfig.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} ServicesUrl
|
|
3
|
+
* @property {string} searchService - URL of the search service
|
|
4
|
+
* @property {string} storiesService - URL of the stories service
|
|
5
|
+
* @property {string} featuresService - URL of the features service
|
|
6
|
+
* @property {string} scoringService - URL of the scoring service
|
|
7
|
+
* @property {string} rankingService - URL of the ranking service
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {Object} StudioConfigOptions
|
|
12
|
+
* @property {string} apiKey - API key for authentication (required)
|
|
13
|
+
* @property {string} [commonUrl] - Base URL used for all services when provided
|
|
14
|
+
* @property {ServicesUrl} [servicesUrl] - Per-service URLs when commonUrl is not provided
|
|
15
|
+
* @property {function(string): void} [log] - Optional log function, defaults to console.log
|
|
16
|
+
* @property {function(*): void} [show] - Optional show function, defaults to console.log
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for MBD Studio SDK.
|
|
21
|
+
* Use commonUrl for a single URL for all services, or servicesUrl for one URL per service.
|
|
22
|
+
*/
|
|
23
|
+
export class StudioConfig {
|
|
24
|
+
/**
|
|
25
|
+
* @param {StudioConfigOptions} options - Configuration options
|
|
26
|
+
* @throws {Error} When parameters are invalid or missing required fields
|
|
27
|
+
*/
|
|
28
|
+
constructor(options) {
|
|
29
|
+
if (!options || typeof options !== 'object') {
|
|
30
|
+
throw new Error('StudioConfig: options object is required');
|
|
31
|
+
}
|
|
32
|
+
const { apiKey, commonUrl, servicesUrl, log, show } = options;
|
|
33
|
+
|
|
34
|
+
if (typeof apiKey !== 'string' || !apiKey.trim()) {
|
|
35
|
+
throw new Error('StudioConfig: apiKey is required and must be a non-empty string');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const hasCommonUrl =
|
|
39
|
+
typeof commonUrl === 'string' && commonUrl.trim().length > 0;
|
|
40
|
+
|
|
41
|
+
if (hasCommonUrl) {
|
|
42
|
+
const url = commonUrl.trim().replace(/\/$/, '');
|
|
43
|
+
this._searchService = url;
|
|
44
|
+
this._storiesService = url;
|
|
45
|
+
this._featuresService = url;
|
|
46
|
+
this._scoringService = url;
|
|
47
|
+
this._rankingService = url;
|
|
48
|
+
} else {
|
|
49
|
+
if (!servicesUrl || typeof servicesUrl !== 'object') {
|
|
50
|
+
throw new Error(
|
|
51
|
+
'StudioConfig: when commonUrl is not provided, servicesUrl must be an object with searchService, storiesService, featuresService, scoringService, rankingService'
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const {
|
|
55
|
+
searchService,
|
|
56
|
+
storiesService,
|
|
57
|
+
featuresService,
|
|
58
|
+
scoringService,
|
|
59
|
+
rankingService,
|
|
60
|
+
} = servicesUrl;
|
|
61
|
+
const services = {
|
|
62
|
+
searchService,
|
|
63
|
+
storiesService,
|
|
64
|
+
featuresService,
|
|
65
|
+
scoringService,
|
|
66
|
+
rankingService,
|
|
67
|
+
};
|
|
68
|
+
const missing = Object.entries(services)
|
|
69
|
+
.filter(([, v]) => typeof v !== 'string' || !v.trim())
|
|
70
|
+
.map(([k]) => k);
|
|
71
|
+
if (missing.length > 0) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`StudioConfig: when using servicesUrl, all service URLs are required. Missing or invalid: ${missing.join(', ')}`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
this._searchService = searchService.trim().replace(/\/$/, '');
|
|
77
|
+
this._storiesService = storiesService.trim().replace(/\/$/, '');
|
|
78
|
+
this._featuresService = featuresService.trim().replace(/\/$/, '');
|
|
79
|
+
this._scoringService = scoringService.trim().replace(/\/$/, '');
|
|
80
|
+
this._rankingService = rankingService.trim().replace(/\/$/, '');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this._apiKey = apiKey.trim();
|
|
84
|
+
this._log = typeof log === 'function' ? log : console.log.bind(console);
|
|
85
|
+
this._show = typeof show === 'function' ? show : console.log.bind(console);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @returns {function(string): void} */
|
|
89
|
+
get log() {
|
|
90
|
+
return this._log;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @returns {function(*): void} */
|
|
94
|
+
get show() {
|
|
95
|
+
return this._show;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** @returns {string} */
|
|
99
|
+
get searchService() {
|
|
100
|
+
return this._searchService;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** @returns {string} */
|
|
104
|
+
get storiesService() {
|
|
105
|
+
return this._storiesService;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @returns {string} */
|
|
109
|
+
get featuresService() {
|
|
110
|
+
return this._featuresService;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** @returns {string} */
|
|
114
|
+
get scoringService() {
|
|
115
|
+
return this._scoringService;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** @returns {string} */
|
|
119
|
+
get rankingService() {
|
|
120
|
+
return this._rankingService;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @returns {string} */
|
|
124
|
+
get apiKey() {
|
|
125
|
+
return this._apiKey;
|
|
126
|
+
}
|
|
127
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mbd-studio-sdk",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "SDK for MBD Studio backend services",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
11
|
"index.js",
|
|
12
|
+
"StudioConfig.js",
|
|
12
13
|
"V1"
|
|
13
14
|
]
|
|
14
15
|
}
|