Skip to content

feat(server): conditional BaseContext types via typed() factory (#2004)#2294

Merged
viniciusdacal merged 4 commits into
mainfrom
viniciusdacal/fix-base-context-types
Apr 5, 2026
Merged

feat(server): conditional BaseContext types via typed() factory (#2004)#2294
viniciusdacal merged 4 commits into
mainfrom
viniciusdacal/fix-base-context-types

Conversation

@viniciusdacal
Copy link
Copy Markdown
Contributor

Summary

  • BaseContext is now generic over ContextFeatures — auth/tenancy fields only appear on ctx when the app has those features configured
  • typed(auth) factory returns narrowed entity() and service() factories where hook ctx parameters reflect the configured features
  • Zero breaking changesBaseContext without a type parameter defaults to FullFeatures (all fields present), so all existing code compiles unchanged

Public API Changes

New types

  • ContextFeatures, FullFeatures, NoFeatures — feature flag types
  • AuthContext, TenantContext, MultiLevelTenantContext — mixin interfaces (from @vertz/server/entity)
  • TypedFactories<F> — typed entity/service factory interface
  • InferFeatures<TAuth> — derives features from auth config
  • InferServerFeatures<TConfig>, InferServerContext<TConfig> — derives features from server config

New function

  • typed(auth?) — returns { entity, service } with narrowed context types

Changed signatures (backwards compatible)

  • BaseContext<TFeatures = FullFeatures> — now generic (was plain interface)
  • EntityConfig<..., TFeatures = FullFeatures> — new 4th type parameter
  • ServiceConfig<..., TFeatures = FullFeatures> — new 3rd type parameter
  • entity() / service() — accept TFeatures type parameter
  • defineAuth<T extends AuthConfig>(config: T): T — now generic (preserves literal types)
  • TenantConfig.multiLevel?: boolean — new optional field

Design doc

Test plan

  • Type-level tests for all 4 feature tiers (NoFeatures, AuthOnly, AuthTenant, FullFeatures)
  • EntityConfig hook ctx narrowing tests
  • ServiceConfig handler ctx narrowing tests
  • typed() factory type tests
  • InferFeatures + InferServerFeatures tests
  • defineAuth generic preservation tests
  • @vertz/agents BaseContextLike structural compatibility test
  • Backwards compatibility: BaseContext defaults to FullFeatures
  • EntityContext assignable to BaseContext
  • Runtime test for defineAuth identity behavior

🤖 Generated with Claude Code

viniciusdacal and others added 4 commits April 4, 2026 20:25
BaseContext is now generic over ContextFeatures. Auth/tenancy fields only
appear on ctx when configured. Use typed(auth) to get narrowed entity()
and service() factories. Existing code is unaffected — BaseContext without
a type parameter defaults to FullFeatures (all fields present).

- Add mixin interfaces: AuthContext, TenantContext, MultiLevelTenantContext
- Add ContextFeatures, FullFeatures, NoFeatures type flags
- Replace BaseContext interface with conditional type alias
- Add TFeatures to EntityConfig and ServiceConfig
- Add typed() factory with TypedFactories<F> interface
- Add InferFeatures, InferServerFeatures, InferServerContext utilities
- Make defineAuth generic to preserve literal types
- Add multiLevel field to TenantConfig
- Comprehensive type-level tests for all 4 feature tiers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…d.ts and service/types.ts (#2004)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@viniciusdacal viniciusdacal merged commit fcf3524 into main Apr 5, 2026
8 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 5, 2026
@github-actions github-actions Bot mentioned this pull request Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant