Adding in enabled headers, making way for different body types.
This commit is contained in:
parent
4f3a116867
commit
04114594bf
|
@ -107,6 +107,7 @@
|
||||||
try {
|
try {
|
||||||
const { _id } = await queries.save(toSave.datasourceId, toSave)
|
const { _id } = await queries.save(toSave.datasourceId, toSave)
|
||||||
saveId = _id
|
saveId = _id
|
||||||
|
query = getSelectedQuery()
|
||||||
notifications.success(`Request saved successfully.`)
|
notifications.success(`Request saved successfully.`)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
notifications.error(`Error creating query. ${err.message}`)
|
notifications.error(`Error creating query. ${err.message}`)
|
||||||
|
|
|
@ -123,7 +123,7 @@ async function enrichQueryFields(fields, parameters = {}) {
|
||||||
enrichedQuery.requestBody
|
enrichedQuery.requestBody
|
||||||
)
|
)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw { message: `JSON Invalid - error: ${err}` }
|
// no json found, ignore
|
||||||
}
|
}
|
||||||
delete enrichedQuery.customData
|
delete enrichedQuery.customData
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,17 @@ module RestModule {
|
||||||
const { formatBytes } = require("../utilities")
|
const { formatBytes } = require("../utilities")
|
||||||
const { performance } = require("perf_hooks")
|
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 {
|
interface RestConfig {
|
||||||
url: string
|
url: string
|
||||||
defaultHeaders: {
|
defaultHeaders: {
|
||||||
|
@ -48,13 +59,6 @@ module RestModule {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Request {
|
|
||||||
path: string
|
|
||||||
queryString?: string
|
|
||||||
headers?: string
|
|
||||||
json?: any
|
|
||||||
}
|
|
||||||
|
|
||||||
const SCHEMA: Integration = {
|
const SCHEMA: Integration = {
|
||||||
docs: "https://github.com/node-fetch/node-fetch",
|
docs: "https://github.com/node-fetch/node-fetch",
|
||||||
description:
|
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.headers = {
|
||||||
...this.config.defaultHeaders,
|
...this.config.defaultHeaders,
|
||||||
...headers,
|
...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 }
|
const input: any = { method, headers: this.headers }
|
||||||
if (json && typeof json === "object" && Object.keys(json).length > 0) {
|
if (json && typeof json === "object" && Object.keys(json).length > 0) {
|
||||||
input.body = JSON.stringify(json)
|
input.body = JSON.stringify(json)
|
||||||
|
@ -165,23 +187,23 @@ module RestModule {
|
||||||
return await this.parseResponse(response)
|
return await this.parseResponse(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(opts: Request) {
|
async create(opts: RestQuery) {
|
||||||
return this._req({ ...opts, method: "POST" })
|
return this._req({ ...opts, method: "POST" })
|
||||||
}
|
}
|
||||||
|
|
||||||
async read(opts: Request) {
|
async read(opts: RestQuery) {
|
||||||
return this._req({ ...opts, method: "GET" })
|
return this._req({ ...opts, method: "GET" })
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(opts: Request) {
|
async update(opts: RestQuery) {
|
||||||
return this._req({ ...opts, method: "PUT" })
|
return this._req({ ...opts, method: "PUT" })
|
||||||
}
|
}
|
||||||
|
|
||||||
async patch(opts: Request) {
|
async patch(opts: RestQuery) {
|
||||||
return this._req({ ...opts, method: "PATCH" })
|
return this._req({ ...opts, method: "PATCH" })
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(opts: Request) {
|
async delete(opts: RestQuery) {
|
||||||
return this._req({ ...opts, method: "DELETE" })
|
return this._req({ ...opts, method: "DELETE" })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue