Edit

provideTelefuncContext()

Environment: server.

provideTelefuncContext() makes the context object available to telefunctions (via getContext()) — it's useful when a telefunction runs outside Telefunc's server middleware and you therefore cannot pass the context to serve({ context }), such as Server-Side Rendering (SSR) or tests.

// Environment: server
 
import { provideTelefuncContext } from 'telefunc'
 
const context = { userId: 42 }
// provideTelefuncContext() returns the value returned by onLoadTodoItems()
const { todoItems } = await provideTelefuncContext(context, () => {
  // Inside the onLoadTodoItems() telefunction, getContext() returns `context`.
  return onLoadTodoItems()
})

With new Telefunc() or serve(), the context is automatically provided to telefunctions — see API > getContext() > Provide. Reach for provideTelefuncContext() only when a telefunction runs outside both.

See also