@ -1,4 +1,7 @@
const env = require ( "../environment" )
const env = require ( "../environment" )
const { DocumentTypes , SEPARATOR } = require ( "../db/utils" )
const APP_PREFIX = DocumentTypes . APP + SEPARATOR
exports . wait = ms => new Promise ( resolve => setTimeout ( resolve , ms ) )
exports . wait = ms => new Promise ( resolve => setTimeout ( resolve , ms ) )
@ -17,26 +20,47 @@ exports.isDev = () => {
* @ returns { string | undefined } If an appId was found it will be returned .
* @ returns { string | undefined } If an appId was found it will be returned .
* /
* /
exports . getAppId = ctx => {
exports . getAppId = ctx => {
let appId = env . CLOUD ? ctx . subdomains [ 1 ] : ctx . params . appId
let appId = ctx . headers [ "x-budibase-app-id" ]
if ( ! appId ) {
appId = env . CLOUD ? ctx . subdomains [ 1 ] : ctx . params . appId
}
// look in body if can't find it in subdomain
// look in body if can't find it in subdomain
if ( ! appId && ctx . request . body && ctx . request . body . appId ) {
if ( ! appId && ctx . request . body && ctx . request . body . appId ) {
appId = ctx . request . body . appId
appId = ctx . request . body . appId
}
}
// if appId can't be determined from path param or subdomain
let appPath =
if ( ! appId && ctx . request . headers . referer ) {
ctx . request . headers . referrer ||
const url = new URL ( ctx . request . headers . referer )
ctx . path . split ( "/" ) . filter ( subPath => subPath . startsWith ( APP_PREFIX ) )
// remove leading and trailing slashes from appId
if ( ! appId && appPath . length !== 0 ) {
appId = url . pathname . replace ( /\//g , "" )
appId = appPath [ 0 ]
}
}
return appId
return appId
}
}
/ * *
/ * *
* Get the name of the cookie which is to be updated / retrieved
* 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
* @ param { string | undefined | null } name OPTIONAL can specify the specific app if previewing etc
* @ returns { string } The name of the token trying to find
* @ returns { string } The name of the token trying to find
* /
* /
exports . getCookieName = ( appId = null ) => {
exports . getCookieName = ( name = "builder" ) => {
let environment = env . CLOUD ? "cloud" : "local"
let environment = env . CLOUD ? "cloud" : "local"
return ` budibase: ${ appId ? appId : "builder" } : ${ environment } `
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 ,
} )
}
}