Skip to content

Commit

Permalink
Gateway: api.stdlib.com, remove support for calling functions without…
Browse files Browse the repository at this point in the history
… named parameters
  • Loading branch information
keithwhor committed Feb 5, 2019
1 parent b831719 commit 3886bde
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/local.js
Expand Up @@ -3,7 +3,7 @@
const path = require('path');
const fs = require('fs');

const FUNCTION_PATH = fs.existsSync('f') ? 'f' : 'functions';
const FUNCTION_PATH = 'functions';

module.exports = (cfg, names, params, callback) => {

Expand Down
27 changes: 13 additions & 14 deletions lib/parse.js
Expand Up @@ -3,25 +3,18 @@ function formatBuffer(b) {
}

function containsKeywords(params) {
return params.length &&
typeof params[0] === 'object' &&
return typeof params[0] === 'object' &&
!Array.isArray(params[0]) &&
!Buffer.isBuffer(params[0]);
}

function formatParams(params) {
if (containsKeywords(params)) {
if (params.length > 1) {
throw new Error('Can not send additional arguments with parameters as keywords');
} else {
return Object.keys(params[0]).reduce((body, name) => {
body[name] = formatBuffer(body[name]);
return body;
}, params[0]);
}
} else {
return params.map(formatBuffer);
}
var src = params[0] || {};
var dst = {};
return Object.keys(params[0] || {}).reduce((dst, name) => {
dst[name] = formatBuffer(src[name]);
return dst;
}, dst);
}

module.exports = (names, params) => {
Expand All @@ -32,6 +25,12 @@ module.exports = (names, params) => {
callback = params.pop();
}

if (params.length > 1) {
throw new Error('No more than one optional argument containing an object of key-value pairs expected.');
} else if (params.length && !containsKeywords(params)) {
throw new Error('Argument must be an object of key-value pairs that act as function parameters.');
}

return {
params: formatParams(params),
callback: callback
Expand Down
11 changes: 8 additions & 3 deletions lib/remote.js
@@ -1,7 +1,7 @@
const https = require('https');
const http = require('http');

const HOST = 'functions.lib.id';
const HOST = 'api.stdlib.com';
const PORT = 443;
const PATH = '/';

Expand All @@ -25,13 +25,18 @@ module.exports = (cfg, names, params, callback) => {
cfg.webhook = cfg.webhook || null; // TODO: Deprecate
cfg.convert = !!cfg.convert;

let pathname;

if (names[2] === `@${LOCALENV}`) {
cfg.host = 'localhost';
cfg.port = LOCALPORT;
names[2] = '';
pathname = names.slice(0, 2).join('/') + names.slice(2).join('/');
} else {
cfg.host = names.slice(0, 1).concat(cfg.host).join('.');
pathname = names.slice(1, 2).join('/') + names.slice(2).join('/');
}

let pathname = names.slice(0, 2).join('/') + names.slice(2).join('/');
pathname = pathname + '/';
let headers = {};
let body;
Expand Down Expand Up @@ -80,7 +85,7 @@ module.exports = (cfg, names, params, callback) => {

if (((res.statusCode / 100) | 0) !== 2) {
let message = typeof response === 'object' ?
(response && response.error && response.error.message) || ('Unspecified error running remote StdLib function "' + names.join('.') + '"') :
(response && response.error && response.error.message) || ('Unspecified error running remote Standard Library function "' + names.join('.') + '"') :
response;
return callback(new Error(message), response);
} else {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
@@ -1,10 +1,10 @@
{
"name": "lib",
"version": "3.0.2",
"version": "4.0.0",
"description": "StdLib: Standard Library for Microservices Node.js Bindings",
"main": "lib.js",
"engines": {
"node" : ">=4.0.0"
"node": ">=4.0.0"
},
"keywords": [
"lib",
Expand Down

0 comments on commit 3886bde

Please sign in to comment.