@voxgig/apidef 8.28.1 → 8.30.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/README.md +18 -0
- package/dist/builder/entity/entity.js +1 -1
- package/dist/builder/entity/entity.js.map +1 -1
- package/dist/builder/entity/info.js +1 -1
- package/dist/builder/entity/info.js.map +1 -1
- package/dist/guide/graphql01.js +6 -3
- package/dist/guide/graphql01.js.map +1 -1
- package/dist/guide/guide.js +2 -10
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.js +87 -18
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/model.d.ts +1 -0
- package/dist/parse.d.ts +2 -1
- package/dist/parse.js +65 -18
- package/dist/parse.js.map +1 -1
- package/dist/transform/entity.js +3 -3
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/field.d.ts +2 -1
- package/dist/transform/field.js +33 -21
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/flowstep.js +21 -1
- package/dist/transform/flowstep.js.map +1 -1
- package/dist/transform/operation.js +13 -24
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/top.js +8 -0
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.d.ts +10 -2
- package/dist/utility.js +177 -28
- package/dist/utility.js.map +1 -1
- package/dist/yamlalias.d.ts +3 -0
- package/dist/yamlalias.js +269 -0
- package/dist/yamlalias.js.map +1 -0
- package/model/apidef.aontu +1 -0
- package/package.json +1 -1
- package/src/builder/entity/entity.ts +1 -1
- package/src/builder/entity/info.ts +1 -1
- package/src/guide/graphql01.ts +9 -3
- package/src/guide/guide.ts +2 -10
- package/src/guide/heuristic01.ts +101 -20
- package/src/model.ts +1 -0
- package/src/parse.ts +68 -26
- package/src/transform/entity.ts +5 -5
- package/src/transform/field.ts +39 -21
- package/src/transform/flowstep.ts +24 -1
- package/src/transform/operation.ts +15 -27
- package/src/transform/top.ts +9 -0
- package/src/utility.ts +204 -28
- package/src/yamlalias.ts +295 -0
package/src/transform/field.ts
CHANGED
|
@@ -113,12 +113,7 @@ const fieldTransform: Transform = async function(
|
|
|
113
113
|
// The FIELD as well as the descriptor, for the reason the branch below
|
|
114
114
|
// documents: a model that declares the descriptor without the field
|
|
115
115
|
// makes the generated type disagree with the generated test.
|
|
116
|
-
fields.id =
|
|
117
|
-
n: 'id',
|
|
118
|
-
h: humanTitle('id'),
|
|
119
|
-
t: '`$STRING`',
|
|
120
|
-
r: false,
|
|
121
|
-
} as ModelField
|
|
116
|
+
fields.id = syntheticIdField()
|
|
122
117
|
}
|
|
123
118
|
|
|
124
119
|
const singleKey = (composite as any).single
|
|
@@ -140,24 +135,14 @@ const fieldTransform: Transform = async function(
|
|
|
140
135
|
// The guide disabled composite; the terminal parameter is the key, and
|
|
141
136
|
// the entity needs the field to carry it for the same reason the
|
|
142
137
|
// composite branch above does.
|
|
143
|
-
fields.id =
|
|
144
|
-
n: 'id',
|
|
145
|
-
h: humanTitle('id'),
|
|
146
|
-
t: '`$STRING`',
|
|
147
|
-
r: false,
|
|
148
|
-
} as ModelField
|
|
138
|
+
fields.id = syntheticIdField()
|
|
149
139
|
}
|
|
150
140
|
|
|
151
141
|
if (idField || null != composite.parts || null != singleKey || keyed) {
|
|
152
142
|
ment.id = { name: 'id', field: 'id', ...composite, ...single }
|
|
153
143
|
}
|
|
154
144
|
else if (addressedById(ment)) {
|
|
155
|
-
fields.id =
|
|
156
|
-
n: 'id',
|
|
157
|
-
h: humanTitle('id'),
|
|
158
|
-
t: '`$STRING`',
|
|
159
|
-
r: false,
|
|
160
|
-
} as ModelField
|
|
145
|
+
fields.id = syntheticIdField()
|
|
161
146
|
|
|
162
147
|
ment.id = { name: 'id', field: 'id', ...composite, ...single }
|
|
163
148
|
}
|
|
@@ -176,6 +161,17 @@ const fieldTransform: Transform = async function(
|
|
|
176
161
|
|
|
177
162
|
|
|
178
163
|
|
|
164
|
+
function syntheticIdField(): ModelField {
|
|
165
|
+
return {
|
|
166
|
+
n: 'id',
|
|
167
|
+
h: humanTitle('id'),
|
|
168
|
+
t: '`$STRING`',
|
|
169
|
+
r: false,
|
|
170
|
+
syn: true,
|
|
171
|
+
} as ModelField
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
179
175
|
const ID_SEP = '/'
|
|
180
176
|
|
|
181
177
|
// Subfields that conventionally carry the identifying value of a nested
|
|
@@ -757,6 +753,10 @@ function modelField(fielddef: SchemaDef): ModelField {
|
|
|
757
753
|
if (null != union) {
|
|
758
754
|
mfield.union = union
|
|
759
755
|
}
|
|
756
|
+
|
|
757
|
+
if (true === (fielddef as any).syn$) {
|
|
758
|
+
mfield.syn = true
|
|
759
|
+
}
|
|
760
760
|
return mfield
|
|
761
761
|
}
|
|
762
762
|
|
|
@@ -880,7 +880,7 @@ function findFieldDefs(
|
|
|
880
880
|
getx(responses, '202 schema')
|
|
881
881
|
}
|
|
882
882
|
if ('list' == mop.name) {
|
|
883
|
-
const unwrapped = unwrapArrayWrapper(fieldSets)
|
|
883
|
+
const unwrapped = transformRecords(fieldSets, mpoint.t?.res) ?? unwrapArrayWrapper(fieldSets)
|
|
884
884
|
envelope = null == unwrapped ? composedEnvelopeProp(fieldSets, 'list') : null
|
|
885
885
|
const itemkey = itemEnvelopeOf(mpoint.t?.res)
|
|
886
886
|
if (unwrapped) {
|
|
@@ -966,9 +966,11 @@ function carrierOf(mpoint: ModelPoint, def: any, media?: string) {
|
|
|
966
966
|
}
|
|
967
967
|
|
|
968
968
|
|
|
969
|
-
// Optional, as the record never holds it.
|
|
969
|
+
// Optional and synthetic, as the record never holds it.
|
|
970
970
|
function carrierDef(carrier: { name: string, type: string | string[], description?: string }): SchemaDef {
|
|
971
|
-
return {
|
|
971
|
+
return {
|
|
972
|
+
key$: carrier.name, type: carrier.type, description: carrier.description, syn$: true,
|
|
973
|
+
} as SchemaDef
|
|
972
974
|
}
|
|
973
975
|
|
|
974
976
|
|
|
@@ -1112,6 +1114,16 @@ function unwrapExample(example: any, envelope?: string | null): any {
|
|
|
1112
1114
|
}
|
|
1113
1115
|
|
|
1114
1116
|
|
|
1117
|
+
// The records a list's response transform reads from one property of the body,
|
|
1118
|
+
// so its fields follow the list it chose among several.
|
|
1119
|
+
function transformRecords(schema: any, res: any): any {
|
|
1120
|
+
const m = 'string' === typeof res ? res.match(/^`body\.([^`.]+)`$/) : null
|
|
1121
|
+
const items = null == m ? null : mergedProperties(schema)?.[m[1]]?.items
|
|
1122
|
+
return null != items && 'object' === typeof items &&
|
|
1123
|
+
(null != items.properties || Array.isArray(items.allOf)) ? items : null
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
|
|
1115
1127
|
function unwrapArrayWrapper(schema: any): any {
|
|
1116
1128
|
if (null == schema || 'object' !== typeof schema) return null
|
|
1117
1129
|
// Direct list shape — caller can resolve from items directly.
|
|
@@ -1170,6 +1182,11 @@ function mergeField(
|
|
|
1170
1182
|
existingField.sh = newField.sh
|
|
1171
1183
|
}
|
|
1172
1184
|
|
|
1185
|
+
// A property declaring the field makes it the record's own.
|
|
1186
|
+
if (true !== newField.syn) {
|
|
1187
|
+
delete existingField.syn
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1173
1190
|
for (const flag of ['ro', 'wo', 'de'] as const) {
|
|
1174
1191
|
if (null == existingField[flag] && null != newField[flag]) {
|
|
1175
1192
|
existingField[flag] = newField[flag]
|
|
@@ -1194,6 +1211,7 @@ function ownType(type: any): any {
|
|
|
1194
1211
|
|
|
1195
1212
|
export {
|
|
1196
1213
|
fieldTransform,
|
|
1214
|
+
findExampleObject,
|
|
1197
1215
|
inferFieldsFromExamples,
|
|
1198
1216
|
inferTypeFromValue,
|
|
1199
1217
|
routeFieldNames,
|
|
@@ -321,6 +321,8 @@ function firstTextField(ent: ModelEntity, op?: ModelOp) {
|
|
|
321
321
|
})
|
|
322
322
|
})
|
|
323
323
|
|
|
324
|
+
const identity = identityFields(ent)
|
|
325
|
+
|
|
324
326
|
const fields = Object.keys(ent.fields).sort().map(n => ent.fields[n])
|
|
325
327
|
for (let fI = 0; fI < fields.length; fI++) {
|
|
326
328
|
const field = fields[fI]
|
|
@@ -329,13 +331,34 @@ function firstTextField(ent: ModelEntity, op?: ModelOp) {
|
|
|
329
331
|
// one holding bytes, never does. Fields are sorted by name, so the choice
|
|
330
332
|
// is alphabetical accident: solar's planet, once its spec declared the
|
|
331
333
|
// server-assigned `forbidReason`, marked that instead of `kind`.
|
|
332
|
-
if ('`$STRING`' === field.t &&
|
|
334
|
+
if ('`$STRING`' === field.t && true !== identity[field.n] && true !== field.ro &&
|
|
333
335
|
'binary' !== field.fo && true !== paramNames[field.n]) {
|
|
334
336
|
return field
|
|
335
337
|
}
|
|
336
338
|
}
|
|
337
339
|
}
|
|
338
340
|
|
|
341
|
+
|
|
342
|
+
// The fields holding the record's identity, which a mark would overwrite: `id`,
|
|
343
|
+
// the identity field, each composite part, and each record field a part is
|
|
344
|
+
// read from.
|
|
345
|
+
function identityFields(ent: ModelEntity): Record<string, boolean> {
|
|
346
|
+
const out: Record<string, boolean> = { id: true }
|
|
347
|
+
const id: any = ent.id
|
|
348
|
+
if (null != id?.field) {
|
|
349
|
+
out[id.field] = true
|
|
350
|
+
}
|
|
351
|
+
for (const part of (Array.isArray(id?.parts) ? id.parts : [])) {
|
|
352
|
+
out[part] = true
|
|
353
|
+
}
|
|
354
|
+
for (const where of Object.values(id?.from ?? {})) {
|
|
355
|
+
if (!String(where).includes('.')) {
|
|
356
|
+
out[String(where)] = true
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return out
|
|
360
|
+
}
|
|
361
|
+
|
|
339
362
|
export {
|
|
340
363
|
flowstepTransform,
|
|
341
364
|
}
|
|
@@ -81,9 +81,6 @@ const operationTransform: Transform = async function(
|
|
|
81
81
|
// After patch has joined update, so each operation's routes are final.
|
|
82
82
|
for (const mop of Object.values(opm)) {
|
|
83
83
|
for (const mpoint of mop?.points ?? []) {
|
|
84
|
-
if (heuristicRequest(on, mop!, mpoint)) {
|
|
85
|
-
mpoint.t.req = undefined
|
|
86
|
-
}
|
|
87
84
|
mpoint.t.req = mpoint.t.req ?? requestDefault(on, kit.entity[entname], opm, mop!, mpoint)
|
|
88
85
|
mpoint.t.res = mpoint.t.res ?? '`body`'
|
|
89
86
|
}
|
|
@@ -231,28 +228,11 @@ function resolveOp(opname: OpName, on: OpEntity): undefined | ModelOp {
|
|
|
231
228
|
|
|
232
229
|
|
|
233
230
|
|
|
234
|
-
// The request transform
|
|
235
|
-
//
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const generated = bodyRequestTransform(requestSchema(on.def, mpoint.m, mpoint.o), [on.gent.name])
|
|
240
|
-
return null != generated && sameTransform(mpoint.t.req, generated)
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
function sameTransform(a: any, b: any): boolean {
|
|
245
|
-
const canon = (t: any) => 'string' === typeof t ? t :
|
|
246
|
-
JSON.stringify(Object.keys(t ?? {}).sort().map((k: string) => [k, t[k]]))
|
|
247
|
-
return canon(a) === canon(b)
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
// An array body is sent from one field of the request data, named for its
|
|
252
|
-
// records, and never for an argument of its operation, a field another route of
|
|
253
|
-
// its entity has, as fields span operations, or a carrier already named for an
|
|
254
|
-
// array of another type or requiredness. Decided here, not by the guide
|
|
255
|
-
// heuristic, as the guide's media type decides the body.
|
|
231
|
+
// The request transform of a point whose guide sets none, read from the body
|
|
232
|
+
// the guide names: an object wraps the record under the entity's name or sends
|
|
233
|
+
// a closed body's own properties; an array is sent from a field named for its
|
|
234
|
+
// records, never for an argument, another route's field, or a carrier already
|
|
235
|
+
// named for an array of another type or requiredness.
|
|
256
236
|
function requestDefault(
|
|
257
237
|
on: OpEntity, ment: ModelEntity, opm: ModelOpMap, mop: ModelOp, mpoint: ModelPoint,
|
|
258
238
|
): any {
|
|
@@ -260,8 +240,7 @@ function requestDefault(
|
|
|
260
240
|
const chosen = media(mop.name, mpoint)
|
|
261
241
|
const list = arrayRequestSchema(on.def, mpoint.m, mpoint.o, chosen)
|
|
262
242
|
if (null == list) {
|
|
263
|
-
return (
|
|
264
|
-
bodyRequestTransform(requestSchema(on.def, mpoint.m, mpoint.o, chosen), [on.gent.name])) ?? '`reqdata`'
|
|
243
|
+
return bodyRequestTransform(objectSchema(on.def, mpoint, chosen), [on.gent.name]) ?? '`reqdata`'
|
|
265
244
|
}
|
|
266
245
|
const required = true === requestDecl(on.def, mpoint.m, mpoint.o)?.required
|
|
267
246
|
const others = mop.points.filter((q) => q !== mpoint)
|
|
@@ -279,6 +258,15 @@ function requestDefault(
|
|
|
279
258
|
}
|
|
280
259
|
|
|
281
260
|
|
|
261
|
+
// The object body a point sends: under the media type the guide names, else
|
|
262
|
+
// the preferred one, else a schema declared on the request body itself.
|
|
263
|
+
function objectSchema(def: any, mpoint: ModelPoint, chosen?: string): any {
|
|
264
|
+
const declared = def?.paths?.[mpoint.o]?.[String(mpoint.m).toLowerCase()]?.requestBody
|
|
265
|
+
return requestSchema(def, mpoint.m, mpoint.o, chosen) ??
|
|
266
|
+
(null == chosen && null != declared && 'object' === typeof declared ? declared.schema : undefined)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
282
270
|
export {
|
|
283
271
|
operationTransform,
|
|
284
272
|
}
|
package/src/transform/top.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from '../utility'
|
|
9
9
|
|
|
10
10
|
import { METHODS } from '../resolved'
|
|
11
|
+
import { IGNORED_HEADERS_KEY } from '../parse'
|
|
11
12
|
|
|
12
13
|
import type { TransformResult } from '../transform'
|
|
13
14
|
|
|
@@ -134,6 +135,14 @@ const topTransform = async function(
|
|
|
134
135
|
}
|
|
135
136
|
}
|
|
136
137
|
|
|
138
|
+
for (const ignored of def[IGNORED_HEADERS_KEY] ?? []) {
|
|
139
|
+
ctx.warn?.({
|
|
140
|
+
note: 'header parameter `' + ignored.name + '` of ' +
|
|
141
|
+
(null == ignored.method ? '' : ignored.method + ' ') + ignored.path +
|
|
142
|
+
' ignored: OpenAPI ignores Accept, Content-Type and Authorization header parameters',
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
137
146
|
const summary = resolveSummary(def)
|
|
138
147
|
if (null != summary) {
|
|
139
148
|
kit.info.summary = summary
|
package/src/utility.ts
CHANGED
|
@@ -29,11 +29,6 @@ const sortedKeys = (obj: any): string[] => Object.keys(obj ?? {}).sort()
|
|
|
29
29
|
const sortedEntries = (obj: any): [string, any][] =>
|
|
30
30
|
Object.entries(obj ?? {}).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
|
|
31
31
|
|
|
32
|
-
// Pre-compiled regex patterns for formatJsonSrc to avoid recompilation per call.
|
|
33
|
-
const RE_JSON_KEY = /"([a-zA-Z_][a-zA-Z_0-9]*)": /g
|
|
34
|
-
const RE_JSON_TRAILING_BRACE = /},/g
|
|
35
|
-
const RE_JSON_COMMENT = /\n(\s*)([a-zA-Z_][a-zA-Z_0-9]*)_COMMENT:\s*"(.*)",/g
|
|
36
|
-
|
|
37
32
|
const RE_BARE_KEY = /^[A-Za-z_][_A-Za-z0-9]*$/
|
|
38
33
|
const isBareKey = (k: string) => RE_BARE_KEY.test(k)
|
|
39
34
|
const quoteKey = (k: string) => (isBareKey(k) ? k : JSON.stringify(k))
|
|
@@ -128,11 +123,38 @@ function loadFile(path: string, what: string, fs: FsUtil, log: Log) {
|
|
|
128
123
|
}
|
|
129
124
|
|
|
130
125
|
|
|
126
|
+
// Unquotes bare keys and breaks the line after `},`, only outside strings, so
|
|
127
|
+
// no string value is rewritten.
|
|
131
128
|
function formatJsonSrc(jsonsrc: string) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
let out = ''
|
|
130
|
+
let i = 0
|
|
131
|
+
while (i < jsonsrc.length) {
|
|
132
|
+
const ch = jsonsrc[i]
|
|
133
|
+
if ('"' === ch) {
|
|
134
|
+
let end = i + 1
|
|
135
|
+
while (end < jsonsrc.length && '"' !== jsonsrc[end]) {
|
|
136
|
+
end += '\\' === jsonsrc[end] ? 2 : 1
|
|
137
|
+
}
|
|
138
|
+
const body = jsonsrc.substring(i + 1, end)
|
|
139
|
+
if (': ' === jsonsrc.substring(end + 1, end + 3) && isBareKey(body)) {
|
|
140
|
+
out += body + ': '
|
|
141
|
+
i = end + 3
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
out += jsonsrc.substring(i, end + 1)
|
|
145
|
+
i = end + 1
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else if ('}' === ch && ',' === jsonsrc[i + 1]) {
|
|
149
|
+
out += '}\n'
|
|
150
|
+
i += 2
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
out += ch
|
|
154
|
+
i++
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return out
|
|
136
158
|
}
|
|
137
159
|
|
|
138
160
|
|
|
@@ -722,6 +744,7 @@ function formatJSONIC(
|
|
|
722
744
|
color?: boolean
|
|
723
745
|
maxlines?: number,
|
|
724
746
|
exclude?: string[],
|
|
747
|
+
comments?: boolean,
|
|
725
748
|
}): string {
|
|
726
749
|
|
|
727
750
|
if (undefined === val) return ''
|
|
@@ -731,6 +754,7 @@ function formatJSONIC(
|
|
|
731
754
|
const useColor = opts?.color ?? false
|
|
732
755
|
const maxlines = opts?.maxlines ?? Number.MAX_VALUE
|
|
733
756
|
const exclude = opts?.exclude ?? []
|
|
757
|
+
const comments = opts?.comments ?? true
|
|
734
758
|
|
|
735
759
|
// ANSI color codes
|
|
736
760
|
const colors = {
|
|
@@ -751,13 +775,8 @@ function formatJSONIC(
|
|
|
751
775
|
if (v === null) return c('null', 'null')
|
|
752
776
|
const t = typeof v
|
|
753
777
|
switch (t) {
|
|
754
|
-
case 'string': return c('string',
|
|
755
|
-
'
|
|
756
|
-
.substring(1)
|
|
757
|
-
.replace(/\\n/g, '\n')
|
|
758
|
-
.replace(/\\"/g, '"')
|
|
759
|
-
.replace(/`/g, '\\`')
|
|
760
|
-
.replace(/"$/, '`'))
|
|
778
|
+
case 'string': return c('string',
|
|
779
|
+
!v.includes('\n') ? JSON.stringify(v) : backtickString(v))
|
|
761
780
|
case 'number': return c('number', Number.isFinite(v) ? String(v) : 'null')
|
|
762
781
|
case 'boolean': return c('boolean', v ? 'true' : 'false')
|
|
763
782
|
case 'bigint': return c('string', JSON.stringify(v.toString()))
|
|
@@ -769,15 +788,44 @@ function formatJSONIC(
|
|
|
769
788
|
}
|
|
770
789
|
}
|
|
771
790
|
|
|
772
|
-
const
|
|
791
|
+
const commentText = (c: any): string | null => {
|
|
773
792
|
if (c == null) return null
|
|
774
793
|
if (Array.isArray(c) && c.every(x => typeof x === 'string')) return c.join('; ')
|
|
775
794
|
if (typeof c === 'string') return c
|
|
776
795
|
try { return JSON.stringify(c) } catch { return String(c) }
|
|
777
796
|
}
|
|
778
797
|
|
|
798
|
+
// A comment runs to the end of its line, so a line break in it would start
|
|
799
|
+
// source.
|
|
800
|
+
const renderComment = (c: any): string | null => {
|
|
801
|
+
const text = comments ? commentText(c) : null
|
|
802
|
+
return null == text ? null : text.replace(RE_LINE_BREAKS, ' ')
|
|
803
|
+
}
|
|
804
|
+
|
|
779
805
|
return renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c,
|
|
780
|
-
renderPrimitive, renderComment, false)
|
|
806
|
+
renderPrimitive, renderComment, comments, false)
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
const RE_LINE_BREAKS = /[\r\n\u2028\u2029]+/g
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
// Its JSON escapes, read in order so an escaped backslash never starts
|
|
814
|
+
// another, with a newline and a quote written as themselves.
|
|
815
|
+
function backtickString(s: string): string {
|
|
816
|
+
const json = JSON.stringify(s)
|
|
817
|
+
let out = '`'
|
|
818
|
+
for (let i = 1; i < json.length - 1; i++) {
|
|
819
|
+
const ch = json[i]
|
|
820
|
+
if ('\\' === ch) {
|
|
821
|
+
const esc = json[++i]
|
|
822
|
+
out += 'n' === esc ? '\n' : '"' === esc ? '"' : '\\' + esc
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
out += '`' === ch ? '\\`' : ch
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return out + '`'
|
|
781
829
|
}
|
|
782
830
|
|
|
783
831
|
|
|
@@ -791,6 +839,7 @@ function renderJSONIC(
|
|
|
791
839
|
c: (color: any, text: string) => string,
|
|
792
840
|
renderPrimitive: (v: any) => string,
|
|
793
841
|
renderComment: (c: any) => string | null,
|
|
842
|
+
comments: boolean,
|
|
794
843
|
decircularized: boolean,
|
|
795
844
|
): string {
|
|
796
845
|
|
|
@@ -874,7 +923,7 @@ function renderJSONIC(
|
|
|
874
923
|
}
|
|
875
924
|
if (undefined !== flat) {
|
|
876
925
|
return renderJSONIC(flat, hsepd, showd, useColor, maxlines, exclude, c,
|
|
877
|
-
renderPrimitive, renderComment, true)
|
|
926
|
+
renderPrimitive, renderComment, comments, true)
|
|
878
927
|
}
|
|
879
928
|
}
|
|
880
929
|
// decircular did not resolve it, so render a marker rather than
|
|
@@ -919,7 +968,7 @@ function renderJSONIC(
|
|
|
919
968
|
keys.unshift('name', 'message', 'stack')
|
|
920
969
|
}
|
|
921
970
|
|
|
922
|
-
const printableKeys = keys.filter(k => !k.endsWith('_COMMENT') &&
|
|
971
|
+
const printableKeys = keys.filter(k => !(comments && k.endsWith('_COMMENT')) &&
|
|
923
972
|
(showd || !k.endsWith('$')))
|
|
924
973
|
|
|
925
974
|
if (printableKeys.length === 0) {
|
|
@@ -1130,6 +1179,49 @@ function foldName(name: string): string {
|
|
|
1130
1179
|
const FIRST_LETTER_RE = /[a-zA-Z]/
|
|
1131
1180
|
|
|
1132
1181
|
|
|
1182
|
+
// Formats a route segment may name beside its resource, or alone.
|
|
1183
|
+
const FORMAT_EXTENSIONS = new Set([
|
|
1184
|
+
'json', 'xml', 'csv', 'tsv', 'yaml', 'yml', 'txt', 'html', 'htm', 'rss',
|
|
1185
|
+
'atom', 'jsonl', 'ndjson', 'geojson',
|
|
1186
|
+
])
|
|
1187
|
+
|
|
1188
|
+
// A route segment as the guide reads it: {id}.json is {id}, repos.json and
|
|
1189
|
+
// data.{format} are repos and data, and a format alone (json) names nothing.
|
|
1190
|
+
function guideSegment(seg: string): string | null {
|
|
1191
|
+
const held = seg.match(/^(\{[^}]+\})\.(?:[A-Za-z0-9]+|\{[^}]+\})$/)
|
|
1192
|
+
if (null != held) return held[1]
|
|
1193
|
+
const named = seg.match(/^([^{}.]+)\.([A-Za-z0-9]+|\{[^}]+\})$/)
|
|
1194
|
+
if (null != named && ('{' === named[2][0] || FORMAT_EXTENSIONS.has(named[2].toLowerCase()))) {
|
|
1195
|
+
return named[1]
|
|
1196
|
+
}
|
|
1197
|
+
return FORMAT_EXTENSIONS.has(seg.toLowerCase()) ? null : seg
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// The guide's parts of a route; a route of formats alone keeps them.
|
|
1201
|
+
function guideParts(path: string): string[] {
|
|
1202
|
+
const parts = path.split('/').filter((p: string) => '' !== p)
|
|
1203
|
+
const kept = parts.map(guideSegment).filter((p): p is string => null != p)
|
|
1204
|
+
return 0 === kept.length ? parts : kept
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
function guidePath(path: string): string {
|
|
1208
|
+
return '/' + guideParts(path).join('/')
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
// aontu cannot unify a spread with a key Object.prototype owns, so no entity
|
|
1213
|
+
// is given one of these names. Listed, not read, so both ports agree.
|
|
1214
|
+
const PROTOTYPE_NAMES = new Set([
|
|
1215
|
+
'constructor', '__defineGetter__', '__defineSetter__', 'hasOwnProperty',
|
|
1216
|
+
'__lookupGetter__', '__lookupSetter__', 'isPrototypeOf',
|
|
1217
|
+
'propertyIsEnumerable', 'toString', 'valueOf', '__proto__', 'toLocaleString',
|
|
1218
|
+
])
|
|
1219
|
+
|
|
1220
|
+
function guardPrototypeName(s: string): string {
|
|
1221
|
+
return PROTOTYPE_NAMES.has(s) ? s + '_item' : s
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
|
|
1133
1225
|
function prefixLeadingDigit(s: string): string {
|
|
1134
1226
|
if (null == s || '' === s) return s
|
|
1135
1227
|
const first = s.charCodeAt(0)
|
|
@@ -1262,7 +1354,7 @@ function ensureMinEntityName(
|
|
|
1262
1354
|
padded = truncated || parts[0].substring(0, MAX_ENTITY_NAME_LEN)
|
|
1263
1355
|
}
|
|
1264
1356
|
|
|
1265
|
-
padded = prefixLeadingDigit(padded)
|
|
1357
|
+
padded = guardPrototypeName(prefixLeadingDigit(padded))
|
|
1266
1358
|
if (padded.length < MIN_ENTITY_NAME_LEN) {
|
|
1267
1359
|
const padding = 'nt'.substring(0, MIN_ENTITY_NAME_LEN - padded.length)
|
|
1268
1360
|
padded = padded + padding
|
|
@@ -1289,15 +1381,33 @@ function ensureMinEntityName(
|
|
|
1289
1381
|
// Unconditional suffixes: framework noise, always stripped.
|
|
1290
1382
|
const CMP_SUFFIXES = ['_rest_controller', '_controller', '_response', '_request']
|
|
1291
1383
|
|
|
1292
|
-
|
|
1384
|
+
// Longest first: the first suffix a name ends with is the only one tried.
|
|
1385
|
+
const CMP_GUARDED_SUFFIXES = [
|
|
1386
|
+
'_create_response', '_update_response', '_page_response',
|
|
1387
|
+
'_response_collection', '_response_single', '_response_dto',
|
|
1388
|
+
'_collection', '_single', '_dto', '_page',
|
|
1389
|
+
]
|
|
1293
1390
|
|
|
1294
1391
|
const CMP_RESULT_VERBS = [
|
|
1295
1392
|
'list', 'create', 'show', 'update', 'delete', 'remove',
|
|
1296
|
-
'get', 'edit', 'resolve', 'rotate', 'search',
|
|
1393
|
+
'get', 'edit', 'resolve', 'rotate', 'search', 'add',
|
|
1297
1394
|
]
|
|
1298
1395
|
|
|
1299
1396
|
const CMP_PREFIXES = ['get_', 'post_', 'put_', 'delete_', 'patch_']
|
|
1300
1397
|
|
|
1398
|
+
const OPERATION_VERBS = [
|
|
1399
|
+
...CMP_RESULT_VERBS, 'post', 'put', 'patch', 'fetch', 'find', 'retrieve', 'read', 'add',
|
|
1400
|
+
]
|
|
1401
|
+
|
|
1402
|
+
// An operationId leads with its action, so get_service_status names
|
|
1403
|
+
// service_status. The verb stays when what follows is too short to name one.
|
|
1404
|
+
function stripOperationVerb(opid: string): string {
|
|
1405
|
+
const cut = opid.indexOf('_')
|
|
1406
|
+
if (cut < 0) return opid
|
|
1407
|
+
const rest = opid.substring(cut + 1)
|
|
1408
|
+
return OPERATION_VERBS.includes(opid.substring(0, cut)) && 3 <= rest.length ? rest : opid
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1301
1411
|
// A guide node (entity, path or op) is active unless guide.aontu explicitly
|
|
1302
1412
|
// says otherwise. `active` has always been declared in the guide model and
|
|
1303
1413
|
// documented as the escape hatch for a misclassified entity; this is the
|
|
@@ -1451,7 +1561,7 @@ function cleanComponentName(
|
|
|
1451
1561
|
// (CallRouteAllowedCaller), so the namespace is tried singular too.
|
|
1452
1562
|
const cands = after
|
|
1453
1563
|
? [canonize(depluralize(after)), canonize(depluralize(before + '_' + after)),
|
|
1454
|
-
canonize(depluralize(before) + '_' + depluralize(after))]
|
|
1564
|
+
canonize(depluralize(before) + '_' + depluralize(after)), canonize(depluralize(before))]
|
|
1455
1565
|
: [canonize(depluralize(before))]
|
|
1456
1566
|
for (const cand of cands) {
|
|
1457
1567
|
if (cand.length >= 3 && knownWithVersion(cand, version)) {
|
|
@@ -1487,6 +1597,15 @@ function cleanComponentName(
|
|
|
1487
1597
|
}
|
|
1488
1598
|
}
|
|
1489
1599
|
|
|
1600
|
+
// A list wrapper names its records: list_asset is asset, when asset is known.
|
|
1601
|
+
if (null != isKnownCmp && cleaned.startsWith('list_')) {
|
|
1602
|
+
const remainder = canonize(cleaned.substring('list_'.length))
|
|
1603
|
+
if (remainder.length >= 3 && knownWithVersion(remainder, version)) {
|
|
1604
|
+
cleaned = remainder
|
|
1605
|
+
stripped = true
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1490
1609
|
// Nothing stripped it: restore the version suffix removed above.
|
|
1491
1610
|
if (!stripped && cleaned === preVersion.replace(/_v\d+$/, '') && null != version) {
|
|
1492
1611
|
cleaned = preVersion
|
|
@@ -1815,7 +1934,43 @@ function itemEnvelopeOf(res: any): string | null {
|
|
|
1815
1934
|
}
|
|
1816
1935
|
|
|
1817
1936
|
|
|
1818
|
-
|
|
1937
|
+
// A list over an object reads its records, never the object: its one envelope,
|
|
1938
|
+
// else its one list of records beside other data, else the first of several.
|
|
1939
|
+
function envelopeProp(resprops: any, opname: string, example?: any): string | null {
|
|
1940
|
+
const key = singleEnvelope(resprops, opname)
|
|
1941
|
+
return null == key && 'list' === opname ? listedRecords(resprops, example) : key
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
|
|
1945
|
+
function recordLists(resprops: any): string[] {
|
|
1946
|
+
return keysof(resprops).filter((k: string) => isRecordList(resprops[k]))
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
|
|
1950
|
+
// Lists of one item schema are categories of one record: the one the example
|
|
1951
|
+
// fills is read, as the example is counted. Otherwise the first by name is,
|
|
1952
|
+
// past one holding errors or messages.
|
|
1953
|
+
function listedRecords(resprops: any, example?: any): string | null {
|
|
1954
|
+
const lists = recordLists(resprops)
|
|
1955
|
+
if (lists.length < 2) {
|
|
1956
|
+
return lists[0] ?? null
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
const refs = new Set(lists.map((k: string) => resprops[k]?.items?.['x-ref']))
|
|
1960
|
+
if (1 === refs.size && null != [...refs][0]) {
|
|
1961
|
+
const filled = lists.filter((k: string) =>
|
|
1962
|
+
Array.isArray(example?.[k]) && 0 < example[k].length)
|
|
1963
|
+
if (1 === filled.length) {
|
|
1964
|
+
return filled[0]
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
const status = (k: string) => isEnvelopeStatusProp(k) || isEnvelopeStatusProp(k.replace(/s$/i, ''))
|
|
1969
|
+
return lists.find((k: string) => !isPageMetaProp(k) && !status(k)) ?? lists[0]
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
function singleEnvelope(resprops: any, opname: string): string | null {
|
|
1819
1974
|
const keys = keysof(resprops)
|
|
1820
1975
|
if (0 === keys.length) {
|
|
1821
1976
|
return null
|
|
@@ -1943,9 +2098,10 @@ function isPageMetaProp(name: string): boolean {
|
|
|
1943
2098
|
}
|
|
1944
2099
|
|
|
1945
2100
|
|
|
1946
|
-
// The
|
|
1947
|
-
// it a page, such as GitHub's
|
|
1948
|
-
|
|
2101
|
+
// The metadata a single item may carry, such as the `info` beside a Disney
|
|
2102
|
+
// character. Paging beside an object makes it a page, such as GitHub's
|
|
2103
|
+
// attestations by digest beside their page info.
|
|
2104
|
+
const ITEM_META_PROPS = new Set(['meta', 'metadata', 'links', 'info'])
|
|
1949
2105
|
|
|
1950
2106
|
|
|
1951
2107
|
// A list under a metadata name is records, as it is on a page.
|
|
@@ -2078,6 +2234,19 @@ function answersPage(schema: any): boolean {
|
|
|
2078
2234
|
}
|
|
2079
2235
|
|
|
2080
2236
|
|
|
2237
|
+
// An object whose entries are records under keys of the API's choosing, with
|
|
2238
|
+
// no properties of its own, such as FXMacroData's catalogue by indicator slug.
|
|
2239
|
+
function answersRecordMap(schema: any): boolean {
|
|
2240
|
+
if (null == schema || 'object' !== typeof schema || Array.isArray(schema) ||
|
|
2241
|
+
(null != schema.type && 'object' !== schema.type) ||
|
|
2242
|
+
0 < keysof(mergedProperties(schema)).length) {
|
|
2243
|
+
return false
|
|
2244
|
+
}
|
|
2245
|
+
const entry = schema.additionalProperties
|
|
2246
|
+
return true !== propIsList(entry) && isObjectSchema(entry)
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
|
|
2081
2250
|
// The properties of a schema and its allOf members; the first declaration of
|
|
2082
2251
|
// a name wins. Read-only, since parsed nodes are shared between references.
|
|
2083
2252
|
function mergedProperties(schema: any): Record<string, any> | undefined {
|
|
@@ -2304,6 +2473,10 @@ export {
|
|
|
2304
2473
|
humanTitle,
|
|
2305
2474
|
foldName,
|
|
2306
2475
|
prefixLeadingDigit,
|
|
2476
|
+
guardPrototypeName,
|
|
2477
|
+
guideParts,
|
|
2478
|
+
guidePath,
|
|
2479
|
+
stripOperationVerb,
|
|
2307
2480
|
debugpath,
|
|
2308
2481
|
debugpathOn,
|
|
2309
2482
|
findPathsWithPrefix,
|
|
@@ -2320,10 +2493,13 @@ export {
|
|
|
2320
2493
|
itemEnvelopeTransform,
|
|
2321
2494
|
itemEnvelopeOf,
|
|
2322
2495
|
envelopeProp,
|
|
2496
|
+
recordLists,
|
|
2497
|
+
singleEnvelope,
|
|
2323
2498
|
envelopeItemRef,
|
|
2324
2499
|
composedEnvelopeProp,
|
|
2325
2500
|
pageRecords,
|
|
2326
2501
|
answersPage,
|
|
2502
|
+
answersRecordMap,
|
|
2327
2503
|
propIsList,
|
|
2328
2504
|
mergedProperties,
|
|
2329
2505
|
closedBodyTransform,
|