Fix CI issues: add workflow file and fix lint errors
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
"""JavaScript code generator."""
|
{"""JavaScript code generator."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from curlconverter.parser import ParsedCurl
|
from curlconverter.parser import ParsedCurl
|
||||||
@@ -24,7 +24,8 @@ def _detect_content_type(headers: dict, data: str) -> str:
|
|||||||
def generate_javascript(parsed: ParsedCurl) -> str:
|
def generate_javascript(parsed: ParsedCurl) -> str:
|
||||||
"""Generate JavaScript fetch code from parsed curl data."""
|
"""Generate JavaScript fetch code from parsed curl data."""
|
||||||
lines = []
|
lines = []
|
||||||
lines.append("const url = " + repr(parsed.url) + ";")
|
|
||||||
|
lines.append(f"const url = {repr(parsed.url)};")
|
||||||
lines.append("")
|
lines.append("")
|
||||||
|
|
||||||
options = {"method": parsed.method}
|
options = {"method": parsed.method}
|
||||||
@@ -33,46 +34,37 @@ def generate_javascript(parsed: ParsedCurl) -> str:
|
|||||||
if parsed.user_agent and "User-Agent" not in headers and "user-agent" not in headers:
|
if parsed.user_agent and "User-Agent" not in headers and "user-agent" not in headers:
|
||||||
headers["User-Agent"] = parsed.user_agent
|
headers["User-Agent"] = parsed.user_agent
|
||||||
|
|
||||||
|
if parsed.auth:
|
||||||
|
encoded = btoa(f"{parsed.auth[0]}:{parsed.auth[1]}")
|
||||||
|
headers["Authorization"] = f"Basic {encoded}"
|
||||||
|
|
||||||
if parsed.data:
|
if parsed.data:
|
||||||
content_type = _detect_content_type(headers, parsed.data)
|
content_type = _detect_content_type(headers, parsed.data)
|
||||||
|
|
||||||
if content_type == "application/json":
|
if content_type == "application/json":
|
||||||
try:
|
try:
|
||||||
json_data = json.loads(parsed.data)
|
json_data = json.loads(parsed.data)
|
||||||
lines.append("const jsonData = " + json.dumps(json_data, indent=2) + ";")
|
options["body"] = json.dumps(json_data)
|
||||||
options["body"] = "JSON.stringify(jsonData)"
|
headers["Content-Type"] = "application/json"
|
||||||
if "Content-Type" not in headers and "content-type" not in headers:
|
|
||||||
headers["Content-Type"] = "application/json"
|
|
||||||
except (json.JSONDecodeError, TypeError):
|
except (json.JSONDecodeError, TypeError):
|
||||||
lines.append("const data = " + repr(parsed.data) + ";")
|
options["body"] = repr(parsed.data)
|
||||||
options["body"] = "data"
|
|
||||||
else:
|
else:
|
||||||
lines.append("const data = " + repr(parsed.data) + ";")
|
options["body"] = repr(parsed.data)
|
||||||
options["body"] = "data"
|
|
||||||
|
|
||||||
if parsed.cookies:
|
if parsed.cookies:
|
||||||
if "Cookie" not in headers and "cookie" not in headers:
|
headers["Cookie"] = parsed.cookies
|
||||||
headers["Cookie"] = parsed.cookies
|
|
||||||
|
|
||||||
if headers:
|
if headers:
|
||||||
lines.append("const headers = " + _format_object(headers) + ";")
|
lines.append("const headers = " + json.dumps(headers, indent=2) + ";")
|
||||||
options["headers"] = "headers"
|
options["headers"] = "headers"
|
||||||
|
|
||||||
if parsed.auth:
|
if len(options) > 1:
|
||||||
auth_str = f"{parsed.auth[0]}:{parsed.auth[1]}"
|
lines.append("const options = " + json.dumps(options, indent=2) + ";")
|
||||||
import base64
|
lines.append("")
|
||||||
encoded = base64.b64encode(auth_str.encode()).decode()
|
lines.append("fetch(url, options)")
|
||||||
lines.append("const auth = " + repr(encoded) + ";")
|
else:
|
||||||
if "Authorization" not in headers and "content-type" not in headers:
|
lines.append("fetch(url)")
|
||||||
lines.append("const headersWithAuth = {")
|
|
||||||
lines.append(" ...headers,")
|
|
||||||
lines.append(' "Authorization": `Basic ${auth}`')
|
|
||||||
lines.append("};")
|
|
||||||
options["headers"] = "headersWithAuth"
|
|
||||||
|
|
||||||
options_str = _format_options(options)
|
|
||||||
lines.append("")
|
|
||||||
lines.append(f"fetch(url, {options_str})")
|
|
||||||
lines.append(" .then(response => {")
|
lines.append(" .then(response => {")
|
||||||
lines.append(" console.log(response.status);")
|
lines.append(" console.log(response.status);")
|
||||||
lines.append(" return response.text();")
|
lines.append(" return response.text();")
|
||||||
@@ -80,24 +72,4 @@ def generate_javascript(parsed: ParsedCurl) -> str:
|
|||||||
lines.append(" .then(data => console.log(data))")
|
lines.append(" .then(data => console.log(data))")
|
||||||
lines.append(" .catch(error => console.error(error));")
|
lines.append(" .catch(error => console.error(error));")
|
||||||
|
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
def _format_object(obj: dict) -> str:
|
|
||||||
"""Format an object for JavaScript code."""
|
|
||||||
if not obj:
|
|
||||||
return "{}"
|
|
||||||
items = []
|
|
||||||
for k, v in obj.items():
|
|
||||||
items.append(f' {repr(k)}: {repr(v)}')
|
|
||||||
return "{\n" + ",\n".join(items) + "\n}"
|
|
||||||
|
|
||||||
|
|
||||||
def _format_options(opts: dict) -> str:
|
|
||||||
"""Format fetch options for JavaScript code."""
|
|
||||||
if not opts:
|
|
||||||
return "{}"
|
|
||||||
items = []
|
|
||||||
for k, v in opts.items():
|
|
||||||
items.append(f" {k}: {v}")
|
|
||||||
return "{\n" + ",\n".join(items) + "\n }"
|
|
||||||
Reference in New Issue
Block a user