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

Commit

Permalink
Updated README. Now uses openfaas instead of faas
Browse files Browse the repository at this point in the history
Signed-off-by: austinfrey <[email protected]>
  • Loading branch information
austinfrey committed Oct 9, 2017
1 parent d52861b commit fa28bae
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 43 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[![XO code
style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
![OpenFaaS](https://img.shields.io/badge/openfaas-serverless-blue.svg)

##### Usage

Add `openfaas` via `npm`

```
$ npm install openfaas --save
```

Example usage

```
const OpenFaaS = require('./openfaas')
const openfaas = OpenFaaS('http://localhost:8080')
openfaas.deploy(
'yolo', // name your function
'func_functions, // choose your network
'hello-serverless // choose the Docker image
)
.then(x => console.log(x))
.catch(err => console.log(err))
openfaas.invoke(
'yolo', // function name
'hello world', // data to send to function
true //should response be JSON? optional. default is false
)
.then(x => console.log(x)) // handle response
.catch(err => console.log(err))
openfaas.remove('yolo')
.then(x => console.log(x)) // handle response
.catch(err => console.log(err))
openfaas.compose('initial data', [
'func_nodeinfo',
'func_echoit',
'func_wordcount'
]
)
.then(x => console.log(x.body)) // handle final output
.catch(err => console.log(err))
```

##### ToDo
* Complete tests
* support additional request options for `got`
12 changes: 2 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
const Faas = require('./faas');
'use strict';

const faas = Faas('http://localhost:8080');

faas.deploy(
'yolo',
'func_functions',
'hello-serverless'
)
.then(x => console.log(x))
.catch(err => console.log(err));
module.exports = require('./openfaas');

File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions faas/index.js → openfaas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const remove = require('./remove');
const invoke = require('./invoke');
const compose = require('./compose');

const faas = url => ({
const OpenFaaS = url => ({
deploy: deploy(url),
remove: remove(url),
invoke: invoke(url),
compose: compose(url)
});

module.exports = faas;
module.exports = OpenFaaS;

5 changes: 3 additions & 2 deletions faas/invoke.js → openfaas/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const got = require('got');
const invoke = gateway => {
const url = gateway;

return (name, data) => {
return (name, data = null, isJson = false, isBinaryResponse = false) => {
const funcPath = path.join('/function', name);
const options = {
method: 'POST',
json: true
json: isJson,
encoding: (isBinaryResponse ? null : 'utf8')
};

if (data) {
Expand Down
File renamed without changes.
79 changes: 77 additions & 2 deletions package-lock.json

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

28 changes: 21 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
{
"name": "open-faas",
"version": "0.0.1",
"description": "",
"name": "openfaas",
"version": "0.0.4",
"description": "NodeJS wrapper around the OpenFaaS Gateway API w/ some additional helper functions",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test",
"lint": "xo --fix"
},
"author": "",
"license": "ISC",
"author": "Austin Frey",
"license": "MIT",
"dependencies": {
"bluebird": "^3.5.0",
"got": "^7.1.0"
},
"devDependencies": {
"nock": "^9.0.20",
"tape": "^4.8.0",
"xo": "^0.18.2"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/austinfrey/node-openfaas.git"
},
"keywords": [
"serverless",
"FaaS"
],
"bugs": {
"url": "https://github.com/austinfrey/node-openfaas/issues"
},
"homepage": "https://github.com/austinfrey/node-openfaas#readme"
}
51 changes: 31 additions & 20 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const test = require('tape');
const BbPromise = require('bluebird')
const FaaS = require('./faas');
const nock = require('nock');
const BbPromise = require('bluebird');
const FaaS = require('./openfaas');

test('Test typeofs', t => {
t.plan(6);
Expand All @@ -12,37 +13,47 @@ test('Test typeofs', t => {
const faas = FaaS('http://localhost:8080');

t.equals(typeof faas, 'object');
t.equals(typeof faas.deploy, 'function')
t.equals(typeof faas.invoke, 'function')
t.equals(typeof faas.compose, 'function')
t.equals(typeof faas.remove, 'function')
})
t.equals(typeof faas.deploy, 'function');
t.equals(typeof faas.invoke, 'function');
t.equals(typeof faas.compose, 'function');
t.equals(typeof faas.remove, 'function');
});

test('Test full API', t => {
t.plan(4)
const faas = FaaS('http://localhost:8080')

const delay = (time) => {
return new BbPromise(resolve => {
setTimeout(resolve, time)
})
}
nock('http://localhost:8080')
.post('/system/functions', {
service: 'test-func',
network: 'func_functions',
image: 'hello-serverless'
}).reply(200)
.post('/function/test-func').reply(200, {status: 'done'})
.post('/function/func_nodeinfo').reply(200, 'hello cruel world')
.post('/function/func_echoit', 'hello cruel world').reply(200, 'hello cruel world')
.post('/function/func_wordcount', 'hello cruel world').reply(200, 3)
.delete('/system/functions', {functionName: 'test-func'}).reply(200);

t.plan(5);
const faas = FaaS('http://localhost:8080');

faas.deploy(
'test-func',
'func_functions',
'hello-serverless'
)
.then(x => t.equals(x.statusCode, 200))
.then(() => delay(5000).then(() => faas.invoke('test-func')))
.then(() => faas.invoke('test-func',null, true))
.then(x => t.same(x.body, {status: 'done'}))
.then(() => faas.compose('', [
'func_nodeinfo',
'func_echoit',
'func_wordcount'
]
'func_nodeinfo',
'func_echoit',
'func_wordcount'
]
))
.then(x => t.equals(x.statusCode, 200))
.then(x => {
t.equals(x.statusCode, 200)
t.equals(x.body, '3')
})
.then(() => faas.remove('test-func'))
.then(x => t.equals(x.statusCode, 200))
.catch(err => console.log(err));
Expand Down

0 comments on commit fa28bae

Please sign in to comment.