@tupik/openapi 0.0.0-stage → 1.1.0
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/LICENSE +21 -0
- package/README.md +106 -2
- package/bin/contract-cli.mjs +4 -0
- package/bin/tupik.mjs +4 -0
- package/dist/cli.d.mts +68 -0
- package/dist/cli.d.ts +68 -0
- package/dist/cli.mjs +299 -0
- package/dist/index.d.mts +164 -0
- package/dist/index.d.ts +164 -0
- package/dist/index.mjs +223 -0
- package/dist/shared/openapi.B3aGIYpz.mjs +544 -0
- package/dist/shared/openapi.BCSoJoZR.d.mts +246 -0
- package/dist/shared/openapi.BCSoJoZR.d.ts +246 -0
- package/package.json +66 -3
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { StandardSchemaV1, ClassPaths, RouteMeta, ErrorDefinition, HTTPMethod, RouteSchema, AccessKind, AuthLevel, RouteDataClasses } from '@tupik/core';
|
|
2
|
+
|
|
3
|
+
type JsonSchemaObject = Record<string, unknown>;
|
|
4
|
+
type SchemaConverter = (schema: StandardSchemaV1) => JsonSchemaObject;
|
|
5
|
+
/** `input` for request slots, `output` for responses (Standard JSON Schema directions). */
|
|
6
|
+
type SchemaDirection = 'input' | 'output';
|
|
7
|
+
/**
|
|
8
|
+
* JSON Schema a schema carries itself: a `jsonSchema` property (e.g.
|
|
9
|
+
* `@tupik/schema`) or Standard JSON Schema (`~standard.jsonSchema[direction]`,
|
|
10
|
+
* exposed by recent Zod, Valibot and ArkType). `undefined` when neither exists.
|
|
11
|
+
*/
|
|
12
|
+
declare function ownJsonSchema(schema: StandardSchemaV1, direction: SchemaDirection): JsonSchemaObject | undefined;
|
|
13
|
+
/** `converter` first (explicit choice), then the schema's own JSON Schema. */
|
|
14
|
+
declare function jsonSchemaOf(schema: StandardSchemaV1, direction: SchemaDirection, converter: SchemaConverter | undefined): JsonSchemaObject | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Copy of `json` with `x-data-class` on every field `classes` names (the
|
|
17
|
+
* schema's own JSON Schema object is never mutated). `$ref` targets are not
|
|
18
|
+
* followed; classify the referenced schema itself instead.
|
|
19
|
+
*/
|
|
20
|
+
declare function annotateDataClasses(json: JsonSchemaObject, classes: ClassPaths): JsonSchemaObject;
|
|
21
|
+
|
|
22
|
+
/** Scheme name → required scopes. `[]` on a route means "no auth"; `{}` inside means "auth optional". */
|
|
23
|
+
type OpenApiSecurityRequirement = Record<string, string[]>;
|
|
24
|
+
/** Every problem a route may answer: its access denials plus its declared `errors`. */
|
|
25
|
+
declare function routeProblems(meta: Pick<RouteMeta, 'access' | 'auth' | 'errors'>): ErrorDefinition[];
|
|
26
|
+
/** RFC 9457 problem body schema whose `code` is the enum of `codes`. */
|
|
27
|
+
declare function problemSchema(codes: readonly string[]): JsonSchemaObject;
|
|
28
|
+
|
|
29
|
+
interface OpenApiInfo {
|
|
30
|
+
title: string;
|
|
31
|
+
version: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
}
|
|
34
|
+
interface OpenApiParameter {
|
|
35
|
+
name: string;
|
|
36
|
+
in: 'path' | 'query' | 'header';
|
|
37
|
+
required?: boolean;
|
|
38
|
+
schema: JsonSchemaObject;
|
|
39
|
+
}
|
|
40
|
+
type OpenApiSecurityScheme = {
|
|
41
|
+
type: 'http';
|
|
42
|
+
scheme: string;
|
|
43
|
+
bearerFormat?: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
} | {
|
|
46
|
+
type: 'apiKey';
|
|
47
|
+
name: string;
|
|
48
|
+
in: 'query' | 'header' | 'cookie';
|
|
49
|
+
description?: string;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'oauth2';
|
|
52
|
+
flows: Record<string, unknown>;
|
|
53
|
+
description?: string;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'openIdConnect';
|
|
56
|
+
openIdConnectUrl: string;
|
|
57
|
+
description?: string;
|
|
58
|
+
} | {
|
|
59
|
+
type: 'mutualTLS';
|
|
60
|
+
description?: string;
|
|
61
|
+
};
|
|
62
|
+
interface OpenApiOperation {
|
|
63
|
+
operationId: string;
|
|
64
|
+
tags?: string[];
|
|
65
|
+
responses: Record<string, unknown>;
|
|
66
|
+
parameters?: OpenApiParameter[];
|
|
67
|
+
requestBody?: {
|
|
68
|
+
required: boolean;
|
|
69
|
+
content: Record<string, {
|
|
70
|
+
schema: JsonSchemaObject;
|
|
71
|
+
}>;
|
|
72
|
+
};
|
|
73
|
+
security?: OpenApiSecurityRequirement[];
|
|
74
|
+
/** Access kind of the route (`public`, `authenticated`, `policy`, `owner`). */
|
|
75
|
+
'x-access'?: AccessKind;
|
|
76
|
+
}
|
|
77
|
+
interface OpenApiPathItem {
|
|
78
|
+
get?: OpenApiOperation;
|
|
79
|
+
post?: OpenApiOperation;
|
|
80
|
+
put?: OpenApiOperation;
|
|
81
|
+
delete?: OpenApiOperation;
|
|
82
|
+
patch?: OpenApiOperation;
|
|
83
|
+
head?: OpenApiOperation;
|
|
84
|
+
options?: OpenApiOperation;
|
|
85
|
+
}
|
|
86
|
+
interface OpenApiDocument {
|
|
87
|
+
openapi: '3.1.0';
|
|
88
|
+
info: OpenApiInfo;
|
|
89
|
+
paths: Record<string, OpenApiPathItem>;
|
|
90
|
+
components?: {
|
|
91
|
+
securitySchemes: Record<string, OpenApiSecurityScheme>;
|
|
92
|
+
};
|
|
93
|
+
security?: OpenApiSecurityRequirement[];
|
|
94
|
+
}
|
|
95
|
+
/** Security of one route; `undefined` inherits the document-level `security`. */
|
|
96
|
+
type RouteSecurityResolver = (route: {
|
|
97
|
+
readonly method: string;
|
|
98
|
+
readonly path: string;
|
|
99
|
+
}) => OpenApiSecurityRequirement[] | undefined;
|
|
100
|
+
interface GenerateOpenApiOptions {
|
|
101
|
+
info?: OpenApiInfo;
|
|
102
|
+
/** Standard Schema → JSON Schema. Without it, only schemas exposing `jsonSchema` convert. */
|
|
103
|
+
schemaConverter?: SchemaConverter;
|
|
104
|
+
/** Without `schemaConverter`, a schema with no `jsonSchema` throws (default) or warns and emits `{}`. */
|
|
105
|
+
onMissingConverter?: 'error' | 'warn';
|
|
106
|
+
securitySchemes?: Record<string, OpenApiSecurityScheme>;
|
|
107
|
+
/**
|
|
108
|
+
* Scheme required by routes whose `access` needs a principal. Default: the
|
|
109
|
+
* only entry of `securitySchemes`; without one, routes get no `security`.
|
|
110
|
+
*/
|
|
111
|
+
securityScheme?: string;
|
|
112
|
+
/** Document-level default requirement. */
|
|
113
|
+
security?: OpenApiSecurityRequirement[];
|
|
114
|
+
/** Per-route override of the requirement derived from `access` (`undefined` keeps it). */
|
|
115
|
+
routeSecurity?: RouteSecurityResolver;
|
|
116
|
+
/** Operation tags. Default: the first static path segment (`/users/:id` → `users`). */
|
|
117
|
+
tags?: (route: OpenApiRouteInfo) => string[] | undefined;
|
|
118
|
+
}
|
|
119
|
+
declare function toOpenApiPath(path: string): string;
|
|
120
|
+
type RouteConverter = (schema: StandardSchemaV1, slot: string, direction?: SchemaDirection) => JsonSchemaObject;
|
|
121
|
+
type MissingConverterMode = 'error' | 'warn' | 'ignore';
|
|
122
|
+
/**
|
|
123
|
+
* Converter bound to one route: JSON Schema with `x-data-class` marks. A
|
|
124
|
+
* schema that cannot be converted throws (naming the route and slot), warns
|
|
125
|
+
* or silently yields `{}`, per `onMissing`.
|
|
126
|
+
*/
|
|
127
|
+
declare function routeSchemaConverter(route: {
|
|
128
|
+
readonly method: string;
|
|
129
|
+
readonly path: string;
|
|
130
|
+
}, converter: SchemaConverter | undefined, onMissing?: MissingConverterMode): RouteConverter;
|
|
131
|
+
/** `GET /users/:id/avatar-url` → `getUsersByIdAvatarUrl`; `/` → `getRoot`; `*` → `Wildcard`. */
|
|
132
|
+
declare function operationIdOf(route: {
|
|
133
|
+
readonly method: string;
|
|
134
|
+
readonly path: string;
|
|
135
|
+
}): string;
|
|
136
|
+
/**
|
|
137
|
+
* Route view consumed by the generator. It omits `pipeline`, whose parameter is
|
|
138
|
+
* contravariant in `deps`, so any `TupikApp<TDeps>` (or `CompiledRoute<TDeps>`)
|
|
139
|
+
* is accepted regardless of its service types.
|
|
140
|
+
*/
|
|
141
|
+
interface OpenApiRouteInfo {
|
|
142
|
+
readonly method: HTTPMethod;
|
|
143
|
+
readonly path: string;
|
|
144
|
+
readonly schema?: RouteSchema | undefined;
|
|
145
|
+
/** Access, data classes and errors (`CompiledRoute.meta`); hand-built route lists may omit it. */
|
|
146
|
+
readonly meta?: RouteMeta;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Builds an OpenAPI 3.1 document: path/query/header parameters and bodies from
|
|
150
|
+
* the route schemas, a stable unique `operationId` per operation, tags and
|
|
151
|
+
* security. Throws when a schema cannot be converted (see `onMissingConverter`).
|
|
152
|
+
*/
|
|
153
|
+
declare function generateOpenApiDocument(app: {
|
|
154
|
+
readonly routes: readonly OpenApiRouteInfo[];
|
|
155
|
+
}, options?: GenerateOpenApiOptions): OpenApiDocument;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Contract snapshots capture only what is inspectable at runtime: the route
|
|
159
|
+
* set, methods, paths, declared response status codes and whether a request
|
|
160
|
+
* schema exists for `body`/`query`/`params`/`headers`. The TypeScript types
|
|
161
|
+
* behind a Standard Schema (`~standard.types`) are erased at runtime, so shape
|
|
162
|
+
* changes *inside* a schema are NOT detected — use typecheck/tests for that.
|
|
163
|
+
* Routes with metadata (`CompiledRoute.meta`) also record their access kind,
|
|
164
|
+
* auth level and data classes; required body fields are recorded when the body
|
|
165
|
+
* schema exposes a JSON Schema. Route security (scheme names) is recorded only
|
|
166
|
+
* when `snapshotContract` gets a `security` resolver.
|
|
167
|
+
*/
|
|
168
|
+
interface ContractSnapshot {
|
|
169
|
+
version: 1;
|
|
170
|
+
generatedWith: string;
|
|
171
|
+
routes: ContractRoute[];
|
|
172
|
+
}
|
|
173
|
+
interface ContractRequestFlags {
|
|
174
|
+
body: boolean;
|
|
175
|
+
query: boolean;
|
|
176
|
+
params: boolean;
|
|
177
|
+
headers: boolean;
|
|
178
|
+
}
|
|
179
|
+
interface ContractRoute {
|
|
180
|
+
method: string;
|
|
181
|
+
path: string;
|
|
182
|
+
request: ContractRequestFlags;
|
|
183
|
+
/** Declared response status codes, ascending. */
|
|
184
|
+
responses: number[];
|
|
185
|
+
/**
|
|
186
|
+
* Accepted security alternatives, sorted: each is its scheme names joined by
|
|
187
|
+
* `+` (`(none)` for an anonymous `{}` alternative); `[]` means no auth. Absent
|
|
188
|
+
* when the snapshot was taken without a security resolver.
|
|
189
|
+
*/
|
|
190
|
+
security?: string[];
|
|
191
|
+
/** Access kind from the route meta. */
|
|
192
|
+
access?: AccessKind;
|
|
193
|
+
/** Auth level from the route meta. */
|
|
194
|
+
auth?: AuthLevel;
|
|
195
|
+
/** Classified fields, sorted: `body.email:pii`, `response.200.profile.diagnosis:health`. */
|
|
196
|
+
dataClasses?: string[];
|
|
197
|
+
/** Required top-level body fields, sorted (only when the body schema has a JSON Schema). */
|
|
198
|
+
requiredBody?: string[];
|
|
199
|
+
}
|
|
200
|
+
type ContractChangeKind = 'route-removed' | 'route-added' | 'response-removed' | 'response-added' | 'request-schema-added' | 'request-schema-removed' | 'method-removed' | 'method-added' | 'security-changed' | 'access-changed' | 'data-class-added' | 'data-class-removed' | 'required-field-added' | 'required-field-removed';
|
|
201
|
+
/** `warning`: not a removal, but clients may need to handle it (e.g. a new 4xx/5xx). */
|
|
202
|
+
type ContractChangeSeverity = 'breaking' | 'warning' | 'compatible';
|
|
203
|
+
interface ContractChange {
|
|
204
|
+
kind: ContractChangeKind;
|
|
205
|
+
severity: ContractChangeSeverity;
|
|
206
|
+
method: string;
|
|
207
|
+
path: string;
|
|
208
|
+
detail?: string;
|
|
209
|
+
}
|
|
210
|
+
interface SnapshotContractOptions {
|
|
211
|
+
generatedWith?: string;
|
|
212
|
+
/** Same resolver as `generateOpenApiDocument`'s `routeSecurity`; recorded per route. */
|
|
213
|
+
security?: RouteSecurityResolver;
|
|
214
|
+
/** Standard Schema → JSON Schema, to learn required body fields of schemas without their own. */
|
|
215
|
+
schemaConverter?: SchemaConverter;
|
|
216
|
+
}
|
|
217
|
+
interface ContractAppLike {
|
|
218
|
+
routes: readonly {
|
|
219
|
+
method: string;
|
|
220
|
+
path: string;
|
|
221
|
+
schema?: RouteSchema | undefined;
|
|
222
|
+
meta?: RouteMeta | undefined;
|
|
223
|
+
}[];
|
|
224
|
+
}
|
|
225
|
+
/** Flat, sorted `slot.path:class` list of a route's data classes. */
|
|
226
|
+
declare function flattenDataClasses(classes: RouteDataClasses): string[];
|
|
227
|
+
/** Builds a deterministic, JSON-serialisable snapshot of the app's route contract. */
|
|
228
|
+
declare function snapshotContract(app: ContractAppLike, options?: SnapshotContractOptions): ContractSnapshot;
|
|
229
|
+
/**
|
|
230
|
+
* Diffs two snapshots. Removing a route/method/response or adding a request
|
|
231
|
+
* schema is `breaking`; the inverse operations are `compatible`, except a new
|
|
232
|
+
* 4xx/5xx response, which is a `warning`. When the previous snapshot recorded
|
|
233
|
+
* security, a change is `security-changed` (`breaking` unless auth was removed). A method
|
|
234
|
+
* removed from a path that still has other methods is `method-removed`; when
|
|
235
|
+
* the whole path disappears it is `route-removed` (and symmetrically for adds).
|
|
236
|
+
* Output is sorted (path, method, detail, kind) and de-duplicated.
|
|
237
|
+
*/
|
|
238
|
+
declare function diffContracts(previous: ContractSnapshot, current: ContractSnapshot): ContractChange[];
|
|
239
|
+
declare function hasBreakingChanges(changes: readonly ContractChange[]): boolean;
|
|
240
|
+
/** `BREAKING route-removed POST /users/:id`, `WARNING response-added GET /a (409)`, `ok route-added GET /health`, or `no changes`. */
|
|
241
|
+
declare function formatContractChanges(changes: readonly ContractChange[]): string;
|
|
242
|
+
/** Validates untrusted JSON (e.g. a baseline file) as a `ContractSnapshot`; throws on mismatch. */
|
|
243
|
+
declare function parseContractSnapshot(value: unknown): ContractSnapshot;
|
|
244
|
+
|
|
245
|
+
export { routeProblems as A, routeSchemaConverter as B, snapshotContract as D, toOpenApiPath as E, annotateDataClasses as p, diffContracts as q, flattenDataClasses as r, formatContractChanges as s, generateOpenApiDocument as t, hasBreakingChanges as u, jsonSchemaOf as v, operationIdOf as w, ownJsonSchema as x, parseContractSnapshot as y, problemSchema as z };
|
|
246
|
+
export type { ContractAppLike as C, GenerateOpenApiOptions as G, JsonSchemaObject as J, MissingConverterMode as M, OpenApiDocument as O, RouteSecurityResolver as R, SchemaConverter as S, ContractSnapshot as a, ContractChange as b, ContractChangeKind as c, ContractChangeSeverity as d, ContractRequestFlags as e, ContractRoute as f, OpenApiInfo as g, OpenApiOperation as h, OpenApiParameter as i, OpenApiPathItem as j, OpenApiRouteInfo as k, OpenApiSecurityRequirement as l, OpenApiSecurityScheme as m, SchemaDirection as n, SnapshotContractOptions as o };
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { StandardSchemaV1, ClassPaths, RouteMeta, ErrorDefinition, HTTPMethod, RouteSchema, AccessKind, AuthLevel, RouteDataClasses } from '@tupik/core';
|
|
2
|
+
|
|
3
|
+
type JsonSchemaObject = Record<string, unknown>;
|
|
4
|
+
type SchemaConverter = (schema: StandardSchemaV1) => JsonSchemaObject;
|
|
5
|
+
/** `input` for request slots, `output` for responses (Standard JSON Schema directions). */
|
|
6
|
+
type SchemaDirection = 'input' | 'output';
|
|
7
|
+
/**
|
|
8
|
+
* JSON Schema a schema carries itself: a `jsonSchema` property (e.g.
|
|
9
|
+
* `@tupik/schema`) or Standard JSON Schema (`~standard.jsonSchema[direction]`,
|
|
10
|
+
* exposed by recent Zod, Valibot and ArkType). `undefined` when neither exists.
|
|
11
|
+
*/
|
|
12
|
+
declare function ownJsonSchema(schema: StandardSchemaV1, direction: SchemaDirection): JsonSchemaObject | undefined;
|
|
13
|
+
/** `converter` first (explicit choice), then the schema's own JSON Schema. */
|
|
14
|
+
declare function jsonSchemaOf(schema: StandardSchemaV1, direction: SchemaDirection, converter: SchemaConverter | undefined): JsonSchemaObject | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Copy of `json` with `x-data-class` on every field `classes` names (the
|
|
17
|
+
* schema's own JSON Schema object is never mutated). `$ref` targets are not
|
|
18
|
+
* followed; classify the referenced schema itself instead.
|
|
19
|
+
*/
|
|
20
|
+
declare function annotateDataClasses(json: JsonSchemaObject, classes: ClassPaths): JsonSchemaObject;
|
|
21
|
+
|
|
22
|
+
/** Scheme name → required scopes. `[]` on a route means "no auth"; `{}` inside means "auth optional". */
|
|
23
|
+
type OpenApiSecurityRequirement = Record<string, string[]>;
|
|
24
|
+
/** Every problem a route may answer: its access denials plus its declared `errors`. */
|
|
25
|
+
declare function routeProblems(meta: Pick<RouteMeta, 'access' | 'auth' | 'errors'>): ErrorDefinition[];
|
|
26
|
+
/** RFC 9457 problem body schema whose `code` is the enum of `codes`. */
|
|
27
|
+
declare function problemSchema(codes: readonly string[]): JsonSchemaObject;
|
|
28
|
+
|
|
29
|
+
interface OpenApiInfo {
|
|
30
|
+
title: string;
|
|
31
|
+
version: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
}
|
|
34
|
+
interface OpenApiParameter {
|
|
35
|
+
name: string;
|
|
36
|
+
in: 'path' | 'query' | 'header';
|
|
37
|
+
required?: boolean;
|
|
38
|
+
schema: JsonSchemaObject;
|
|
39
|
+
}
|
|
40
|
+
type OpenApiSecurityScheme = {
|
|
41
|
+
type: 'http';
|
|
42
|
+
scheme: string;
|
|
43
|
+
bearerFormat?: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
} | {
|
|
46
|
+
type: 'apiKey';
|
|
47
|
+
name: string;
|
|
48
|
+
in: 'query' | 'header' | 'cookie';
|
|
49
|
+
description?: string;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'oauth2';
|
|
52
|
+
flows: Record<string, unknown>;
|
|
53
|
+
description?: string;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'openIdConnect';
|
|
56
|
+
openIdConnectUrl: string;
|
|
57
|
+
description?: string;
|
|
58
|
+
} | {
|
|
59
|
+
type: 'mutualTLS';
|
|
60
|
+
description?: string;
|
|
61
|
+
};
|
|
62
|
+
interface OpenApiOperation {
|
|
63
|
+
operationId: string;
|
|
64
|
+
tags?: string[];
|
|
65
|
+
responses: Record<string, unknown>;
|
|
66
|
+
parameters?: OpenApiParameter[];
|
|
67
|
+
requestBody?: {
|
|
68
|
+
required: boolean;
|
|
69
|
+
content: Record<string, {
|
|
70
|
+
schema: JsonSchemaObject;
|
|
71
|
+
}>;
|
|
72
|
+
};
|
|
73
|
+
security?: OpenApiSecurityRequirement[];
|
|
74
|
+
/** Access kind of the route (`public`, `authenticated`, `policy`, `owner`). */
|
|
75
|
+
'x-access'?: AccessKind;
|
|
76
|
+
}
|
|
77
|
+
interface OpenApiPathItem {
|
|
78
|
+
get?: OpenApiOperation;
|
|
79
|
+
post?: OpenApiOperation;
|
|
80
|
+
put?: OpenApiOperation;
|
|
81
|
+
delete?: OpenApiOperation;
|
|
82
|
+
patch?: OpenApiOperation;
|
|
83
|
+
head?: OpenApiOperation;
|
|
84
|
+
options?: OpenApiOperation;
|
|
85
|
+
}
|
|
86
|
+
interface OpenApiDocument {
|
|
87
|
+
openapi: '3.1.0';
|
|
88
|
+
info: OpenApiInfo;
|
|
89
|
+
paths: Record<string, OpenApiPathItem>;
|
|
90
|
+
components?: {
|
|
91
|
+
securitySchemes: Record<string, OpenApiSecurityScheme>;
|
|
92
|
+
};
|
|
93
|
+
security?: OpenApiSecurityRequirement[];
|
|
94
|
+
}
|
|
95
|
+
/** Security of one route; `undefined` inherits the document-level `security`. */
|
|
96
|
+
type RouteSecurityResolver = (route: {
|
|
97
|
+
readonly method: string;
|
|
98
|
+
readonly path: string;
|
|
99
|
+
}) => OpenApiSecurityRequirement[] | undefined;
|
|
100
|
+
interface GenerateOpenApiOptions {
|
|
101
|
+
info?: OpenApiInfo;
|
|
102
|
+
/** Standard Schema → JSON Schema. Without it, only schemas exposing `jsonSchema` convert. */
|
|
103
|
+
schemaConverter?: SchemaConverter;
|
|
104
|
+
/** Without `schemaConverter`, a schema with no `jsonSchema` throws (default) or warns and emits `{}`. */
|
|
105
|
+
onMissingConverter?: 'error' | 'warn';
|
|
106
|
+
securitySchemes?: Record<string, OpenApiSecurityScheme>;
|
|
107
|
+
/**
|
|
108
|
+
* Scheme required by routes whose `access` needs a principal. Default: the
|
|
109
|
+
* only entry of `securitySchemes`; without one, routes get no `security`.
|
|
110
|
+
*/
|
|
111
|
+
securityScheme?: string;
|
|
112
|
+
/** Document-level default requirement. */
|
|
113
|
+
security?: OpenApiSecurityRequirement[];
|
|
114
|
+
/** Per-route override of the requirement derived from `access` (`undefined` keeps it). */
|
|
115
|
+
routeSecurity?: RouteSecurityResolver;
|
|
116
|
+
/** Operation tags. Default: the first static path segment (`/users/:id` → `users`). */
|
|
117
|
+
tags?: (route: OpenApiRouteInfo) => string[] | undefined;
|
|
118
|
+
}
|
|
119
|
+
declare function toOpenApiPath(path: string): string;
|
|
120
|
+
type RouteConverter = (schema: StandardSchemaV1, slot: string, direction?: SchemaDirection) => JsonSchemaObject;
|
|
121
|
+
type MissingConverterMode = 'error' | 'warn' | 'ignore';
|
|
122
|
+
/**
|
|
123
|
+
* Converter bound to one route: JSON Schema with `x-data-class` marks. A
|
|
124
|
+
* schema that cannot be converted throws (naming the route and slot), warns
|
|
125
|
+
* or silently yields `{}`, per `onMissing`.
|
|
126
|
+
*/
|
|
127
|
+
declare function routeSchemaConverter(route: {
|
|
128
|
+
readonly method: string;
|
|
129
|
+
readonly path: string;
|
|
130
|
+
}, converter: SchemaConverter | undefined, onMissing?: MissingConverterMode): RouteConverter;
|
|
131
|
+
/** `GET /users/:id/avatar-url` → `getUsersByIdAvatarUrl`; `/` → `getRoot`; `*` → `Wildcard`. */
|
|
132
|
+
declare function operationIdOf(route: {
|
|
133
|
+
readonly method: string;
|
|
134
|
+
readonly path: string;
|
|
135
|
+
}): string;
|
|
136
|
+
/**
|
|
137
|
+
* Route view consumed by the generator. It omits `pipeline`, whose parameter is
|
|
138
|
+
* contravariant in `deps`, so any `TupikApp<TDeps>` (or `CompiledRoute<TDeps>`)
|
|
139
|
+
* is accepted regardless of its service types.
|
|
140
|
+
*/
|
|
141
|
+
interface OpenApiRouteInfo {
|
|
142
|
+
readonly method: HTTPMethod;
|
|
143
|
+
readonly path: string;
|
|
144
|
+
readonly schema?: RouteSchema | undefined;
|
|
145
|
+
/** Access, data classes and errors (`CompiledRoute.meta`); hand-built route lists may omit it. */
|
|
146
|
+
readonly meta?: RouteMeta;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Builds an OpenAPI 3.1 document: path/query/header parameters and bodies from
|
|
150
|
+
* the route schemas, a stable unique `operationId` per operation, tags and
|
|
151
|
+
* security. Throws when a schema cannot be converted (see `onMissingConverter`).
|
|
152
|
+
*/
|
|
153
|
+
declare function generateOpenApiDocument(app: {
|
|
154
|
+
readonly routes: readonly OpenApiRouteInfo[];
|
|
155
|
+
}, options?: GenerateOpenApiOptions): OpenApiDocument;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Contract snapshots capture only what is inspectable at runtime: the route
|
|
159
|
+
* set, methods, paths, declared response status codes and whether a request
|
|
160
|
+
* schema exists for `body`/`query`/`params`/`headers`. The TypeScript types
|
|
161
|
+
* behind a Standard Schema (`~standard.types`) are erased at runtime, so shape
|
|
162
|
+
* changes *inside* a schema are NOT detected — use typecheck/tests for that.
|
|
163
|
+
* Routes with metadata (`CompiledRoute.meta`) also record their access kind,
|
|
164
|
+
* auth level and data classes; required body fields are recorded when the body
|
|
165
|
+
* schema exposes a JSON Schema. Route security (scheme names) is recorded only
|
|
166
|
+
* when `snapshotContract` gets a `security` resolver.
|
|
167
|
+
*/
|
|
168
|
+
interface ContractSnapshot {
|
|
169
|
+
version: 1;
|
|
170
|
+
generatedWith: string;
|
|
171
|
+
routes: ContractRoute[];
|
|
172
|
+
}
|
|
173
|
+
interface ContractRequestFlags {
|
|
174
|
+
body: boolean;
|
|
175
|
+
query: boolean;
|
|
176
|
+
params: boolean;
|
|
177
|
+
headers: boolean;
|
|
178
|
+
}
|
|
179
|
+
interface ContractRoute {
|
|
180
|
+
method: string;
|
|
181
|
+
path: string;
|
|
182
|
+
request: ContractRequestFlags;
|
|
183
|
+
/** Declared response status codes, ascending. */
|
|
184
|
+
responses: number[];
|
|
185
|
+
/**
|
|
186
|
+
* Accepted security alternatives, sorted: each is its scheme names joined by
|
|
187
|
+
* `+` (`(none)` for an anonymous `{}` alternative); `[]` means no auth. Absent
|
|
188
|
+
* when the snapshot was taken without a security resolver.
|
|
189
|
+
*/
|
|
190
|
+
security?: string[];
|
|
191
|
+
/** Access kind from the route meta. */
|
|
192
|
+
access?: AccessKind;
|
|
193
|
+
/** Auth level from the route meta. */
|
|
194
|
+
auth?: AuthLevel;
|
|
195
|
+
/** Classified fields, sorted: `body.email:pii`, `response.200.profile.diagnosis:health`. */
|
|
196
|
+
dataClasses?: string[];
|
|
197
|
+
/** Required top-level body fields, sorted (only when the body schema has a JSON Schema). */
|
|
198
|
+
requiredBody?: string[];
|
|
199
|
+
}
|
|
200
|
+
type ContractChangeKind = 'route-removed' | 'route-added' | 'response-removed' | 'response-added' | 'request-schema-added' | 'request-schema-removed' | 'method-removed' | 'method-added' | 'security-changed' | 'access-changed' | 'data-class-added' | 'data-class-removed' | 'required-field-added' | 'required-field-removed';
|
|
201
|
+
/** `warning`: not a removal, but clients may need to handle it (e.g. a new 4xx/5xx). */
|
|
202
|
+
type ContractChangeSeverity = 'breaking' | 'warning' | 'compatible';
|
|
203
|
+
interface ContractChange {
|
|
204
|
+
kind: ContractChangeKind;
|
|
205
|
+
severity: ContractChangeSeverity;
|
|
206
|
+
method: string;
|
|
207
|
+
path: string;
|
|
208
|
+
detail?: string;
|
|
209
|
+
}
|
|
210
|
+
interface SnapshotContractOptions {
|
|
211
|
+
generatedWith?: string;
|
|
212
|
+
/** Same resolver as `generateOpenApiDocument`'s `routeSecurity`; recorded per route. */
|
|
213
|
+
security?: RouteSecurityResolver;
|
|
214
|
+
/** Standard Schema → JSON Schema, to learn required body fields of schemas without their own. */
|
|
215
|
+
schemaConverter?: SchemaConverter;
|
|
216
|
+
}
|
|
217
|
+
interface ContractAppLike {
|
|
218
|
+
routes: readonly {
|
|
219
|
+
method: string;
|
|
220
|
+
path: string;
|
|
221
|
+
schema?: RouteSchema | undefined;
|
|
222
|
+
meta?: RouteMeta | undefined;
|
|
223
|
+
}[];
|
|
224
|
+
}
|
|
225
|
+
/** Flat, sorted `slot.path:class` list of a route's data classes. */
|
|
226
|
+
declare function flattenDataClasses(classes: RouteDataClasses): string[];
|
|
227
|
+
/** Builds a deterministic, JSON-serialisable snapshot of the app's route contract. */
|
|
228
|
+
declare function snapshotContract(app: ContractAppLike, options?: SnapshotContractOptions): ContractSnapshot;
|
|
229
|
+
/**
|
|
230
|
+
* Diffs two snapshots. Removing a route/method/response or adding a request
|
|
231
|
+
* schema is `breaking`; the inverse operations are `compatible`, except a new
|
|
232
|
+
* 4xx/5xx response, which is a `warning`. When the previous snapshot recorded
|
|
233
|
+
* security, a change is `security-changed` (`breaking` unless auth was removed). A method
|
|
234
|
+
* removed from a path that still has other methods is `method-removed`; when
|
|
235
|
+
* the whole path disappears it is `route-removed` (and symmetrically for adds).
|
|
236
|
+
* Output is sorted (path, method, detail, kind) and de-duplicated.
|
|
237
|
+
*/
|
|
238
|
+
declare function diffContracts(previous: ContractSnapshot, current: ContractSnapshot): ContractChange[];
|
|
239
|
+
declare function hasBreakingChanges(changes: readonly ContractChange[]): boolean;
|
|
240
|
+
/** `BREAKING route-removed POST /users/:id`, `WARNING response-added GET /a (409)`, `ok route-added GET /health`, or `no changes`. */
|
|
241
|
+
declare function formatContractChanges(changes: readonly ContractChange[]): string;
|
|
242
|
+
/** Validates untrusted JSON (e.g. a baseline file) as a `ContractSnapshot`; throws on mismatch. */
|
|
243
|
+
declare function parseContractSnapshot(value: unknown): ContractSnapshot;
|
|
244
|
+
|
|
245
|
+
export { routeProblems as A, routeSchemaConverter as B, snapshotContract as D, toOpenApiPath as E, annotateDataClasses as p, diffContracts as q, flattenDataClasses as r, formatContractChanges as s, generateOpenApiDocument as t, hasBreakingChanges as u, jsonSchemaOf as v, operationIdOf as w, ownJsonSchema as x, parseContractSnapshot as y, problemSchema as z };
|
|
246
|
+
export type { ContractAppLike as C, GenerateOpenApiOptions as G, JsonSchemaObject as J, MissingConverterMode as M, OpenApiDocument as O, RouteSecurityResolver as R, SchemaConverter as S, ContractSnapshot as a, ContractChange as b, ContractChangeKind as c, ContractChangeSeverity as d, ContractRequestFlags as e, ContractRoute as f, OpenApiInfo as g, OpenApiOperation as h, OpenApiParameter as i, OpenApiPathItem as j, OpenApiRouteInfo as k, OpenApiSecurityRequirement as l, OpenApiSecurityScheme as m, SchemaDirection as n, SnapshotContractOptions as o };
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tupik/openapi",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "OpenAPI 3.1 from Tupik route metadata, plus the tupik CLI: security audit (text/JSON/SARIF), personal data map (LGPD/GDPR) and contract snapshot/breaking-change check.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tupik",
|
|
8
|
+
"typescript",
|
|
9
|
+
"web-framework",
|
|
10
|
+
"openapi",
|
|
11
|
+
"openapi-3.1",
|
|
12
|
+
"contract-testing",
|
|
13
|
+
"api-contract",
|
|
14
|
+
"security-audit",
|
|
15
|
+
"sarif",
|
|
16
|
+
"lgpd",
|
|
17
|
+
"gdpr"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/CodeHamper/tupikjs/tree/main/packages/openapi#readme",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/CodeHamper/tupikjs.git",
|
|
23
|
+
"directory": "packages/openapi"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/CodeHamper/tupikjs/issues"
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"bin": {
|
|
33
|
+
"tupik": "./bin/tupik.mjs",
|
|
34
|
+
"tupik-contract": "./bin/contract-cli.mjs"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"bin"
|
|
39
|
+
],
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"import": "./dist/index.mjs"
|
|
44
|
+
},
|
|
45
|
+
"./cli": {
|
|
46
|
+
"types": "./dist/cli.d.ts",
|
|
47
|
+
"import": "./dist/cli.mjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=22.12"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@tupik/core": "^1.1.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@valibot/to-json-schema": "^1.8.0",
|
|
58
|
+
"typescript": "^5.6.0",
|
|
59
|
+
"unbuild": "^3.0.0",
|
|
60
|
+
"valibot": "^1.5.0",
|
|
61
|
+
"vitest": "^2.1.0",
|
|
62
|
+
"zod": "^4.6.7"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "unbuild",
|
|
66
|
+
"typecheck": "tsc --noEmit",
|
|
67
|
+
"test": "vitest run"
|
|
68
|
+
}
|
|
6
69
|
}
|