-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocumentation.zod.ts
More file actions
594 lines (496 loc) · 18.2 KB
/
documentation.zod.ts
File metadata and controls
594 lines (496 loc) · 18.2 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* API Documentation & Testing Interface Protocol
*
* Provides schemas for generating interactive API documentation and testing
* interfaces similar to Swagger UI, GraphQL Playground, Postman, etc.
*
* Features:
* - OpenAPI/Swagger specification generation
* - Interactive API testing playground
* - API versioning and changelog
* - Code generation templates
* - Mock server configuration
*
* Architecture Alignment:
* - Swagger UI: Interactive API documentation
* - Postman: API testing collections
* - GraphQL Playground: GraphQL-specific testing
* - Redoc: Documentation rendering
*
* @example Documentation Config
* ```typescript
* const docConfig: ApiDocumentationConfig = {
* enabled: true,
* title: 'ObjectStack API',
* version: '1.0.0',
* servers: [{ url: 'https://api.example.com', description: 'Production' }],
* ui: {
* type: 'swagger-ui',
* theme: 'light',
* enableTryItOut: true
* }
* }
* ```
*/
// ==========================================
// OpenAPI Specification
// ==========================================
/**
* OpenAPI Server Schema
*
* Server configuration for OpenAPI specification.
*/
export const OpenApiServerSchema = z.object({
/** Server URL */
url: z.string().url().describe('Server base URL'),
/** Server description */
description: z.string().optional().describe('Server description'),
/** Server variables */
variables: z.record(z.string(), z.object({
default: z.string(),
description: z.string().optional(),
enum: z.array(z.string()).optional(),
})).optional().describe('URL template variables'),
});
export type OpenApiServer = z.infer<typeof OpenApiServerSchema>;
/**
* OpenAPI Security Scheme Schema
*
* Security scheme definition for OpenAPI.
*/
export const OpenApiSecuritySchemeSchema = z.object({
/** Security scheme type */
type: z.enum(['apiKey', 'http', 'oauth2', 'openIdConnect']).describe('Security type'),
/** Scheme name */
scheme: z.string().optional().describe('HTTP auth scheme (bearer, basic, etc.)'),
/** Bearer format */
bearerFormat: z.string().optional().describe('Bearer token format (e.g., JWT)'),
/** API key name */
name: z.string().optional().describe('API key parameter name'),
/** API key location */
in: z.enum(['header', 'query', 'cookie']).optional().describe('API key location'),
/** OAuth flows */
flows: z.object({
implicit: z.unknown().optional(),
password: z.unknown().optional(),
clientCredentials: z.unknown().optional(),
authorizationCode: z.unknown().optional(),
}).optional().describe('OAuth2 flows'),
/** OpenID Connect URL */
openIdConnectUrl: z.string().url().optional().describe('OpenID Connect discovery URL'),
/** Description */
description: z.string().optional().describe('Security scheme description'),
});
export type OpenApiSecurityScheme = z.infer<typeof OpenApiSecuritySchemeSchema>;
/**
* OpenAPI Specification Schema
*
* Complete OpenAPI 3.0 specification structure.
*
* @see https://swagger.io/specification/
*
* @example
* ```json
* {
* "openapi": "3.0.0",
* "info": {
* "title": "ObjectStack API",
* "version": "1.0.0",
* "description": "ObjectStack unified API"
* },
* "servers": [
* { "url": "https://api.example.com" }
* ],
* "paths": { ... },
* "components": { ... }
* }
* ```
*/
export const OpenApiSpecSchema = z.object({
/** OpenAPI version */
openapi: z.string().default('3.0.0').describe('OpenAPI specification version'),
/** API information */
info: z.object({
title: z.string().describe('API title'),
version: z.string().describe('API version'),
description: z.string().optional().describe('API description'),
termsOfService: z.string().url().optional().describe('Terms of service URL'),
contact: z.object({
name: z.string().optional(),
url: z.string().url().optional(),
email: z.string().email().optional(),
}).optional(),
license: z.object({
name: z.string(),
url: z.string().url().optional(),
}).optional(),
}).describe('API metadata'),
/** Servers */
servers: z.array(OpenApiServerSchema).optional().default([]).describe('API servers'),
/** API paths */
paths: z.record(z.string(), z.unknown()).describe('API paths and operations'),
/** Reusable components */
components: z.object({
schemas: z.record(z.string(), z.unknown()).optional(),
responses: z.record(z.string(), z.unknown()).optional(),
parameters: z.record(z.string(), z.unknown()).optional(),
examples: z.record(z.string(), z.unknown()).optional(),
requestBodies: z.record(z.string(), z.unknown()).optional(),
headers: z.record(z.string(), z.unknown()).optional(),
securitySchemes: z.record(z.string(), OpenApiSecuritySchemeSchema).optional(),
links: z.record(z.string(), z.unknown()).optional(),
callbacks: z.record(z.string(), z.unknown()).optional(),
}).optional().describe('Reusable components'),
/** Security requirements */
security: z.array(z.record(z.string(), z.array(z.string()))).optional()
.describe('Global security requirements'),
/** Tags */
tags: z.array(z.object({
name: z.string(),
description: z.string().optional(),
externalDocs: z.object({
description: z.string().optional(),
url: z.string().url(),
}).optional(),
})).optional().describe('Tag definitions'),
/** External documentation */
externalDocs: z.object({
description: z.string().optional(),
url: z.string().url(),
}).optional().describe('External documentation'),
});
export type OpenApiSpec = z.infer<typeof OpenApiSpecSchema>;
// ==========================================
// API Testing Playground
// ==========================================
/**
* API Testing UI Type
*/
export const ApiTestingUiType = z.enum([
'swagger-ui', // Swagger UI
'redoc', // Redoc
'rapidoc', // RapiDoc
'stoplight', // Stoplight Elements
'scalar', // Scalar API Reference
'graphql-playground', // GraphQL Playground
'graphiql', // GraphiQL
'postman', // Postman-like interface
'custom', // Custom implementation
]);
export type ApiTestingUiType = z.infer<typeof ApiTestingUiType>;
/**
* API Testing UI Configuration Schema
*
* Configuration for interactive API testing interface.
*
* @example Swagger UI Config
* ```json
* {
* "type": "swagger-ui",
* "path": "/api-docs",
* "theme": "light",
* "enableTryItOut": true,
* "enableFilter": true,
* "enableCors": true,
* "defaultModelsExpandDepth": 1
* }
* ```
*/
export const ApiTestingUiConfigSchema = z.object({
/** UI type */
type: ApiTestingUiType.describe('Testing UI implementation'),
/** UI path */
path: z.string().default('/api-docs').describe('URL path for documentation UI'),
/** UI theme */
theme: z.enum(['light', 'dark', 'auto']).default('light').describe('UI color theme'),
/** Enable try-it-out feature */
enableTryItOut: z.boolean().default(true).describe('Enable interactive API testing'),
/** Enable filtering */
enableFilter: z.boolean().default(true).describe('Enable endpoint filtering'),
/** Enable CORS for testing */
enableCors: z.boolean().default(true).describe('Enable CORS for browser testing'),
/** Default expand depth for models */
defaultModelsExpandDepth: z.number().int().min(-1).default(1)
.describe('Default expand depth for schemas (-1 = fully expand)'),
/** Display request duration */
displayRequestDuration: z.boolean().default(true).describe('Show request duration'),
/** Syntax highlighting */
syntaxHighlighting: z.boolean().default(true).describe('Enable syntax highlighting'),
/** Custom CSS URL */
customCssUrl: z.string().url().optional().describe('Custom CSS stylesheet URL'),
/** Custom JavaScript URL */
customJsUrl: z.string().url().optional().describe('Custom JavaScript URL'),
/** Layout options */
layout: z.object({
showExtensions: z.boolean().default(false).describe('Show vendor extensions'),
showCommonExtensions: z.boolean().default(false).describe('Show common extensions'),
deepLinking: z.boolean().default(true).describe('Enable deep linking'),
displayOperationId: z.boolean().default(false).describe('Display operation IDs'),
defaultModelRendering: z.enum(['example', 'model']).default('example')
.describe('Default model rendering mode'),
defaultModelsExpandDepth: z.number().int().default(1).describe('Models expand depth'),
defaultModelExpandDepth: z.number().int().default(1).describe('Single model expand depth'),
docExpansion: z.enum(['list', 'full', 'none']).default('list')
.describe('Documentation expansion mode'),
}).optional().describe('Layout configuration'),
});
export type ApiTestingUiConfig = z.infer<typeof ApiTestingUiConfigSchema>;
/**
* API Test Request Schema
*
* Represents a saved/example API test request.
*
* @example
* ```json
* {
* "name": "Get Customer by ID",
* "description": "Retrieves a customer record",
* "method": "GET",
* "url": "/api/v1/data/customer/123",
* "headers": {
* "Authorization": "Bearer {{token}}"
* },
* "variables": {
* "token": "sample_token"
* }
* }
* ```
*/
export const ApiTestRequestSchema = z.object({
/** Request name */
name: z.string().describe('Test request name'),
/** Request description */
description: z.string().optional().describe('Request description'),
/** HTTP method */
method: z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'])
.describe('HTTP method'),
/** Request URL */
url: z.string().describe('Request URL (can include variables)'),
/** Request headers */
headers: z.record(z.string(), z.string()).optional().default({})
.describe('Request headers'),
/** Query parameters */
queryParams: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))
.optional().default({}).describe('Query parameters'),
/** Request body */
body: z.unknown().optional().describe('Request body'),
/** Environment variables */
variables: z.record(z.string(), z.unknown()).optional().default({})
.describe('Template variables'),
/** Expected response */
expectedResponse: z.object({
statusCode: z.number().int(),
body: z.unknown().optional(),
}).optional().describe('Expected response for validation'),
});
export type ApiTestRequest = z.infer<typeof ApiTestRequestSchema>;
/**
* API Test Collection Schema
*
* Collection of test requests (similar to Postman collections).
*
* @example
* ```json
* {
* "name": "Customer API Tests",
* "description": "Test collection for customer endpoints",
* "variables": {
* "baseUrl": "https://api.example.com",
* "apiKey": "test_key"
* },
* "requests": [...]
* }
* ```
*/
export const ApiTestCollectionSchema = z.object({
/** Collection name */
name: z.string().describe('Collection name'),
/** Collection description */
description: z.string().optional().describe('Collection description'),
/** Collection variables */
variables: z.record(z.string(), z.unknown()).optional().default({})
.describe('Shared variables'),
/** Test requests */
requests: z.array(ApiTestRequestSchema).describe('Test requests in this collection'),
/** Folders/grouping */
folders: z.array(z.object({
name: z.string(),
description: z.string().optional(),
requests: z.array(ApiTestRequestSchema),
})).optional().describe('Request folders for organization'),
});
export type ApiTestCollection = z.infer<typeof ApiTestCollectionSchema>;
// ==========================================
// API Documentation Configuration
// ==========================================
/**
* API Changelog Entry Schema
*
* Documents changes in API versions.
*/
export const ApiChangelogEntrySchema = z.object({
/** Version */
version: z.string().describe('API version'),
/** Release date */
date: z.string().date().describe('Release date'),
/** Changes */
changes: z.object({
added: z.array(z.string()).optional().default([]).describe('New features'),
changed: z.array(z.string()).optional().default([]).describe('Changes'),
deprecated: z.array(z.string()).optional().default([]).describe('Deprecations'),
removed: z.array(z.string()).optional().default([]).describe('Removed features'),
fixed: z.array(z.string()).optional().default([]).describe('Bug fixes'),
security: z.array(z.string()).optional().default([]).describe('Security fixes'),
}).describe('Version changes'),
/** Migration guide */
migrationGuide: z.string().optional().describe('Migration guide URL or text'),
});
export type ApiChangelogEntry = z.infer<typeof ApiChangelogEntrySchema>;
/**
* Code Generation Template Schema
*
* Templates for generating client code.
*/
export const CodeGenerationTemplateSchema = z.object({
/** Language/framework */
language: z.string().describe('Target language/framework (e.g., typescript, python, curl)'),
/** Template name */
name: z.string().describe('Template name'),
/** Template content */
template: z.string().describe('Code template with placeholders'),
/** Template variables */
variables: z.array(z.string()).optional().describe('Required template variables'),
});
export type CodeGenerationTemplate = z.infer<typeof CodeGenerationTemplateSchema>;
/**
* API Documentation Configuration Schema
*
* Complete configuration for API documentation and testing interface.
*
* @example
* ```json
* {
* "enabled": true,
* "title": "ObjectStack API Documentation",
* "version": "1.0.0",
* "description": "Unified API for ObjectStack platform",
* "servers": [
* { "url": "https://api.example.com", "description": "Production" }
* ],
* "ui": {
* "type": "swagger-ui",
* "theme": "light",
* "enableTryItOut": true
* },
* "generateOpenApi": true,
* "generateTestCollections": true
* }
* ```
*/
export const ApiDocumentationConfigSchema = z.object({
/** Enable documentation */
enabled: z.boolean().default(true).describe('Enable API documentation'),
/** Documentation title */
title: z.string().default('API Documentation').describe('Documentation title'),
/** API version */
version: z.string().describe('API version'),
/** API description */
description: z.string().optional().describe('API description'),
/** Server configurations */
servers: z.array(OpenApiServerSchema).optional().default([])
.describe('API server URLs'),
/** UI configuration */
ui: ApiTestingUiConfigSchema.optional().describe('Testing UI configuration'),
/** Generate OpenAPI spec */
generateOpenApi: z.boolean().default(true).describe('Generate OpenAPI 3.0 specification'),
/** Generate test collections */
generateTestCollections: z.boolean().default(true)
.describe('Generate API test collections'),
/** Test collections */
testCollections: z.array(ApiTestCollectionSchema).optional().default([])
.describe('Predefined test collections'),
/** API changelog */
changelog: z.array(ApiChangelogEntrySchema).optional().default([])
.describe('API version changelog'),
/** Code generation templates */
codeTemplates: z.array(CodeGenerationTemplateSchema).optional().default([])
.describe('Code generation templates'),
/** Terms of service */
termsOfService: z.string().url().optional().describe('Terms of service URL'),
/** Contact information */
contact: z.object({
name: z.string().optional(),
url: z.string().url().optional(),
email: z.string().email().optional(),
}).optional().describe('Contact information'),
/** License */
license: z.object({
name: z.string(),
url: z.string().url().optional(),
}).optional().describe('API license'),
/** External documentation */
externalDocs: z.object({
description: z.string().optional(),
url: z.string().url(),
}).optional().describe('External documentation link'),
/** Security schemes */
securitySchemes: z.record(z.string(), OpenApiSecuritySchemeSchema).optional()
.describe('Security scheme definitions'),
/** Global tags */
tags: z.array(z.object({
name: z.string(),
description: z.string().optional(),
externalDocs: z.object({
description: z.string().optional(),
url: z.string().url(),
}).optional(),
})).optional().describe('Global tag definitions'),
});
export type ApiDocumentationConfig = z.infer<typeof ApiDocumentationConfigSchema>;
// ==========================================
// API Documentation Generation
// ==========================================
/**
* Generated API Documentation Schema
*
* Output of documentation generation process.
*/
export const GeneratedApiDocumentationSchema = z.object({
/** OpenAPI specification */
openApiSpec: OpenApiSpecSchema.optional().describe('Generated OpenAPI specification'),
/** Test collections */
testCollections: z.array(ApiTestCollectionSchema).optional()
.describe('Generated test collections'),
/** Markdown documentation */
markdown: z.string().optional().describe('Generated markdown documentation'),
/** HTML documentation */
html: z.string().optional().describe('Generated HTML documentation'),
/** Generation timestamp */
generatedAt: z.string().datetime().describe('Generation timestamp'),
/** Source APIs */
sourceApis: z.array(z.string()).describe('Source API IDs used for generation'),
});
export type GeneratedApiDocumentation = z.infer<typeof GeneratedApiDocumentationSchema>;
// ==========================================
// Helper Functions
// ==========================================
/**
* Helper to create API documentation config
*/
export const ApiDocumentationConfig = Object.assign(ApiDocumentationConfigSchema, {
create: <T extends z.input<typeof ApiDocumentationConfigSchema>>(config: T) => config,
});
/**
* Helper to create API test collection
*/
export const ApiTestCollection = Object.assign(ApiTestCollectionSchema, {
create: <T extends z.input<typeof ApiTestCollectionSchema>>(config: T) => config,
});
/**
* Helper to create OpenAPI specification
*/
export const OpenApiSpec = Object.assign(OpenApiSpecSchema, {
create: <T extends z.input<typeof OpenApiSpecSchema>>(config: T) => config,
});