2020-10-29 09:35:06 +13:00
|
|
|
const env = require("../environment")
|
2020-11-04 02:45:49 +13:00
|
|
|
const { DocumentTypes, SEPARATOR } = require("../db/utils")
|
|
|
|
|
|
|
|
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
2020-10-29 09:35:06 +13:00
|
|
|
|
2020-10-20 03:33:26 +13:00
|
|
|
exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms))
|
2020-10-27 06:49:33 +13:00
|
|
|
|
|
|
|
exports.isDev = () => {
|
|
|
|
return (
|
2020-10-29 23:45:02 +13:00
|
|
|
!env.CLOUD &&
|
2020-10-29 09:35:06 +13:00
|
|
|
env.NODE_ENV !== "production" &&
|
|
|
|
env.NODE_ENV !== "jest" &&
|
|
|
|
env.NODE_ENV !== "cypress"
|
2020-10-27 06:49:33 +13:00
|
|
|
)
|
|
|
|
}
|
2020-11-03 04:46:08 +13:00
|
|
|
|
2020-11-03 09:14:10 +13:00
|
|
|
/**
|
|
|
|
* Given a request tries to find the appId, which can be located in various places
|
|
|
|
* @param {object} ctx The main request body to look through.
|
|
|
|
* @returns {string|undefined} If an appId was found it will be returned.
|
|
|
|
*/
|
2020-11-03 04:46:08 +13:00
|
|
|
exports.getAppId = ctx => {
|
2020-11-04 02:45:49 +13:00
|
|
|
let appId = ctx.headers["x-budibase-app-id"]
|
|
|
|
if (!appId) {
|
|
|
|
appId = env.CLOUD ? ctx.subdomains[1] : ctx.params.appId
|
|
|
|
}
|
2020-11-03 09:14:10 +13:00
|
|
|
// look in body if can't find it in subdomain
|
|
|
|
if (!appId && ctx.request.body && ctx.request.body.appId) {
|
|
|
|
appId = ctx.request.body.appId
|
|
|
|
}
|
2020-11-04 02:45:49 +13:00
|
|
|
let appPath =
|
|
|
|
ctx.request.headers.referrer ||
|
|
|
|
ctx.path.split("/").filter(subPath => subPath.startsWith(APP_PREFIX))
|
|
|
|
if (!appId && appPath.length !== 0) {
|
|
|
|
appId = appPath[0]
|
2020-11-03 09:14:10 +13:00
|
|
|
}
|
|
|
|
return appId
|
|
|
|
}
|
2020-11-03 04:46:08 +13:00
|
|
|
|
2020-11-03 09:14:10 +13:00
|
|
|
/**
|
|
|
|
* Get the name of the cookie which is to be updated/retrieved
|
2020-11-04 02:45:49 +13:00
|
|
|
* @param {string|undefined|null} name OPTIONAL can specify the specific app if previewing etc
|
2020-11-03 09:14:10 +13:00
|
|
|
* @returns {string} The name of the token trying to find
|
|
|
|
*/
|
2020-11-04 02:45:49 +13:00
|
|
|
exports.getCookieName = (name = "builder") => {
|
2020-11-03 11:46:31 +13:00
|
|
|
let environment = env.CLOUD ? "cloud" : "local"
|
2020-11-04 02:45:49 +13:00
|
|
|
return `budibase:${name}:${environment}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a cookie for the request, has a hardcoded expiry.
|
|
|
|
* @param {object} ctx The request which is to be manipulated.
|
|
|
|
* @param {string} name The name of the cookie to set.
|
|
|
|
* @param {string|object} value The value of cookie which will be set.
|
|
|
|
*/
|
|
|
|
exports.setCookie = (ctx, name, value) => {
|
|
|
|
const expires = new Date()
|
|
|
|
expires.setDate(expires.getDate() + 1)
|
|
|
|
|
|
|
|
ctx.cookies.set(exports.getCookieName(name), value, {
|
|
|
|
expires,
|
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
|
|
|
overwrite: true,
|
|
|
|
})
|
2020-11-03 04:46:08 +13:00
|
|
|
}
|