N02
Error
Requests
N02 — A resource over a write
A resource asks whenever the page draws it and whenever what it reads changes. A write — a POST, PUT, PATCH or DELETE — is sent when something happens, from an action, once.
What the compiler said
error[N02]: `resource made` reads `Backend.create`, a `POST`: a write is sent when something happens, not whenever the page draws --> src/App.wf:8:5 | 8 | resource made = Backend.create(title: "Milk") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: Call it from an action: `action save() { match await Backend.create(…) { .ok(v) { … } .err(e) { … } } }` = docs: https://webfluent.monzeromer.dev/errors/N02
The fix
Broken
type Todo { id: String, title: String }
api Backend(base: "/api") {
post create(title: String) at "todos" -> Todo!
}
page P(path: "/", title: "T", description: "D") {
resource made = Backend.create(title: "Milk")
Heading("New").h1
match made {
.loaded(t) { Text(t.title) }
else { Spinner }
}
}
Fixed
type Todo { id: String, title: String }
api Backend(base: "/api") {
post create(title: String) at "todos" -> Todo!
}
page P(path: "/", title: "T", description: "D") {
state said = ""
Heading("New").h1
Button("Add milk") {
on click {
match await Backend.create(title: "Milk") {
.ok(todo) { said = todo.title }
.err(e) { said = e.message }
}
}
}
Text(said)
}
Call the write from a handler or an action, and match what it
returns. If the page shows what the write changed, read it with a get,
and say on the write that it invalidates that read.
Each example is a whole program: it is all of src/App.wf in a project with no other .wf file (trying an example).
Why this rule exists
Error.
A resource asks whenever the page draws it, and again whenever what it
reads changes or the page comes back into focus. That is right for a read,
which changes nothing on the server. A write — a POST, PUT, PATCH or
DELETE — would then be sent again each time: a second todo, a second
order. A write is sent once, when something happens, from an action or a
handler.
.update(change), which shows a change before the server agrees, is a
read's for the same reason: it changes what a resource over the read shows.
lints cannot lower it: it would ship a broken page.