budibase/packages/server/src/integrations/rest.js

117 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-02-15 18:05:53 +01:00
const fetch = require("node-fetch")
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
const SCHEMA = {
docs: "https://github.com/node-fetch/node-fetch",
datasource: {
url: {
type: FIELD_TYPES.STRING,
default: "localhost",
required: true,
},
},
query: {
2021-02-15 19:41:56 +01:00
create: {
type: QUERY_TYPES.FIELDS,
fields: {
path: {
type: FIELD_TYPES.STRING,
},
headers: {
type: FIELD_TYPES.OBJECT,
},
requestBody: {
type: FIELD_TYPES.JSON,
},
},
},
2021-02-15 18:05:53 +01:00
read: {
type: QUERY_TYPES.FIELDS,
fields: {
path: {
type: FIELD_TYPES.STRING,
},
headers: {
type: FIELD_TYPES.OBJECT,
},
},
},
2021-02-15 19:41:56 +01:00
update: {
type: QUERY_TYPES.FIELDS,
fields: {
path: {
type: FIELD_TYPES.STRING,
},
headers: {
type: FIELD_TYPES.OBJECT,
},
requestBody: {
type: FIELD_TYPES.JSON,
},
},
},
delete: {
type: QUERY_TYPES.FIELDS,
fields: {
path: {
type: FIELD_TYPES.STRING,
},
headers: {
type: FIELD_TYPES.OBJECT,
},
requestBody: {
type: FIELD_TYPES.JSON,
},
},
},
2021-02-15 18:05:53 +01:00
},
}
class RestIntegration {
constructor(config) {
this.config = config
}
2021-02-15 20:57:49 +01:00
async create({ path, headers = {}, json }) {
2021-02-15 19:41:56 +01:00
const response = await fetch(this.config.url + path, {
method: "POST",
headers,
2021-02-15 20:57:49 +01:00
body: JSON.stringify(json),
2021-02-15 19:41:56 +01:00
})
return await response.json()
}
2021-02-15 18:05:53 +01:00
async read({ path, headers = {} }) {
const response = await fetch(this.config.url + path, {
headers,
})
return await response.json()
}
2021-02-15 20:57:49 +01:00
async update({ path, headers = {}, json }) {
2021-02-15 19:41:56 +01:00
const response = await fetch(this.config.url + path, {
method: "POST",
headers,
2021-02-15 20:57:49 +01:00
body: JSON.stringify(json),
2021-02-15 19:41:56 +01:00
})
return await response.json()
}
2021-02-15 18:05:53 +01:00
2021-02-15 19:41:56 +01:00
async delete({ path, headers = {} }) {
const response = await fetch(this.config.url + path, {
method: "DELETE",
headers,
})
return await response.json()
}
2021-02-15 18:05:53 +01:00
}
module.exports = {
schema: SCHEMA,
integration: RestIntegration,
}