Skip to content

Commit a0db351

Browse files
Copilothotlong
andcommitted
Improve type safety - replace unsafe type assertions with proper interfaces
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 8211d64 commit a0db351

3 files changed

Lines changed: 38 additions & 23 deletions

File tree

packages/foundation/core/src/formula-plugin.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import type { RuntimePlugin, RuntimeContext, ObjectStackKernel } from '@objectst
1010
import { FormulaEngine } from './formula-engine';
1111
import type { FormulaEngineConfig } from '@objectql/types';
1212

13+
/**
14+
* Extended ObjectStack Kernel with formula engine capability
15+
*/
16+
interface KernelWithFormulas extends ObjectStackKernel {
17+
formulaEngine?: FormulaEngine;
18+
}
19+
1320
/**
1421
* Configuration for the Formula Plugin
1522
*/
@@ -49,12 +56,12 @@ export class FormulaPlugin implements RuntimePlugin {
4956
* Registers formula evaluation capabilities
5057
*/
5158
async install(ctx: RuntimeContext): Promise<void> {
52-
const kernel = ctx.engine as ObjectStackKernel;
59+
const kernel = ctx.engine as KernelWithFormulas;
5360

5461
console.log(`[${this.name}] Installing formula plugin...`);
5562

5663
// Make formula engine accessible from the kernel for direct usage
57-
(kernel as any).formulaEngine = this.engine;
64+
kernel.formulaEngine = this.engine;
5865

5966
// Register formula provider if the kernel supports it
6067
this.registerFormulaProvider(kernel);
@@ -71,13 +78,10 @@ export class FormulaPlugin implements RuntimePlugin {
7178
* Register the formula provider with the kernel
7279
* @private
7380
*/
74-
private registerFormulaProvider(kernel: ObjectStackKernel): void {
81+
private registerFormulaProvider(kernel: KernelWithFormulas): void {
7582
// Check if kernel supports formula provider registration
76-
// Note: Using type assertion since registerFormulaProvider may not be in the interface
77-
const kernelWithFormulas = kernel as any;
78-
79-
if (typeof kernelWithFormulas.registerFormulaProvider === 'function') {
80-
kernelWithFormulas.registerFormulaProvider({
83+
if (typeof (kernel as any).registerFormulaProvider === 'function') {
84+
(kernel as any).registerFormulaProvider({
8185
evaluate: (formula: string, context: any) => {
8286
// Delegate to the formula engine
8387
// Note: In a real implementation, we would need to properly construct
@@ -96,24 +100,19 @@ export class FormulaPlugin implements RuntimePlugin {
96100
return this.engine.extractMetadata(fieldName, expression, dataType);
97101
}
98102
});
99-
} else {
100-
// If the kernel doesn't support formula provider registration yet,
101-
// we still register the engine for direct access
102-
kernelWithFormulas.formulaEngine = this.engine;
103103
}
104+
// Note: formulaEngine is already registered in install() method above
104105
}
105106

106107
/**
107108
* Register formula evaluation middleware
108109
* @private
109110
*/
110-
private registerFormulaMiddleware(kernel: ObjectStackKernel): void {
111+
private registerFormulaMiddleware(kernel: KernelWithFormulas): void {
111112
// Check if kernel supports middleware hooks
112-
const kernelWithHooks = kernel as any;
113-
114-
if (typeof kernelWithHooks.use === 'function') {
113+
if (typeof (kernel as any).use === 'function') {
115114
// Register middleware to evaluate formulas after queries
116-
kernelWithHooks.use('afterQuery', async (context: any) => {
115+
(kernel as any).use('afterQuery', async (context: any) => {
117116
// Formula evaluation logic would go here
118117
// This would automatically compute formula fields after data is retrieved
119118
if (context.results && context.metadata?.fields) {

packages/foundation/core/src/repository.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import { Validator } from './validator';
1313
import { FormulaEngine } from './formula-engine';
1414
import { QueryBuilder } from './query';
1515

16+
/**
17+
* Extended ObjectStack Kernel with optional ObjectQL plugin capabilities.
18+
* These properties are attached by ValidatorPlugin and FormulaPlugin during installation.
19+
*/
20+
interface ExtendedKernel extends ObjectStackKernel {
21+
validator?: Validator;
22+
formulaEngine?: FormulaEngine;
23+
}
24+
1625
export class ObjectRepository {
1726
private queryBuilder: QueryBuilder;
1827

@@ -29,7 +38,7 @@ export class ObjectRepository {
2938
* Falls back to creating a new instance if not available
3039
*/
3140
private getValidator(): Validator {
32-
const kernel = this.getKernel() as any;
41+
const kernel = this.getKernel() as ExtendedKernel;
3342
if (kernel.validator) {
3443
return kernel.validator;
3544
}
@@ -42,7 +51,7 @@ export class ObjectRepository {
4251
* Falls back to creating a new instance if not available
4352
*/
4453
private getFormulaEngine(): FormulaEngine {
45-
const kernel = this.getKernel() as any;
54+
const kernel = this.getKernel() as ExtendedKernel;
4655
if (kernel.formulaEngine) {
4756
return kernel.formulaEngine;
4857
}

packages/foundation/core/src/validator-plugin.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
import type { RuntimePlugin, RuntimeContext, ObjectStackKernel } from '@objectstack/runtime';
1010
import { Validator, ValidatorOptions } from './validator';
1111

12+
/**
13+
* Extended ObjectStack Kernel with validator capability
14+
*/
15+
interface KernelWithValidator extends ObjectStackKernel {
16+
validator?: Validator;
17+
}
18+
1219
/**
1320
* Configuration for the Validator Plugin
1421
*/
@@ -58,12 +65,12 @@ export class ValidatorPlugin implements RuntimePlugin {
5865
* Registers validation middleware for queries and mutations
5966
*/
6067
async install(ctx: RuntimeContext): Promise<void> {
61-
const kernel = ctx.engine as ObjectStackKernel;
68+
const kernel = ctx.engine as KernelWithValidator;
6269

6370
console.log(`[${this.name}] Installing validator plugin...`);
6471

6572
// Make validator accessible from the kernel for direct usage
66-
(kernel as any).validator = this.validator;
73+
kernel.validator = this.validator;
6774

6875
// Register validation middleware for queries (if enabled)
6976
if (this.config.enableQueryValidation !== false) {
@@ -82,7 +89,7 @@ export class ValidatorPlugin implements RuntimePlugin {
8289
* Register query validation middleware
8390
* @private
8491
*/
85-
private registerQueryValidation(kernel: ObjectStackKernel): void {
92+
private registerQueryValidation(kernel: KernelWithValidator): void {
8693
// Check if kernel supports middleware hooks
8794
if (typeof (kernel as any).use === 'function') {
8895
(kernel as any).use('beforeQuery', async (context: any) => {
@@ -104,7 +111,7 @@ export class ValidatorPlugin implements RuntimePlugin {
104111
* Register mutation validation middleware
105112
* @private
106113
*/
107-
private registerMutationValidation(kernel: ObjectStackKernel): void {
114+
private registerMutationValidation(kernel: KernelWithValidator): void {
108115
// Check if kernel supports middleware hooks
109116
if (typeof (kernel as any).use === 'function') {
110117
(kernel as any).use('beforeMutation', async (context: any) => {

0 commit comments

Comments
 (0)