From 7aa8896997a5d5e013bde09cd5dfb25851473406 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Tue, 7 Feb 2023 21:54:52 -0500 Subject: [PATCH] Query parameters for net serve is now a table instead of a string --- CHANGELOG.md | 7 +++++++ luneTypes.d.luau | 4 ++-- packages/lib/src/globals/net.rs | 31 +++++++++++++++++++++++-------- tests/net/serve.luau | 4 +++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57df78b..b8bc539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), 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 ### Added diff --git a/luneTypes.d.luau b/luneTypes.d.luau index ce07f27..39f561e 100644 --- a/luneTypes.d.luau +++ b/luneTypes.d.luau @@ -142,7 +142,7 @@ export type NetFetchResponse = { export type NetRequest = { path: string, - query: string, + query: { [string]: string? }, method: NetMethod, headers: { [string]: string }, body: string, @@ -437,4 +437,4 @@ declare task: { declare print: (T...) -> () declare info: (T...) -> () declare warn: (T...) -> () -declare error: (message: T, level: number?) -> never +declare error: (message: T, level: number?) -> () diff --git a/packages/lib/src/globals/net.rs b/packages/lib/src/globals/net.rs index 7076dbd..fd71260 100644 --- a/packages/lib/src/globals/net.rs +++ b/packages/lib/src/globals/net.rs @@ -194,21 +194,36 @@ impl Service> for NetService { .unwrap() .upgrade() .unwrap(); - // 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", parts.uri.query().unwrap_or_default())? - .with_value("method", parts.method.as_str())? - .with_value( - "headers", + // Create a readonly table for the request query params + let query_params = TableBuilder::new(&lua)? + .with_values( + parts + .uri + .query() + .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 .headers .iter() .map(|(name, value)| { (name.to_string(), value.to_str().unwrap().to_string()) }) - .collect::>(), + .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)?)? .build_readonly()?; match handler.call_async(request).await { diff --git a/tests/net/serve.luau b/tests/net/serve.luau index 03921f0..1f8ab87 100644 --- a/tests/net/serve.luau +++ b/tests/net/serve.luau @@ -4,11 +4,13 @@ task.spawn(function() net.serve(8080, function(request) -- info("Request:", request) -- info("Responding with", RESPONSE) + assert(request.path == "/some/path") + assert(request.query.key == "param2") return RESPONSE 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") task.delay(1, function()