From fa28bae0192c9b64838b48fae59af724ee698e83 Mon Sep 17 00:00:00 2001 From: Austin Frey Date: Sat, 23 Sep 2017 11:46:30 -0400 Subject: [PATCH] Updated README. Now uses `openfaas` instead of `faas` Signed-off-by: austinfrey --- README.md | 52 +++++++++++++++++++++++ index.js | 12 +----- {faas => openfaas}/compose.js | 0 {faas => openfaas}/deploy.js | 0 {faas => openfaas}/index.js | 4 +- {faas => openfaas}/invoke.js | 5 ++- {faas => openfaas}/remove.js | 0 package-lock.json | 79 ++++++++++++++++++++++++++++++++++- package.json | 28 +++++++++---- test.js | 51 +++++++++++++--------- 10 files changed, 188 insertions(+), 43 deletions(-) create mode 100644 README.md rename {faas => openfaas}/compose.js (100%) rename {faas => openfaas}/deploy.js (100%) rename {faas => openfaas}/index.js (82%) rename {faas => openfaas}/invoke.js (70%) rename {faas => openfaas}/remove.js (100%) diff --git a/README.md b/README.md new file mode 100644 index 0000000..addf376 --- /dev/null +++ b/README.md @@ -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` diff --git a/index.js b/index.js index 6ccf9df..e2dfb34 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/faas/compose.js b/openfaas/compose.js similarity index 100% rename from faas/compose.js rename to openfaas/compose.js diff --git a/faas/deploy.js b/openfaas/deploy.js similarity index 100% rename from faas/deploy.js rename to openfaas/deploy.js diff --git a/faas/index.js b/openfaas/index.js similarity index 82% rename from faas/index.js rename to openfaas/index.js index e9fc1f6..703eef2 100644 --- a/faas/index.js +++ b/openfaas/index.js @@ -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; diff --git a/faas/invoke.js b/openfaas/invoke.js similarity index 70% rename from faas/invoke.js rename to openfaas/invoke.js index 2d1ca5e..96b058c 100644 --- a/faas/invoke.js +++ b/openfaas/invoke.js @@ -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) { diff --git a/faas/remove.js b/openfaas/remove.js similarity index 100% rename from faas/remove.js rename to openfaas/remove.js diff --git a/package-lock.json b/package-lock.json index fa8f92e..c3d0693 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "open-faas", - "version": "0.0.1", + "name": "openfaas", + "version": "0.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -145,6 +145,12 @@ "integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=", "dev": true }, + "assertion-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.0.2.tgz", + "integrity": "sha1-E8pRXYYgbaC6xm6DTdOX2HWBCUw=", + "dev": true + }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -309,6 +315,17 @@ "integrity": "sha1-Sm+gc5nCa7pH8LJJa00PtAjFVQ0=", "dev": true }, + "chai": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-3.5.0.tgz", + "integrity": "sha1-TQJjewZ/6Vi9v906QOxW/vc3Mkc=", + "dev": true, + "requires": { + "assertion-error": "1.0.2", + "deep-eql": "0.1.3", + "type-detect": "1.0.0" + } + }, "chalk": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", @@ -511,6 +528,23 @@ "is-obj": "1.0.1" } }, + "deep-eql": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz", + "integrity": "sha1-71WKyrjeJSBs1xOQbXTlaTDrafI=", + "dev": true, + "requires": { + "type-detect": "0.1.1" + }, + "dependencies": { + "type-detect": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-0.1.1.tgz", + "integrity": "sha1-C6XsKohWQORw6k6FBZcZANrFiCI=", + "dev": true + } + } + }, "deep-equal": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", @@ -1614,6 +1648,12 @@ "jsonify": "0.0.0" } }, + "json-stringify-safe": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", + "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=", + "dev": true + }, "jsonify": { "version": "0.0.0", "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", @@ -1949,6 +1989,23 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, + "nock": { + "version": "9.0.20", + "resolved": "https://registry.npmjs.org/nock/-/nock-9.0.20.tgz", + "integrity": "sha512-nlId7xk1zwZYOcwcIR3djl3jMT4HbnFA1Pr4fktVz1iYI2SbyRkAgMN2+vZz0QhMhpXpFjMfCBI5Lpm2jdIqRg==", + "dev": true, + "requires": { + "chai": "3.5.0", + "debug": "2.6.9", + "deep-equal": "1.0.1", + "json-stringify-safe": "5.0.1", + "lodash": "4.17.4", + "mkdirp": "0.5.1", + "propagate": "0.4.0", + "qs": "6.5.1", + "semver": "5.4.1" + } + }, "normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", @@ -2257,6 +2314,12 @@ "integrity": "sha1-4mDHj2Fhzdmw5WzD4Khd4Xx6V74=", "dev": true }, + "propagate": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/propagate/-/propagate-0.4.0.tgz", + "integrity": "sha1-8/zKCm/gZzanulcpZgaWF8EwtIE=", + "dev": true + }, "proto-props": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/proto-props/-/proto-props-0.2.1.tgz", @@ -2269,6 +2332,12 @@ "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", "dev": true }, + "qs": { + "version": "6.5.1", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.1.tgz", + "integrity": "sha512-eRzhrN1WSINYCDCbrz796z37LOe3m5tmW7RQf6oBntukAG1nmovJvhnwHHRMAfeoItc1m2Hk02WER2aQ/iqs+A==", + "dev": true + }, "rc": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.1.tgz", @@ -2762,6 +2831,12 @@ "prelude-ls": "1.1.2" } }, + "type-detect": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-1.0.0.tgz", + "integrity": "sha1-diIXzAbbJY7EiQihKY6LlRIejqI=", + "dev": true + }, "typedarray": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", diff --git a/package.json b/package.json index cf52f73..1957f05 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/test.js b/test.js index 7e5a027..e3e89db 100644 --- a/test.js +++ b/test.js @@ -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); @@ -12,21 +13,28 @@ 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', @@ -34,15 +42,18 @@ test('Test full API', t => { '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));