Skip to content

Commit

Permalink
🐛 Fixed wrong type on query by adding query_str function
Browse files Browse the repository at this point in the history
  • Loading branch information
nwrenger committed Aug 2, 2024
1 parent 9e100a2 commit 3361286
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where

/// Generate frontend TypeScript API client from the API routes.
pub fn generate_client<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), String> {
let fetch_api_function = r#" async function fetchApi(endpoint: string, options: RequestInit): Promise<any> {
let fetch_api_function = r#" async function fetch_api(endpoint: string, options: RequestInit): Promise<any> {
const response = await fetch(endpoint, {
headers: {
"Content-Type": "application/json",
Expand All @@ -75,6 +75,18 @@ where
});
return response.json();
}
function query_str(params: Record<string, any>): string {
if (params) {
let data: Record<string, string> = {};
for (let key in params) {
if (params[key] != null) data[key] = params[key].toString();
}
// the URLSearchParams escapes any problematic values
return '?' + new URLSearchParams(data).toString();
}
return '';
}
"#;
let namespace_start = "namespace api {\n";
let namespace_end = "}\n\nexport default api;";
Expand Down Expand Up @@ -253,13 +265,12 @@ fn generate_ts_function(
url += "?";
url += "${new URLSearchParams(queryMap).toString()}";
} else if params_str.contains("query") {
url += "?";
url += "${encodeURIComponent(query)}";
url += "${query_str(query)}";
}

format!(
r#" export async function {fn_name}({params_str}): Promise<{response_type}> {{
return fetchApi(`{url}`, {{
return fetch_api(`{url}`, {{
method: "{method}", {body_assignment}
}});
}}
Expand Down
18 changes: 15 additions & 3 deletions tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace api {
huh: T;
}

async function fetchApi(endpoint: string, options: RequestInit): Promise<any> {
async function fetch_api(endpoint: string, options: RequestInit): Promise<any> {
const response = await fetch(endpoint, {
headers: {
"Content-Type": "application/json",
Expand All @@ -27,15 +27,27 @@ namespace api {
return response.json();
}

function query_str(params: Record<string, any>): string {
if (params) {
let data: Record<string, string> = {};
for (let key in params) {
if (params[key] != null) data[key] = params[key].toString();
}
// the URLSearchParams escapes any problematic values
return '?' + new URLSearchParams(data).toString();
}
return '';
}

export async function add_root(path: number, data: Hello<Hello<Huh<Age>, string>, string>): Promise<string> {
return fetchApi(`/${encodeURIComponent(path)}`, {
return fetch_api(`/${encodeURIComponent(path)}`, {
method: "POST",
body: JSON.stringify(data)
});
}

export async function fetch_root(queryMap: Record<string, string>, path: number): Promise<string> {
return fetchApi(`/${encodeURIComponent(path)}?${new URLSearchParams(queryMap).toString()}`, {
return fetch_api(`/${encodeURIComponent(path)}?${new URLSearchParams(queryMap).toString()}`, {
method: "GET",
});
}
Expand Down

0 comments on commit 3361286

Please sign in to comment.