@opengis/fastify-table 1.2.92 → 1.2.94

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.2.92",
3
+ "version": "1.2.94",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,8 +1,18 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+
1
3
  import userTemplateDir from './userTemplateDir.js';
4
+ import customTokens from './customTokens.js';
5
+ import yml2json from '../../yml/funcs/yml2json.js'
2
6
 
3
7
  export default function addTemplateDir(dir) {
4
8
  if (dir && !userTemplateDir.includes(dir)) {
5
9
  userTemplateDir.push(dir);
10
+
11
+ if (existsSync(`${dir}/tokens.yml`)) {
12
+ const json = yml2json(readFileSync(`${dir}/tokens.yml`));
13
+ Object.assign(customTokens, { ...json });
14
+ console.log('load user tokens', dir);
15
+ }
6
16
  }
7
17
  return userTemplateDir;
8
18
  }
@@ -0,0 +1 @@
1
+ export default {}
@@ -100,7 +100,7 @@ export default async function getFilterSQL({
100
100
  // const cls = await getTemplate(['cls', 'select'], el.data); // only git cls
101
101
  const cls = await getSelect(el.data, pg); // git + db cls
102
102
 
103
- if (Array.isArray(cls) && cls?.length) {
103
+ if (Array.isArray(cls?.arr) && cls.arr.length) {
104
104
  Object.assign(el, { cls: el.data, options: cls, select: `select code, name from admin.cls where parent='${el.data}'` });
105
105
  }
106
106
  else if (typeof (cls?.sql || cls) === 'string') {
@@ -12,7 +12,9 @@ export default function formatValue({
12
12
  if (!name
13
13
  || !value
14
14
  || (!dataTypeID && !extra && !sql && !options)
15
- || extra?.input?.toLowerCase?.() === 'datatable') {
15
+ || extra?.input?.toLowerCase?.() === 'datatable'
16
+ || !['check', 'autocomplete', 'tags', 'avatar', 'radio'].includes(filterType) && options?.find?.(el => el?.sql)
17
+ ) {
16
18
  return {};
17
19
  }
18
20
 
@@ -87,10 +89,10 @@ export default function formatValue({
87
89
  }
88
90
 
89
91
  /* select query - from admin.cls / filter options */
90
- if (['check', 'autocomplete', 'text', 'tags', 'avatar', 'radio'].includes(filterType)) {
92
+ if (['check', 'autocomplete', 'tags', 'avatar', 'radio'].includes(filterType)) {
91
93
  // multiple checks with predefined query
92
- if (filterType === 'check' && options?.length) {
93
- const query = options.filter(el => value.split(',').includes(el.id)).map(el => el.sql).join(' and ') || 'false';
94
+ if (options?.find?.(el => el?.sql)) {
95
+ const query = options.filter(el => value.split(',').includes(el.id)).map(el => el.sql || 'false').join(' and ') || 'false';
94
96
  return { op: '=', query };
95
97
  }
96
98
 
@@ -1,10 +1,12 @@
1
- import path from 'path';
2
- import fs, { existsSync, readdirSync } from 'fs';
1
+ import path from 'node:path';
2
+ import fs, { existsSync, readdirSync, readFileSync } from 'node:fs';
3
3
 
4
4
  import config from '../../../../config.js';
5
5
 
6
6
  import loadTemplatePath from './loadTemplatePath.js';
7
7
  import userTemplateDir from './userTemplateDir.js';
8
+ import customTokens from './customTokens.js';
9
+ import yml2json from '../../yml/funcs/yml2json.js';
8
10
 
9
11
  const cwd = process.cwd();
10
12
 
@@ -18,8 +20,16 @@ export default function getTemplatePath(type) {
18
20
 
19
21
  const moduleList = [];
20
22
  const moduleDir = path.join(cwd, 'module');
23
+
21
24
  if (fs.existsSync(moduleDir) && !moduleList.length) {
22
- readdirSync(moduleDir).forEach(el => moduleList.push(path.join(moduleDir, el, type)));
25
+ readdirSync(moduleDir).forEach(el => {
26
+ if (existsSync(path.join(cwd, 'module', el, 'tokens.yml'))) {
27
+ const json = yml2json(readFileSync(path.join(cwd, 'module', el, 'tokens.yml')));
28
+ Object.assign(customTokens, { ...json });
29
+ console.log('load user tokens: root', `${cwd}/${el}`);
30
+ }
31
+ moduleList.push(path.join(moduleDir, el, type))
32
+ });
23
33
  }
24
34
 
25
35
  // add user template dir
@@ -0,0 +1 @@
1
+ export default {}
@@ -0,0 +1,39 @@
1
+ import { customTokens, userTokens, handlebarsSync, setOpt, getOpt } from "../../../../utils.js";
2
+
3
+ export default async function userTokensAPI({ params = {}, user = {} }, reply) {
4
+ if (!user?.uid) {
5
+ return reply.status(403).send('access restricted: user');
6
+ }
7
+
8
+ if (!params?.token) {
9
+ return reply.status(400).send('not enough params: token');
10
+ }
11
+
12
+ if (!customTokens[params.token]) {
13
+ return reply.status(404).send('token not found');
14
+ }
15
+
16
+ // return from cache
17
+ if (userTokens[user.uid]?.[params.token]) {
18
+ const opt = await getOpt(userTokens[user.uid][params.token], user.uid);
19
+ if (opt) return userTokens[user.uid][params.token];
20
+ }
21
+
22
+ const token = setOpt(
23
+ {
24
+ ...customTokens[params.token],
25
+ query: handlebarsSync.compile(customTokens[params.token]?.query || 'true')({ user, uid: user.uid })
26
+ },
27
+ user.uid,
28
+ );
29
+
30
+ // first api call from user
31
+ if (!userTokens[user.uid]) {
32
+ userTokens[user.uid] = {};
33
+ }
34
+
35
+ // assign token
36
+ userTokens[user.uid][params.token] = token;
37
+
38
+ return token;
39
+ }
@@ -1,11 +1,12 @@
1
1
  import nextId from './controllers/next.id.js';
2
2
  import statusMonitor from './controllers/status.monitor.js';
3
+ import userTokens from './controllers/user.tokens.js'
3
4
 
4
- async function plugin(fastify, config = {}) {
5
+ async function plugin(app, config = {}) {
5
6
  const prefix = config.prefix || '/api';
6
-
7
- fastify.get(`${prefix}/next-id`, {}, nextId);
8
- fastify.get(`${prefix}/status-monitor`, {}, statusMonitor);
7
+ app.get(`${prefix}/next-id`, {}, nextId);
8
+ app.get(`${prefix}/status-monitor`, {}, statusMonitor);
9
+ app.get(`${prefix}/user-tokens/:token`, { config: { policy: ['user'] } }, userTokens);
9
10
  }
10
11
 
11
12
  export default plugin;
package/utils.js CHANGED
@@ -23,6 +23,8 @@ import getTemplates from './server/plugins/table/funcs/getTemplates.js';
23
23
  import getTemplatePath from './server/plugins/table/funcs/getTemplatePath.js';
24
24
  import addTemplateDir from './server/plugins/table/funcs/addTemplateDir.js';
25
25
  import userTemplateDir from './server/plugins/table/funcs/userTemplateDir.js';
26
+ import customTokens from './server/plugins/table/funcs/customTokens.js';
27
+ import userTokens from './server/plugins/table/funcs/userTokens.js';
26
28
 
27
29
  // table
28
30
  import metaFormat from './server/plugins/table/funcs/metaFormat/index.js';
@@ -103,6 +105,8 @@ export {
103
105
  getTemplatePath,
104
106
  addTemplateDir,
105
107
  userTemplateDir,
108
+ customTokens,
109
+ userTokens,
106
110
 
107
111
  // security
108
112
  checkXSS,