Types
This commit is contained in:
parent
05a169e2fd
commit
972cbf43b8
|
@ -63,7 +63,7 @@ function buildList(parts: any[], value: any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitBySpace(layer: string) {
|
function splitBySpace(layer: string) {
|
||||||
const parts = []
|
const parts: string[] = []
|
||||||
let started = null,
|
let started = null,
|
||||||
endChar = null,
|
endChar = null,
|
||||||
last = 0
|
last = 0
|
||||||
|
|
|
@ -27,11 +27,11 @@ dayjs.extend(dayjsTimezonePlugin)
|
||||||
* https://github.com/helpers/helper-date
|
* https://github.com/helpers/helper-date
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function isOptions(val) {
|
function isOptions(val: any) {
|
||||||
return typeof val === "object" && typeof val.hash === "object"
|
return typeof val === "object" && typeof val.hash === "object"
|
||||||
}
|
}
|
||||||
|
|
||||||
function isApp(thisArg) {
|
function isApp(thisArg: any) {
|
||||||
return (
|
return (
|
||||||
typeof thisArg === "object" &&
|
typeof thisArg === "object" &&
|
||||||
typeof thisArg.options === "object" &&
|
typeof thisArg.options === "object" &&
|
||||||
|
@ -39,7 +39,7 @@ function isApp(thisArg) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getContext(thisArg, locals, options) {
|
function getContext(thisArg: any, locals: any, options: any) {
|
||||||
if (isOptions(thisArg)) {
|
if (isOptions(thisArg)) {
|
||||||
return getContext({}, locals, thisArg)
|
return getContext({}, locals, thisArg)
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ function getContext(thisArg, locals, options) {
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
function initialConfig(str, pattern, options?) {
|
function initialConfig(str: any, pattern: any, options?: any) {
|
||||||
if (isOptions(pattern)) {
|
if (isOptions(pattern)) {
|
||||||
options = pattern
|
options = pattern
|
||||||
pattern = null
|
pattern = null
|
||||||
|
@ -82,7 +82,7 @@ function initialConfig(str, pattern, options?) {
|
||||||
return { str, pattern, options }
|
return { str, pattern, options }
|
||||||
}
|
}
|
||||||
|
|
||||||
function setLocale(str, pattern, options?) {
|
function setLocale(str: any, pattern: any, options?: any) {
|
||||||
// if options is null then it'll get updated here
|
// if options is null then it'll get updated here
|
||||||
const config = initialConfig(str, pattern, options)
|
const config = initialConfig(str, pattern, options)
|
||||||
const defaults = { lang: "en", date: new Date(config.str) }
|
const defaults = { lang: "en", date: new Date(config.str) }
|
||||||
|
@ -93,7 +93,7 @@ function setLocale(str, pattern, options?) {
|
||||||
dayjs.locale(opts.lang || opts.language)
|
dayjs.locale(opts.lang || opts.language)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const date = (str, pattern, options) => {
|
export const date = (str: any, pattern: any, options: any) => {
|
||||||
const config = initialConfig(str, pattern, options)
|
const config = initialConfig(str, pattern, options)
|
||||||
|
|
||||||
// if no args are passed, return a formatted date
|
// if no args are passed, return a formatted date
|
||||||
|
@ -119,7 +119,7 @@ export const date = (str, pattern, options) => {
|
||||||
return date.format(config.pattern)
|
return date.format(config.pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const duration = (str, pattern, format) => {
|
export const duration = (str: any, pattern: any, format: any) => {
|
||||||
const config = initialConfig(str, pattern)
|
const config = initialConfig(str, pattern)
|
||||||
|
|
||||||
setLocale(config.str, config.pattern)
|
setLocale(config.str, config.pattern)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import helpers from "@budibase/handlebars-helpers"
|
const helpers = require("@budibase/handlebars-helpers")
|
||||||
import { date, duration } from "./date"
|
import { date, duration } from "./date"
|
||||||
import { HelperFunctionBuiltin } from "./constants"
|
import { HelperFunctionBuiltin } from "./constants"
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,23 @@
|
||||||
import { atob, isBackendService, isJSAllowed } from "../utilities"
|
import { atob, isBackendService, isJSAllowed } from "../utilities"
|
||||||
import cloneDeep from "lodash.clonedeep"
|
import cloneDeep from "lodash/fp/clonedeep"
|
||||||
import { LITERAL_MARKER } from "../helpers/constants"
|
import { LITERAL_MARKER } from "../helpers/constants"
|
||||||
import { getJsHelperList } from "./list"
|
import { getJsHelperList } from "./list"
|
||||||
|
|
||||||
// The method of executing JS scripts depends on the bundle being built.
|
// The method of executing JS scripts depends on the bundle being built.
|
||||||
// This setter is used in the entrypoint (either index.js or index.mjs).
|
// This setter is used in the entrypoint (either index.js or index.mjs).
|
||||||
let runJS
|
let runJS: (js: string, context: any) => any
|
||||||
export const setJSRunner = runner => (runJS = runner)
|
export const setJSRunner = (runner: typeof runJS) => (runJS = runner)
|
||||||
|
|
||||||
export const removeJSRunner = () => (runJS = undefined)
|
export const removeJSRunner = () => {
|
||||||
|
runJS = undefined
|
||||||
|
}
|
||||||
|
|
||||||
let onErrorLog
|
let onErrorLog: (message: string) => void
|
||||||
export const setOnErrorLog = delegate => (onErrorLog = delegate)
|
export const setOnErrorLog = (delegate: typeof onErrorLog) =>
|
||||||
|
(onErrorLog = delegate)
|
||||||
|
|
||||||
// Helper utility to strip square brackets from a value
|
// Helper utility to strip square brackets from a value
|
||||||
const removeSquareBrackets = value => {
|
const removeSquareBrackets = (value: string) => {
|
||||||
if (!value || typeof value !== "string") {
|
if (!value || typeof value !== "string") {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
@ -28,7 +31,7 @@ const removeSquareBrackets = value => {
|
||||||
|
|
||||||
// Our context getter function provided to JS code as $.
|
// Our context getter function provided to JS code as $.
|
||||||
// Extracts a value from context.
|
// Extracts a value from context.
|
||||||
const getContextValue = (path, context) => {
|
const getContextValue = (path: string, context: any) => {
|
||||||
let data = context
|
let data = context
|
||||||
path.split(".").forEach(key => {
|
path.split(".").forEach(key => {
|
||||||
if (data == null || typeof data !== "object") {
|
if (data == null || typeof data !== "object") {
|
||||||
|
@ -40,7 +43,7 @@ const getContextValue = (path, context) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluates JS code against a certain context
|
// Evaluates JS code against a certain context
|
||||||
export function processJS(handlebars, context) {
|
export function processJS(handlebars: string, context: any) {
|
||||||
if (!isJSAllowed() || (isBackendService() && !runJS)) {
|
if (!isJSAllowed() || (isBackendService() && !runJS)) {
|
||||||
throw new Error("JS disabled in environment.")
|
throw new Error("JS disabled in environment.")
|
||||||
}
|
}
|
||||||
|
@ -53,7 +56,7 @@ export function processJS(handlebars, context) {
|
||||||
// We clone the context to avoid mutation in the binding affecting real
|
// We clone the context to avoid mutation in the binding affecting real
|
||||||
// app context.
|
// app context.
|
||||||
const sandboxContext = {
|
const sandboxContext = {
|
||||||
$: path => getContextValue(path, cloneDeep(context)),
|
$: (path: string) => getContextValue(path, cloneDeep(context)),
|
||||||
helpers: getJsHelperList(),
|
helpers: getJsHelperList(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ const addedHelpers = {
|
||||||
duration: duration,
|
duration: duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
let helpers = undefined
|
let helpers: Record<string, any> = undefined
|
||||||
|
|
||||||
export function getJsHelperList() {
|
export function getJsHelperList() {
|
||||||
if (helpers) {
|
if (helpers) {
|
||||||
|
@ -31,11 +31,12 @@ export function getJsHelperList() {
|
||||||
for (let collection of Object.values(getExternalCollections())) {
|
for (let collection of Object.values(getExternalCollections())) {
|
||||||
for (let [key, func] of Object.entries(collection)) {
|
for (let [key, func] of Object.entries(collection)) {
|
||||||
// Handlebars injects the hbs options to the helpers by default. We are adding an empty {} as a last parameter to simulate it
|
// Handlebars injects the hbs options to the helpers by default. We are adding an empty {} as a last parameter to simulate it
|
||||||
helpers[key] = (...props) => func(...props, {})
|
helpers[key] = (...props: any) => func(...props, {})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let key of Object.keys(addedHelpers)) {
|
helpers = {
|
||||||
helpers[key] = addedHelpers[key]
|
...helpers,
|
||||||
|
addedHelpers,
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const toRemove of helpersToRemoveForJs) {
|
for (const toRemove of helpersToRemoveForJs) {
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
|
"noImplicitAny": true,
|
||||||
"lib": ["dom"],
|
"lib": ["dom"],
|
||||||
"allowJs": true,
|
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"types": ["node", "jest"],
|
"types": ["node", "jest"],
|
||||||
|
|
Loading…
Reference in New Issue