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
|
||||
from curlconverter.parser import ParsedCurl
|
||||
@@ -24,7 +24,8 @@ def _detect_content_type(headers: dict, data: str) -> str:
|
||||
def generate_javascript(parsed: ParsedCurl) -> str:
|
||||
"""Generate JavaScript fetch code from parsed curl data."""
|
||||
lines = []
|
||||
lines.append("const url = " + repr(parsed.url) + ";")
|
||||
|
||||
lines.append(f"const url = {repr(parsed.url)};")
|
||||
lines.append("")
|
||||
|
||||
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:
|
||||
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:
|
||||
content_type = _detect_content_type(headers, parsed.data)
|
||||
|
||||
if content_type == "application/json":
|
||||
try:
|
||||
json_data = json.loads(parsed.data)
|
||||
lines.append("const jsonData = " + json.dumps(json_data, indent=2) + ";")
|
||||
options["body"] = "JSON.stringify(jsonData)"
|
||||
if "Content-Type" not in headers and "content-type" not in headers:
|
||||
headers["Content-Type"] = "application/json"
|
||||
options["body"] = json.dumps(json_data)
|
||||
headers["Content-Type"] = "application/json"
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
lines.append("const data = " + repr(parsed.data) + ";")
|
||||
options["body"] = "data"
|
||||
options["body"] = repr(parsed.data)
|
||||
else:
|
||||
lines.append("const data = " + repr(parsed.data) + ";")
|
||||
options["body"] = "data"
|
||||
options["body"] = repr(parsed.data)
|
||||
|
||||
if parsed.cookies:
|
||||
if "Cookie" not in headers and "cookie" not in headers:
|
||||
headers["Cookie"] = parsed.cookies
|
||||
headers["Cookie"] = parsed.cookies
|
||||
|
||||
if headers:
|
||||
lines.append("const headers = " + _format_object(headers) + ";")
|
||||
lines.append("const headers = " + json.dumps(headers, indent=2) + ";")
|
||||
options["headers"] = "headers"
|
||||
|
||||
if parsed.auth:
|
||||
auth_str = f"{parsed.auth[0]}:{parsed.auth[1]}"
|
||||
import base64
|
||||
encoded = base64.b64encode(auth_str.encode()).decode()
|
||||
lines.append("const auth = " + repr(encoded) + ";")
|
||||
if "Authorization" not in headers and "content-type" not in headers:
|
||||
lines.append("const headersWithAuth = {")
|
||||
lines.append(" ...headers,")
|
||||
lines.append(' "Authorization": `Basic ${auth}`')
|
||||
lines.append("};")
|
||||
options["headers"] = "headersWithAuth"
|
||||
if len(options) > 1:
|
||||
lines.append("const options = " + json.dumps(options, indent=2) + ";")
|
||||
lines.append("")
|
||||
lines.append("fetch(url, options)")
|
||||
else:
|
||||
lines.append("fetch(url)")
|
||||
|
||||
options_str = _format_options(options)
|
||||
lines.append("")
|
||||
lines.append(f"fetch(url, {options_str})")
|
||||
lines.append(" .then(response => {")
|
||||
lines.append(" console.log(response.status);")
|
||||
lines.append(" return response.text();")
|
||||
@@ -80,24 +72,4 @@ def generate_javascript(parsed: ParsedCurl) -> str:
|
||||
lines.append(" .then(data => console.log(data))")
|
||||
lines.append(" .catch(error => console.error(error));")
|
||||
|
||||
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 }"
|
||||
return "\n".join(lines)
|
||||
Reference in New Issue
Block a user