Skip to content

Commit

Permalink
Merge pull request #6 from feugy/async
Browse files Browse the repository at this point in the history
Use async/await
  • Loading branch information
feugy committed Jan 30, 2018
2 parents 343086f + 9441aff commit 342627e
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 128 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Changelog

## 4.0.0
- Replace promise-based code with async/await equivalent

## 3.3.0
- Replace internals to use latest validation mecanics
Error message when invoking an API with too many parameters is slightly different.
Previously: `must contain at most`, now: `"x" is not allowed`
- Update docs template from docdash to docma
- Dependencies update

## 3.2.1
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ When Mini-client is running on local mode, such situation can never happen.
Copyright [Damien Simonin Feugas][feugy] and other contributors, licensed under [MIT](./LICENSE).


## 2.x to 3.x changes
## 3.x to 4.x migration

None: v4 is using async/await, which requires node@8+. Code is fully backward compatible.


## 2.x to 3.x migration

Groups are now used as sub-objects of mini-client.

Expand All @@ -136,7 +141,7 @@ client = {
```


## 1.x to 2.x changes
## 1.x to 2.x migration

Local services, as remote services, **must** have `name` and `version` options defined

Expand Down
13 changes: 6 additions & 7 deletions docma.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
],
"dest": "./docs",
"jsdoc": {
"allowUnknownTags": false,
"allowUnknownTags": true,
"hierarchy": true,
"recurse": true,
"sort": "grouped"
"recurse": true
},
"app": {
"title": "Mini-client documentation",
Expand All @@ -17,21 +16,21 @@
},
"template": {
"path": "default",
"outline": "flat",
"options": {
"title": "mini-client",
"outline": "tree",
"navItems": [{
"iconClass": "glyphicon glyphicon-map-marker",
"label": "Welcome",
"href": "#?content=readme"
"href": "?content=readme"
}, {
"iconClass": "glyphicon glyphicon-book",
"label": "API Reference",
"href": "#?api"
"href": "?api"
}, {
"iconClass": "glyphicon glyphicon-fire",
"label": "Changelog",
"href": "#?content=changelog"
"href": "?content=changelog"
}, {
"iconClass": "ico-md ico-github",
"label": "GitHub",
Expand Down
5 changes: 5 additions & 0 deletions docs/content/changelog.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@

<html><head></head><body><h1 id="changelog">Changelog</h1>
<hr />
<h2 id="4-0-0">4.0.0</h2>
<ul>
<li>Replace promise-based code with async/await equivalent</li>
</ul>
<h2 id="3-3-0">3.3.0</h2>
<ul>
<li>Replace internals to use latest validation mecanics
Error message when invoking an API with too many parameters is slightly different.
Previously: <code>must contain at most</code>, now: <code>"x" is not allowed</code></li>
<li>Update docs template from docdash to docma</li>
<li>Dependencies update</li>
</ul>
<h2 id="3-2-1">3.2.1</h2>
Expand Down
6 changes: 4 additions & 2 deletions docs/content/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ <h2 id="checksum-compatibility-and-automatic-reloads">Checksum compatibility and
<p>When Mini-client is running on local mode, such situation can never happen.</p>
<h2 id="license">License</h2>
<p>Copyright <a href="https://github.com/feugy">Damien Simonin Feugas</a> and other contributors, licensed under <a href="./LICENSE">MIT</a>.</p>
<h2 id="2-x-to-3-x-changes">2.x to 3.x changes</h2>
<h2 id="3-x-to-4-x-migration">3.x to 4.x migration</h2>
<p>None: v4 is using async/await, which requires node@8+. Code is fully backward compatible.</p>
<h2 id="2-x-to-3-x-migration">2.x to 3.x migration</h2>
<p>Groups are now used as sub-objects of mini-client.</p>
<p>Given a service exposing:</p>
<ul>
Expand All @@ -103,7 +105,7 @@ <h2 id="2-x-to-3-x-changes">2.x to 3.x changes</h2>
}
}
</code></pre>
<h2 id="1-x-to-2-x-changes">1.x to 2.x changes</h2>
<h2 id="1-x-to-2-x-migration">1.x to 2.x migration</h2>
<p>Local services, as remote services, <strong>must</strong> have <code>name</code> and <code>version</code> options defined</p>
<p>When loading services, the <code>services</code> property was renamed to <code>groups</code>, and <code>serviceOpts</code> is now <code>groupOpts</code>:</p>
<pre><code class="lang-javascript">const {startServer} = require('mini-service')
Expand Down
2 changes: 1 addition & 1 deletion docs/js/docma-web.js

Large diffs are not rendered by default.

32 changes: 15 additions & 17 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ const reserved = ['then', 'catch', 'finally']

/**
* Generic client to a local or remote µService built with mini-service.
*
* @class
* @memberof module:index
*/
class Client {
/**
* Service version.
* @summary Service version.
*
* For remote clients, undefined until the first call. Then, set to server's version
* @desc For remote clients, undefined until the first call. Then, set to server's version
*
* @member {String}
*/
Expand All @@ -36,14 +38,14 @@ class Client {
* @param {String} [opts.timeout] HTTP request timeout (ms). Default to a 20 seconds (remote client only)
* @param {String} [opts.name] service name (local client only)
* @param {String} [opts.version] service version (local client only)
* @param {Function} [opts.init] initialization function that takes options as parameter and returns
* a Promise resolved with exposed APIs (an object with functions that returns promises).
* @param {Function} [opts.init] async initialization function that takes options as parameter and
* resolves with exposed APIs (an object of async functions).
* Takes precedence over `opts.groups` as a simpler alternative of API group.
* The `opts` object itself will be used as options for this single API group.
* @param {Array<Object>} [opts.groups] exposed APIs groups, an array containing for each group:
* @param {String} opts.groups.name group friendly name (a valid JavaScript identifier)
* @param {Function} opts.groups.init initialization function that takes options as parameter and returns
* a Promise resolved with exposed APIs (an object with functions that returns promises)
* @param {Function} opts.groups.init async initialization function that takes options as parameter and
* resolves with exposed APIs (an object of async functions)
* @param {Object} [opts.groupOpts] per-group configuration. might contain a properties named after group
*/
constructor (opts = {}) {
Expand Down Expand Up @@ -130,19 +132,15 @@ class Client {
* `init()` needs to be called for local clients, as it will invoke service definition's
* `init()` async methods. But for remote clients, this is a noop, and can be skipped.
*
* @returns {Promise} resolved when client is initialized, without any arguments
* @async
*/
init () {
if (this.initialized || this.options.remote) return Promise.resolve()
async init () {
if (this.initialized || this.options.remote) return
// local: register all available apis serially
const {logger} = this.options
return Promise.resolve()
.then(() =>
// local: register all available apis serially
registerLocal(this, this.options, logger)
).then(() => {
this.initialized = true
logger.debug('local client ready')
})
await registerLocal(this, this.options, logger)
this.initialized = true
logger.debug('local client ready')
}
}

Expand Down
Loading

0 comments on commit 342627e

Please sign in to comment.