@demokit-ai/schema 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +461 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +459 -0
- package/dist/index.d.ts +459 -0
- package/dist/index.js +444 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for DemoKit schema representation
|
|
3
|
+
*
|
|
4
|
+
* These types represent a parsed API schema with relationship detection,
|
|
5
|
+
* independent of the source format (OpenAPI, GraphQL, etc.)
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* The main schema representation for DemoKit
|
|
9
|
+
* Contains all information needed for fixture generation
|
|
10
|
+
*/
|
|
11
|
+
interface DemokitSchema {
|
|
12
|
+
/**
|
|
13
|
+
* Metadata about the API
|
|
14
|
+
*/
|
|
15
|
+
info: SchemaInfo;
|
|
16
|
+
/**
|
|
17
|
+
* All API endpoints discovered from the spec
|
|
18
|
+
*/
|
|
19
|
+
endpoints: Endpoint[];
|
|
20
|
+
/**
|
|
21
|
+
* All data models (schemas) from the spec
|
|
22
|
+
*/
|
|
23
|
+
models: Record<string, DataModel>;
|
|
24
|
+
/**
|
|
25
|
+
* Detected relationships between models
|
|
26
|
+
*/
|
|
27
|
+
relationships: Relationship[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* API metadata
|
|
31
|
+
*/
|
|
32
|
+
interface SchemaInfo {
|
|
33
|
+
/**
|
|
34
|
+
* API title from the spec
|
|
35
|
+
*/
|
|
36
|
+
title: string;
|
|
37
|
+
/**
|
|
38
|
+
* API version
|
|
39
|
+
*/
|
|
40
|
+
version: string;
|
|
41
|
+
/**
|
|
42
|
+
* Optional description
|
|
43
|
+
*/
|
|
44
|
+
description?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Base URL for the API (if specified)
|
|
47
|
+
*/
|
|
48
|
+
baseUrl?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* An API endpoint
|
|
52
|
+
*/
|
|
53
|
+
interface Endpoint {
|
|
54
|
+
/**
|
|
55
|
+
* HTTP method (GET, POST, PUT, PATCH, DELETE)
|
|
56
|
+
*/
|
|
57
|
+
method: HttpMethod;
|
|
58
|
+
/**
|
|
59
|
+
* URL path with parameter placeholders
|
|
60
|
+
* @example "/users/{id}" or "/orders/{orderId}/items"
|
|
61
|
+
*/
|
|
62
|
+
path: string;
|
|
63
|
+
/**
|
|
64
|
+
* Operation ID from OpenAPI (if available)
|
|
65
|
+
*/
|
|
66
|
+
operationId?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Human-readable summary
|
|
69
|
+
*/
|
|
70
|
+
summary?: string;
|
|
71
|
+
/**
|
|
72
|
+
* Detailed description
|
|
73
|
+
*/
|
|
74
|
+
description?: string;
|
|
75
|
+
/**
|
|
76
|
+
* Path parameters
|
|
77
|
+
*/
|
|
78
|
+
pathParams: ParameterDef[];
|
|
79
|
+
/**
|
|
80
|
+
* Query parameters
|
|
81
|
+
*/
|
|
82
|
+
queryParams: ParameterDef[];
|
|
83
|
+
/**
|
|
84
|
+
* Request body schema (for POST/PUT/PATCH)
|
|
85
|
+
*/
|
|
86
|
+
requestBody?: RequestBody;
|
|
87
|
+
/**
|
|
88
|
+
* Response schemas by status code
|
|
89
|
+
*/
|
|
90
|
+
responses: Record<string, ResponseDef>;
|
|
91
|
+
/**
|
|
92
|
+
* Tags for grouping endpoints
|
|
93
|
+
*/
|
|
94
|
+
tags: string[];
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* HTTP methods supported
|
|
98
|
+
*/
|
|
99
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
100
|
+
/**
|
|
101
|
+
* A request or response body definition
|
|
102
|
+
*/
|
|
103
|
+
interface RequestBody {
|
|
104
|
+
/**
|
|
105
|
+
* Content type (usually application/json)
|
|
106
|
+
*/
|
|
107
|
+
contentType: string;
|
|
108
|
+
/**
|
|
109
|
+
* Reference to the model name, or inline schema
|
|
110
|
+
*/
|
|
111
|
+
schema: SchemaRef | DataModel;
|
|
112
|
+
/**
|
|
113
|
+
* Whether the body is required
|
|
114
|
+
*/
|
|
115
|
+
required: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Description of the body
|
|
118
|
+
*/
|
|
119
|
+
description?: string;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* A response definition
|
|
123
|
+
*/
|
|
124
|
+
interface ResponseDef {
|
|
125
|
+
/**
|
|
126
|
+
* HTTP status code
|
|
127
|
+
*/
|
|
128
|
+
statusCode: string;
|
|
129
|
+
/**
|
|
130
|
+
* Description of the response
|
|
131
|
+
*/
|
|
132
|
+
description?: string;
|
|
133
|
+
/**
|
|
134
|
+
* Content type to schema mapping
|
|
135
|
+
*/
|
|
136
|
+
content?: Record<string, SchemaRef | DataModel>;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A parameter definition (path or query)
|
|
140
|
+
*/
|
|
141
|
+
interface ParameterDef {
|
|
142
|
+
/**
|
|
143
|
+
* Parameter name
|
|
144
|
+
*/
|
|
145
|
+
name: string;
|
|
146
|
+
/**
|
|
147
|
+
* Where the parameter is located
|
|
148
|
+
*/
|
|
149
|
+
in: 'path' | 'query' | 'header' | 'cookie';
|
|
150
|
+
/**
|
|
151
|
+
* Whether the parameter is required
|
|
152
|
+
*/
|
|
153
|
+
required: boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Parameter type
|
|
156
|
+
*/
|
|
157
|
+
type: PropertyType;
|
|
158
|
+
/**
|
|
159
|
+
* Optional format hint
|
|
160
|
+
*/
|
|
161
|
+
format?: string;
|
|
162
|
+
/**
|
|
163
|
+
* Description
|
|
164
|
+
*/
|
|
165
|
+
description?: string;
|
|
166
|
+
/**
|
|
167
|
+
* Example value
|
|
168
|
+
*/
|
|
169
|
+
example?: unknown;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Reference to another schema/model
|
|
173
|
+
*/
|
|
174
|
+
interface SchemaRef {
|
|
175
|
+
/**
|
|
176
|
+
* The referenced model name
|
|
177
|
+
*/
|
|
178
|
+
$ref: string;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* A data model representing an object schema
|
|
182
|
+
*/
|
|
183
|
+
interface DataModel {
|
|
184
|
+
/**
|
|
185
|
+
* Model name (from the schema component name)
|
|
186
|
+
*/
|
|
187
|
+
name: string;
|
|
188
|
+
/**
|
|
189
|
+
* The type of this model
|
|
190
|
+
*/
|
|
191
|
+
type: ModelType;
|
|
192
|
+
/**
|
|
193
|
+
* Description from the spec
|
|
194
|
+
*/
|
|
195
|
+
description?: string;
|
|
196
|
+
/**
|
|
197
|
+
* Properties for object types
|
|
198
|
+
*/
|
|
199
|
+
properties?: Record<string, PropertyDef>;
|
|
200
|
+
/**
|
|
201
|
+
* Required property names
|
|
202
|
+
*/
|
|
203
|
+
required?: string[];
|
|
204
|
+
/**
|
|
205
|
+
* For array types, the item schema
|
|
206
|
+
*/
|
|
207
|
+
items?: SchemaRef | DataModel;
|
|
208
|
+
/**
|
|
209
|
+
* Enum values for enum types
|
|
210
|
+
*/
|
|
211
|
+
enum?: unknown[];
|
|
212
|
+
/**
|
|
213
|
+
* Example value from the spec
|
|
214
|
+
*/
|
|
215
|
+
example?: unknown;
|
|
216
|
+
/**
|
|
217
|
+
* Additional properties schema (for dictionaries)
|
|
218
|
+
*/
|
|
219
|
+
additionalProperties?: boolean | SchemaRef | DataModel;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Model types
|
|
223
|
+
*/
|
|
224
|
+
type ModelType = 'object' | 'array' | 'string' | 'number' | 'integer' | 'boolean' | 'null';
|
|
225
|
+
/**
|
|
226
|
+
* A property definition within a model
|
|
227
|
+
*/
|
|
228
|
+
interface PropertyDef {
|
|
229
|
+
/**
|
|
230
|
+
* Property name
|
|
231
|
+
*/
|
|
232
|
+
name: string;
|
|
233
|
+
/**
|
|
234
|
+
* Property type
|
|
235
|
+
*/
|
|
236
|
+
type: PropertyType;
|
|
237
|
+
/**
|
|
238
|
+
* Format hint (uuid, email, date-time, etc.)
|
|
239
|
+
*/
|
|
240
|
+
format?: string;
|
|
241
|
+
/**
|
|
242
|
+
* Description
|
|
243
|
+
*/
|
|
244
|
+
description?: string;
|
|
245
|
+
/**
|
|
246
|
+
* Whether this property is required
|
|
247
|
+
*/
|
|
248
|
+
required?: boolean;
|
|
249
|
+
/**
|
|
250
|
+
* Whether this property is nullable
|
|
251
|
+
*/
|
|
252
|
+
nullable?: boolean;
|
|
253
|
+
/**
|
|
254
|
+
* Enum values for string enums
|
|
255
|
+
*/
|
|
256
|
+
enum?: unknown[];
|
|
257
|
+
/**
|
|
258
|
+
* For array types, the item schema
|
|
259
|
+
*/
|
|
260
|
+
items?: SchemaRef | DataModel;
|
|
261
|
+
/**
|
|
262
|
+
* Reference to another model (for $ref properties)
|
|
263
|
+
*/
|
|
264
|
+
$ref?: string;
|
|
265
|
+
/**
|
|
266
|
+
* Example value
|
|
267
|
+
*/
|
|
268
|
+
example?: unknown;
|
|
269
|
+
/**
|
|
270
|
+
* Default value
|
|
271
|
+
*/
|
|
272
|
+
default?: unknown;
|
|
273
|
+
/**
|
|
274
|
+
* Minimum value (for numbers)
|
|
275
|
+
*/
|
|
276
|
+
minimum?: number;
|
|
277
|
+
/**
|
|
278
|
+
* Maximum value (for numbers)
|
|
279
|
+
*/
|
|
280
|
+
maximum?: number;
|
|
281
|
+
/**
|
|
282
|
+
* Min length (for strings)
|
|
283
|
+
*/
|
|
284
|
+
minLength?: number;
|
|
285
|
+
/**
|
|
286
|
+
* Max length (for strings)
|
|
287
|
+
*/
|
|
288
|
+
maxLength?: number;
|
|
289
|
+
/**
|
|
290
|
+
* Pattern (regex for strings)
|
|
291
|
+
*/
|
|
292
|
+
pattern?: string;
|
|
293
|
+
/**
|
|
294
|
+
* Detected relationship to another model
|
|
295
|
+
* Set by relationship detection, not directly from spec
|
|
296
|
+
*/
|
|
297
|
+
relationshipTo?: RelationshipTarget;
|
|
298
|
+
/**
|
|
299
|
+
* Custom extension for explicit relationship hints
|
|
300
|
+
* @example "x-demokit-relationship": { "model": "User", "field": "id" }
|
|
301
|
+
*/
|
|
302
|
+
'x-demokit-relationship'?: RelationshipTarget;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Property types
|
|
306
|
+
*/
|
|
307
|
+
type PropertyType = 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'null';
|
|
308
|
+
/**
|
|
309
|
+
* A detected relationship between two models
|
|
310
|
+
*/
|
|
311
|
+
interface Relationship {
|
|
312
|
+
/**
|
|
313
|
+
* The source side of the relationship
|
|
314
|
+
*/
|
|
315
|
+
from: RelationshipSide;
|
|
316
|
+
/**
|
|
317
|
+
* The target side of the relationship
|
|
318
|
+
*/
|
|
319
|
+
to: RelationshipSide;
|
|
320
|
+
/**
|
|
321
|
+
* Type of relationship
|
|
322
|
+
*/
|
|
323
|
+
type: RelationshipType;
|
|
324
|
+
/**
|
|
325
|
+
* Whether the relationship is required (not nullable)
|
|
326
|
+
*/
|
|
327
|
+
required: boolean;
|
|
328
|
+
/**
|
|
329
|
+
* How this relationship was detected
|
|
330
|
+
*/
|
|
331
|
+
detectedBy: RelationshipDetectionMethod;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* One side of a relationship
|
|
335
|
+
*/
|
|
336
|
+
interface RelationshipSide {
|
|
337
|
+
/**
|
|
338
|
+
* Model name
|
|
339
|
+
*/
|
|
340
|
+
model: string;
|
|
341
|
+
/**
|
|
342
|
+
* Field name
|
|
343
|
+
*/
|
|
344
|
+
field: string;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Target for a relationship from a property
|
|
348
|
+
*/
|
|
349
|
+
interface RelationshipTarget {
|
|
350
|
+
/**
|
|
351
|
+
* Target model name
|
|
352
|
+
*/
|
|
353
|
+
model: string;
|
|
354
|
+
/**
|
|
355
|
+
* Target field (usually "id")
|
|
356
|
+
*/
|
|
357
|
+
field: string;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Types of relationships between models
|
|
361
|
+
*/
|
|
362
|
+
type RelationshipType = 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many';
|
|
363
|
+
/**
|
|
364
|
+
* How a relationship was detected
|
|
365
|
+
*/
|
|
366
|
+
type RelationshipDetectionMethod = 'explicit-ref' | 'naming-convention' | 'x-demokit-extension' | 'inferred';
|
|
367
|
+
/**
|
|
368
|
+
* Helper type to check if something is a schema reference
|
|
369
|
+
*/
|
|
370
|
+
declare function isSchemaRef(value: unknown): value is SchemaRef;
|
|
371
|
+
/**
|
|
372
|
+
* Extract the model name from a $ref string
|
|
373
|
+
* @example "#/components/schemas/User" -> "User"
|
|
374
|
+
*/
|
|
375
|
+
declare function extractRefName(ref: string): string;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* OpenAPI 3.x parser for DemoKit
|
|
379
|
+
*
|
|
380
|
+
* Parses OpenAPI specs into DemoKit's internal schema representation
|
|
381
|
+
* with automatic relationship detection.
|
|
382
|
+
*/
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Options for parsing OpenAPI specs
|
|
386
|
+
*/
|
|
387
|
+
interface ParseOptions {
|
|
388
|
+
/**
|
|
389
|
+
* Whether to dereference all $ref pointers
|
|
390
|
+
* @default true
|
|
391
|
+
*/
|
|
392
|
+
dereference?: boolean;
|
|
393
|
+
/**
|
|
394
|
+
* Whether to detect relationships automatically
|
|
395
|
+
* @default true
|
|
396
|
+
*/
|
|
397
|
+
detectRelationships?: boolean;
|
|
398
|
+
/**
|
|
399
|
+
* Whether to include response schemas in models
|
|
400
|
+
* @default true
|
|
401
|
+
*/
|
|
402
|
+
includeResponses?: boolean;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Parse an OpenAPI spec from a file path or URL
|
|
406
|
+
*/
|
|
407
|
+
declare function parseOpenAPIFromPath(pathOrUrl: string, options?: ParseOptions): Promise<DemokitSchema>;
|
|
408
|
+
/**
|
|
409
|
+
* Parse an OpenAPI spec from a string (JSON or YAML)
|
|
410
|
+
*/
|
|
411
|
+
declare function parseOpenAPIFromString(spec: string, options?: ParseOptions): Promise<DemokitSchema>;
|
|
412
|
+
/**
|
|
413
|
+
* Parse an OpenAPI spec from an already-parsed object
|
|
414
|
+
*/
|
|
415
|
+
declare function parseOpenAPIFromObject(spec: Record<string, unknown>, options?: ParseOptions): Promise<DemokitSchema>;
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Relationship detection for DemoKit schemas
|
|
419
|
+
*
|
|
420
|
+
* Detects relationships between models using:
|
|
421
|
+
* 1. Explicit $ref references in OpenAPI
|
|
422
|
+
* 2. Naming conventions (userId -> User.id)
|
|
423
|
+
* 3. x-demokit-relationship extension
|
|
424
|
+
*/
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Check if a property looks like a foreign key based on naming conventions
|
|
428
|
+
*/
|
|
429
|
+
declare function detectRelationshipFromNaming(property: PropertyDef, availableModels: Set<string>): RelationshipTarget | null;
|
|
430
|
+
/**
|
|
431
|
+
* Check if a property has an explicit x-demokit-relationship extension
|
|
432
|
+
*/
|
|
433
|
+
declare function detectRelationshipFromExtension(property: PropertyDef): RelationshipTarget | null;
|
|
434
|
+
/**
|
|
435
|
+
* Check if a property is a $ref to another model
|
|
436
|
+
*/
|
|
437
|
+
declare function detectRelationshipFromRef(property: PropertyDef, availableModels: Set<string>): RelationshipTarget | null;
|
|
438
|
+
/**
|
|
439
|
+
* Detect all relationships in a set of models
|
|
440
|
+
*/
|
|
441
|
+
declare function detectRelationships(models: Record<string, DataModel>): Relationship[];
|
|
442
|
+
/**
|
|
443
|
+
* Find all relationships for a specific model
|
|
444
|
+
*/
|
|
445
|
+
declare function getRelationshipsForModel(modelName: string, relationships: Relationship[]): {
|
|
446
|
+
outgoing: Relationship[];
|
|
447
|
+
incoming: Relationship[];
|
|
448
|
+
};
|
|
449
|
+
/**
|
|
450
|
+
* Check if a model is referenced by other models
|
|
451
|
+
*/
|
|
452
|
+
declare function isModelReferenced(modelName: string, relationships: Relationship[]): boolean;
|
|
453
|
+
/**
|
|
454
|
+
* Get the dependency order for models based on relationships
|
|
455
|
+
* Returns models in order such that dependencies come before dependents
|
|
456
|
+
*/
|
|
457
|
+
declare function getModelDependencyOrder(models: Record<string, DataModel>, relationships: Relationship[]): string[];
|
|
458
|
+
|
|
459
|
+
export { type DataModel, type DemokitSchema, type Endpoint, type HttpMethod, type ModelType, type ParameterDef, type ParseOptions, type PropertyDef, type PropertyType, type Relationship, type RelationshipDetectionMethod, type RelationshipSide, type RelationshipTarget, type RelationshipType, type RequestBody, type ResponseDef, type SchemaInfo, type SchemaRef, detectRelationshipFromExtension, detectRelationshipFromNaming, detectRelationshipFromRef, detectRelationships, extractRefName, getModelDependencyOrder, getRelationshipsForModel, isModelReferenced, isSchemaRef, parseOpenAPIFromObject, parseOpenAPIFromPath, parseOpenAPIFromString };
|