Skip to content

Commit 5b0d412

Browse files
committed
1 parent 93525c0 commit 5b0d412

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

index.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { generateApolloClient } from "@deep-foundation/hasura/client.js";
33
import { HasuraApi } from '@deep-foundation/hasura/api.js';
44
import { DeepClient, parseJwt } from "@deep-foundation/deeplinks/imports/client.js";
55
import { gql } from '@apollo/client/index.js';
6+
import { serializeError } from 'serialize-error';
67
import memoize from 'lodash/memoize.js';
78
import http from 'http';
89
// import { parseStream, parseFile } from 'music-metadata';
@@ -29,8 +30,6 @@ const requireWrapper = (id: string) => {
2930

3031
DeepClient.resolveDependency = requireWrapper;
3132

32-
const toJSON = (data) => JSON.stringify(data, Object.getOwnPropertyNames(data), 2);
33-
3433
const makeFunction = (code: string) => {
3534
const fn = memoEval(code);
3635
if (typeof fn !== 'function')
@@ -63,46 +62,55 @@ const makeDeepClient = (token: string, secret?: string) => {
6362
return deepClient;
6463
}
6564

65+
const execute = async (args, options) => {
66+
const { jwt, secret, code, data } = options;
67+
const fn = makeFunction(code);
68+
const deep = makeDeepClient(jwt, secret);
69+
// await supports both sync and async functions the same way
70+
const result = await fn(...args, { data, deep, gql, require: requireWrapper });
71+
return result;
72+
}
73+
6674
app.use(bodyParser.json({limit: '50mb'}));
6775
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
76+
6877
app.get('/healthz', (req, res) => {
6978
res.json({});
7079
});
80+
7181
app.post('/init', (req, res) => {
7282
res.json({});
7383
});
84+
7485
app.post('/call', async (req, res) => {
7586
try {
76-
console.log('call body params', req?.body?.params);
77-
const { jwt, secret, code, data } = req?.body?.params || {};
78-
const fn = makeFunction(code);
79-
const deep = makeDeepClient(jwt, secret);
80-
const result = await fn({ data, deep, gql, require: requireWrapper }); // Supports both sync and async functions the same way
87+
const options = req?.body?.params || {};
88+
console.log('call options', options);
89+
const result = await execute([], options);
8190
console.log('call result', result);
8291
res.json({ resolved: result });
8392
}
8493
catch(rejected)
8594
{
86-
const processedRejection = JSON.parse(toJSON(rejected));
95+
const processedRejection = serializeError(rejected);
8796
console.log('rejected', processedRejection);
8897
res.json({ rejected: processedRejection });
8998
}
9099
});
91100

92101
app.use('/http-call', async (req, res, next) => {
93102
try {
94-
const options = decodeURI(`${req.headers['deep-call-options']}`) || '{}';
95-
console.log('deep-call-options', options);
96-
const { jwt, secret, code, data } = JSON.parse(options as string);
97-
const fn = makeFunction(code);
98-
const deep = makeDeepClient(jwt, secret);
99-
await fn(req, res, next, { data, deep, gql, require: requireWrapper }); // Supports both sync and async functions the same way
103+
const options = JSON.parse(decodeURI(`${req.headers['deep-call-options']}`) || '{}');
104+
console.log('http call options', options);
105+
const result = await execute([req, res, next], options);
106+
console.log('http call result', result);
107+
return result;
100108
}
101109
catch(rejected)
102110
{
103-
const processedRejection = JSON.parse(toJSON(rejected));
111+
const processedRejection = serializeError(rejected);
104112
console.log('rejected', processedRejection);
105-
res.json({ rejected: processedRejection }); // TODO: Do we need to send json to client?
113+
res.json({ rejected: processedRejection }); // TODO: Do we need to send json to client? HTTP respone may not expect json output.
106114
}
107115
});
108116

package-lock.json

Lines changed: 37 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"react-i18next": "^13.5.0",
8080
"rss-parser": "^3.13.0",
8181
"ru-compromise": "^0.0.2",
82+
"serialize-error": "^11.0.3",
8283
"telegram": "^2.17.10",
8384
"tinkoff-sdk-grpc-js": "^1.8.0-fix",
8485
"ts-node": "^10.9.1",

0 commit comments

Comments
 (0)