@opengis/fastify-table 1.0.65 → 1.0.67

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/Changelog.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.67- 25.07.2024
4
+
5
+ - fix suggest cors
6
+
7
+ ## 1.0.66 - 25.07.2024
8
+
9
+ - add notification compile template from data params support
10
+
3
11
  ## 1.0.65 - 23.07.2024
4
12
 
5
13
  - add rest API token support
@@ -1,104 +1,111 @@
1
- // eslint-disable-next-line max-len, no-control-regex
2
- const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
3
-
4
- // eslint-disable-next-line import/no-relative-packages
5
- // import downloadFile from '../../../fastify-file/file/funcs/downloadFile.js'; // test only
6
- import sendEmail from './utils/sendEmail.js';
7
-
8
- async function generateNotificationContent({
9
- pg, funcs, table, template, id, message,
10
- }) {
11
- if (table && template && id) {
12
- const data = await pg.one(`select * from ${table} where ${pg.pk[table]}=$1`, [id]);
13
- // console.log(data);
14
- const body = await funcs.getTemplate('pt', template);
15
- // console.log(body);
16
- const html = funcs.handlebars.compile(body || 'template not found')(data);
17
- // console.log(html);
18
- return html;
19
- }
20
- return message;
21
- }
22
-
23
- export default async function notification({
24
- pg,
25
- funcs,
26
- log,
27
- provider = ['email'],
28
- from,
29
- to,
30
- template,
31
- table,
32
- message,
33
- title,
34
- file,
35
- id,
36
- nocache,
37
- }) {
38
- const rclient = funcs.getRedis();
39
-
40
- if (pg?.readonly) {
41
- return null;
42
- }
43
-
44
- const keyTo = `${pg.options?.database}:mail:${provider[0]}:${to || ''}${id || ''}${table || ''}${title || ''}`;
45
- const uniqueTo = await rclient.setnx(keyTo, 1);
46
- log.info('notification/sent', { keyTo, send: uniqueTo, nocache });
47
-
48
- if (!uniqueTo && !nocache) {
49
- return `already sent: ${keyTo}`;
50
- }
51
- await rclient.expire(keyTo, 1000 * 600);
52
-
53
- if (!to.length) {
54
- return null;
55
- }
56
-
57
- if (!(Array.isArray(provider) && provider.length)) {
58
- return 'notification provider - must be array and not empty';
59
- }
60
-
61
- try {
62
- const html = await generateNotificationContent({
63
- pg, funcs, table, template, id, message,
64
- });
65
-
66
- // return html;
67
-
68
- if (provider.includes('email')) {
69
- const files = Array.isArray(file) ? file : [file];
70
- const attachments = files?.length ? await Promise.all(files?.filter((el) => el).map(async (el) => {
71
- const content = await funcs.downloadFile(el, { funcs, buffer: true }); // ?
72
- return {
73
- filename: el.split('/').pop(),
74
- encoding: 'base64',
75
- content,
76
- };
77
- })) : [];
78
-
79
- const toEmail = Array.isArray(to) ? to.map((emails) => emails.match(emailReg)?.join(',')) : to;
80
-
81
- const result = await sendEmail({
82
- funcs,
83
- attachments,
84
- html,
85
- subject: title,
86
- from,
87
- to: toEmail,
88
- });
89
-
90
- log.info('notification', {
91
- provider, title, to: toEmail, from, result, len: message?.length, files,
92
- });
93
-
94
- return result;
95
- }
96
- return null;
97
- }
98
- catch (err) {
99
- log.info('notification/error', {
100
- provider, from, to, err: err.toString(),
101
- });
102
- throw err;
103
- }
104
- }
1
+ // eslint-disable-next-line max-len, no-control-regex
2
+ const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
3
+
4
+ // eslint-disable-next-line import/no-relative-packages
5
+ // import downloadFile from '../../../fastify-file/file/funcs/downloadFile.js'; // test only
6
+ import pgClients from '../../pg/pgClients.js';
7
+ import sendEmail from './utils/sendEmail.js';
8
+ import getTemplate from '../../table/controllers/utils/getTemplate.js';
9
+ import getRedis from '../../redis/funcs/getRedis.js';
10
+
11
+ async function generateNotificationContent({
12
+ pg, funcs, table, template, id, message, data: data1,
13
+ }) {
14
+ if (template) {
15
+ const data = table && id ? await pg.one(`select * from ${table} where ${pg.pk[table]}=$1`, [id]) : data1;
16
+ // console.log(data);
17
+ const body = await getTemplate('pt', template);
18
+ // console.log(body);
19
+ const html = funcs.handlebars.compile(body || 'template not found')(data || {});
20
+ // console.log(html);
21
+ return html;
22
+ }
23
+ return message;
24
+ }
25
+
26
+ export default async function notification({
27
+ pg: pg1,
28
+ funcs,
29
+ log,
30
+ provider = ['email'],
31
+ from,
32
+ to,
33
+ template,
34
+ table,
35
+ message,
36
+ title,
37
+ file,
38
+ data,
39
+ id,
40
+ nocache,
41
+ }) {
42
+ const rclient = getRedis();
43
+ const pg = pg1 || pgClients.client;
44
+
45
+ if (!pg) throw new Error('need pg');
46
+
47
+ if (pg?.readonly) {
48
+ return null;
49
+ }
50
+
51
+ const keyTo = `${pg.options?.database}:mail:${provider[0]}:${to || ''}${id || ''}${table || ''}${title || ''}`;
52
+ const uniqueTo = await rclient.setnx(keyTo, 1);
53
+ log.info('notification/sent', { keyTo, send: uniqueTo, nocache });
54
+
55
+ if (!uniqueTo && !nocache) {
56
+ return `already sent: ${keyTo}`;
57
+ }
58
+ await rclient.expire(keyTo, 1000 * 600);
59
+
60
+ if (!to.length) {
61
+ return null;
62
+ }
63
+
64
+ if (!(Array.isArray(provider) && provider.length)) {
65
+ return 'notification provider - must be array and not empty';
66
+ }
67
+
68
+ try {
69
+ const html = await generateNotificationContent({
70
+ pg, funcs, table, template, id, message, data,
71
+ });
72
+
73
+ // return html;
74
+
75
+ if (provider.includes('email')) {
76
+ const files = Array.isArray(file) ? file : [file];
77
+ const attachments = files?.length ? await Promise.all(files?.filter((el) => el).map(async (el) => {
78
+ const content = await funcs.downloadFile(el, { funcs, buffer: true }); // ?
79
+ return {
80
+ filename: el.split('/').pop(),
81
+ encoding: 'base64',
82
+ content,
83
+ };
84
+ })) : [];
85
+
86
+ const toEmail = Array.isArray(to) ? to.map((emails) => emails.match(emailReg)?.join(',')) : to;
87
+
88
+ const result = await sendEmail({
89
+ funcs,
90
+ attachments,
91
+ html,
92
+ subject: title,
93
+ from,
94
+ to: toEmail,
95
+ });
96
+
97
+ log.info('notification', {
98
+ provider, title, to: toEmail, from, result, len: message?.length, files,
99
+ });
100
+
101
+ return result;
102
+ }
103
+ return null;
104
+ }
105
+ catch (err) {
106
+ log.info('notification/error', {
107
+ provider, from, to, err: err.toString(),
108
+ });
109
+ throw err;
110
+ }
111
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.65",
3
+ "version": "1.0.67",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -4,19 +4,20 @@ import getPG from '../../pg/funcs/getPG.js';
4
4
  const limit = 50;
5
5
  const headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Cache-Control': 'no-cache' };
6
6
 
7
- export default async function suggest(req) {
7
+ export default async function suggest(req, res) {
8
8
  const { params, query, pg: pg1 } = req;
9
9
 
10
10
  const lang = query.lang || 'ua';
11
11
  const time = Date.now();
12
12
  const parent = query.parent || '';
13
13
 
14
+ res.headers(headers);
14
15
  const selectName = query.sel || query.name || params.data;
15
- if (!selectName) return { headers, status: 400, message: 'name is required' };
16
+ if (!selectName) return { status: 400, message: 'name is required' };
16
17
 
17
18
  const meta = await getSelectMeta({ name: selectName });
18
19
  const pg = meta.db ? getPG({ db: meta.db }) : pg1;
19
- if (!meta) return { headers, status: 404, message: 'Not found query select ' };
20
+ if (!meta) return { status: 404, message: 'Not found query select ' };
20
21
 
21
22
  const { arr, searchQuery } = meta;
22
23