@@ -3,6 +3,7 @@ import { generateApolloClient } from "@deep-foundation/hasura/client.js";
3
3
import { HasuraApi } from '@deep-foundation/hasura/api.js' ;
4
4
import { DeepClient , parseJwt } from "@deep-foundation/deeplinks/imports/client.js" ;
5
5
import { gql } from '@apollo/client/index.js' ;
6
+ import { serializeError } from 'serialize-error' ;
6
7
import memoize from 'lodash/memoize.js' ;
7
8
import http from 'http' ;
8
9
// import { parseStream, parseFile } from 'music-metadata';
@@ -29,8 +30,6 @@ const requireWrapper = (id: string) => {
29
30
30
31
DeepClient . resolveDependency = requireWrapper ;
31
32
32
- const toJSON = ( data ) => JSON . stringify ( data , Object . getOwnPropertyNames ( data ) , 2 ) ;
33
-
34
33
const makeFunction = ( code : string ) => {
35
34
const fn = memoEval ( code ) ;
36
35
if ( typeof fn !== 'function' )
@@ -63,46 +62,55 @@ const makeDeepClient = (token: string, secret?: string) => {
63
62
return deepClient ;
64
63
}
65
64
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
+
66
74
app . use ( bodyParser . json ( { limit : '50mb' } ) ) ;
67
75
app . use ( bodyParser . urlencoded ( { limit : '50mb' , extended : true } ) ) ;
76
+
68
77
app . get ( '/healthz' , ( req , res ) => {
69
78
res . json ( { } ) ;
70
79
} ) ;
80
+
71
81
app . post ( '/init' , ( req , res ) => {
72
82
res . json ( { } ) ;
73
83
} ) ;
84
+
74
85
app . post ( '/call' , async ( req , res ) => {
75
86
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 ) ;
81
90
console . log ( 'call result' , result ) ;
82
91
res . json ( { resolved : result } ) ;
83
92
}
84
93
catch ( rejected )
85
94
{
86
- const processedRejection = JSON . parse ( toJSON ( rejected ) ) ;
95
+ const processedRejection = serializeError ( rejected ) ;
87
96
console . log ( 'rejected' , processedRejection ) ;
88
97
res . json ( { rejected : processedRejection } ) ;
89
98
}
90
99
} ) ;
91
100
92
101
app . use ( '/http-call' , async ( req , res , next ) => {
93
102
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 ;
100
108
}
101
109
catch ( rejected )
102
110
{
103
- const processedRejection = JSON . parse ( toJSON ( rejected ) ) ;
111
+ const processedRejection = serializeError ( rejected ) ;
104
112
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.
106
114
}
107
115
} ) ;
108
116
0 commit comments