2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
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
|
|
|
|
|
|
|
module RestModule {
|
|
|
|
const fetch = require("node-fetch")
|
|
|
|
|
|
|
|
interface RestConfig {
|
2021-06-24 19:17:26 +02:00
|
|
|
url: string
|
2021-06-24 19:16:48 +02:00
|
|
|
defaultHeaders: {
|
2021-06-24 19:17:26 +02:00
|
|
|
[key: string]: any
|
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,
|
|
|
|
fields: {
|
|
|
|
path: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-12-01 14:11:35 +01:00
|
|
|
display: "URL",
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
queryString: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
},
|
|
|
|
requestBody: {
|
|
|
|
type: DatasourceFieldTypes.JSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
displayName: "GET",
|
|
|
|
readable: true,
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
path: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-12-01 14:11:35 +01:00
|
|
|
display: "URL",
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
queryString: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
displayName: "PUT",
|
|
|
|
readable: true,
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
path: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-12-01 14:11:35 +01:00
|
|
|
display: "URL",
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
queryString: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
},
|
|
|
|
requestBody: {
|
|
|
|
type: DatasourceFieldTypes.JSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-30 22:55:12 +02:00
|
|
|
patch: {
|
|
|
|
displayName: "PATCH",
|
|
|
|
readable: true,
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
path: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-12-01 14:11:35 +01:00
|
|
|
display: "URL",
|
2021-08-30 22:55:12 +02:00
|
|
|
},
|
|
|
|
queryString: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
},
|
|
|
|
requestBody: {
|
|
|
|
type: DatasourceFieldTypes.JSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
delete: {
|
|
|
|
displayName: "DELETE",
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
path: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-12-01 14:11:35 +01:00
|
|
|
display: "URL",
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
queryString: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
},
|
|
|
|
requestBody: {
|
|
|
|
type: DatasourceFieldTypes.JSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
} = {}
|
|
|
|
|
|
|
|
constructor(config: RestConfig) {
|
|
|
|
this.config = config
|
|
|
|
}
|
|
|
|
|
|
|
|
async parseResponse(response: any) {
|
2021-11-03 14:12:20 +01:00
|
|
|
const contentType = response.headers.get("content-type")
|
|
|
|
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
|
|
return await response.json()
|
|
|
|
} else {
|
|
|
|
return await response.text()
|
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}`
|
|
|
|
if (!this.config.url) {
|
|
|
|
return main
|
|
|
|
} else {
|
|
|
|
return `${this.config.url}/${main}`
|
|
|
|
}
|
2021-10-05 12:20:09 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 19:16:48 +02:00
|
|
|
async create({ path = "", queryString = "", headers = {}, json = {} }) {
|
|
|
|
this.headers = {
|
|
|
|
...this.config.defaultHeaders,
|
|
|
|
...headers,
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:20:09 +02:00
|
|
|
const response = await fetch(this.getUrl(path, queryString), {
|
2021-06-24 19:16:48 +02:00
|
|
|
method: "POST",
|
|
|
|
headers: this.headers,
|
|
|
|
body: JSON.stringify(json),
|
|
|
|
})
|
|
|
|
|
|
|
|
return await this.parseResponse(response)
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:17:26 +02:00
|
|
|
async read({ path = "", queryString = "", headers = {} }) {
|
2021-06-24 19:16:48 +02:00
|
|
|
this.headers = {
|
|
|
|
...this.config.defaultHeaders,
|
|
|
|
...headers,
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:20:09 +02:00
|
|
|
const response = await fetch(this.getUrl(path, queryString), {
|
2021-06-24 19:16:48 +02:00
|
|
|
headers: this.headers,
|
|
|
|
})
|
|
|
|
|
|
|
|
return await this.parseResponse(response)
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:17:26 +02:00
|
|
|
async update({ path = "", queryString = "", headers = {}, json = {} }) {
|
2021-06-24 19:16:48 +02:00
|
|
|
this.headers = {
|
|
|
|
...this.config.defaultHeaders,
|
|
|
|
...headers,
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:20:09 +02:00
|
|
|
const response = await fetch(this.getUrl(path, queryString), {
|
2021-11-03 14:12:20 +01:00
|
|
|
method: "PUT",
|
2021-06-24 19:16:48 +02:00
|
|
|
headers: this.headers,
|
|
|
|
body: JSON.stringify(json),
|
|
|
|
})
|
|
|
|
|
|
|
|
return await this.parseResponse(response)
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:55:12 +02:00
|
|
|
async patch({ path = "", queryString = "", headers = {}, json = {} }) {
|
|
|
|
this.headers = {
|
|
|
|
...this.config.defaultHeaders,
|
|
|
|
...headers,
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:20:09 +02:00
|
|
|
const response = await fetch(this.getUrl(path, queryString), {
|
2021-08-30 22:55:12 +02:00
|
|
|
method: "PATCH",
|
|
|
|
headers: this.headers,
|
|
|
|
body: JSON.stringify(json),
|
|
|
|
})
|
|
|
|
|
|
|
|
return await this.parseResponse(response)
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:17:26 +02:00
|
|
|
async delete({ path = "", queryString = "", headers = {} }) {
|
2021-06-24 19:16:48 +02:00
|
|
|
this.headers = {
|
|
|
|
...this.config.defaultHeaders,
|
|
|
|
...headers,
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:20:09 +02:00
|
|
|
const response = await fetch(this.getUrl(path, queryString), {
|
2021-06-24 19:16:48 +02:00
|
|
|
method: "DELETE",
|
|
|
|
headers: this.headers,
|
|
|
|
})
|
|
|
|
|
|
|
|
return await this.parseResponse(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: RestIntegration,
|
|
|
|
}
|
|
|
|
}
|