Skip to content

Commit

Permalink
feat(promises): if caller doesn't specify a callback, return a promise (
Browse files Browse the repository at this point in the history
#193)

Resolves #189

BREAKING CHANGE: This removes the (deprecated) `request`-like behavior of
returning a stream when the caller omits a callback. Use `.getStream` (instead
of `.get` without a callback) to get a stream.
  • Loading branch information
silasbw committed Feb 11, 2018
1 parent d728814 commit 4a84e7f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 169 deletions.
26 changes: 3 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,9 @@ const core = new Api.Core(Api.config.fromKubeconfig());

### **Experimental** support for promises and async/await

kubernetes-client exposes **experimental** support for promises via
the `promises` option passed to API group constructors. The API is the
same, except for the functions that previously took a callback
(*e.g.*, `.get`). Those functions now return a promise.

```js
// Notice the promises: true
const core = new Api.Core({
url: 'http://my-k8s-api-server.com',
version: 'v1', // Defaults to 'v1'
promises: true, // Enable promises
namespace: 'my-project' // Defaults to 'default'
});
```

and then:
kubernetes-client has **experimental** support for promises. If you
omit callbacks an HTTP method function (*e.g.*, `.get`), it will
return a promise.

```js
core.namespaces.replicationcontrollers('http-rc').get()
Expand All @@ -99,13 +86,6 @@ or with `async/await`:
print(null, await core.namespaces.replicationcontrollers('http-rc').get());
```

You can invoke promise-based and callback-based functions explictly:

```js
print(null, await core.namespaces.replicationcontrollers('http-rc').getPromise());
core.namespaces.replicationcontrollers('http-rc').getCb(print);
```

### Creating and updating

kubernetes-client objects expose `.post`, `.patch`, and `.put`
Expand Down
11 changes: 4 additions & 7 deletions lib/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const promisify = require('util.promisify');
const promy = require('promy');

const matchExpression = require('./match-expression');

Expand Down Expand Up @@ -76,10 +76,7 @@ class BaseObject extends CallableObject {

const apiFunctions = ['delete', 'get', 'patch', 'post', 'put'];
apiFunctions.forEach(func => {
this[`${ func }Promise`] = promisify(this[`_${ func }`].bind(this));
this[`${ func }Cb`] = this[`_${ func }`].bind(this);
if (this.api.resourceConfig.promises) this[func] = this[`${ func }Promise`];
else this[func] = this[`${ func }Cb`];
this[func] = promy(this[`_${ func }`].bind(this));
});
}

Expand All @@ -96,7 +93,7 @@ class BaseObject extends CallableObject {
options = { name: options };
}
this.api.delete({ path: this._path(options), qs: options.qs, body: options.body },
cb200(cb));
cb200(cb));
}

_path(options) {
Expand Down Expand Up @@ -154,7 +151,7 @@ class BaseObject extends CallableObject {
*/
_post(options, cb) {
this.api.post({ path: this._path(options), body: options.body },
cb200(cb));
cb200(cb));
}

/**
Expand Down
171 changes: 41 additions & 130 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"async": "^2.6.0",
"js-yaml": "^3.10.0",
"lodash.merge": "^4.6.0",
"request": "^2.83.0",
"util.promisify": "^1.0.0"
"promy": "^0.1.0",
"request": "^2.83.0"
},
"devDependencies": {
"@types/node": "^9.4.2",
Expand Down
Loading

0 comments on commit 4a84e7f

Please sign in to comment.