2020-10-28 21:35:06 +01:00
|
|
|
const env = require("../environment")
|
|
|
|
|
2020-10-19 16:33:26 +02:00
|
|
|
exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms))
|
2020-10-26 18:49:33 +01:00
|
|
|
|
|
|
|
exports.isDev = () => {
|
|
|
|
return (
|
2020-10-29 11:45:02 +01:00
|
|
|
!env.CLOUD &&
|
2020-10-28 21:35:06 +01:00
|
|
|
env.NODE_ENV !== "production" &&
|
|
|
|
env.NODE_ENV !== "jest" &&
|
|
|
|
env.NODE_ENV !== "cypress"
|
2020-10-26 18:49:33 +01:00
|
|
|
)
|
|
|
|
}
|
2020-11-02 16:46:08 +01:00
|
|
|
|
2020-11-02 21:14:10 +01: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-02 16:46:08 +01:00
|
|
|
exports.getAppId = ctx => {
|
2020-11-02 21:14:10 +01:00
|
|
|
let appId = env.CLOUD ? ctx.subdomains[1] : ctx.params.appId
|
|
|
|
// look in body if can't find it in subdomain
|
|
|
|
if (!appId && ctx.request.body && ctx.request.body.appId) {
|
|
|
|
appId = ctx.request.body.appId
|
|
|
|
}
|
|
|
|
// if appId can't be determined from path param or subdomain
|
|
|
|
if (!appId && ctx.request.headers.referer) {
|
|
|
|
const url = new URL(ctx.request.headers.referer)
|
|
|
|
// remove leading and trailing slashes from appId
|
|
|
|
appId = url.pathname.replace(/\//g, "")
|
|
|
|
}
|
|
|
|
return appId
|
|
|
|
}
|
2020-11-02 16:46:08 +01:00
|
|
|
|
2020-11-02 21:14:10 +01:00
|
|
|
/**
|
|
|
|
* Get the name of the cookie which is to be updated/retrieved
|
|
|
|
* @param {string|undefined|null} appId OPTIONAL can specify the specific app if previewing etc
|
|
|
|
* @returns {string} The name of the token trying to find
|
|
|
|
*/
|
|
|
|
exports.getCookieName = (appId = null) => {
|
2020-11-02 23:46:31 +01:00
|
|
|
let environment = env.CLOUD ? "cloud" : "local"
|
|
|
|
return `budibase:${appId ? appId : "builder"}:${environment}`
|
2020-11-02 16:46:08 +01:00
|
|
|
}
|