Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

Generate a typed client

Check handler names, arguments and results at compile time, from your luna types in TypeScript and from a generated file in Swift.

A call that names its handler as a string and its arguments as JSON compiles even when a name or a type is wrong. The mistake shows up only when the call fails, and a typed client moves it to compile time.

Get types in TypeScript

A TypeScript app needs no generated file. Import the luna type as a type and pass it to client.luna.

import type { List } from "../luna/lunas/list.ts";
import { client } from "./luna.ts";

const list = client.luna<typeof List>("List", "groceries");
await list.mutate("add", { text: "milk" });
const items = await list.read("all");

TypeScript now checks the handler name, each argument and the result’s type. A misspelled { txt: "milk" } fails to compile. The type import is erased, so none of the luna’s code reaches the app. React’s useLuna<typeof List> works the same way.

Generate a Swift client

luna generate swift reads your luna types and writes LunaClients.swift. Run it in the project folder.

luna generate swift --out app/Sources/ListApp/LunaClients.swift
Option Does
--dir The app’s folder: where luna.config.ts is, or the folder whose luna folder has it. The current folder by default
--out The file to write. LunaClients.swift in the app’s folder by default

The command loads your luna types on your machine and does not deploy. It rewrites the file only when its content changes, so run it again after each change to a handler.

Call through the generated file

The file adds a method to LunaClient for each public luna type, named after the type and taking its key fields. Each handler becomes a method with labelled arguments.

let list = client.list(id: "groceries")
try await list.add(text: "milk")
let all = list.all()
Handler Becomes
A query handler A method that returns a LiveQuery
Any other handler An async throws method with timeout and optimistic, and an enqueue method such as enqueueAdd
A push An on method such as onJoined
A stream A method that returns a StreamReader, such as logStream

Optional arguments default to nil. A handler whose name or arguments are not valid Swift names gets a comment instead, and you call it through handle. Each type has a handle property with the untyped LunaHandle underneath. Private luna types and indexes are left out.

Type the results

Without returns, a handler’s result is JSON in Swift. Give the handler a returns type and the file declares a Swift type for it.

all: h(
  { query: true, returns: t.array(t.object({ id: t.int(), text: t.string() })) },
  (ctx) => ctx.db.items.find({ orderBy: "id" }),
),

After luna generate swift runs again, list.all() returns LiveQuery<[ListLuna.AllResultItem]>. Objects become structs and enums become Swift enums, each nested in the luna type’s struct. An enum with a value that is not a valid Swift name stays a String.

Type in t Swift type
t.string(), t.uuid() String
t.int(), t.timestamp() Int
t.float() Double
t.bool() Bool
t.bytes() Data
t.json() JSON
t.array(...) An array
.optional() An optional

Next steps

Was this page helpful?