Skip to content

Commit cf89a88

Browse files
committed
refactor: use direct status codes instead of @Mocket namespace
Replace @mocket.OK and @mocket.NotFound with direct status code references (OK, NotFound) for cleaner code and better readability
1 parent 5658191 commit cf89a88

7 files changed

Lines changed: 18 additions & 15 deletions

File tree

src/cors/cors.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn handle_cors(
6666
max_age~,
6767
)
6868
// 对于预检请求,直接返回空响应,不调用next()
69-
@mocket.HttpResponse::new(@mocket.OK).to_responder()
69+
@mocket.HttpResponse::new(OK).to_responder()
7070
} else {
7171
append_cors_headers(
7272
event,

src/examples/responder/pkg.generated.mbti

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Generated using `moon info`, DON'T EDIT IT
22
package "oboard/mocket/examples/responder"
33

4+
import(
5+
"moonbitlang/core/json"
6+
)
7+
48
// Values
59

610
// Errors
711

812
// Types and methods
913
type Person
1014
impl ToJson for Person
15+
impl @json.FromJson for Person
1116

1217
// Type aliases
1318

src/examples/route/main.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn main {
5757
..post("/echo", e => e.req)
5858

5959
// 404 Page
60-
..get("/404", _ => @mocket.HttpResponse::new(@mocket.NotFound).body(
60+
..get("/404", _ => @mocket.HttpResponse::new(NotFound).body(
6161
@mocket.html(
6262
(
6363
#|<html>

src/pkg.generated.mbti

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ fn HttpRequestInternal::on_complete(Self, FuncRef[() -> Unit]) -> Unit
9696
fn HttpRequestInternal::on_headers(Self, FuncRef[(@native.CStr) -> Unit]) -> Unit
9797

9898
pub(all) struct HttpResponse {
99-
mut status_code : &StatusCoder
99+
mut status_code : StatusCode
100100
headers : Map[StringView, StringView]
101101
cookies : Map[String, CookieItem]
102102
mut raw_body : Bytes
103103
}
104104
fn HttpResponse::body(Self, &Responder) -> Self
105105
fn HttpResponse::delete_cookie(Self, String) -> Unit
106-
fn HttpResponse::json(Self, Json) -> Self
107-
fn HttpResponse::new(&StatusCoder, headers? : Map[StringView, StringView], cookies? : Map[String, CookieItem], raw_body? : Bytes) -> Self
106+
fn HttpResponse::json(Self, &ToJson) -> Self
107+
fn HttpResponse::new(StatusCode, headers? : Map[StringView, StringView], cookies? : Map[String, CookieItem], raw_body? : Bytes) -> Self
108108
fn HttpResponse::set_cookie(Self, String, String, max_age? : Int, path? : String, domain? : String, secure? : Bool, http_only? : Bool, same_site? : SameSiteOption) -> Unit
109109
fn HttpResponse::to_responder(Self) -> &Responder
110110
impl Responder for HttpResponse
@@ -239,10 +239,12 @@ pub(all) enum StatusCode {
239239
LoopDetected
240240
NotExtended
241241
NetworkAuthenticationRequired
242+
Custom(Int)
242243
}
244+
fn StatusCode::from_int(Int) -> Self
245+
fn StatusCode::to_int(Self) -> Int
243246
impl Show for StatusCode
244247
impl ToJson for StatusCode
245-
impl StatusCoder for StatusCode
246248

247249
pub enum WebSocketAggregatedMessage {
248250
Text(String)
@@ -304,8 +306,3 @@ pub(open) trait ServeStaticProvider {
304306
get_fallthrough(Self) -> Bool
305307
}
306308

307-
pub trait StatusCoder {
308-
to_int(Self) -> Int
309-
}
310-
impl StatusCoder for Int
311-

src/request.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(all) struct HttpRequest {
88

99
///|
1010
pub(open) trait BodyReader {
11-
from_request(req: HttpRequest) -> Self raise
11+
from_request(req : HttpRequest) -> Self raise
1212
}
1313

1414
///|

src/static_file/static_file.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ pub impl ServeStaticProvider for StaticFileProvider with get_contents(
2626
self,
2727
id : String,
2828
) -> &Responder {
29-
let res : &Responder = @mocket.HttpResponse::new(@mocket.OK).body(
29+
let res : &Responder = @mocket.HttpResponse::new(OK).body(
3030
@fs.read_file_to_bytes(self.path + "/" + id),
3131
) catch {
32-
_ => @mocket.HttpResponse::new(@mocket.NotFound).body("Not Found")
32+
_ => @mocket.HttpResponse::new(NotFound).body("Not Found")
3333
}
3434
res
3535
}

src/status_code.mbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ pub(all) enum StatusCode {
129129
Custom(Int)
130130
} derive(Show, ToJson)
131131

132+
///|
132133
pub fn StatusCode::from_int(i : Int) -> StatusCode {
133134
Custom(i)
134135
}
135136

136137
///|
137-
pub fn StatusCode::to_int(self: StatusCode) -> Int {
138+
pub fn StatusCode::to_int(self : StatusCode) -> Int {
138139
match self {
139140
Continue => 100
140141
SwitchingProtocols => 101

0 commit comments

Comments
 (0)