@voxgig/apidef 8.23.0 → 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.
Files changed (39) hide show
  1. package/dist/builder/entity/entity.d.ts +2 -1
  2. package/dist/builder/entity/entity.js +14 -11
  3. package/dist/builder/entity/entity.js.map +1 -1
  4. package/dist/guide/guide.js +5 -2
  5. package/dist/guide/guide.js.map +1 -1
  6. package/dist/guide/heuristic01.d.ts +2 -1
  7. package/dist/guide/heuristic01.js +232 -85
  8. package/dist/guide/heuristic01.js.map +1 -1
  9. package/dist/transform/args.d.ts +4 -1
  10. package/dist/transform/args.js +42 -13
  11. package/dist/transform/args.js.map +1 -1
  12. package/dist/transform/body.d.ts +19 -2
  13. package/dist/transform/body.js +189 -15
  14. package/dist/transform/body.js.map +1 -1
  15. package/dist/transform/field.d.ts +3 -1
  16. package/dist/transform/field.js +159 -87
  17. package/dist/transform/field.js.map +1 -1
  18. package/dist/transform/operation.js +77 -24
  19. package/dist/transform/operation.js.map +1 -1
  20. package/dist/transform/select.js +10 -4
  21. package/dist/transform/select.js.map +1 -1
  22. package/dist/transform/top.d.ts +2 -1
  23. package/dist/transform/top.js +50 -3
  24. package/dist/transform/top.js.map +1 -1
  25. package/dist/tsconfig.tsbuildinfo +1 -1
  26. package/dist/utility.d.ts +9 -1
  27. package/dist/utility.js +139 -13
  28. package/dist/utility.js.map +1 -1
  29. package/package.json +1 -1
  30. package/src/builder/entity/entity.ts +17 -15
  31. package/src/guide/guide.ts +5 -3
  32. package/src/guide/heuristic01.ts +292 -93
  33. package/src/transform/args.ts +50 -14
  34. package/src/transform/body.ts +231 -15
  35. package/src/transform/field.ts +190 -88
  36. package/src/transform/operation.ts +97 -26
  37. package/src/transform/select.ts +13 -4
  38. package/src/transform/top.ts +59 -3
  39. package/src/utility.ts +169 -13
@@ -96,10 +96,19 @@ const topTransform = async function(
96
96
  // Institute of Chicago lists `api.artic.edu/api/v1` (no
97
97
  // https://). Go's net/http barfs on that with "unsupported
98
98
  // protocol scheme". Default to https when the URL has no scheme
99
- // and the value isn't a relative path.
99
+ // and is neither a relative path nor opened by a server variable.
100
100
  for (const server of (kit.info.servers as any[])) {
101
101
  if (!server || 'string' !== typeof server.url) continue
102
- server.url = withScheme(server.url)
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
+ }
103
112
  }
104
113
 
105
114
  // No server named: the `server` option, else a `base` variable the caller fills.
@@ -199,15 +208,61 @@ function resolveSummary(def: any): string | undefined {
199
208
  }
200
209
 
201
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
+
202
218
  function withScheme(url: string): string {
203
219
  const u = url.trim()
204
220
  if ('' === u || /^[a-z][a-z0-9+.-]*:\/\//i.test(u)) return url
205
221
  if (u.startsWith('//')) return 'https:' + u
206
- if (u.startsWith('/')) return url
222
+ if (u.startsWith('/') || LEADING_VARIABLE_RE.test(u)) return url
207
223
  return 'https://' + u
208
224
  }
209
225
 
210
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
+
211
266
  function resolveWebsite(def: any, servers: any[]): string | undefined {
212
267
  const info = def?.info ?? {}
213
268
 
@@ -506,6 +561,7 @@ function stringifyInfoScalars(node: any): any {
506
561
 
507
562
  export {
508
563
  topTransform,
564
+ stringifyInfoScalars,
509
565
  resolveSecurity,
510
566
  resolveSummary,
511
567
  ensureDescription,
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>()
@@ -1613,30 +1655,112 @@ export type {
1613
1655
  }
1614
1656
 
1615
1657
 
1616
- // A response property only "wraps" the entity when it is itself a structured
1617
- // value that could contain the entity: an object, an array, a $ref, or a
1618
- // composed (allOf/oneOf/anyOf) schema. A scalar property (string, integer,
1619
- // number, boolean) that merely shares the entity's name is a field of the
1620
- // entity, not a wrapper, so the response must not be unwrapped down to it.
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.
1621
1704
  function isEntityWrapperProp(propSchema: any): boolean {
1622
- if (null == propSchema || 'object' !== typeof propSchema) {
1705
+ const prop = collapseScalarAllOf(propSchema)
1706
+ if (null == prop || 'object' !== typeof prop) {
1623
1707
  return false
1624
1708
  }
1625
- if (null != propSchema.$ref) {
1709
+ if (null != prop.$ref) {
1626
1710
  return true
1627
1711
  }
1628
- if (null != propSchema.properties ||
1629
- null != propSchema.items ||
1630
- null != propSchema.allOf ||
1631
- null != propSchema.oneOf ||
1632
- null != propSchema.anyOf) {
1712
+ if (null != prop.properties ||
1713
+ null != prop.items ||
1714
+ null != prop.allOf ||
1715
+ holdsStructuredBranch(prop.oneOf) ||
1716
+ holdsStructuredBranch(prop.anyOf)) {
1633
1717
  return true
1634
1718
  }
1635
- const t = propSchema.type
1719
+ const t = prop.type
1636
1720
  return 'object' === t || 'array' === t
1637
1721
  }
1638
1722
 
1639
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
+
1640
1764
  function envelopeProp(resprops: any, opname: string): string | null {
1641
1765
  const keys = keysof(resprops)
1642
1766
  if (0 === keys.length) {
@@ -1973,6 +2097,30 @@ function closedBodyTransform(schema: any): Record<string, string> | null {
1973
2097
  return out
1974
2098
  }
1975
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
+
1976
2124
  function firstSentence(text: string): string {
1977
2125
  const collapsed = text.replace(/\s+/g, ' ').trim()
1978
2126
  const m = collapsed.match(/^(.+?[.!?])(\s|$)/)
@@ -1986,11 +2134,13 @@ function firstSentence(text: string): string {
1986
2134
 
1987
2135
 
1988
2136
  export {
2137
+ bodyRequestTransform,
1989
2138
  nom,
1990
2139
  getdlog,
1991
2140
  loadFile,
1992
2141
  formatJsonSrc,
1993
2142
  depluralize,
2143
+ pluralize,
1994
2144
  setCustomPlurals,
1995
2145
  clearCustomPlurals,
1996
2146
  find,
@@ -2029,12 +2179,18 @@ export {
2029
2179
  getModelPath,
2030
2180
  sortedKeys,
2031
2181
  sortedEntries,
2182
+ collapseScalarAllOf,
2032
2183
  isEntityWrapperProp,
2184
+ itemEnvelopeKey,
2185
+ itemEnvelopeTransform,
2186
+ itemEnvelopeOf,
2033
2187
  envelopeProp,
2034
2188
  envelopeItemRef,
2035
2189
  composedEnvelopeProp,
2190
+ propIsList,
2036
2191
  mergedProperties,
2037
2192
  closedBodyTransform,
2193
+ requestWrapperOf,
2038
2194
  untaggedUnionBranches,
2039
2195
  scanUntaggedUnion,
2040
2196
  firstSentence,