Promote develop to main for 26.11.0-rc.1 #23
No files matched your search
@@ -13,6 +13,12 @@ is pinned to is `CORE_VERSION` in `src/template.rs`, and each entry below says w
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- **`matcha add cookie`** adds `GET /prefs`, which reads a cookie with `@cookie('theme')`, and
|
||||||
|
`POST /prefs`, which sets it through a `@Transformer` and `setCookie()`. Core has no response-cookie
|
||||||
|
API, so the transformer is where a `Set-Cookie` header is sent. Needs `@green-tea/core` RC.1.
|
||||||
|
- **`matcha create transformer <Name>`** writes `src/transformers/<name>.transformer.ts`, a typed
|
||||||
|
`TransformerFn`, and prints the `@Transformer(...)` line that attaches it. A transformer belongs to
|
||||||
|
a route, not a module, so there is nothing to wire.
|
||||||
- **Every starter ships an `AGENTS.md` and a `CLAUDE.md` that imports it.** green-tea is new, so a
|
- **Every starter ships an `AGENTS.md` and a `CLAUDE.md` that imports it.** green-tea is new, so a
|
||||||
coding agent falls back on Express habits. The file states the rules that differ: it is a graph, not a
|
coding agent falls back on Express habits. The file states the rules that differ: it is a graph, not a
|
||||||
chain, a handler declares its needs in its signature, and the graph is checked at boot. It also points
|
chain, a handler declares its needs in its signature, and the graph is checked at boot. It also points
|
||||||
|
|||||||
@@ -96,9 +96,10 @@ echo "✓ graph, explain and openapi resolve"
|
|||||||
# core lazy-requires `ws` and `busboy`, so neither is needed to compile, and a
|
# core lazy-requires `ws` and `busboy`, so neither is needed to compile, and a
|
||||||
# check that needed them would be hiding that fact.
|
# check that needed them would be hiding that fact.
|
||||||
echo "▸ matcha add (every capability)"
|
echo "▸ matcha add (every capability)"
|
||||||
for cap in sse ws upload stream buffer; do
|
for cap in sse ws upload stream buffer cookie; do
|
||||||
"$MATCHA" add "$cap" >/dev/null || { echo "matcha add $cap failed"; exit 1; }
|
"$MATCHA" add "$cap" >/dev/null || { echo "matcha add $cap failed"; exit 1; }
|
||||||
done
|
done
|
||||||
|
"$MATCHA" create transformer Csv >/dev/null || { echo "matcha create transformer failed"; exit 1; }
|
||||||
"${CHECK[@]}" || { echo "the generated handlers do not type-check"; exit 1; }
|
"${CHECK[@]}" || { echo "the generated handlers do not type-check"; exit 1; }
|
||||||
echo "✓ every add capability type-checks"
|
echo "✓ every add capability type-checks"
|
||||||
|
|
||||||
|
|||||||
@@ -139,6 +139,39 @@ const CAPABILITIES: &[Capability] = &[
|
|||||||
marker: "'/upload'",
|
marker: "'/upload'",
|
||||||
peer: Some(("busboy", "every runtime")),
|
peer: Some(("busboy", "every runtime")),
|
||||||
},
|
},
|
||||||
|
Capability {
|
||||||
|
name: "cookie",
|
||||||
|
// Reading is a decorator. Writing is a `Set-Cookie` header, which core only builds
|
||||||
|
// (`setCookie`) and a transformer sends: core has no response-cookie API of its own.
|
||||||
|
imports: &["Get", "Post", "Transformer", "body", "cookie", "setCookie"],
|
||||||
|
type_imports: &[],
|
||||||
|
method: r#"
|
||||||
|
@Get('/prefs')
|
||||||
|
prefs(@cookie('theme') theme?: string) {
|
||||||
|
return { theme: theme ?? 'light' };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('/prefs')
|
||||||
|
@Transformer((value) => {
|
||||||
|
const { theme } = value as { theme: string };
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
// Secure and HttpOnly are on by default. Safari, a LAN IP or a plain-HTTP host other
|
||||||
|
// than localhost drops a Secure cookie: pass `secure: false` there in development.
|
||||||
|
'set-cookie': setCookie('theme', encodeURIComponent(theme), { maxAge: 60 * 60 * 24 * 365 }),
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ theme }),
|
||||||
|
};
|
||||||
|
})
|
||||||
|
savePrefs(@body('theme') theme: string) {
|
||||||
|
return { theme };
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
marker: "'/prefs'",
|
||||||
|
peer: None,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
/// The capability names, for clap's `value_parser` and for error messages.
|
/// The capability names, for clap's `value_parser` and for error messages.
|
||||||
|
|||||||
+32
-1
@@ -56,7 +56,7 @@ pub fn kinds() -> Vec<&'static str> {
|
|||||||
["module"]
|
["module"]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(PIECES.iter().map(|p| p.kind))
|
.chain(PIECES.iter().map(|p| p.kind))
|
||||||
.chain(["plugin"])
|
.chain(["transformer", "plugin"])
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +121,9 @@ pub fn run(kind: &str, name: &str, check: bool) -> std::io::Result<()> {
|
|||||||
if kind == "module" {
|
if kind == "module" {
|
||||||
return create_module(name, check);
|
return create_module(name, check);
|
||||||
}
|
}
|
||||||
|
if kind == "transformer" {
|
||||||
|
return create_transformer(name, check);
|
||||||
|
}
|
||||||
match PIECES.iter().find(|p| p.kind == kind) {
|
match PIECES.iter().find(|p| p.kind == kind) {
|
||||||
Some(piece) => create_piece(name, check, piece),
|
Some(piece) => create_piece(name, check, piece),
|
||||||
None => Err(Error::new(
|
None => Err(Error::new(
|
||||||
@@ -200,6 +203,34 @@ fn create_module(name: &str, check: bool) -> std::io::Result<()> {
|
|||||||
maybe_check(check)
|
maybe_check(check)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Writes `src/transformers/<name>.transformer.ts`. Nothing to wire: a transformer is attached
|
||||||
|
/// per route with `@Transformer`, so the hint says where it goes instead.
|
||||||
|
fn create_transformer(name: &str, check: bool) -> std::io::Result<()> {
|
||||||
|
let lower = name.to_lowercase();
|
||||||
|
let file = Path::new("src/transformers").join(format!("{lower}.transformer.ts"));
|
||||||
|
let symbol = format!("{}Transformer", token(name));
|
||||||
|
write_new(&file, transformer_stub(&symbol))?;
|
||||||
|
println!("✓ {}", file.display());
|
||||||
|
println!(
|
||||||
|
"→ attach it to a route: @Transformer({symbol}), imported from './transformers/{lower}.transformer'"
|
||||||
|
);
|
||||||
|
maybe_check(check)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transformer_stub(symbol: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"import type {{ TransformerFn }} from '@green-tea/core';\n\n\
|
||||||
|
/**\n * Turns what a handler returns into the response. Attach it to a route with\n\
|
||||||
|
* `@Transformer({symbol})`; routes without one use the default JSON transformer.\n\
|
||||||
|
* Set the status and headers here (`set-cookie` takes an array, one header per cookie).\n */\n\
|
||||||
|
export const {symbol}: TransformerFn = (value) => ({{\n\
|
||||||
|
status: 200,\n\
|
||||||
|
headers: {{ 'content-type': 'application/json' }},\n\
|
||||||
|
body: JSON.stringify(value),\n\
|
||||||
|
}});\n"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn maybe_check(check: bool) -> std::io::Result<()> {
|
pub(crate) fn maybe_check(check: bool) -> std::io::Result<()> {
|
||||||
if check {
|
if check {
|
||||||
match crate::check::run(Path::new(".")) {
|
match crate::check::run(Path::new(".")) {
|
||||||
|
|||||||
@@ -199,3 +199,37 @@ fn add_rejects_a_controller_path_that_does_not_exist() {
|
|||||||
.failure()
|
.failure()
|
||||||
.stderr(predicates::str::contains("no such controller"));
|
.stderr(predicates::str::contains("no such controller"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_cookie_reads_with_the_decorator_and_writes_through_a_transformer() {
|
||||||
|
let d = tempdir().unwrap();
|
||||||
|
std::fs::create_dir_all(d.path().join("src/controllers")).unwrap();
|
||||||
|
std::fs::write(
|
||||||
|
d.path().join("src/controllers/home.controller.ts"),
|
||||||
|
"import { Route, Get } from '@green-tea/core';\n\n\
|
||||||
|
@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
for _ in 0..2 {
|
||||||
|
Command::cargo_bin("matcha")
|
||||||
|
.unwrap()
|
||||||
|
.current_dir(d.path())
|
||||||
|
.args(["add", "cookie"])
|
||||||
|
.assert()
|
||||||
|
.success();
|
||||||
|
}
|
||||||
|
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
|
||||||
|
assert!(out.contains("@cookie('theme')"), "{out}");
|
||||||
|
assert!(out.contains("'set-cookie': setCookie("), "{out}");
|
||||||
|
for symbol in ["Post", "Transformer", "body", "cookie", "setCookie"] {
|
||||||
|
assert!(
|
||||||
|
out.contains(&format!("{symbol},")) || out.contains(&format!("{symbol} }}")),
|
||||||
|
"{symbol} not imported:\n{out}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
out.matches("@Post('/prefs')").count(),
|
||||||
|
1,
|
||||||
|
"not idempotent:\n{out}"
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -170,3 +170,29 @@ fn create_does_not_overwrite_an_existing_file() {
|
|||||||
.stderr(predicates::str::contains("already exists"));
|
.stderr(predicates::str::contains("already exists"));
|
||||||
assert_eq!(std::fs::read_to_string(&file).unwrap(), "// edited\n");
|
assert_eq!(std::fs::read_to_string(&file).unwrap(), "// edited\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_transformer_writes_a_typed_function_and_says_where_it_goes() {
|
||||||
|
let d = tempdir().unwrap();
|
||||||
|
seed_module(d.path());
|
||||||
|
create(d.path(), "transformer", "csv-export")
|
||||||
|
.success()
|
||||||
|
.stdout(predicates::str::contains(
|
||||||
|
"@Transformer(csvExportTransformer)",
|
||||||
|
));
|
||||||
|
let file = std::fs::read_to_string(d.path().join("src/transformers/csvexport.transformer.ts"))
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
file.contains("import type { TransformerFn } from '@green-tea/core'"),
|
||||||
|
"{file}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
file.contains("export const csvExportTransformer: TransformerFn"),
|
||||||
|
"{file}"
|
||||||
|
);
|
||||||
|
let module = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap();
|
||||||
|
assert!(
|
||||||
|
!module.contains("Transformer"),
|
||||||
|
"a transformer is not a module member:\n{module}"
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user