@voxgig/apidef 8.31.0 → 8.32.1

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.
@@ -40,6 +40,7 @@ const argsTransform: Transform = async function(
40
40
  ): Promise<TransformResult> {
41
41
  const { apimodel, def } = ctx
42
42
  const kit: KitModel = apimodel.main[KIT]
43
+ const swagger = null != def?.swagger
43
44
 
44
45
  let msg = 'args '
45
46
 
@@ -75,7 +76,7 @@ const argsTransform: Transform = async function(
75
76
  argdefs.push(...routeArgdefs(def, mpoint))
76
77
  }
77
78
 
78
- resolveArgs(ctx, ment, mop, mpoint, argdefs)
79
+ resolveArgs(ctx, ment, mop, mpoint, argdefs, swagger)
79
80
  })
80
81
 
81
82
  })
@@ -98,7 +99,7 @@ function routeArgdefs(def: any, mpoint: ModelPoint): ParameterDef[] {
98
99
  function routeArgNames(def: any, mpoint: ModelPoint): string[] {
99
100
  const route = { ...mpoint, g: {} } as ModelPoint
100
101
  resolveArgs(undefined, { name: '' } as ModelEntity, { name: '', points: [] } as unknown as ModelOp, route,
101
- routeArgdefs(def, mpoint))
102
+ routeArgdefs(def, mpoint), null != def?.swagger)
102
103
  return Object.values(route.g).flat().map((arg: any) => arg.n)
103
104
  }
104
105
 
@@ -130,7 +131,8 @@ const ARG_KIND: Record<string, ModelArg["k"]> = {
130
131
 
131
132
  function resolveArgs(
132
133
  ctx: any,
133
- ment: ModelEntity, mop: ModelOp, mpoint: ModelPoint, argdefs: ParameterDef[]
134
+ ment: ModelEntity, mop: ModelOp, mpoint: ModelPoint, argdefs: ParameterDef[],
135
+ swagger: boolean,
134
136
  ) {
135
137
  const touchedKeys = new Set<string>()
136
138
  const placeholders = [...String(mpoint.o ?? '').matchAll(/\{([^}]+)\}/g)].map((m) => m[1])
@@ -205,6 +207,12 @@ function resolveArgs(
205
207
  marg.t = ['`$ONE`', '`$NULL`', marg.t]
206
208
  }
207
209
 
210
+ const sent = path ? undefined : argSerialization(argdef, kind, swagger)
211
+ if (null != sent) {
212
+ marg.style = sent.style
213
+ marg.explode = sent.explode
214
+ }
215
+
208
216
  const argsKey = (marg.k === 'param' ? 'params' : marg.k) as keyof typeof mpoint.g
209
217
  let kindargs = (mpoint.g[argsKey] = mpoint.g[argsKey] ?? [])
210
218
  kindargs.push(marg)
@@ -245,6 +253,32 @@ function resolveArgs(
245
253
  }
246
254
 
247
255
 
256
+ // The OpenAPI style for each Swagger collection format, where `tsv` has none.
257
+ const SWAGGER_STYLE: Record<string, string> = {
258
+ ssv: 'spaceDelimited', pipes: 'pipeDelimited', tsv: 'tabDelimited',
259
+ }
260
+
261
+
262
+ // How a query, header or cookie argument sends a list or a map: as declared,
263
+ // else as its location does, where only a form explodes. A Swagger array
264
+ // declares a collection format instead, `csv` unless stated. A parameter with
265
+ // `content` is sent as its media type says, so it has neither.
266
+ function argSerialization(argdef: any, kind: string, swagger: boolean):
267
+ { style: string, explode: boolean } | undefined {
268
+ const location = 'header' === kind ? 'simple' : 'form'
269
+ if (swagger && 'array' === argdef.type) {
270
+ const format = 'string' === typeof argdef.collectionFormat ? argdef.collectionFormat : 'csv'
271
+ return 'multi' === format ? { style: 'form', explode: true } :
272
+ { style: SWAGGER_STYLE[format] ?? location, explode: false }
273
+ }
274
+ if (null == argdef.schema && null != argdef.content) {
275
+ return undefined
276
+ }
277
+ const style = 'string' === typeof argdef.style && '' !== argdef.style ? argdef.style : location
278
+ return { style, explode: 'boolean' === typeof argdef.explode ? argdef.explode : 'form' === style }
279
+ }
280
+
281
+
248
282
  // Type facts sit on a Swagger 2 parameter itself, and under `schema` in
249
283
  // OpenAPI 3.
250
284
  function paramSchema(argdef: any): any {
package/src/types.ts CHANGED
@@ -315,6 +315,7 @@ type GuidePathOp = {
315
315
  transform: {
316
316
  req: any
317
317
  res: any
318
+ page?: Record<string, string>
318
319
  }
319
320
  }
320
321
 
package/src/utility.ts CHANGED
@@ -2238,6 +2238,62 @@ function pageRecords(schema: any): any {
2238
2238
  }
2239
2239
 
2240
2240
 
2241
+ // The names that hold each part of a page's metadata, preferred first.
2242
+ const PAGE_ROLES: [string, string[]][] = [
2243
+ ['total', ['total', 'totalcount', 'totalresults', 'totalitems', 'totalhits', 'count']],
2244
+ ['cursor', ['nextcursor', 'cursor', 'nextpagetoken', 'nexttoken', 'endcursor']],
2245
+ ['more', ['hasmore', 'hasnext', 'hasnextpage']],
2246
+ ['next', ['next', 'nexturl', 'nextlink', 'nextpageurl']],
2247
+ ]
2248
+
2249
+
2250
+ function isPageRoleName(name: string): boolean {
2251
+ const norm = name.toLowerCase().replace(/[_-]/g, '')
2252
+ return PAGE_ROLES.some(([, names]) => names.includes(norm))
2253
+ }
2254
+
2255
+
2256
+ // Where a page keeps its total, the cursor or link to the next page, and
2257
+ // whether there is one: beside the records, else in the page's metadata.
2258
+ function pageTransform(props: any, records: string): Record<string, string> | undefined {
2259
+ if (null == props || 'object' !== typeof props) {
2260
+ return undefined
2261
+ }
2262
+ const levels: [string, any][] = [['', props]]
2263
+ for (const k of keysof(props)) {
2264
+ const meta = k !== records && isPageMetaProp(k) ? mergedProperties(props[k]) : undefined
2265
+ if (null != meta) {
2266
+ levels.push([k + '.', meta])
2267
+ }
2268
+ }
2269
+ // A `count` beside data of its own counts that data, as GitHub's traffic does.
2270
+ const ownData = holdsOwnData(Object.fromEntries(
2271
+ Object.entries(props).filter(([k]) => !isPageRoleName(k))))
2272
+ const find = (name: string): string | undefined => {
2273
+ for (const [prefix, level] of levels) {
2274
+ if ('count' === name && '' === prefix && ownData) {
2275
+ continue
2276
+ }
2277
+ const key = keysof(level).find((k: string) => ('' !== prefix || k !== records) &&
2278
+ name === k.toLowerCase().replace(/[_-]/g, '') &&
2279
+ !isObjectSchema(level[k]) && false === propIsList(level[k]))
2280
+ if (null != key) {
2281
+ return '`body.' + prefix + key + '`'
2282
+ }
2283
+ }
2284
+ return undefined
2285
+ }
2286
+ const page: Record<string, string> = {}
2287
+ for (const [role, names] of PAGE_ROLES) {
2288
+ const path = names.map(find).find((found) => null != found)
2289
+ if (null != path) {
2290
+ page[role] = path
2291
+ }
2292
+ }
2293
+ return 0 < keysof(page).length ? page : undefined
2294
+ }
2295
+
2296
+
2241
2297
  function answersPage(schema: any): boolean {
2242
2298
  return null != pageRecords(schema)
2243
2299
  }
@@ -2507,6 +2563,8 @@ export {
2507
2563
  envelopeItemRef,
2508
2564
  composedEnvelopeProp,
2509
2565
  pageRecords,
2566
+ pageTransform,
2567
+ PAGE_ROLES,
2510
2568
  answersPage,
2511
2569
  answersRecordMap,
2512
2570
  propIsList,