Comparing version 1.2.0 to 2.0.0
49
API.md
## Gofer - API | ||
The module exports one function, `buildGofer`. | ||
The module exports one class, `Gofer`. | ||
In addition it exposes `gofer/hub` which exports [`Hub`](#hub). | ||
#### buildGofer(serviceName: String, serviceVersion: String) -> Gofer | ||
Create a new gofer class for a given service. | ||
`serviceName` is used in multiple places: | ||
* It is the key under which instances of the class will be looking for service-level [configuration](#configuration) | ||
* It is injected into the fetch options, see [events and logging](#events-and-logging) | ||
* It is used to build the `User-Agent` header | ||
### Gofer | ||
@@ -23,6 +14,19 @@ | ||
#### Static methods | ||
This class can be used directly, | ||
but it's mainly meant to be the base class for individual service clients. | ||
Child classes should add `serviceName` and `serviceVersion` to their prototype. | ||
Example: | ||
##### Gofer.addOptionMapper(mapFn) | ||
```js | ||
function MyClient() { | ||
Gofer.apply(this, arguments); | ||
} | ||
MyClient.prototype.serviceName = 'myService'; | ||
MyClient.prototype.serviceVersion = require('package.json').version; | ||
``` | ||
#### Methods modifying the prototype | ||
##### Gofer.prototype.addOptionMapper(mapFn) | ||
Add a new option mapper to *all* instances that are created afterwards. This | ||
@@ -33,3 +37,3 @@ can also be called on an instance which doesn't have a global effect. | ||
##### Gofer.clearOptionMappers() | ||
##### Gofer.prototype.clearOptionMappers() | ||
@@ -39,3 +43,3 @@ Clear the option mapper chain for all instances that are created afterwards. | ||
##### Gofer.registerEndpoints(endpointMap) | ||
##### Gofer.prototype.registerEndpoints(endpointMap) | ||
@@ -194,3 +198,4 @@ Registers "endpoints". Endpoints are convenience methods for easier | ||
```js | ||
var buildGofer = require('gofer'); | ||
var Gofer = require('gofer'); | ||
var util = require('util'); | ||
@@ -209,4 +214,9 @@ var config = { | ||
var GoferA = buildGofer('a'), GoferB = buildGofer('b'); | ||
GoferB.registerEndpoints({ | ||
function GoferA() { Gofer.apply(this, arguments); } | ||
util.inherits(GoferA, Gofer); | ||
function GoferB() { Gofer.apply(this, arguments); } | ||
util.inherits(GoferB, Gofer); | ||
GoferB.prototype.registerEndpoints({ | ||
x: function(request) { | ||
@@ -230,5 +240,4 @@ return function(cb) { return request('/something', cb); } | ||
```js | ||
var buildGofer = require('gofer'); | ||
var GoferA = buildGofer('a'); // client for service "a" | ||
var GoferB = buildGofer('b'); // client for service "b" | ||
var GoferA = require('a-gofer'); // client for service "a" | ||
var GoferB = require('b-gofer'); // client for service "b" | ||
@@ -235,0 +244,0 @@ var hub = require('gofer/hub')(); // create a new hub |
@@ -35,3 +35,3 @@ /* | ||
void function () { | ||
var addOptionMapper, applyBaseUrl, buildGofer, buildUserAgent, cache$, cache$1, clearOptionMappers, Gofer, Hub, isJsonResponse, merge, parseDefaults, registerEndpoints, replacePathParms, resolveOptional, safeParseJSON; | ||
var applyBaseUrl, buildUserAgent, cache$, cache$1, Gofer, Hub, isJsonResponse, merge, parseDefaults, replacePathParms, resolveOptional, safeParseJSON; | ||
Hub = require('./hub'); | ||
@@ -224,2 +224,12 @@ cache$ = require('./helpers'); | ||
}; | ||
Gofer.prototype._mappers = [function (opts) { | ||
var baseUrl; | ||
baseUrl = opts.baseUrl; | ||
if (null != baseUrl) { | ||
delete opts.baseUrl; | ||
return this.applyBaseUrl(baseUrl, opts); | ||
} else { | ||
return opts; | ||
} | ||
}]; | ||
return Gofer; | ||
@@ -229,64 +239,4 @@ }(); | ||
Gofer.prototype.get = Gofer.prototype.request; | ||
Gofer.prototype._mappers = [function (opts) { | ||
var baseUrl; | ||
baseUrl = opts.baseUrl; | ||
if (null != baseUrl) { | ||
delete opts.baseUrl; | ||
return this.applyBaseUrl(baseUrl, opts); | ||
} else { | ||
return opts; | ||
} | ||
}]; | ||
addOptionMapper = function (GoferClass, mapper) { | ||
return GoferClass.prototype.addOptionMapper(mapper); | ||
}; | ||
clearOptionMappers = function (GoferClass) { | ||
return GoferClass.prototype.clearOptionMappers(); | ||
}; | ||
registerEndpoints = function (GoferClass, endpointMap) { | ||
return GoferClass.prototype.registerEndpoints(endpointMap); | ||
}; | ||
module.exports = buildGofer = function (serviceName, serviceVersion) { | ||
var CustomGofer; | ||
CustomGofer = function (super$) { | ||
extends$(CustomGofer, super$); | ||
function CustomGofer(config, hub) { | ||
Gofer.call(this, config, hub); | ||
} | ||
CustomGofer.prototype.serviceName = serviceName; | ||
CustomGofer.prototype.serviceVersion = serviceVersion; | ||
return CustomGofer; | ||
}(Gofer); | ||
CustomGofer.addOptionMapper = function (mapper) { | ||
return addOptionMapper(CustomGofer, mapper); | ||
}; | ||
CustomGofer.clearOptionMappers = function () { | ||
return clearOptionMappers(CustomGofer); | ||
}; | ||
CustomGofer.registerEndpoints = function (endpointMap) { | ||
return registerEndpoints(CustomGofer, endpointMap); | ||
}; | ||
CustomGofer.serviceName = serviceName; | ||
return CustomGofer; | ||
}; | ||
buildGofer.Gofer = Gofer; | ||
buildGofer.addOptionMapper = addOptionMapper; | ||
buildGofer.clearOptionMappers = clearOptionMappers; | ||
buildGofer.registerEndpoints = registerEndpoints; | ||
buildGofer['default'] = buildGofer; | ||
function isOwn$(o, p) { | ||
return {}.hasOwnProperty.call(o, p); | ||
} | ||
function extends$(child, parent) { | ||
for (var key in parent) | ||
if (isOwn$(parent, key)) | ||
child[key] = parent[key]; | ||
function ctor() { | ||
this.constructor = child; | ||
} | ||
ctor.prototype = parent.prototype; | ||
child.prototype = new ctor; | ||
child.__super__ = parent.prototype; | ||
return child; | ||
} | ||
module.exports = Gofer; | ||
Gofer['default'] = Gofer; | ||
}.call(this); |
{ | ||
"name": "gofer", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A general purpose service client library for node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/gofer.js", |
@@ -12,3 +12,3 @@ # gofer | ||
A base library to develop specialized ReST clients with. | ||
A base class to develop specialized ReST clients with. | ||
The general design is meant to enforce a certain level of consistency in how the clients are configured and instrumented. | ||
@@ -110,3 +110,11 @@ Uses [request](https://github.com/mikeal/request) to do the actual fetching. | ||
```js | ||
var Github = require('gofer')('github'); | ||
var Gofer = require('gofer'); | ||
var util = require('util'); | ||
function Github() { | ||
Gofer.apply(this, arguments); | ||
} | ||
util.inherits(Github, Gofer); | ||
Github.prototype.serviceName = 'github'; | ||
Github.prototype.serviceVersion = require('./package.json').version; | ||
``` | ||
@@ -120,3 +128,3 @@ | ||
```js | ||
Github.registerEndpoints({ | ||
Github.prototype.registerEndpoints({ | ||
// Every instance of Github will get an `emojis` property. On | ||
@@ -166,5 +174,5 @@ // access it will be initialized with an instrumented version of the | ||
// we need to remove it. | ||
Github.clearOptionMappers(); | ||
Github.prototype.clearOptionMappers(); | ||
omit = require('lodash').omit; | ||
Github.addOptionMapper(function(opts) { | ||
Github.prototype.addOptionMapper(function(opts) { | ||
// opts contains the already merged options, including global-, service-, | ||
@@ -171,0 +179,0 @@ // and endpoint-defaults. In our example opts.uri will be '/emojis', |
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
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
204
47175
631