Working file export; need to test out error handling and flows

This commit is contained in:
efarooqui
2021-02-09 15:38:54 -08:00
parent fb77e1c365
commit 7c963eb1d0
5 changed files with 26 additions and 17 deletions

View File

@@ -1,6 +1,5 @@
import { version } from '../../package.json'
import { Router } from 'express'
import { exportToFile } from '../utilities'
import copy from '../copy/en'
export default ({ config, controller }) => {

View File

@@ -1,7 +1,11 @@
export default {
errors: {
update: 'The server could not update. Check your API credentials and internet connection and try again.',
export: 'The server could not export. Check that you provided a file path to export to and try again.',
export: {
fileMissing: 'The server could not export. Check that you provided a file path to export to and try again.',
writeFailed: 'The server could not export the data to the file. There is an issue with the data format'
},
exportFileMissing: 'The server could not export. Check that you provided a file path to export to and try again.',
onlySheet: 'You cannot query a sheet directly. The URL needs to be in the format /:sheet/:tab/:resource.',
onlyTab: 'You cannot query a tab directly. The URL needs to be in the format /:sheet/:tab/:resource.',
noSheet: sheet => `The sheet ${sheet} is not available in this server.`,

View File

@@ -1,4 +1,5 @@
import copy from '../copy/en'
import { exportToFile } from '../lib/util'
/**
* Controller
@@ -40,7 +41,7 @@ class Controller {
}
retrieveAll (fileDest) {
if (fileDest === '') throw new Error(copy.errors.export)
if (fileDest === '') return Promise.reject(new Error(copy.errors.export.fileMissing))
const indexedData = {}
const urls = []
@@ -52,14 +53,19 @@ class Controller {
urls.push(bp.urls[0])
return this.retrieve(bp.sheet.name, bp.name, resource)
})
).then(results => {
).then(async results => {
if (results.every(res => res)) {
urls.forEach((item, idx) => {
indexedData[item] = results[idx]
})
return copy.success.export(fileDest)
try {
await exportToFile(fileDest, indexedData)
return copy.success.export(fileDest)
} catch (e) {
return Promise.reject(e)
}
} else {
throw new Error(copy.errors.export)
return Promise.reject(new Error(copy.errors.export.writeFailed))
}
})
}

View File

@@ -1,4 +1,6 @@
import R from 'ramda'
import fs from 'file-system'
import copy from '../copy/en'
/* eslint-disable */
String.prototype.replaceAll = function (search, replacement) {
@@ -14,6 +16,15 @@ function camelize (str) {
})
}
export function exportToFile(fileDest, data) {
const stringifiedData = JSON.stringify(data, null, 2)
const filePath = `${fileDest}/export.json`
fs.writeFile(filePath, stringifiedData, (err) => {
if (err) throw new Error(copy.errors.export.writeFailed)
})
}
export const fmtObj = R.curry(
(
columnNames,

View File

@@ -1,11 +0,0 @@
import fs from 'file-system'
function exportToFile(url, data) {
console.info(url, data)
return 1
}
export const utilities = {
default: exportToFile(),
exportToFile
}