JSFiddle - React, Tailwind, and code Playground
by Chris Ball
JavaScript
let baseURL = 'http://hmtest.local/wp-json/wp/v2/'
class GetPostByType {
#params;
constructor(endpoint, params = {}) {
this.result = `${baseURL}${endpoint}`
this.#params = params
this.setParams()
}
setParams() {
// Exit if no params are set
if (Object.keys(this.#params).length === 0) return;
// Otherwise append to result
const paramStr = new URLSearchParams(this.#params).toString()
this.result += `?${paramStr}`
}
}
let test = new GetPostByType('events', {
per_page: 2,
order: 'desc'
})
console.log('test:', test)
let test2 = new GetPostByType('events')
console.log('test2:', test2)