-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice-registry.zod.ts
More file actions
258 lines (223 loc) · 7.04 KB
/
service-registry.zod.ts
File metadata and controls
258 lines (223 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Service Registry Protocol
*
* Zod schemas for service registry data structures.
* These schemas align with the IServiceRegistry contract interface.
*
* Following ObjectStack "Zod First" principle - all data structures
* must have Zod schemas for runtime validation and JSON Schema generation.
*
* Note: IServiceRegistry itself is a runtime interface (methods only),
* so it correctly remains a TypeScript interface. This file contains
* schemas for configuration and metadata related to service registry.
*/
// ============================================================================
// Service Metadata Schemas
// ============================================================================
/**
* Service Scope Type Enum
* Different service scoping strategies
*/
export const ServiceScopeType = z.enum([
'singleton', // Single instance shared across the application
'transient', // New instance created each time
'scoped', // Instance per scope (request, session, transaction, etc.)
]).describe('Service scope type');
export type ServiceScopeType = z.infer<typeof ServiceScopeType>;
/**
* Service Metadata Schema
* Metadata about a registered service
*
* @example
* {
* "name": "database",
* "scope": "singleton",
* "type": "IDataEngine",
* "registeredAt": 1706659200000
* }
*/
export const ServiceMetadataSchema = z.object({
/**
* Service name (unique identifier)
*/
name: z.string().min(1).describe('Unique service name identifier'),
/**
* Service scope type
*/
scope: ServiceScopeType.optional().default('singleton')
.describe('Service scope type'),
/**
* Service type or interface name (optional)
*/
type: z.string().optional().describe('Service type or interface name'),
/**
* Registration timestamp (Unix milliseconds)
*/
registeredAt: z.number().int().optional()
.describe('Unix timestamp in milliseconds when service was registered'),
/**
* Additional metadata
*/
metadata: z.record(z.string(), z.unknown()).optional()
.describe('Additional service-specific metadata'),
});
export type ServiceMetadata = z.infer<typeof ServiceMetadataSchema>;
// ============================================================================
// Service Registry Configuration Schemas
// ============================================================================
/**
* Service Registry Configuration Schema
* Configuration for service registry behavior
*
* @example
* {
* "strictMode": true,
* "allowOverwrite": false,
* "enableLogging": true,
* "scopeTypes": ["singleton", "transient", "request", "session"]
* }
*/
export const ServiceRegistryConfigSchema = z.object({
/**
* Strict mode: throw errors on invalid operations
* @default true
*/
strictMode: z.boolean().optional().default(true)
.describe('Throw errors on invalid operations (duplicate registration, service not found, etc.)'),
/**
* Allow overwriting existing services
* @default false
*/
allowOverwrite: z.boolean().optional().default(false)
.describe('Allow overwriting existing service registrations'),
/**
* Enable logging for service operations
* @default false
*/
enableLogging: z.boolean().optional().default(false)
.describe('Enable logging for service registration and retrieval'),
/**
* Custom scope types (beyond singleton, transient, scoped)
* @default ['singleton', 'transient', 'scoped']
*/
scopeTypes: z.array(z.string()).optional()
.describe('Supported scope types'),
/**
* Maximum number of services (prevent memory leaks)
*/
maxServices: z.number().int().min(1).optional()
.describe('Maximum number of services that can be registered'),
});
export type ServiceRegistryConfig = z.infer<typeof ServiceRegistryConfigSchema>;
export type ServiceRegistryConfigInput = z.input<typeof ServiceRegistryConfigSchema>;
// ============================================================================
// Service Factory Schemas
// ============================================================================
/**
* Service Factory Registration Schema
* Configuration for registering a service factory
*
* @example
* {
* "name": "logger",
* "scope": "singleton",
* "factoryType": "sync"
* }
*/
export const ServiceFactoryRegistrationSchema = z.object({
/**
* Service name (unique identifier)
*/
name: z.string().min(1).describe('Unique service name identifier'),
/**
* Service scope type
*/
scope: ServiceScopeType.optional().default('singleton')
.describe('Service scope type'),
/**
* Factory type (sync or async)
*/
factoryType: z.enum(['sync', 'async']).optional().default('sync')
.describe('Whether factory is synchronous or asynchronous'),
/**
* Whether this is a singleton (cache factory result)
*/
singleton: z.boolean().optional().default(true)
.describe('Whether to cache the factory result (singleton pattern)'),
});
export type ServiceFactoryRegistration = z.infer<typeof ServiceFactoryRegistrationSchema>;
// ============================================================================
// Scoped Service Schemas
// ============================================================================
/**
* Scope Configuration Schema
* Configuration for creating a new scope
*
* @example
* {
* "scopeType": "request",
* "scopeId": "req-12345",
* "metadata": {
* "userId": "user-123",
* "requestId": "req-12345"
* }
* }
*/
export const ScopeConfigSchema = z.object({
/**
* Type of scope (request, session, transaction, etc.)
*/
scopeType: z.string().describe('Type of scope'),
/**
* Scope identifier (optional, auto-generated if not provided)
*/
scopeId: z.string().optional().describe('Unique scope identifier'),
/**
* Scope metadata (context information)
*/
metadata: z.record(z.string(), z.unknown()).optional()
.describe('Scope-specific context metadata'),
});
export type ScopeConfig = z.infer<typeof ScopeConfigSchema>;
/**
* Scope Info Schema
* Information about an active scope
*
* @example
* {
* "scopeId": "req-12345",
* "scopeType": "request",
* "createdAt": 1706659200000,
* "serviceCount": 5,
* "metadata": {
* "userId": "user-123"
* }
* }
*/
export const ScopeInfoSchema = z.object({
/**
* Scope identifier
*/
scopeId: z.string().describe('Unique scope identifier'),
/**
* Type of scope
*/
scopeType: z.string().describe('Type of scope'),
/**
* Creation timestamp (Unix milliseconds)
*/
createdAt: z.number().int().describe('Unix timestamp in milliseconds when scope was created'),
/**
* Number of services in this scope
*/
serviceCount: z.number().int().min(0).optional()
.describe('Number of services registered in this scope'),
/**
* Scope metadata
*/
metadata: z.record(z.string(), z.unknown()).optional()
.describe('Scope-specific context metadata'),
});
export type ScopeInfo = z.infer<typeof ScopeInfoSchema>;