diff --git a/packages/builder/src/components/integration/index.svelte b/packages/builder/src/components/integration/index.svelte index 5c821d3a53..a0e1b8c1c4 100644 --- a/packages/builder/src/components/integration/index.svelte +++ b/packages/builder/src/components/integration/index.svelte @@ -17,9 +17,9 @@ $: urlDisplay = schema.urlDisplay && - `${datasource.config.url}${query.fields.path ?? ""}${ - query.fields.queryString ?? "" - }` + `${datasource.config.url}${ + query.fields.path ? "/" + query.fields.path : "" + }${query.fields.queryString ? "?" + query.fields.queryString : ""}` function updateQuery({ detail }) { query.fields[schema.type] = detail.value diff --git a/packages/server/src/integrations/rest.ts b/packages/server/src/integrations/rest.ts index d6cf71e324..9c6ece52d6 100644 --- a/packages/server/src/integrations/rest.ts +++ b/packages/server/src/integrations/rest.ts @@ -152,13 +152,17 @@ module RestModule { } } + getUrl(path: string, queryString: string): string { + return `${this.config.url}/${path}?${queryString}` + } + async create({ path = "", queryString = "", headers = {}, json = {} }) { this.headers = { ...this.config.defaultHeaders, ...headers, } - const response = await fetch(this.config.url + path + queryString, { + const response = await fetch(this.getUrl(path, queryString), { method: "POST", headers: this.headers, body: JSON.stringify(json), @@ -173,7 +177,7 @@ module RestModule { ...headers, } - const response = await fetch(this.config.url + path + queryString, { + const response = await fetch(this.getUrl(path, queryString), { headers: this.headers, }) @@ -186,7 +190,7 @@ module RestModule { ...headers, } - const response = await fetch(this.config.url + path + queryString, { + const response = await fetch(this.getUrl(path, queryString), { method: "POST", headers: this.headers, body: JSON.stringify(json), @@ -201,7 +205,7 @@ module RestModule { ...headers, } - const response = await fetch(this.config.url + path + queryString, { + const response = await fetch(this.getUrl(path, queryString), { method: "PATCH", headers: this.headers, body: JSON.stringify(json), @@ -216,7 +220,7 @@ module RestModule { ...headers, } - const response = await fetch(this.config.url + path + queryString, { + const response = await fetch(this.getUrl(path, queryString), { method: "DELETE", headers: this.headers, }) diff --git a/packages/server/src/integrations/tests/rest.spec.js b/packages/server/src/integrations/tests/rest.spec.js index a0b12f6c17..cb69d3c38d 100644 --- a/packages/server/src/integrations/tests/rest.spec.js +++ b/packages/server/src/integrations/tests/rest.spec.js @@ -1,98 +1,100 @@ -jest.mock("node-fetch", () => jest.fn(() => ({ json: jest.fn(), text: jest.fn() }))) +jest.mock("node-fetch", () => + jest.fn(() => ({ json: jest.fn(), text: jest.fn() })) +) const fetch = require("node-fetch") const RestIntegration = require("../rest") class TestConfiguration { constructor(config = {}) { - this.integration = new RestIntegration.integration(config) + this.integration = new RestIntegration.integration(config) } } describe("REST Integration", () => { const BASE_URL = "https://myapi.com" - let config + let config beforeEach(() => { config = new TestConfiguration({ - url: BASE_URL + url: BASE_URL, }) }) it("calls the create method with the correct params", async () => { const query = { - path: "/api", - queryString: "?test=1", + path: "api", + queryString: "test=1", headers: { - Accept: "application/json" + Accept: "application/json", }, json: { - name: "test" - } + name: "test", + }, } const response = await config.integration.create(query) expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { method: "POST", - body: "{\"name\":\"test\"}", + body: '{"name":"test"}', headers: { - Accept: "application/json" - } + Accept: "application/json", + }, }) }) it("calls the read method with the correct params", async () => { const query = { - path: "/api", - queryString: "?test=1", + path: "api", + queryString: "test=1", headers: { - Accept: "text/html" - } + Accept: "text/html", + }, } const response = await config.integration.read(query) expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { headers: { - Accept: "text/html" - } + Accept: "text/html", + }, }) }) it("calls the update method with the correct params", async () => { const query = { - path: "/api", - queryString: "?test=1", + path: "api", + queryString: "test=1", headers: { - Accept: "application/json" + Accept: "application/json", }, json: { - name: "test" - } + name: "test", + }, } const response = await config.integration.update(query) expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { method: "POST", - body: "{\"name\":\"test\"}", + body: '{"name":"test"}', headers: { - Accept: "application/json" - } + Accept: "application/json", + }, }) }) it("calls the delete method with the correct params", async () => { const query = { - path: "/api", - queryString: "?test=1", + path: "api", + queryString: "test=1", headers: { - Accept: "application/json" + Accept: "application/json", }, json: { - name: "test" - } + name: "test", + }, } const response = await config.integration.delete(query) expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { method: "DELETE", headers: { - Accept: "application/json" - } + Accept: "application/json", + }, }) }) -}) \ No newline at end of file +})