@voxgig/apidef 8.22.1 → 8.23.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/dist/apidef.d.ts +1 -1
- package/dist/apidef.js +2 -0
- package/dist/apidef.js.map +1 -1
- package/dist/guide/heuristic01.d.ts +10 -1
- package/dist/guide/heuristic01.js +249 -39
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/model.d.ts +18 -1
- package/dist/transform/args.js +27 -9
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/body.d.ts +6 -0
- package/dist/transform/body.js +300 -0
- package/dist/transform/body.js.map +1 -0
- package/dist/transform/entity.d.ts +1 -1
- package/dist/transform/entity.js +13 -4
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/select.js +34 -7
- package/dist/transform/select.js.map +1 -1
- package/dist/transform/top.d.ts +1 -1
- package/dist/transform/top.js +77 -8
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.js.map +1 -1
- package/dist/utility.d.ts +3 -1
- package/dist/utility.js +17 -0
- package/dist/utility.js.map +1 -1
- package/model/apidef.aontu +29 -6
- package/model/guide.aontu +8 -0
- package/package.json +1 -1
- package/src/apidef.ts +5 -0
- package/src/guide/heuristic01.ts +320 -36
- package/src/model.ts +27 -0
- package/src/transform/args.ts +35 -10
- package/src/transform/body.ts +422 -0
- package/src/transform/entity.ts +20 -4
- package/src/transform/select.ts +36 -7
- package/src/transform/top.ts +95 -12
- package/src/types.ts +6 -0
- package/src/utility.ts +21 -0
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/* Copyright (c) 2026 Voxgig Ltd, MIT License */
|
|
2
|
+
|
|
3
|
+
import type { TransformResult, Transform } from '../transform'
|
|
4
|
+
|
|
5
|
+
import { KIT } from '../types'
|
|
6
|
+
|
|
7
|
+
import { guideActive, mergedProperties, sortedKeys } from '../utility'
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
BodyKind,
|
|
11
|
+
ModelBody,
|
|
12
|
+
ModelBodyField,
|
|
13
|
+
ModelPoint,
|
|
14
|
+
} from '../model'
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
// JSON first, as generated SDKs send it; then the kinds by what each can carry.
|
|
18
|
+
const KIND_ORDER: BodyKind[] = ['json', 'multipart', 'form', 'raw']
|
|
19
|
+
|
|
20
|
+
const JSON_MEDIA = 'application/json'
|
|
21
|
+
const FORM_MEDIA = 'application/x-www-form-urlencoded'
|
|
22
|
+
const MULTIPART_MEDIA = 'multipart/form-data'
|
|
23
|
+
const OCTET_MEDIA = 'application/octet-stream'
|
|
24
|
+
|
|
25
|
+
const SUCCESS_RE = /^2(\d\d|xx)$/i
|
|
26
|
+
|
|
27
|
+
const TYPING_KEYS = [
|
|
28
|
+
'type', 'format', 'properties', 'additionalProperties', 'items',
|
|
29
|
+
'allOf', 'anyOf', 'oneOf', 'enum', 'const', 'contentMediaType', 'contentEncoding',
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
type Offer = {
|
|
34
|
+
media: string
|
|
35
|
+
schema?: any
|
|
36
|
+
encoding?: any
|
|
37
|
+
swagger?: boolean
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
const bodyTransform: Transform = async function(
|
|
42
|
+
ctx: any,
|
|
43
|
+
): Promise<TransformResult> {
|
|
44
|
+
const { apimodel, def, guide } = ctx
|
|
45
|
+
const entities = apimodel.main[KIT].entity
|
|
46
|
+
|
|
47
|
+
let msg = 'body '
|
|
48
|
+
|
|
49
|
+
for (const entname of sortedKeys(entities)) {
|
|
50
|
+
const ops = entities[entname].op ?? {}
|
|
51
|
+
for (const opname of sortedKeys(ops)) {
|
|
52
|
+
for (const mpoint of (ops[opname]?.points ?? []) as ModelPoint[]) {
|
|
53
|
+
if ('graphql' === mpoint.k) {
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
const media = guideMedia(guide, entname, opname, mpoint)
|
|
57
|
+
const rb = requestBody(def, mpoint.m, mpoint.o, media.body)
|
|
58
|
+
if (null != rb) {
|
|
59
|
+
mpoint.rb = rb
|
|
60
|
+
}
|
|
61
|
+
const rs = responseBody(def, mpoint.m, mpoint.o, media.response)
|
|
62
|
+
if (null != rs) {
|
|
63
|
+
mpoint.rs = rs
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
msg += entname + ' '
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return { ok: true, msg }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
// The entry of the point's own op, as ops can share a path and method.
|
|
75
|
+
// A patch the operation pass promotes to update keeps its entry under patch.
|
|
76
|
+
function guideMedia(
|
|
77
|
+
guide: any,
|
|
78
|
+
entname: string,
|
|
79
|
+
opname: string,
|
|
80
|
+
mpoint: ModelPoint,
|
|
81
|
+
): { body?: string, response?: string } {
|
|
82
|
+
const gops = guide?.entity?.[entname]?.path?.[mpoint.o]?.op ?? {}
|
|
83
|
+
for (const name of 'update' === opname ? ['update', 'patch'] : [opname]) {
|
|
84
|
+
const gop = gops[name]
|
|
85
|
+
if (null != gop && guideActive(gop) &&
|
|
86
|
+
String(gop.method ?? '').toUpperCase() === String(mpoint.m).toUpperCase()) {
|
|
87
|
+
return { body: textOf(gop.body?.media), response: textOf(gop.response?.media) }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return {}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
// Undefined when the operation sends JSON alone.
|
|
95
|
+
function requestBody(
|
|
96
|
+
def: any,
|
|
97
|
+
method: string,
|
|
98
|
+
path: string,
|
|
99
|
+
media?: string,
|
|
100
|
+
): ModelBody | undefined {
|
|
101
|
+
const pathdef = def?.paths?.[path]
|
|
102
|
+
const opdef = pathdef?.[String(method).toLowerCase()]
|
|
103
|
+
if (!isMap(opdef)) {
|
|
104
|
+
return undefined
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const body = chooseBody(null != def.swagger ?
|
|
108
|
+
swaggerOffers(def, pathdef, opdef) : openapiOffers(opdef), media)
|
|
109
|
+
|
|
110
|
+
if (null == body || ('json' === body.kind && JSON_MEDIA === essence(body.media) &&
|
|
111
|
+
(body.alternatives ?? []).every((other) => 'json' === other.kind))) {
|
|
112
|
+
return undefined
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return body
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
// Undefined when no success response declares a body.
|
|
120
|
+
function responseBody(
|
|
121
|
+
def: any,
|
|
122
|
+
method: string,
|
|
123
|
+
path: string,
|
|
124
|
+
media?: string,
|
|
125
|
+
): ModelBody | undefined {
|
|
126
|
+
const opdef = def?.paths?.[path]?.[String(method).toLowerCase()]
|
|
127
|
+
if (!isMap(opdef)) {
|
|
128
|
+
return undefined
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return chooseBody(null != def.swagger ?
|
|
132
|
+
swaggerResponseOffers(def, opdef) : openapiResponseOffers(opdef), media)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
function chooseBody(offers: Offer[], media?: string): ModelBody | undefined {
|
|
137
|
+
const ranked = offers
|
|
138
|
+
.sort((a, b) => compare(a.media, b.media))
|
|
139
|
+
.map((offer) => ({ declared: offer.media.trim().toLowerCase(), body: describeBody(offer) }))
|
|
140
|
+
.sort((a, b) => byPreference(a.body, b.body))
|
|
141
|
+
const bodies = ranked.map((entry) => entry.body).filter((body, i, all) =>
|
|
142
|
+
i === all.findIndex((other) => other.media === body.media))
|
|
143
|
+
|
|
144
|
+
// A named media type is matched as declared first, so a range keeps its schema.
|
|
145
|
+
const named = textOf(media)?.toLowerCase()
|
|
146
|
+
const chosen = null == named ? bodies[0] :
|
|
147
|
+
ranked.find((entry) => entry.declared === named)?.body ??
|
|
148
|
+
bodies.find((body) => body.media.toLowerCase() === named) ??
|
|
149
|
+
describeBody({ media: textOf(media) as string })
|
|
150
|
+
|
|
151
|
+
if (null == chosen) {
|
|
152
|
+
return undefined
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const alternatives = bodies.filter((body) => body.media !== chosen.media)
|
|
156
|
+
return 0 < alternatives.length ? { ...chosen, alternatives } : chosen
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
function openapiOffers(opdef: any): Offer[] {
|
|
161
|
+
const content = opdef.requestBody?.content
|
|
162
|
+
if (!isMap(content)) {
|
|
163
|
+
return []
|
|
164
|
+
}
|
|
165
|
+
return Object.keys(content).map((media) => ({
|
|
166
|
+
media,
|
|
167
|
+
schema: content[media]?.schema,
|
|
168
|
+
encoding: content[media]?.encoding,
|
|
169
|
+
}))
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
// A response's `encoding` is ignored, as OpenAPI applies it to request bodies only.
|
|
174
|
+
function openapiResponseOffers(opdef: any): Offer[] {
|
|
175
|
+
const responses = isMap(opdef.responses) ? opdef.responses : {}
|
|
176
|
+
return sortedKeys(responses).filter((status) => SUCCESS_RE.test(status))
|
|
177
|
+
.flatMap((status) => {
|
|
178
|
+
const content = responses[status]?.content
|
|
179
|
+
return isMap(content) ?
|
|
180
|
+
Object.keys(content).map((media) => ({ media, schema: content[media]?.schema })) : []
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
// Swagger declares a body as a `body` parameter or as `formData` parameters,
|
|
186
|
+
// and its media types in `consumes`, the operation's replacing the document's.
|
|
187
|
+
function swaggerOffers(def: any, pathdef: any, opdef: any): Offer[] {
|
|
188
|
+
const params = swaggerParams(pathdef, opdef)
|
|
189
|
+
const body = params.find((param) => 'body' === param.in)
|
|
190
|
+
const form = params.filter((param) =>
|
|
191
|
+
'formData' === param.in && 'string' === typeof param.name && '' !== param.name)
|
|
192
|
+
|
|
193
|
+
if (null == body && 0 === form.length) {
|
|
194
|
+
return []
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const bodySchema = null == body ? undefined : (body.schema ?? {})
|
|
198
|
+
const formSchema = 0 === form.length ? undefined : {
|
|
199
|
+
type: 'object',
|
|
200
|
+
properties: Object.fromEntries(form.map((param) => [param.name, formProperty(param)])),
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const declared = listOf(Array.isArray(opdef.consumes) ? opdef.consumes : def.consumes)
|
|
204
|
+
.filter((media) => null != textOf(media))
|
|
205
|
+
const consumes = 0 < declared.length ? declared : [
|
|
206
|
+
null != body ? JSON_MEDIA :
|
|
207
|
+
form.some((param) => 'file' === param.type) ? MULTIPART_MEDIA : FORM_MEDIA
|
|
208
|
+
]
|
|
209
|
+
|
|
210
|
+
return consumes.map((media: string) => ({
|
|
211
|
+
media,
|
|
212
|
+
schema: fielded(essence(media)) ? (formSchema ?? bodySchema) : (bodySchema ?? formSchema),
|
|
213
|
+
swagger: true,
|
|
214
|
+
}))
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
// An operation's parameter replaces the path's of the same location and name.
|
|
219
|
+
function swaggerParams(pathdef: any, opdef: any): any[] {
|
|
220
|
+
const key = (param: any) => (textOf(param.in) ?? '') + '\u0000' + (textOf(param.name) ?? '')
|
|
221
|
+
const own = listOf(opdef.parameters).filter(isMap)
|
|
222
|
+
const owned = new Set(own.map(key))
|
|
223
|
+
return [...own, ...listOf(pathdef?.parameters).filter(isMap).filter((param) => !owned.has(key(param)))]
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
// A Swagger response with no schema has no body.
|
|
228
|
+
function swaggerResponseOffers(def: any, opdef: any): Offer[] {
|
|
229
|
+
const responses = isMap(opdef.responses) ? opdef.responses : {}
|
|
230
|
+
const status = sortedKeys(responses).find((code) =>
|
|
231
|
+
SUCCESS_RE.test(code) && null != responses[code]?.schema)
|
|
232
|
+
if (null == status) {
|
|
233
|
+
return []
|
|
234
|
+
}
|
|
235
|
+
const declared = listOf(Array.isArray(opdef.produces) ? opdef.produces : def.produces)
|
|
236
|
+
.filter((media) => null != textOf(media))
|
|
237
|
+
return (0 < declared.length ? declared : [JSON_MEDIA])
|
|
238
|
+
.map((media: string) => ({ media, schema: responses[status].schema }))
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
function formProperty(param: any): any {
|
|
243
|
+
const prop: any = {}
|
|
244
|
+
for (const key of ['type', 'format', 'items', 'collectionFormat']) {
|
|
245
|
+
if (null != param[key]) {
|
|
246
|
+
prop[key] = param[key]
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return prop
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
function describeBody(offer: Offer): ModelBody {
|
|
254
|
+
const media = offer.media.trim()
|
|
255
|
+
const type = essence(media)
|
|
256
|
+
const [major, minor = ''] = type.split('/')
|
|
257
|
+
|
|
258
|
+
if (JSON_MEDIA === type || 'text/json' === type || minor.endsWith('+json')) {
|
|
259
|
+
return { kind: 'json', media: type.includes('*') ? JSON_MEDIA : media }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (FORM_MEDIA === type) {
|
|
263
|
+
return withFields({ kind: 'form', media }, offer)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if ('multipart' === major) {
|
|
267
|
+
return withFields({ kind: 'multipart', media: '*' === minor ? MULTIPART_MEDIA : media }, offer)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// A range that admits JSON stays JSON unless its schema is bytes.
|
|
271
|
+
if ('*' === minor && ('*' === major || 'application' === major)) {
|
|
272
|
+
return binarySchema(offer.schema) ?
|
|
273
|
+
{ kind: 'raw', media: OCTET_MEDIA, binary: true } :
|
|
274
|
+
{ kind: 'json', media: JSON_MEDIA }
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const body: ModelBody = { kind: 'raw', media }
|
|
278
|
+
if (rawBinary(type, offer.schema)) {
|
|
279
|
+
body.binary = true
|
|
280
|
+
}
|
|
281
|
+
return body
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
function withFields(body: ModelBody, offer: Offer): ModelBody {
|
|
286
|
+
const props = mergedProperties(offer.schema)
|
|
287
|
+
const fields = sortedKeys(props).map((name: string) =>
|
|
288
|
+
bodyField(name, props![name], offer.encoding?.[name], offer.swagger ? 'swagger' : body.kind))
|
|
289
|
+
if (0 < fields.length) {
|
|
290
|
+
body.fields = fields
|
|
291
|
+
}
|
|
292
|
+
return body
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
function bodyField(name: string, prop: any, encoding: any, arrays: string): ModelBodyField {
|
|
297
|
+
const list = hasType(prop, 'array')
|
|
298
|
+
const item = list ? prop.items : prop
|
|
299
|
+
const field: ModelBodyField = { name }
|
|
300
|
+
if (binarySchema(item)) {
|
|
301
|
+
field.binary = true
|
|
302
|
+
}
|
|
303
|
+
if (list) {
|
|
304
|
+
const join = arrayJoin(prop, encoding, arrays)
|
|
305
|
+
if (null == join) {
|
|
306
|
+
field.list = true
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
field.join = join
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const media = textOf(encoding?.contentType) ?? textOf(isMap(item) ? item.contentMediaType : undefined)
|
|
313
|
+
if (null != media) {
|
|
314
|
+
field.media = media
|
|
315
|
+
}
|
|
316
|
+
return field
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
// The delimiter an array's items are joined with, or undefined when each item
|
|
321
|
+
// is sent as a field of its own: Swagger's `collectionFormat` (`csv` unless
|
|
322
|
+
// `multi`), a form's `style` and `explode`, and always for a multipart part.
|
|
323
|
+
function arrayJoin(prop: any, encoding: any, arrays: string): string | undefined {
|
|
324
|
+
if ('swagger' === arrays) {
|
|
325
|
+
const format = 'string' === typeof prop.collectionFormat ? prop.collectionFormat : 'csv'
|
|
326
|
+
return 'multi' === format ? undefined : delimiter(format)
|
|
327
|
+
}
|
|
328
|
+
if ('form' === arrays) {
|
|
329
|
+
const style = 'string' === typeof encoding?.style ? encoding.style : 'form'
|
|
330
|
+
const explode = 'boolean' === typeof encoding?.explode ? encoding.explode : 'form' === style
|
|
331
|
+
return explode ? undefined : delimiter(style)
|
|
332
|
+
}
|
|
333
|
+
return undefined
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
function delimiter(format: string): string {
|
|
338
|
+
return 'ssv' === format || 'spaceDelimited' === format ? ' ' :
|
|
339
|
+
'tsv' === format ? '\t' :
|
|
340
|
+
'pipes' === format || 'pipeDelimited' === format ? '|' : ','
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
// Bytes unless the schema says text, or the media type is text and the schema typed.
|
|
345
|
+
function rawBinary(type: string, schema: any): boolean {
|
|
346
|
+
if (encodedText(schema)) {
|
|
347
|
+
return false
|
|
348
|
+
}
|
|
349
|
+
return untyped(schema) || binarySchema(schema) || !textMedia(type)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
function binarySchema(schema: any): boolean {
|
|
354
|
+
return isMap(schema) && !encodedText(schema) &&
|
|
355
|
+
('binary' === schema.format || 'file' === schema.type || null != schema.contentMediaType)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
function encodedText(schema: any): boolean {
|
|
360
|
+
return isMap(schema) && ('byte' === schema.format || null != schema.contentEncoding)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
function untyped(schema: any): boolean {
|
|
365
|
+
return !isMap(schema) || !TYPING_KEYS.some((key) => null != schema[key])
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
function textMedia(type: string): boolean {
|
|
370
|
+
const [major, minor = ''] = type.split('/')
|
|
371
|
+
return 'text' === major || 'xml' === minor || minor.endsWith('+xml')
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
function fielded(type: string): boolean {
|
|
376
|
+
return FORM_MEDIA === type || type.startsWith('multipart/')
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
function byPreference(a: ModelBody, b: ModelBody): number {
|
|
381
|
+
return KIND_ORDER.indexOf(a.kind) - KIND_ORDER.indexOf(b.kind) ||
|
|
382
|
+
Number(JSON_MEDIA !== essence(a.media)) - Number(JSON_MEDIA !== essence(b.media)) ||
|
|
383
|
+
compare(a.media, b.media)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
function essence(media: string): string {
|
|
388
|
+
return media.split(';')[0].trim().toLowerCase()
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
function hasType(schema: any, type: string): boolean {
|
|
393
|
+
return isMap(schema) &&
|
|
394
|
+
(type === schema.type || (Array.isArray(schema.type) && schema.type.includes(type)))
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
function textOf(val: any): string | undefined {
|
|
399
|
+
return 'string' === typeof val && '' !== val.trim() ? val.trim() : undefined
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
function listOf(val: any): any[] {
|
|
404
|
+
return Array.isArray(val) ? val : []
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
function isMap(val: any): boolean {
|
|
409
|
+
return null != val && 'object' === typeof val && !Array.isArray(val)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
function compare(a: string, b: string): number {
|
|
414
|
+
return a < b ? -1 : a > b ? 1 : 0
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
export {
|
|
419
|
+
bodyTransform,
|
|
420
|
+
requestBody,
|
|
421
|
+
responseBody,
|
|
422
|
+
}
|
package/src/transform/entity.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
ModelEntity,
|
|
23
23
|
} from '../model'
|
|
24
24
|
|
|
25
|
-
import { depluralize, guideActive } from '../utility'
|
|
25
|
+
import { depluralize, guideActive, paramName } from '../utility'
|
|
26
26
|
|
|
27
27
|
import { byCodePoint } from '../refcount'
|
|
28
28
|
|
|
@@ -98,6 +98,8 @@ function mergeCollectionPaths(
|
|
|
98
98
|
guide: any,
|
|
99
99
|
log?: any,
|
|
100
100
|
recordRef?: (pathStr: string, methods: string[], collection: boolean) => string | null,
|
|
101
|
+
distinct?: (sharePath: string, shareMethods: string[],
|
|
102
|
+
itemPath: string, itemMethods: string[]) => boolean,
|
|
101
103
|
): string[] {
|
|
102
104
|
const entities = guide.entity as Record<string, any>
|
|
103
105
|
const emptied: string[] = []
|
|
@@ -153,6 +155,11 @@ function mergeCollectionPaths(
|
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
157
|
|
|
158
|
+
// A share answering with its own record, unlike the owner's item's, stays.
|
|
159
|
+
const apart = (ename: string, pathStr: string, owner: CollectionOwner) =>
|
|
160
|
+
true === distinct?.(pathStr, methodsOf(entities[ename].path[pathStr]),
|
|
161
|
+
owner.route, methodsOf(entities[owner.ename]?.path?.[owner.route]))
|
|
162
|
+
|
|
156
163
|
// Second pass: for each entity with a "/X" path, if X has an owner
|
|
157
164
|
// elsewhere, move the path there.
|
|
158
165
|
for (const [ename, entity] of Object.entries(entities)) {
|
|
@@ -161,7 +168,7 @@ function mergeCollectionPaths(
|
|
|
161
168
|
|
|
162
169
|
for (const pathStr of Object.keys(entity.path)) {
|
|
163
170
|
const owner = owned[pathStr]
|
|
164
|
-
if (null != owner && owner.ename !== ename) {
|
|
171
|
+
if (null != owner && owner.ename !== ename && !apart(ename, pathStr, owner)) {
|
|
165
172
|
pathsToMove.push([pathStr, owner])
|
|
166
173
|
}
|
|
167
174
|
}
|
|
@@ -250,11 +257,11 @@ function resolvePathList(guideEntity: GuideEntity, def: { paths: Record<string,
|
|
|
250
257
|
.filter(p => '' != p)
|
|
251
258
|
.map(p => {
|
|
252
259
|
if ('{' !== p[0] || '}' !== p[p.length - 1]) {
|
|
253
|
-
return { lit: p }
|
|
260
|
+
return { lit: nameLitParams(p, rename.param) }
|
|
254
261
|
}
|
|
255
262
|
const raw = p.slice(1, -1)
|
|
256
263
|
if ('' === raw || raw.includes('{') || raw.includes('}')) {
|
|
257
|
-
return { lit: p }
|
|
264
|
+
return { lit: nameLitParams(p, rename.param) }
|
|
258
265
|
}
|
|
259
266
|
// Renames map the spec's parameter name to the model's. Applied
|
|
260
267
|
// here, on the NAME, rather than by rewriting a braced string.
|
|
@@ -281,6 +288,15 @@ function resolvePathList(guideEntity: GuideEntity, def: { paths: Record<string,
|
|
|
281
288
|
}
|
|
282
289
|
|
|
283
290
|
|
|
291
|
+
// The runtimes fill a literal's placeholders by their parameters' model names.
|
|
292
|
+
function nameLitParams(lit: string, renames?: Record<string, any>): string {
|
|
293
|
+
return lit.replace(/\{([^{}]+)\}/g, (placeholder: string, wire: string) => {
|
|
294
|
+
const name = paramName(wire, renames)
|
|
295
|
+
return '' === name ? placeholder : '{' + name + '}'
|
|
296
|
+
})
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
|
|
284
300
|
// Root-field equivalent of resolvePathList for GraphQL guides. A root field
|
|
285
301
|
// has no path to split, so `segments` stays empty (GraphQL points address the
|
|
286
302
|
// single endpoint and carry their operation document instead) and `def` is
|
package/src/transform/select.ts
CHANGED
|
@@ -44,6 +44,7 @@ const selectTransform: Transform = async function(
|
|
|
44
44
|
})
|
|
45
45
|
if (null != mop.points && 0 < mop.points.length) {
|
|
46
46
|
sortPoints(guide, ment, mop)
|
|
47
|
+
warnSharedSelectors(ctx, ment, mop)
|
|
47
48
|
}
|
|
48
49
|
})
|
|
49
50
|
|
|
@@ -66,16 +67,16 @@ function resolveSelect(
|
|
|
66
67
|
|
|
67
68
|
const argkinds = ['params', 'query', 'header', 'cookie']
|
|
68
69
|
|
|
69
|
-
// `exist` names values that must be PRESENT for this point to be chosen
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
// required
|
|
74
|
-
const
|
|
70
|
+
// `exist` names values that must be PRESENT for this point to be chosen,
|
|
71
|
+
// so an optional argument would make the point unreachable to a caller
|
|
72
|
+
// who omits it. A path parameter fills the route and always counts; a
|
|
73
|
+
// GraphQL root field's arguments are params too, and count only when
|
|
74
|
+
// required.
|
|
75
|
+
const graphql = 'graphql' === (mpoint as any).k
|
|
75
76
|
|
|
76
77
|
argkinds.map((kind: string) => {
|
|
77
78
|
each(margs[kind], (marg: ModelArg) => {
|
|
78
|
-
if (
|
|
79
|
+
if (!marg.r && (graphql || 'params' !== kind)) {
|
|
79
80
|
return
|
|
80
81
|
}
|
|
81
82
|
if (!select.exist.includes(marg.n)) {
|
|
@@ -137,6 +138,34 @@ function sortPoints(
|
|
|
137
138
|
})
|
|
138
139
|
}
|
|
139
140
|
|
|
141
|
+
// The first point whose selector matches is chosen, so of the points that
|
|
142
|
+
// share a selector only the first is ever reached.
|
|
143
|
+
function warnSharedSelectors(ctx: any, ment: ModelEntity, mop: ModelOp) {
|
|
144
|
+
const groups = new Map<string, ModelPoint[]>()
|
|
145
|
+
for (const mpoint of mop.points) {
|
|
146
|
+
const key = JSON.stringify([mpoint.q.$action ?? null, mpoint.q.exist])
|
|
147
|
+
groups.set(key, [...(groups.get(key) ?? []), mpoint])
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
for (const group of groups.values()) {
|
|
151
|
+
if (group.length < 2) continue
|
|
152
|
+
const points = group.map((mpoint) => mpoint.m + ' ' + mpoint.o)
|
|
153
|
+
const { exist, $action } = group[0].q
|
|
154
|
+
ctx.warn?.({
|
|
155
|
+
note: `Points ${points.slice(0, -1).join(', ')} and ${points[points.length - 1]}` +
|
|
156
|
+
` on entity=${ment.name} op=${mop.name} have the same selector` +
|
|
157
|
+
` (exist: ${exist.join(',') || 'none'}` +
|
|
158
|
+
(null == $action ? '' : `; $action: ${$action}`) +
|
|
159
|
+
`), so only ${points[0]} is ever chosen.` +
|
|
160
|
+
' An action or another entity in guide.aontu tells them apart.',
|
|
161
|
+
entity: ment.name,
|
|
162
|
+
op: mop.name,
|
|
163
|
+
points,
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
140
169
|
export {
|
|
141
170
|
selectTransform,
|
|
142
171
|
}
|
package/src/transform/top.ts
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
firstSentence, authExchangeOp, specSecuredByDefault, sortedEntries,
|
|
8
8
|
} from '../utility'
|
|
9
9
|
|
|
10
|
+
import { METHODS } from '../resolved'
|
|
11
|
+
|
|
10
12
|
import type { TransformResult } from '../transform'
|
|
11
13
|
|
|
12
14
|
import type {
|
|
@@ -250,28 +252,109 @@ function isHttpUrl(v: any): boolean {
|
|
|
250
252
|
}
|
|
251
253
|
|
|
252
254
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
255
|
+
// A client sends one credential, so the primary scheme comes from an entry
|
|
256
|
+
// naming a single scheme: the one every secured operation lists first, else
|
|
257
|
+
// the definition's first, else the first declared scheme. Every other entry
|
|
258
|
+
// the operations accept is an alternative, its schemes sent together.
|
|
259
|
+
function resolveSecurity(def: any): Record<string, any> | null {
|
|
260
|
+
const defined = def.components?.securitySchemes ?? def.securityDefinitions
|
|
261
|
+
const schemes: Record<string, any> = isObject(defined) ? defined : {}
|
|
256
262
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
'object' === typeof def.security[0] ?
|
|
260
|
-
Object.keys(def.security[0])[0] : undefined
|
|
263
|
+
const declared = (name: string) =>
|
|
264
|
+
Object.prototype.hasOwnProperty.call(schemes, name) && isObject(schemes[name])
|
|
261
265
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
266
|
+
const opsecurity = operationSecurity(def)
|
|
267
|
+
const firsts = new Set(opsecurity.map((reqs) => firstSingleScheme(reqs, declared)))
|
|
268
|
+
firsts.delete(undefined)
|
|
269
|
+
|
|
270
|
+
const schemeName: string | undefined =
|
|
271
|
+
(1 === firsts.size ? [...firsts][0] : undefined) ??
|
|
272
|
+
firstSingleScheme(def.security, declared) ??
|
|
273
|
+
Object.keys(schemes)[0]
|
|
265
274
|
|
|
266
275
|
const scheme = null == schemeName ? null : schemes[schemeName]
|
|
267
|
-
if (
|
|
276
|
+
if (!isObject(scheme)) {
|
|
268
277
|
return null
|
|
269
278
|
}
|
|
270
279
|
|
|
280
|
+
const out: Record<string, any> = describeScheme(def, schemeName as string, scheme)
|
|
281
|
+
|
|
282
|
+
const alternatives = otherSchemeSets(
|
|
283
|
+
0 < opsecurity.length ? opsecurity : [def.security], schemeName as string, declared)
|
|
284
|
+
if (0 < alternatives.length) {
|
|
285
|
+
out.alternatives = alternatives.map((names) =>
|
|
286
|
+
names.map((name) => describeScheme(def, name, schemes[name])))
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return out
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
function isObject(v: any): boolean {
|
|
294
|
+
return null != v && 'object' === typeof v && !Array.isArray(v)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
// Each operation's security list: its own, else the definition's.
|
|
299
|
+
function operationSecurity(def: any): any[][] {
|
|
300
|
+
const out: any[][] = []
|
|
301
|
+
for (const [, pathItem] of sortedEntries(isObject(def.paths) ? def.paths : {})) {
|
|
302
|
+
if (!isObject(pathItem)) continue
|
|
303
|
+
for (const method of METHODS) {
|
|
304
|
+
const op = pathItem[method]
|
|
305
|
+
if (!isObject(op)) continue
|
|
306
|
+
const reqs = Array.isArray(op.security) ? op.security : def.security
|
|
307
|
+
if (Array.isArray(reqs)) out.push(reqs)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return out
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
// Sorted, so a set of schemes compares and prints the same in both ports.
|
|
315
|
+
function schemeSet(req: any): string[] | null {
|
|
316
|
+
return isObject(req) ? Object.keys(req).sort() : null
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
function firstSingleScheme(
|
|
321
|
+
reqs: any, declared: (name: string) => boolean): string | undefined {
|
|
322
|
+
if (!Array.isArray(reqs)) return undefined
|
|
323
|
+
for (const req of reqs) {
|
|
324
|
+
const names = schemeSet(req)
|
|
325
|
+
if (null != names && 1 === names.length && declared(names[0])) {
|
|
326
|
+
return names[0]
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return undefined
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
function otherSchemeSets(
|
|
334
|
+
lists: any[], primary: string, declared: (name: string) => boolean): string[][] {
|
|
335
|
+
const seen = new Set([JSON.stringify([primary])])
|
|
336
|
+
const out: string[][] = []
|
|
337
|
+
for (const reqs of lists) {
|
|
338
|
+
if (!Array.isArray(reqs)) continue
|
|
339
|
+
for (const req of reqs) {
|
|
340
|
+
const names = schemeSet(req)
|
|
341
|
+
if (null == names || 0 === names.length || !names.every(declared)) continue
|
|
342
|
+
const key = JSON.stringify(names)
|
|
343
|
+
if (!seen.has(key)) {
|
|
344
|
+
seen.add(key)
|
|
345
|
+
out.push(names)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return out
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
function describeScheme(def: any, schemeName: string, scheme: any): Record<string, string> {
|
|
271
354
|
const type = String(scheme.type ?? '').toLowerCase()
|
|
272
355
|
|
|
273
356
|
const out: Record<string, string> = {
|
|
274
|
-
scheme: schemeName
|
|
357
|
+
scheme: schemeName,
|
|
275
358
|
type: scheme.type ?? '',
|
|
276
359
|
in: scheme.in ?? 'header',
|
|
277
360
|
name: scheme.name ?? 'Authorization',
|
package/src/types.ts
CHANGED
|
@@ -306,6 +306,12 @@ type GuidePathOp = {
|
|
|
306
306
|
// an HTTP verb (points still synthesize method 'POST').
|
|
307
307
|
optype?: string
|
|
308
308
|
why_op: string[]
|
|
309
|
+
body?: {
|
|
310
|
+
media?: string
|
|
311
|
+
}
|
|
312
|
+
response?: {
|
|
313
|
+
media?: string
|
|
314
|
+
}
|
|
309
315
|
transform: {
|
|
310
316
|
req: any
|
|
311
317
|
res: any
|