Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ import { Api } from '@dpgradio/creative'
const api = new Api('https://api.qmusic.be')

api.setRadioToken(token)
api.request({ passAuth: true }).post(`/forbidden_words/${id}/detections`) // for authenticated endpoints
api.request().get(`/forbidden_words/${id}`) // for non-authenticate endpoints
api.withAuth().request().post('/some-non-predefined-endpoint') // for authenticated endpoints
api.request().get('/some-non-predefined-endpoint') // for non-authenticate endpoints

```

Expand Down
20 changes: 10 additions & 10 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,11 @@ export class Api {
return this.baseUrlOverride || config('api_base_url')
}

request(config = { passAuth: false }) {
request() {
const modifiers = [...this.requestModifiers]

this.apiKey && modifiers.push((request) => request.withQueryParameters({ api_key: this.apiKey }))

if (config.passAuth) {
if (this.radioToken) {
modifiers.push((request) => request.withHeader('Authorization', `Bearer ${this.radioToken}`))
} else {
throw new Error('No currentUserToken available')
}
}

return tap(new Request(this.baseUrl, this.version, this.errorHandlers), (request) => {
modifiers.forEach((modifier) => modifier(request))
})
Expand All @@ -79,6 +71,15 @@ export class Api {
return tap(this.clone(), (api) => (api.baseUrlOverride = GLOBAL_API_URL))
}

withAuth() {
return tap(this.clone(), (api) => {
if (!this.radioToken) {
throw new Error('No currentUserToken available')
}
api.requestModifiers.push((request) => request.withHeader('Authorization', `Bearer ${this.radioToken}`))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De requestModifiers voelt als een private ding (maar ik zie ook niet meteen waarom we deze bijhouden in een array), dus ik had hier verwacht dat je het ging zetten op een this.passAuth of iets, zodat je dat in de request() method weer op zou kunnen bouwen.

(Wat ik zeg doet eigenlijk exact hetzelfde als wat jij hier doet, dus dit is geen 'mijn manier is de goede' meer een 'wat denk je van dit?'-comment)

})
}

addErrorHandler(handler) {
this.errorHandlers.push(handler)
return this
Expand All @@ -88,7 +89,6 @@ export class Api {
return tap(new Api(this.baseUrlOverride, this.version), (api) => {
api.apiKey = this.apiKey
api.radioToken = this.radioToken
api.requestModifiers = this.requestModifiers
api.errorHandlers = this.errorHandlers
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/api/endpoints/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export default class Endpoint {
* @param {requestCallback} callback
* @returns {object}
*/
async requestData(callback, key = 'data', config = { passAuth: false }) {
const response = await callback(this.api.request(config))
async requestData(callback, key = 'data') {
const response = await callback(this.api.request())

if (response === null) {
throw new Error(`Endpoint returned invalid JSON.`)
Expand All @@ -40,8 +40,8 @@ export default class Endpoint {
* @param {requestCallback} callback
* @returns {PaginatedResponse}
*/
async requestPaginatedData(callback, key = 'data', config = { passAuth: false }) {
const response = await callback(this.api.request(config))
async requestPaginatedData(callback, key = 'data', config = { withAuth: false }) {
const response = await callback(config.withAuth ? this.api.withAuth().request() : this.api.request())
Comment thread
vhsiebe marked this conversation as resolved.
Outdated

if (response === null) {
throw new Error(`Endpoint returned invalid JSON.`)
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/ForbiddenWord.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Endpoint from './Endpoint.js'

export default class ForbiddenWord extends Endpoint {
async detection(id) {
await this.api.request({ passAuth: true }).post(`/forbidden_words/${id}/detections`)
await this.api.withAuth().request().post(`/forbidden_words/${id}/detections`)
}
}
4 changes: 2 additions & 2 deletions src/api/endpoints/Members.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Endpoint from './Endpoint.js'

export default class Members extends Endpoint {
async me() {
return await this.api.request({ passAuth: true }).get('/members/me')
return await this.api.withAuth().request().get('/members/me')
}

async updateProfile(profile) {
return await this.api.request({ passAuth: true }).put('/members/me', { profile })
return await this.api.withAuth().request().put('/members/me', { profile })
}
}
6 changes: 3 additions & 3 deletions src/api/endpoints/Ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import Endpoint from './Endpoint.js'

export default class Ratings extends Endpoint {
async allForMember() {
return await this.requestPaginatedData((r) => r.get('/members/me/ratings'), 'ratings', { passAuth: true })
return await this.requestPaginatedData((r) => r.get('/members/me/ratings'), 'ratings', { withAuth: true })
}

async like(selectorCode) {
await this.api.request({ passAuth: true }).post(`/tracks/${selectorCode}/ratings`, { rating: 1 })
await this.api.withAuth().request().post(`/tracks/${selectorCode}/ratings`, { rating: 1 })
}

async unlike(selectorCode) {
await this.api.request({ passAuth: true }).delete(`/tracks/${selectorCode}/ratings`)
await this.api.withAuth().request().delete(`/tracks/${selectorCode}/ratings`)
}
}
2 changes: 1 addition & 1 deletion src/api/endpoints/Requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Endpoint from './Endpoint.js'

export default class Requests extends Endpoint {
async requestTrack(eventSlug, request) {
return await this.api.request({ passAuth: true }).post(`/requests/${eventSlug}`, request)
return await this.api.withAuth().request().post(`/requests/${eventSlug}`, request)
}

async requestsForTrack(eventSlug, selectorCode) {
Expand Down