ucanto icon indicating copy to clipboard operation
ucanto copied to clipboard

allow passing a `context` into service handlers

Open Gozala opened this issue 3 years ago • 1 comments

At the moment only way to access external things from capability handler is either:

  1. Binding it via handler.bind(context) which is verbose and not ideal.
  2. 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.

Gozala avatar Jun 14 '22 03:06 Gozala

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
})

Gozala avatar Jun 14 '22 03:06 Gozala