From 4c2c856db5e335ad883119b64da92dfad0694472 Mon Sep 17 00:00:00 2001 From: Kevin Chappell Date: Wed, 30 Oct 2024 13:14:16 -0700 Subject: [PATCH] fix: fetch and apply --- src/fetch.js | 44 ++++++++++++-------------------------------- src/mi18n.js | 11 ++++++----- 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/fetch.js b/src/fetch.js index 3c1f5f1..c26bb6c 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -1,6 +1,3 @@ -import https from 'https' -import http from 'http' - const defaultOptions = Object.freeze({ method: 'GET', body: '' }) /** * Fetches a resource from the network. @@ -19,35 +16,18 @@ const defaultOptions = Object.freeze({ method: 'GET', body: '' }) * @return {function(): Promise} return.arrayBuffer - A function that returns a promise resolving to the body as an ArrayBuffer. * @return {function(): Promise} return.json - A function that returns a promise resolving to the body parsed as JSON. */ -export function fetch(url, fetchOptions = {}) { - const options = { ...defaultOptions, ...fetchOptions } - const handler = url.startsWith('https') ? https : http - - return new Promise((resolve, reject) => { - const request = handler.request(url, options, response => { - processResponse(response, resolve) - }) - - request.on('error', error => reject(new Error(error.message))) - if (options?.body) request.write(options.body) - request.end() - }) -} -const processResponse = (response, onEnd) => { - const data = [] - response.on('data', chunk => data.push(chunk)) - response.on('end', () => { - const bodyBuffer = Buffer.concat(data) - const result = { - ok: response.statusCode >= 200 && response.statusCode < 300, - status: response.statusCode, - statusText: response.statusMessage, - headers: new Map(Object.entries(response.headers)), - body: bodyBuffer, - arrayBuffer: () => Promise.resolve(bodyBuffer), - json: () => Promise.resolve(JSON.parse(bodyBuffer.toString())), +export async function fetchData(url) { + try { + const response = await fetch(url) + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) } - onEnd(result) - }) + const handler = url.endsWith('.lang') ? 'text' : 'json' + const data = await response[handler]() + return data + } catch (error) { + console.error('There has been a problem with your fetch operation:', error) + throw error + } } diff --git a/src/mi18n.js b/src/mi18n.js index 57c54b0..b22b982 100644 --- a/src/mi18n.js +++ b/src/mi18n.js @@ -1,4 +1,4 @@ -import { fetch } from './fetch.js' +import { fetchData } from './fetch.js' const DEFAULT_CONFIG = { extension: '.lang', @@ -186,14 +186,15 @@ export class I18N { return resolve(_this.langs[locale]) } else { const langFile = [_this.config.location, locale, _this.config.extension].join('') - return fetch(langFile) - .then(({ data: lang }) => { - const processedFile = _this.processFile(lang) + return fetchData(langFile) + .then(lang => { + const processedFile = I18N.processFile(lang) _this.applyLanguage(locale, processedFile) _this.loaded.push(locale) return resolve(_this.langs[locale]) }) - .catch(() => { + .catch(err => { + console.error(err) const lang = _this.applyLanguage(locale) resolve(lang) })