Skip to content

Commit

Permalink
feat(next-core): support parsing matcher config object
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 17, 2024
1 parent 048697b commit 900dd3d
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 91 deletions.
21 changes: 13 additions & 8 deletions packages/next-swc/crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use next_core::{
next_edge::entry::wrap_edge_entry,
next_manifests::{EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2},
next_server::{get_server_runtime_entries, ServerContextType},
util::parse_config_from_source,
util::{parse_config_from_source, MiddlewareMatcherKind},
};
use tracing::Instrument;
use turbo_tasks::{Completion, Value, Vc};
Expand Down Expand Up @@ -136,13 +136,18 @@ impl MiddlewareEndpoint {
get_wasm_paths_from_root(&node_root_value, &all_output_assets).await?;

let matchers = if let Some(matchers) = config.await?.matcher.as_ref() {
matchers
.iter()
.map(|matcher| MiddlewareMatcher {
original_source: matcher.to_string(),
..Default::default()
})
.collect()
match matchers {
MiddlewareMatcherKind::Str(matchers) => matchers
.iter()
.map(|matcher| MiddlewareMatcher {
original_source: matcher.to_string(),
..Default::default()
})
.collect(),
MiddlewareMatcherKind::Matcher(matcher) => {
vec![matcher.clone()]
}
}
} else {
vec![MiddlewareMatcher {
regexp: Some("^/.*$".to_string()),
Expand Down
16 changes: 14 additions & 2 deletions packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result};
use indexmap::IndexMap;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value as JsonValue;
use turbo_tasks::{trace::TraceRawVcs, Vc};
use turbo_tasks::{trace::TraceRawVcs, TaskInput, Vc};
use turbopack_binding::{
turbo::{tasks_env::EnvMap, tasks_fs::FileSystemPath},
turbopack::{
Expand Down Expand Up @@ -193,7 +193,19 @@ pub enum OutputType {
Export,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[derive(
Debug,
Clone,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
TaskInput,
TraceRawVcs,
Serialize,
Deserialize,
)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum RouteHas {
Header {
Expand Down
40 changes: 15 additions & 25 deletions packages/next-swc/crates/next-core/src/next_manifests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indexmap::IndexSet;
use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, TaskInput};

use crate::next_config::{CrossOriginConfig, Rewrites};
use crate::next_config::{CrossOriginConfig, Rewrites, RouteHas};

#[derive(Serialize, Default, Debug)]
pub struct PagesManifest {
Expand Down Expand Up @@ -44,30 +44,20 @@ impl Default for MiddlewaresManifest {
}
}

#[derive(Serialize, Debug)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum RouteHas {
Header {
key: String,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<String>,
},
Cookie {
key: String,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<String>,
},
Query {
key: String,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<String>,
},
Host {
value: String,
},
}

#[derive(Serialize, Default, Debug)]
#[derive(
Debug,
Clone,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
TaskInput,
TraceRawVcs,
Serialize,
Deserialize,
Default,
)]
#[serde(rename_all = "camelCase")]
pub struct MiddlewareMatcher {
// When skipped next.js with fill that during merging.
Expand Down
Loading

0 comments on commit 900dd3d

Please # to comment.