-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexternal-lookup.zod.ts
More file actions
247 lines (224 loc) · 6.18 KB
/
external-lookup.zod.ts
File metadata and controls
247 lines (224 loc) · 6.18 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
import { z } from 'zod';
import { FieldMappingSchema as BaseFieldMappingSchema } from '../shared/mapping.zod';
/**
* External Data Source Schema
*
* Configuration for connecting to external data systems.
* Similar to Salesforce External Objects for real-time data integration.
*
* @example
* ```json
* {
* "id": "salesforce-accounts",
* "name": "Salesforce Account Data",
* "type": "rest-api",
* "endpoint": "https://api.salesforce.com/services/data/v58.0",
* "authentication": {
* "type": "oauth2",
* "config": {
* "clientId": "...",
* "clientSecret": "...",
* "tokenUrl": "https://login.salesforce.com/services/oauth2/token"
* }
* }
* }
* ```
*/
export const ExternalDataSourceSchema = z.object({
/**
* Unique identifier for the external data source
*/
id: z.string().describe('Data source ID'),
/**
* Human-readable name of the data source
*/
name: z.string().describe('Data source name'),
/**
* Protocol type for connecting to the data source
*/
type: z.enum(['odata', 'rest-api', 'graphql', 'custom']).describe('Protocol type'),
/**
* Base URL endpoint for the external system
*/
endpoint: z.string().url().describe('API endpoint URL'),
/**
* Authentication configuration
*/
authentication: z.object({
/**
* Authentication method
*/
type: z.enum(['oauth2', 'api-key', 'basic', 'none']).describe('Auth type'),
/**
* Authentication-specific configuration
* Structure varies based on auth type
*/
config: z.record(z.string(), z.unknown()).describe('Auth configuration'),
}).describe('Authentication'),
});
/**
* Field Mapping Schema for External Lookups
*
* Extends the base field mapping with external lookup specific features.
* Uses the canonical field mapping protocol from shared/mapping.zod.ts.
*
* @see {@link BaseFieldMappingSchema} for the base field mapping schema
*
* @example
* ```json
* {
* "source": "AccountName",
* "target": "name",
* "readonly": true
* }
* ```
*/
export const ExternalFieldMappingSchema = BaseFieldMappingSchema.extend({
/**
* Field data type
*/
type: z.string().optional().describe('Field type'),
/**
* Whether the field is read-only
* @default true
*/
readonly: z.boolean().optional().default(true).describe('Read-only field'),
});
/**
* External Lookup Schema
*
* Real-time data lookup protocol for external systems.
* Enables querying external data sources without replication.
* Inspired by Salesforce External Objects and OData protocols.
*
* @example
* ```json
* {
* "fieldName": "external_account",
* "dataSource": {
* "id": "salesforce-api",
* "name": "Salesforce",
* "type": "rest-api",
* "endpoint": "https://api.salesforce.com/services/data/v58.0",
* "authentication": {
* "type": "oauth2",
* "config": {"clientId": "..."}
* }
* },
* "query": {
* "endpoint": "/sobjects/Account",
* "method": "GET",
* "parameters": {"limit": 100}
* },
* "fieldMappings": [
* {
* "externalField": "Name",
* "localField": "account_name",
* "type": "text",
* "readonly": true
* }
* ],
* "caching": {
* "enabled": true,
* "ttl": 300,
* "strategy": "ttl"
* },
* "fallback": {
* "enabled": true,
* "showError": true
* },
* "rateLimit": {
* "requestsPerSecond": 10,
* "burstSize": 20
* }
* }
* ```
*/
export const ExternalLookupSchema = z.object({
/**
* Name of the field that uses external lookup
*/
fieldName: z.string().describe('Field name'),
/**
* External data source configuration
*/
dataSource: ExternalDataSourceSchema.describe('External data source'),
/**
* Query configuration for fetching external data
*/
query: z.object({
/**
* API endpoint path (relative to base endpoint)
*/
endpoint: z.string().describe('Query endpoint path'),
/**
* HTTP method for the query
* @default 'GET'
*/
method: z.enum(['GET', 'POST']).optional().default('GET').describe('HTTP method'),
/**
* Query parameters or request body
*/
parameters: z.record(z.string(), z.unknown()).optional().describe('Query parameters'),
}).describe('Query configuration'),
/**
* Mapping between external and local fields
*/
fieldMappings: z.array(ExternalFieldMappingSchema).describe('Field mappings'),
/**
* Cache configuration for external data
*/
caching: z.object({
/**
* Whether caching is enabled
* @default true
*/
enabled: z.boolean().optional().default(true).describe('Cache enabled'),
/**
* Time-to-live in seconds
* @default 300
*/
ttl: z.number().optional().default(300).describe('Cache TTL (seconds)'),
/**
* Cache eviction strategy
* @default 'ttl'
*/
strategy: z.enum(['lru', 'lfu', 'ttl']).optional().default('ttl').describe('Cache strategy'),
}).optional().describe('Caching configuration'),
/**
* Fallback behavior when external system is unavailable
*/
fallback: z.object({
/**
* Whether fallback is enabled
* @default true
*/
enabled: z.boolean().optional().default(true).describe('Fallback enabled'),
/**
* Default value to use when external system fails
*/
defaultValue: z.unknown().optional().describe('Default fallback value'),
/**
* Whether to show error message to user
* @default true
*/
showError: z.boolean().optional().default(true).describe('Show error to user'),
}).optional().describe('Fallback configuration'),
/**
* Rate limiting to prevent overwhelming external system
*/
rateLimit: z.object({
/**
* Maximum requests per second
*/
requestsPerSecond: z.number().describe('Requests per second limit'),
/**
* Burst size for handling spikes
*/
burstSize: z.number().optional().describe('Burst size'),
}).optional().describe('Rate limiting'),
});
// Type exports
export type ExternalLookup = z.infer<typeof ExternalLookupSchema>;
export type ExternalDataSource = z.infer<typeof ExternalDataSourceSchema>;
export type ExternalFieldMapping = z.infer<typeof ExternalFieldMappingSchema>;