diff --git a/src/content/docs/workers/examples/basic-auth.mdx b/src/content/docs/workers/examples/basic-auth.mdx
index 46105a13575d8a7..74ec57b26f8a871 100644
--- a/src/content/docs/workers/examples/basic-auth.mdx
+++ b/src/content/docs/workers/examples/basic-auth.mdx
@@ -260,21 +260,22 @@ export default {
```
+
```rs
use base64::prelude::*;
use worker::*;
#[event(fetch)]
async fn fetch(req: Request, env: Env, _ctx: Context) -> Result {
-let basic_user = "admin";
-// You will need an admin password. This should be
-// attached to your Worker as an encrypted secret.
-// Refer to https://developers.cloudflare.com/workers/configuration/secrets/
-let basic_pass = match env.secret("PASSWORD") {
-Ok(s) => s.to_string(),
-Err(_) => "password".to_string(),
-};
-let url = req.url()?;
+ let basic_user = "admin";
+ // You will need an admin password. This should be
+ // attached to your Worker as an encrypted secret.
+ // Refer to https://developers.cloudflare.com/workers/configuration/secrets/
+ let basic_pass = match env.secret("PASSWORD") {
+ Ok(s) => s.to_string(),
+ Err(_) => "password".to_string(),
+ };
+ let url = req.url()?;
match url.path() {
"/" => Response::ok("Anyone can access the homepage."),
@@ -328,10 +329,10 @@ let url = req.url()?;
}
_ => Response::error("Not Found.", 404),
}
-
}
-````
+```
+
```ts
@@ -346,17 +347,17 @@ import { basicAuth } from "hono/basic-auth";
// Define environment interface
interface Env {
- Bindings: {
+ Bindings: {
USERNAME: string;
- PASSWORD: string;
- };
+ PASSWORD: string;
+ };
}
const app = new Hono();
// Public homepage - accessible to everyone
app.get("/", (c) => {
- return c.text("Anyone can access the homepage.");
+ return c.text("Anyone can access the homepage.");
});
// Admin route - protected with Basic Auth
@@ -365,8 +366,8 @@ app.get(
async (c, next) => {
const auth = basicAuth({
username: c.env.USERNAME,
- password: c.env.PASSWORD
- })
+ password: c.env.PASSWORD,
+ });
return await auth(c, next);
},
@@ -374,10 +375,10 @@ app.get(
return c.text("🎉 You have private access!", 200, {
"Cache-Control": "no-store",
});
- }
+ },
);
export default app;
-````
+```
diff --git a/src/content/docs/workers/examples/cors-header-proxy.mdx b/src/content/docs/workers/examples/cors-header-proxy.mdx
index 34f467d6e5db1b6..ab87ea853e1151e 100644
--- a/src/content/docs/workers/examples/cors-header-proxy.mdx
+++ b/src/content/docs/workers/examples/cors-header-proxy.mdx
@@ -583,67 +583,69 @@ async def on_fetch(request):
```
+
```rs
use std::{borrow::Cow, collections::HashMap};
use worker::*;
-fn raw*html_response(html: &str) -> Result {
-Response::from_html(html)
+fn raw_html_response(html: &str) -> Result {
+ Response::from_html(html)
}
async fn handle_request(req: Request, api_url: &str) -> Result {
-let url = req.url().unwrap();
-let mut api_url2 = url
-.query_pairs()
-.find(|x| x.0 == Cow::Borrowed("apiurl"))
-.unwrap()
-.1
-.to_string();
-if api_url2 == String::from("") {
-api_url2 = api_url.to_string();
-}
-let mut request = req.clone_mut()?;
-\*request.path_mut()? = api_url2.clone();
-if let url::Origin::Tuple(origin, *, _) = Url::parse(&api_url2)?.origin() {
-(\*request.headers_mut()?).set("Origin", &origin)?;
-}
-let mut response = Fetch::Request(request).send().await?.cloned()?;
-let headers = response.headers_mut();
-if let url::Origin::Tuple(origin, _, \_) = url.origin() {
-headers.set("Access-Control-Allow-Origin", &origin)?;
-headers.set("Vary", "Origin")?;
-}
+ let url = req.url().unwrap();
+ let mut api_url2 = url
+ .query_pairs()
+ .find(|x| x.0 == Cow::Borrowed("apiurl"))
+ .unwrap()
+ .1
+ .to_string();
+ if api_url2 == String::from("") {
+ api_url2 = api_url.to_string();
+ }
+ let mut request = req.clone_mut()?;
+ *request.path_mut()? = api_url2.clone();
+ if let url::Origin::Tuple(origin, _, _) = Url::parse(&api_url2)?.origin() {
+ (*request.headers_mut()?).set("Origin", &origin)?;
+ }
+ let mut response = Fetch::Request(request).send().await?.cloned()?;
+ let headers = response.headers_mut();
+ if let url::Origin::Tuple(origin, _, _) = url.origin() {
+ headers.set("Access-Control-Allow-Origin", &origin)?;
+ headers.set("Vary", "Origin")?;
+ }
Ok(response)
-
}
-fn handle*options(req: Request, cors_headers: &HashMap<&str, &str>) -> Result {
-let headers: Vec<*> = req.headers().keys().collect();
-if [
-"access-control-request-method",
-"access-control-request-headers",
-"origin",
-]
-.iter()
-.all(|i| headers.contains(&i.to_string()))
-{
-let mut headers = Headers::new();
-for (k, v) in cors_headers.iter() {
-headers.set(k, v)?;
-}
-return Ok(Response::empty()?.with_headers(headers));
+fn handle_options(req: Request, cors_headers: &HashMap<&str, &str>) -> Result {
+ let headers: Vec<_> = req.headers().keys().collect();
+ if [
+ "access-control-request-method",
+ "access-control-request-headers",
+ "origin",
+ ]
+ .iter()
+ .all(|i| headers.contains(&i.to_string()))
+ {
+ let mut headers = Headers::new();
+ for (k, v) in cors_headers.iter() {
+ headers.set(k, v)?;
+ }
+ return Ok(Response::empty()?.with_headers(headers));
+ }
+ Response::empty()
}
-Response::empty()
-} #[event(fetch)]
-async fn fetch(req: Request, \_env: Env, \_ctx: Context) -> Result {
-let cors_headers = HashMap::from([
-("Access-Control-Allow-Origin", "*"),
-("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS"),
-("Access-Control-Max-Age", "86400"),
-]);
-let api_url = "https://examples.cloudflareworkers.com/demos/demoapi";
-let proxy_endpoint = "/corsproxy/";
-let demo_page = format!(
+
+#[event(fetch)]
+async fn fetch(req: Request, _env: Env, _ctx: Context) -> Result {
+ let cors_headers = HashMap::from([
+ ("Access-Control-Allow-Origin", "*"),
+ ("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS"),
+ ("Access-Control-Max-Age", "86400"),
+ ]);
+ let api_url = "https://examples.cloudflareworkers.com/demos/demoapi";
+ let proxy_endpoint = "/corsproxy/";
+ let demo_page = format!(
r#"
@@ -696,7 +698,7 @@ return response.json()