ucanto
ucanto copied to clipboard
allow passing a `context` into service handlers
At the moment only way to access external things from capability handler is either:
- Binding it via
handler.bind(context)which is verbose and not ideal. - By defining service as a class and instantiating it with context e.g.
new Service(context).
Both have drawbacks. Ideally it would be something along these lines instead:
Server.create({
service,
context,
decoder: CAR,
encoder: CBOR,
capability
})
However above may prove tricky since handlers would not know the type of the context until it's bound. Another alternative could be something like this:
const service = {
init(): Promise<Context>
store: {
add: (invocation:Invocation<Cap>, context:Context) => {
// ...
}
}
}
Later might make inference easier.
It may also be worth considering how we could integrate capability parsers into the system, such that they could be leveraged instead of having to redefine all the input types e.g. something like this would be pretty reasonable:
const read = capability({
can: "file/read"
with: URI({ protocol: "file:" })
}).handler(invocation => {
))
const register = capability({
can: "identity/register",
with: URI()
}).handler(invocation => {
...
})
const service = read.or(register)
const sever = Server.create({
service: read.or(register)
decoder: CAR,
encoder: CBOR
})