mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Query parameters for net serve is now a table instead of a string
This commit is contained in:
parent
16d9c94822
commit
7aa8896997
4 changed files with 35 additions and 11 deletions
|
@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `NetRequest` query parameters value has been changed to be a table of key-value pairs similar to `process.env`.
|
||||||
|
If any query parameter is specified more than once in the request url, the value chosen will be the last one that was specified.
|
||||||
|
|
||||||
## `0.3.0` - February 6th, 2023
|
## `0.3.0` - February 6th, 2023
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -142,7 +142,7 @@ export type NetFetchResponse = {
|
||||||
|
|
||||||
export type NetRequest = {
|
export type NetRequest = {
|
||||||
path: string,
|
path: string,
|
||||||
query: string,
|
query: { [string]: string? },
|
||||||
method: NetMethod,
|
method: NetMethod,
|
||||||
headers: { [string]: string },
|
headers: { [string]: string },
|
||||||
body: string,
|
body: string,
|
||||||
|
@ -437,4 +437,4 @@ declare task: {
|
||||||
declare print: <T...>(T...) -> ()
|
declare print: <T...>(T...) -> ()
|
||||||
declare info: <T...>(T...) -> ()
|
declare info: <T...>(T...) -> ()
|
||||||
declare warn: <T...>(T...) -> ()
|
declare warn: <T...>(T...) -> ()
|
||||||
declare error: <T>(message: T, level: number?) -> never
|
declare error: <T>(message: T, level: number?) -> ()
|
||||||
|
|
|
@ -194,21 +194,36 @@ impl Service<Request<Body>> for NetService {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.upgrade()
|
.upgrade()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// Create a readonly table with request info to pass to the handler
|
// Create a readonly table for the request query params
|
||||||
let request = TableBuilder::new(&lua)?
|
let query_params = TableBuilder::new(&lua)?
|
||||||
.with_value("path", parts.uri.path())?
|
.with_values(
|
||||||
.with_value("query", parts.uri.query().unwrap_or_default())?
|
parts
|
||||||
.with_value("method", parts.method.as_str())?
|
.uri
|
||||||
.with_value(
|
.query()
|
||||||
"headers",
|
.unwrap_or_default()
|
||||||
|
.split('&')
|
||||||
|
.filter_map(|q| q.split_once('='))
|
||||||
|
.collect(),
|
||||||
|
)?
|
||||||
|
.build_readonly()?;
|
||||||
|
// Do the same for headers
|
||||||
|
let header_map = TableBuilder::new(&lua)?
|
||||||
|
.with_values(
|
||||||
parts
|
parts
|
||||||
.headers
|
.headers
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(name, value)| {
|
.map(|(name, value)| {
|
||||||
(name.to_string(), value.to_str().unwrap().to_string())
|
(name.to_string(), value.to_str().unwrap().to_string())
|
||||||
})
|
})
|
||||||
.collect::<HashMap<String, String>>(),
|
.collect(),
|
||||||
)?
|
)?
|
||||||
|
.build_readonly()?;
|
||||||
|
// Create a readonly table with request info to pass to the handler
|
||||||
|
let request = TableBuilder::new(&lua)?
|
||||||
|
.with_value("path", parts.uri.path())?
|
||||||
|
.with_value("query", query_params)?
|
||||||
|
.with_value("method", parts.method.as_str())?
|
||||||
|
.with_value("headers", header_map)?
|
||||||
.with_value("body", lua.create_string(&bytes)?)?
|
.with_value("body", lua.create_string(&bytes)?)?
|
||||||
.build_readonly()?;
|
.build_readonly()?;
|
||||||
match handler.call_async(request).await {
|
match handler.call_async(request).await {
|
||||||
|
|
|
@ -4,11 +4,13 @@ task.spawn(function()
|
||||||
net.serve(8080, function(request)
|
net.serve(8080, function(request)
|
||||||
-- info("Request:", request)
|
-- info("Request:", request)
|
||||||
-- info("Responding with", RESPONSE)
|
-- info("Responding with", RESPONSE)
|
||||||
|
assert(request.path == "/some/path")
|
||||||
|
assert(request.query.key == "param2")
|
||||||
return RESPONSE
|
return RESPONSE
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local response = net.request("http://127.0.0.1:8080").body
|
local response = net.request("http://127.0.0.1:8080/some/path?key=param1&key=param2").body
|
||||||
assert(response == RESPONSE, "Invalid response from server")
|
assert(response == RESPONSE, "Invalid response from server")
|
||||||
|
|
||||||
task.delay(1, function()
|
task.delay(1, function()
|
||||||
|
|
Loading…
Reference in a new issue