mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
* chore: Rename APIContext to Context and flatten its fields Auth and transaction now live directly on the context instead of in ctx.state and a nested ctx.context, and model helpers read them directly. APIContext remains as a deprecated alias. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove deprecated APIContext alias Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * fix: Read auth from context in OAuth provider callbacks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Add withUser and withTransaction context helpers Replaces manual createContext rebuilds that copy fields from an existing context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove arbitrary property access from Koa context types AppContext and Context no longer inherit the any index signature from Koa's DefaultContext, custom fields are declared explicitly, and importing Koa's loose Context type is disallowed by lint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf --------- Co-authored-by: Claude <noreply@anthropic.com>
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import type { Transaction } from "sequelize";
|
|
import type { User } from "@server/models";
|
|
import type { AppContext, Authentication, Context } from "@server/types";
|
|
import { AuthenticationType } from "@server/types";
|
|
|
|
/** The fields of a context that model hooks read from query options. */
|
|
export interface HookContextFields {
|
|
/** The authentication for the actor performing the mutation. */
|
|
auth: Authentication;
|
|
/** The transaction the mutation runs in. */
|
|
transaction?: Transaction;
|
|
/** The IP address of the actor. */
|
|
ip?: string;
|
|
}
|
|
|
|
/**
|
|
* Factory to create a new context.
|
|
*
|
|
* @param options.user the actor for the context.
|
|
* @param options.authType the type of authentication used by the actor.
|
|
* @param options.ip the IP address of the actor, defaults to the last active IP.
|
|
* @param options.transaction the transaction for database mutations.
|
|
* @returns the context.
|
|
*/
|
|
export function createContext({
|
|
user,
|
|
authType = AuthenticationType.APP,
|
|
ip,
|
|
transaction,
|
|
}: {
|
|
user?: User;
|
|
authType?: AuthenticationType | null;
|
|
ip?: string | null;
|
|
transaction?: Transaction;
|
|
}) {
|
|
return {
|
|
auth: { user, type: authType },
|
|
ip: ip ?? user?.lastActiveIp,
|
|
transaction,
|
|
} as Context;
|
|
}
|
|
|
|
/**
|
|
* Derives a context from an existing one that acts as the given user.
|
|
*
|
|
* @param ctx the context to derive from.
|
|
* @param user the actor for the new context.
|
|
* @returns the derived context.
|
|
*/
|
|
export function withUser(ctx: AppContext | Context, user?: User) {
|
|
return createContext({
|
|
user,
|
|
authType: ctx.auth?.type,
|
|
ip: ctx.ip,
|
|
transaction: ctx.transaction,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Derives a context from an existing one that runs in the given transaction.
|
|
*
|
|
* @param ctx the context to derive from.
|
|
* @param transaction the transaction for the new context.
|
|
* @returns the derived context.
|
|
*/
|
|
export function withTransaction(ctx: Context, transaction: Transaction) {
|
|
return { auth: ctx.auth, ip: ctx.ip, transaction } as Context;
|
|
}
|
|
|
|
/**
|
|
* Extracts the fields that model hooks read from a context, for use as query options.
|
|
*
|
|
* @param ctx the context to read from.
|
|
* @returns the hook context fields.
|
|
*/
|
|
export function toHookContext(ctx: Context): HookContextFields {
|
|
return {
|
|
auth: ctx.auth,
|
|
transaction: ctx.transaction,
|
|
ip: ctx.ip,
|
|
};
|
|
}
|