|
| 1 | +import { Button } from "@controlkit/ui"; |
| 2 | +import useRequestStore from "@src/stores/request_store"; |
| 3 | +import type { T_Header } from "@src/stores/request_store/request_slice"; |
| 4 | +import { invoke } from "@tauri-apps/api/core"; |
| 5 | + |
| 6 | +export default function SendRequestBtn() { |
| 7 | + // const sendRequest = useRequestStore((state) => state.sendRequest); |
| 8 | + |
| 9 | + const getHTTPRequest = useRequestStore((state) => state.getHTTPRequest); |
| 10 | + const getEnabledEnvironmentAndDetails = useRequestStore( |
| 11 | + (state) => state.getEnabledEnvironmentAndDetails, |
| 12 | + ); |
| 13 | + |
| 14 | + const setLoading = useRequestStore((state) => state.setLoading); |
| 15 | + const setResponse = useRequestStore((state) => state.setResponse); |
| 16 | + const setResponseHeaders = useRequestStore((state) => state.setResponseHeaders); |
| 17 | + const setResponseCookies = useRequestStore((state) => state.setResponseCookies); |
| 18 | + |
| 19 | + function ReplaceManagedVariable(env: any, sub_target: any): string { |
| 20 | + console.log(env.variables); |
| 21 | + |
| 22 | + let s = sub_target; |
| 23 | + for(const variable of env.variables) { |
| 24 | + console.log("VAR - ", s, variable.key, variable.initial_value, variable.current_value); |
| 25 | + |
| 26 | + if(variable.current_value !== "") { |
| 27 | + if(variable.current_value === "NULL") { |
| 28 | + s = s.replace(`{{${variable.key}}}`, "null"); |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + s = s.replace(`{{${variable.key}}}`, variable.current_value); |
| 33 | + } |
| 34 | + |
| 35 | + if(variable.initial_value !== "") { |
| 36 | + if(variable.initial_value === "NULL") { |
| 37 | + s = s.replace(`{{${variable.key}}}`, "null"); |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + s = s.replace(`{{${variable.key}}}`, variable.initial_value); |
| 42 | + } |
| 43 | + |
| 44 | + // console.log(s.replace(`{{${variable.key}}}`, variable.value)); |
| 45 | + // s = s.replace(`{{${variable.key}}}`, variable.value); |
| 46 | + } |
| 47 | + |
| 48 | + return s; |
| 49 | + } |
| 50 | + |
| 51 | + function AttemptToSendHTTPRequest() { |
| 52 | + setLoading(true); |
| 53 | + |
| 54 | + const { url, method, autoHeaders, headers, body, cookies } = |
| 55 | + getHTTPRequest(); |
| 56 | + |
| 57 | + // const method = get().method; |
| 58 | + // const s = get(); |
| 59 | + // const { url, body, cookies, autoHeaders, headers } = get(); |
| 60 | + |
| 61 | + // set({ loading: true, error: null }); |
| 62 | + |
| 63 | + console.group("AttemptToSendHTTPRequest"); |
| 64 | + console.log("METHOD - ", method); |
| 65 | + console.log("URL - ", url); |
| 66 | + console.log("AUTO HEADERS - ", autoHeaders); |
| 67 | + console.log("HEADERS - ", headers); |
| 68 | + console.log("BODY - ", body); |
| 69 | + console.log("COOKIES - ", cookies); |
| 70 | + |
| 71 | + // TODO: verify data before invoking |
| 72 | + // console.log(url); |
| 73 | + const trimmedUrl = url.trim(); |
| 74 | + if (trimmedUrl.length === 0) { |
| 75 | + // TODO: error state ui |
| 76 | + // set({ error: "URL cannot be empty", loading: false }); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const env = getEnabledEnvironmentAndDetails(); |
| 81 | + |
| 82 | + console.log("ENV - ", env); |
| 83 | + |
| 84 | + // url |
| 85 | + // body |
| 86 | + // headers |
| 87 | + |
| 88 | + // get().environments(); |
| 89 | + |
| 90 | + // console.log(trimmedUrl); |
| 91 | + |
| 92 | + console.log("SUBBED"); |
| 93 | + |
| 94 | + let subbed_url = trimmedUrl; |
| 95 | + let subbed_body = body; |
| 96 | + let subbed_headers = structuredClone(headers); |
| 97 | + |
| 98 | + subbed_url = ReplaceManagedVariable(env, subbed_url); |
| 99 | + subbed_body = ReplaceManagedVariable(env, subbed_body); |
| 100 | + for(let idx = 0; idx < subbed_headers.length; ++idx) { |
| 101 | + subbed_headers[idx].value = ReplaceManagedVariable(env, subbed_headers[idx].value); |
| 102 | + } |
| 103 | + |
| 104 | + const filtered_headers = subbed_headers |
| 105 | + .filter((h: T_Header) => h.enabled) |
| 106 | + .map((h: T_Header) => `${h.key}: ${h.value}`) |
| 107 | + .join(", "); |
| 108 | + |
| 109 | + console.log("METHOD - ", method); |
| 110 | + console.log("URL - ", subbed_url); |
| 111 | + console.log("AUTO HEADERS - ", autoHeaders); |
| 112 | + console.log("HEADERS - ", subbed_headers); |
| 113 | + console.log("FILTERED HEADERS - ", filtered_headers); |
| 114 | + console.log("BODY - ", subbed_body); |
| 115 | + console.log("COOKIES - ", cookies); |
| 116 | + |
| 117 | + invoke("http_request", { |
| 118 | + method: method.value, |
| 119 | + headers: filtered_headers, |
| 120 | + url: subbed_url, |
| 121 | + body: subbed_body, |
| 122 | + cookies: "", |
| 123 | + }) |
| 124 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 125 | + .then((message: any) => { |
| 126 | + console.log(message); |
| 127 | + |
| 128 | + setResponse(message.response_data_string); |
| 129 | + setResponseHeaders(message.response_headers); |
| 130 | + setResponseCookies(message.response_cookies); |
| 131 | + setLoading(false); |
| 132 | + |
| 133 | + // setResponse(message.response_data_string); |
| 134 | + // set_HTTP_API_Response_Body(message.response_data_string); |
| 135 | + // set_HTTP_API_Response_Headers(message.response_headers); |
| 136 | + }) |
| 137 | + .catch((error_message) => { |
| 138 | + console.error(error_message); |
| 139 | + |
| 140 | + setLoading(false); |
| 141 | + }); |
| 142 | + |
| 143 | + console.groupEnd(); |
| 144 | + } |
| 145 | + |
| 146 | + return ( |
| 147 | + <Button |
| 148 | + onClick={(_) => { |
| 149 | + // sendRequest(); |
| 150 | + AttemptToSendHTTPRequest(); |
| 151 | + }} |
| 152 | + > |
| 153 | + Send |
| 154 | + </Button> |
| 155 | + ); |
| 156 | +} |
0 commit comments