From 41fa958c78fbc6a80516db138a3a5094f5210ac6 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Fri, 26 Nov 2021 09:51:56 +0000 Subject: [PATCH] File upload working --- .../modals/ImportRestDatasourceModal.svelte | 26 ++++++++++++------- packages/server/src/api/controllers/query.js | 2 ++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/modals/ImportRestDatasourceModal.svelte b/packages/builder/src/components/backend/DatasourceNavigator/modals/ImportRestDatasourceModal.svelte index dd25880491..64cffb3f7f 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/modals/ImportRestDatasourceModal.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/modals/ImportRestDatasourceModal.svelte @@ -25,24 +25,29 @@ let lastTouched = "url" - $: { - console.log({ data }) - console.log({ lastTouched }) - } + const getPayload = async () => { + let payloadData + let type + + // parse the file into memory and send as string + if (lastTouched === "file") { + type = "raw" + payloadData = await data.file[0].text() + } else { + type = lastTouched + payloadData = data[lastTouched] + } - const getPayload = () => { return { - type: lastTouched, - data: data[lastTouched], + type: type, + data: payloadData, } } async function importDatasource() { try { - // Create datasource - const resp = await datasources.import(getPayload()) + const resp = await datasources.import(await getPayload()) - // // update the tables incase data source plus await queries.fetch() await datasources.select(resp._id) $goto(`./datasource/${resp._id}`) @@ -82,6 +87,7 @@ (lastTouched = "file")} fileTags={["OpenAPI", "Swagger 2.0"]} diff --git a/packages/server/src/api/controllers/query.js b/packages/server/src/api/controllers/query.js index 13b14a6343..40fc1adf21 100644 --- a/packages/server/src/api/controllers/query.js +++ b/packages/server/src/api/controllers/query.js @@ -110,6 +110,8 @@ exports.import = async function (ctx) { data = await fetch(importConfig.data).then(res => res.json()) } else if (importConfig.type === "raw") { data = JSON.parse(importConfig.data) + } else { + throw new Error("Invalid data type") } const db = new CouchDB(ctx.appId)