diff --git a/main.go b/main.go index af0efbcb69289482212e8fffe26c674d9a2f1c2a..6d931629ef40a58c49882b40be4f203a03e4dd4f 100644 --- a/main.go +++ b/main.go @@ -38,9 +38,17 @@ func buildPenpalMethods() string { prefixes := []string{} endPoints := server.GetEndPoints(prefixes) - result := - "import Penpal from 'penpal';\n\n" + - "export default {\n" + result := ` +const encodeResponse = (code, data, status) => { + return { + code, + data, + status + }; +}; + +export default { +` methods := generatePenpalRemoteMethods(endPoints) @@ -590,31 +598,13 @@ func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string { privateCheckSnippet := ` const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (authenticationPublicKey === null) { - result({ - "data" : "", - "code" : "400", - "status" : "Identity not authenticated" - }); - } - - if (loadedIdentities[authenticationPublicKey] === null) { - result({ - "data" : "", - "code" : "400", - "status" : "Identity not authenticated" - }); - } - - const success = extendPinCodeTtl(authenticationPublicKey); - - if(success === false) { - result({"data" : "", - "code" : "400", - "status" : "Identity not authenticated" - }) - } - + if ( + !authenticationPublicKey || + !window.loadedIdentities[authenticationPublicKey] || + !extendPinCodeTtl(authenticationPublicKey) + ) { + return encodeResponse("400", "", "Identity not authenticated"); + } ` for i := 0; i < keysLen; i++ { @@ -626,24 +616,14 @@ func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string { if url == "/identity/getIdentityProfileData" { privateCheckSnippet = ` - const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - - if (authenticationPublicKey === null) { - result({ - "data" : "", - "code" : "400", - "status" : "Identity not authenticated" - }); - } - - if (loadedIdentities[authenticationPublicKey] === null) { - result({ - "data" : "", - "code" : "400", - "status" : "Identity not authenticated" - }); - } - + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + + if ( + !authenticationPublicKey || + !window.loadedIdentities[authenticationPublicKey] + ) { + return encodeResponse("400", "", "Identity not authenticated"); + } ` } @@ -685,12 +665,9 @@ func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string { lastComma = "" } - method := packageStr + strings.Title(methodStr) + ": function(" + args + ") {\n" + - " return new Penpal.Promise(function(result) {\n" + snippet + - " executeRestfulFunction(\"" + endPoints[url].HandlerType + "\", viamApi, viamApi." + packageStr + strings.Title(methodStr) + ", null" + lastComma + args + ").then(function(executeResult) {\n" + - " result(executeResult);\n" + - " });\n" + - " });\n" + + method := packageStr + strings.Title(methodStr) + ": async function(" + args + ") {\n" + + snippet + + " return await executeRestfulFunction(\"" + endPoints[url].HandlerType + "\", viamApi, viamApi." + packageStr + strings.Title(methodStr) + ", null" + lastComma + args + ");\n" + "}" methods += method @@ -713,12 +690,9 @@ func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string { snippet = privateCheckSnippet } - method := packageStr + strings.Title(methodStr) + ": function() {\n" + - " return new Penpal.Promise(function(result) {\n" + snippet + - " executeRestfulFunction(\"" + endPoints[url].HandlerType + "\", viamApi, viamApi." + packageStr + strings.Title(methodStr) + ", null).then(function(executeResult) {\n" + - " result(executeResult);\n" + - " });\n" + - " });\n" + + method := packageStr + strings.Title(methodStr) + ": async function() {\n" + + snippet + + " return await executeRestfulFunction(\"" + endPoints[url].HandlerType + "\", viamApi, viamApi." + packageStr + strings.Title(methodStr) + ", null);\n" + "}" methods += method