/**
 * fetch.js
 */
;(() => {
  window.jsFetch = {
    /**
     * wrapper for fetch
     * @param {string} url
     * @param {string} method
     * @param {JSON} data
     * @returns
     */
    async sendData(url, data, method = 'GET') {
      const options = {
        method,
        headers: { 'Content-Type': 'application/json' },
        body: data ? JSON.stringify(data) : undefined
      }

      try {
        const res = await fetch(url, options)
        if (!res.ok) {
          console.error('Response error:', res)
          return
        }

        return await res.json()
      } catch (err) {
        console.error('Request catch error:', err)
        throw err
      }
    }
  }
})()
