-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ edition = "2021" | |
repository = "https://github.com/MadebyAe/lambda-utils" | ||
description = "Lambda Utils for AWS Rust Lambda" | ||
authors = ["Ae <[email protected]>"] | ||
keywords = ["aws", "lambda", "serverless", "rust"] | ||
keywords = ["aws", "lambda", "serverless", "rust", "macros", "mongodb", "sqs"] | ||
categories = ["utils"] | ||
license = "MIT" | ||
|
||
|
@@ -14,6 +14,7 @@ aws-config = { version = "1.1.2", features = ["behavior-version-latest"], option | |
aws-sdk-sqs = { version = "1.9.0", optional = true } | ||
aws-types = { version = "1.1.2", optional = true } | ||
aws_lambda_events = { version = "0.15.0", optional = true } | ||
chrono = { version = "0.4", optional = true } | ||
lambda_http = { version = "0.11.1", optional = true } | ||
mongodb = { version = "2.3.1", optional = true } | ||
once_cell = { version = "1.17.0", optional = true } | ||
|
@@ -26,4 +27,5 @@ tokio = { version = "1.17.0", features = ["full", "test-util"] } | |
headers = ["lambda_http", "serde_json"] | ||
mongodb = ["dep:mongodb", "once_cell"] | ||
network = ["aws_lambda_events", "lambda_http"] | ||
response = ["chrono", "lambda_http", "serde_json"] | ||
sqs = ["aws-sdk-sqs", "aws-config", "serde_json"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#[macro_export] | ||
macro_rules! json_error { | ||
($status_code:expr, $message:expr) => {{ | ||
use chrono::Utc; | ||
use lambda_http::Body; | ||
use lambda_http::http::{Response, StatusCode}; | ||
use serde_json::json; | ||
|
||
fn error_code_to_type(status: StatusCode) -> &'static str { | ||
match status { | ||
StatusCode::BAD_REQUEST => "BadRequest", | ||
StatusCode::FORBIDDEN => "Forbidden", | ||
StatusCode::INTERNAL_SERVER_ERROR => "InternalServerError", | ||
StatusCode::UNAUTHORIZED => "Unauthorized", | ||
_ => "Unknown", | ||
} | ||
} | ||
|
||
let now = Utc::now(); | ||
let body = json!({ | ||
"code": $status_code.as_u16(), | ||
"message": $message, | ||
"timestamp": now.timestamp_millis(), | ||
"type": error_code_to_type($status_code), | ||
}).to_string(); | ||
|
||
Response::builder() | ||
.status($status_code) | ||
.header("Content-Type", "application/json") | ||
.body(Body::from(body)) | ||
.expect("Failed to construct error response") | ||
}}; | ||
} | ||
pub use json_error; | ||
|
||
#[macro_export] | ||
macro_rules! json_ok { | ||
($data:tt) => {{ | ||
use lambda_http::http::Response; | ||
use lambda_http::Body; | ||
use serde_json::json; | ||
|
||
let body = json!($data).to_string(); | ||
|
||
Response::builder() | ||
.status(StatusCode::OK) | ||
.header("Content-Type", "application/json") | ||
.body(Body::from(body)) | ||
.expect("Failed to construct success response") | ||
}}; | ||
} | ||
pub use json_ok; | ||
|
||
#[cfg(test)] | ||
mod response_tests { | ||
use super::*; | ||
use lambda_http::http::StatusCode; | ||
use lambda_http::{Body, Response}; | ||
use serde_json::{from_slice, Value}; | ||
|
||
fn extract_body(response: Response<Body>) -> Value { | ||
let body = response.into_body(); | ||
|
||
return from_slice(&body).unwrap_or_default(); | ||
} | ||
|
||
#[test] | ||
fn json_error_macro_test() { | ||
let response = json_error!(StatusCode::FORBIDDEN, "Forbidden access"); | ||
assert_eq!(response.status(), StatusCode::FORBIDDEN); | ||
|
||
let body = extract_body(response); | ||
assert_eq!(body["code"], 403); | ||
assert_eq!(body["message"], "Forbidden access"); | ||
assert_eq!(body["type"], "Forbidden"); | ||
} | ||
|
||
#[test] | ||
fn json_ok_macro_test() { | ||
let response = json_ok!({ "data": "Success" }); | ||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
let body = extract_body(response); | ||
assert_eq!(body["data"], "Success"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters