@dpgradio/creative 5.0.3 → 5.0.4

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 CHANGED
@@ -225,8 +225,10 @@ const image = new ImageGeneratorProperties('https://static.qmusic.be/acties/joe-
225
225
 
226
226
  This package provides a number of utility functions.
227
227
 
228
- | Function | Description |
229
- | ------------------------------- | ----------- |
230
- | `loadScript(url, { timeout })` | Dynammically loads a JS script. |
231
- | `openLink(url)` | App-compatible link opener. |
232
- | `tap(value, callback)` | Invokes `callback` with the `value` and then returns `value`. |
228
+ | Function | Description |
229
+ | ------------------------------- | ---------------------------------------------------------------------------------------- |
230
+ | `loadScript(url, { timeout })` | Dynammically loads a JS script. |
231
+ | `openLink(url)` | App-compatible link opener. |
232
+ | `tap(value, callback)` | Invokes `callback` with the `value` and then returns `value`. |
233
+ | `cdnImageUrl(endpoint[, size])` | Get a full image URL for an endpoint in a given size (`w480`, `w800`, `w1200`, `w2400`). |
234
+ | `cdnUrl(endpoint)` | Get a full CDN URL for a given endpoint. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dpgradio/creative",
3
- "version": "5.0.3",
3
+ "version": "5.0.4",
4
4
  "description": "Support package for standalone Javascript applications",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/api/api.js CHANGED
@@ -17,6 +17,11 @@ export class Api {
17
17
 
18
18
  this.requestModifiers = []
19
19
 
20
+ this.errorHandlers = []
21
+
22
+ this.apiKey = null
23
+ this.radioToken = null
24
+
20
25
  // Endpoints
21
26
  this.channels = new Channels(this)
22
27
  this.config = new Config(this)
@@ -29,18 +34,23 @@ export class Api {
29
34
  }
30
35
 
31
36
  request() {
32
- return tap(new Request(this.baseUrl, this.version), (request) => {
33
- this.requestModifiers.forEach((modifier) => modifier(request))
37
+ const modifiers = [...this.requestModifiers]
38
+
39
+ this.apiKey && modifiers.push((request) => request.withQueryParameters({ api_key: this.apiKey }))
40
+ this.radioToken && modifiers.push((request) => request.withHeader('Authorization', `Bearer ${this.radioToken}`))
41
+
42
+ return tap(new Request(this.baseUrl, this.version, this.errorHandlers), (request) => {
43
+ modifiers.forEach((modifier) => modifier(request))
34
44
  })
35
45
  }
36
46
 
37
47
  setApiKey(apiKey) {
38
- this.requestModifiers.push((request) => request.withQueryParameters({ api_key: apiKey }))
48
+ this.apiKey = apiKey
39
49
  return this
40
50
  }
41
51
 
42
52
  setRadioToken(token) {
43
- this.requestModifiers.push((request) => request.withHeader('Authorization', `Bearer ${token}`))
53
+ this.radioToken = token
44
54
  return this
45
55
  }
46
56
 
@@ -52,9 +62,15 @@ export class Api {
52
62
  return tap(this.clone(), (api) => (api.baseUrlOverride = GLOBAL_API_URL))
53
63
  }
54
64
 
65
+ addErrorHandler(handler) {
66
+ this.errorHandlers.push(handler)
67
+ return this
68
+ }
69
+
55
70
  clone() {
56
71
  return tap(new Api(this.baseUrlOverride, this.version), (api) => {
57
72
  api.requestModifiers = this.requestModifiers
73
+ api.errorHandlers = this.errorHandlers
58
74
  })
59
75
  }
60
76
  }
@@ -1,9 +1,10 @@
1
1
  import tap from '../utils/tap.js'
2
2
 
3
3
  export default class Request {
4
- constructor(baseUrl, version) {
4
+ constructor(baseUrl, version, errorHandlers = []) {
5
5
  this.baseUrl = baseUrl
6
6
  this.version = version
7
+ this.errorHandlers = errorHandlers
7
8
  this.queryParameters = {}
8
9
  this.data = null
9
10
  this.headers = {}
@@ -75,6 +76,8 @@ export default class Request {
75
76
  })
76
77
 
77
78
  if (!response.ok) {
79
+ this.errorHandlers.forEach((handler) => handler({ response }))
80
+
78
81
  throw new Error(
79
82
  `API request (${method} ${url}) failed: [${response.status}] ${response.statusText}\n\n` +
80
83
  `RESPONSE\n--------\n${await response.text()}`