Browse Source

Merge pull request #2894 from mslourens/update_rest_url_with_slash_questionmark

add a slash before the path and a questionmark before the querystring
gh-pages
Martin McKeaveney 5 years ago
committed by GitHub
parent
commit
eb64cb9b07
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      packages/builder/src/components/integration/index.svelte
  2. 14
      packages/server/src/integrations/rest.ts
  3. 70
      packages/server/src/integrations/tests/rest.spec.js

6
packages/builder/src/components/integration/index.svelte

@ -17,9 +17,9 @@
$: urlDisplay = $: urlDisplay =
schema.urlDisplay && schema.urlDisplay &&
`${datasource.config.url}${query.fields.path ?? ""}${ `${datasource.config.url}${
query.fields.queryString ?? "" query.fields.path ? "/" + query.fields.path : ""
}` }${query.fields.queryString ? "?" + query.fields.queryString : ""}`
function updateQuery({ detail }) { function updateQuery({ detail }) {
query.fields[schema.type] = detail.value query.fields[schema.type] = detail.value

14
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 = {} }) { async create({ path = "", queryString = "", headers = {}, json = {} }) {
this.headers = { this.headers = {
...this.config.defaultHeaders, ...this.config.defaultHeaders,
...headers, ...headers,
} }
const response = await fetch(this.config.url + path + queryString, { const response = await fetch(this.getUrl(path, queryString), {
method: "POST", method: "POST",
headers: this.headers, headers: this.headers,
body: JSON.stringify(json), body: JSON.stringify(json),
@ -173,7 +177,7 @@ module RestModule {
...headers, ...headers,
} }
const response = await fetch(this.config.url + path + queryString, { const response = await fetch(this.getUrl(path, queryString), {
headers: this.headers, headers: this.headers,
}) })
@ -186,7 +190,7 @@ module RestModule {
...headers, ...headers,
} }
const response = await fetch(this.config.url + path + queryString, { const response = await fetch(this.getUrl(path, queryString), {
method: "POST", method: "POST",
headers: this.headers, headers: this.headers,
body: JSON.stringify(json), body: JSON.stringify(json),
@ -201,7 +205,7 @@ module RestModule {
...headers, ...headers,
} }
const response = await fetch(this.config.url + path + queryString, { const response = await fetch(this.getUrl(path, queryString), {
method: "PATCH", method: "PATCH",
headers: this.headers, headers: this.headers,
body: JSON.stringify(json), body: JSON.stringify(json),
@ -216,7 +220,7 @@ module RestModule {
...headers, ...headers,
} }
const response = await fetch(this.config.url + path + queryString, { const response = await fetch(this.getUrl(path, queryString), {
method: "DELETE", method: "DELETE",
headers: this.headers, headers: this.headers,
}) })

70
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 fetch = require("node-fetch")
const RestIntegration = require("../rest") const RestIntegration = require("../rest")
class TestConfiguration { class TestConfiguration {
constructor(config = {}) { constructor(config = {}) {
this.integration = new RestIntegration.integration(config) this.integration = new RestIntegration.integration(config)
} }
} }
describe("REST Integration", () => { describe("REST Integration", () => {
const BASE_URL = "https://myapi.com" const BASE_URL = "https://myapi.com"
let config let config
beforeEach(() => { beforeEach(() => {
config = new TestConfiguration({ config = new TestConfiguration({
url: BASE_URL url: BASE_URL,
}) })
}) })
it("calls the create method with the correct params", async () => { it("calls the create method with the correct params", async () => {
const query = { const query = {
path: "/api", path: "api",
queryString: "?test=1", queryString: "test=1",
headers: { headers: {
Accept: "application/json" Accept: "application/json",
}, },
json: { json: {
name: "test" name: "test",
} },
} }
const response = await config.integration.create(query) const response = await config.integration.create(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "POST", method: "POST",
body: "{\"name\":\"test\"}", body: '{"name":"test"}',
headers: { headers: {
Accept: "application/json" Accept: "application/json",
} },
}) })
}) })
it("calls the read method with the correct params", async () => { it("calls the read method with the correct params", async () => {
const query = { const query = {
path: "/api", path: "api",
queryString: "?test=1", queryString: "test=1",
headers: { headers: {
Accept: "text/html" Accept: "text/html",
} },
} }
const response = await config.integration.read(query) const response = await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
headers: { headers: {
Accept: "text/html" Accept: "text/html",
} },
}) })
}) })
it("calls the update method with the correct params", async () => { it("calls the update method with the correct params", async () => {
const query = { const query = {
path: "/api", path: "api",
queryString: "?test=1", queryString: "test=1",
headers: { headers: {
Accept: "application/json" Accept: "application/json",
}, },
json: { json: {
name: "test" name: "test",
} },
} }
const response = await config.integration.update(query) const response = await config.integration.update(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "POST", method: "POST",
body: "{\"name\":\"test\"}", body: '{"name":"test"}',
headers: { headers: {
Accept: "application/json" Accept: "application/json",
} },
}) })
}) })
it("calls the delete method with the correct params", async () => { it("calls the delete method with the correct params", async () => {
const query = { const query = {
path: "/api", path: "api",
queryString: "?test=1", queryString: "test=1",
headers: { headers: {
Accept: "application/json" Accept: "application/json",
}, },
json: { json: {
name: "test" name: "test",
} },
} }
const response = await config.integration.delete(query) const response = await config.integration.delete(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "DELETE", method: "DELETE",
headers: { headers: {
Accept: "application/json" Accept: "application/json",
} },
}) })
}) })
}) })

Loading…
Cancel
Save