Skip to content

Commit

Permalink
refactor(breaking): rename class
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandotv committed Nov 3, 2020
1 parent bc322b1 commit a2dde64
Show file tree
Hide file tree
Showing 3 changed files with 515 additions and 114 deletions.
32 changes: 16 additions & 16 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ export const StationSearchOrder = {
} as const

export const StationSearchType = {
uuid: 'byuuid',
name: 'byname',
nameExact: 'bynameexact',
codec: 'bycodec',
codexExact: 'bycodecexact',
country: 'bycountry',
countryExact: 'bycountryexact',
countryCodeExact: 'bycountrycodeexact',
state: 'bystate',
stateExact: 'bystateexact',
language: 'bylanguage',
languageExact: 'bylanguageexact',
tag: 'bytag',
tagExact: 'bytagexact'
byUuid: 'byUuid',
byName: 'byName',
byNameExact: 'byNameExact',
byCodec: 'byCodec',
byCodexExact: 'byCodecExact',
byCountry: 'byCountry',
byCountryExact: 'byCountryExact',
byCountryCodeExact: 'byCountryCodeExact',
byState: 'byState',
byStateExact: 'byStateExact',
byLanguage: 'byLanguage',
byLanguageExact: 'byLanguageexact',
byTag: 'byTag',
byTagExact: 'byTagExact'
} as const

export type Station = {
Expand All @@ -43,11 +43,11 @@ export type Station = {
urlresolved: string
homepage: string
favicon: string
tags: string[]
tags: string
country: string
countrycode: string
state: string
language: string[]
language: string
votes: number
lastchangetime: Date
codec: string
Expand Down
131 changes: 67 additions & 64 deletions src/radioBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
// todo list of station checks
// todo list of station clicks

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

protected fetchConfig: RequestInit = {
Expand All @@ -26,21 +26,19 @@ export class RadioBrowser {
async resolveBaseUrl(
autoSet = true,
config: RequestInit = {}
): Promise<{ ip: string; name: string }> {
// alternative 'http://all.api.radio-browser.info/json/servers',

): Promise<{ ip: string; name: string }[]> {
const result = await this.runRequest<{ name: string; ip: string }[]>(
this.buildRequest('servers'),
'https://all.api.radio-browser.info/json/servers',
config
)

if (autoSet) {
this.baseUrl = result[0].ip
this.baseUrl = `https://${result[0].name}`
}

return result[0]
return result
}

//
setBaseUrl(url: string): void {
this.baseUrl = url
}
Expand All @@ -49,35 +47,6 @@ export class RadioBrowser {
return this.baseUrl
}

protected buildRequest(
endPoint: string,
search?: string,
query?: object
): string {
search = search ? `/${encodeURIComponent(search)}` : ''
const queryParams = query ? this.createQueryParams(query) : ''

return `${this.baseUrl}/${endPoint}${search}${queryParams}`
}

protected async runRequest<T>(
url: string,
config: RequestInit = {}
): Promise<T> {
// manual merge deep
const headers = Object.assign({}, this.fetchConfig.headers, config.headers)
const fetchConfig = Object.assign({}, this.fetchConfig, config)
fetchConfig.headers = headers

const response = await this.fetchImpl(url, fetchConfig)

if (response.ok) {
return response.json()
} else {
throw response
}
}

async getCountries(
search?: string,
query?: Query,
Expand All @@ -89,12 +58,12 @@ export class RadioBrowser {
)
}

async getCountryCodes(
async getCountryByCountryCode(
search?: string,
query?: Query,
fetchConfig?: RequestInit
): Promise<CountryResult[]> {
search = search ? `/${search.toUpperCase()}` : ''
search = search ? `${search.toUpperCase()}` : ''

return this.runRequest(
this.buildRequest('countrycodes', search, query),
Expand All @@ -103,51 +72,42 @@ export class RadioBrowser {
}

async getCodecs(
search?: string,
query?: Query,
fetchConfig?: RequestInit
): Promise<CountryResult[]> {
search = search ? `/${search.toUpperCase()}` : ''

return this.runRequest(
this.buildRequest('codecs', search, query),
fetchConfig
)
return this.runRequest(this.buildRequest('codecs', '', query), fetchConfig)
}

async getCountryStates(
search?: string,
country?: string,
query?: Query,
fetchConfig?: RequestInit
): Promise<CountryStateResult[]> {
return this.runRequest(
this.buildRequest('states', search, query),
this.buildRequest('states', country, query),
fetchConfig
)
}

async getLanguages(
search?: string,
language?: string,
config?: Query,
fetchConfig?: RequestInit
): Promise<CountryResult[]> {
return this.runRequest(
this.buildRequest('languages', search, config),
this.buildRequest('languages', language, config),
fetchConfig
)
}

async getTags(
search?: string,
tag?: string,
config?: Query,
fetchConfig?: RequestInit
): Promise<CountryResult[]> {
search = search ? search.toLowerCase() : ''
tag = tag ? tag.toLowerCase() : ''

return this.runRequest(
this.buildRequest('tags', search, config),
fetchConfig
)
return this.runRequest(this.buildRequest('tags', tag, config), fetchConfig)
}

// http://fr1.api.radio-browser.info/{format}/stations/byuuid/{searchterm}
Expand All @@ -157,14 +117,15 @@ export class RadioBrowser {
query?: StationQuery,
fetchConfig?: RequestInit
): Promise<Station[]> {
// searchType = searchType.toLowerCase() as keyof typeof StationSearchType
if (!StationSearchType[searchType]) {
throw new Error(`search type does not exist: ${searchType}`)
}

search = search ? search.toLowerCase() : ''

return this.runRequest(
this.buildRequest('stations', `${searchType}/${search}`, query),
this.buildRequest(`stations/${searchType.toLowerCase()}`, search, query),
fetchConfig
)
}
Expand All @@ -179,6 +140,16 @@ export class RadioBrowser {
)
}

async searchStations(
query: AdvancedStationQuery,
fetchConfig?: RequestInit
): Promise<Station[]> {
return this.runRequest(
this.buildRequest('stations/search', undefined, query),
fetchConfig
)
}

async sendStationClick(
uuid: string,
fetchConfig?: RequestInit
Expand All @@ -192,14 +163,46 @@ export class RadioBrowser {
return this.runRequest(this.buildRequest('url', uuid), fetchConfig)
}

async searchStations(
query: AdvancedStationQuery,
async voteForStation(
uuid: string,
fetchConfig?: RequestInit
): Promise<Station[]> {
return this.runRequest(
this.buildRequest('search', undefined, query),
fetchConfig
)
): Promise<{
ok: boolean
message: string
stationuuid: string
name: string
url: string
}> {
return this.runRequest(this.buildRequest('vote', uuid), fetchConfig)
}

protected buildRequest(
endPoint: string,
search?: string,
query?: object
): string {
search = search ? `/${encodeURIComponent(search)}` : ''
const queryParams = query ? this.createQueryParams(query) : ''

return `${this.baseUrl}/${endPoint}${search}${queryParams}`
}

protected async runRequest<T>(
url: string,
config: RequestInit = {}
): Promise<T> {
// manual merge deep
const headers = Object.assign({}, this.fetchConfig.headers, config.headers)
const fetchConfig = Object.assign({}, this.fetchConfig, config)
fetchConfig.headers = headers

const response = await this.fetchImpl(url, fetchConfig)

if (response.ok) {
return response.json()
} else {
throw response
}
}

protected createQueryParams(params?: object): string {
Expand Down

0 comments on commit a2dde64

Please sign in to comment.