@opengis/fastify-table 1.0.89 → 1.0.90

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.
Files changed (49) hide show
  1. package/Changelog.md +4 -0
  2. package/crud/controllers/deleteCrud.js +19 -19
  3. package/crud/controllers/insert.js +54 -54
  4. package/crud/controllers/update.js +59 -59
  5. package/crud/funcs/dataInsert.js +24 -24
  6. package/crud/funcs/dataUpdate.js +24 -24
  7. package/crud/funcs/getAccess.js +53 -53
  8. package/crud/funcs/getOpt.js +10 -10
  9. package/crud/funcs/setOpt.js +16 -16
  10. package/helper.js +28 -28
  11. package/index.js +97 -97
  12. package/notification/controllers/userNotifications.js +19 -19
  13. package/notification/funcs/addNotification.js +8 -8
  14. package/package.json +1 -1
  15. package/pg/pgClients.js +20 -20
  16. package/policy/funcs/checkPolicy.js +83 -83
  17. package/policy/funcs/sqlInjection.js +33 -33
  18. package/policy/index.js +14 -14
  19. package/redis/client.js +8 -8
  20. package/redis/funcs/redisClients.js +2 -2
  21. package/redis/index.js +19 -19
  22. package/server/migrations/0.sql +78 -78
  23. package/server/templates/form/test.dataset.form.json +411 -411
  24. package/server/templates/table/test.dataset.table.json +28 -28
  25. package/server/templates/table/test.gis.map.table.json +44 -44
  26. package/table/controllers/data.js +103 -103
  27. package/table/controllers/suggest.js +79 -79
  28. package/table/controllers/table.js +49 -49
  29. package/table/controllers/utils/gisIRColumn.js +68 -68
  30. package/table/funcs/getFilterSQL/index.js +75 -75
  31. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  32. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  33. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  34. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  35. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  36. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  37. package/table/funcs/metaFormat/index.js +28 -28
  38. package/test/api/crud.test.js +88 -88
  39. package/test/api/table.test.js +89 -89
  40. package/util/controllers/logger.file.js +90 -0
  41. package/util/controllers/status.monitor.js +8 -8
  42. package/util/controllers/utils/checkUserAccess.js +19 -0
  43. package/util/controllers/utils/getRootDir.js +21 -0
  44. package/util/index.js +23 -21
  45. package/widget/controllers/utils/historyFormat.js +76 -76
  46. package/widget/controllers/utils/obj2db.js +13 -13
  47. package/widget/controllers/widget.del.js +44 -44
  48. package/widget/controllers/widget.get.js +96 -96
  49. package/widget/controllers/widget.set.js +70 -70
package/index.js CHANGED
@@ -1,97 +1,97 @@
1
- import path from 'path';
2
- import { existsSync, readdirSync, readFileSync } from 'fs';
3
-
4
- import fp from 'fastify-plugin';
5
- import config from './config.js';
6
- // import rclient from './redis/client.js';
7
-
8
- import redisPlugin from './redis/index.js';
9
- import pgPlugin from './pg/index.js';
10
- import tablePlugin from './table/index.js';
11
- import notificationPlugin from './notification/index.js';
12
- import widgetPlugin from './widget/index.js';
13
- import crudPlugin from './crud/index.js';
14
- import policyPlugin from './policy/index.js';
15
- import utilPlugin from './util/index.js';
16
- import cronPlugin from './cron/index.js';
17
-
18
- import pgClients from './pg/pgClients.js';
19
-
20
- import execMigrations from './migration/exec.migrations.js';
21
-
22
- async function plugin(fastify, opt) {
23
- // console.log(opt);
24
- config.pg = opt.pg;
25
- config.redis = opt.redis;
26
- config.root = opt.root;
27
- config.mapServerRoot = opt.mapServerRoot;
28
-
29
- // independent npm start / unit test
30
- if (!fastify.config) {
31
- fastify.decorate('config', config);
32
- }
33
-
34
- fastify.register(import('@fastify/sensible'), {
35
- errorHandler: false,
36
- });
37
-
38
- fastify.register(import('@fastify/url-data'), {
39
- errorHandler: false,
40
- });
41
-
42
- fastify.register(import('@opengis/fastify-hb'));
43
- fastify.decorate('getFolder', (req, type = 'server') => {
44
- if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
45
- const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
46
- const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
47
- return filepath;
48
- });
49
-
50
- fastify.addHook('onListen', async () => {
51
- const { client } = pgClients;
52
- if (client?.pk?.['crm.cls']) {
53
- const clsDir = path.join(process.cwd(), 'server/templates/cls');
54
- const files = existsSync(clsDir) ? readdirSync(clsDir) : [];
55
- if (files.length) {
56
- const res = await Promise.all(files.map(async (filename) => {
57
- const filepath = path.join(clsDir, filename);
58
- const data = JSON.parse(readFileSync(filepath));
59
- return { name: path.parse(filename).name, data };
60
- }));
61
- await client.query('truncate table crm.cls');
62
- const { rows } = await client.query(`insert into crm.cls(name, type)
63
- select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
64
- rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
65
- const sql = `insert into crm.cls(code, name, parent)
66
- select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
67
- await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
68
- }
69
- }
70
- // call from another repo / project
71
- fastify.execMigrations = execMigrations;
72
- // execute core migrations
73
- await fastify.execMigrations();
74
- });
75
- if (!fastify.funcs) {
76
- fastify.addHook('onRequest', async (req) => {
77
- req.funcs = fastify;
78
- if (!req.user && req.session?.passport?.user) {
79
- const { user } = req.session?.passport || {};
80
- req.user = user;
81
- }
82
- });
83
- // fastify.decorateRequest('funcs', fastify);
84
- }
85
-
86
- policyPlugin(fastify);
87
- redisPlugin(fastify);
88
- await pgPlugin(fastify, opt);
89
- tablePlugin(fastify, opt);
90
- crudPlugin(fastify, opt);
91
- notificationPlugin(fastify, opt);
92
- widgetPlugin(fastify, opt);
93
- utilPlugin(fastify, opt);
94
- cronPlugin(fastify, opt);
95
- }
96
- export default fp(plugin);
97
- // export { rclient };
1
+ import path from 'path';
2
+ import { existsSync, readdirSync, readFileSync } from 'fs';
3
+
4
+ import fp from 'fastify-plugin';
5
+ import config from './config.js';
6
+ // import rclient from './redis/client.js';
7
+
8
+ import redisPlugin from './redis/index.js';
9
+ import pgPlugin from './pg/index.js';
10
+ import tablePlugin from './table/index.js';
11
+ import notificationPlugin from './notification/index.js';
12
+ import widgetPlugin from './widget/index.js';
13
+ import crudPlugin from './crud/index.js';
14
+ import policyPlugin from './policy/index.js';
15
+ import utilPlugin from './util/index.js';
16
+ import cronPlugin from './cron/index.js';
17
+
18
+ import pgClients from './pg/pgClients.js';
19
+
20
+ import execMigrations from './migration/exec.migrations.js';
21
+
22
+ async function plugin(fastify, opt) {
23
+ // console.log(opt);
24
+ config.pg = opt.pg;
25
+ config.redis = opt.redis;
26
+ config.root = opt.root;
27
+ config.mapServerRoot = opt.mapServerRoot;
28
+
29
+ // independent npm start / unit test
30
+ if (!fastify.config) {
31
+ fastify.decorate('config', config);
32
+ }
33
+
34
+ fastify.register(import('@fastify/sensible'), {
35
+ errorHandler: false,
36
+ });
37
+
38
+ fastify.register(import('@fastify/url-data'), {
39
+ errorHandler: false,
40
+ });
41
+
42
+ fastify.register(import('@opengis/fastify-hb'));
43
+ fastify.decorate('getFolder', (req, type = 'server') => {
44
+ if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
45
+ const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
46
+ const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
47
+ return filepath;
48
+ });
49
+
50
+ fastify.addHook('onListen', async () => {
51
+ const { client } = pgClients;
52
+ if (client?.pk?.['crm.cls']) {
53
+ const clsDir = path.join(process.cwd(), 'server/templates/cls');
54
+ const files = existsSync(clsDir) ? readdirSync(clsDir) : [];
55
+ if (files.length) {
56
+ const res = await Promise.all(files.map(async (filename) => {
57
+ const filepath = path.join(clsDir, filename);
58
+ const data = JSON.parse(readFileSync(filepath));
59
+ return { name: path.parse(filename).name, data };
60
+ }));
61
+ await client.query('truncate table crm.cls');
62
+ const { rows } = await client.query(`insert into crm.cls(name, type)
63
+ select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
64
+ rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
65
+ const sql = `insert into crm.cls(code, name, parent)
66
+ select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
67
+ await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
68
+ }
69
+ }
70
+ // call from another repo / project
71
+ fastify.execMigrations = execMigrations;
72
+ // execute core migrations
73
+ await fastify.execMigrations();
74
+ });
75
+ if (!fastify.funcs) {
76
+ fastify.addHook('onRequest', async (req) => {
77
+ req.funcs = fastify;
78
+ if (!req.user && req.session?.passport?.user) {
79
+ const { user } = req.session?.passport || {};
80
+ req.user = user;
81
+ }
82
+ });
83
+ // fastify.decorateRequest('funcs', fastify);
84
+ }
85
+
86
+ policyPlugin(fastify);
87
+ redisPlugin(fastify);
88
+ await pgPlugin(fastify, opt);
89
+ tablePlugin(fastify, opt);
90
+ crudPlugin(fastify, opt);
91
+ notificationPlugin(fastify, opt);
92
+ widgetPlugin(fastify, opt);
93
+ utilPlugin(fastify, opt);
94
+ cronPlugin(fastify, opt);
95
+ }
96
+ export default fp(plugin);
97
+ // export { rclient };
@@ -1,19 +1,19 @@
1
- export default async function userNotifications({
2
- pg, session = {}, query = {},
3
- }) {
4
- const time = Date.now();
5
- try {
6
- const { uid } = session.passport?.user || {};
7
- if (!uid) return { error: 'access restricted', status: 403 };
8
-
9
- const queryFunc = query.nocache ? pg.query : pg.queryCache;
10
-
11
- // queryCache not supports $1 params
12
- const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
13
- notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
14
- return { time: Date.now() - time, total: rows?.length, rows };
15
- }
16
- catch (err) {
17
- return { error: err.toString(), status: 500 };
18
- }
19
- }
1
+ export default async function userNotifications({
2
+ pg, session = {}, query = {},
3
+ }) {
4
+ const time = Date.now();
5
+ try {
6
+ const { uid } = session.passport?.user || {};
7
+ if (!uid) return { error: 'access restricted', status: 403 };
8
+
9
+ const queryFunc = query.nocache ? pg.query : pg.queryCache;
10
+
11
+ // queryCache not supports $1 params
12
+ const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
13
+ notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
14
+ return { time: Date.now() - time, total: rows?.length, rows };
15
+ }
16
+ catch (err) {
17
+ return { error: err.toString(), status: 500 };
18
+ }
19
+ }
@@ -1,8 +1,8 @@
1
- export default async function addNotification({
2
- pg, session = {}, title, body, link, notificationType, uid: uid1,
3
- }) {
4
- const uid = uid1 || session.passport?.user?.uid || {};
5
- const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
6
- values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
7
- return { id, status };
8
- }
1
+ export default async function addNotification({
2
+ pg, session = {}, title, body, link, notificationType, uid: uid1,
3
+ }) {
4
+ const uid = uid1 || session.passport?.user?.uid || {};
5
+ const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
6
+ values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
7
+ return { id, status };
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.89",
3
+ "version": "1.0.90",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
package/pg/pgClients.js CHANGED
@@ -1,20 +1,20 @@
1
- import pg from 'pg';
2
- import config from '../config.js';
3
- import init from './funcs/init.js';
4
-
5
- const pgClients = {};
6
- if (config.pg) {
7
- const client = new pg.Pool({
8
- host: config.pg?.host || '127.0.0.1',
9
- port: config.pg?.port || 5432,
10
- database: config.pg?.database || 'postgres',
11
- user: config.pg?.user || 'postgres',
12
- password: config.pg?.password || 'postgres',
13
- });
14
- client.init = async () => {
15
- await init(client);
16
- };
17
- client.init();
18
- pgClients.client = client;
19
- }
20
- export default pgClients;
1
+ import pg from 'pg';
2
+ import config from '../config.js';
3
+ import init from './funcs/init.js';
4
+
5
+ const pgClients = {};
6
+ if (config.pg) {
7
+ const client = new pg.Pool({
8
+ host: config.pg?.host || '127.0.0.1',
9
+ port: config.pg?.port || 5432,
10
+ database: config.pg?.database || 'postgres',
11
+ user: config.pg?.user || 'postgres',
12
+ password: config.pg?.password || 'postgres',
13
+ });
14
+ client.init = async () => {
15
+ await init(client);
16
+ };
17
+ client.init();
18
+ pgClients.client = client;
19
+ }
20
+ export default pgClients;
@@ -1,83 +1,83 @@
1
- import block from './sqlInjection.js';
2
-
3
- /**
4
- * Middleware func
5
- *
6
- * @type function
7
- * @alias checkPolicy
8
- * @summary Функція дозволяє налаштувати доступ до сайту або API для адмін. та публічної частини веб-ресурсу
9
- * @param {String} path - назва апі
10
- * @returns {object|null} Returns object
11
- */
12
-
13
- export default function checkPolicy(req) {
14
- const {
15
- originalUrl: path, hostname, query, params, headers: hs, log, sid = 35, funcs = {},
16
- } = req;
17
- const user = req.user || req.session?.passport?.user;
18
-
19
- const { config } = funcs;
20
- const isUser = config.debug || !!user;
21
-
22
- const isServer = process.argv[2];
23
- const { policy = [] } = req.routeOptions?.config || {};
24
-
25
- /*= == 0.Check superadmin access === */
26
- if (policy.includes('superadmin') && user?.user_type !== 'superadmin') {
27
- log.warn({
28
- name: 'api/superadmin', params, query, body: JSON.stringify(req?.body || {}).substring(30), message: 'access restricted: 0',
29
- });
30
- return { message: 'access restricted: 0', status: 403 };
31
- }
32
-
33
- /*= == 1.File injection === */
34
- if (JSON.stringify(params || {})?.includes('../') || JSON.stringify(query || {})?.includes('../') || path?.includes('../')) {
35
- log.warn({
36
- name: 'injection/file', params, query, message: 'access restricted: 1',
37
- });
38
- return { message: 'access restricted: 1', status: 403 };
39
- }
40
-
41
- /*= == 1.1 File === */
42
- const allowExtPublic = ['.png', '.jpg', '.svg'];
43
- const ext = path.toLowerCase().substr(-4);
44
- if (path.includes('files/') && allowExtPublic.includes(ext)) return null;
45
-
46
- /*= == 2.SQL Injection policy: no-sql === */
47
- if (!policy.includes('no-sql')) {
48
- // skip polyline param - data filter (geometry bounds)
49
- const stopWords = block.filter((el) => path.replace(query.polyline, '').includes(el));
50
- if (stopWords?.length) {
51
- log.warn({ name: 'injection/sql', stopWords, message: 'access restricted: 2' });
52
- return { message: 'access restricted: 2', status: 403 };
53
- }
54
- }
55
- /* Check is Not API */
56
- const isApi = ['/files/', '/api/format/', '/api-user/', '/logger', '/file/'].filter((el) => path.includes(el)).length;
57
- if (!isApi) return null;
58
-
59
- /*= == 3. policy: referer === */
60
- if (!hs?.referer?.includes?.(hostname) && policy.includes('referer') && !config.local && !config.debug) {
61
- log.warn({ name: 'referer', message: 'access restricted: 3' });
62
- return { message: 'access restricted: 3', status: 403 };
63
- }
64
-
65
- /*= == policy: public === */
66
- if (policy.includes('public')) {
67
- return null;
68
- }
69
-
70
- /*= == 4. policy: site auth === */
71
- if (!policy.includes('site') && sid === 1 && isUser && !config.local && !config.debug) {
72
- log.warn({ name: 'site', message: 'access restricted: 4' });
73
- return { message: 'access restricted: 4', status: 403 };
74
- }
75
-
76
- /*= == 5. base policy: block api === */
77
- if (sid === 35 && !isUser && isServer && !config.local && !config.debug) {
78
- log.warn({ name: 'api', message: 'access restricted: 5' });
79
- return { message: 'access restricted: 5', status: 403 };
80
- }
81
-
82
- return null;
83
- }
1
+ import block from './sqlInjection.js';
2
+
3
+ /**
4
+ * Middleware func
5
+ *
6
+ * @type function
7
+ * @alias checkPolicy
8
+ * @summary Функція дозволяє налаштувати доступ до сайту або API для адмін. та публічної частини веб-ресурсу
9
+ * @param {String} path - назва апі
10
+ * @returns {object|null} Returns object
11
+ */
12
+
13
+ export default function checkPolicy(req) {
14
+ const {
15
+ originalUrl: path, hostname, query, params, headers: hs, log, sid = 35, funcs = {},
16
+ } = req;
17
+ const user = req.user || req.session?.passport?.user;
18
+
19
+ const { config } = funcs;
20
+ const isUser = config.debug || !!user;
21
+
22
+ const isServer = process.argv[2];
23
+ const { policy = [] } = req.routeOptions?.config || {};
24
+
25
+ /*= == 0.Check superadmin access === */
26
+ if (policy.includes('superadmin') && user?.user_type !== 'superadmin') {
27
+ log.warn({
28
+ name: 'api/superadmin', params, query, body: JSON.stringify(req?.body || {}).substring(30), message: 'access restricted: 0',
29
+ });
30
+ return { message: 'access restricted: 0', status: 403 };
31
+ }
32
+
33
+ /*= == 1.File injection === */
34
+ if (JSON.stringify(params || {})?.includes('../') || JSON.stringify(query || {})?.includes('../') || path?.includes('../')) {
35
+ log.warn({
36
+ name: 'injection/file', params, query, message: 'access restricted: 1',
37
+ });
38
+ return { message: 'access restricted: 1', status: 403 };
39
+ }
40
+
41
+ /*= == 1.1 File === */
42
+ const allowExtPublic = ['.png', '.jpg', '.svg'];
43
+ const ext = path.toLowerCase().substr(-4);
44
+ if (path.includes('files/') && allowExtPublic.includes(ext)) return null;
45
+
46
+ /*= == 2.SQL Injection policy: no-sql === */
47
+ if (!policy.includes('no-sql')) {
48
+ // skip polyline param - data filter (geometry bounds)
49
+ const stopWords = block.filter((el) => path.replace(query.polyline, '').includes(el));
50
+ if (stopWords?.length) {
51
+ log.warn({ name: 'injection/sql', stopWords, message: 'access restricted: 2' });
52
+ return { message: 'access restricted: 2', status: 403 };
53
+ }
54
+ }
55
+ /* Check is Not API */
56
+ const isApi = ['/files/', '/api/format/', '/api-user/', '/logger', '/file/'].filter((el) => path.includes(el)).length;
57
+ if (!isApi) return null;
58
+
59
+ /*= == 3. policy: referer === */
60
+ if (!hs?.referer?.includes?.(hostname) && policy.includes('referer') && !config.local && !config.debug) {
61
+ log.warn({ name: 'referer', message: 'access restricted: 3' });
62
+ return { message: 'access restricted: 3', status: 403 };
63
+ }
64
+
65
+ /*= == policy: public === */
66
+ if (policy.includes('public')) {
67
+ return null;
68
+ }
69
+
70
+ /*= == 4. policy: site auth === */
71
+ if (!policy.includes('site') && sid === 1 && isUser && !config.local && !config.debug) {
72
+ log.warn({ name: 'site', message: 'access restricted: 4' });
73
+ return { message: 'access restricted: 4', status: 403 };
74
+ }
75
+
76
+ /*= == 5. base policy: block api === */
77
+ if (sid === 35 && !isUser && isServer && !config.local && !config.debug) {
78
+ log.warn({ name: 'api', message: 'access restricted: 5' });
79
+ return { message: 'access restricted: 5', status: 403 };
80
+ }
81
+
82
+ return null;
83
+ }
@@ -1,33 +1,33 @@
1
- const sqlInjection = [
2
- '()',
3
- '^',
4
- '*',
5
- 'like ',
6
- '@variable',
7
- '@@variable',
8
- 'group by ',
9
- 'union ',
10
- 'select ',
11
- 'having ',
12
- 'as injectx',
13
- 'where ',
14
- 'rlike ',
15
- 'if(',
16
- 'sleep(',
17
- 'waitfor delay',
18
- 'benchmark(',
19
- 'pg_sleep(',
20
- "'\\\"",
21
- 'randomblob(',
22
- 'order by ',
23
- 'union all ',
24
- '+or',
25
- 'or ',
26
- 'and ',
27
- "'' ",
28
- '""" ',
29
- '<script',
30
- 'javascript:',
31
- ]
32
-
33
- export default sqlInjection;
1
+ const sqlInjection = [
2
+ '()',
3
+ '^',
4
+ '*',
5
+ 'like ',
6
+ '@variable',
7
+ '@@variable',
8
+ 'group by ',
9
+ 'union ',
10
+ 'select ',
11
+ 'having ',
12
+ 'as injectx',
13
+ 'where ',
14
+ 'rlike ',
15
+ 'if(',
16
+ 'sleep(',
17
+ 'waitfor delay',
18
+ 'benchmark(',
19
+ 'pg_sleep(',
20
+ "'\\\"",
21
+ 'randomblob(',
22
+ 'order by ',
23
+ 'union all ',
24
+ '+or',
25
+ 'or ',
26
+ 'and ',
27
+ "'' ",
28
+ '""" ',
29
+ '<script',
30
+ 'javascript:',
31
+ ]
32
+
33
+ export default sqlInjection;
package/policy/index.js CHANGED
@@ -1,14 +1,14 @@
1
- // import fp from 'fastify-plugin';
2
-
3
- import checkPolicy from './funcs/checkPolicy.js';
4
-
5
- async function plugin(fastify) {
6
- fastify.addHook('onRequest', async (request, reply) => {
7
- const hookData = checkPolicy(request);
8
- if (hookData?.status && hookData?.message) {
9
- return reply.status(hookData?.status).send(hookData.message);
10
- }
11
- });
12
- }
13
-
14
- export default plugin;
1
+ // import fp from 'fastify-plugin';
2
+
3
+ import checkPolicy from './funcs/checkPolicy.js';
4
+
5
+ async function plugin(fastify) {
6
+ fastify.addHook('onRequest', async (request, reply) => {
7
+ const hookData = checkPolicy(request);
8
+ if (hookData?.status && hookData?.message) {
9
+ return reply.status(hookData?.status).send(hookData.message);
10
+ }
11
+ });
12
+ }
13
+
14
+ export default plugin;
package/redis/client.js CHANGED
@@ -1,8 +1,8 @@
1
- import redisClients from './funcs/redisClients.js';
2
- import getRedis from './funcs/getRedis.js';
3
-
4
- if (!redisClients[0]) {
5
- getRedis({ db: 0 });
6
- }
7
-
8
- export default redisClients[0];
1
+ import redisClients from './funcs/redisClients.js';
2
+ import getRedis from './funcs/getRedis.js';
3
+
4
+ if (!redisClients[0]) {
5
+ getRedis({ db: 0 });
6
+ }
7
+
8
+ export default redisClients[0];
@@ -1,2 +1,2 @@
1
- const redisClients = {};
2
- export default redisClients;
1
+ const redisClients = {};
2
+ export default redisClients;
package/redis/index.js CHANGED
@@ -1,19 +1,19 @@
1
- // import client from './client.js';
2
- import getRedis from './funcs/getRedis.js';
3
- // import client from './funcs/redisClients.js';
4
-
5
- function close(fastify) {
6
- fastify.rclient.quit();
7
- // fastify.rclient2.quit();
8
- }
9
-
10
- async function plugin(fastify) {
11
- const client = getRedis({ db: 0, funcs: fastify });
12
- client.getJSON = client.get;
13
- fastify.decorate('rclient', client);
14
- fastify.decorate('getRedis', getRedis);
15
- // fastify.decorate('rclient2', client2);
16
- fastify.addHook('onClose', close);
17
- }
18
-
19
- export default plugin;
1
+ // import client from './client.js';
2
+ import getRedis from './funcs/getRedis.js';
3
+ // import client from './funcs/redisClients.js';
4
+
5
+ function close(fastify) {
6
+ fastify.rclient.quit();
7
+ // fastify.rclient2.quit();
8
+ }
9
+
10
+ async function plugin(fastify) {
11
+ const client = getRedis({ db: 0, funcs: fastify });
12
+ client.getJSON = client.get;
13
+ fastify.decorate('rclient', client);
14
+ fastify.decorate('getRedis', getRedis);
15
+ // fastify.decorate('rclient2', client2);
16
+ fastify.addHook('onClose', close);
17
+ }
18
+
19
+ export default plugin;