Repository: withastro/astro
File: packages/astro/src/types/public/config.ts (TypeScript)

Code before:
	options?: Record<string, unknown>;
}

interface TestSessionConfig extends CommonSessionConfig {
	driver: 'test';
	options: {
		mockStorage: Storage;
	};
}

export type SessionConfig<TDriver extends SessionDriverName> =
	TDriver extends keyof BuiltinDriverOptions
		? BuiltinSessionConfig<TDriver>
		: TDriver extends 'test'
			? TestSessionConfig
			: CustomSessionConfig;

export type ResolvedSessionConfig<TDriver extends SessionDriverName> = SessionConfig<TDriver> & {
	driverModule?: () => Promise<{ default: () => Driver }>;
};

export interface ViteUserConfig extends OriginalViteUserConfig {
	ssr?: ViteSSROptions;
}

export type CspAlgorithm = 'SHA-256' | 'SHA-384' | 'SHA-512';

// NOTE(fks): We choose to keep our hand-generated AstroUserConfig interface so that
// we can add JSDoc-style documentation and link to the definition file in our repo.
// However, Zod comes with the ability to auto-generate AstroConfig from the schema
// above. If we ever get to the point where we no longer need the dedicated type,
// consider replacing it with the following lines:
// export interface AstroUserConfig extends z.input<typeof AstroConfigSchema> {
// }

/**
 * Astro User Config
 * Docs: https://docs.astro.build/reference/configuration-reference/
 *
 * Generics do not follow semver and may change at any time.
 */ export interface AstroUserConfig<
	TLocales extends Locales = never,
	TSession extends SessionDriverName = never,
	TFontFamilies extends FontFamily[] = never,
> {
	/**
	 * @docs
	 * @kind heading
	 * @name Top-Level Options
	 */


Review comment (refactor):
What you could do to keep a single source of truth is:
- extract the algorithm schema from the config
- use z.infer for this type

Code after:
interface CustomSessionConfig extends CommonSessionConfig {
	/** Entrypoint for a custom session driver */
	driver: string;
	options?: Record<string, unknown>;
}

interface TestSessionConfig extends CommonSessionConfig {
	driver: 'test';
	options: {
		mockStorage: Storage;
	};
}

export type SessionConfig<TDriver extends SessionDriverName> =
	TDriver extends keyof BuiltinDriverOptions
		? BuiltinSessionConfig<TDriver>
		: TDriver extends 'test'
			? TestSessionConfig
			: CustomSessionConfig;

export type ResolvedSessionConfig<TDriver extends SessionDriverName> = SessionConfig<TDriver> & {
	driverModule?: () => Promise<{ default: () => Driver }>;
};

export interface ViteUserConfig extends OriginalViteUserConfig {
	ssr?: ViteSSROptions;
}

// NOTE(fks): We choose to keep our hand-generated AstroUserConfig interface so that
// we can add JSDoc-style documentation and link to the definition file in our repo.
// However, Zod comes with the ability to auto-generate AstroConfig from the schema
// above. If we ever get to the point where we no longer need the dedicated type,
// consider replacing it with the following lines:
// export interface AstroUserConfig extends z.input<typeof AstroConfigSchema> {
// }

/**
 * Astro User Config
 * Docs: https://docs.astro.build/reference/configuration-reference/
 *
 * Generics do not follow semver and may change at any time.
 */ export interface AstroUserConfig<
	TLocales extends Locales = never,
	TSession extends SessionDriverName = never,
	TFontFamilies extends FontFamily[] = never,
> {
	/**
	 * @docs
	 * @kind heading
	 * @name Top-Level Options
	 */