@dpgradio/creative 5.0.3 → 5.0.5

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.5",
4
4
  "description": "Support package for standalone Javascript applications",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,41 @@
1
+ import api from './api'
2
+
3
+ export default class PaginatedResponse {
4
+ constructor(response, dataKey = 'data') {
5
+ this.pagination = response.pagination
6
+ this.data = response[dataKey]
7
+ this.dataKey = dataKey
8
+ }
9
+
10
+ async fetchNext() {
11
+ const response = await api.onVersion(null).request().get(this.pagination.next)
12
+ this.pagination.next = response.pagination.next
13
+ this.data = [...this.data, ...response[this.dataKey]]
14
+
15
+ return response[this.dataKey]
16
+ }
17
+
18
+ async fetchPrevious() {
19
+ const response = await api.onVersion(null).request().get(this.pagination.previous)
20
+ this.pagination.previous = response.pagination.previous
21
+ this.data = [...response[this.dataKey], ...this.data]
22
+
23
+ return response[this.dataKey]
24
+ }
25
+
26
+ async fetchAllNext() {
27
+ while ((await this.fetchNext()).length > 0) {
28
+ continue
29
+ }
30
+
31
+ return this.data
32
+ }
33
+
34
+ async fetchAllPrevious() {
35
+ while ((await this.fetchPrevious()).length > 0) {
36
+ continue
37
+ }
38
+
39
+ return this.data
40
+ }
41
+ }
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,17 @@ 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) => {
72
+ api.apiKey = this.apiKey
73
+ api.radioToken = this.radioToken
57
74
  api.requestModifiers = this.requestModifiers
75
+ api.errorHandlers = this.errorHandlers
58
76
  })
59
77
  }
60
78
  }
@@ -2,6 +2,7 @@
2
2
  import { Api } from '../api.js'
3
3
  // eslint-disable-next-line no-unused-vars -- used in JSDoc
4
4
  import Request from '../request.js'
5
+ import PaginatedResponse from '../PaginatedResponse.js'
5
6
 
6
7
  export default class Endpoint {
7
8
  /**
@@ -34,4 +35,24 @@ export default class Endpoint {
34
35
  }
35
36
  return response[key]
36
37
  }
38
+
39
+ /**
40
+ * @param {requestCallback} callback
41
+ * @returns {PaginatedResponse}
42
+ */
43
+ async requestPaginatedData(callback, key = 'data') {
44
+ const response = await callback(this.api.request())
45
+
46
+ if (response === null) {
47
+ throw new Error(`Endpoint returned invalid JSON.`)
48
+ }
49
+ if (response[key] === undefined) {
50
+ throw new Error(`Key '${key}' not found in response: ${JSON.stringify(response)}`)
51
+ }
52
+ if (response.pagination === undefined) {
53
+ throw new Error(`Key 'pagination' not found in response: ${JSON.stringify(response)}`)
54
+ }
55
+
56
+ return new PaginatedResponse(response, key)
57
+ }
37
58
  }
@@ -2,7 +2,7 @@ import Endpoint from './Endpoint.js'
2
2
 
3
3
  export default class Ratings extends Endpoint {
4
4
  async allForMember() {
5
- return await this.requestData((r) => r.get('/members/me/ratings'), 'ratings')
5
+ return await this.requestPaginatedData((r) => r.get('/members/me/ratings'), 'ratings')
6
6
  }
7
7
 
8
8
  async like(selectorCode) {
@@ -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()}`
@@ -93,8 +96,13 @@ export default class Request {
93
96
 
94
97
  const endpointWithoutSlash = endpoint.replace(/^\/+/, '')
95
98
 
96
- return tap(new URL(`${baseUrlWithProtocol}/${this.version}/${endpointWithoutSlash}`), (url) => {
97
- url.search = new URLSearchParams(this.queryParameters).toString()
99
+ const urlParts = [baseUrlWithProtocol, this.version, endpointWithoutSlash].filter(Boolean)
100
+
101
+ return tap(new URL(urlParts.join('/')), (url) => {
102
+ url.search = new URLSearchParams({
103
+ ...Object.fromEntries(new URLSearchParams(url.search)),
104
+ ...this.queryParameters,
105
+ }).toString()
98
106
  })
99
107
  }
100
108
  }