Skip to content

Commit

Permalink
simplify request() function
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Oct 25, 2023
1 parent 73946c4 commit c7b94ac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 48 deletions.
10 changes: 4 additions & 6 deletions assets/src/js/components/block-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ export function BlockComponent(root, apiEndpoint, rowView, onUpdate) {

function fetch() {
request(apiEndpoint, {
body: {
offset,
limit,
start_date: toISO8601(startDate),
end_date: toISO8601(endDate)
}
offset,
limit,
start_date: toISO8601(startDate),
end_date: toISO8601(endDate)
}).then(items => {
total = items.length
root = patch(root, render(items))
Expand Down
8 changes: 3 additions & 5 deletions assets/src/js/components/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ export default function(root, height) {
dateFormatOptions = groupByMonth ? {month: 'short', year: 'numeric'} : undefined

request('/stats', {
body: {
start_date: toISO8601(startDate),
end_date: toISO8601(endDate),
monthly: groupByMonth ? 1 : 0,
}
start_date: toISO8601(startDate),
end_date: toISO8601(endDate),
monthly: groupByMonth ? 1 : 0,
}).then(data => {
root = patch(root, render(data))
})
Expand Down
15 changes: 6 additions & 9 deletions assets/src/js/components/totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ export default function(root) {
*/
function update(startDate, endDate) {
request('/totals', {
body: {
start_date: toISO8601(startDate),
end_date: toISO8601(endDate)
}}).then(data => {
updateDom(root.children[0], data.visitors, data.visitors_change, data.visitors_change_rel)
updateDom(root.children[1], data.pageviews, data.pageviews_change, data.pageviews_change_rel)
start_date: toISO8601(startDate),
end_date: toISO8601(endDate)
}).then(data => {
updateDom(root.children[0], data.visitors, data.visitors_change, data.visitors_change_rel)
updateDom(root.children[1], data.pageviews, data.pageviews_change, data.pageviews_change_rel)
})
}

function updateRealtime() {
request('/realtime', {
body: {
since: '-1 hour'
}
since: '-1 hour'
}).then(data => {
root.children[2].children[1].textContent = formatLargeNumber(data)
})
Expand Down
34 changes: 6 additions & 28 deletions assets/src/js/util/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,19 @@ const {nonce, root} = window.koko_analytics
/**
*
* @param {string} path
* @param {object} opts
* @param {object} params
* @returns {Promise<any>}
*/
export function request (path, opts = {}) {
Object.assign(opts, {
export function request (path, params = {}) {
let url = root + 'koko-analytics/v1' + path + '?' + (new URLSearchParams(params))

return fetch(url, {
headers: {
'X-WP-Nonce': nonce,
Accepts: 'application/json'
},
credentials: 'same-origin'
})

let url = root + 'koko-analytics/v1' + path
if (opts.body) {
// allow passing "body" option for GET requests, convert it to query params
if (!opts.method || opts.method === 'GET') {
url += url.indexOf('?') > -1 ? '&' : '?'

for (const key in opts.body) {
url += encodeURIComponent(key) + '=' + encodeURIComponent(opts.body[key]) + '&'
}
url = url.slice(0, -1)
delete opts.body
}

if (opts.method === 'POST') {
opts.headers['Content-Type'] = 'application/json'

if (typeof opts.body !== 'string') {
opts.body = JSON.stringify(opts.body)
}
}
}

return fetch(url, opts).then(r => {
}).then(r => {
// reject response when status is not ok-ish
if (r.status >= 400) {
console.error('Koko Analytics encountered an error trying to request data from the REST endpoints. Please check your PHP error logs for the error that occurred.')
Expand Down

0 comments on commit c7b94ac

Please sign in to comment.