prisma-labs / konn

MIT License
89 stars 4 forks source link

Allow using a group of providers at once #16

Open jasonkuhrt opened 3 years ago

jasonkuhrt commented 3 years ago

Perceived Problem

const ctx = kont()
  .useBeforeAll(a())
  .useBeforeEach(b())
  .useBeforeEach(c())
  .done()

What if consumer/producer wants to be able to have a,b,c providers packages up into one group of providers?

Ideas / Proposed Solution(s)

const ctx = kont()
  .useBeforeAll(a(), b(), c())
  .done()
const group = [a(), b(), c()]

const ctx = kont()
  .useBeforeAll(...group)
  .done()
const ctx = kont()
  .useBeforeAll([a(), b(), c()])
  .done()
const group = [a(), b(), c()]

const ctx = kont()
  .useBeforeAll(group)
  .done()
jasonkuhrt commented 3 years ago
const group = [a(), b(), c()]

const ctx = konn()
  .useBeforeAll(group)
  .done()

A problem with this is when downstream depends on upstream, then those types are lost in the group definition.

To fix we need a function helper.

const abc = join(a(), b(), c())

const ctx = konn()
  .useBeforeAll(abc)
  .done()
jasonkuhrt commented 3 years ago

What if consumer wants to reuse repetitive parts of konn chain?

const ctx = konn()
  .useBeforeAll(a())
  .useBeforeAll(b())
  .useBeforeEach(c())
  .done()

Ideas

const abc = konn()
  .useBeforeAll(a())
  .useBeforeAll(b())
  .useBeforeEach(c())

// maybe another file, maybe multiple
const ctx = konn().use(abc).done()

.use would take a callback to get the callback to be contravariant to the host ctx type:

const ctx = konn().use(thing).use({ _use: (ctx) => /* ... */ }).done()

This would require the konn chain to have a _use helper that would provide the needed static type state.

Alternative using a new API group:

const abc = group()
  .useBeforeAll(a())
  .useBeforeAll(b())
  .useBeforeEach(c())
  .done()

// maybe another file, maybe multiple
const ctx = konn().use(abc).done()
jasonkuhrt commented 3 years ago

Maybe different function name, providersGroup instead of join:

const abc = providersGroup(a(), b(), c())

const ctx = konn()
  .useBeforeAll(abc)
  .done()