feat: initialize Lux package registry
- Add registry README with API documentation - Add initial packages: json, http-client, testing - Add package index.json for registry server Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
58
packages/http-client/README.md
Normal file
58
packages/http-client/README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# http-client
|
||||
|
||||
HTTP client utilities for Lux.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
lux pkg add http-client 0.1.0
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```lux
|
||||
import http-client as http
|
||||
|
||||
fn main(): Unit with {Console, Http} = {
|
||||
// Simple GET request
|
||||
let body = http.get("https://api.example.com/data")
|
||||
Console.print(body)
|
||||
|
||||
// GET and parse JSON
|
||||
let data = http.getJson("https://api.example.com/users")
|
||||
Console.print("Got " ++ toString(List.length(data)) ++ " users")
|
||||
|
||||
// POST with JSON body
|
||||
let response = http.postJson(
|
||||
"https://api.example.com/users",
|
||||
Json.object([
|
||||
("name", JsonString("Alice")),
|
||||
("email", JsonString("alice@example.com"))
|
||||
])
|
||||
)
|
||||
|
||||
// GET with query parameters
|
||||
let results = http.getWithParams(
|
||||
"https://api.example.com/search",
|
||||
[("q", "lux programming"), ("limit", "10")]
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
| Function | Description |
|
||||
|----------|-------------|
|
||||
| `get(url)` | GET request, return body |
|
||||
| `getJson(url)` | GET and parse as JSON |
|
||||
| `postJson(url, data)` | POST with JSON body |
|
||||
| `putJson(url, data)` | PUT with JSON body |
|
||||
| `delete(url)` | DELETE request |
|
||||
| `postJsonGetJson(url, data)` | POST JSON, parse response |
|
||||
| `getWithParams(url, params)` | GET with query parameters |
|
||||
| `urlEncode(s)` | URL-encode a string |
|
||||
| `buildQueryString(params)` | Build query string from pairs |
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
72
packages/http-client/lib.lux
Normal file
72
packages/http-client/lib.lux
Normal file
@@ -0,0 +1,72 @@
|
||||
// HTTP client utilities for Lux
|
||||
//
|
||||
// Provides convenient wrappers around the Http effect.
|
||||
|
||||
// HTTP response type
|
||||
type Response = {
|
||||
status: Int,
|
||||
body: String,
|
||||
headers: List<(String, String)>
|
||||
}
|
||||
|
||||
// Make a GET request and return the body
|
||||
pub fn get(url: String): String with Http = {
|
||||
Http.get(url)
|
||||
}
|
||||
|
||||
// Make a POST request with JSON body
|
||||
pub fn postJson(url: String, data: JsonValue): String with Http = {
|
||||
Http.post(url, Json.stringify(data))
|
||||
}
|
||||
|
||||
// Make a PUT request with JSON body
|
||||
pub fn putJson(url: String, data: JsonValue): String with Http = {
|
||||
Http.put(url, Json.stringify(data))
|
||||
}
|
||||
|
||||
// Make a DELETE request
|
||||
pub fn delete(url: String): String with Http = {
|
||||
Http.delete(url)
|
||||
}
|
||||
|
||||
// Fetch JSON and parse it
|
||||
pub fn getJson(url: String): JsonValue with Http = {
|
||||
let body = Http.get(url)
|
||||
Json.parse(body)
|
||||
}
|
||||
|
||||
// Post JSON and parse response as JSON
|
||||
pub fn postJsonGetJson(url: String, data: JsonValue): JsonValue with Http = {
|
||||
let body = Http.post(url, Json.stringify(data))
|
||||
Json.parse(body)
|
||||
}
|
||||
|
||||
// URL encode a string
|
||||
pub fn urlEncode(s: String): String = {
|
||||
// Simple URL encoding
|
||||
String.replace(
|
||||
String.replace(
|
||||
String.replace(s, " ", "%20"),
|
||||
"&", "%26"
|
||||
),
|
||||
"=", "%3D"
|
||||
)
|
||||
}
|
||||
|
||||
// Build query string from parameters
|
||||
pub fn buildQueryString(params: List<(String, String)>): String = {
|
||||
let encoded = List.map(params, fn(pair) => {
|
||||
urlEncode(pair.0) ++ "=" ++ urlEncode(pair.1)
|
||||
})
|
||||
String.join(encoded, "&")
|
||||
}
|
||||
|
||||
// Make a GET request with query parameters
|
||||
pub fn getWithParams(baseUrl: String, params: List<(String, String)>): String with Http = {
|
||||
let qs = buildQueryString(params)
|
||||
let url = if String.contains(baseUrl, "?") then
|
||||
baseUrl ++ "&" ++ qs
|
||||
else
|
||||
baseUrl ++ "?" ++ qs
|
||||
Http.get(url)
|
||||
}
|
||||
8
packages/http-client/lux.toml
Normal file
8
packages/http-client/lux.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[project]
|
||||
name = "http-client"
|
||||
version = "0.1.0"
|
||||
description = "HTTP client utilities for Lux"
|
||||
authors = ["Lux Core Team"]
|
||||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
Reference in New Issue
Block a user