2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
2021-12-13 14:50:15 +01:00
|
|
|
RestConfig,
|
|
|
|
RestQueryFields as RestQuery,
|
2021-12-14 10:52:16 +01:00
|
|
|
AuthType,
|
|
|
|
BasicAuthConfig,
|
2021-12-14 19:03:49 +01:00
|
|
|
BearerAuthConfig,
|
2021-06-27 00:09:46 +02:00
|
|
|
} from "../definitions/datasource"
|
2021-11-10 20:35:09 +01:00
|
|
|
import { IntegrationBase } from "./base/IntegrationBase"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2021-12-02 18:53:14 +01:00
|
|
|
const BodyTypes = {
|
|
|
|
NONE: "none",
|
|
|
|
FORM_DATA: "form",
|
2021-12-14 18:59:02 +01:00
|
|
|
XML: "xml",
|
2021-12-02 18:53:14 +01:00
|
|
|
ENCODED: "encoded",
|
|
|
|
JSON: "json",
|
|
|
|
TEXT: "text",
|
|
|
|
}
|
|
|
|
|
|
|
|
const coreFields = {
|
|
|
|
path: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
display: "URL",
|
|
|
|
},
|
|
|
|
queryString: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
},
|
|
|
|
enabledHeaders: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
},
|
|
|
|
requestBody: {
|
|
|
|
type: DatasourceFieldTypes.JSON,
|
|
|
|
},
|
|
|
|
bodyType: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-12-03 19:39:05 +01:00
|
|
|
enum: Object.values(BodyTypes),
|
2021-12-02 18:53:14 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:16:48 +02:00
|
|
|
module RestModule {
|
|
|
|
const fetch = require("node-fetch")
|
2021-12-06 19:23:18 +01:00
|
|
|
const { formatBytes } = require("../utilities")
|
|
|
|
const { performance } = require("perf_hooks")
|
2021-12-14 18:59:02 +01:00
|
|
|
const FormData = require("form-data")
|
|
|
|
const { URLSearchParams } = require("url")
|
2021-12-15 14:09:03 +01:00
|
|
|
const { parseStringPromise: xmlParser, Builder: XmlBuilder } = require("xml2js")
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://github.com/node-fetch/node-fetch",
|
|
|
|
description:
|
2021-11-30 19:11:29 +01:00
|
|
|
"With the REST API datasource, you can connect, query and pull data from multiple REST APIs. You can then use the retrieved data to build apps.",
|
2021-06-24 19:16:48 +02:00
|
|
|
friendlyName: "REST API",
|
|
|
|
datasource: {
|
|
|
|
url: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-11-30 17:21:16 +01:00
|
|
|
default: "",
|
|
|
|
required: false,
|
|
|
|
deprecated: true,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
defaultHeaders: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
required: false,
|
|
|
|
default: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
readable: true,
|
|
|
|
displayName: "POST",
|
|
|
|
type: QueryTypes.FIELDS,
|
2021-12-02 18:53:14 +01:00
|
|
|
fields: coreFields,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
read: {
|
|
|
|
displayName: "GET",
|
|
|
|
readable: true,
|
|
|
|
type: QueryTypes.FIELDS,
|
2021-12-02 18:53:14 +01:00
|
|
|
fields: coreFields,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
update: {
|
|
|
|
displayName: "PUT",
|
|
|
|
readable: true,
|
|
|
|
type: QueryTypes.FIELDS,
|
2021-12-02 18:53:14 +01:00
|
|
|
fields: coreFields,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
2021-08-30 22:55:12 +02:00
|
|
|
patch: {
|
|
|
|
displayName: "PATCH",
|
|
|
|
readable: true,
|
|
|
|
type: QueryTypes.FIELDS,
|
2021-12-02 18:53:14 +01:00
|
|
|
fields: coreFields,
|
2021-08-30 22:55:12 +02:00
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
delete: {
|
|
|
|
displayName: "DELETE",
|
|
|
|
type: QueryTypes.FIELDS,
|
2021-12-02 18:53:14 +01:00
|
|
|
fields: coreFields,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:35:09 +01:00
|
|
|
class RestIntegration implements IntegrationBase {
|
2021-06-24 19:16:48 +02:00
|
|
|
private config: RestConfig
|
|
|
|
private headers: {
|
|
|
|
[key: string]: string
|
|
|
|
} = {}
|
2021-12-06 19:23:18 +01:00
|
|
|
private startTimeMs: number = performance.now()
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
constructor(config: RestConfig) {
|
|
|
|
this.config = config
|
|
|
|
}
|
|
|
|
|
|
|
|
async parseResponse(response: any) {
|
2021-12-09 13:30:05 +01:00
|
|
|
let data, raw, headers
|
2021-12-14 18:59:02 +01:00
|
|
|
const contentType = response.headers.get("content-type") || ""
|
|
|
|
try {
|
|
|
|
if (contentType.includes("application/json")) {
|
|
|
|
data = await response.json()
|
|
|
|
raw = JSON.stringify(data)
|
2021-12-14 19:03:49 +01:00
|
|
|
} else if (
|
|
|
|
contentType.includes("text/xml") ||
|
|
|
|
contentType.includes("application/xml")
|
|
|
|
) {
|
2021-12-14 18:59:02 +01:00
|
|
|
const rawXml = await response.text()
|
2021-12-14 19:03:49 +01:00
|
|
|
data =
|
|
|
|
(await xmlParser(rawXml, {
|
|
|
|
explicitArray: false,
|
|
|
|
trim: true,
|
|
|
|
explicitRoot: false,
|
|
|
|
})) || {}
|
2021-12-14 18:59:02 +01:00
|
|
|
// there is only one structure, its an array, return the array so it appears as rows
|
|
|
|
const keys = Object.keys(data)
|
|
|
|
if (keys.length === 1 && Array.isArray(data[keys[0]])) {
|
|
|
|
data = data[keys[0]]
|
|
|
|
}
|
|
|
|
raw = rawXml
|
|
|
|
} else {
|
|
|
|
data = await response.text()
|
|
|
|
raw = data
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
throw "Failed to parse response body."
|
2021-12-06 19:23:18 +01:00
|
|
|
}
|
2021-12-13 19:20:02 +01:00
|
|
|
const size = formatBytes(
|
|
|
|
response.headers.get("content-length") || Buffer.byteLength(raw, "utf8")
|
|
|
|
)
|
2021-12-06 19:23:18 +01:00
|
|
|
const time = `${Math.round(performance.now() - this.startTimeMs)}ms`
|
2021-12-09 13:30:05 +01:00
|
|
|
headers = response.headers.raw()
|
|
|
|
for (let [key, value] of Object.entries(headers)) {
|
|
|
|
headers[key] = Array.isArray(value) ? value[0] : value
|
|
|
|
}
|
2021-12-06 19:23:18 +01:00
|
|
|
return {
|
|
|
|
data,
|
|
|
|
info: {
|
|
|
|
code: response.status,
|
|
|
|
size,
|
|
|
|
time,
|
|
|
|
},
|
2021-12-09 13:30:05 +01:00
|
|
|
extra: {
|
|
|
|
raw,
|
|
|
|
headers,
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:20:09 +02:00
|
|
|
getUrl(path: string, queryString: string): string {
|
2021-11-30 18:56:15 +01:00
|
|
|
const main = `${path}?${queryString}`
|
2021-12-13 13:41:47 +01:00
|
|
|
let complete = main
|
|
|
|
if (this.config.url && !main.startsWith(this.config.url)) {
|
|
|
|
complete = !this.config.url ? main : `${this.config.url}/${main}`
|
|
|
|
}
|
2021-12-09 11:02:47 +01:00
|
|
|
if (!complete.startsWith("http")) {
|
|
|
|
complete = `http://${complete}`
|
2021-11-30 18:56:15 +01:00
|
|
|
}
|
2021-12-09 11:02:47 +01:00
|
|
|
return complete
|
2021-10-05 12:20:09 +02:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:59:02 +01:00
|
|
|
addBody(bodyType: string, body: string | any, input: any) {
|
|
|
|
let error, object, string
|
|
|
|
try {
|
|
|
|
string = typeof body !== "string" ? JSON.stringify(body) : body
|
|
|
|
object = typeof body === "object" ? body : JSON.parse(body)
|
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
2021-12-15 14:09:03 +01:00
|
|
|
if (!input.headers) {
|
|
|
|
input.headers = {}
|
|
|
|
}
|
2021-12-14 18:59:02 +01:00
|
|
|
switch (bodyType) {
|
2021-12-15 14:09:03 +01:00
|
|
|
case BodyTypes.NONE:
|
|
|
|
break
|
2021-12-14 18:59:02 +01:00
|
|
|
case BodyTypes.TEXT:
|
|
|
|
// content type defaults to plaintext
|
|
|
|
input.body = string
|
|
|
|
break
|
|
|
|
case BodyTypes.ENCODED:
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
for (let [key, value] of Object.entries(object)) {
|
|
|
|
params.append(key, value)
|
|
|
|
}
|
|
|
|
input.body = params
|
|
|
|
break
|
|
|
|
case BodyTypes.FORM_DATA:
|
|
|
|
const form = new FormData()
|
|
|
|
for (let [key, value] of Object.entries(object)) {
|
|
|
|
form.append(key, value)
|
|
|
|
}
|
|
|
|
input.body = form
|
|
|
|
break
|
|
|
|
case BodyTypes.XML:
|
2021-12-15 14:09:03 +01:00
|
|
|
if (object != null) {
|
|
|
|
string = (new XmlBuilder()).buildObject(object)
|
|
|
|
}
|
2021-12-14 18:59:02 +01:00
|
|
|
input.body = string
|
2021-12-15 14:09:03 +01:00
|
|
|
input.headers["Content-Type"] = "application/xml"
|
2021-12-14 18:59:02 +01:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
case BodyTypes.JSON:
|
|
|
|
// if JSON error, throw it
|
|
|
|
if (error) {
|
|
|
|
throw "Invalid JSON for request body"
|
|
|
|
}
|
2021-12-15 13:23:00 +01:00
|
|
|
input.body = string
|
2021-12-14 18:59:02 +01:00
|
|
|
input.headers["Content-Type"] = "application/json"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2021-12-14 19:03:49 +01:00
|
|
|
getAuthHeaders(authConfigId: string): { [key: string]: any } {
|
2021-12-11 22:43:03 +01:00
|
|
|
let headers: any = {}
|
|
|
|
|
|
|
|
if (this.config.authConfigs && authConfigId) {
|
|
|
|
const authConfig = this.config.authConfigs.filter(
|
|
|
|
c => c._id === authConfigId
|
|
|
|
)[0]
|
2021-12-12 00:34:30 +01:00
|
|
|
// check the config still exists before proceeding
|
|
|
|
// if not - do nothing
|
|
|
|
if (authConfig) {
|
|
|
|
let config
|
|
|
|
switch (authConfig.type) {
|
|
|
|
case AuthType.BASIC:
|
|
|
|
config = authConfig.config as BasicAuthConfig
|
|
|
|
headers.Authorization = `Basic ${Buffer.from(
|
|
|
|
`${config.username}:${config.password}`
|
|
|
|
).toString("base64")}`
|
|
|
|
break
|
|
|
|
case AuthType.BEARER:
|
|
|
|
config = authConfig.config as BearerAuthConfig
|
|
|
|
headers.Authorization = `Bearer ${config.token}`
|
|
|
|
break
|
|
|
|
}
|
2021-12-11 22:43:03 +01:00
|
|
|
}
|
2021-12-07 23:33:26 +01:00
|
|
|
}
|
2021-12-11 22:43:03 +01:00
|
|
|
|
|
|
|
return headers
|
2021-12-07 23:33:26 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 20:11:19 +01:00
|
|
|
async _req(query: RestQuery) {
|
2021-12-13 19:20:02 +01:00
|
|
|
const {
|
|
|
|
path = "",
|
|
|
|
queryString = "",
|
|
|
|
headers = {},
|
|
|
|
method = "GET",
|
|
|
|
disabledHeaders,
|
|
|
|
bodyType,
|
|
|
|
requestBody,
|
2021-12-14 19:03:49 +01:00
|
|
|
authConfigId,
|
2021-12-13 19:20:02 +01:00
|
|
|
} = query
|
2021-12-11 22:43:03 +01:00
|
|
|
const authHeaders = this.getAuthHeaders(authConfigId)
|
|
|
|
|
2021-06-24 19:16:48 +02:00
|
|
|
this.headers = {
|
|
|
|
...this.config.defaultHeaders,
|
|
|
|
...headers,
|
2021-12-11 22:43:03 +01:00
|
|
|
...authHeaders,
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 12:24:13 +01:00
|
|
|
if (disabledHeaders) {
|
2021-12-08 20:11:19 +01:00
|
|
|
for (let headerKey of Object.keys(this.headers)) {
|
2021-12-13 12:24:13 +01:00
|
|
|
if (disabledHeaders[headerKey]) {
|
2021-12-08 20:11:19 +01:00
|
|
|
delete this.headers[headerKey]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:59:02 +01:00
|
|
|
let input: any = { method, headers: this.headers }
|
2021-12-13 19:17:20 +01:00
|
|
|
if (requestBody) {
|
2021-12-14 18:59:02 +01:00
|
|
|
input = this.addBody(bodyType, requestBody, input)
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-12-06 19:23:18 +01:00
|
|
|
this.startTimeMs = performance.now()
|
2021-12-12 00:34:30 +01:00
|
|
|
const url = this.getUrl(path, queryString)
|
|
|
|
const response = await fetch(url, input)
|
2021-06-24 19:16:48 +02:00
|
|
|
return await this.parseResponse(response)
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:11:19 +01:00
|
|
|
async create(opts: RestQuery) {
|
2021-12-06 19:23:18 +01:00
|
|
|
return this._req({ ...opts, method: "POST" })
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 20:11:19 +01:00
|
|
|
async read(opts: RestQuery) {
|
2021-12-06 19:23:18 +01:00
|
|
|
return this._req({ ...opts, method: "GET" })
|
2021-08-30 22:55:12 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 20:11:19 +01:00
|
|
|
async update(opts: RestQuery) {
|
2021-12-06 19:23:18 +01:00
|
|
|
return this._req({ ...opts, method: "PUT" })
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2021-12-08 20:11:19 +01:00
|
|
|
async patch(opts: RestQuery) {
|
2021-12-06 19:23:18 +01:00
|
|
|
return this._req({ ...opts, method: "PATCH" })
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2021-12-08 20:11:19 +01:00
|
|
|
async delete(opts: RestQuery) {
|
2021-12-06 19:23:18 +01:00
|
|
|
return this._req({ ...opts, method: "DELETE" })
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: RestIntegration,
|
2021-12-08 16:27:58 +01:00
|
|
|
AuthType,
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
}
|