@dpgradio/creative 5.0.4 → 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/package.json
CHANGED
|
@@ -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
|
@@ -69,6 +69,8 @@ export class Api {
|
|
|
69
69
|
|
|
70
70
|
clone() {
|
|
71
71
|
return tap(new Api(this.baseUrlOverride, this.version), (api) => {
|
|
72
|
+
api.apiKey = this.apiKey
|
|
73
|
+
api.radioToken = this.radioToken
|
|
72
74
|
api.requestModifiers = this.requestModifiers
|
|
73
75
|
api.errorHandlers = this.errorHandlers
|
|
74
76
|
})
|
|
@@ -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.
|
|
5
|
+
return await this.requestPaginatedData((r) => r.get('/members/me/ratings'), 'ratings')
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
async like(selectorCode) {
|
package/src/api/request.js
CHANGED
|
@@ -96,8 +96,13 @@ export default class Request {
|
|
|
96
96
|
|
|
97
97
|
const endpointWithoutSlash = endpoint.replace(/^\/+/, '')
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
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()
|
|
101
106
|
})
|
|
102
107
|
}
|
|
103
108
|
}
|