@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
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 {
|
|
@@ -94,10 +96,19 @@ const topTransform = async function(
|
|
|
94
96
|
// Institute of Chicago lists `api.artic.edu/api/v1` (no
|
|
95
97
|
// https://). Go's net/http barfs on that with "unsupported
|
|
96
98
|
// protocol scheme". Default to https when the URL has no scheme
|
|
97
|
-
// and
|
|
99
|
+
// and is neither a relative path nor opened by a server variable.
|
|
98
100
|
for (const server of (kit.info.servers as any[])) {
|
|
99
101
|
if (!server || 'string' !== typeof server.url) continue
|
|
100
|
-
|
|
102
|
+
const given = server.url
|
|
103
|
+
const braced = singleBraces(given)
|
|
104
|
+
server.url = withScheme(braced.url)
|
|
105
|
+
if (0 < braced.names.length) {
|
|
106
|
+
declareVariables(server, braced.names)
|
|
107
|
+
ctx.warn?.({
|
|
108
|
+
note: 'server URL `' + given + '` writes its variables in Postman\'s double' +
|
|
109
|
+
' braces: taken as `' + server.url + '`',
|
|
110
|
+
})
|
|
111
|
+
}
|
|
101
112
|
}
|
|
102
113
|
|
|
103
114
|
// No server named: the `server` option, else a `base` variable the caller fills.
|
|
@@ -197,15 +208,61 @@ function resolveSummary(def: any): string | undefined {
|
|
|
197
208
|
}
|
|
198
209
|
|
|
199
210
|
|
|
211
|
+
// A server variable that opens the URL stands for its origin, or for its
|
|
212
|
+
// scheme when `://` follows, so its value carries the scheme.
|
|
213
|
+
const LEADING_VARIABLE_RE = /^\{([^{}]+)\}($|[/?#]|:\/\/)/
|
|
214
|
+
const ORIGIN_DESCRIPTION =
|
|
215
|
+
'The origin of the API, with its scheme, such as https://api.example.com.'
|
|
216
|
+
|
|
217
|
+
|
|
200
218
|
function withScheme(url: string): string {
|
|
201
219
|
const u = url.trim()
|
|
202
220
|
if ('' === u || /^[a-z][a-z0-9+.-]*:\/\//i.test(u)) return url
|
|
203
221
|
if (u.startsWith('//')) return 'https:' + u
|
|
204
|
-
if (u.startsWith('/')) return url
|
|
222
|
+
if (u.startsWith('/') || LEADING_VARIABLE_RE.test(u)) return url
|
|
205
223
|
return 'https://' + u
|
|
206
224
|
}
|
|
207
225
|
|
|
208
226
|
|
|
227
|
+
function originVariable(url: string): string | undefined {
|
|
228
|
+
const lead = LEADING_VARIABLE_RE.exec(url.trim())
|
|
229
|
+
return null == lead || '://' === lead[2] ? undefined : lead[1]
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
// OpenAPI reads only the inner pair of Postman's `{{name}}` as the variable.
|
|
234
|
+
// Any other run of braces, or a name the SDKs cannot substitute, is kept.
|
|
235
|
+
const BRACE_RUN_RE = /\{+[A-Za-z0-9_]+\}+/g
|
|
236
|
+
const POSTMAN_VARIABLE_RE = /^\{\{([A-Za-z0-9_]+)\}\}$/
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
function singleBraces(url: string): { url: string, names: string[] } {
|
|
240
|
+
const names: string[] = []
|
|
241
|
+
const out = url.replace(BRACE_RUN_RE, (run: string) => {
|
|
242
|
+
const name = POSTMAN_VARIABLE_RE.exec(run)?.[1]
|
|
243
|
+
if (null == name) return run
|
|
244
|
+
if (!names.includes(name)) names.push(name)
|
|
245
|
+
return '{' + name + '}'
|
|
246
|
+
})
|
|
247
|
+
return { url: out, names }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
// An empty default leaves the value to the SDK's caller.
|
|
252
|
+
function declareVariables(server: any, names: string[]) {
|
|
253
|
+
const declared = null == server.variables ? {} : server.variables
|
|
254
|
+
if (!isObject(declared)) return
|
|
255
|
+
const origin = originVariable(server.url)
|
|
256
|
+
const missing = names.filter((name) =>
|
|
257
|
+
!Object.prototype.hasOwnProperty.call(declared, name))
|
|
258
|
+
server.variables = {
|
|
259
|
+
...declared,
|
|
260
|
+
...Object.fromEntries(missing.map((name) => [name, origin === name ?
|
|
261
|
+
{ default: '', description: ORIGIN_DESCRIPTION } : { default: '' }])),
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
|
|
209
266
|
function resolveWebsite(def: any, servers: any[]): string | undefined {
|
|
210
267
|
const info = def?.info ?? {}
|
|
211
268
|
|
|
@@ -250,28 +307,109 @@ function isHttpUrl(v: any): boolean {
|
|
|
250
307
|
}
|
|
251
308
|
|
|
252
309
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
310
|
+
// A client sends one credential, so the primary scheme comes from an entry
|
|
311
|
+
// naming a single scheme: the one every secured operation lists first, else
|
|
312
|
+
// the definition's first, else the first declared scheme. Every other entry
|
|
313
|
+
// the operations accept is an alternative, its schemes sent together.
|
|
314
|
+
function resolveSecurity(def: any): Record<string, any> | null {
|
|
315
|
+
const defined = def.components?.securitySchemes ?? def.securityDefinitions
|
|
316
|
+
const schemes: Record<string, any> = isObject(defined) ? defined : {}
|
|
256
317
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
'object' === typeof def.security[0] ?
|
|
260
|
-
Object.keys(def.security[0])[0] : undefined
|
|
318
|
+
const declared = (name: string) =>
|
|
319
|
+
Object.prototype.hasOwnProperty.call(schemes, name) && isObject(schemes[name])
|
|
261
320
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
321
|
+
const opsecurity = operationSecurity(def)
|
|
322
|
+
const firsts = new Set(opsecurity.map((reqs) => firstSingleScheme(reqs, declared)))
|
|
323
|
+
firsts.delete(undefined)
|
|
324
|
+
|
|
325
|
+
const schemeName: string | undefined =
|
|
326
|
+
(1 === firsts.size ? [...firsts][0] : undefined) ??
|
|
327
|
+
firstSingleScheme(def.security, declared) ??
|
|
328
|
+
Object.keys(schemes)[0]
|
|
265
329
|
|
|
266
330
|
const scheme = null == schemeName ? null : schemes[schemeName]
|
|
267
|
-
if (
|
|
331
|
+
if (!isObject(scheme)) {
|
|
268
332
|
return null
|
|
269
333
|
}
|
|
270
334
|
|
|
335
|
+
const out: Record<string, any> = describeScheme(def, schemeName as string, scheme)
|
|
336
|
+
|
|
337
|
+
const alternatives = otherSchemeSets(
|
|
338
|
+
0 < opsecurity.length ? opsecurity : [def.security], schemeName as string, declared)
|
|
339
|
+
if (0 < alternatives.length) {
|
|
340
|
+
out.alternatives = alternatives.map((names) =>
|
|
341
|
+
names.map((name) => describeScheme(def, name, schemes[name])))
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return out
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
function isObject(v: any): boolean {
|
|
349
|
+
return null != v && 'object' === typeof v && !Array.isArray(v)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
// Each operation's security list: its own, else the definition's.
|
|
354
|
+
function operationSecurity(def: any): any[][] {
|
|
355
|
+
const out: any[][] = []
|
|
356
|
+
for (const [, pathItem] of sortedEntries(isObject(def.paths) ? def.paths : {})) {
|
|
357
|
+
if (!isObject(pathItem)) continue
|
|
358
|
+
for (const method of METHODS) {
|
|
359
|
+
const op = pathItem[method]
|
|
360
|
+
if (!isObject(op)) continue
|
|
361
|
+
const reqs = Array.isArray(op.security) ? op.security : def.security
|
|
362
|
+
if (Array.isArray(reqs)) out.push(reqs)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return out
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
// Sorted, so a set of schemes compares and prints the same in both ports.
|
|
370
|
+
function schemeSet(req: any): string[] | null {
|
|
371
|
+
return isObject(req) ? Object.keys(req).sort() : null
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
function firstSingleScheme(
|
|
376
|
+
reqs: any, declared: (name: string) => boolean): string | undefined {
|
|
377
|
+
if (!Array.isArray(reqs)) return undefined
|
|
378
|
+
for (const req of reqs) {
|
|
379
|
+
const names = schemeSet(req)
|
|
380
|
+
if (null != names && 1 === names.length && declared(names[0])) {
|
|
381
|
+
return names[0]
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return undefined
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
function otherSchemeSets(
|
|
389
|
+
lists: any[], primary: string, declared: (name: string) => boolean): string[][] {
|
|
390
|
+
const seen = new Set([JSON.stringify([primary])])
|
|
391
|
+
const out: string[][] = []
|
|
392
|
+
for (const reqs of lists) {
|
|
393
|
+
if (!Array.isArray(reqs)) continue
|
|
394
|
+
for (const req of reqs) {
|
|
395
|
+
const names = schemeSet(req)
|
|
396
|
+
if (null == names || 0 === names.length || !names.every(declared)) continue
|
|
397
|
+
const key = JSON.stringify(names)
|
|
398
|
+
if (!seen.has(key)) {
|
|
399
|
+
seen.add(key)
|
|
400
|
+
out.push(names)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return out
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
function describeScheme(def: any, schemeName: string, scheme: any): Record<string, string> {
|
|
271
409
|
const type = String(scheme.type ?? '').toLowerCase()
|
|
272
410
|
|
|
273
411
|
const out: Record<string, string> = {
|
|
274
|
-
scheme: schemeName
|
|
412
|
+
scheme: schemeName,
|
|
275
413
|
type: scheme.type ?? '',
|
|
276
414
|
in: scheme.in ?? 'header',
|
|
277
415
|
name: scheme.name ?? 'Authorization',
|
|
@@ -423,6 +561,7 @@ function stringifyInfoScalars(node: any): any {
|
|
|
423
561
|
|
|
424
562
|
export {
|
|
425
563
|
topTransform,
|
|
564
|
+
stringifyInfoScalars,
|
|
426
565
|
resolveSecurity,
|
|
427
566
|
resolveSummary,
|
|
428
567
|
ensureDescription,
|
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
|
package/src/utility.ts
CHANGED
|
@@ -137,6 +137,7 @@ function formatJsonSrc(jsonsrc: string) {
|
|
|
137
137
|
|
|
138
138
|
|
|
139
139
|
const IRREGULARS: Record<string, string> = Object.assign(Object.create(null), {
|
|
140
|
+
'aircraft': 'aircraft',
|
|
140
141
|
'analytics': 'analytics',
|
|
141
142
|
'analyses': 'analysis',
|
|
142
143
|
'appendices': 'appendix',
|
|
@@ -153,12 +154,14 @@ const IRREGULARS: Record<string, string> = Object.assign(Object.create(null), {
|
|
|
153
154
|
'crises': 'crisis',
|
|
154
155
|
'criteria': 'criterion',
|
|
155
156
|
// 'data': 'datum',
|
|
157
|
+
'deer': 'deer',
|
|
156
158
|
'diagnoses': 'diagnosis',
|
|
157
159
|
'doses': 'dose',
|
|
158
160
|
'douches': 'douche',
|
|
159
161
|
'enterprises': 'enterprise',
|
|
160
162
|
'exercises': 'exercise',
|
|
161
163
|
'feet': 'foot',
|
|
164
|
+
'fish': 'fish',
|
|
162
165
|
'franchises': 'franchise',
|
|
163
166
|
'furnaces': 'furnace',
|
|
164
167
|
'geese': 'goose',
|
|
@@ -172,6 +175,7 @@ const IRREGULARS: Record<string, string> = Object.assign(Object.create(null), {
|
|
|
172
175
|
'matrices': 'matrix',
|
|
173
176
|
'men': 'man',
|
|
174
177
|
'mice': 'mouse',
|
|
178
|
+
'moose': 'moose',
|
|
175
179
|
'moustaches': 'moustache',
|
|
176
180
|
'movies': 'movie',
|
|
177
181
|
'mustaches': 'mustache',
|
|
@@ -181,6 +185,7 @@ const IRREGULARS: Record<string, string> = Object.assign(Object.create(null), {
|
|
|
181
185
|
'nurses': 'nurse',
|
|
182
186
|
'oases': 'oasis',
|
|
183
187
|
'oboes': 'oboe',
|
|
188
|
+
'oxen': 'ox',
|
|
184
189
|
'pastiches': 'pastiche',
|
|
185
190
|
'pauses': 'pause',
|
|
186
191
|
'phases': 'phase',
|
|
@@ -191,11 +196,13 @@ const IRREGULARS: Record<string, string> = Object.assign(Object.create(null), {
|
|
|
191
196
|
'psyches': 'psyche',
|
|
192
197
|
'purchases': 'purchase',
|
|
193
198
|
'purses': 'purse',
|
|
199
|
+
'quizzes': 'quiz',
|
|
194
200
|
'releases': 'release',
|
|
195
201
|
'roses': 'rose',
|
|
196
202
|
'people': 'person',
|
|
197
203
|
'phenomena': 'phenomenon',
|
|
198
204
|
'series': 'series',
|
|
205
|
+
'sheep': 'sheep',
|
|
199
206
|
'shoes': 'shoe',
|
|
200
207
|
'sources': 'source',
|
|
201
208
|
'species': 'species',
|
|
@@ -223,6 +230,9 @@ const F_PLURAL_STEMS = [
|
|
|
223
230
|
// today, but the sort makes any future entry safe by construction.
|
|
224
231
|
const IRREGULAR_KEYS = Object.keys(IRREGULARS).sort((a, b) => b.length - a.length)
|
|
225
232
|
|
|
233
|
+
// Nouns in -o whose plural takes -es; the rest take -s (photos).
|
|
234
|
+
const OES_NOUNS = ['echo', 'embargo', 'hero', 'potato', 'tomato', 'torpedo', 'veto']
|
|
235
|
+
|
|
226
236
|
|
|
227
237
|
function matchCase(source: string, target: string): string {
|
|
228
238
|
if (source === source.toLowerCase()) return target.toLowerCase()
|
|
@@ -368,6 +378,38 @@ function depluralize(word: string): string {
|
|
|
368
378
|
}
|
|
369
379
|
|
|
370
380
|
|
|
381
|
+
// The plural of a snake name's last word that depluralize reads back as the
|
|
382
|
+
// name, so the two cannot disagree.
|
|
383
|
+
function pluralize(word: string): string {
|
|
384
|
+
if (null == word || '' === word) {
|
|
385
|
+
return word
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const cut = word.lastIndexOf('_') + 1
|
|
389
|
+
const last = word.slice(cut)
|
|
390
|
+
const lower = last.toLowerCase()
|
|
391
|
+
|
|
392
|
+
const plurals = [
|
|
393
|
+
...pluralsOf(CUSTOM_PLURALS, lower),
|
|
394
|
+
...pluralsOf(IRREGULARS, lower),
|
|
395
|
+
...(/[^aeiou]y$/.test(lower) ? [lower.slice(0, -1) + 'ies'] : []),
|
|
396
|
+
...(/fe?$/.test(lower) ? [lower.replace(/fe?$/, 'ves')] : []),
|
|
397
|
+
...(/(s|x|z|ch|sh)$/.test(lower) || OES_NOUNS.includes(lower) ? [lower + 'es'] : []),
|
|
398
|
+
lower + 's',
|
|
399
|
+
].map((plural) => word.slice(0, cut) + matchCase(last, plural))
|
|
400
|
+
|
|
401
|
+
return plurals.find((plural) => depluralize(plural) === word) ??
|
|
402
|
+
plurals[plurals.length - 1]
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
function pluralsOf(plurals: Record<string, string>, singular: string): string[] {
|
|
407
|
+
return Object.keys(plurals)
|
|
408
|
+
.filter((plural) => plurals[plural].toLowerCase() === singular)
|
|
409
|
+
.sort()
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
|
|
371
413
|
function find(obj: any, qkey: string): any[] {
|
|
372
414
|
const vals: any[] = []
|
|
373
415
|
const seen = new WeakSet<object>()
|
|
@@ -1129,6 +1171,25 @@ function normalizeFieldName(s: string): string {
|
|
|
1129
1171
|
}
|
|
1130
1172
|
|
|
1131
1173
|
|
|
1174
|
+
function canonizeParam(name: string): string {
|
|
1175
|
+
return depluralize(snakify(normalizeFieldName(name)))
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
// A guide may key a rename by the definition's spelling, its normalized form
|
|
1180
|
+
// or its canonical form.
|
|
1181
|
+
function paramName(wire: string, renames?: Record<string, any> | null): string {
|
|
1182
|
+
for (const key of [wire, normalizeFieldName(wire), canonizeParam(wire)]) {
|
|
1183
|
+
const target = null != renames && Object.prototype.hasOwnProperty.call(renames, key) ?
|
|
1184
|
+
renames[key] : undefined
|
|
1185
|
+
if ('string' === typeof target && '' !== target) {
|
|
1186
|
+
return target
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
return canonizeParam(wire)
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
|
|
1132
1193
|
const MIN_ENTITY_NAME_LEN = 3
|
|
1133
1194
|
const MAX_ENTITY_NAME_LEN = 67
|
|
1134
1195
|
|
|
@@ -1594,30 +1655,112 @@ export type {
|
|
|
1594
1655
|
}
|
|
1595
1656
|
|
|
1596
1657
|
|
|
1597
|
-
//
|
|
1598
|
-
//
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1658
|
+
// What an allOf member holds when it only describes. The siblings of a $ref
|
|
1659
|
+
// are ignored in OpenAPI 3.0, so a property describes one in an allOf.
|
|
1660
|
+
const ANNOTATION_KEYS = new Set(['description', 'title', 'example', 'nullable', 'deprecated'])
|
|
1661
|
+
|
|
1662
|
+
const SCALAR_TYPES = new Set(['string', 'integer', 'number', 'boolean'])
|
|
1663
|
+
|
|
1664
|
+
|
|
1665
|
+
// An allOf of one scalar and annotations is that scalar, under the
|
|
1666
|
+
// annotations and then the property's own keys. Any other allOf is kept.
|
|
1667
|
+
function collapseScalarAllOf(property: any): any {
|
|
1668
|
+
const members = property?.allOf
|
|
1669
|
+
if (!Array.isArray(members)) {
|
|
1670
|
+
return property
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
const valued = members.filter((member: any) => !isAnnotation(member))
|
|
1674
|
+
if (1 !== valued.length || !isScalarSchema(valued[0])) {
|
|
1675
|
+
return property
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
const { allOf: _members, ...own } = property
|
|
1679
|
+
return Object.assign({}, valued[0], ...members.filter(isAnnotation), own)
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
|
|
1683
|
+
function isAnnotation(member: any): boolean {
|
|
1684
|
+
return null != member && 'object' === typeof member && !Array.isArray(member) &&
|
|
1685
|
+
Object.keys(member).every((key: string) => ANNOTATION_KEYS.has(key))
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
|
|
1689
|
+
function isScalarSchema(schema: any): boolean {
|
|
1690
|
+
if (null == schema || 'object' !== typeof schema || Array.isArray(schema) ||
|
|
1691
|
+
null != schema.allOf || null != schema.oneOf || null != schema.anyOf) {
|
|
1692
|
+
return false
|
|
1693
|
+
}
|
|
1694
|
+
const types = (Array.isArray(schema.type) ? schema.type : [schema.type])
|
|
1695
|
+
.filter((type: any) => 'null' !== type)
|
|
1696
|
+
return 1 === types.length && SCALAR_TYPES.has(types[0])
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
|
|
1700
|
+
// A response property only "wraps" the entity when it is a structured value
|
|
1701
|
+
// that could contain it: an object, an array, a $ref, or a composition that
|
|
1702
|
+
// could hold one. A scalar that shares the entity's name is a field, however
|
|
1703
|
+
// it is composed, so the response is not unwrapped down to it.
|
|
1602
1704
|
function isEntityWrapperProp(propSchema: any): boolean {
|
|
1603
|
-
|
|
1705
|
+
const prop = collapseScalarAllOf(propSchema)
|
|
1706
|
+
if (null == prop || 'object' !== typeof prop) {
|
|
1604
1707
|
return false
|
|
1605
1708
|
}
|
|
1606
|
-
if (null !=
|
|
1709
|
+
if (null != prop.$ref) {
|
|
1607
1710
|
return true
|
|
1608
1711
|
}
|
|
1609
|
-
if (null !=
|
|
1610
|
-
null !=
|
|
1611
|
-
null !=
|
|
1612
|
-
|
|
1613
|
-
|
|
1712
|
+
if (null != prop.properties ||
|
|
1713
|
+
null != prop.items ||
|
|
1714
|
+
null != prop.allOf ||
|
|
1715
|
+
holdsStructuredBranch(prop.oneOf) ||
|
|
1716
|
+
holdsStructuredBranch(prop.anyOf)) {
|
|
1614
1717
|
return true
|
|
1615
1718
|
}
|
|
1616
|
-
const t =
|
|
1719
|
+
const t = prop.type
|
|
1617
1720
|
return 'object' === t || 'array' === t
|
|
1618
1721
|
}
|
|
1619
1722
|
|
|
1620
1723
|
|
|
1724
|
+
// A null branch only makes a union of scalars nullable.
|
|
1725
|
+
function holdsStructuredBranch(branches: any): boolean {
|
|
1726
|
+
return null != branches && !(Array.isArray(branches) && 0 < branches.length &&
|
|
1727
|
+
branches.every((branch: any) => isScalarSchema(branch) || 'null' === branch?.type))
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
// The key each item of a list response wraps the record under, where the item
|
|
1732
|
+
// holds nothing else, as each item of a Maxio list holds one record under the
|
|
1733
|
+
// name of its type.
|
|
1734
|
+
function itemEnvelopeKey(schema: any, names: string[]): string | null {
|
|
1735
|
+
if ('array' !== schema?.type) {
|
|
1736
|
+
return null
|
|
1737
|
+
}
|
|
1738
|
+
const keys = keysof(mergedProperties(schema.items))
|
|
1739
|
+
const key = keys[0]
|
|
1740
|
+
if (1 !== keys.length || !names.includes(key) || !/^[^.`$]+$/.test(key)) {
|
|
1741
|
+
return null
|
|
1742
|
+
}
|
|
1743
|
+
const prop = collapseScalarAllOf(mergedProperties(schema.items)?.[key])
|
|
1744
|
+
return isEntityWrapperProp(prop) && 'array' !== prop?.type && null == prop?.items ?
|
|
1745
|
+
key : null
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
// The response transform that answers each item's record under its key.
|
|
1750
|
+
function itemEnvelopeTransform(key: string): any[] {
|
|
1751
|
+
return ['`$EACH`', 'body', { '`$MERGE`': '`.' + key + '`' }]
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
// The key of an itemEnvelopeTransform, else null.
|
|
1756
|
+
function itemEnvelopeOf(res: any): string | null {
|
|
1757
|
+
const merge = Array.isArray(res) && 3 === res.length &&
|
|
1758
|
+
'`$EACH`' === res[0] && 'body' === res[1] ? res[2]?.['`$MERGE`'] : null
|
|
1759
|
+
const m = 'string' === typeof merge ? merge.match(/^`\.([^.`$]+)`$/) : null
|
|
1760
|
+
return null == m ? null : m[1]
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
|
|
1621
1764
|
function envelopeProp(resprops: any, opname: string): string | null {
|
|
1622
1765
|
const keys = keysof(resprops)
|
|
1623
1766
|
if (0 === keys.length) {
|
|
@@ -1954,6 +2097,30 @@ function closedBodyTransform(schema: any): Record<string, string> | null {
|
|
|
1954
2097
|
return out
|
|
1955
2098
|
}
|
|
1956
2099
|
|
|
2100
|
+
|
|
2101
|
+
// The key a request transform sends the whole record under, else null.
|
|
2102
|
+
function requestWrapperOf(req: any): string | null {
|
|
2103
|
+
if (null == req || 'object' !== typeof req || Array.isArray(req)) {
|
|
2104
|
+
return null
|
|
2105
|
+
}
|
|
2106
|
+
const keys = Object.keys(req)
|
|
2107
|
+
return 1 === keys.length && '`reqdata`' === req[keys[0]] ? keys[0] : null
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
// A body wraps the record under the entity's name only when that is all it
|
|
2111
|
+
// holds, and it is structured; otherwise the name is one field of the record,
|
|
2112
|
+
// such as the container in SaladCloud's container group create. A CLOSED body
|
|
2113
|
+
// names every property the server accepts, so the body is those properties,
|
|
2114
|
+
// not the whole payload, which also carries the op's path parameters.
|
|
2115
|
+
function bodyRequestTransform(schema: any, names: string[]): any {
|
|
2116
|
+
if (null == schema) return undefined
|
|
2117
|
+
const props = schema.properties
|
|
2118
|
+
const wrapped = names.find((name: string) => null != name && '' !== name &&
|
|
2119
|
+
isEntityWrapperProp(props?.[name]) && keysof(props).every((k: string) => k === name))
|
|
2120
|
+
return null != wrapped ? { [wrapped]: '`reqdata`' } : (closedBodyTransform(schema) ?? undefined)
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
|
|
1957
2124
|
function firstSentence(text: string): string {
|
|
1958
2125
|
const collapsed = text.replace(/\s+/g, ' ').trim()
|
|
1959
2126
|
const m = collapsed.match(/^(.+?[.!?])(\s|$)/)
|
|
@@ -1967,11 +2134,13 @@ function firstSentence(text: string): string {
|
|
|
1967
2134
|
|
|
1968
2135
|
|
|
1969
2136
|
export {
|
|
2137
|
+
bodyRequestTransform,
|
|
1970
2138
|
nom,
|
|
1971
2139
|
getdlog,
|
|
1972
2140
|
loadFile,
|
|
1973
2141
|
formatJsonSrc,
|
|
1974
2142
|
depluralize,
|
|
2143
|
+
pluralize,
|
|
1975
2144
|
setCustomPlurals,
|
|
1976
2145
|
clearCustomPlurals,
|
|
1977
2146
|
find,
|
|
@@ -1996,6 +2165,8 @@ export {
|
|
|
1996
2165
|
ensureMinEntityName,
|
|
1997
2166
|
inferFieldType,
|
|
1998
2167
|
normalizeFieldName,
|
|
2168
|
+
canonizeParam,
|
|
2169
|
+
paramName,
|
|
1999
2170
|
humanTitle,
|
|
2000
2171
|
prefixLeadingDigit,
|
|
2001
2172
|
debugpath,
|
|
@@ -2008,12 +2179,18 @@ export {
|
|
|
2008
2179
|
getModelPath,
|
|
2009
2180
|
sortedKeys,
|
|
2010
2181
|
sortedEntries,
|
|
2182
|
+
collapseScalarAllOf,
|
|
2011
2183
|
isEntityWrapperProp,
|
|
2184
|
+
itemEnvelopeKey,
|
|
2185
|
+
itemEnvelopeTransform,
|
|
2186
|
+
itemEnvelopeOf,
|
|
2012
2187
|
envelopeProp,
|
|
2013
2188
|
envelopeItemRef,
|
|
2014
2189
|
composedEnvelopeProp,
|
|
2190
|
+
propIsList,
|
|
2015
2191
|
mergedProperties,
|
|
2016
2192
|
closedBodyTransform,
|
|
2193
|
+
requestWrapperOf,
|
|
2017
2194
|
untaggedUnionBranches,
|
|
2018
2195
|
scanUntaggedUnion,
|
|
2019
2196
|
firstSentence,
|