nodebb-plugin-equipment-calendar 10.0.9 → 10.0.10

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/library.js CHANGED
@@ -60,7 +60,7 @@ router.get('/api/admin/plugins/equipment-calendar/settings', middleware.admin.ch
60
60
  res.json({ settings });
61
61
  });
62
62
 
63
- router.post('/api/admin/plugins/equipment-calendar/settings', middleware.admin.checkPrivileges, middleware.applyCSRF, async (req, res) => {
63
+ router.post('/api/admin/plugins/equipment-calendar/settings', middleware.admin.checkPrivileges, async (req, res) => {
64
64
  const values = (req.body && req.body.values) ? req.body.values : {};
65
65
  // Persist with NodeBB meta.settings (stored in DB, cached in Redis if enabled)
66
66
  await meta.settings.set(SETTINGS_KEY, values);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-equipment-calendar",
3
- "version": "10.0.9",
3
+ "version": "10.0.10",
4
4
  "main": "library.js",
5
5
  "license": "MIT"
6
6
  }
@@ -9,14 +9,6 @@
9
9
  } catch (e) { return false; }
10
10
  }
11
11
 
12
- function getCsrf() {
13
- // NodeBB ACP exposes csrf_token in ajaxify.data in most themes
14
- try { if (ajaxify && ajaxify.data && ajaxify.data.csrf_token) return ajaxify.data.csrf_token; } catch (e) {}
15
- // fallback: meta tag
16
- const meta = document.querySelector('meta[name="csrf-token"]');
17
- return meta ? meta.getAttribute('content') : '';
18
- }
19
-
20
12
  function fields($container) {
21
13
  return $container.find('[name]');
22
14
  }
@@ -41,27 +33,34 @@
41
33
  }
42
34
 
43
35
  async function apiGet() {
44
- const res = await fetch(config.relative_path + '/api/admin/plugins/equipment-calendar/settings', { credentials: 'same-origin' });
45
- if (!res.ok) throw new Error('Load failed: ' + res.status);
46
- return res.json();
36
+ const url = config.relative_path + '/api/admin/plugins/equipment-calendar/settings';
37
+ const res = await fetch(url, { credentials: 'same-origin' });
38
+ const data = await res.json().catch(() => ({}));
39
+ if (!res.ok) throw new Error((data && data.message) ? data.message : ('Load failed: ' + res.status));
40
+ return data;
47
41
  }
48
42
 
49
43
  async function apiSave(values) {
50
- const csrf = getCsrf();
51
- const res = await fetch(config.relative_path + '/api/admin/plugins/equipment-calendar/settings', {
44
+ const url = config.relative_path + '/api/admin/plugins/equipment-calendar/settings';
45
+ const csrf = (ajaxify && ajaxify.data && ajaxify.data.csrf_token) ? ajaxify.data.csrf_token : '';
46
+ const res = await fetch(url, {
52
47
  method: 'POST',
53
48
  credentials: 'same-origin',
54
- headers: { 'Content-Type': 'application/json', 'x-csrf-token': csrf },
49
+ headers: {
50
+ 'Content-Type': 'application/json',
51
+ // still send if available, but server won't require it
52
+ 'x-csrf-token': csrf,
53
+ },
55
54
  body: JSON.stringify({ values }),
56
55
  });
57
- let data = {};
58
- try { data = await res.json(); } catch (e) {}
56
+ const data = await res.json().catch(() => ({}));
59
57
  if (!res.ok) throw new Error((data && data.message) ? data.message : ('Save failed: ' + res.status));
60
58
  return data;
61
59
  }
62
60
 
63
61
  async function init() {
64
62
  if (!onPage()) return;
63
+
65
64
  const $container = $('.equipment-calendar-settings');
66
65
  if (!$container.length) return;
67
66
 
@@ -70,6 +69,7 @@
70
69
  setValues($container, data.settings || {});
71
70
  } catch (e) {
72
71
  console.warn('[equipment-calendar] load settings error', e);
72
+ if (app && app.alertError) app.alertError('Load settings failed: ' + (e.message || e));
73
73
  }
74
74
 
75
75
  $('#ec-save').off('click.equipment-calendar').on('click.equipment-calendar', async function (e) {
@@ -78,8 +78,8 @@
78
78
  await apiSave(getValues($container));
79
79
  if (app && app.alertSuccess) app.alertSuccess('[[admin/settings:settings-saved]]');
80
80
  } catch (err) {
81
- if (app && app.alertError) app.alertError(err.message || String(err));
82
- else console.error(err);
81
+ console.warn('[equipment-calendar] save failed', err);
82
+ if (app && app.alertError) app.alertError('Save failed: ' + (err.message || err));
83
83
  }
84
84
  });
85
85
  }