feat(add,create): matcha add cookie and matcha create transformer
CI / installer (macos-14) (pull_request) Successful in 3s
CI / check (pull_request) Successful in 27s
CI / starter (bun) (pull_request) Failing after 31s
CI / starter (deno) (pull_request) Failing after 32s
CI / starter (edge) (pull_request) Failing after 37s
CI / core-freshness (pull_request) Has been skipped
CI / installer (ubuntu-latest) (pull_request) Successful in 9s
CI / starter (node) (pull_request) Failing after 32s
CI / installer (macos-14) (pull_request) Successful in 3s
CI / check (pull_request) Successful in 27s
CI / starter (bun) (pull_request) Failing after 31s
CI / starter (deno) (pull_request) Failing after 32s
CI / starter (edge) (pull_request) Failing after 37s
CI / core-freshness (pull_request) Has been skipped
CI / installer (ubuntu-latest) (pull_request) Successful in 9s
CI / starter (node) (pull_request) Failing after 32s
add cookie reads a cookie with @cookie and sets one through a @Transformer and setCookie, since core sends Set-Cookie only as a header. create transformer writes a typed TransformerFn and prints how to attach it; there is nothing to wire into a module. starter-smoke now covers both. add cookie uses @cookie and setCookie, which are new in core RC.1, so this lands together with the CORE_VERSION bump. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+133
-2
No files matched your search
@@ -139,6 +139,39 @@ const CAPABILITIES: &[Capability] = &[
|
||||
marker: "'/upload'",
|
||||
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.
|
||||
|
||||
+32
-1
@@ -56,7 +56,7 @@ pub fn kinds() -> Vec<&'static str> {
|
||||
["module"]
|
||||
.into_iter()
|
||||
.chain(PIECES.iter().map(|p| p.kind))
|
||||
.chain(["plugin"])
|
||||
.chain(["transformer", "plugin"])
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -121,6 +121,9 @@ pub fn run(kind: &str, name: &str, check: bool) -> std::io::Result<()> {
|
||||
if kind == "module" {
|
||||
return create_module(name, check);
|
||||
}
|
||||
if kind == "transformer" {
|
||||
return create_transformer(name, check);
|
||||
}
|
||||
match PIECES.iter().find(|p| p.kind == kind) {
|
||||
Some(piece) => create_piece(name, check, piece),
|
||||
None => Err(Error::new(
|
||||
@@ -200,6 +203,34 @@ fn create_module(name: &str, check: bool) -> std::io::Result<()> {
|
||||
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<()> {
|
||||
if check {
|
||||
match crate::check::run(Path::new(".")) {
|
||||
|
||||
Reference in new issue
Block a user