Skip to content

Commit ca73987

Browse files
committed
properly handle .toJSON in output
1 parent 56c849e commit ca73987

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/gateway.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,21 +1962,36 @@ class Gateway extends EventEmitter {
19621962
}
19631963
}
19641964

1965+
jsonify (obj) {
1966+
if (
1967+
obj !== null &&
1968+
typeof obj === 'object' &&
1969+
!Buffer.isBuffer(obj)
1970+
) {
1971+
if (typeof obj.toJSON === 'function') {
1972+
obj = obj.toJSON();
1973+
}
1974+
if (Array.isArray(obj)) {
1975+
obj = obj.map(item => this.jsonify(item));
1976+
} else if (
1977+
obj !== null &&
1978+
typeof obj === 'object' &&
1979+
!Buffer.isBuffer(obj)
1980+
) {
1981+
Object.keys(obj).forEach(key => obj[key] = this.jsonify(obj[key]));
1982+
}
1983+
}
1984+
return obj;
1985+
}
1986+
19651987
execute (definition, functionArgs, data, headers, callback) {
19661988
headers = headers || {};
19671989
let fn;
19681990
let complete = false;
19691991
let callbackWrapper = (err, result, headers, executionUuid) => {
19701992
if (!complete) {
19711993
complete = true;
1972-
if (
1973-
result !== null &&
1974-
typeof result === 'object' &&
1975-
!Buffer.isBuffer(result) &&
1976-
typeof result.toJSON === 'function'
1977-
) {
1978-
result = JSON.parse(JSON.stringify(result.toJSON()));
1979-
}
1994+
result = this.jsonify(result);
19801995
callback(err, result, headers, executionUuid);
19811996
}
19821997
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "functionscript",
3-
"version": "2.10.5",
3+
"version": "2.10.6",
44
"description": "An API gateway and framework for turning functions into web services",
55
"author": "Keith Horwood <keithwhor@gmail.com>",
66
"main": "index.js",

0 commit comments

Comments
 (0)