This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
forked from austinfrey/node-openfaas
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor into ES6 class, add more tests and update docs
Signed-off-by: Finnian Anderson <[email protected]>
- Loading branch information
1 parent
fa28bae
commit bd3190f
Showing
9 changed files
with
421 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,96 @@ | ||
'use strict'; | ||
const path = require('path') | ||
const got = require('got') | ||
|
||
const deploy = require('./deploy'); | ||
const remove = require('./remove'); | ||
const invoke = require('./invoke'); | ||
const compose = require('./compose'); | ||
class OpenFaaS { | ||
constructor(gateway, options) { | ||
if (options) { | ||
// they passed two arguments (url, options) | ||
this.gateway = gateway | ||
} else if (typeof gateway === 'string') { | ||
// they passed a single string | ||
options = {} | ||
this.gateway = gateway | ||
} else { | ||
// they passed a single object | ||
options = gateway | ||
this.gateway = options.gateway | ||
} | ||
|
||
const OpenFaaS = url => ({ | ||
deploy: deploy(url), | ||
remove: remove(url), | ||
invoke: invoke(url), | ||
compose: compose(url) | ||
}); | ||
if (!this.gateway) throw new Error('Missing option `gateway`') | ||
|
||
module.exports = OpenFaaS; | ||
if (options.user) { | ||
if (!options.pass) throw new Error('Missing option `pass`') | ||
options.auth = `${options.user}:${options.pass}` | ||
} | ||
|
||
// save options for got | ||
this.gotOptions = options | ||
} | ||
|
||
invoke(fn, data, options) { | ||
options = options || {} | ||
|
||
// merge our defaults and their passed options | ||
const gotOptions = { ...this.gotOptions, ...options, method: 'POST' } | ||
|
||
gotOptions.encoding = options.isBinaryResponse ? null : 'utf8' | ||
|
||
if (data) gotOptions.body = data | ||
|
||
return got(this.buildFunctionPath(fn), gotOptions) | ||
} | ||
|
||
deploy({ name, network, image }, options) { | ||
const gotOptions = { | ||
...this.gotOptions, | ||
...(options || {}), | ||
body: { | ||
service: name, | ||
network: network || 'func_functions', | ||
image | ||
}, | ||
json: true | ||
} | ||
|
||
return got.post(this.gateway + '/system/functions', gotOptions) | ||
} | ||
|
||
list(options) { | ||
const gotOptions = { ...this.gotOptions, ...(options || {}), json: true } | ||
|
||
return got.get(this.gateway + '/system/functions', gotOptions) | ||
} | ||
|
||
compose(data, functions, options) { | ||
// no initial data | ||
if (!functions) { | ||
functions = data | ||
data = undefined | ||
} | ||
|
||
// build an array of functions to be called with result from previous function | ||
// [ function1(data), function2(data), ... ] | ||
const calls = functions.map(f => data => this.invoke(f, data, options || {})) | ||
|
||
return calls.reduce((chain, current) => { | ||
return chain.then(res => current(res.body)) | ||
}, Promise.resolve({ body: data })) | ||
} | ||
|
||
remove(fn, options) { | ||
const gotOptions = { | ||
...this.gotOptions, | ||
...(options || {}), | ||
json: true, | ||
body: { functionName: fn } | ||
} | ||
|
||
return got.delete(this.gateway + '/system/functions', gotOptions) | ||
} | ||
|
||
buildFunctionPath(fn) { | ||
return this.gateway + path.join('/function', fn) | ||
} | ||
} | ||
|
||
module.exports = OpenFaaS |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.