From f2e1f1f4e947ca05e93e2bf8c27f190d61d40797 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 9 Nov 2020 09:42:35 +0000 Subject: [PATCH] The API that the components library would use was not always consistent with the API client library would use and this would sometimes break things. --- packages/client/src/api/index.js | 8 +++++-- packages/client/src/state/bbComponentApi.js | 22 +++++-------------- .../server/src/middleware/authenticated.js | 1 + 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/packages/client/src/api/index.js b/packages/client/src/api/index.js index 158ad8e800..0b79081187 100644 --- a/packages/client/src/api/index.js +++ b/packages/client/src/api/index.js @@ -1,8 +1,8 @@ import { authenticate } from "./authenticate" import { getAppId } from "../render/getAppId" -const apiCall = method => async ({ url, body }) => { - const response = await fetch(url, { +export async function baseApiCall(method, url, body) { + return await fetch(url, { method: method, headers: { "Content-Type": "application/json", @@ -11,6 +11,10 @@ const apiCall = method => async ({ url, body }) => { body: body && JSON.stringify(body), credentials: "same-origin", }) +} + +const apiCall = method => async ({ url, body }) => { + const response = await baseApiCall(method, url, body) switch (response.status) { case 200: diff --git a/packages/client/src/state/bbComponentApi.js b/packages/client/src/state/bbComponentApi.js index 99503bb2b9..1c2819f0ad 100644 --- a/packages/client/src/state/bbComponentApi.js +++ b/packages/client/src/state/bbComponentApi.js @@ -1,30 +1,18 @@ import setBindableComponentProp from "./setBindableComponentProp" import { attachChildren } from "../render/attachChildren" import store from "../state/store" - -export const trimSlash = str => str.replace(/^\/+|\/+$/g, "") +import { baseApiCall } from "../api/index" export const bbFactory = ({ componentLibraries, onScreenSlotRendered, runEventActions, }) => { - const apiCall = method => (url, body) => { - return fetch(url, { - method: method, - headers: { - "Content-Type": "application/json", - }, - body: body && JSON.stringify(body), - credentials: "same-origin", - }) - } - const api = { - post: apiCall("POST"), - get: apiCall("GET"), - patch: apiCall("PATCH"), - delete: apiCall("DELETE"), + post: (url, body) => baseApiCall("POST", url, body), + get: (url, body) => baseApiCall("GET", url, body), + patch: (url, body) => baseApiCall("PATCH", url, body), + delete: (url, body) => baseApiCall("DELETE", url, body), } return (treeNode, setupState) => { diff --git a/packages/server/src/middleware/authenticated.js b/packages/server/src/middleware/authenticated.js index a29fa9f51c..529581f362 100644 --- a/packages/server/src/middleware/authenticated.js +++ b/packages/server/src/middleware/authenticated.js @@ -42,6 +42,7 @@ module.exports = async (ctx, next) => { if (!token) { ctx.auth.authenticated = false + ctx.appId = appId ctx.user = { appId, }