Skip to content

Commit

Permalink
feat!: hide broken stations by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandotv committed Nov 3, 2020
1 parent 38ffe14 commit 55d14fe
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 23 deletions.
34 changes: 27 additions & 7 deletions src/radioBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ import {
StationQuery
} from './constants'

// todo list of station checks
// todo list of station clicks

export class RadioBrowserApi {
protected baseUrl = 'https://fr1.api.radio-browser.info/json'

hideBroken: boolean

protected fetchConfig: RequestInit = {
method: 'GET',
redirect: 'follow'
}

constructor(protected userAgent: string, protected fetchImpl: typeof fetch) {
constructor(
protected userAgent: string,
protected fetchImpl: typeof fetch,
hideBroken = true
) {
this.fetchConfig.headers = { 'user-agent': this.userAgent }
this.hideBroken = hideBroken
}

async resolveBaseUrl(
Expand Down Expand Up @@ -160,7 +164,10 @@ export class RadioBrowserApi {
name: string
url: string
}> {
return this.runRequest(this.buildRequest('url', uuid), fetchConfig)
return this.runRequest(
this.buildRequest('url', uuid, undefined, false),
fetchConfig
)
}

async voteForStation(
Expand All @@ -179,10 +186,23 @@ export class RadioBrowserApi {
protected buildRequest(
endPoint: string,
search?: string,
query?: object
query?: Query | AdvancedStationQuery | StationQuery,
addHideBrokenParam = true
): string {
search = search ? `/${encodeURIComponent(search)}` : ''
const queryParams = query ? this.createQueryParams(query) : ''

let queryCopy
if (query) {
queryCopy = { ...query }
if ('tagList' in queryCopy && Array.isArray(queryCopy.tagList)) {
queryCopy.tagList = [...queryCopy.tagList]
}
if (addHideBrokenParam && typeof queryCopy.hideBroken === 'undefined') {
queryCopy.hideBroken = this.hideBroken
}
}

const queryParams = queryCopy ? this.createQueryParams(queryCopy) : ''

return `${this.baseUrl}/${endPoint}${search}${queryParams}`
}
Expand Down
178 changes: 162 additions & 16 deletions tests/radioBrowser.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import nock from 'nock'
import nodeFetch from 'node-fetch'
import { StationQuery } from '../dist/types'
import { Query } from '../src'
import { RadioBrowserApi } from '../src/radioBrowser'
import { StationSearchType } from '../src/constants'
import { AdvancedStationQuery, StationQuery } from '../dist/types'
import { RadioBrowserApi } from '../src/radioBrowser'

const globalTest = {
fetch: (nodeFetch as unknown) as typeof fetch
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('Radio Browser', () => {
const headerValue = '1'
const mockResult = [{ name: 'rs', stationcount: 10 }]
const country = 'germany'
const query = { order: 'name' }
const query = { order: 'name', hideBroken: true }

const scope = nock(baseUrl, {
reqheaders: {
Expand All @@ -145,7 +145,7 @@ describe('Radio Browser', () => {
}
})
.get(`/json/countries/${country}`)
.query(query)
.query({ order: 'name', hidebroken: 'true' })
.reply(200, mockResult)

const result = await api.getCountries(country, query as Query, {
Expand Down Expand Up @@ -175,7 +175,10 @@ describe('Radio Browser', () => {
}
})
.get(`/json/countrycodes/${country}`)
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getCountryByCountryCode(country, query as Query, {
Expand Down Expand Up @@ -204,7 +207,10 @@ describe('Radio Browser', () => {
}
})
.get('/json/codecs')
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getCodecs(query as Query, {
Expand Down Expand Up @@ -234,7 +240,10 @@ describe('Radio Browser', () => {
}
})
.get(`/json/states/${country}`)
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getCountryStates(country, query as Query, {
Expand Down Expand Up @@ -264,7 +273,10 @@ describe('Radio Browser', () => {
}
})
.get(`/json/languages/${language}`)
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getLanguages(language, query as Query, {
Expand Down Expand Up @@ -294,7 +306,10 @@ describe('Radio Browser', () => {
}
})
.get(`/json/tags/${tag}`)
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getTags(tag, query as Query, {
Expand Down Expand Up @@ -324,7 +339,10 @@ describe('Radio Browser', () => {
}
})
.get(`/json/stations/bylanguage/${language}`)
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getStationsBy(
Expand Down Expand Up @@ -359,7 +377,10 @@ describe('Radio Browser', () => {
}
})
.get(`/json/stations/bytag/${tag}`)
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getStationsBy(
Expand Down Expand Up @@ -409,7 +430,10 @@ describe('Radio Browser', () => {
}
})
.get('/json/stations')
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getAllStations(query as StationQuery, {
Expand Down Expand Up @@ -495,7 +519,10 @@ describe('Radio Browser', () => {
}
})
.get(`/json/countrycodes/${country}`)
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.getCountryByCountryCode(country, query as Query, {
Expand All @@ -509,15 +536,15 @@ describe('Radio Browser', () => {
})
describe('Advanced station search', () => {
// advanced station search
xtest('by tag list', async () => {
test('by tag list', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)

const headerName = 'x-jest-test'
const headerValue = '1'
const mockResult = [{ name: 'rs', stationcount: 10 }]
const query = {
tagList: 'rap,pop,jazz'
taglist: 'rap,pop,jazz'
}

const scope = nock(baseUrl, {
Expand All @@ -527,7 +554,10 @@ describe('Radio Browser', () => {
}
})
.get('/json/stations/search')
.query(query)
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.searchStations(
Expand All @@ -545,4 +575,120 @@ describe('Radio Browser', () => {
expect(result).toEqual(mockResult)
})
})
describe('Show or hide broken stations', () => {
// advanced station search
test('hide broken stations by default', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)

const headerName = 'x-jest-test'
const headerValue = '1'
const mockResult = [{ name: 'rs', stationcount: 10 }]
const query = {
taglist: 'rap,pop,jazz'
}

const scope = nock(baseUrl, {
reqheaders: {
[headerName]: headerValue,
'user-agent': userAgent
}
})
.get('/json/stations/search')
.query({
hidebroken: 'true',
...query
})
.reply(200, mockResult)

const result = await api.searchStations(
{
tagList: ['rap', 'pop', 'jazz']
},
{
headers: {
[headerName]: headerValue
}
}
)

expect(scope.isDone()).toBe(true)
expect(result).toEqual(mockResult)
})
})

test('hide broken stations explicitly', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)

const headerName = 'x-jest-test'
const headerValue = '1'
const mockResult = [{ name: 'rs', stationcount: 10 }]
const query = {
taglist: 'rap,pop,jazz',
hidebroken: 'true'
}

const scope = nock(baseUrl, {
reqheaders: {
[headerName]: headerValue,
'user-agent': userAgent
}
})
.get('/json/stations/search')
.query(query)
.reply(200, mockResult)

const result = await api.searchStations(
{
tagList: ['rap', 'pop', 'jazz'],
hideBroken: true
},
{
headers: {
[headerName]: headerValue
}
}
)

expect(scope.isDone()).toBe(true)
expect(result).toEqual(mockResult)
})
test('Show broken stations', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)

const headerName = 'x-jest-test'
const headerValue = '1'
const mockResult = [{ name: 'rs', stationcount: 10 }]
const query = {
taglist: 'rap,pop,jazz',
hidebroken: 'false'
}

const scope = nock(baseUrl, {
reqheaders: {
[headerName]: headerValue,
'user-agent': userAgent
}
})
.get('/json/stations/search')
.query(query)
.reply(200, mockResult)

const result = await api.searchStations(
{
tagList: ['rap', 'pop', 'jazz'],
hideBroken: false
},
{
headers: {
[headerName]: headerValue
}
}
)

expect(scope.isDone()).toBe(true)
expect(result).toEqual(mockResult)
})
})

0 comments on commit 55d14fe

Please sign in to comment.