Skip to content

Commit b5bc43b

Browse files
Copilothotlong
andcommitted
Fix TypeScript compilation errors - add Input types for Zod schemas
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 0e6395b commit b5bc43b

4 files changed

Lines changed: 42 additions & 35 deletions

File tree

packages/core/src/api-registry-plugin.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ObjectKernel } from './kernel';
33
import { createApiRegistryPlugin } from './api-registry-plugin';
44
import { ApiRegistry } from './api-registry';
55
import type { Plugin } from './types';
6-
import type { ApiRegistryEntry } from '@objectstack/spec/api';
6+
import type { ApiRegistryEntryInput } from '@objectstack/spec/api';
77

88
describe('API Registry Plugin', () => {
99
let kernel: ObjectKernel;
@@ -49,7 +49,7 @@ describe('API Registry Plugin', () => {
4949
init: async (ctx) => {
5050
const registry = ctx.getService<ApiRegistry>('api-registry');
5151

52-
const api: ApiRegistryEntry = {
52+
const api: ApiRegistryEntryInput = {
5353
id: 'test_api',
5454
name: 'Test API',
5555
type: 'rest',

packages/core/src/api-registry.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect, beforeEach, vi } from 'vitest';
22
import { ApiRegistry } from './api-registry';
33
import type {
4-
ApiRegistryEntry,
4+
ApiRegistryEntryInput,
55
} from '@objectstack/spec/api';
66
import type { Logger } from '@objectstack/spec/contracts';
77

@@ -40,7 +40,7 @@ describe('ApiRegistry', () => {
4040

4141
describe('registerApi', () => {
4242
it('should register a simple REST API', () => {
43-
const api: ApiRegistryEntry = {
43+
const api: ApiRegistryEntryInput = {
4444
id: 'customer_api',
4545
name: 'Customer API',
4646
type: 'rest',
@@ -71,7 +71,7 @@ describe('ApiRegistry', () => {
7171
});
7272

7373
it('should throw error when registering duplicate API', () => {
74-
const api: ApiRegistryEntry = {
74+
const api: ApiRegistryEntryInput = {
7575
id: 'test_api',
7676
name: 'Test API',
7777
type: 'rest',
@@ -88,7 +88,7 @@ describe('ApiRegistry', () => {
8888
});
8989

9090
it('should register API with multiple endpoints', () => {
91-
const api: ApiRegistryEntry = {
91+
const api: ApiRegistryEntryInput = {
9292
id: 'crud_api',
9393
name: 'CRUD API',
9494
type: 'rest',
@@ -135,7 +135,7 @@ describe('ApiRegistry', () => {
135135
});
136136

137137
it('should register API with RBAC permissions', () => {
138-
const api: ApiRegistryEntry = {
138+
const api: ApiRegistryEntryInput = {
139139
id: 'protected_api',
140140
name: 'Protected API',
141141
type: 'rest',
@@ -162,7 +162,7 @@ describe('ApiRegistry', () => {
162162

163163
describe('unregisterApi', () => {
164164
it('should unregister an API', () => {
165-
const api: ApiRegistryEntry = {
165+
const api: ApiRegistryEntryInput = {
166166
id: 'temp_api',
167167
name: 'Temporary API',
168168
type: 'rest',
@@ -195,7 +195,7 @@ describe('ApiRegistry', () => {
195195
describe('Route Conflict Detection', () => {
196196
describe('error strategy', () => {
197197
it('should throw error on route conflict', () => {
198-
const api1: ApiRegistryEntry = {
198+
const api1: ApiRegistryEntryInput = {
199199
id: 'api1',
200200
name: 'API 1',
201201
type: 'rest',
@@ -211,7 +211,7 @@ describe('ApiRegistry', () => {
211211
],
212212
};
213213

214-
const api2: ApiRegistryEntry = {
214+
const api2: ApiRegistryEntryInput = {
215215
id: 'api2',
216216
name: 'API 2',
217217
type: 'rest',
@@ -232,7 +232,7 @@ describe('ApiRegistry', () => {
232232
});
233233

234234
it('should allow same path with different methods', () => {
235-
const api: ApiRegistryEntry = {
235+
const api: ApiRegistryEntryInput = {
236236
id: 'multi_method',
237237
name: 'Multi Method API',
238238
type: 'rest',
@@ -271,7 +271,7 @@ describe('ApiRegistry', () => {
271271
});
272272

273273
it('should prefer higher priority endpoint', () => {
274-
const api1: ApiRegistryEntry = {
274+
const api1: ApiRegistryEntryInput = {
275275
id: 'low_priority',
276276
name: 'Low Priority API',
277277
type: 'rest',
@@ -288,7 +288,7 @@ describe('ApiRegistry', () => {
288288
],
289289
};
290290

291-
const api2: ApiRegistryEntry = {
291+
const api2: ApiRegistryEntryInput = {
292292
id: 'high_priority',
293293
name: 'High Priority API',
294294
type: 'rest',
@@ -314,7 +314,7 @@ describe('ApiRegistry', () => {
314314
});
315315

316316
it('should keep higher priority when registering lower priority', () => {
317-
const api1: ApiRegistryEntry = {
317+
const api1: ApiRegistryEntryInput = {
318318
id: 'high_priority',
319319
name: 'High Priority API',
320320
type: 'rest',
@@ -331,7 +331,7 @@ describe('ApiRegistry', () => {
331331
],
332332
};
333333

334-
const api2: ApiRegistryEntry = {
334+
const api2: ApiRegistryEntryInput = {
335335
id: 'low_priority',
336336
name: 'Low Priority API',
337337
type: 'rest',
@@ -363,7 +363,7 @@ describe('ApiRegistry', () => {
363363
});
364364

365365
it('should keep first registered endpoint', () => {
366-
const api1: ApiRegistryEntry = {
366+
const api1: ApiRegistryEntryInput = {
367367
id: 'first',
368368
name: 'First API',
369369
type: 'rest',
@@ -379,7 +379,7 @@ describe('ApiRegistry', () => {
379379
],
380380
};
381381

382-
const api2: ApiRegistryEntry = {
382+
const api2: ApiRegistryEntryInput = {
383383
id: 'second',
384384
name: 'Second API',
385385
type: 'rest',
@@ -410,7 +410,7 @@ describe('ApiRegistry', () => {
410410
});
411411

412412
it('should use last registered endpoint', () => {
413-
const api1: ApiRegistryEntry = {
413+
const api1: ApiRegistryEntryInput = {
414414
id: 'first',
415415
name: 'First API',
416416
type: 'rest',
@@ -426,7 +426,7 @@ describe('ApiRegistry', () => {
426426
],
427427
};
428428

429-
const api2: ApiRegistryEntry = {
429+
const api2: ApiRegistryEntryInput = {
430430
id: 'second',
431431
name: 'Second API',
432432
type: 'rest',
@@ -543,7 +543,7 @@ describe('ApiRegistry', () => {
543543

544544
describe('getEndpoint', () => {
545545
it('should get endpoint by API and endpoint ID', () => {
546-
const api: ApiRegistryEntry = {
546+
const api: ApiRegistryEntryInput = {
547547
id: 'test_api',
548548
name: 'Test API',
549549
type: 'rest',
@@ -575,7 +575,7 @@ describe('ApiRegistry', () => {
575575

576576
describe('findEndpointByRoute', () => {
577577
it('should find endpoint by method and path', () => {
578-
const api: ApiRegistryEntry = {
578+
const api: ApiRegistryEntryInput = {
579579
id: 'route_api',
580580
name: 'Route API',
581581
type: 'rest',
@@ -721,7 +721,7 @@ describe('ApiRegistry', () => {
721721

722722
describe('Multi-protocol Support', () => {
723723
it('should register GraphQL API', () => {
724-
const api: ApiRegistryEntry = {
724+
const api: ApiRegistryEntryInput = {
725725
id: 'graphql',
726726
name: 'GraphQL API',
727727
type: 'graphql',
@@ -742,7 +742,7 @@ describe('ApiRegistry', () => {
742742
});
743743

744744
it('should register WebSocket API', () => {
745-
const api: ApiRegistryEntry = {
745+
const api: ApiRegistryEntryInput = {
746746
id: 'websocket',
747747
name: 'WebSocket API',
748748
type: 'websocket',
@@ -769,7 +769,7 @@ describe('ApiRegistry', () => {
769769
});
770770

771771
it('should register Plugin API', () => {
772-
const api: ApiRegistryEntry = {
772+
const api: ApiRegistryEntryInput = {
773773
id: 'custom_plugin',
774774
name: 'Custom Plugin API',
775775
type: 'plugin',

packages/core/src/api-registry.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type {
22
ApiRegistry as ApiRegistryType,
33
ApiRegistryEntry,
4+
ApiRegistryEntryInput,
45
ApiEndpointRegistration,
5-
ApiProtocolType,
66
ConflictResolutionStrategy,
77
ApiDiscoveryQuery,
88
ApiDiscoveryResponse,
@@ -74,30 +74,33 @@ export class ApiRegistry {
7474
* @param api - API registry entry
7575
* @throws Error if API already registered or route conflicts detected
7676
*/
77-
registerApi(api: ApiRegistryEntry): void {
77+
registerApi(api: ApiRegistryEntryInput): void {
7878
// Check if API already exists
7979
if (this.apis.has(api.id)) {
8080
throw new Error(`[ApiRegistry] API '${api.id}' already registered`);
8181
}
8282

83+
// Cast to full type after validation
84+
const fullApi = api as ApiRegistryEntry;
85+
8386
// Validate and register endpoints
84-
for (const endpoint of api.endpoints) {
85-
this.validateEndpoint(endpoint, api.id);
87+
for (const endpoint of fullApi.endpoints) {
88+
this.validateEndpoint(endpoint, fullApi.id);
8689
}
8790

8891
// Register the API
89-
this.apis.set(api.id, api);
92+
this.apis.set(fullApi.id, fullApi);
9093

9194
// Register endpoints
92-
for (const endpoint of api.endpoints) {
93-
this.registerEndpoint(api.id, endpoint);
95+
for (const endpoint of fullApi.endpoints) {
96+
this.registerEndpoint(fullApi.id, endpoint);
9497
}
9598

9699
this.updatedAt = new Date().toISOString();
97-
this.logger.info(`API registered: ${api.id}`, {
98-
api: api.id,
99-
type: api.type,
100-
endpointCount: api.endpoints.length,
100+
this.logger.info(`API registered: ${fullApi.id}`, {
101+
api: fullApi.id,
102+
type: fullApi.type,
103+
endpointCount: fullApi.endpoints.length,
101104
});
102105
}
103106

packages/spec/src/api/registry.zod.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export const ApiResponseSchema = z.object({
266266
});
267267

268268
export type ApiResponse = z.infer<typeof ApiResponseSchema>;
269+
export type ApiResponseInput = z.input<typeof ApiResponseSchema>;
269270

270271
/**
271272
* API Endpoint Registration Schema
@@ -514,6 +515,7 @@ export const ApiEndpointRegistrationSchema = z.object({
514515
});
515516

516517
export type ApiEndpointRegistration = z.infer<typeof ApiEndpointRegistrationSchema>;
518+
export type ApiEndpointRegistrationInput = z.input<typeof ApiEndpointRegistrationSchema>;
517519

518520
// ==========================================
519521
// API Registry Entry
@@ -543,6 +545,7 @@ export const ApiMetadataSchema = z.object({
543545
});
544546

545547
export type ApiMetadata = z.infer<typeof ApiMetadataSchema>;
548+
export type ApiMetadataInput = z.input<typeof ApiMetadataSchema>;
546549

547550
/**
548551
* API Registry Entry Schema
@@ -629,6 +632,7 @@ export const ApiRegistryEntrySchema = z.object({
629632
});
630633

631634
export type ApiRegistryEntry = z.infer<typeof ApiRegistryEntrySchema>;
635+
export type ApiRegistryEntryInput = z.input<typeof ApiRegistryEntrySchema>;
632636

633637
// ==========================================
634638
// API Registry

0 commit comments

Comments
 (0)