@voxgig/apidef 8.22.1 → 8.24.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/builder/entity/entity.d.ts +2 -1
- package/dist/builder/entity/entity.js +14 -11
- package/dist/builder/entity/entity.js.map +1 -1
- package/dist/guide/guide.js +5 -2
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.d.ts +11 -1
- package/dist/guide/heuristic01.js +457 -100
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/model.d.ts +18 -1
- package/dist/transform/args.d.ts +4 -1
- package/dist/transform/args.js +67 -20
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/body.d.ts +23 -0
- package/dist/transform/body.js +474 -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/field.d.ts +3 -1
- package/dist/transform/field.js +159 -87
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/operation.js +77 -24
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/select.js +44 -11
- package/dist/transform/select.js.map +1 -1
- package/dist/transform/top.d.ts +3 -2
- package/dist/transform/top.js +127 -11
- 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 +11 -1
- package/dist/utility.js +156 -13
- 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/builder/entity/entity.ts +17 -15
- package/src/guide/guide.ts +5 -3
- package/src/guide/heuristic01.ts +587 -104
- package/src/model.ts +27 -0
- package/src/transform/args.ts +83 -22
- package/src/transform/body.ts +638 -0
- package/src/transform/entity.ts +20 -4
- package/src/transform/field.ts +190 -88
- package/src/transform/operation.ts +97 -26
- package/src/transform/select.ts +49 -11
- package/src/transform/top.ts +154 -15
- package/src/types.ts +6 -0
- package/src/utility.ts +190 -13
|
@@ -0,0 +1,638 @@
|
|
|
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
|
+
type Ranked = {
|
|
41
|
+
offer: Offer
|
|
42
|
+
declared: string
|
|
43
|
+
body: ModelBody
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const bodyTransform: Transform = async function(
|
|
48
|
+
ctx: any,
|
|
49
|
+
): Promise<TransformResult> {
|
|
50
|
+
const { apimodel, def, guide } = ctx
|
|
51
|
+
const entities = apimodel.main[KIT].entity
|
|
52
|
+
|
|
53
|
+
let msg = 'body '
|
|
54
|
+
|
|
55
|
+
for (const entname of sortedKeys(entities)) {
|
|
56
|
+
const ops = entities[entname].op ?? {}
|
|
57
|
+
for (const opname of sortedKeys(ops)) {
|
|
58
|
+
for (const mpoint of (ops[opname]?.points ?? []) as ModelPoint[]) {
|
|
59
|
+
if ('graphql' === mpoint.k) {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
const media = guideMedia(guide, entname, opname, mpoint)
|
|
63
|
+
const rb = requestBody(def, mpoint.m, mpoint.o, media.body)
|
|
64
|
+
if (null != rb) {
|
|
65
|
+
mpoint.rb = rb
|
|
66
|
+
}
|
|
67
|
+
const rs = responseBody(def, mpoint.m, mpoint.o, media.response)
|
|
68
|
+
if (null != rs) {
|
|
69
|
+
mpoint.rs = rs
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
msg += entname + ' '
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return { ok: true, msg }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// The entry of the point's own op, as ops can share a path and method.
|
|
81
|
+
// A patch the operation pass promotes to update keeps its entry under patch.
|
|
82
|
+
function guideMedia(
|
|
83
|
+
guide: any,
|
|
84
|
+
entname: string,
|
|
85
|
+
opname: string,
|
|
86
|
+
mpoint: ModelPoint,
|
|
87
|
+
): { body?: string, response?: string } {
|
|
88
|
+
const gops = guide?.entity?.[entname]?.path?.[mpoint.o]?.op ?? {}
|
|
89
|
+
for (const name of 'update' === opname ? ['update', 'patch'] : [opname]) {
|
|
90
|
+
const gop = gops[name]
|
|
91
|
+
if (null != gop && guideActive(gop) &&
|
|
92
|
+
String(gop.method ?? '').toUpperCase() === String(mpoint.m).toUpperCase()) {
|
|
93
|
+
return { body: textOf(gop.body?.media), response: textOf(gop.response?.media) }
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
// Undefined when the operation sends JSON alone.
|
|
101
|
+
function requestBody(
|
|
102
|
+
def: any,
|
|
103
|
+
method: string,
|
|
104
|
+
path: string,
|
|
105
|
+
media?: string,
|
|
106
|
+
): ModelBody | undefined {
|
|
107
|
+
const offers = requestOffers(def, method, path)
|
|
108
|
+
if (null == offers) {
|
|
109
|
+
return undefined
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const body = chooseBody(offers, media)
|
|
113
|
+
|
|
114
|
+
if (null == body || ('json' === body.kind && JSON_MEDIA === essence(body.media) &&
|
|
115
|
+
(body.alternatives ?? []).every((other) => 'json' === other.kind))) {
|
|
116
|
+
return undefined
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return body
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
// Undefined when no success response declares a body.
|
|
124
|
+
function responseBody(
|
|
125
|
+
def: any,
|
|
126
|
+
method: string,
|
|
127
|
+
path: string,
|
|
128
|
+
media?: string,
|
|
129
|
+
): ModelBody | undefined {
|
|
130
|
+
const opdef = def?.paths?.[path]?.[String(method).toLowerCase()]
|
|
131
|
+
if (!isMap(opdef)) {
|
|
132
|
+
return undefined
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return chooseBody(null != def.swagger ?
|
|
136
|
+
swaggerResponseOffers(def, opdef) : openapiResponseOffers(opdef), media)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
function requestOffers(def: any, method: string, path: string): Offer[] | undefined {
|
|
141
|
+
const pathdef = def?.paths?.[path]
|
|
142
|
+
const opdef = pathdef?.[String(method).toLowerCase()]
|
|
143
|
+
if (!isMap(opdef)) {
|
|
144
|
+
return undefined
|
|
145
|
+
}
|
|
146
|
+
return null != def.swagger ? swaggerOffers(def, pathdef, opdef) : openapiOffers(opdef)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
function chooseBody(offers: Offer[], media?: string): ModelBody | undefined {
|
|
151
|
+
const ranked = rankOffers(offers)
|
|
152
|
+
const bodies = ranked.map((entry) => entry.body).filter((body, i, all) =>
|
|
153
|
+
i === all.findIndex((other) => other.media === body.media))
|
|
154
|
+
|
|
155
|
+
const chosen = chooseOffer(ranked, media)?.body ??
|
|
156
|
+
(null == textOf(media) ? undefined : describeBody({ media: textOf(media) as string }))
|
|
157
|
+
|
|
158
|
+
if (null == chosen) {
|
|
159
|
+
return undefined
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const alternatives = bodies.filter((body) => body.media !== chosen.media)
|
|
163
|
+
return 0 < alternatives.length ? { ...chosen, alternatives } : chosen
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
function rankOffers(offers: Offer[]): Ranked[] {
|
|
168
|
+
return offers
|
|
169
|
+
.sort((a, b) => compare(a.media, b.media))
|
|
170
|
+
.map((offer) => ({ offer, declared: offer.media.trim().toLowerCase(), body: describeBody(offer) }))
|
|
171
|
+
.sort((a, b) => byPreference(a.body, b.body))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
// A named media type is matched as declared first, so a range keeps its schema.
|
|
176
|
+
function chooseOffer(ranked: Ranked[], media?: string): Ranked | undefined {
|
|
177
|
+
const named = textOf(media)?.toLowerCase()
|
|
178
|
+
return null == named ? ranked[0] :
|
|
179
|
+
ranked.find((entry) => entry.declared === named) ??
|
|
180
|
+
ranked.find((entry) => entry.body.media.toLowerCase() === named)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
function openapiOffers(opdef: any): Offer[] {
|
|
185
|
+
const content = opdef.requestBody?.content
|
|
186
|
+
if (!isMap(content)) {
|
|
187
|
+
return []
|
|
188
|
+
}
|
|
189
|
+
return Object.keys(content).map((media) => ({
|
|
190
|
+
media,
|
|
191
|
+
schema: content[media]?.schema,
|
|
192
|
+
encoding: content[media]?.encoding,
|
|
193
|
+
}))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
// A response's `encoding` is ignored, as OpenAPI applies it to request bodies only.
|
|
198
|
+
function openapiResponseOffers(opdef: any): Offer[] {
|
|
199
|
+
const responses = isMap(opdef.responses) ? opdef.responses : {}
|
|
200
|
+
return sortedKeys(responses).filter((status) => SUCCESS_RE.test(status))
|
|
201
|
+
.flatMap((status) => {
|
|
202
|
+
const content = responses[status]?.content
|
|
203
|
+
return isMap(content) ?
|
|
204
|
+
Object.keys(content).map((media) => ({ media, schema: content[media]?.schema })) : []
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
// Swagger declares a body as a `body` parameter or as `formData` parameters,
|
|
210
|
+
// and its media types in `consumes`, the operation's replacing the document's.
|
|
211
|
+
function swaggerOffers(def: any, pathdef: any, opdef: any): Offer[] {
|
|
212
|
+
const params = swaggerParams(pathdef, opdef)
|
|
213
|
+
const body = params.find((param) => 'body' === param.in)
|
|
214
|
+
const form = params.filter((param) =>
|
|
215
|
+
'formData' === param.in && 'string' === typeof param.name && '' !== param.name)
|
|
216
|
+
|
|
217
|
+
if (null == body && 0 === form.length) {
|
|
218
|
+
return []
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const bodySchema = null == body ? undefined : (body.schema ?? {})
|
|
222
|
+
const required = form.filter((param) => true === param.required).map((param) => param.name)
|
|
223
|
+
const formSchema = 0 === form.length ? undefined : {
|
|
224
|
+
type: 'object',
|
|
225
|
+
properties: Object.fromEntries(form.map((param) => [param.name, formProperty(param)])),
|
|
226
|
+
...(0 < required.length ? { required } : {}),
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const declared = listOf(Array.isArray(opdef.consumes) ? opdef.consumes : def.consumes)
|
|
230
|
+
.filter((media) => null != textOf(media))
|
|
231
|
+
const consumes = 0 < declared.length ? declared : [
|
|
232
|
+
null != body ? JSON_MEDIA :
|
|
233
|
+
form.some((param) => 'file' === param.type) ? MULTIPART_MEDIA : FORM_MEDIA
|
|
234
|
+
]
|
|
235
|
+
|
|
236
|
+
return consumes.map((media: string) => ({
|
|
237
|
+
media,
|
|
238
|
+
schema: fielded(essence(media)) ? (formSchema ?? bodySchema) : (bodySchema ?? formSchema),
|
|
239
|
+
swagger: true,
|
|
240
|
+
}))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
// An operation's parameter replaces the path's of the same location and name.
|
|
245
|
+
function swaggerParams(pathdef: any, opdef: any): any[] {
|
|
246
|
+
const key = (param: any) => (textOf(param.in) ?? '') + '\u0000' + (textOf(param.name) ?? '')
|
|
247
|
+
const own = listOf(opdef.parameters).filter(isMap)
|
|
248
|
+
const owned = new Set(own.map(key))
|
|
249
|
+
return [...own, ...listOf(pathdef?.parameters).filter(isMap).filter((param) => !owned.has(key(param)))]
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
// A Swagger response with no schema has no body.
|
|
254
|
+
function swaggerResponseOffers(def: any, opdef: any): Offer[] {
|
|
255
|
+
const responses = isMap(opdef.responses) ? opdef.responses : {}
|
|
256
|
+
const status = sortedKeys(responses).find((code) =>
|
|
257
|
+
SUCCESS_RE.test(code) && null != responses[code]?.schema)
|
|
258
|
+
if (null == status) {
|
|
259
|
+
return []
|
|
260
|
+
}
|
|
261
|
+
const declared = listOf(Array.isArray(opdef.produces) ? opdef.produces : def.produces)
|
|
262
|
+
.filter((media) => null != textOf(media))
|
|
263
|
+
return (0 < declared.length ? declared : [JSON_MEDIA])
|
|
264
|
+
.map((media: string) => ({ media, schema: responses[status].schema }))
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
function formProperty(param: any): any {
|
|
269
|
+
const prop: any = {}
|
|
270
|
+
for (const key of ['type', 'format', 'items', 'collectionFormat']) {
|
|
271
|
+
if (null != param[key]) {
|
|
272
|
+
prop[key] = param[key]
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return prop
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
function describeBody(offer: Offer): ModelBody {
|
|
280
|
+
const media = offer.media.trim()
|
|
281
|
+
const type = essence(media)
|
|
282
|
+
const [major, minor = ''] = type.split('/')
|
|
283
|
+
|
|
284
|
+
if (JSON_MEDIA === type || 'text/json' === type || minor.endsWith('+json')) {
|
|
285
|
+
return { kind: 'json', media: type.includes('*') ? JSON_MEDIA : media }
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (FORM_MEDIA === type) {
|
|
289
|
+
return withFields({ kind: 'form', media }, offer)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if ('multipart' === major) {
|
|
293
|
+
return withFields({ kind: 'multipart', media: '*' === minor ? MULTIPART_MEDIA : media }, offer)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// A range that admits JSON stays JSON unless its schema is bytes.
|
|
297
|
+
if ('*' === minor && ('*' === major || 'application' === major)) {
|
|
298
|
+
return binarySchema(offer.schema) ?
|
|
299
|
+
{ kind: 'raw', media: OCTET_MEDIA, binary: true } :
|
|
300
|
+
{ kind: 'json', media: JSON_MEDIA }
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const body: ModelBody = { kind: 'raw', media }
|
|
304
|
+
if (rawBinary(type, offer.schema)) {
|
|
305
|
+
body.binary = true
|
|
306
|
+
}
|
|
307
|
+
return body
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
function withFields(body: ModelBody, offer: Offer): ModelBody {
|
|
312
|
+
const props = mergedProperties(offer.schema)
|
|
313
|
+
const fields = sortedKeys(props).map((name: string) =>
|
|
314
|
+
bodyField(name, props![name], offer.encoding?.[name], offer.swagger ? 'swagger' : body.kind))
|
|
315
|
+
if (0 < fields.length) {
|
|
316
|
+
body.fields = fields
|
|
317
|
+
}
|
|
318
|
+
return body
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
function bodyField(name: string, prop: any, encoding: any, arrays: string): ModelBodyField {
|
|
323
|
+
const list = hasType(prop, 'array')
|
|
324
|
+
const item = list ? prop.items : prop
|
|
325
|
+
const field: ModelBodyField = { name }
|
|
326
|
+
if (binarySchema(item)) {
|
|
327
|
+
field.binary = true
|
|
328
|
+
}
|
|
329
|
+
if (list) {
|
|
330
|
+
const join = arrayJoin(prop, encoding, arrays)
|
|
331
|
+
if (null == join) {
|
|
332
|
+
field.list = true
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
field.join = join
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const media = textOf(encoding?.contentType) ?? textOf(isMap(item) ? item.contentMediaType : undefined)
|
|
339
|
+
if (null != media) {
|
|
340
|
+
field.media = media
|
|
341
|
+
}
|
|
342
|
+
return field
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
// The delimiter an array's items are joined with, or undefined when each item
|
|
347
|
+
// is sent as a field of its own: Swagger's `collectionFormat` (`csv` unless
|
|
348
|
+
// `multi`), a form's `style` and `explode`, and always for a multipart part.
|
|
349
|
+
function arrayJoin(prop: any, encoding: any, arrays: string): string | undefined {
|
|
350
|
+
if ('swagger' === arrays) {
|
|
351
|
+
const format = 'string' === typeof prop.collectionFormat ? prop.collectionFormat : 'csv'
|
|
352
|
+
return 'multi' === format ? undefined : delimiter(format)
|
|
353
|
+
}
|
|
354
|
+
if ('form' === arrays) {
|
|
355
|
+
const style = 'string' === typeof encoding?.style ? encoding.style : 'form'
|
|
356
|
+
const explode = 'boolean' === typeof encoding?.explode ? encoding.explode : 'form' === style
|
|
357
|
+
return explode ? undefined : delimiter(style)
|
|
358
|
+
}
|
|
359
|
+
return undefined
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
function delimiter(format: string): string {
|
|
364
|
+
return 'ssv' === format || 'spaceDelimited' === format ? ' ' :
|
|
365
|
+
'tsv' === format ? '\t' :
|
|
366
|
+
'pipes' === format || 'pipeDelimited' === format ? '|' : ','
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
// Bytes unless the schema says text, or the media type is text and the schema typed.
|
|
371
|
+
function rawBinary(type: string, schema: any): boolean {
|
|
372
|
+
if (encodedText(schema)) {
|
|
373
|
+
return false
|
|
374
|
+
}
|
|
375
|
+
return untyped(schema) || binarySchema(schema) || !textMedia(type)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
function binarySchema(schema: any): boolean {
|
|
380
|
+
return isMap(schema) && !encodedText(schema) &&
|
|
381
|
+
('binary' === schema.format || 'file' === schema.type || null != schema.contentMediaType)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
function encodedText(schema: any): boolean {
|
|
386
|
+
return isMap(schema) && ('byte' === schema.format || null != schema.contentEncoding)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
function untyped(schema: any): boolean {
|
|
391
|
+
return !isMap(schema) || !TYPING_KEYS.some((key) => null != schema[key])
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
function textMedia(type: string): boolean {
|
|
396
|
+
const [major, minor = ''] = type.split('/')
|
|
397
|
+
return 'text' === major || 'xml' === minor || minor.endsWith('+xml')
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
function fielded(type: string): boolean {
|
|
402
|
+
return FORM_MEDIA === type || type.startsWith('multipart/')
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
function byPreference(a: ModelBody, b: ModelBody): number {
|
|
407
|
+
return KIND_ORDER.indexOf(a.kind) - KIND_ORDER.indexOf(b.kind) ||
|
|
408
|
+
Number(JSON_MEDIA !== essence(a.media)) - Number(JSON_MEDIA !== essence(b.media)) ||
|
|
409
|
+
compare(a.media, b.media)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
function essence(media: string): string {
|
|
414
|
+
return media.split(';')[0].trim().toLowerCase()
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
function hasType(schema: any, type: string): boolean {
|
|
419
|
+
return isMap(schema) &&
|
|
420
|
+
(type === schema.type || (Array.isArray(schema.type) && schema.type.includes(type)))
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
function textOf(val: any): string | undefined {
|
|
425
|
+
return 'string' === typeof val && '' !== val.trim() ? val.trim() : undefined
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
function listOf(val: any): any[] {
|
|
430
|
+
return Array.isArray(val) ? val : []
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
function isMap(val: any): boolean {
|
|
435
|
+
return null != val && 'object' === typeof val && !Array.isArray(val)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
function compare(a: string, b: string): number {
|
|
440
|
+
return a < b ? -1 : a > b ? 1 : 0
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
// The schema the body step sends a request body with, when that body is JSON.
|
|
445
|
+
function jsonSchema(offers: Offer[], media?: string): any {
|
|
446
|
+
const chosen = chooseOffer(rankOffers(offers), media)
|
|
447
|
+
return 'json' === chosen?.body.kind ? chosen.offer.schema : undefined
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
function jsonRequestSchema(opdef: any): any {
|
|
452
|
+
return isMap(opdef) ? jsonSchema(openapiOffers(opdef)) : undefined
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
// The JSON schema a point's request body is sent with, under the media type
|
|
457
|
+
// the guide names, else the one the body step prefers.
|
|
458
|
+
function requestSchema(def: any, method: string, path: string, media?: string): any {
|
|
459
|
+
return jsonSchema(requestOffers(def, method, path) ?? [], media)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
// The schema of the request body offered under the media type the guide
|
|
464
|
+
// names, of any kind, else the preferred JSON one.
|
|
465
|
+
function selectedRequestSchema(def: any, method: string, path: string, media?: string): any {
|
|
466
|
+
const offers = requestOffers(def, method, path) ?? []
|
|
467
|
+
const named = null == textOf(media) ? undefined : chooseOffer(rankOffers(offers), media)
|
|
468
|
+
return null == named ? jsonSchema(offers) : named.offer.schema
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
function arrayRequestSchema(def: any, method: string, path: string, media?: string): any {
|
|
473
|
+
return arrayShape(requestSchema(def, method, path, media))
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
// An array, or an allOf whose parts make one, or a oneOf or anyOf of one, or
|
|
478
|
+
// of arrays alone, beside any null: each fact from the first part that states
|
|
479
|
+
// it, outermost first, the items from the array's own part first, and null
|
|
480
|
+
// only where the composition admits it.
|
|
481
|
+
function arrayShape(schema: any): any {
|
|
482
|
+
const parts: any[] = []
|
|
483
|
+
const visit = (node: any) => {
|
|
484
|
+
if (isMap(node) && !parts.includes(node)) {
|
|
485
|
+
parts.push(node)
|
|
486
|
+
; (Array.isArray(node.allOf) ? node.allOf : []).forEach(visit)
|
|
487
|
+
for (const one of [node.oneOf, node.anyOf]) {
|
|
488
|
+
const members = Array.isArray(one) ? one.filter((member: any) => !nullOnly(member)) : []
|
|
489
|
+
if (1 === members.length) visit(members[0])
|
|
490
|
+
else if (1 < members.length && members.every(arrayOnly)) parts.push(unionArray(members))
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
visit(schema)
|
|
495
|
+
const found = parts.find(isArray)
|
|
496
|
+
const list = null == found || null != found.type ? found : { ...found, type: 'array' }
|
|
497
|
+
if (null == list) {
|
|
498
|
+
return list
|
|
499
|
+
}
|
|
500
|
+
const items = list.items ?? parts.find((part) => null != part.items)?.items
|
|
501
|
+
const description = parts.find((part) => null != part.description)?.description
|
|
502
|
+
const types = [list.type].flat().filter((type: any) => 'null' !== type)
|
|
503
|
+
return {
|
|
504
|
+
...list,
|
|
505
|
+
type: 1 === types.length ? types[0] : types,
|
|
506
|
+
...(null == items ? {} : { items }),
|
|
507
|
+
...(null == description ? {} : { description }),
|
|
508
|
+
nullable: admitsNull(schema),
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
// Null must pass the schema's own type, const and enum, every allOf part,
|
|
514
|
+
// exactly one oneOf member, and some anyOf member.
|
|
515
|
+
function admitsNull(schema: any): boolean {
|
|
516
|
+
if (!isMap(schema)) return false
|
|
517
|
+
const own = (null == schema.type || hasType(schema, 'null') || true === schema.nullable) &&
|
|
518
|
+
(!('const' in schema) || null === schema.const) &&
|
|
519
|
+
(!Array.isArray(schema.enum) || schema.enum.includes(null))
|
|
520
|
+
return own && listOf(schema.allOf).every(admitsNull) &&
|
|
521
|
+
(!Array.isArray(schema.oneOf) || 1 === schema.oneOf.filter(admitsNull).length) &&
|
|
522
|
+
(!Array.isArray(schema.anyOf) || schema.anyOf.some(admitsNull))
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
// Only null passes: by its type, a const or enum of null alone, an allOf part
|
|
527
|
+
// that passes only null, or a oneOf or anyOf every member of which does.
|
|
528
|
+
function nullOnly(schema: any): boolean {
|
|
529
|
+
if (!isMap(schema)) return false
|
|
530
|
+
if ('const' in schema) return null === schema.const
|
|
531
|
+
if (Array.isArray(schema.enum)) return 0 < schema.enum.length && schema.enum.every((v: any) => null === v)
|
|
532
|
+
if (listOf(schema.allOf).some(nullOnly)) return true
|
|
533
|
+
if ([schema.oneOf, schema.anyOf].some((one) => Array.isArray(one) && 0 < one.length && one.every(nullOnly))) {
|
|
534
|
+
return true
|
|
535
|
+
}
|
|
536
|
+
return null != schema.type && [schema.type].flat().every((type) => 'null' === type)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
// Only arrays pass: by a const, or an enum, of arrays alone.
|
|
541
|
+
function arrayValued(schema: any): boolean {
|
|
542
|
+
return Array.isArray(schema.const) ||
|
|
543
|
+
(Array.isArray(schema.enum) && 0 < schema.enum.length && schema.enum.every(Array.isArray))
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
function isArray(schema: any): boolean {
|
|
548
|
+
return isMap(schema) && (hasType(schema, 'array') || (null == schema.type && arrayValued(schema)))
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
// Only arrays pass, beside any null: a union member that admits a string too is
|
|
553
|
+
// no array.
|
|
554
|
+
function arrayOnly(schema: any): boolean {
|
|
555
|
+
if (!isMap(schema)) return false
|
|
556
|
+
if (arrayValued(schema)) return true
|
|
557
|
+
const types = null == schema.type ? [] : [schema.type].flat()
|
|
558
|
+
return types.includes('array') && types.every((type: any) => 'array' === type || 'null' === type)
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
// The items are kept only where every member's items name one component.
|
|
563
|
+
function unionArray(members: any[]): any {
|
|
564
|
+
const ref = members[0].items?.['x-ref']
|
|
565
|
+
const one = null != ref && members.every((member) => ref === member.items?.['x-ref'])
|
|
566
|
+
return { type: 'array', ...(one ? { items: members[0].items } : {}) }
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
// A nullable array says so with `nullable` in OpenAPI 3.0, and a type list in 3.1.
|
|
571
|
+
function nullableType(schema: any): any {
|
|
572
|
+
return true === schema.nullable && !hasType(schema, 'null') ? [schema.type, 'null'].flat() : schema.type
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
const REQDATA_FIELD_RE = /^`reqdata\.([A-Za-z_][A-Za-z0-9_]*)`$/
|
|
577
|
+
|
|
578
|
+
// The field of the request data a point's JSON array body is sent from, when
|
|
579
|
+
// its request transform unwraps one.
|
|
580
|
+
function arrayCarrier(
|
|
581
|
+
def: any,
|
|
582
|
+
mpoint: ModelPoint,
|
|
583
|
+
media?: string,
|
|
584
|
+
): { name: string, required: boolean, type: string | string[], description?: string } | undefined {
|
|
585
|
+
const req: any = mpoint.t?.req
|
|
586
|
+
const name = 'string' === typeof req ? req.match(REQDATA_FIELD_RE)?.[1] : undefined
|
|
587
|
+
const schema = null == name ? undefined : arrayRequestSchema(def, mpoint.m, mpoint.o, media)
|
|
588
|
+
if (null == name || null == schema) {
|
|
589
|
+
return undefined
|
|
590
|
+
}
|
|
591
|
+
const decl = requestDecl(def, mpoint.m, mpoint.o)
|
|
592
|
+
return {
|
|
593
|
+
name,
|
|
594
|
+
required: true === decl?.required,
|
|
595
|
+
type: nullableType(schema),
|
|
596
|
+
description: textOf(decl?.description) ?? textOf(schema.description),
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
// OpenAPI's request body, or the Swagger parameter that is one.
|
|
602
|
+
function requestDecl(def: any, method: string, path: string): any {
|
|
603
|
+
const pathdef = def?.paths?.[path]
|
|
604
|
+
const opdef = pathdef?.[String(method).toLowerCase()]
|
|
605
|
+
if (!isMap(opdef)) {
|
|
606
|
+
return undefined
|
|
607
|
+
}
|
|
608
|
+
return null != def.swagger ?
|
|
609
|
+
swaggerParams(pathdef, opdef).find((param) => 'body' === param.in) : opdef.requestBody
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
// Types compare by value, and a type list as the set it is, in one order.
|
|
614
|
+
function sameType(a: any, b: any): boolean {
|
|
615
|
+
return JSON.stringify(typeSet(a)) === JSON.stringify(typeSet(b))
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
function typeSet(type: any): any {
|
|
620
|
+
return !Array.isArray(type) ? type :
|
|
621
|
+
type.every((member: any) => 'string' === typeof member) ? [...type].sort() : type.map(typeSet)
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
export {
|
|
626
|
+
bodyTransform,
|
|
627
|
+
guideMedia,
|
|
628
|
+
requestBody,
|
|
629
|
+
responseBody,
|
|
630
|
+
jsonRequestSchema,
|
|
631
|
+
requestSchema,
|
|
632
|
+
arrayRequestSchema,
|
|
633
|
+
arrayCarrier,
|
|
634
|
+
requestDecl,
|
|
635
|
+
nullableType,
|
|
636
|
+
sameType,
|
|
637
|
+
selectedRequestSchema,
|
|
638
|
+
}
|
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
|