getContext()
Environment: server.
getContext() 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
Before you can use getContext(), you must provide the context object to your Telefunc server middleware, see:
For example:
- Hono
- Express.js
// server/index.js
// Environment: server
// Server entry (Hono)
import { Hono } from 'hono'
import { telefunc } from 'telefunc'
const app = new Hono()
app.all('/_telefunc', async (c) => {
const httpResponse = await telefunc({
request: c.req.raw,
// Provide the context object here:
context: {
user: c.get('user')
}
})
return new Response(httpResponse.body, {
status: httpResponse.statusCode,
headers: { 'content-type': httpResponse.contentType }
})
})
// Some authentication middleware — sets the user on Hono's context
app.use(async (c, next) => {
const userId = c.req.cookie('user-id')
const user = await getUserFromDatabase(userId)
// Or using a third-party authentication provider (e.g. Auth0):
const user = await authProviderApi.getUser(c.req.headers)
// Authentication middlewares usually provide information
// about the logged-in user on the context object:
c.set('user', user)
await next()
})// server/index.ts
// Environment: server
// Server entry (Hono)
import { Hono } from 'hono'
import { telefunc } from 'telefunc'
const app = new Hono()
app.all('/_telefunc', async (c) => {
const httpResponse = await telefunc({
request: c.req.raw,
// Provide the context object here:
context: {
user: c.get('user')
}
})
return new Response(httpResponse.body, {
status: httpResponse.statusCode,
headers: { 'content-type': httpResponse.contentType }
})
})
// Some authentication middleware — sets the user on Hono's context
app.use(async (c, next) => {
const userId = c.req.cookie('user-id')
const user = await getUserFromDatabase(userId)
// Or using a third-party authentication provider (e.g. Auth0):
const user = await authProviderApi.getUser(c.req.headers)
// Authentication middlewares usually provide information
// about the logged-in user on the context object:
c.set('user', user)
await next()
})See also:
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 Telefunc.Context to globally set the type of const context = getContext():
// TelefuncContext.d.ts
import 'telefunc'
import type { User } from './User.ts'
declare module 'telefunc' {
namespace Telefunc {
interface Context {
user: null | User
}
}
}// 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>()
}