Skip to content

Commit b36d779

Browse files
migrate fetchJson method to a helper file and use it on all plugins
1 parent 2b0d10e commit b36d779

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

src/Colors/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20+
import fetchJson from '../helpers/fetchJson'
2021
import Log from '../Log'
2122
import { mergeColors, calculateAlpha, isObject, isString, argbToHSLA, hslaToARGB } from './utils.js'
2223

@@ -60,8 +61,7 @@ export const initColors = file => {
6061
addColors(file)
6162
return resolve()
6263
}
63-
fetch(file)
64-
.then(response => response.json())
64+
fetchJson(file)
6565
.then(json => {
6666
addColors(json)
6767
return resolve()

src/Language/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20+
import fetchJson from '../helpers/fetchJson'
2021
import Log from '../Log'
2122
import Utils from '../Utils'
2223

@@ -27,8 +28,7 @@ let dictionary = null
2728

2829
export const initLanguage = (file, language = null) => {
2930
return new Promise((resolve, reject) => {
30-
fetch(file)
31-
.then(response => response.json())
31+
fetchJson(file)
3232
.then(json => {
3333
setTranslations(json)
3434
// set language (directly or in a promise)
@@ -102,8 +102,7 @@ const setLanguage = lng => {
102102
} else if (typeof translationsObj === 'string') {
103103
const url = Utils.asset(translationsObj)
104104

105-
fetch(url)
106-
.then(response => response.json())
105+
fetchJson(url)
107106
.then(json => {
108107
// save the translations for this language (to prevent loading twice)
109108
translations[language] = json

src/helpers/fetchJson.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* If not stated otherwise in this file or this component's LICENSE file the
3+
* following copyright and licenses apply:
4+
*
5+
* Copyright 2020 Metrological
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the License);
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
export default (file) => {
21+
return new Promise((resolve, reject) => {
22+
var xhr = new XMLHttpRequest()
23+
xhr.onreadystatechange = function() {
24+
if (xhr.readyState == XMLHttpRequest.DONE) {
25+
if (xhr.status === 200) resolve(JSON.parse(xhr.responseText))
26+
else reject(xhr.statusText)
27+
}
28+
}
29+
xhr.open('GET', file)
30+
xhr.send(null)
31+
})
32+
}

src/startApp.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* limitations under the License.
1818
*/
1919

20+
import fetchJson from './helpers/fetchJson'
21+
2022
const style = document.createElement('style')
2123

2224
document.head.appendChild(style)
@@ -191,20 +193,6 @@ const removeJS = id => {
191193
}
192194
}
193195

194-
const fetchJson = file => {
195-
return new Promise((resolve, reject) => {
196-
var xhr = new XMLHttpRequest()
197-
xhr.onreadystatechange = function() {
198-
if (xhr.readyState == XMLHttpRequest.DONE) {
199-
if (xhr.status === 200) resolve(JSON.parse(xhr.responseText))
200-
else reject(xhr.statusText)
201-
}
202-
}
203-
xhr.open('GET', file)
204-
xhr.send(null)
205-
})
206-
}
207-
208196
const sequence = steps => {
209197
return steps.reduce((promise, method) => {
210198
return promise.then(() => method())

0 commit comments

Comments
 (0)