Adding in enabled headers, making way for different body types.

This commit is contained in:
mike12345567 2021-12-08 19:11:19 +00:00
parent 4f3a116867
commit 04114594bf
3 changed files with 37 additions and 14 deletions

View File

@ -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}`)

View File

@ -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
}

View File

@ -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" })
}
}