Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ struct User {
}

async fn create_user(
JSON(CreateUser { name }): JSON<CreateUser<'_>>
) -> status::Created<JSON<User>> {
status::Created(JSON(User {
Json(CreateUser { name }): Json<CreateUser<'_>>
) -> status::Created<Json<User>> {
status::Created(Json(User {
id: 42,
name: name.to_string()
}))
Expand All @@ -246,8 +246,8 @@ async fn create_user(
})]
/// This doc comment is used for the
/// `description` field of OpenAPI document
async fn list_users() -> JSON<Vec<User>> {
JSON(vec![])
async fn list_users() -> Json<Vec<User>> {
Json(vec![])
}

#[tokio::main]
Expand All @@ -272,7 +272,7 @@ async fn main() {
}
```

- Currently, only JSON is supported as the document format.
- Currently, only Json is supported as the document format.
- When the binary size matters, you should prepare a feature flag activating `ohkami/openapi` in your package, and put all your codes around `openapi` behind that feature via `#[cfg(feature = ...)]` or `#[cfg_attr(feature = ...)]`.
- In `rt_worker`, `.generate` is not available because `Ohkami` can't have access to your local filesystem by `wasm32` binary on Minifalre. So ohkami provides [a CLI tool](./scripts/workers_openapi.js) to generate document from `#[ohkami::worker] Ohkami` with `openapi` feature.

Expand Down Expand Up @@ -367,7 +367,7 @@ Hello, secure ohkami!

### Typed payload

*builtin payload* : `JSON`, `Text`, `HTML`, `URLEncoded`, `Multipart`
*builtin payload* : `Json`, `Text`, `Html`, `UrlEncoded`, `Multipart`

```rust
use ohkami::prelude::*;
Expand All @@ -387,9 +387,9 @@ struct User {
}

async fn create_user(
JSON(req): JSON<CreateUserRequest<'_>>
) -> status::Created<JSON<User>> {
status::Created(JSON(
Json(req): Json<CreateUserRequest<'_>>
) -> status::Created<Json<User>> {
status::Created(Json(
User {
name: String::from(req.name)
}
Expand Down Expand Up @@ -436,8 +436,8 @@ struct SearchResult {

async fn search(
Query(query): Query<SearchQuery<'_>>
) -> JSON<Vec<SearchResult>> {
JSON(vec![
) -> Json<Vec<SearchResult>> {
Json(vec![
SearchResult { title: String::from("ohkami") },
])
}
Expand All @@ -452,8 +452,8 @@ There are two types of fangs : *global fangs* and *local fangs*. While global fa
*builtin fang* :

- `Context` *( typed interaction with reuqest context )*
- `CORS`, `JWT`, `BasicAuth`
- `Timeout` *( native runtime )*
- `Cors`, `Jwt`, `BasicAuth`
- `Timeout` *( native runtime only )*
- `Enamel` *( experimantal; security headers )*

```rust,no_run
Expand Down Expand Up @@ -524,7 +524,7 @@ async fn create_user(
```rust,no_run
use ohkami::{Response, IntoResponse};
use ohkami::serde::Serialize;
use ohkami::format::JSON;
use ohkami::format::Json;
use ohkami::fang::Context;

enum MyError {
Expand All @@ -547,7 +547,7 @@ struct User {
async fn get_user(
id: u32,
Context(pool): Context<'_, sqlx::PgPool>,
) -> Result<JSON<User>, MyError> {
) -> Result<Json<User>, MyError> {
let sql = r#"
SELECT name FROM users WHERE id = $1
"#;
Expand All @@ -557,7 +557,7 @@ async fn get_user(
.await
.map_err(MyError::Sqlx)?;

Ok(JSON(User { id, name }))
Ok(Json(User { id, name }))
}
```

Expand Down Expand Up @@ -627,16 +627,16 @@ struct User {
name: String
}

async fn list_users() -> JSON<Vec<User>> {
JSON(vec![
async fn list_users() -> Json<Vec<User>> {
Json(vec![
User { name: String::from("actix") },
User { name: String::from("axum") },
User { name: String::from("ohkami") },
])
}

async fn create_user() -> status::Created<JSON<User>> {
status::Created(JSON(User {
async fn create_user() -> status::Created<Json<User>> {
status::Created(Json(User {
name: String::from("ohkami web framework")
}))
}
Expand Down Expand Up @@ -753,10 +753,10 @@ struct User {
async fn get_user<R: Repository>(
id: u32,
Context(r): Context<'_, R>,
) -> Result<JSON<User>, MyError> {
) -> Result<Json<User>, MyError> {
let user_row = r.get_user_by_id(id as i64).await?;

Ok(JSON(User {
Ok(Json(User {
id: user_row.id as u32,
name: user_row.name,
}))
Expand Down
6 changes: 3 additions & 3 deletions benches_rt/tokio/src/bin/hello.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use ohkami::prelude::*;
use ohkami::format::JSON;
use ohkami::format::Json;

#[derive(Serialize)]
struct Message {
message: String
}

async fn hello(name: &str) -> JSON<Message> {
JSON(Message {
async fn hello(name: &str) -> Json<Message> {
Json(Message {
message: format!("Hello, {name}!")
})
}
Expand Down
4 changes: 2 additions & 2 deletions examples/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod health_handler {


mod hello_handler {
use ohkami::format::{Query, JSON};
use ohkami::format::{Query, Json};
use ohkami::serde::Deserialize;

#[derive(Deserialize)]
Expand Down Expand Up @@ -50,7 +50,7 @@ mod hello_handler {
}

pub async fn hello_by_json(
JSON(HelloRequest { name, repeat }): JSON<HelloRequest<'_>>
Json(HelloRequest { name, repeat }): Json<HelloRequest<'_>>
) -> String {
tracing::info!("\
Called `hello_by_json`\
Expand Down
6 changes: 3 additions & 3 deletions examples/html_layout/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ohkami::prelude::*;
use ohkami::serde::Deserialize;
use ohkami::format::{Query, HTML};
use ohkami::format::{Query, Html};
use uibeam::{UI, Beam};

struct Layout {
Expand Down Expand Up @@ -87,10 +87,10 @@ struct CounterMeta {
init: Option<i32>,
}

async fn index(Query(q): Query<CounterMeta>) -> HTML<std::borrow::Cow<'static, str>> {
async fn index(Query(q): Query<CounterMeta>) -> Html<std::borrow::Cow<'static, str>> {
let initial_count = q.init.unwrap_or(0);

HTML(uibeam::shoot(UI! {
Html(uibeam::shoot(UI! {
<Counter initial_count={initial_count} />
}))
}
Expand Down
10 changes: 5 additions & 5 deletions examples/json_response/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ohkami::{Ohkami, Route};
use ohkami::format::JSON;
use ohkami::format::Json;
use ohkami::serde::Serialize;


Expand All @@ -9,15 +9,15 @@ struct User {
name: String,
}

async fn single_user() -> JSON<User> {
JSON(User {
async fn single_user() -> Json<User> {
Json(User {
id: 42,
name: String::from("ohkami"),
})
}

async fn multiple_users() -> JSON<Vec<User>> {
JSON(vec![
async fn multiple_users() -> Json<Vec<User>> {
Json(vec![
User {
id: 42,
name: String::from("ohkami"),
Expand Down
12 changes: 6 additions & 6 deletions examples/jwt/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ohkami::prelude::*;
use ohkami::fang::{JWT, JWTToken};
use ohkami::fang::{Jwt, JwtToken};

fn jwt() -> JWT<JwtPayload> {
JWT::default(std::env::var("JWT_SECRET").unwrap())
fn jwt() -> Jwt<JwtPayload> {
Jwt::default(std::env::var("JWT_SECRET").unwrap())
}

#[derive(Serialize, Deserialize)]
Expand All @@ -23,16 +23,16 @@ impl JwtSub for DefaultJwtSub {
#[derive(Serialize)]
#[cfg_attr(test, derive(Deserialize))]
struct AuthResponse {
token: JWTToken,
token: JwtToken,
}

async fn auth<S: JwtSub>() -> JSON<AuthResponse> {
async fn auth<S: JwtSub>() -> Json<AuthResponse> {
let token = jwt().issue(JwtPayload {
sub: S::sub(),
exp: ohkami::util::unix_timestamp() + 86400,
});

JSON(AuthResponse { token })
Json(AuthResponse { token })
}

async fn private(
Expand Down
4 changes: 2 additions & 2 deletions ohkami/src/fang/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ mod basicauth;
pub use basicauth::BasicAuth;

mod cors;
pub use cors::CORS;
pub use cors::Cors;

mod jwt;
pub use jwt::{JWT, JWTToken};
pub use jwt::{Jwt, JwtToken};

mod context;
pub use context::Context;
Expand Down
Loading