@@ -17,51 +17,76 @@ Server::Server(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> invoker) {
1717 const std::string path = args[1 ].asString (rt).utf8 (rt);
1818 auto callback = std::make_shared<jsi::Value>(rt, args[2 ]);
1919
20- if (method == " GET" ) {
21- server.Get (
22- path, [this , &rt, invoker, callback](const httplib::Request &req,
23- httplib::Response &res) {
24- auto responseDone = std::make_shared<std::promise<std::string>>();
25- auto responseFuture = responseDone->get_future ();
26-
27- invoker->invokeAsync ([callback, responseDone](jsi::Runtime &rt) {
28- try {
29- auto promise = callback->asObject (rt).asFunction (rt).call (rt);
30-
31- if (promise.isObject ()) {
32- auto promiseObj = promise.asObject (rt);
33- auto then = promiseObj.getPropertyAsFunction (rt, " then" );
34-
35- // Create success handler
36- auto successHandler = HFN (responseDone) {
37- if (count > 0 && args[0 ].isString ()) {
38- responseDone->set_value (args[0 ].asString (rt).utf8 (rt));
39- } else {
40- responseDone->set_value (" Success!" );
41- }
42- return jsi::Value::undefined ();
43- });
44-
45- then.callWithThis (rt, promiseObj, successHandler);
20+ // Abstracted request handler
21+ auto handleRequest = [this , &rt, invoker,
22+ callback](const httplib::Request &req,
23+ httplib::Response &res) {
24+ auto responseDone = std::make_shared<std::promise<std::string>>();
25+ auto responseFuture = responseDone->get_future ();
26+
27+ invoker->invokeAsync ([callback, responseDone](jsi::Runtime &rt) {
28+ try {
29+ auto promise = callback->asObject (rt).asFunction (rt).call (rt);
30+
31+ auto promiseObj = promise.asObject (rt);
32+ auto then_fn = promiseObj.getPropertyAsFunction (rt, " then" );
33+ auto catch_fn = promiseObj.getPropertyAsFunction (rt, " catch" );
34+
35+ // Create success handler
36+ auto success_handler = HFN (responseDone) {
37+ if (count > 0 && args[0 ].isString ()) {
38+ responseDone->set_value (args[0 ].asString (rt).utf8 (rt));
39+ } else {
40+ responseDone->set_value (" Success!" );
41+ }
42+ return jsi::Value::undefined ();
43+ });
44+
45+ // reject handler
46+ auto reject_handler = HFN (responseDone) {
47+ std::string errorMsg = " Error: " ;
48+ if (count > 0 ) {
49+ if (args[0 ].isString ()) {
50+ errorMsg += args[0 ].asString (rt).utf8 (rt);
51+ } else if (args[0 ].isObject ()) {
52+ auto errObj = args[0 ].asObject (rt);
53+ if (errObj.hasProperty (rt, " message" )) {
54+ auto msgVal = errObj.getProperty (rt, " message" );
55+ if (msgVal.isString ()) {
56+ errorMsg += msgVal.asString (rt).utf8 (rt);
57+ }
4658 }
47- } catch (const std::exception &e) {
48- responseDone->set_value (std::string (" Error: " ) + e.what ());
4959 }
50- });
51-
52- if (responseFuture.wait_for (std::chrono::seconds (5 )) ==
53- std::future_status::ready) {
54- res.set_content (responseFuture.get (), " text/plain" );
55- } else {
56- res.set_content (" Timeout" , " text/plain" );
5760 }
61+ responseDone->set_value (errorMsg);
62+ return jsi::Value::undefined ();
5863 });
5964
65+ then_fn.callWithThis (rt, promiseObj, success_handler);
66+ catch_fn.callWithThis (rt, promiseObj, reject_handler);
67+
68+ } catch (const std::exception &e) {
69+ responseDone->set_value (std::string (" Error: " ) + e.what ());
70+ }
71+ });
72+
73+ if (responseFuture.wait_for (std::chrono::seconds (5 )) ==
74+ std::future_status::ready) {
75+ res.set_content (responseFuture.get (), " text/plain" );
76+ } else {
77+ res.set_content (" Timeout" , " text/plain" );
78+ }
79+ };
80+
81+ // Register the appropriate HTTP method
82+ if (method == " GET" ) {
83+ server.Get (path, handleRequest);
6084 } else if (method == " POST" ) {
61- server.Post (path,
62- [callback](const httplib::Request &, httplib::Response &res) {
63- res.set_content (" Hello World!" , " text/plain" );
64- });
85+ server.Post (path, handleRequest);
86+ } else if (method == " PUT" ) {
87+ server.Put (path, handleRequest);
88+ } else if (method == " DELETE" ) {
89+ server.Delete (path, handleRequest);
6590 }
6691
6792 return {};
0 commit comments