Comparing version 0.0.5 to 0.1.0
@@ -1,2 +0,5 @@ | ||
### 0.0.5 (SNAPSHOT) | ||
### 0.1.0-pre | ||
* Update constructor signature, url and proxy are now optional | ||
### 0.0.5 | ||
* Handle non-200 endpoint response. | ||
@@ -3,0 +6,0 @@ |
/** | ||
* Get by Listing ID endpoint name and parameters. | ||
* Reference: http://developers.sensis.com.au/docs/endpoint_reference/Metadata | ||
**/ | ||
*/ | ||
var name = 'getByListingId', | ||
@@ -9,10 +9,11 @@ params = { | ||
optional: [] | ||
}; | ||
}, | ||
handlers = { 200: _success, 400: _invalid }; | ||
/** | ||
* getbylistingid#path -> String | ||
* - params (Object): endpoint parameters constructed from chainable functions | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
* | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
**/ | ||
* @param params {Object}: endpoint parameters constructed from chainable functions | ||
* @return {String} endpoint path | ||
*/ | ||
function path (params) { | ||
@@ -22,19 +23,12 @@ return name; | ||
/** | ||
* getbylistingid#handlers -> Object | ||
* - cb (Function): standard cb(err, result) callback | ||
* | ||
* Set up endpoint success and error handlers. | ||
**/ | ||
function handlers(cb) { | ||
return { | ||
200: function (result) { | ||
cb(null, result); | ||
}, | ||
400: function (result) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
}; | ||
// endpoint handlers | ||
function _success(result, cb) { | ||
cb(null, result); | ||
} | ||
function _invalid(result, cb) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
exports.name = name; | ||
@@ -41,0 +35,0 @@ exports.params = params; |
/** | ||
* Metadata endpoint name and parameters. | ||
* Reference: http://developers.sensis.com.au/docs/endpoint_reference/Metadata | ||
**/ | ||
*/ | ||
var name = 'metadata', | ||
@@ -9,10 +9,11 @@ params = { | ||
optional: [] | ||
}; | ||
}, | ||
handlers = { 200: _success }; | ||
/** | ||
* metadata#path -> String | ||
* - params (Object): endpoint parameters constructed from chainable functions | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
* | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
**/ | ||
* @param params {Object}: endpoint parameters constructed from chainable functions | ||
* @return {String} endpoint path | ||
*/ | ||
function path (params) { | ||
@@ -22,14 +23,6 @@ return name + '/' + params.dataType; | ||
/** | ||
* metadata#handlers -> Object | ||
* - cb (Function): standard cb(err, result) callback | ||
* | ||
* Set up endpoint success and error handlers. | ||
**/ | ||
function handlers(cb) { | ||
return { | ||
200: function (result) { | ||
cb(null, result); | ||
} | ||
}; | ||
// endpoint handlers | ||
function _success(result, cb) { | ||
cb(null, result); | ||
} | ||
@@ -36,0 +29,0 @@ |
/** | ||
* Report endpoint name and parameters. | ||
* Reference: http://developers.sensis.com.au/docs/endpoint_reference/Report | ||
**/ | ||
*/ | ||
var name = 'report', | ||
@@ -9,10 +9,11 @@ params = { | ||
optional: ['userAgent', 'userSessionId', 'content'] | ||
}; | ||
}, | ||
handlers = { 200: _success, 206: _spellChecked, 400: _invalid }; | ||
/** | ||
* report#path -> String | ||
* - params (Object): endpoint parameters constructed from chainable functions | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
* | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
**/ | ||
* @param params {Object}: endpoint parameters constructed from chainable functions | ||
* @return {String} endpoint path | ||
*/ | ||
function path (params) { | ||
@@ -22,22 +23,16 @@ return name + '/' + params.eventName; | ||
/** | ||
* report#handlers -> Object | ||
* - cb (Function): standard cb(err, result) callback | ||
* | ||
* Set up endpoint success and error handlers. | ||
**/ | ||
function handlers(cb) { | ||
return { | ||
200: function (result) { | ||
cb(null, result); | ||
}, | ||
206: function (result) { | ||
cb(null, result); | ||
}, | ||
400: function (result) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
}; | ||
// endpoint handlers | ||
function _success(result, cb) { | ||
cb(null, result); | ||
} | ||
function _spellChecked(result, cb) { | ||
cb(null, result); | ||
} | ||
function _invalid(result, cb) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
exports.name = name; | ||
@@ -44,0 +39,0 @@ exports.params = params; |
/** | ||
* Search endpoint name and parameters. | ||
* Reference: http://developers.sensis.com.au/docs/endpoint_reference/Search | ||
**/ | ||
*/ | ||
var name = 'search', | ||
@@ -9,10 +9,11 @@ params = { | ||
optional: ['page', 'rows', 'sortBy', 'sensitiveCategories', 'categoryId', 'postcode', 'radius', 'locationTiers', 'suburb', 'state', 'boundingBox', 'content', 'productKeyword'] | ||
}; | ||
}, | ||
handlers = { 200: _success, 206: _spellChecked, 400: _invalid }; | ||
/** | ||
* search#path -> String | ||
* - params (Object): endpoint parameters constructed from chainable functions | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
* | ||
* Create the endpoint URL path, to be appended to SAPI base URL. | ||
**/ | ||
* @param {Object} params: endpoint parameters constructed from chainable functions | ||
* @return {String} endpoint path | ||
*/ | ||
function path (params) { | ||
@@ -22,22 +23,16 @@ return name; | ||
/** | ||
* search#handlers -> Object | ||
* - cb (Function): standard cb(err, result) callback | ||
* | ||
* Set up endpoint success and error handlers. | ||
**/ | ||
function handlers(cb) { | ||
return { | ||
200: function (result) { | ||
cb(null, result); | ||
}, | ||
206: function (result) { | ||
cb(null, result); | ||
}, | ||
400: function (result) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
}; | ||
// endpoint handlers | ||
function _success(result, cb) { | ||
cb(null, result); | ||
} | ||
function _spellChecked(result, cb) { | ||
cb(null, result); | ||
} | ||
function _invalid(result, cb) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
exports.name = name; | ||
@@ -44,0 +39,0 @@ exports.params = params; |
111
lib/sapi.js
@@ -1,2 +0,3 @@ | ||
var request = require('request'), | ||
var _ = require('underscore'), | ||
bag = require('bagofholding'), | ||
search = require('./endpoint/search'), | ||
@@ -8,73 +9,31 @@ getByListingId = require('./endpoint/getbylistingid'), | ||
/** internal | ||
* sapi#_http(method, url, qs, handlers, cb) | ||
* - method (String): http method | ||
* - url (String): Jenkins URL without query string | ||
* - proxy (String): proxy server URL in format http://user:pass@host:port | ||
* - qs (Object): object containing URL query strings with format { name: value } | ||
* - handlers (Object): response handlers with format { statuscode: handlerfunction } | ||
* - cb (Function): standard cb(err, result) callback | ||
/** | ||
* class Sapi | ||
* | ||
* Sends a HTTP request to a SAPI URL and handle the following errors: | ||
* - request error | ||
* - authentication error | ||
* - unexpected status error | ||
**/ | ||
function _http(method, url, proxy, queryStrings, handlers, cb) { | ||
* @param {String} key: Sensis API key, apply for the key here http://developers.sensis.com.au/docs/getting_started/Apply_for_an_API_key | ||
* @param {Object} opts: optional | ||
* - url: optional Sensis API base URL, defaults to http://developers.sensis.com.au/docs/using_endpoints | ||
*/ | ||
function Sapi(key, opts) { | ||
opts = opts || {}; | ||
var params = { | ||
method: method, | ||
uri: url, | ||
qs: queryStrings | ||
}; | ||
if (proxy) { | ||
params.proxy = proxy; | ||
} | ||
request(params, function (err, result) { | ||
if (err) { | ||
cb(err); | ||
} else if (result.statusCode === 403) { | ||
// SAPI does not differentiate required authentication and failed authentication | ||
// since both return status code 403, need to check key param existence to differentiate the two | ||
if (queryStrings.key) { | ||
cb(new Error('Authentication failed - invalid key ' + queryStrings.key)); | ||
} else { | ||
cb(new Error('SAPI requires authentication - set key to SAPI instance')); | ||
} | ||
} else if (handlers[result.statusCode]) { | ||
// SAPI documentation indicates that all expected response is in JSON format | ||
handlers[result.statusCode](JSON.parse(result.body)); | ||
function _authRequire(result, cb) { | ||
// SAPI does not differentiate required authentication and failed authentication | ||
// since both return status code 403, need to check key param existence to differentiate the two | ||
if (key) { | ||
cb(new Error('Authentication failed - invalid key ' + key)); | ||
} else { | ||
cb(new Error('Unexpected status code ' + result.statusCode + ' from SAPI\nResponse body:\n' + result.body)); | ||
cb(new Error('Authentication required - set API key in Sapi constructor')); | ||
} | ||
}); | ||
} | ||
} | ||
/** | ||
* class Sapi | ||
* - key (String): Sensis API key, apply here http://developers.sensis.com.au/docs/getting_started/Apply_for_an_API_key | ||
* - url (String): Sensis API base URL - http://developers.sensis.com.au/docs/using_endpoints | ||
**/ | ||
function Sapi(key, url) { | ||
this.params = { key: key }; | ||
this.url = (url || 'http://api.sensis.com.au/ob-20110511/test').replace(/\/$/, ''); | ||
if (process.env.http_proxy) { | ||
this._proxy = process.env.http_proxy; | ||
} | ||
this.url = (opts.url || 'http://api.sensis.com.au/ob-20110511/test').replace(/\/$/, ''); | ||
this.opts = { | ||
handlers: { | ||
403: _authRequire | ||
} | ||
}; | ||
} | ||
/** | ||
* Sapi#proxy(String) -> Object | ||
* - proxy (String): proxy server URL in format http://user:pass@host:port | ||
* | ||
* Set proxy server URL, used to connect to SAPI service. | ||
**/ | ||
Sapi.prototype.proxy = function (proxy) { | ||
this._proxy = proxy; | ||
return this; | ||
}; | ||
endpoints.forEach(function (endpoint) { | ||
@@ -86,9 +45,8 @@ | ||
/** | ||
* Sapi#endpointparam(value) -> Object | ||
* - value (String): parameter value | ||
* | ||
* Chainable function for each parameter. | ||
* Usage pattern: sapi.param1().param2().param3().end(); | ||
* The idea here is to allow the construction of 0...* parameters before calling the endpoint. | ||
**/ | ||
* | ||
* @param {String} value: parameter value | ||
*/ | ||
Sapi.prototype[param] = function (value) { | ||
@@ -101,12 +59,15 @@ this.params[param] = value; | ||
/** | ||
* Sapi#endpointname(cb) | ||
* - cb (Function): standard cb(err, result) callback | ||
* End function for each endpoint that makes http request to SAPI. | ||
* | ||
* End function for each endpoint that makes http request to SAPI. | ||
**/ | ||
Sapi.prototype[endpoint.name] = function _endpoint(cb) { | ||
_http('get', this.url + '/' + endpoint.path(this.params), this._proxy, this.params, endpoint.handlers(cb), cb); | ||
* @param {Function} cb: standard cb(err, result) callback | ||
*/ | ||
Sapi.prototype[endpoint.name] = function (cb) { | ||
this.opts.queryStrings = this.params; | ||
this.opts.handlers = _.extend(this.opts.handlers, endpoint.handlers); | ||
bag.http.request('get', this.url + '/' + endpoint.path(this.params), this.opts, cb); | ||
}; | ||
}); | ||
module.exports = Sapi; | ||
module.exports = Sapi; |
@@ -11,8 +11,8 @@ { | ||
], | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"homepage": "http://github.com/cliffano/sapi", | ||
"author": "Cliffano Subagio <blah@cliffano.com> (http://blog.cliffano.com)", | ||
"bin": { | ||
"sapi": "./bin/sapi" | ||
}, | ||
"contributors": [ | ||
"All contributors (https://github.com/cliffano/sapi/graphs/contributors)" | ||
], | ||
"main": "./lib/sapi", | ||
@@ -32,10 +32,9 @@ "repository": { | ||
"dependencies": { | ||
"request": "2.9.203" | ||
"bagofholding": "0.1.2", | ||
"underscore": "1.4.4" | ||
}, | ||
"devDependencies": { | ||
"bagofholding": "0.0.10", | ||
"mocha": "1.3.0", | ||
"sandboxed-module": "0.1.3", | ||
"should": "0.6.3" | ||
"buster": "0.6.12" | ||
}, | ||
"scripts": {}, | ||
"engines": { | ||
@@ -42,0 +41,0 @@ "node": ">= 0.4.0" |
@@ -1,5 +0,6 @@ | ||
Sapi [![http://travis-ci.org/cliffano/sapi](https://secure.travis-ci.org/cliffano/sapi.png?branch=master)](http://travis-ci.org/cliffano/sapi) | ||
Sapi [![Build Status](https://secure.travis-ci.org/cliffano/sapi.png?branch=master)](http://travis-ci.org/cliffano/sapi) [![Dependencies Status](https://david-dm.org/cliffano/sapi.png)](http://david-dm.org/cliffano/sapi) | ||
----------- | ||
<img align="right" src="https://raw.github.com/cliffano/sapi/master/avatar.jpg" alt="Avatar"/> | ||
[Sensis API](http://developers.sensis.com.au/about) Node.js client. | ||
Sapi is a [Sensis API](http://developers.sensis.com.au/about) Node.js client. | ||
@@ -24,5 +25,9 @@ This is handy when you want to use [Sensis API](http://developers.sensis.com.au/about) service from a Node.js application. Sapi module provides a chainable interface to set the endpoint parameters. | ||
var sapi = new (require('sapi'))('key', 'http://api.sensis.com.au/ob-20110511/test/'); | ||
var Sapi = require('sapi'), | ||
sapi = new Sapi('apikey', { | ||
url: 'http://api.sensis.com.au/ob-20110511/test/', | ||
proxy: 'http://user:pass@proxy:8080' | ||
}); | ||
sapi.proxy('http://user:pass@proxy:8080'); // optional | ||
You can [apply for Sensis API key](http://developers.sensis.com.au/docs/getting_started/Apply_for_an_API_key) from Sensis Developer Centre. | ||
@@ -29,0 +34,0 @@ Parameters can then be chained to an endpoint: |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
1
87
0
10596
2
10
179
1
+ Addedbagofholding@0.1.2
+ Addedunderscore@1.4.4
+ Addedasync@0.1.22(transitive)
+ Addedbagofholding@0.1.2(transitive)
+ Addedcommander@1.1.1(transitive)
+ Addeddateformat@1.0.4-1.2.3(transitive)
+ Addedjazz@0.0.18(transitive)
+ Addedkeypress@0.1.0(transitive)
+ Addedrequest@2.12.0(transitive)
+ Addedunderscore@1.4.4(transitive)
- Removedrequest@2.9.203
- Removedrequest@2.9.203(transitive)