MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / response

Function response

aiscript-vm/src/builtins/response.rs:11–84  ·  view source on GitHub ↗

Creates a new Response instance with the given status code, body, headers, and cookies. Usage: response(status_code=200, body={}, headers={}, cookies={})

(state: &mut State<'gc>, args: Vec<Value<'gc>>)

Source from the content-addressed store, hash-verified

9/// Creates a new Response instance with the given status code, body, headers, and cookies.
10/// Usage: response(status_code=200, body={}, headers={}, cookies={})
11pub fn response<'gc>(state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
12 // Initialize default values
13 let mut status_code = 200.0;
14 let mut body = Value::Nil; // body can be any Value
15 let mut headers = Value::Object(Gc::new(state, RefLock::default()));
16 let mut cookies = Value::Object(Gc::new(state, RefLock::default()));
17
18 // Parse and validate keyword arguments in a single pass
19 let mut i = 0;
20 while i < args.len() {
21 match &args[i] {
22 Value::String(key) if i + 1 < args.len() => match key.to_str().unwrap() {
23 "status_code" => {
24 status_code = args[i + 1].as_number().map_err(|_| {
25 VmError::RuntimeError("status_code must be a number".into())
26 })?;
27 i += 2;
28 }
29 "body" => {
30 body = args[i + 1];
31 i += 2;
32 }
33 "headers" => {
34 let value = args[i + 1];
35 if !value.is_object() {
36 return Err(VmError::RuntimeError("headers must be an object".into()));
37 }
38 headers = value;
39 i += 2;
40 }
41 "cookies" => {
42 let value = args[i + 1];
43 if !value.is_object() {
44 return Err(VmError::RuntimeError("cookies must be an object".into()));
45 }
46 cookies = value;
47 i += 2;
48 }
49 _ => {
50 return Err(VmError::RuntimeError(format!(
51 "Unknown keyword argument: {}",
52 key
53 )));
54 }
55 },
56 _ => {
57 return Err(VmError::RuntimeError(
58 "Response() requires keyword arguments (e.g., Response(status_code=200, body=\"hello\"))".into(),
59 ));
60 }
61 }
62 }
63
64 // Validate status code
65 if !(100.0..=599.0).contains(&status_code) {
66 return Err(VmError::RuntimeError(
67 "status_code must be between 100 and 599".into(),
68 ));

Callers 2

temporary_redirectFunction · 0.85
permanent_redirectFunction · 0.85

Calls 9

ObjectClass · 0.85
RuntimeErrorClass · 0.85
InstanceClass · 0.85
lenMethod · 0.80
to_strMethod · 0.80
as_numberMethod · 0.80
is_objectMethod · 0.80
containsMethod · 0.80
internMethod · 0.45

Tested by

no test coverage detected