Edit

getContext()

Environment: server.

Enables telefunctions to access contextual information.

// TodoList.telefunc.js
// Environment: server
 
import { getContext } from 'telefunc'
 
export async function onLoad() {
  const context = getContext()
  const { user } = context
  const todoItems = await Todo.findMany({ select: 'text', authorId: user.id })
  return {
    todoItems,
    userName: user.name
  }
}
// TodoList.telefunc.ts
// Environment: server
 
import { getContext } from 'telefunc'
 
export async function onLoad() {
  const context = getContext()
  const { user } = context
  const todoItems = await Todo.findMany({ select: 'text', authorId: user.id })
  return {
    todoItems,
    userName: user.name
  }
}

It's most commonly used for implementing permissions, see Guides > Permissions.

Provide

You can add context properties when calling telefunc.serve() — see API > new Telefunc().

You can also provide context outside the server middleware (e.g. when testing) via provideTelefuncContext().

Access

If you get this error:

[telefunc][Wrong Usage][getContext()] Cannot access context object, see https://telefunc.com/getContext#access

Then this means that getContext() was called after an await operator:

// TodoList.telefunc.js
 
export async function myTelefunction() {
  await something()
  // ❌ Wrong: getContext() must be called before `await something()`
  const context = getContext()
}
// TodoList.telefunc.ts
 
export async function myTelefunction() {
  await something()
  // ❌ Wrong: getContext() must be called before `await something()`
  const context = getContext()
}

Make sure to call getContext() before any await operators:

// TodoList.telefunc.js
 
export async function myTelefunction() {
  // ✅ Good: getContext() is called before `await`
  const context = getContext()
  await something()
}
// TodoList.telefunc.ts
 
export async function myTelefunction() {
  // ✅ Good: getContext() is called before `await`
  const context = getContext()
  await something()
}

TypeScript

You can use the Telefunc.Context interface to globally set the type of const context = getContext():

// TelefuncContext.d.ts
 
import type { User } from './User.ts'
 
declare global {
  namespace Telefunc {
    interface Context {
      user: null | User
    }
  }
}
 
// Add this line if TypeScript complains about `declare global` (it requires the file
// to have at least one `import` or `export` so that TypeScript treats it as a module).
// https://typescriptlang.org/docs/handbook/2/modules.html#how-javascript-modules-are-defined
export {}
// User.ts
 
export type User = { id: number }
// *.telefunc.ts
 
import { getContext } from 'telefunc'
 
export async function someTelefunction() {
  // TypeScript knows that `user.id` is a `number`
  const { user } = getContext()
}

You can also directly set const context = getContext<MyContext>():

// *.telefunc.ts
 
import { getContext } from 'telefunc'
 
type Context = {
  userId: number
}
 
export async function someTelefunction() {
  // TypeScript knows that `userId` is a `number`
  const { userId } = getContext<Context>()
}

onClose()

The context object includes onClose(), see API > onClose() > context.onClose().

signal

The context object includes a signal (an AbortSignal), see API > onClose() > context.signal.

See also