@samitouri / QOSami-HFS / commits / ab4bcfff

plugins: config.getError

Massimo Melina committed Mar 23, 2025 at 11:38 UTC ab4bcfff53cbef0a7ddf3a92e431f5e9904ae043
2 files changed +19 -6
admin/src/InstalledPlugins.ts
+1
@@ -246,6 +246,7 @@ function makeFields(config: any, values: any) {
246 if (!o) return
247 let { type, defaultValue, frontend, showIf, ...rest } = o
248 try {
249 + rest.getError = eval(rest.getError)
250 if (typeof showIf === 'string') // compile once
251 rest.showIf = showIf = eval(showIf) // eval is normally considered a threat, but this code is coming from a plugin that's already running on your server, so you already decided to trust it. Here it will run in your browser, and inside the page that administrating the same server.
252 if (showIf && !showIf(values))
dev-plugins.md
+18 -6
@@ -28,6 +28,15 @@ The list above may become outdated, but you can always find an updated version a
28
29 For example, put a file "login.png" into "icons" to customize that icon.
30
31 +## Definitions
32 +
33 +In this document we define some types using pseudo-typescript syntax.
34 +We use some predefined types for brevity:
35 +
36 +`Promisable<Type> = Type | Promise<Type>` where Type can be wrapped in a promise or not (direct).
37 +
38 +`Functionable<Type, Arguments> = Type | ((...args: Arguments) => Type)` where Type can be returned by a function or not (direct).
39 +
40 ## Exported object
41
42 `plugin.js` is a javascript module (executed by Node.js), and its main way to communicate with HFS is by exporting things.
@@ -99,7 +108,7 @@ All the following properties are optional unless otherwise specified.
108 **WARNING:** All the properties above are a bit special and must go in `exports` only (thus, not returned in `init`) and the syntax
109 used must be strictly JSON (thus, no single quotes, only double quotes for strings and objects), and must fit one line.
110
102 -- `init: (api: object) => void | object | function` described in the previous section. If an object is returned,
111 +- `init: (api: object) => (void | object | function)` described in the previous section. If an object is returned,
112 it will be merged with other "exported" properties described in this section, so you can return `{ unload }` for example.
113 If you return a function, this is just a shorter way to return the `unload`.
114 - `frontend_css: string | string[]` path to one or more css files that you want the frontend to load. These are to be placed in the `public` folder (refer below).
@@ -126,7 +135,7 @@ used must be strictly JSON (thus, no single quotes, only double quotes for strin
135 - `onDirEntry: ({ entry: DirEntry, listUri: string, ctx, node: VfsNode }) => Promisable<void | false>`
136 by providing this callback you can manipulate the record that is sent to the frontend (`entry`),
137 or you can return false to exclude this entry from the results. Refer to source `frontend/src/state.ts`.
129 -- `config: { [key]: FieldDescriptor } | function` declare a set of admin-configurable values owned by the plugin
138 +- `config: Functionable<{ [key]: FieldDescriptor }, values:object>` declare a set of admin-configurable values owned by the plugin
139 that will be displayed inside Admin-panel for change. Each property is identified by its key,
140 and the descriptor is another object with options about the field.
141
@@ -147,7 +156,7 @@ used must be strictly JSON (thus, no single quotes, only double quotes for strin
156 To handle more complex cases, you can pass a function to `config` instead of an object. The function will receive a parameter `values`.
157
158 - `configDialog: DialogOptions` object to override dialog options. Please refer to sources for details.
150 -- `onFrontendConfig: (config: object) => void | object` manipulate config values exposed to frontend.
159 +- `onFrontendConfig: (config: object) => (void | object)` manipulate config values exposed to frontend.
160 - `customHtml: object | () => object` return custom-html sections programmatically.
161 - `customRest: { [name]: (parameters: object, ctx) => any }` declare backend functions to be called by frontend with `HFS.customRestCall`
162 E.g.
@@ -169,9 +178,11 @@ A FieldDescriptor is an object and can be empty. Currently, these optional prope
178 - `showIf: (values: object) => boolean` only show this field if the function returns truthy.
179 Must not reference variables of the outer scope. [See example](https://github.com/rejetto/rich-folder/blob/main/dist/plugin.js).
180 - `frontend: boolean` expose this setting on the frontend, so that javascript can access it
172 - using `HFS.getPluginConfig()[CONFIG_KEY]` but also css can access it as `var(--PLUGIN_NAME-CONFIG_KEY)`.
173 - Hint: if you need to use a numeric config in CSS but you need to add a unit (like `em`),
174 - the trick is to use something like this `calc(var(--plugin-something) * 1em)`.
181 + using `HFS.getPluginConfig()[CONFIG_KEY]` but also css can access it as `var(--PLUGIN_NAME-CONFIG_KEY)`.
182 + Hint: if you need to use a numeric config in CSS but you need to add a unit (like `em`),
183 + the trick is to use something like this `calc(var(--plugin-something) * 1em)`.
184 +- `getError: (value: any, { values: object, fields: object }) => (boolean | string)` a validator for the field.
185 + Return false if value is valid, true for generic error, or a string for specific error.
186
187 Based on `type`, other properties are supported:
188 - `string`
@@ -810,6 +821,7 @@ If you want to override a text regardless of the language, use the special langu
821 - HFS.watchState added third parameter
822 - frontend events: async for fileMenu and html-producers
823 - config.type: date_time, net_mask
824 + - config.getError
825 - 11.6 (v0.56.0)
826 - api.setError
827 - frontend events: afterBreadcrumbs, afterFolderStats, afterFilter