@nakedcreativity/cee-ms-node 0.2.2 → 0.2.4

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 (2) hide show
  1. package/index.js +47 -21
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -21,6 +21,8 @@ function ceems(config) {
21
21
  cache: true
22
22
  });
23
23
 
24
+ this.config = config;
25
+
24
26
  // register liquid engine
25
27
  this.app.engine('liquid', this.engine.express({
26
28
  strict_variables: false,
@@ -65,6 +67,7 @@ function ceems(config) {
65
67
  next();
66
68
  });
67
69
 
70
+
68
71
  if(!config.customHandler){
69
72
  this.app.get(/(.*)/, (req, res)=>{
70
73
  this.render(req,res);
@@ -75,7 +78,7 @@ function ceems(config) {
75
78
 
76
79
  // validate authorisation JWT here
77
80
  this.site = JSON.parse(fs.readFileSync('./pages/site.json', 'utf8'));
78
- console.log(req.body.page)
81
+ //console.log(req.body.page)
79
82
  req.query.edit = true;
80
83
  this.render(req,res, req.body.page);
81
84
 
@@ -83,36 +86,59 @@ function ceems(config) {
83
86
 
84
87
  }
85
88
 
86
- ceems.prototype.render = function(req, res, page=null) {
89
+ ceems.prototype.load = function(path){
87
90
 
88
- if(!page){
89
- this.site = JSON.parse(fs.readFileSync('./pages/site.json', 'utf8'));
90
- this.pages_map = _.indexBy(this.site.pages, 'url');
91
+ this.site = JSON.parse(fs.readFileSync('./pages/site.json', 'utf8'));
92
+ this.pages_map = _.indexBy(this.site.pages, 'url');
91
93
 
92
- if(this.pages_map[req._parsedUrl.pathname]){
93
- var page = this.pages_map[req._parsedUrl.pathname]
94
+ if(this.pages_map[path]){
95
+ var page = this.pages_map[path]
94
96
 
95
- if(page.redirect){
96
- res.redirect(parseInt(page.redirect.type), page.redirect.to)
97
- }
97
+ if(page.redirect){
98
+ res.redirect(parseInt(page.redirect.type), page.redirect.to)
99
+ }
98
100
 
99
- var page = JSON.parse(fs.readFileSync('./pages/'+page.handle+'.json', 'utf8'));
101
+ var page = JSON.parse(fs.readFileSync('./pages/'+page.handle+'.json', 'utf8'));
100
102
 
101
- }else{
102
- res.status(404).send('Sorry, this page does not exist');
103
- }
103
+ return page
104
+
105
+ }else{
106
+ console.log('404 for ',path)
107
+ res.status(404).send('Sorry, this page does not exist');
108
+ }
109
+
110
+
111
+ }
112
+
113
+ ceems.prototype.render = function(req, res, page=null) {
114
+
115
+ if(!page){
116
+ this.load(req._parsedUrl.pathname)
104
117
  }
105
118
 
106
119
  // get any external data, and save this into the page
107
- _.each(page.external, function(ext, key){
120
+ if(page.external){
121
+ _.each(page.external, function(ext, key){
108
122
 
109
- try {
110
- page.external[key].data = JSON.parse(fs.readFileSync('./'+ext.file, 'utf8'));
111
- }catch(error){
112
- console.log(error)
113
- }
123
+ try {
124
+ page.external[key].data = JSON.parse(fs.readFileSync('./'+ext.file, 'utf8'));
125
+ }catch(error){
126
+ console.log(error)
127
+ }
114
128
 
115
- })
129
+ })
130
+ }
131
+
132
+ // if there are whitelisted cookies, add these to the page so they can be rendered
133
+ if(this.config.whitelistedCookies){
134
+ page.cookies = []
135
+ _.each(this.config.whitelistedCookies, cookieName=>{
136
+ if(req.cookies[cookieName]){
137
+ page.cookies[cookieName] = req.cookies[cookieName]
138
+ }
139
+ })
140
+ }
141
+
116
142
 
117
143
  var renderedRegions = {}
118
144
  var jobs = []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakedcreativity/cee-ms-node",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Cee.ms for Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {