-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathAutoBeOpenApi.ts
More file actions
610 lines (548 loc) · 19.3 KB
/
AutoBeOpenApi.ts
File metadata and controls
610 lines (548 loc) · 19.3 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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import { tags } from "typia";
import { CamelCasePattern } from "../typings/CamelCasePattern";
/**
* AST for OpenAPI v3.1 specification generation via AI function calling.
*
* Simplified from full OpenAPI to remove ambiguous/duplicated expressions while
* maintaining type safety for AI-driven code generation.
*
* All description fields MUST be written in English. Never use other languages.
*/
export namespace AutoBeOpenApi {
/* -----------------------------------------------------------
DOCUMENT
----------------------------------------------------------- */
/**
* Root document for Restful API operations and components.
*
* Corresponds to the top-level OpenAPI structure, containing all API
* operations and reusable component schemas.
*/
export interface IDocument {
/**
* List of API operations.
*
* Combination of {@link AutoBeOpenApi.IOperation.path} and
* {@link AutoBeOpenApi.IOperation.method} must be unique.
*
* @minItems 1
*/
operations: AutoBeOpenApi.IOperation[];
/**
* Reusable components referenced by API operations.
*
* Type naming conventions for schemas:
*
* - `IEntityName`: Full detailed entity (e.g., `IShoppingSale`)
* - `IEntityName.ICreate`: Request body for POST creation
* - `IEntityName.IUpdate`: Request body for PUT update
* - `IEntityName.IRequest`: Search/filter/pagination parameters
* - `IEntityName.ISummary`: Simplified view for list operations
* - `IEntityName.IAbridge`: Intermediate detail level
* - `IEntityName.IInvert`: Alternative perspective of an entity
* - `IPageIEntityName`: Paginated results with `pagination` and `data`
*/
components: AutoBeOpenApi.IComponents;
}
/**
* Single API endpoint with method, path, parameters, and request/response.
*
* All request/response bodies must be object types referencing named
* components. Content-type is always `application/json`. For file
* upload/download, use `string & tags.Format<"uri">` instead of binary.
*/
export interface IOperation extends IEndpoint {
/**
* Internal implementation guidance for downstream agents (Realize, Test).
*
* Describe HOW this operation should be implemented: service logic, DB
* queries, business rules, edge cases, and error handling.
*
* > MUST be written in English. Never use other languages.
*/
specification: string;
/**
* API documentation for consumers. Describe the operation's purpose,
* business logic, relationships, and error handling.
*
* Format: summary sentence first, `\n\n`, then paragraphs grouped by topic.
* Reference DB schema table/column descriptions for consistency.
*
* Do NOT use "soft delete" / "soft-delete" unless the operation actually
* implements soft deletion (triggers validation expecting
* soft_delete_column).
*
* > MUST be written in English. Never use other languages.
*/
description: string;
/**
* Authorization type of the API operation.
*
* - `"login"`: Credential validation operations
* - `"join"`: Account registration operations
* - `"refresh"`: Token renewal operations
* - `null`: All other operations
*/
authorizationType: "login" | "join" | "refresh" | null;
/**
* List of path parameters.
*
* Each parameter name must correspond to a `{paramName}` in the
* {@link path}.
*/
parameters: AutoBeOpenApi.IParameter[];
/** Request body of the API operation, or `null` if none. */
requestBody: AutoBeOpenApi.IRequestBody | null;
/** Response body of the API operation, or `null` if none. */
responseBody: AutoBeOpenApi.IResponseBody | null;
/**
* Authorization actor required to access this API operation.
*
* MUST use camelCase. The actor name MUST match exactly with a user
* type/table defined in the database schema.
*
* Set to `null` for public endpoints requiring no authentication.
*/
authorizationActor: (string & CamelCasePattern & tags.MinLength<1>) | null;
/**
* Functional name of the API endpoint. MUST use camelCase.
*
* MUST NOT be a JS/TS reserved word (`delete`, `for`, `if`, `class`,
* `return`, `new`, `this`, `void`, `const`, `let`, `var`, `async`, `await`,
* `export`, `import`, `switch`, `case`, `throw`, `try`). Use `erase`
* instead of `delete`, `iterate` instead of `for`.
*
* Standard names:
*
* - `index`: list/search (PATCH), `at`: get by ID (GET)
* - `create`: POST, `update`: PUT, `erase`: DELETE
*
* Accessor uniqueness: the accessor is formed by joining non-parameter path
* segments with dots, then appending the name. E.g., path
* `/shopping/sale/{saleId}/review/{reviewId}` + name `at` = accessor
* `shopping.sale.review.at`. Must be globally unique.
*/
name: string & CamelCasePattern;
/**
* Prerequisites: API operations that must succeed before this one.
*
* ONLY for business logic dependencies (resource existence, state checks,
* data availability). NEVER for authentication -- use `authorizationActor`
* instead.
*
* Prerequisites are executed in array order; all must return 2xx before the
* main operation proceeds.
*
* @see {@link IPrerequisite}
*/
prerequisites: IPrerequisite[];
/**
* Accessor of the operation.
*
* If you configure this property, the assigned value will be used as
* {@link IHttpMigrateRoute.accessor}. Also, it can be used as the
* {@link IHttpLlmFunction.name} by joining with `.` character in the LLM
* function calling application.
*
* Note that the `x-samchon-accessor` value must be unique in the entire
* OpenAPI document operations. If there are duplicated `x-samchon-accessor`
* values, {@link IHttpMigrateRoute.accessor} will ignore all duplicated
* `x-samchon-accessor` values and generate the
* {@link IHttpMigrateRoute.accessor} by itself.
*
* @internal
*/
accessor?: string[] | undefined;
}
/**
* Authorization definition for an actor type.
*
* Uses `Authorization: Bearer <token>` header only. The token is guaranteed
* to include the authenticated actor's `id` field.
*/
export interface IAuthorization {
/**
* Actor name in camelCase. MUST exactly match a table name in the database
* schema representing this user type.
*/
name: string & CamelCasePattern;
/**
* Description of this authorization actor's purpose and capabilities.
*
* > MUST be written in English. Never use other languages.
*/
description: string;
}
/** Path parameter definition for an API route. */
export interface IParameter {
/**
* Description of the path parameter.
*
* > MUST be written in English. Never use other languages.
*/
description: string;
/**
* Identifier name in camelCase. Must match the `{paramName}` in the
* {@link AutoBeOpenApi.IOperation.path}.
*/
name: string & CamelCasePattern;
/** Type schema of the path parameter (primitive types only). */
schema:
| AutoBeOpenApi.IJsonSchema.IInteger
| AutoBeOpenApi.IJsonSchema.INumber
| AutoBeOpenApi.IJsonSchema.IString;
}
/**
* Request body for an API operation.
*
* Content-type is always `application/json`. For file uploads, use a URI
* string property instead of `multipart/form-data`.
*/
export interface IRequestBody {
/**
* Description of the request body.
*
* > MUST be written in English. Never use other languages.
*/
description: string;
/**
* Type name referencing a component schema.
*
* Naming convention: `IEntityName.ICreate` (POST), `IEntityName.IUpdate`
* (PUT), `IEntityName.IRequest` (list/search).
*/
typeName: string;
}
/**
* Response body for an API operation.
*
* Content-type is always `application/json`. For file downloads, use a URI
* string property instead of `application/octet-stream`.
*/
export interface IResponseBody {
/**
* Description of the response body.
*
* > MUST be written in English. Never use other languages.
*/
description: string;
/**
* Type name referencing a component schema.
*
* Naming convention: `IEntityName` (full), `IEntityName.ISummary`,
* `IEntityName.IInvert`, `IPageIEntityName` (paginated).
*/
typeName: string;
}
/* -----------------------------------------------------------
JSON SCHEMA
----------------------------------------------------------- */
/** Reusable named DTO schemas and security schemes. */
export interface IComponents {
/**
* Named DTO schemas.
*
* Type naming conventions:
*
* - `IEntityName`: Full detailed entity
* - `IEntityName.ICreate`: POST request body
* - `IEntityName.IUpdate`: PUT request body
* - `IEntityName.ISummary`: Simplified list view
* - `IEntityName.IRequest`: Search/filter parameters
* - `IEntityName.IInvert`: Alternative perspective
* - `IPageIEntityName`: Paginated results (`pagination` + `data`)
*/
schemas: Record<string, IJsonSchemaDescriptive>;
/** Authorization schemes for authenticated actors. */
authorizations: IAuthorization[];
}
/**
* JSON Schema type following OpenAPI v3.1 (simplified).
*
* CRITICAL: Union types MUST use `IOneOf`. NEVER use array in `type` field.
*
* Wrong: `{ type: ["string", "null"] }` Correct: `{ oneOf: [{ type: "string"
* }, { type: "null" }] }`
*
* The `type` field is a discriminator and MUST be a single string value.
*/
export type IJsonSchema =
| IJsonSchema.IConstant
| IJsonSchema.IBoolean
| IJsonSchema.IInteger
| IJsonSchema.INumber
| IJsonSchema.IString
| IJsonSchema.IArray
| IJsonSchema.IObject
| IJsonSchema.IReference
| IJsonSchema.IOneOf
| IJsonSchema.INull;
export namespace IJsonSchema {
/** Constant value type. */
export interface IConstant {
/** The constant value. */
const: boolean | number | string;
}
/** Boolean type info. */
export interface IBoolean extends ISignificant<"boolean"> {}
/** Integer type info. */
export interface IInteger extends ISignificant<"integer"> {
/** @type int64 */
minimum?: number;
/** @type int64 */
maximum?: number;
/** @type int64 */
exclusiveMinimum?: number;
/** @type int64 */
exclusiveMaximum?: number;
/**
* @type uint64
* @exclusiveMinimum 0
*/
multipleOf?: number;
}
/** Number (double) type info. */
export interface INumber extends ISignificant<"number"> {
minimum?: number;
maximum?: number;
exclusiveMinimum?: number;
exclusiveMaximum?: number;
/** @exclusiveMinimum 0 */
multipleOf?: number;
}
/** String type info. */
export interface IString extends ISignificant<"string"> {
/** Format restriction. */
format?:
| "password"
| "regex"
| "uuid"
| "email"
| "hostname"
| "idn-email"
| "idn-hostname"
| "iri"
| "iri-reference"
| "ipv4"
| "ipv6"
| "uri"
| "uri-reference"
| "uri-template"
| "url"
| "date-time"
| "date"
| "time"
| "duration"
| "json-pointer"
| "relative-json-pointer";
/** Pattern restriction. */
pattern?: string;
/**
* Content media type restriction.
*
* For multiple media types, use `oneOf` with separate string schemas per
* `contentMediaType` value. Never use an array here.
*/
contentMediaType?: string;
/** @type uint64 */
minLength?: number;
/** @type uint64 */
maxLength?: number;
}
/** Array type info. */
export interface IArray extends ISignificant<"array"> {
/** Type schema of array elements. */
items: Exclude<IJsonSchema, IJsonSchema.IObject>;
/** If `true`, array elements must be unique. */
uniqueItems?: boolean;
/** @type uint64 */
minItems?: number;
/** @type uint64 */
maxItems?: number;
}
/** Object type info. */
export interface IObject extends ISignificant<"object"> {
/** @ignore */
"x-autobe-database-schema"?: string | null | undefined;
/** Key-value pairs of the object's named properties. */
properties: Record<string, IJsonSchema>;
/**
* Schema for dynamic keys (`Record<string, T>`), or `false` if no
* additional properties are allowed.
*/
additionalProperties?: false | Exclude<IJsonSchema, IJsonSchema.IObject>;
/**
* Property keys that must be present. Properties not listed here are
* optional.
*/
required: string[];
}
/** Reference type directing named schema. */
export interface IReference {
/**
* JSON Pointer reference to a named schema (e.g.,
* `#/components/schemas/SomeObject`).
*/
$ref: string;
}
/**
* Union type.
*
* `IOneOf` represents a union type in TypeScript (`A | B | C`).
*
* For reference, even though your Swagger (or OpenAPI) document has defined
* `anyOf` instead of the `oneOf`, {@link AutoBeOpenApi} forcibly converts it
* to `oneOf` type.
*/
export interface IOneOf {
/** List of the union types. */
oneOf: Exclude<IJsonSchema, IJsonSchema.IOneOf | IJsonSchema.IObject>[];
/** Discriminator info of the union type. */
discriminator?: IOneOf.IDiscriminator;
}
export namespace IOneOf {
/** Discriminator info of the union type. */
export interface IDiscriminator {
/** Property name for the discriminator. */
propertyName: string;
/**
* Mapping of the discriminator value to the schema name.
*
* This property is valid only for {@link IReference} typed
* {@link IOneOf.oneOf} elements. Therefore, `key` of `mapping` is the
* discriminator value, and `value` of `mapping` is the schema name like
* `#/components/schemas/SomeObject`.
*/
mapping?: Record<string, string>;
}
}
/** Null type. */
export interface INull extends ISignificant<"null"> {}
interface ISignificant<Type extends string> {
/**
* Discriminator value. MUST be a single string, NEVER an array.
*
* For nullable types, use `IOneOf` instead: `{ oneOf: [{ type: "string"
* }, { type: "null" }] }`
*/
type: Type;
}
}
/**
* Descriptive type schema info with required documentation.
*
* `AutoBeOpenApi.IJsonSchemaDescriptive` extends the base JSON schema types
* with a required `description` field for API documentation. For object
* types, it also includes an `x-autobe-specification` field for
* implementation guidance.
*
* @ignore
*/
export type IJsonSchemaDescriptive =
| IJsonSchemaDescriptive.IConstant
| IJsonSchemaDescriptive.IBoolean
| IJsonSchemaDescriptive.IInteger
| IJsonSchemaDescriptive.INumber
| IJsonSchemaDescriptive.IString
| IJsonSchemaDescriptive.IArray
| IJsonSchemaDescriptive.IObject
| IJsonSchemaDescriptive.IReference
| IJsonSchemaDescriptive.IOneOf
| IJsonSchemaDescriptive.INull;
export namespace IJsonSchemaDescriptive {
export interface IConstant extends IDescriptive, IJsonSchema.IConstant {}
export interface IBoolean extends IDescriptive, IJsonSchema.IBoolean {}
export interface IInteger extends IDescriptive, IJsonSchema.IInteger {}
export interface INumber extends IDescriptive, IJsonSchema.INumber {}
export interface IString extends IDescriptive, IJsonSchema.IString {}
export interface IArray extends IDescriptive, IJsonSchema.IArray {}
export interface IObject extends IDescriptive, IJsonSchema.IObject {
properties: Record<string, IJsonSchemaProperty>;
}
export interface IReference extends IDescriptive, IJsonSchema.IReference {}
export interface IOneOf extends IDescriptive, IJsonSchema.IOneOf {}
export interface INull extends IDescriptive, IJsonSchema.INull {}
interface IDescriptive {
"x-autobe-specification"?: string | undefined;
description: string;
}
}
/**
* Type schema for object properties with implementation specifications.
*
* `IJsonSchemaProperty` extends the base JSON Schema types with
* implementation specifications. Each property in an
* {@link IJsonSchema.IObject object schema} uses this type.
*
* @ignore
*/
export type IJsonSchemaProperty =
| IJsonSchemaProperty.IConstant
| IJsonSchemaProperty.IBoolean
| IJsonSchemaProperty.IInteger
| IJsonSchemaProperty.INumber
| IJsonSchemaProperty.IString
| IJsonSchemaProperty.IArray
| IJsonSchemaProperty.IReference
| IJsonSchemaProperty.IOneOf
| IJsonSchemaProperty.INull;
export namespace IJsonSchemaProperty {
export interface IConstant extends IProperty, IJsonSchema.IConstant {}
export interface IBoolean extends IProperty, IJsonSchema.IBoolean {}
export interface IInteger extends IProperty, IJsonSchema.IInteger {}
export interface INumber extends IProperty, IJsonSchema.INumber {}
export interface IString extends IProperty, IJsonSchema.IString {}
export interface IArray extends IProperty, IJsonSchema.IArray {}
export interface IReference extends IProperty, IJsonSchema.IReference {}
export interface IOneOf extends IProperty, IJsonSchema.IOneOf {}
export interface INull extends IProperty, IJsonSchema.INull {}
interface IProperty {
"x-autobe-database-schema-property"?: string | null | undefined;
"x-autobe-specification"?: string | undefined;
description: string;
}
}
/* -----------------------------------------------------------
BACKGROUNDS
----------------------------------------------------------- */
/** API endpoint information. */
export interface IEndpoint {
/**
* HTTP path of the API operation.
*
* Must start with `/`. Parameters use curly braces: `{paramName}`. Resource
* names in camelCase. No quotes, spaces, role prefixes (`/admin/`), or API
* version prefixes (`/api/v1/`).
*
* Allowed characters: letters, digits, `/`, `{`, `}`, `-`, `_`, `.`
*/
path: string & tags.Pattern<"^\\/[a-zA-Z0-9\\/_{}.-]*$">;
/**
* HTTP method (lowercase only).
*
* Use `patch` (not `get`) when a read operation needs a complex
* {@link requestBody}. `get` cannot have a request body.
*/
method: "get" | "post" | "put" | "delete" | "patch";
}
/**
* Prerequisite API operation that must succeed before the main operation.
*
* ONLY for business logic dependencies (resource existence, state checks,
* data availability). NEVER for authentication or authorization -- those are
* handled via `authorizationActor`.
*
* Keep prerequisite chains minimal. Descriptions should explain WHY the
* dependency is needed.
*/
export interface IPrerequisite {
/** The API endpoint that must be called first. */
endpoint: IEndpoint;
/**
* Why this prerequisite is required (specific condition or state).
*
* > MUST be written in English. Never use other languages.
*/
description: string;
}
}