@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.
Files changed (54) hide show
  1. package/dist/apidef.d.ts +1 -1
  2. package/dist/apidef.js +2 -0
  3. package/dist/apidef.js.map +1 -1
  4. package/dist/builder/entity/entity.d.ts +2 -1
  5. package/dist/builder/entity/entity.js +14 -11
  6. package/dist/builder/entity/entity.js.map +1 -1
  7. package/dist/guide/guide.js +5 -2
  8. package/dist/guide/guide.js.map +1 -1
  9. package/dist/guide/heuristic01.d.ts +11 -1
  10. package/dist/guide/heuristic01.js +457 -100
  11. package/dist/guide/heuristic01.js.map +1 -1
  12. package/dist/model.d.ts +18 -1
  13. package/dist/transform/args.d.ts +4 -1
  14. package/dist/transform/args.js +67 -20
  15. package/dist/transform/args.js.map +1 -1
  16. package/dist/transform/body.d.ts +23 -0
  17. package/dist/transform/body.js +474 -0
  18. package/dist/transform/body.js.map +1 -0
  19. package/dist/transform/entity.d.ts +1 -1
  20. package/dist/transform/entity.js +13 -4
  21. package/dist/transform/entity.js.map +1 -1
  22. package/dist/transform/field.d.ts +3 -1
  23. package/dist/transform/field.js +159 -87
  24. package/dist/transform/field.js.map +1 -1
  25. package/dist/transform/operation.js +77 -24
  26. package/dist/transform/operation.js.map +1 -1
  27. package/dist/transform/select.js +44 -11
  28. package/dist/transform/select.js.map +1 -1
  29. package/dist/transform/top.d.ts +3 -2
  30. package/dist/transform/top.js +127 -11
  31. package/dist/transform/top.js.map +1 -1
  32. package/dist/tsconfig.tsbuildinfo +1 -1
  33. package/dist/types.d.ts +6 -0
  34. package/dist/types.js.map +1 -1
  35. package/dist/utility.d.ts +11 -1
  36. package/dist/utility.js +156 -13
  37. package/dist/utility.js.map +1 -1
  38. package/model/apidef.aontu +29 -6
  39. package/model/guide.aontu +8 -0
  40. package/package.json +1 -1
  41. package/src/apidef.ts +5 -0
  42. package/src/builder/entity/entity.ts +17 -15
  43. package/src/guide/guide.ts +5 -3
  44. package/src/guide/heuristic01.ts +587 -104
  45. package/src/model.ts +27 -0
  46. package/src/transform/args.ts +83 -22
  47. package/src/transform/body.ts +638 -0
  48. package/src/transform/entity.ts +20 -4
  49. package/src/transform/field.ts +190 -88
  50. package/src/transform/operation.ts +97 -26
  51. package/src/transform/select.ts +49 -11
  52. package/src/transform/top.ts +154 -15
  53. package/src/types.ts +6 -0
  54. package/src/utility.ts +190 -13
package/src/model.ts CHANGED
@@ -129,6 +129,28 @@ type ModelPathSegment = {
129
129
  }
130
130
 
131
131
 
132
+ type BodyKind = 'json' | 'raw' | 'multipart' | 'form'
133
+
134
+
135
+ type ModelBodyField = {
136
+ name: string
137
+ binary?: boolean
138
+ list?: boolean
139
+ join?: string
140
+ media?: string
141
+ }
142
+
143
+
144
+ // One media type of a request body or a success response.
145
+ type ModelBody = {
146
+ kind: BodyKind
147
+ media: string
148
+ binary?: boolean
149
+ fields?: ModelBodyField[]
150
+ alternatives?: ModelBody[]
151
+ }
152
+
153
+
132
154
  type ModelPoint = {
133
155
  co?: { version: 2, id: string, source: string }
134
156
  li?: boolean | Record<string, any>
@@ -157,6 +179,8 @@ type ModelPoint = {
157
179
  exist: string[]
158
180
  $action?: string
159
181
  }
182
+ rb?: ModelBody
183
+ rs?: ModelBody
160
184
  }
161
185
 
162
186
 
@@ -250,6 +274,9 @@ export type {
250
274
  ModelArg,
251
275
  ModelPoint,
252
276
  ModelPathSegment,
277
+ BodyKind,
278
+ ModelBodyField,
279
+ ModelBody,
253
280
  ModelOp,
254
281
  ModelEntity,
255
282
  ModelEntityFlow,
@@ -4,7 +4,14 @@ import { each, snakify } from 'jostraca'
4
4
 
5
5
  import type { TransformResult, Transform } from '../transform'
6
6
 
7
- import { formatJSONIC, depluralize, inferFieldType, normalizeFieldName, validator } from '../utility'
7
+ import {
8
+ depluralize,
9
+ canonizeParam,
10
+ inferFieldType,
11
+ normalizeFieldName,
12
+ paramName,
13
+ validator,
14
+ } from '../utility'
8
15
 
9
16
 
10
17
  import { KIT } from '../types'
@@ -64,11 +71,7 @@ const argsTransform: Transform = async function(
64
71
  }
65
72
  }
66
73
  else {
67
- const pathdef: PathDef = def.paths[mpoint.o]
68
- argdefs.push(...((pathdef as any)?.parameters ?? []))
69
-
70
- const opdef: MethodDef = (pathdef as any)?.[mpoint.m.toLowerCase()]
71
- argdefs.push(...(opdef?.parameters ?? []))
74
+ argdefs.push(...routeArgdefs(def, mpoint))
72
75
  }
73
76
 
74
77
  resolveArgs(ctx, ment, mop, mpoint, argdefs)
@@ -83,6 +86,22 @@ const argsTransform: Transform = async function(
83
86
  }
84
87
 
85
88
 
89
+ function routeArgdefs(def: any, mpoint: ModelPoint): ParameterDef[] {
90
+ const pathdef: PathDef = def.paths?.[mpoint.o]
91
+ const opdef: MethodDef = (pathdef as any)?.[mpoint.m.toLowerCase()]
92
+ return [...((pathdef as any)?.parameters ?? []), ...(opdef?.parameters ?? [])]
93
+ }
94
+
95
+
96
+ // The names a caller gives a REST route's arguments, as this step names them.
97
+ function routeArgNames(def: any, mpoint: ModelPoint): string[] {
98
+ const route = { ...mpoint, g: {} } as ModelPoint
99
+ resolveArgs(undefined, { name: '' } as ModelEntity, { name: '', points: [] } as unknown as ModelOp, route,
100
+ routeArgdefs(def, mpoint))
101
+ return Object.values(route.g).flat().map((arg: any) => arg.n)
102
+ }
103
+
104
+
86
105
  // Locate the normalised root-field descriptor a GraphQL point came from.
87
106
  function graphqlFieldDef(def: any, mpoint: ModelPoint): any {
88
107
  const field = mpoint.gq?.field ?? mpoint.o
@@ -113,8 +132,14 @@ function resolveArgs(
113
132
  ment: ModelEntity, mop: ModelOp, mpoint: ModelPoint, argdefs: ParameterDef[]
114
133
  ) {
115
134
  const touchedKeys = new Set<string>()
135
+ const placeholders = [...String(mpoint.o ?? '').matchAll(/\{([^}]+)\}/g)].map((m) => m[1])
116
136
 
117
137
  each(argdefs, (argdef: ParameterDef) => {
138
+ // A Swagger body parameter is the request body, which the body step reads.
139
+ if ('body' === (argdef as any).in) {
140
+ return
141
+ }
142
+
118
143
  const specName = normalizeFieldName(argdef.name)
119
144
  const orig = depluralize(snakify(specName))
120
145
 
@@ -132,23 +157,45 @@ function resolveArgs(
132
157
  return
133
158
  }
134
159
 
135
- const kind = ARG_KIND[argdef.in] ?? 'query'
136
- // Rename map can be keyed by either the spec original (camelCase) or by
137
- // the snakified form depending on which path went through heuristic01.
138
- // Try both before falling through to `orig`.
160
+ let kind = ARG_KIND[argdef.in] ?? 'query'
161
+ let placed = false
162
+ const where: unknown = argdef.in
163
+ if ('string' !== typeof where || '' === where) {
164
+ placed = placeholders.includes(argdef.name)
165
+ kind = placed ? 'param' : 'query'
166
+ ctx?.warn?.({
167
+ note: `Parameter ${argdef.name} on entity=${ment.name} op=${mop.name}` +
168
+ ` path=${mpoint.o} has no \`in\`` +
169
+ (placed ? `; it names the path placeholder {${argdef.name}}, so it is taken as` +
170
+ ' a path parameter.' : ', so it is taken as a query parameter.') +
171
+ ' A parameter needs an `in`.',
172
+ entity: ment.name,
173
+ path: mpoint.o,
174
+ op: mop.name,
175
+ param: argdef.name,
176
+ })
177
+ }
178
+ // A path argument is named by the lookup that names its segment, so the
179
+ // two agree under a raw rename key, and one that fills a placeholder is
180
+ // required, as OpenAPI requires; a GraphQL argument keeps its own flag.
181
+ // Any other rename map is keyed by the spec original or the snakified form.
182
+ const path = 'param' === kind
183
+ const fills = path && placeholders.some((p) => p === argdef.name || canonizeParam(p) === orig)
139
184
  const renameMap = mpoint.r[kind]
140
- const name = renameMap?.[specName] ?? renameMap?.[orig] ?? orig
185
+ const name = path ? paramName(argdef.name, mpoint.r.param) :
186
+ (renameMap?.[specName] ?? renameMap?.[orig] ?? orig)
187
+ const schema = paramSchema(argdef)
141
188
  // The name the definition gives, which the SDK sends on the wire. The
142
189
  // model name beside it is only what a caller writes.
143
190
  const marg: ModelArg = {
144
191
  n: name,
145
192
  or: String(argdef.name),
146
- t: inferFieldType(name, validator(argdef.schema?.type)),
193
+ t: inferFieldType(name, validator(schema?.type)),
147
194
  k: kind,
148
- r: !!argdef.required
195
+ r: fills || !!argdef.required
149
196
  }
150
197
 
151
- const example = resolveArgExample(argdef)
198
+ const example = resolveArgExample(argdef, schema)
152
199
  if (undefined !== example) {
153
200
  marg.ex = example
154
201
  }
@@ -167,12 +214,11 @@ function resolveArgs(
167
214
  // DELETE /call/{id}, still takes a value: it gets a required string
168
215
  // argument under its own name, and a warning.
169
216
  if ('graphql' !== mpoint.k) {
170
- const canon = (wire: string) => depluralize(snakify(normalizeFieldName(wire)))
171
- const declared = new Set((mpoint.g.params ?? []).map((arg: ModelArg) => canon(String(arg.or))))
172
- for (const [, wire] of String(mpoint.o ?? '').matchAll(/\{([^}]+)\}/g)) {
173
- const orig = canon(wire)
174
- const renameMap = mpoint.r.param
175
- const name = renameMap?.[wire] ?? renameMap?.[normalizeFieldName(wire)] ?? renameMap?.[orig] ?? orig
217
+ const declared = new Set((mpoint.g.params ?? [])
218
+ .map((arg: ModelArg) => canonizeParam(String(arg.or))))
219
+ for (const wire of placeholders) {
220
+ const orig = canonizeParam(wire)
221
+ const name = paramName(wire, mpoint.r.param)
176
222
  // A declared parameter the placeholder is renamed to already fills it.
177
223
  if ('' === orig || declared.has(orig) ||
178
224
  (mpoint.g.params ?? []).some((arg: ModelArg) => arg.n === name)) continue
@@ -198,7 +244,21 @@ function resolveArgs(
198
244
  }
199
245
 
200
246
 
201
- function resolveArgExample(argdef: any): any {
247
+ // Type facts sit on a Swagger 2 parameter itself, and under `schema` in
248
+ // OpenAPI 3. A formData parameter converts to a body field, not a parameter.
249
+ function paramSchema(argdef: any): any {
250
+ if (null != argdef?.schema) {
251
+ return argdef.schema
252
+ }
253
+ if ('formData' === argdef?.in) {
254
+ return undefined
255
+ }
256
+ // Swagger 2's file type is a binary string in OpenAPI 3.
257
+ return 'file' === argdef?.type ? { ...argdef, type: 'string', format: 'binary' } : argdef
258
+ }
259
+
260
+
261
+ function resolveArgExample(argdef: any, schema: any): any {
202
262
  if (undefined !== argdef?.example) return argdef.example
203
263
 
204
264
  const examples = argdef?.examples
@@ -210,7 +270,6 @@ function resolveArgExample(argdef: any): any {
210
270
  }
211
271
  }
212
272
 
213
- const schema = argdef?.schema
214
273
  if (schema) {
215
274
  if (undefined !== schema.example) return schema.example
216
275
  if (undefined !== schema.default) return schema.default
@@ -222,4 +281,6 @@ function resolveArgExample(argdef: any): any {
222
281
 
223
282
  export {
224
283
  argsTransform,
284
+ resolveArgExample,
285
+ routeArgNames,
225
286
  }