Skip to content

Commit 4901ecc

Browse files
committed
fix: don’t modify original schema endpoint
otherwise placeholders no longer exist
1 parent d522eca commit 4901ecc

File tree

10 files changed

+235
-83
lines changed

10 files changed

+235
-83
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<img src="http://52.209.207.148/assets/products/dadi-web-full.png" alt="DADI Web" height="65"/>
22

33
[![npm (scoped)](https://img.shields.io/npm/v/@dadi/web.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/web)
4-
[![coverage](https://img.shields.io/badge/coverage-67%25-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/web)
4+
[![coverage](https://img.shields.io/badge/coverage-68%25-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/web)
55
[![Build Status](https://travis-ci.org/dadi/web.svg?branch=master)](https://travis-ci.org/dadi/web)
66
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
77
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)

dadi/lib/datasource/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Datasource.prototype.processRequest = function (datasource, req) {
142142

143143
// Regular expression search for {param.nameOfParam} and replace with requestParameters
144144
var paramRule = /("\{)(\bparams.\b)(.*?)(\}")/gmi
145+
145146
this.schema.datasource.filter = JSON.parse(JSON.stringify(this.schema.datasource.filter).replace(paramRule, function (match, p1, p2, p3, p4, offset, string) {
146147
if (req.params[p3]) {
147148
return req.params[p3]
@@ -168,7 +169,7 @@ Datasource.prototype.processRequest = function (datasource, req) {
168169
this.schema.datasource.filter[obj.field] = paramValue
169170
} else if (obj.target === 'endpoint') {
170171
var placeholderRegex = new RegExp('{' + obj.field + '}', 'ig')
171-
this.schema.datasource.source.endpoint = this.schema.datasource.source.endpoint.replace(placeholderRegex, paramValue)
172+
this.source.modifiedEndpoint = this.schema.datasource.source.endpoint.replace(placeholderRegex, paramValue)
172173
}
173174
} else {
174175
if (obj.target === 'filter') {

dadi/lib/index.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ function onConnection (socket) {
718718

719719
function onListening (e) {
720720
// check that our API connection is valid
721-
help.isApiAvailable(function (err, result) {
721+
help.isApiAvailable((err, result) => {
722722
if (err) {
723723
console.log(err)
724724
console.log()
@@ -774,47 +774,3 @@ function onError (err) {
774774
process.exit(0)
775775
}
776776
}
777-
778-
// function processSortParameter (obj) {
779-
// var sort = {}
780-
// if (typeof obj !== 'object' || obj === null) return sort
781-
//
782-
// _.each(obj, function (value, key) {
783-
// if (typeof value === 'object' && value.hasOwnProperty('field') && value.hasOwnProperty('order')) {
784-
// sort[value.field] = (value.order === 'asc') ? 1 : -1
785-
// }
786-
// })
787-
//
788-
// return sort
789-
// }
790-
791-
// function parseRoutes (endpoints, req_path) {
792-
// var params = {}
793-
// var route_path = ''
794-
// _.each(endpoints, function (endpoint) {
795-
// var paths = endpoint.page.route.paths
796-
// var req_path_items = req_path.split('/')
797-
// _.each(paths, function (path) {
798-
// path_items = path.split('/')
799-
// if (path_items.length == req_path_items.length) {
800-
// var alias = _.filter(path_items, function (item) {
801-
// return item == '' || item.slice(0, 1) != ':'
802-
// })
803-
//
804-
// if (_.difference(alias, _.intersection(path_items, req_path_items)).length == 0) {
805-
// _.each(path_items, function (item, index) {
806-
// if (item != '' && item.slice(0, 1) == ':') {
807-
// params[item.slice(1)] = req_path_items[index]
808-
// }
809-
// })
810-
// route_path = path
811-
// }
812-
// }
813-
// })
814-
// })
815-
//
816-
// return {
817-
// route_path: route_path,
818-
// params: params
819-
// }
820-
// }

dadi/lib/providers/remote.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ RemoteProvider.prototype.buildEndpoint = function buildEndpoint () {
4444
const port = source.port || apiConfig.port
4545

4646
const uri = [protocol, '://', host, (port !== '' ? ':' : ''),
47-
port, '/', source.endpoint].join('')
47+
port, '/', this.datasource.source.modifiedEndpoint || source.endpoint].join('')
4848

4949
this.endpoint = this.processDatasourceParameters(this.schema, uri)
5050
}
@@ -111,7 +111,6 @@ RemoteProvider.prototype.getHeaders = function getHeaders (done) {
111111
* @return {void}
112112
*/
113113
RemoteProvider.prototype.handleResponse = function handleResponse (res, done) {
114-
const self = this
115114
const encoding = res.headers['content-encoding'] ? res.headers['content-encoding'] : ''
116115
let output = ''
117116

@@ -123,7 +122,7 @@ RemoteProvider.prototype.handleResponse = function handleResponse (res, done) {
123122
buffer.push(data.toString())
124123
}).on('end', () => {
125124
output = buffer.join('')
126-
self.processOutput(res, output, (err, data, res) => {
125+
this.processOutput(res, output, (err, data, res) => {
127126
if (err) return done(err)
128127
return done(null, data, res)
129128
})
@@ -138,7 +137,7 @@ RemoteProvider.prototype.handleResponse = function handleResponse (res, done) {
138137
})
139138

140139
res.on('end', () => {
141-
self.processOutput(res, output, (err, data, res) => {
140+
this.processOutput(res, output, (err, data, res) => {
142141
if (err) return done(err)
143142
return done(null, data, res)
144143
})
@@ -166,8 +165,6 @@ RemoteProvider.prototype.keepAliveAgent = function keepAliveAgent (protocol) {
166165
* @return {void}
167166
*/
168167
RemoteProvider.prototype.load = function (requestUrl, done) {
169-
const self = this
170-
171168
this.requestUrl = requestUrl
172169
this.dataCache = DatasourceCache()
173170

@@ -187,24 +184,24 @@ RemoteProvider.prototype.load = function (requestUrl, done) {
187184

188185
debug('load %s', this.endpoint)
189186

190-
self.getHeaders((err, headers) => {
187+
this.getHeaders((err, headers) => {
191188
err && done(err)
192189

193-
self.options = _.extend(self.options, headers)
190+
this.options = _.extend(this.options, headers)
194191

195-
log.info({module: 'helper'}, "GET datasource '" + self.datasource.schema.datasource.key + "': " + self.options.path)
192+
log.info({module: 'helper'}, "GET datasource '" + this.datasource.schema.datasource.key + "': " + this.options.path)
196193

197-
const agent = (self.options.protocol === 'https') ? https : http
198-
let request = agent.request(self.options, (res) => {
199-
self.handleResponse(res, done)
194+
const agent = (this.options.protocol === 'https') ? https : http
195+
let request = agent.request(this.options, (res) => {
196+
this.handleResponse(res, done)
200197
})
201198

202199
request.on('error', (err) => {
203-
const message = err.toString() + ". Couldn't request data from " + self.datasource.endpoint
200+
const message = err.toString() + ". Couldn't request data from " + this.datasource.endpoint
204201
err.name = 'GetData'
205202
err.message = message
206-
err.remoteIp = self.options.host
207-
err.remotePort = self.options.port
203+
err.remoteIp = this.options.host
204+
err.remotePort = this.options.port
208205
return done(err)
209206
})
210207

@@ -221,6 +218,7 @@ RemoteProvider.prototype.load = function (requestUrl, done) {
221218
* @returns {string} uri with query string appended
222219
*/
223220
RemoteProvider.prototype.processDatasourceParameters = function processDatasourceParameters (schema, uri) {
221+
debug('processDatasourceParameters %s', uri)
224222
let query = '?'
225223

226224
const params = [
@@ -258,8 +256,6 @@ RemoteProvider.prototype.processDatasourceParameters = function processDatasourc
258256
* @return {void}
259257
*/
260258
RemoteProvider.prototype.processOutput = function processOutput (res, data, done) {
261-
const self = this
262-
263259
// Return a 202 Accepted response immediately,
264260
// along with the datasource response
265261
if (res.statusCode === 202) {
@@ -291,8 +287,8 @@ RemoteProvider.prototype.processOutput = function processOutput (res, data, done
291287
err.message = 'Datasource "' + this.datasource.name + '" failed. ' + res.statusMessage + ' (' + res.statusCode + ')' + ': ' + this.endpoint
292288
if (data) err.message += '\n' + data
293289

294-
err.remoteIp = self.options.host
295-
err.remotePort = self.options.port
290+
err.remoteIp = this.options.host
291+
err.remotePort = this.options.port
296292

297293
log.error({module: 'helper'}, res.statusMessage + ' (' + res.statusCode + ')' + ': ' + this.endpoint)
298294

dadi/lib/providers/rss.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ RSSProvider.prototype.load = function load (requestUrl, done) {
7676

7777
const items = []
7878
const feedparser = new FeedParser()
79+
7980
const req = request(this.endpoint, {
8081
pool: false,
8182
headers: {
@@ -86,6 +87,7 @@ RSSProvider.prototype.load = function load (requestUrl, done) {
8687

8788
// request events
8889
req.on('error', done)
90+
8991
req.on('response', (res) => {
9092
if (res.statusCode !== 200) return done('Bad status code', null)
9193
res.pipe(feedparser)
@@ -96,7 +98,7 @@ RSSProvider.prototype.load = function load (requestUrl, done) {
9698

9799
feedparser.on('end', () => {
98100
context.dataCache.cacheResponse(this.datasource, JSON.stringify(items), () => {})
99-
done(null, items)
101+
return done(null, items)
100102
})
101103

102104
feedparser.on('readable', function () {

test/rss.xml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?xml version="1.0" encoding="windows-1252"?>
2+
<rss version="2.0">
3+
<channel>
4+
<title>FeedForAll Sample Feed</title>
5+
<description>RSS is a fascinating technology. The uses for RSS are expanding daily. Take a closer look at how various industries are using the benefits of RSS in their businesses.</description>
6+
<link>http://www.feedforall.com/industry-solutions.htm</link>
7+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
8+
<copyright>Copyright 2004 NotePage, Inc.</copyright>
9+
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
10+
<language>en-us</language>
11+
<lastBuildDate>Tue, 19 Oct 2004 13:39:14 -0400</lastBuildDate>
12+
<managingEditor>marketing@feedforall.com</managingEditor>
13+
<pubDate>Tue, 19 Oct 2004 13:38:55 -0400</pubDate>
14+
<webMaster>webmaster@feedforall.com</webMaster>
15+
<generator>FeedForAll Beta1 (0.0.1.8)</generator>
16+
<image>
17+
<url>http://www.feedforall.com/ffalogo48x48.gif</url>
18+
<title>FeedForAll Sample Feed</title>
19+
<link>http://www.feedforall.com/industry-solutions.htm</link>
20+
<description>FeedForAll Sample Feed</description>
21+
<width>48</width>
22+
<height>48</height>
23+
</image>
24+
<item>
25+
<title>RSS Solutions for Restaurants</title>
26+
<description>&lt;b&gt;FeedForAll &lt;/b&gt;helps Restaurant&apos;s communicate with customers. Let your customers know the latest specials or events.&lt;br&gt;
27+
&lt;br&gt;
28+
RSS feed uses include:&lt;br&gt;
29+
&lt;i&gt;&lt;font color=&quot;#FF0000&quot;&gt;Daily Specials &lt;br&gt;
30+
Entertainment &lt;br&gt;
31+
Calendar of Events &lt;/i&gt;&lt;/font&gt;</description>
32+
<link>http://www.feedforall.com/restaurant.htm</link>
33+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
34+
<comments>http://www.feedforall.com/forum</comments>
35+
<pubDate>Tue, 19 Oct 2004 11:09:11 -0400</pubDate>
36+
</item>
37+
<item>
38+
<title>RSS Solutions for Schools and Colleges</title>
39+
<description>FeedForAll helps Educational Institutions communicate with students about school wide activities, events, and schedules.&lt;br&gt;
40+
&lt;br&gt;
41+
RSS feed uses include:&lt;br&gt;
42+
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Homework Assignments &lt;br&gt;
43+
School Cancellations &lt;br&gt;
44+
Calendar of Events &lt;br&gt;
45+
Sports Scores &lt;br&gt;
46+
Clubs/Organization Meetings &lt;br&gt;
47+
Lunches Menus &lt;/i&gt;&lt;/font&gt;</description>
48+
<link>http://www.feedforall.com/schools.htm</link>
49+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
50+
<comments>http://www.feedforall.com/forum</comments>
51+
<pubDate>Tue, 19 Oct 2004 11:09:09 -0400</pubDate>
52+
</item>
53+
<item>
54+
<title>RSS Solutions for Computer Service Companies</title>
55+
<description>FeedForAll helps Computer Service Companies communicate with clients about cyber security and related issues. &lt;br&gt;
56+
&lt;br&gt;
57+
Uses include:&lt;br&gt;
58+
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Cyber Security Alerts &lt;br&gt;
59+
Specials&lt;br&gt;
60+
Job Postings &lt;/i&gt;&lt;/font&gt;</description>
61+
<link>http://www.feedforall.com/computer-service.htm</link>
62+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
63+
<comments>http://www.feedforall.com/forum</comments>
64+
<pubDate>Tue, 19 Oct 2004 11:09:07 -0400</pubDate>
65+
</item>
66+
<item>
67+
<title>RSS Solutions for Governments</title>
68+
<description>FeedForAll helps Governments communicate with the general public about positions on various issues, and keep the community aware of changes in important legislative issues. &lt;b&gt;&lt;i&gt;&lt;br&gt;
69+
&lt;/b&gt;&lt;/i&gt;&lt;br&gt;
70+
RSS uses Include:&lt;br&gt;
71+
&lt;i&gt;&lt;font color=&quot;#00FF00&quot;&gt;Legislative Calendar&lt;br&gt;
72+
Votes&lt;br&gt;
73+
Bulletins&lt;/i&gt;&lt;/font&gt;</description>
74+
<link>http://www.feedforall.com/government.htm</link>
75+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
76+
<comments>http://www.feedforall.com/forum</comments>
77+
<pubDate>Tue, 19 Oct 2004 11:09:05 -0400</pubDate>
78+
</item>
79+
<item>
80+
<title>RSS Solutions for Politicians</title>
81+
<description>FeedForAll helps Politicians communicate with the general public about positions on various issues, and keep the community notified of their schedule. &lt;br&gt;
82+
&lt;br&gt;
83+
Uses Include:&lt;br&gt;
84+
&lt;i&gt;&lt;font color=&quot;#FF0000&quot;&gt;Blogs&lt;br&gt;
85+
Speaking Engagements &lt;br&gt;
86+
Statements&lt;br&gt;
87+
&lt;/i&gt;&lt;/font&gt;</description>
88+
<link>http://www.feedforall.com/politics.htm</link>
89+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
90+
<comments>http://www.feedforall.com/forum</comments>
91+
<pubDate>Tue, 19 Oct 2004 11:09:03 -0400</pubDate>
92+
</item>
93+
<item>
94+
<title>RSS Solutions for Meteorologists</title>
95+
<description>FeedForAll helps Meteorologists communicate with the general public about storm warnings and weather alerts, in specific regions. Using RSS meteorologists are able to quickly disseminate urgent and life threatening weather warnings. &lt;br&gt;
96+
&lt;br&gt;
97+
Uses Include:&lt;br&gt;
98+
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Weather Alerts&lt;br&gt;
99+
Plotting Storms&lt;br&gt;
100+
School Cancellations &lt;/i&gt;&lt;/font&gt;</description>
101+
<link>http://www.feedforall.com/weather.htm</link>
102+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
103+
<comments>http://www.feedforall.com/forum</comments>
104+
<pubDate>Tue, 19 Oct 2004 11:09:01 -0400</pubDate>
105+
</item>
106+
<item>
107+
<title>RSS Solutions for Realtors &amp; Real Estate Firms</title>
108+
<description>FeedForAll helps Realtors and Real Estate companies communicate with clients informing them of newly available properties, and open house announcements. RSS helps to reach a targeted audience and spread the word in an inexpensive, professional manner. &lt;font color=&quot;#0000FF&quot;&gt;&lt;br&gt;
109+
&lt;/font&gt;&lt;br&gt;
110+
Feeds can be used for:&lt;br&gt;
111+
&lt;i&gt;&lt;font color=&quot;#FF0000&quot;&gt;Open House Dates&lt;br&gt;
112+
New Properties For Sale&lt;br&gt;
113+
Mortgage Rates&lt;/i&gt;&lt;/font&gt;</description>
114+
<link>http://www.feedforall.com/real-estate.htm</link>
115+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
116+
<comments>http://www.feedforall.com/forum</comments>
117+
<pubDate>Tue, 19 Oct 2004 11:08:59 -0400</pubDate>
118+
</item>
119+
<item>
120+
<title>RSS Solutions for Banks / Mortgage Companies</title>
121+
<description>FeedForAll helps &lt;b&gt;Banks, Credit Unions and Mortgage companies&lt;/b&gt; communicate with the general public about rate changes in a prompt and professional manner. &lt;br&gt;
122+
&lt;br&gt;
123+
Uses include:&lt;br&gt;
124+
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Mortgage Rates&lt;br&gt;
125+
Foreign Exchange Rates &lt;br&gt;
126+
Bank Rates&lt;br&gt;
127+
Specials&lt;/i&gt;&lt;/font&gt;</description>
128+
<link>http://www.feedforall.com/banks.htm</link>
129+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
130+
<comments>http://www.feedforall.com/forum</comments>
131+
<pubDate>Tue, 19 Oct 2004 11:08:57 -0400</pubDate>
132+
</item>
133+
<item>
134+
<title>RSS Solutions for Law Enforcement</title>
135+
<description>&lt;b&gt;FeedForAll&lt;/b&gt; helps Law Enforcement Professionals communicate with the general public and other agencies in a prompt and efficient manner. Using RSS police are able to quickly disseminate urgent and life threatening information. &lt;br&gt;
136+
&lt;br&gt;
137+
Uses include:&lt;br&gt;
138+
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Amber Alerts&lt;br&gt;
139+
Sex Offender Community Notification &lt;br&gt;
140+
Weather Alerts &lt;br&gt;
141+
Scheduling &lt;br&gt;
142+
Security Alerts &lt;br&gt;
143+
Police Report &lt;br&gt;
144+
Meetings&lt;/i&gt;&lt;/font&gt;</description>
145+
<link>http://www.feedforall.com/law-enforcement.htm</link>
146+
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
147+
<comments>http://www.feedforall.com/forum</comments>
148+
<pubDate>Tue, 19 Oct 2004 11:08:56 -0400</pubDate>
149+
</item>
150+
</channel>
151+
</rss>

0 commit comments

Comments
 (0)