|
2 | 2 | style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
|
3 | 3 | 
|
4 | 4 |
|
5 |
| -##### Usage |
| 5 | +# Installation |
6 | 6 |
|
7 | 7 | Add `openfaas` via `npm`
|
8 | 8 |
|
9 | 9 | ```
|
10 |
| -$ npm install openfaas --save |
| 10 | +$ npm i openfaas |
11 | 11 | ```
|
12 | 12 |
|
13 |
| -Example usage |
| 13 | +# Example usage |
14 | 14 |
|
| 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)) |
15 | 41 | ```
|
16 |
| -const OpenFaaS = require('./openfaas') |
17 | 42 |
|
18 |
| -const openfaas = OpenFaaS('http://localhost:8080') |
| 43 | +## Listing functions |
19 | 44 |
|
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 | +``` |
27 | 58 |
|
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 |
35 | 60 |
|
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: |
39 | 62 |
|
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 | +}) |
48 | 74 | ```
|
49 | 75 |
|
| 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 | + |
50 | 115 | ##### ToDo
|
51 | 116 | * Complete tests
|
52 |
| -* support additional request options for `got` |
| 117 | +* support additional request options for `got` (**done** - see [!6](https://github.com/openfaas-incubator/node-openfaas/pull/6)) |
0 commit comments