Bijection library plus a package bijection client swift generates for your app. The generated package holds a ref for every
exposed function, typed by its arguments and value, and a connect that pins
the app contract. It runs on macOS 13 and iOS 16 or later.
Install
Generate the app’s client package and add it to your Swift package. It depends on theBijection runtime, which SwiftPM resolves with it:
Package.swift
Generate a client
--name is the Swift package and module name. The command reads the app’s
exposed tree and refuses when the committed bijection.lock is stale: run
bijection check --write-lock and commit the lock first. Generate again when
the app’s exposed functions change.
The package has:
App, the tree of refs:App.Orders.placeis the function atorders.place. Only functions withallowappear.- a struct for each object argument and value, with its codec, and a
String-backed enum for each set of string literals; contract, thecontractline ofbijection.lock, andconnect, which pins it.
Connect
token for a fixed credential, or tokenSource (a TokenSource) to be
asked for the current token on every call and live connection. Leave both out
to call anonymously.
The generated connect sends the pinned contract with every call. A
installation whose contract differs refuses every call with Conflict, and the
message starts with ContractStale: generate the client again against the
deployed app.
For a dynamic tool, pass .init(contract: .discover) to the raw connector. A pinned client refuses a path absent from its contract map before sending it.
Calls
query runs a query once. mutate runs a mutation as one transaction. A
function without arguments takes .init().
Every mutation has an identity, once. Leave it out and the client mints one
for the call. When a mutation answers Unknown, its reply was lost and it may
have committed: call it again with the same once. The same once with the
same arguments answers the original result, and with other arguments it
refuses Invalid.
An uncertain mutation carries its original request key in Refusal.once.
Frames
A frame method returns a new client and leaves the original unchanged:
A committed mutation carries its
version. Pass it as seen to a later
query, watch, mutation or trial to answer from state at least that new:
Results
Every verb answersBijection.Result<V>: .ok(value, version:) or
.refusal(Refusal). A refusal has a code, on a host code its reason (the
precise cause, such as BudgetExhausted under Limit), a message the
caller may read and, on Forbidden, the guard that refused. Result here is
Bijection.Result: qualify it where Swift.Result is also in scope.
HostCode.unknown, .disallowed,
.forbidden, .invalid, .absent, .exists, .conflict, .erased,
.expired, .withdrawn, .limit, .failed) or one of the codes the
function declares in refuses. Any other code from the host is folded into
its host code, and the message keeps the original code. result.get()
returns the value or throws the refusal.
Watch
watch runs a query and keeps its value current. It is an AsyncSequence
of results:
live.stop() to end it. A client in another world or
at another instant reads once and ends. The live connection is the
installation’s HTTP event stream through URLSession on iOS and macOS
alike; ConnectOptions.live takes another.
Trial
trial runs a call in a discarded world and commits nothing:
writes lists each write the call would make (verb, table, key,
before, after), and tasks each task it would submit (target, args,
due). validUntil is the instant the answer is valid until (a call at the same
version committed before it is decided the same way), or nil when nothing
time-dependent bounds it.
Values
A duration with years or months has no fixed length and decodes as an
Invalid refusal. An untyped call takes and answers Values:
client.query(QueryRef("notes.list"), .object([:])).
Change entries
A query that returnsselection.changes answers Change<Fact> entries:
key, before (nil on an insert), after (nil on a delete) and the
change’s version.
iOS
An iOS app uses the sameBijection package and the generated connect:
there is no separate mobile package. To sign in through an identity provider,
implement AuthProvider and pass SignIn(provider:) as tokenSource;
login(), loginFromCache(), logout() and authState drive it. SignIn
answers Swift.Result; the client’s own is Bijection.Result, so qualify
one of them where both are in scope.