Skip to content
This repository was archived by the owner on Feb 4, 2020. It is now read-only.

Commit 94aa5a1

Browse files
committed
Refactor into ES6 class, add more tests and update docs
Signed-off-by: Finnian Anderson <[email protected]>
1 parent fa28bae commit 94aa5a1

File tree

9 files changed

+421
-221
lines changed

9 files changed

+421
-221
lines changed

README.md

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,116 @@
22
style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
33
![OpenFaaS](https://img.shields.io/badge/openfaas-serverless-blue.svg)
44

5-
##### Usage
5+
# Installation
66

77
Add `openfaas` via `npm`
88

99
```
10-
$ npm install openfaas --save
10+
$ npm i openfaas
1111
```
1212

13-
Example usage
13+
# Example usage
1414

15+
```javascript
16+
const OpenFaaS = require('openfaas')
17+
18+
const openfaas = new OpenFaaS('http://localhost:8080')
19+
```
20+
21+
## Invoking functions
22+
23+
```js
24+
openfaas
25+
.invoke('function name', 'input')
26+
.then(res => console.log(res.body))
27+
.catch(err => console.log(err))
28+
```
29+
30+
## Deploying functions
31+
32+
```js
33+
openfaas
34+
.deploy({
35+
name: 'my-function', // name your function
36+
network: 'func_functions', // choose your network (default func_functions)
37+
image: 'hello-serverless' // choose the Docker image
38+
})
39+
.then(res => console.log(res))
40+
.catch(err => console.log(err))
1541
```
16-
const OpenFaaS = require('./openfaas')
1742

18-
const openfaas = OpenFaaS('http://localhost:8080')
43+
## Listing functions
1944

20-
openfaas.deploy(
21-
'yolo', // name your function
22-
'func_functions, // choose your network
23-
'hello-serverless // choose the Docker image
24-
)
25-
.then(x => console.log(x))
26-
.catch(err => console.log(err))
45+
```js
46+
openfaas.list()
47+
.then(res => console.log(res.body)) // an array of the deployed functions
48+
.catch(err => console.log(err))
49+
```
50+
51+
## Removing functions
52+
53+
```js
54+
openfaas.remove('my-function')
55+
.then(res => console.log(res))
56+
.catch(err => console.log(err))
57+
```
2758

28-
openfaas.invoke(
29-
'yolo', // function name
30-
'hello world', // data to send to function
31-
true //should response be JSON? optional. default is false
32-
)
33-
.then(x => console.log(x)) // handle response
34-
.catch(err => console.log(err))
59+
## Composing functions
3560

36-
openfaas.remove('yolo')
37-
.then(x => console.log(x)) // handle response
38-
.catch(err => console.log(err))
61+
You have the ability to chain functions which rely on the previous execution's output by using `openfaas.compose()`, like this:
3962

40-
openfaas.compose('initial data', [
41-
'func_nodeinfo',
42-
'func_echoit',
43-
'func_wordcount'
44-
]
45-
)
46-
.then(x => console.log(x.body)) // handle final output
47-
.catch(err => console.log(err))
63+
```js
64+
// the input for the first function
65+
const markdown = `
66+
# OpenFaaS chained functions example
67+
68+
[Find out more](https://github.com/openfaas-incubator/node-openfaas)
69+
`
70+
71+
openfaas.compose(markdown, ['func_markdown', 'func_base64']).then(res => {
72+
console.log(res.body)
73+
})
4874
```
4975

76+
```
77+
PGgxPk9wZW5GYWFTIGNoYWluZWQgZnVuY3Rpb25zIGV4YW1wbGU8L2gxPgoKPHA+PGEgaHJlZj0i
78+
aHR0cHM6Ly9naXRodWIuY29tL29wZW5mYWFzLWluY3ViYXRvci9ub2RlLW9wZW5mYWFzIiByZWw9
79+
Im5vZm9sbG93Ij5GaW5kIG91dCBtb3JlPC9hPjwvcD4KCg==
80+
```
81+
82+
This passes the output from the markdown renderer to the base64 function, and returns the output.
83+
84+
# Configuration
85+
86+
The OpenFaaS class constructor method accepts options in any of the following formats:
87+
```js
88+
const openfaas = new OpenFaaS('http://gateway:8080')
89+
const openfaas = new OpenFaaS('http://gateway:8080', options)
90+
const openfaas = new OpenFaaS(options)
91+
```
92+
93+
`options` is an object with the following properties:
94+
```js
95+
{
96+
gateway: 'gateway url', // (optional if passed as first parameter to the constructor)
97+
user: 'basic auth username', // (optional)
98+
pass: 'basic auth password' // (optional)
99+
}
100+
```
101+
102+
You can also add any of the options `got` supports since we just proxy them through. This includes all the options available through [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback). One example of this could be HTTP basic authentication (to set this up on your OpenFaaS cluster check out [the official guides](https://github.com/openfaas/faas/tree/master/guide#a-foreword-on-security)).
103+
104+
```js
105+
const openfaas = new OpenFaaS('http://localhost:8080', {
106+
user: 'user',
107+
pass: 'pass'
108+
})
109+
```
110+
111+
All the main methods (`invoke`, `deploy`, `list`, `remove` and `compose`) accept the same extra options parameter as above too.
112+
113+
In addition to this, `invoke` accepts a extra boolean option called `isBinaryResponse`. Setting this parameter to `true` in the options will mark the response as being binary content and will cause `invoke` to resolve to a response object who's body is a buffer.
114+
50115
##### ToDo
51116
* Complete tests
52-
* support additional request options for `got`
117+
* support additional request options for `got` (**done** - see #2)

openfaas/compose.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

openfaas/deploy.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

openfaas/index.js

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,96 @@
1-
'use strict';
1+
const path = require('path')
2+
const got = require('got')
23

3-
const deploy = require('./deploy');
4-
const remove = require('./remove');
5-
const invoke = require('./invoke');
6-
const compose = require('./compose');
4+
class OpenFaaS {
5+
constructor(gateway, options) {
6+
if (options) {
7+
// they passed two arguments (url, options)
8+
this.gateway = gateway
9+
} else if (typeof gateway === 'string') {
10+
// they passed a single string
11+
options = {}
12+
this.gateway = gateway
13+
} else {
14+
// they passed a single object
15+
options = gateway
16+
this.gateway = options.gateway
17+
}
718

8-
const OpenFaaS = url => ({
9-
deploy: deploy(url),
10-
remove: remove(url),
11-
invoke: invoke(url),
12-
compose: compose(url)
13-
});
19+
if (!this.gateway) throw new Error('Missing option `gateway`')
1420

15-
module.exports = OpenFaaS;
21+
if (options.user) {
22+
if (!options.pass) throw new Error('Missing option `pass`')
23+
options.auth = `${options.user}:${options.pass}`
24+
}
1625

26+
// save options for got
27+
this.gotOptions = options
28+
}
29+
30+
invoke(fn, data, options) {
31+
options = options || {}
32+
33+
// merge our defaults and their passed options
34+
const gotOptions = { ...this.gotOptions, ...options, method: 'POST' }
35+
36+
gotOptions.encoding = options.isBinaryResponse ? null : 'utf8'
37+
38+
if (data) gotOptions.body = data
39+
40+
return got(this.buildFunctionPath(fn), gotOptions)
41+
}
42+
43+
deploy({ name, network, image }, options) {
44+
const gotOptions = {
45+
...this.gotOptions,
46+
...(options || {}),
47+
body: {
48+
service: name,
49+
network: network || 'func_functions',
50+
image
51+
},
52+
json: true
53+
}
54+
55+
return got.post(this.gateway + '/system/functions', gotOptions)
56+
}
57+
58+
list(options) {
59+
const gotOptions = { ...this.gotOptions, ...(options || {}), json: true }
60+
61+
return got.get(this.gateway + '/system/functions', gotOptions)
62+
}
63+
64+
compose(data, functions, options) {
65+
// no initial data
66+
if (!functions) {
67+
functions = data
68+
data = undefined
69+
}
70+
71+
// build an array of functions to be called with result from previous function
72+
// [ function1(data), function2(data), ... ]
73+
const calls = functions.map(f => data => this.invoke(f, data, options || {}))
74+
75+
return calls.reduce((chain, current) => {
76+
return chain.then(res => current(res.body))
77+
}, Promise.resolve({ body: data }))
78+
}
79+
80+
remove(fn, options) {
81+
const gotOptions = {
82+
...this.gotOptions,
83+
...(options || {}),
84+
json: true,
85+
body: { functionName: fn }
86+
}
87+
88+
return got.delete(this.gateway + '/system/functions', gotOptions)
89+
}
90+
91+
buildFunctionPath(fn) {
92+
return this.gateway + path.join('/function', fn)
93+
}
94+
}
95+
96+
module.exports = OpenFaaS

openfaas/invoke.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

openfaas/remove.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)