Context

Learn how to add custom properties to the app context accessible across all procedures.

The app context in Kottster is designed to share of common information across your app's procedures.

By default, the context includes:

  • stage: "production" or "development".

  • user: An object representing the current user.

Interface of an app context: https://github.com/kottster/kottster/blob/main/packages/common/lib/models/appContext.model.ts

Usage

Example if using the context in procedures

// File: src/server/procedures/getHello.js

import { t } from '../trpc';

const getHello = t.procedure
  .query(async ({ ctx }) => {
    const { user } = ctx; // Accessing the property from the context
    return `hello, user #${user.id}`;
  });

export default getHello;
// File: src/server/procedures/createTicket.js

import { t } from '../trpc';

const createTicket = t.procedure
  .mutation(async ({ ctx }) => {
    const { stage } = ctx;
    
    if (stage === 'production) {
        // Create a new message in Slack channel
        // ...
    }

    // Something else
  });

export default createTicket;

Last updated