@dpgradio/creative 5.1.0 → 5.2.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/README.md +7 -2
- package/examples/useConfig.js +9 -5
- package/package.json +1 -1
- package/src/api/endpoints/Config.js +4 -2
- package/src/api/endpoints/Endpoint.js +4 -0
- package/src/config/config.js +32 -11
package/README.md
CHANGED
|
@@ -40,15 +40,20 @@ The configuration will also be used by other components of this package (e.g. pr
|
|
|
40
40
|
```js
|
|
41
41
|
import { configuration } from '@dpgradio/creative'
|
|
42
42
|
|
|
43
|
+
// Global config only
|
|
44
|
+
await configuration.retrieveConfigForDetectedStation()
|
|
45
|
+
|
|
46
|
+
// Global and app-specific config
|
|
43
47
|
await configuration.retrieveConfigForDetectedStation(appId) // Default, by hostname or query parameter
|
|
44
48
|
|
|
45
49
|
// Or, if you want to retrieve the config by hostnames only.
|
|
46
50
|
await configuration.retrieveConfigByHostname(appId)
|
|
47
51
|
// Or, if you want to retrieve the config for a specific station:
|
|
48
|
-
await configuration.retrieveConfigByStation(
|
|
52
|
+
await configuration.retrieveConfigByStation(stationId, appId)
|
|
49
53
|
|
|
50
54
|
// By default the first station ID (stationIdA) is used as the current station of the configuration.
|
|
51
|
-
// If you want to use a different station, you can do so by calling:
|
|
55
|
+
// If you want to use a different station, you can do so by calling setStation:
|
|
56
|
+
await configuration.retrieveConfigByStations([stationIdA, stationIdB, stationIdC], appId)
|
|
52
57
|
configuration.setStation(stationIdC)
|
|
53
58
|
```
|
|
54
59
|
You can change the station later on at any time without having to retrieve the config again.
|
package/examples/useConfig.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import configuration, { config } from '../src/config/config.js'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
global.window = { location: { hostname: 'qmusic.nl', search: '' } }
|
|
4
|
+
|
|
5
|
+
const appId = 'player'
|
|
4
6
|
const stationIdA = 'qmusic_be'
|
|
5
7
|
const stationIdB = 'qmusic_nl'
|
|
6
8
|
|
|
7
|
-
await configuration.
|
|
9
|
+
await configuration.retrieveConfigForDetectedStation()
|
|
10
|
+
console.log(config()) // global config
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
await configuration.retrieveConfigForDetectedStation(appId)
|
|
13
|
+
console.log(config('app')) // player config of qmusic_be
|
|
10
14
|
|
|
15
|
+
await configuration.retrieveConfigForStations([stationIdA, stationIdB], appId)
|
|
11
16
|
configuration.setStation(stationIdB)
|
|
12
|
-
|
|
13
|
-
console.log(config('app')) // greety config of qmusic_nl
|
|
17
|
+
console.log(config('app')) // player config of qmusic_nl
|
package/package.json
CHANGED
|
@@ -2,11 +2,13 @@ import Endpoint from './Endpoint.js'
|
|
|
2
2
|
|
|
3
3
|
export default class Config extends Endpoint {
|
|
4
4
|
async global({ stationIds = null, domains = null }) {
|
|
5
|
-
return await this.requestData((r) => r.get('/config', { station_ids: stationIds, domains }))
|
|
5
|
+
return await this.requestData((r) => r.get('/config', this.withoutNullValues({ station_ids: stationIds, domains })))
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
async app(appId, { stationIds = null, domains = null }) {
|
|
9
|
-
return await this.requestData((r) =>
|
|
9
|
+
return await this.requestData((r) =>
|
|
10
|
+
r.get(`/config/${appId}`, this.withoutNullValues({ station_ids: stationIds, domains }))
|
|
11
|
+
)
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
async updateSchema(appId, schema) {
|
package/src/config/config.js
CHANGED
|
@@ -15,7 +15,10 @@ class Configuration {
|
|
|
15
15
|
return this
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Retrieve the configuration for the current hostname or present query parameter and the given app (optional).
|
|
20
|
+
*/
|
|
21
|
+
async retrieveConfigForDetectedStation(appId = null) {
|
|
19
22
|
const parameters = new URLSearchParams(window.location.search)
|
|
20
23
|
if (parameters.has('stationId')) {
|
|
21
24
|
return this.retrieveConfigForStation(appId, parameters.get('stationId'))
|
|
@@ -24,15 +27,20 @@ class Configuration {
|
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
/**
|
|
27
|
-
* Retrieve the configuration for the
|
|
30
|
+
* Retrieve the configuration for the current hostname and the given app (optional).
|
|
28
31
|
*/
|
|
29
|
-
async retrieveConfigByHostname(appId) {
|
|
32
|
+
async retrieveConfigByHostname(appId = null) {
|
|
30
33
|
this.appId = appId
|
|
31
34
|
|
|
32
35
|
// This is not the most robust way to get the hostname without subdomains, e.g. .co.uk domains will break.
|
|
33
36
|
// However, for the TLDs we use, this should be fine.
|
|
34
37
|
const hostname = window.location.hostname.split('.').splice(-2).join('.')
|
|
35
|
-
|
|
38
|
+
|
|
39
|
+
if (appId) {
|
|
40
|
+
this.rawConfig = await api.global().config.app(appId, { domains: [hostname] })
|
|
41
|
+
} else {
|
|
42
|
+
this.rawConfig = await api.global().config.global({ domains: [hostname] })
|
|
43
|
+
}
|
|
36
44
|
|
|
37
45
|
this.stationId = Object.keys(this.rawConfig)[0]
|
|
38
46
|
|
|
@@ -40,13 +48,24 @@ class Configuration {
|
|
|
40
48
|
}
|
|
41
49
|
|
|
42
50
|
/**
|
|
43
|
-
* Retrieve the configuration for the given
|
|
51
|
+
* Retrieve the configuration for the given station and the given app (optional).
|
|
44
52
|
*/
|
|
45
|
-
async retrieveConfigForStation(
|
|
53
|
+
async retrieveConfigForStation(stationId, appId = null) {
|
|
54
|
+
return await this.retrieveConfigForStations([stationId], appId)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Retrieve the configuration for the given stations and the given app (optional).
|
|
59
|
+
*/
|
|
60
|
+
async retrieveConfigForStations(stationIds, appId = null) {
|
|
46
61
|
this.appId = appId
|
|
47
62
|
this.stationId = stationIds[0]
|
|
48
63
|
|
|
49
|
-
|
|
64
|
+
if (appId) {
|
|
65
|
+
this.rawConfig = await api.global().config.app(appId, { stationIds })
|
|
66
|
+
} else {
|
|
67
|
+
this.rawConfig = await api.global().config.global({ stationIds })
|
|
68
|
+
}
|
|
50
69
|
|
|
51
70
|
return this
|
|
52
71
|
}
|
|
@@ -58,10 +77,12 @@ class Configuration {
|
|
|
58
77
|
if (!this.stationId) {
|
|
59
78
|
throw new Error('No station set. First use [config.setStation] to set the station.')
|
|
60
79
|
}
|
|
61
|
-
if (!this.
|
|
62
|
-
throw new Error(
|
|
80
|
+
if (!this.rawConfig[this.stationId]) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`No configuration found for station [${this.stationId}]. Make sure there is a global config for this station.`
|
|
83
|
+
)
|
|
63
84
|
}
|
|
64
|
-
if (!this.rawConfig[this.stationId]?.[this.appId]) {
|
|
85
|
+
if (this.appId && !this.rawConfig[this.stationId]?.[this.appId]) {
|
|
65
86
|
throw new Error(
|
|
66
87
|
`No configuration found for station [${this.stationId}] and app [${this.appId}]. Make sure the app is enabled for this station.`
|
|
67
88
|
)
|
|
@@ -69,7 +90,7 @@ class Configuration {
|
|
|
69
90
|
|
|
70
91
|
const stationConfig = {
|
|
71
92
|
...this.rawConfig[this.stationId],
|
|
72
|
-
app: this.rawConfig[this.stationId][this.appId],
|
|
93
|
+
...(this.appId ? { app: this.rawConfig[this.stationId][this.appId] } : {}),
|
|
73
94
|
}
|
|
74
95
|
|
|
75
96
|
return property ? dotSyntaxAccess(stationConfig, property) : stationConfig
|