Repository: directus/directus
File: api/src/permissions/modules/validate-access/lib/validate-item-access.ts (TypeScript)

Code before:
import { fetchPermissions } from '../../../lib/fetch-permissions.js';
import { fetchPolicies } from '../../../lib/fetch-policies.js';
import type { Context } from '../../../types.js';
import { fetchAllowedFields } from '../../fetch-allowed-fields/fetch-allowed-fields.js';
import { injectCases } from '../../process-ast/lib/inject-cases.js';
import { processAst } from '../../process-ast/process-ast.js';

export interface ValidateItemAccessOptions {
	accountability: Accountability;
	action: PermissionsAction;
	collection: string;
	primaryKeys: PrimaryKey[];
	fields?: string[];
	returnAllowedRootFields?: boolean;
}

export interface ValidateItemAccessOptionsWithFields extends ValidateItemAccessOptions {
	returnAllowedRootFields: true;
}

export interface ValidateItemAccessResult {
	accessAllowed: boolean;
	allowedRootFields?: string[];
}

export interface ValidateItemAccessResultWithFields extends ValidateItemAccessResult {
	allowedRootFields: string[];
}

export async function validateItemAccess(
	options: ValidateItemAccessOptionsWithFields,
	context: Context,
): Promise<ValidateItemAccessResultWithFields>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions,
	context: Context,
): Promise<ValidateItemAccessResult>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions | ValidateItemAccessOptionsWithFields,
	context: Context,
): Promise<ValidateItemAccessResult | ValidateItemAccessResultWithFields> {
	const primaryKeyField = context.schema.collections[options.collection]?.primary;

	if (!primaryKeyField) {
		throw new Error(`Cannot find primary key for collection "${options.collection}"`);
	}

	// When we're looking up access to specific items, we have to read them from the database to
	// make sure you are allowed to access them.

Review comment (suggestion):
```suggestion
export interface ValidateItemAccessResultWithRootFields extends ValidateItemAccessResult {
```

Code after:
import { fetchPermissions } from '../../../lib/fetch-permissions.js';
import { fetchPolicies } from '../../../lib/fetch-policies.js';
import type { Context } from '../../../types.js';
import { fetchAllowedFields } from '../../fetch-allowed-fields/fetch-allowed-fields.js';
import { injectCases } from '../../process-ast/lib/inject-cases.js';
import { processAst } from '../../process-ast/process-ast.js';

export interface ValidateItemAccessOptions {
	accountability: Accountability;
	action: PermissionsAction;
	collection: string;
	primaryKeys: PrimaryKey[];
	fields?: string[];
	returnAllowedRootFields?: boolean;
}

export interface ValidateItemAccessOptionsWithRootFields extends ValidateItemAccessOptions {
	returnAllowedRootFields: true;
}

export interface ValidateItemAccessResult {
	accessAllowed: boolean;
	allowedRootFields?: string[];
}

export interface ValidateItemAccessResultWithRootFields extends ValidateItemAccessResult {
	allowedRootFields: string[];
}

export async function validateItemAccess(
	options: ValidateItemAccessOptionsWithRootFields,
	context: Context,
): Promise<ValidateItemAccessResultWithRootFields>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions,
	context: Context,
): Promise<ValidateItemAccessResult>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions | ValidateItemAccessOptionsWithRootFields,
	context: Context,
): Promise<ValidateItemAccessResult | ValidateItemAccessResultWithRootFields> {
	const primaryKeyField = context.schema.collections[options.collection]?.primary;

	if (!primaryKeyField) {
		throw new Error(`Cannot find primary key for collection "${options.collection}"`);
	}

	// When we're looking up access to specific items, we have to read them from the database to
	// make sure you are allowed to access them.