Skip to content

Commit

Permalink
fix: fetch and apply
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinchappell committed Oct 30, 2024
1 parent 4428064 commit 4c2c856
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
44 changes: 12 additions & 32 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,35 +16,18 @@ const defaultOptions = Object.freeze({ method: 'GET', body: '' })
* @return {function(): Promise<Buffer>} return.arrayBuffer - A function that returns a promise resolving to the body as an ArrayBuffer.
* @return {function(): Promise<Object>} 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
}
}
11 changes: 6 additions & 5 deletions src/mi18n.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch } from './fetch.js'
import { fetchData } from './fetch.js'

const DEFAULT_CONFIG = {
extension: '.lang',
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 4c2c856

Please sign in to comment.