N08

Error

Requests

N08 — A form 7.0 removed

fetch(…), an endpoint that does not say how it fails, errors { }, and a read's loading/error/ready arms, .data, .state, .refreshing and on: were how 6.x asked a service for things and read the answer. In 7.0 a request is http.get(…) or an endpoint declared -> T ! E or -> T!, whose answer is a result, and a read is one value: .loading, .failed(e) or .loaded(value). Each form has its fix.

What the compiler said

error[N08]: `resource rows = fetch(…)`: `fetch` is the browser's, and a read is `http.get(…)` --> src/App.wf:4:5 | 4 | resource rows: [Row] = fetch("/api/rows") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: Write `http.get(url, …)` — or `http.post`, `http.put` for another method — with the type it holds: `resource rows: [Row] = http.get(…)` = docs: https://webfluent.monzeromer.dev/errors/N08

Your editor offers this fix: Write http.get. wf fix --code N08 applies it to every place it fits.

The fix

Broken

wf

type Row { id: String, title: String } page P(path: "/", title: "T", description: "D") { resource rows: [Row] = fetch("/api/rows") Heading("Rows").h1 match rows { .loaded(list) { for r in list by r.id { Text(r.title) } } else { Text("…") } } }

Fixed

wf

type Row { id: String, title: String } page P(path: "/", title: "T", description: "D") { resource rows: [Row] = http.get("/api/rows") Heading("Rows").h1 match rows { .loaded(list) { for r in list by r.id { Text(r.title) } } else { Text("…") } } }

The editor's quick fix and wf fix --code N08 write http.get for fetch, -> T! for an endpoint, a read's 7.0 arms, .value, .isRefreshing, refresh:, a missing at and {name} for :name. An errors { } block, a fetch with a method: and a read's .state are rewritten by hand.

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.

Going from WebFluent 6 → 7, a service is no longer asked, nor its answers read, in these ways:

  • fetch(…), in a resource or an action. A request is now

http.get(url, …), http.post(…), http.put(…), http.patch(…), http.delete(…); in an action its answer is a result — .ok(value) or .err(e) with an HttpError — read with try or match, and its type is named where the answer goes: let rows: [Row] = try await http.get(url). window.fetch is the browser's own, untouched.

  • An endpoint that does not say how it fails: `get user(id: String) at "user" ->

User. Every call of an endpoint answers with a result, so it is declared -> User! — a User or an HttpError — or -> User ! UserError with an error enum; -> !` for one that answers with nothing.

  • errors { 422 -> T }, which typed a failure's body. A status is now

a case of an error enum, carrying its body: unprocessableContent(problem: T) on 422, beside http(error: HttpError).

  • A read's 6.x arms and members. A resource is one value:

.loading, .failed(e) or .loaded(value, refreshing, stale). The arms loading, error(e) and ready(v) are those cases; .data is .value, .refreshing is .isRefreshing, and .state is asked as .isLoading, .isLoaded or .isFailed. on: is refresh: — .onFocus, .onReconnect, .every(30.seconds), .never.

  • A service's headers { } and hooks. What touches a request is

middleware, in the order written: headers { Authorization: "Bearer {Session.token}" } is middleware [bearer(Session.token)], any other header header("Accept-Language", Session.language); on error(e) refreshing a token and returning "retry" is refreshOn401(Session.refresh), written before bearer; anything else an on request, on response or on error hook did is a middleware name(req, next) { … } of the project's, named in the list.

  • An endpoint with no at, and a path's :name. 6.x made a path from

the endpoint's name when it gave none; 7.0 asks for it, get users() at "users" -> [User]!, and names a parameter in it as OpenAPI does, at "users/{id}".

  • A group that sets how requests are sent. api Backend.users { … }

is reached through Backend and sends what it sends: its base:, timeout:, retry: and middleware are written on Backend.

lints cannot lower it: it would ship a broken page.