refactor(utils): clean up fetchGameData

This commit is contained in:
Walid 2023-04-19 17:14:11 +00:00
parent 9a1feb829c
commit 36ea248c9f
Signed by: Walidoux
GPG Key ID: CCF21881FE8BEBAF

View File

@ -3,7 +3,7 @@ import { XMLParser } from 'fast-xml-parser'
import { GAME_ENDPOINTS, client } from '../config/ENDPOINTS' import { GAME_ENDPOINTS, client } from '../config/ENDPOINTS'
import type { DomainTypes } from '../types/Domain' import type { DomainTypes } from '../types/Domain'
import { convertXML } from '../mapping' import { convertTXT, convertXML } from '../mapping'
import { parseData } from './parseData' import { parseData } from './parseData'
export const fetchGameData = async ( export const fetchGameData = async (
@ -17,18 +17,23 @@ export const fetchGameData = async (
await client await client
.get(endpoint.src, { responseType: ResponseType.Text }) .get(endpoint.src, { responseType: ResponseType.Text })
.then(async ({ data }) => { .then(async ({ data }) => {
const currentData = data as string
switch (endpoint.convert) { switch (endpoint.convert) {
case 'XML': case 'XML':
const convertedData = new XMLParser({ const convertedData = new XMLParser({
ignoreAttributes: false, ignoreAttributes: false,
attributeNamePrefix: '' attributeNamePrefix: ''
}).parse(data as string) }).parse(currentData)
const XML2JSON = convertXML(convertedData, endpoint.src) const XML2JSON = convertXML(convertedData, endpoint.src)
console.log(XML2JSON)
return await parseData(endpoint.fileName, XML2JSON) return await parseData(endpoint.fileName, XML2JSON)
case 'TXT':
return await convertTXT(currentData)
default:
return await parseData(endpoint.fileName, currentData)
} }
}) })
.catch((error) => { .catch((error) => {