diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte index 7e2c083a0..bafca7bc4 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte @@ -107,6 +107,7 @@ try { const { _id } = await queries.save(toSave.datasourceId, toSave) saveId = _id + query = getSelectedQuery() notifications.success(`Request saved successfully.`) } catch (err) { notifications.error(`Error creating query. ${err.message}`) diff --git a/packages/server/src/api/controllers/query/index.js b/packages/server/src/api/controllers/query/index.js index b5c0c9bf9..7c9bf173b 100644 --- a/packages/server/src/api/controllers/query/index.js +++ b/packages/server/src/api/controllers/query/index.js @@ -123,7 +123,7 @@ async function enrichQueryFields(fields, parameters = {}) { enrichedQuery.requestBody ) } catch (err) { - throw { message: `JSON Invalid - error: ${err}` } + // no json found, ignore } delete enrichedQuery.customData } diff --git a/packages/server/src/integrations/rest.ts b/packages/server/src/integrations/rest.ts index 57bd11ef9..696671a6f 100644 --- a/packages/server/src/integrations/rest.ts +++ b/packages/server/src/integrations/rest.ts @@ -41,6 +41,17 @@ module RestModule { const { formatBytes } = require("../utilities") const { performance } = require("perf_hooks") + interface RestQuery { + path: string + queryString?: string + headers: { [key: string]: any } + enabledHeaders: { [key: string]: any } + requestBody: any + bodyType: string + json: object + method: string + } + interface RestConfig { url: string defaultHeaders: { @@ -48,13 +59,6 @@ module RestModule { } } - interface Request { - path: string - queryString?: string - headers?: string - json?: any - } - const SCHEMA: Integration = { docs: "https://github.com/node-fetch/node-fetch", description: @@ -149,12 +153,30 @@ module RestModule { } } - async _req({ path = "", queryString = "", headers = {}, json = {}, method = "GET" }) { + async _req(query: RestQuery) { + const { path = "", queryString = "", headers = {}, method = "GET", enabledHeaders, bodyType, requestBody } = query this.headers = { ...this.config.defaultHeaders, ...headers, } + if (enabledHeaders) { + for (let headerKey of Object.keys(this.headers)) { + if (!enabledHeaders[headerKey]) { + delete this.headers[headerKey] + } + } + } + + let json + if (bodyType === BodyTypes.JSON && requestBody) { + try { + json = JSON.parse(requestBody) + } catch (err) { + throw "Invalid JSON for request body" + } + } + const input: any = { method, headers: this.headers } if (json && typeof json === "object" && Object.keys(json).length > 0) { input.body = JSON.stringify(json) @@ -165,23 +187,23 @@ module RestModule { return await this.parseResponse(response) } - async create(opts: Request) { + async create(opts: RestQuery) { return this._req({ ...opts, method: "POST" }) } - async read(opts: Request) { + async read(opts: RestQuery) { return this._req({ ...opts, method: "GET" }) } - async update(opts: Request) { + async update(opts: RestQuery) { return this._req({ ...opts, method: "PUT" }) } - async patch(opts: Request) { + async patch(opts: RestQuery) { return this._req({ ...opts, method: "PATCH" }) } - async delete(opts: Request) { + async delete(opts: RestQuery) { return this._req({ ...opts, method: "DELETE" }) } }