Skip to content

Commit dbda194

Browse files
committed
remove opinionated payload
1 parent 7311b6e commit dbda194

File tree

3 files changed

+39
-76
lines changed

3 files changed

+39
-76
lines changed

examples/simple/api/complex.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ use vercel_runtime::{
66
Request, RequestExt, Response, ServiceBuilder, StatusCode,
77
};
88

9+
#[derive(Debug, Serialize, Deserialize)]
10+
struct Payload {
11+
trainer_name: String,
12+
}
13+
14+
#[derive(Serialize)]
15+
pub struct APIError {
16+
pub message: &'static str,
17+
pub code: &'static str,
18+
}
19+
920
#[tokio::main]
1021
async fn main() -> Result<(), Error> {
1122
tracing_subscriber::fmt()
@@ -23,18 +34,19 @@ async fn main() -> Result<(), Error> {
2334
run_service(handler).await
2435
}
2536

26-
#[derive(Debug, Serialize, Deserialize)]
27-
struct Payload {
28-
trainer_name: String,
29-
}
30-
3137
pub async fn handler(req: Request) -> Result<Response<Body>, Error> {
3238
tracing::info!("Choosing a starter Pokemon");
3339
let payload = req.payload::<Payload>();
3440

3541
match payload {
36-
Err(..) => bad_request("Invalid payload"),
37-
Ok(None) => bad_request("Invalid payload"),
42+
Err(..) => bad_request(APIError {
43+
message: "Invalid payload",
44+
code: "invalid_payload",
45+
}),
46+
Ok(None) => bad_request(APIError {
47+
message: "No payload",
48+
code: "no_payload",
49+
}),
3850
Ok(Some(payload)) => {
3951
let starter = choose_starter();
4052

examples/simple/api/user/[id].rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
use serde::Serialize;
12
use std::collections::HashMap;
23
use url::Url;
34
use vercel_runtime::{http::bad_request, run, Body, Error, Request, Response, StatusCode};
45

6+
#[derive(Serialize)]
7+
pub struct APIError {
8+
pub message: &'static str,
9+
pub code: &'static str,
10+
}
11+
512
#[tokio::main]
613
async fn main() -> Result<(), Error> {
714
run(handler).await
@@ -14,7 +21,10 @@ pub async fn handler(req: Request) -> Result<Response<Body>, Error> {
1421

1522
match id_key {
1623
None => {
17-
return bad_request("Invalid query string");
24+
return bad_request(APIError {
25+
message: "Query string is invalid",
26+
code: "query_string_invalid",
27+
});
1828
}
1929
Some(id) => Ok(Response::builder()
2030
.status(StatusCode::OK)

vercel_runtime/src/http.rs

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,37 @@
11
use lambda_http::{http::StatusCode, Body, Error, Response};
22
use serde::Serialize;
3-
use std::error::Error as StdError;
4-
use tracing::error;
53

6-
pub fn ok<T: Serialize>(val: T) -> Result<Response<Body>, Error> {
4+
pub fn ok(val: impl Serialize) -> Result<Response<Body>, Error> {
75
Ok(Response::builder()
86
.status(StatusCode::OK)
97
.header("content-type", "application/json")
108
.body(Body::Text(serde_json::to_string(&val).unwrap()))?)
119
}
1210

13-
#[derive(Serialize)]
14-
pub struct APIError {
15-
pub message: &'static str,
16-
pub code: &'static str,
17-
}
18-
19-
impl From<APIError> for lambda_http::Body {
20-
fn from(val: APIError) -> Self {
21-
lambda_http::Body::Text(serde_json::to_string(&val).unwrap())
22-
}
23-
}
24-
25-
pub fn bad_request(message: &'static str) -> Result<Response<Body>, Error> {
11+
pub fn bad_request(val: impl Serialize) -> Result<Response<Body>, Error> {
2612
Ok(Response::builder()
2713
.status(StatusCode::BAD_REQUEST)
2814
.header("content-type", "application/json")
29-
.body(
30-
APIError {
31-
message,
32-
code: "bad_request",
33-
}
34-
.into(),
35-
)?)
36-
}
37-
38-
#[derive(Serialize)]
39-
struct Found {
40-
found: bool,
41-
}
42-
43-
impl From<Found> for lambda_http::Body {
44-
fn from(val: Found) -> Self {
45-
lambda_http::Body::Text(serde_json::to_string(&val).unwrap())
46-
}
47-
}
48-
49-
const NOT_FOUND: Found = Found { found: false };
50-
51-
pub fn not_found() -> Result<Response<Body>, Error> {
52-
Ok(Response::builder()
53-
.status(StatusCode::OK)
54-
.header("content-type", "application/json")
55-
.body(NOT_FOUND.into())?)
15+
.body(Body::Text(serde_json::to_string(&val).unwrap()))?)
5616
}
5717

58-
pub fn endpoint_not_found() -> Result<Response<Body>, Error> {
18+
pub fn not_found(val: impl Serialize) -> Result<Response<Body>, Error> {
5919
Ok(Response::builder()
6020
.status(StatusCode::NOT_FOUND)
6121
.header("content-type", "application/json")
62-
.body(
63-
APIError {
64-
message: "Not found",
65-
code: "not_found",
66-
}
67-
.into(),
68-
)?)
22+
.body(Body::Text(serde_json::to_string(&val).unwrap()))?)
6923
}
7024

71-
pub fn unauthorized() -> Result<Response<Body>, Error> {
25+
pub fn unauthorized(val: impl Serialize) -> Result<Response<Body>, Error> {
7226
Ok(Response::builder()
7327
.status(StatusCode::UNAUTHORIZED)
7428
.header("content-type", "application/json")
75-
.body(
76-
APIError {
77-
message: "Unauthorized",
78-
code: "unauthorized",
79-
}
80-
.into(),
81-
)?)
29+
.body(Body::Text(serde_json::to_string(&val).unwrap()))?)
8230
}
8331

84-
pub fn internal_server_error(err: impl StdError) -> Result<Response<Body>, Error> {
85-
error!(error = ?err, "internal server error");
32+
pub fn internal_server_error(val: impl Serialize) -> Result<Response<Body>, Error> {
8633
Ok(Response::builder()
8734
.status(StatusCode::INTERNAL_SERVER_ERROR)
8835
.header("content-type", "application/json")
89-
.body(
90-
APIError {
91-
message: "Internal server error",
92-
code: "internal_server_error",
93-
}
94-
.into(),
95-
)?)
36+
.body(Body::Text(serde_json::to_string(&val).unwrap()))?)
9637
}

0 commit comments

Comments
 (0)