Search…
Static
Static Decorators
Using static decorators, a script can declare environment behavior that is picked up and merged into the runtime. Because static decorators are processed at compile-time, they must not include variables in their declarations.
Only object expressions, array literals, primitives, and numeric literals preceded by the - unary expression are allowed.
1
const { job } = require('decorators'),
2
cron = '*/10 * * * *'
3
4
class Jobs {
5
6
@job(cron) // invalid. contains a variable
7
@job('*/10 * * * *') // valid
8
@job('*/10 * * * *', { weight: -1 }) // valid
9
@job('*/10 * * * *', { weight: +1 }) // invalid
10
@job('*/10 * * * *', { weight: 1 }) // valid
11
@job('*/10 * * * *', { principal: script.principal }) // invalid
12
@job('*/10 * * * *', { principal: consts.principals.anonymous }) // invalid. contains a variable
13
@job('*/10 * * * *', { principal: 'anonymous' }) // valid
14
doJob() {
15
}
16
}
Copied!
Stored objects, scripts and policies can be named, and must be unique. If declared with a static decorator, a developer can override named items by duplicating the name and assigning a greater weight. See each decorator's documentation for runtime sorting details. the default weight is 0.
1
const { job } = require('decorators')
2
3
// overrides a saved job script by the same name
4
class Jobs {
5
6
@job('*/10 * * * *', { name: 'c_existing_job', weight: 1})
7
doJob() {
8
}
9
}
10
11
// overrides the statically declared job of the same name and changes the job frequency
12
class CustomJobs extends Jobs {
13
14
@job('*/5 * * * *', { name: 'c_existing_job', weight: 2})
15
doJob(...args) {
16
super.doJob(...args)
17
}
18
}
Copied!
Any number of decorators may stack on top of one another and will all be added to the runtime. Although their order is determined by weight and priority (depending on the type), not the order in which they are declared.
1
const { route, trigger } = require('decorators')
2
class Foo {
3
4
@route('GET foos/:param') // called second
5
getFoos() {
6
}
7
8
@route('GET foos/bars', { priority: 10 }) // called first.
9
getFooBars() {
10
}
11
12
}
Copied!
If two or more similarly named static decorator declarations are made that share the same weight, the one selected is arbitrary and must be determined via examining the environment runtime to determined which was selected (org.runtime). As such, it's recommended to always assign weights to named declarations.
1
const { route } = require('decorators')
2
class Foo {
3
4
@route('GET foos', { c_name: 'c_existing_saved_foo_route' }) // may or may not be selected, though only 1 will be added.
5
getFoos() {
6
}
7
8
@route('GET bars', { c_name: 'c_existing_saved_bar_route', weight: 1 }) // overrides saved route script with default weight
9
getBars() {
10
}
11
12
@route('GET foos', { priority: 999 }) // no name, so will stack and run based on priority alone.
13
@route('GET bars', { priority: 999 })
14
getBeforeBars({ body, next }) {
15
if (body.isArray()) {
16
body(body.getLength(), 'pushed')
17
}
18
next() // allow the next route to run.
19
}
20
21
}
Copied!
When combining static environment decorators with runtime decorators, the order of the decorators does not apply; static decorators are always applied prior to the script being allocated a sandbox.
In the following example, the principal will be c_service_account_with_site_manager_role even though @as precedes @route (which runs as the runtime caller by default) because @as is evaluated at runtime. Also, the route will only allow a caller with the c_site_manager role to run the route because the @as decorator does not assign the c_service_account_with_site_manager_role until after the @route has been evaluated.
1
const { as, route } = require('decorators')
2
class Foo {
3
4
@as('c_service_account_with_site_manager_role')
5
@route('GET foos', { acl: 'role.c_site_manager'})
6
getFoos() {
7
return script.principal // c_service_account
8
}
9
10
}
Copied!
Last modified 1mo ago
Copy link