Skip to content

Commit

Permalink
fix: don’t modify original schema endpoint
Browse files Browse the repository at this point in the history
otherwise placeholders no longer exist
  • Loading branch information
jimlambie committed Mar 16, 2017
1 parent d522eca commit 4901ecc
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="http://52.209.207.148/assets/products/dadi-web-full.png" alt="DADI Web" height="65"/>

[![npm (scoped)](https://img.shields.io/npm/v/@dadi/web.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/web)
[![coverage](https://img.shields.io/badge/coverage-67%25-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/web)
[![coverage](https://img.shields.io/badge/coverage-68%25-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/web)
[![Build Status](https://travis-ci.org/dadi/web.svg?branch=master)](https://travis-ci.org/dadi/web)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
[![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)
Expand Down
3 changes: 2 additions & 1 deletion dadi/lib/datasource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Datasource.prototype.processRequest = function (datasource, req) {

// Regular expression search for {param.nameOfParam} and replace with requestParameters
var paramRule = /("\{)(\bparams.\b)(.*?)(\}")/gmi

this.schema.datasource.filter = JSON.parse(JSON.stringify(this.schema.datasource.filter).replace(paramRule, function (match, p1, p2, p3, p4, offset, string) {
if (req.params[p3]) {
return req.params[p3]
Expand All @@ -168,7 +169,7 @@ Datasource.prototype.processRequest = function (datasource, req) {
this.schema.datasource.filter[obj.field] = paramValue
} else if (obj.target === 'endpoint') {
var placeholderRegex = new RegExp('{' + obj.field + '}', 'ig')
this.schema.datasource.source.endpoint = this.schema.datasource.source.endpoint.replace(placeholderRegex, paramValue)
this.source.modifiedEndpoint = this.schema.datasource.source.endpoint.replace(placeholderRegex, paramValue)
}
} else {
if (obj.target === 'filter') {
Expand Down
46 changes: 1 addition & 45 deletions dadi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ function onConnection (socket) {

function onListening (e) {
// check that our API connection is valid
help.isApiAvailable(function (err, result) {
help.isApiAvailable((err, result) => {
if (err) {
console.log(err)
console.log()
Expand Down Expand Up @@ -774,47 +774,3 @@ function onError (err) {
process.exit(0)
}
}

// function processSortParameter (obj) {
// var sort = {}
// if (typeof obj !== 'object' || obj === null) return sort
//
// _.each(obj, function (value, key) {
// if (typeof value === 'object' && value.hasOwnProperty('field') && value.hasOwnProperty('order')) {
// sort[value.field] = (value.order === 'asc') ? 1 : -1
// }
// })
//
// return sort
// }

// function parseRoutes (endpoints, req_path) {
// var params = {}
// var route_path = ''
// _.each(endpoints, function (endpoint) {
// var paths = endpoint.page.route.paths
// var req_path_items = req_path.split('/')
// _.each(paths, function (path) {
// path_items = path.split('/')
// if (path_items.length == req_path_items.length) {
// var alias = _.filter(path_items, function (item) {
// return item == '' || item.slice(0, 1) != ':'
// })
//
// if (_.difference(alias, _.intersection(path_items, req_path_items)).length == 0) {
// _.each(path_items, function (item, index) {
// if (item != '' && item.slice(0, 1) == ':') {
// params[item.slice(1)] = req_path_items[index]
// }
// })
// route_path = path
// }
// }
// })
// })
//
// return {
// route_path: route_path,
// params: params
// }
// }
34 changes: 15 additions & 19 deletions dadi/lib/providers/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ RemoteProvider.prototype.buildEndpoint = function buildEndpoint () {
const port = source.port || apiConfig.port

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

this.endpoint = this.processDatasourceParameters(this.schema, uri)
}
Expand Down Expand Up @@ -111,7 +111,6 @@ RemoteProvider.prototype.getHeaders = function getHeaders (done) {
* @return {void}
*/
RemoteProvider.prototype.handleResponse = function handleResponse (res, done) {
const self = this
const encoding = res.headers['content-encoding'] ? res.headers['content-encoding'] : ''
let output = ''

Expand All @@ -123,7 +122,7 @@ RemoteProvider.prototype.handleResponse = function handleResponse (res, done) {
buffer.push(data.toString())
}).on('end', () => {
output = buffer.join('')
self.processOutput(res, output, (err, data, res) => {
this.processOutput(res, output, (err, data, res) => {
if (err) return done(err)
return done(null, data, res)
})
Expand All @@ -138,7 +137,7 @@ RemoteProvider.prototype.handleResponse = function handleResponse (res, done) {
})

res.on('end', () => {
self.processOutput(res, output, (err, data, res) => {
this.processOutput(res, output, (err, data, res) => {
if (err) return done(err)
return done(null, data, res)
})
Expand Down Expand Up @@ -166,8 +165,6 @@ RemoteProvider.prototype.keepAliveAgent = function keepAliveAgent (protocol) {
* @return {void}
*/
RemoteProvider.prototype.load = function (requestUrl, done) {
const self = this

this.requestUrl = requestUrl
this.dataCache = DatasourceCache()

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

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

self.getHeaders((err, headers) => {
this.getHeaders((err, headers) => {
err && done(err)

self.options = _.extend(self.options, headers)
this.options = _.extend(this.options, headers)

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

const agent = (self.options.protocol === 'https') ? https : http
let request = agent.request(self.options, (res) => {
self.handleResponse(res, done)
const agent = (this.options.protocol === 'https') ? https : http
let request = agent.request(this.options, (res) => {
this.handleResponse(res, done)
})

request.on('error', (err) => {
const message = err.toString() + ". Couldn't request data from " + self.datasource.endpoint
const message = err.toString() + ". Couldn't request data from " + this.datasource.endpoint
err.name = 'GetData'
err.message = message
err.remoteIp = self.options.host
err.remotePort = self.options.port
err.remoteIp = this.options.host
err.remotePort = this.options.port
return done(err)
})

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

const params = [
Expand Down Expand Up @@ -258,8 +256,6 @@ RemoteProvider.prototype.processDatasourceParameters = function processDatasourc
* @return {void}
*/
RemoteProvider.prototype.processOutput = function processOutput (res, data, done) {
const self = this

// Return a 202 Accepted response immediately,
// along with the datasource response
if (res.statusCode === 202) {
Expand Down Expand Up @@ -291,8 +287,8 @@ RemoteProvider.prototype.processOutput = function processOutput (res, data, done
err.message = 'Datasource "' + this.datasource.name + '" failed. ' + res.statusMessage + ' (' + res.statusCode + ')' + ': ' + this.endpoint
if (data) err.message += '\n' + data

err.remoteIp = self.options.host
err.remotePort = self.options.port
err.remoteIp = this.options.host
err.remotePort = this.options.port

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

Expand Down
4 changes: 3 additions & 1 deletion dadi/lib/providers/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ RSSProvider.prototype.load = function load (requestUrl, done) {

const items = []
const feedparser = new FeedParser()

const req = request(this.endpoint, {
pool: false,
headers: {
Expand All @@ -86,6 +87,7 @@ RSSProvider.prototype.load = function load (requestUrl, done) {

// request events
req.on('error', done)

req.on('response', (res) => {
if (res.statusCode !== 200) return done('Bad status code', null)
res.pipe(feedparser)
Expand All @@ -96,7 +98,7 @@ RSSProvider.prototype.load = function load (requestUrl, done) {

feedparser.on('end', () => {
context.dataCache.cacheResponse(this.datasource, JSON.stringify(items), () => {})
done(null, items)
return done(null, items)
})

feedparser.on('readable', function () {
Expand Down
151 changes: 151 additions & 0 deletions test/rss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="windows-1252"?>
<rss version="2.0">
<channel>
<title>FeedForAll Sample Feed</title>
<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>
<link>http://www.feedforall.com/industry-solutions.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<copyright>Copyright 2004 NotePage, Inc.</copyright>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<language>en-us</language>
<lastBuildDate>Tue, 19 Oct 2004 13:39:14 -0400</lastBuildDate>
<managingEditor>[email protected]</managingEditor>
<pubDate>Tue, 19 Oct 2004 13:38:55 -0400</pubDate>
<webMaster>[email protected]</webMaster>
<generator>FeedForAll Beta1 (0.0.1.8)</generator>
<image>
<url>http://www.feedforall.com/ffalogo48x48.gif</url>
<title>FeedForAll Sample Feed</title>
<link>http://www.feedforall.com/industry-solutions.htm</link>
<description>FeedForAll Sample Feed</description>
<width>48</width>
<height>48</height>
</image>
<item>
<title>RSS Solutions for Restaurants</title>
<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;
&lt;br&gt;
RSS feed uses include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#FF0000&quot;&gt;Daily Specials &lt;br&gt;
Entertainment &lt;br&gt;
Calendar of Events &lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/restaurant.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:09:11 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Schools and Colleges</title>
<description>FeedForAll helps Educational Institutions communicate with students about school wide activities, events, and schedules.&lt;br&gt;
&lt;br&gt;
RSS feed uses include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Homework Assignments &lt;br&gt;
School Cancellations &lt;br&gt;
Calendar of Events &lt;br&gt;
Sports Scores &lt;br&gt;
Clubs/Organization Meetings &lt;br&gt;
Lunches Menus &lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/schools.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:09:09 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Computer Service Companies</title>
<description>FeedForAll helps Computer Service Companies communicate with clients about cyber security and related issues. &lt;br&gt;
&lt;br&gt;
Uses include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Cyber Security Alerts &lt;br&gt;
Specials&lt;br&gt;
Job Postings &lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/computer-service.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:09:07 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Governments</title>
<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;
&lt;/b&gt;&lt;/i&gt;&lt;br&gt;
RSS uses Include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#00FF00&quot;&gt;Legislative Calendar&lt;br&gt;
Votes&lt;br&gt;
Bulletins&lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/government.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:09:05 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Politicians</title>
<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;
&lt;br&gt;
Uses Include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#FF0000&quot;&gt;Blogs&lt;br&gt;
Speaking Engagements &lt;br&gt;
Statements&lt;br&gt;
&lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/politics.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:09:03 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Meteorologists</title>
<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;
&lt;br&gt;
Uses Include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Weather Alerts&lt;br&gt;
Plotting Storms&lt;br&gt;
School Cancellations &lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/weather.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:09:01 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Realtors &amp; Real Estate Firms</title>
<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;
&lt;/font&gt;&lt;br&gt;
Feeds can be used for:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#FF0000&quot;&gt;Open House Dates&lt;br&gt;
New Properties For Sale&lt;br&gt;
Mortgage Rates&lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/real-estate.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:08:59 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Banks / Mortgage Companies</title>
<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;
&lt;br&gt;
Uses include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Mortgage Rates&lt;br&gt;
Foreign Exchange Rates &lt;br&gt;
Bank Rates&lt;br&gt;
Specials&lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/banks.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:08:57 -0400</pubDate>
</item>
<item>
<title>RSS Solutions for Law Enforcement</title>
<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;
&lt;br&gt;
Uses include:&lt;br&gt;
&lt;i&gt;&lt;font color=&quot;#0000FF&quot;&gt;Amber Alerts&lt;br&gt;
Sex Offender Community Notification &lt;br&gt;
Weather Alerts &lt;br&gt;
Scheduling &lt;br&gt;
Security Alerts &lt;br&gt;
Police Report &lt;br&gt;
Meetings&lt;/i&gt;&lt;/font&gt;</description>
<link>http://www.feedforall.com/law-enforcement.htm</link>
<category domain="www.dmoz.com">Computers/Software/Internet/Site Management/Content Management</category>
<comments>http://www.feedforall.com/forum</comments>
<pubDate>Tue, 19 Oct 2004 11:08:56 -0400</pubDate>
</item>
</channel>
</rss>
Loading

0 comments on commit 4901ecc

Please sign in to comment.