I'm trying to pass an API URL that's has it's params manipulated with the URLSearchParams interface, and I would like to pass that result to an adapter class to fetch that url. As a JS newbie, I am struggling to pass the URL generated in one class to the fetch method in the adapter class. Any ideas? Here is an example. Thank you.
class Search {
constructor() {
this.adapter = new StocksAdapter()
this.baseUrl = "https://www.alphavantage.co/query?"
this.baseUrlAPI = "https://www.alphavantage.co/queryfunction=TIME_SERIES_DAILY&symbol=CCL&apikey=demo"
}
symbolQuery(paramSymbol) {
const url = new URL(this.baseUrlAPI)
const params = new URLSearchParams(url.search)
params.set("symbol", `${paramSymbol}`)
let searchUrl = this.baseUrl.concat(params).toString()
this.adapter.fetchSymbol(this.searchUrl)
}
}
class StocksAdapter {
constructor() {}
fetchSymbol(searchUrl) {
return fetch(searchUrl)
.then(res => res.json())
}
}