@voxgig/sdkgen 4.40.0 → 4.41.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 (85) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/cmp/Main.js +4 -0
  3. package/dist/cmp/Main.js.map +1 -1
  4. package/dist/helpers/testPolicy.d.ts +5 -1
  5. package/dist/helpers/testPolicy.js +32 -3
  6. package/dist/helpers/testPolicy.js.map +1 -1
  7. package/dist/helpers/useragent.d.ts +2 -0
  8. package/dist/helpers/useragent.js +31 -0
  9. package/dist/helpers/useragent.js.map +1 -0
  10. package/dist/sdkgen.d.ts +2 -2
  11. package/dist/sdkgen.js +6 -3
  12. package/dist/sdkgen.js.map +1 -1
  13. package/dist/testkit.js +1 -1
  14. package/dist/testkit.js.map +1 -1
  15. package/dist/tsconfig.tsbuildinfo +1 -1
  16. package/model/sdkgen.aontu +6 -0
  17. package/package.json +2 -2
  18. package/project/.sdk/src/cmp/csharp/TestEntity_csharp.ts +35 -16
  19. package/project/.sdk/src/cmp/go/ReadmeExamplesTest_go.ts +7 -6
  20. package/project/.sdk/src/cmp/go/TestEntity_go.ts +24 -13
  21. package/project/.sdk/src/cmp/java/TestEntity_java.ts +21 -11
  22. package/project/.sdk/src/cmp/kotlin/TestEntity_kotlin.ts +26 -14
  23. package/project/.sdk/src/cmp/lua/ReadmeExamplesTest_lua.ts +7 -5
  24. package/project/.sdk/src/cmp/lua/ReadmeHowto_lua.ts +12 -0
  25. package/project/.sdk/src/cmp/lua/TestEntity_lua.ts +25 -13
  26. package/project/.sdk/src/cmp/perl/ReadmeExamplesTest_perl.ts +15 -1
  27. package/project/.sdk/src/cmp/perl/TestEntity_perl.ts +23 -13
  28. package/project/.sdk/src/cmp/php/ReadmeExamplesTest_php.ts +31 -2
  29. package/project/.sdk/src/cmp/php/TestEntity_php.ts +29 -14
  30. package/project/.sdk/src/cmp/py/ReadmeExamplesTest_py.ts +14 -1
  31. package/project/.sdk/src/cmp/py/TestEntity_py.ts +28 -14
  32. package/project/.sdk/src/cmp/rb/ReadmeExamplesTest_rb.ts +18 -1
  33. package/project/.sdk/src/cmp/rb/TestEntity_rb.ts +24 -13
  34. package/project/.sdk/src/cmp/rust/TestEntity_rust.ts +27 -14
  35. package/project/.sdk/src/cmp/ts/ReadmeExamplesTest_ts.ts +7 -2
  36. package/project/.sdk/tm/c/feature/test.c +22 -2
  37. package/project/.sdk/tm/c/tests/transport_test.c +2 -2
  38. package/project/.sdk/tm/c/utility/http.c +1 -1
  39. package/project/.sdk/tm/clojure/src/sdk/core.clj +1 -1
  40. package/project/.sdk/tm/clojure/src/sdk/features.clj +13 -1
  41. package/project/.sdk/tm/cpp/feature/test.hpp +16 -2
  42. package/project/.sdk/tm/csharp/feature/TestFeature.cs +18 -0
  43. package/project/.sdk/tm/csharp/utility/Fetcher.cs +5 -4
  44. package/project/.sdk/tm/elixir/lib/projectname/feature/test.ex +19 -1
  45. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +1 -1
  46. package/project/.sdk/tm/go/feature/test_feature.go +16 -0
  47. package/project/.sdk/tm/go/utility/fetcher.go +4 -3
  48. package/project/.sdk/tm/java/feature/TestFeature.java +15 -0
  49. package/project/.sdk/tm/java/utility/Fetcher.java +4 -4
  50. package/project/.sdk/tm/js/src/feature/test/TestFeature.js +16 -0
  51. package/project/.sdk/tm/js/src/utility/FetcherUtility.js +4 -3
  52. package/project/.sdk/tm/js/src/utility/ResultBodyUtility.js +19 -1
  53. package/project/.sdk/tm/js/test/live-entity.js +2 -1
  54. package/project/.sdk/tm/kotlin/feature/TestFeature.kt +15 -0
  55. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +1 -1
  56. package/project/.sdk/tm/lua/feature/secrets_feature.lua +9 -2
  57. package/project/.sdk/tm/lua/feature/test_feature.lua +18 -0
  58. package/project/.sdk/tm/lua/utility/fetcher.lua +11 -25
  59. package/project/.sdk/tm/lua/utility/tls.lua +241 -0
  60. package/project/.sdk/tm/ocaml/sdk_features.ml +18 -1
  61. package/project/.sdk/tm/perl/feature/test_feature.pm +12 -0
  62. package/project/.sdk/tm/perl/utility/fetcher.pm +4 -4
  63. package/project/.sdk/tm/php/feature/TestFeature.php +18 -1
  64. package/project/.sdk/tm/php/utility/Fetcher.php +4 -4
  65. package/project/.sdk/tm/py/pkg/feature/test_feature.py +20 -4
  66. package/project/.sdk/tm/py/pkg/utility/fetcher.py +4 -4
  67. package/project/.sdk/tm/rb/feature/test_feature.rb +14 -1
  68. package/project/.sdk/tm/rb/utility/fetcher.rb +56 -6
  69. package/project/.sdk/tm/rust/feature/test.rs +18 -0
  70. package/project/.sdk/tm/rust/utility/fetcher.rs +4 -4
  71. package/project/.sdk/tm/scala/feature/TestFeature.scala +12 -0
  72. package/project/.sdk/tm/scala/utility/Fetcher.scala +1 -1
  73. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/TestFeature.swift +15 -0
  74. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Fetcher.swift +1 -1
  75. package/project/.sdk/tm/ts/src/feature/test/TestFeature.ts +26 -5
  76. package/project/.sdk/tm/ts/src/utility/FetcherUtility.ts +4 -3
  77. package/project/.sdk/tm/ts/src/utility/ResultBodyUtility.ts +19 -1
  78. package/project/.sdk/tm/ts/test/live-entity.ts +2 -1
  79. package/project/.sdk/tm/zig/feature/test.zig +23 -0
  80. package/project/sdkgen-package.json +1 -1
  81. package/src/cmp/Main.ts +5 -0
  82. package/src/helpers/testPolicy.ts +32 -3
  83. package/src/helpers/useragent.ts +41 -0
  84. package/src/sdkgen.ts +4 -0
  85. package/src/testkit.ts +1 -1
@@ -216,6 +216,15 @@ fn build_args(ctx: &Rc<Context>, args: &Value) -> Value {
216
216
  q
217
217
  }
218
218
 
219
+ // The entity's declared identity field (`entity.id.field`), where it names
220
+ // one other than the `id` every record already carries.
221
+ fn declared_id_field(config: &Value, entname: &str) -> Option<String> {
222
+ match crate::core::helpers::getpath(&["entity", entname, "id", "field"], config) {
223
+ Value::Str(f) if !f.is_empty() && f.as_str() != "id" => Some(f.to_string()),
224
+ _ => None,
225
+ }
226
+ }
227
+
219
228
  fn test_fetch(
220
229
  entity: &Value,
221
230
  ctx: &Rc<Context>,
@@ -332,6 +341,11 @@ fn test_fetch(
332
341
  if let Value::Map(_) = &ent {
333
342
  vs::del_prop(ent.clone(), &Value::str("$body"));
334
343
  setp(&ent, "id", id.clone());
344
+ if let Some(id_field) = declared_id_field(&ctx.config.borrow(), &op.entity) {
345
+ if getp(&ent, &id_field).is_noval() || getp(&ent, &id_field).is_null() {
346
+ setp(&ent, &id_field, id.clone());
347
+ }
348
+ }
335
349
  if let Value::Str(id_str) = &id {
336
350
  setp(&entmap, id_str, ent.clone());
337
351
  }
@@ -435,11 +449,15 @@ impl Feature for TestFeature {
435
449
  }
436
450
 
437
451
  // Ensure entity ids are correct.
452
+ let config = ctx.config.borrow().clone();
438
453
  let mut fix_ids = |key: &Value, val: &Value, _parent: &Value, path: &[String]| -> Value {
439
454
  if path.len() == 2 {
440
455
  if let Value::Map(_) = val {
441
456
  if let Value::Str(k) = key {
442
457
  setp(val, "id", Value::str(k.clone()));
458
+ if let Some(id_field) = declared_id_field(&config, &path[0]) {
459
+ setp(val, &id_field, Value::str(k.clone()));
460
+ }
443
461
  }
444
462
  }
445
463
  }
@@ -92,11 +92,11 @@ fn default_http_fetch(fullurl: &str, fetchdef: &Value) -> Result<Value, ProjectN
92
92
  }
93
93
  }
94
94
  }
95
- // Default User-Agent — some CDNs block library defaults. Use a
96
- // Mozilla-shaped UA unless the caller already set one, and record it with
97
- // the headers the request sent.
95
+ // Default User-Agent — some CDNs block library defaults. Use the project's
96
+ // agent (main.kit.http.useragent, browser-shaped by default) unless the
97
+ // caller already set one, and record it with the headers the request sent.
98
98
  if !has_ua {
99
- let agent = "Mozilla/5.0 (compatible; ProjectNameSDK/1.0)";
99
+ let agent = "PROJECTUSERAGENT";
100
100
  req = req.set("User-Agent", agent);
101
101
  if let Value::Map(_) = &sent_headers {
102
102
  setp(&sent_headers, "user-agent", Value::str(agent));
@@ -27,6 +27,8 @@ class TestFeature extends BaseFeature("test", "0.0.1", true) {
27
27
  Struct.walk(entity, (key: String, valx: Object, parent: Object, path: java.util.List[String]) => {
28
28
  if (path != null && path.size() == 2 && valx.isInstanceOf[JMap[_, _]] && key != null) {
29
29
  valx.asInstanceOf[JMap[String, Object]].put("id", key)
30
+ val idField = declaredIdField(ctx.config, path.get(0))
31
+ if (idField != null) valx.asInstanceOf[JMap[String, Object]].put(idField, key)
30
32
  }
31
33
  valx
32
34
  })
@@ -40,6 +42,14 @@ class TestFeature extends BaseFeature("test", "0.0.1", true) {
40
42
  else ctx.utility.fetcher = makeNetsim(net, testFetcher)
41
43
  }
42
44
 
45
+ // The entity's declared identity field (`entity.id.field`), where it names
46
+ // one other than the `id` every record already carries.
47
+ private def declaredIdField(config: Object, entityName: String): String =
48
+ Struct.getpath(config, java.util.List.of("entity", if (entityName == null) "" else entityName, "id", "field")) match {
49
+ case s: String if s != "" && s != "id" => s
50
+ case _ => null
51
+ }
52
+
43
53
  // THE MOCK HAS TO AGREE WITH THE MODEL. A point carrying
44
54
  // `transform.res: `body.item`` describes an API that answers {"item": {...}}
45
55
  // and the response transform unwraps that key on the way back. Returning the
@@ -204,6 +214,8 @@ class TestFeature extends BaseFeature("test", "0.0.1", true) {
204
214
  case entm0: JMap[_, _] =>
205
215
  val entm = entm0.asInstanceOf[JMap[String, Object]]
206
216
  entm.put("id", id)
217
+ val idField = declaredIdField(ctx.config, op.entity)
218
+ if (idField != null && entm.get(idField) == null) entm.put(idField, id)
207
219
  id match { case s: String => entmap.put(s, entm); case _ => }
208
220
  Struct.delprop(entm, "$KEY")
209
221
  val out = Struct.clone(entm)
@@ -93,7 +93,7 @@ object Fetcher {
93
93
  }
94
94
  // The default User-Agent is recorded with the headers the request sent.
95
95
  if (!hasUA) {
96
- val agent = "Mozilla/5.0 (compatible; ProjectNameSDK/1.0)"
96
+ val agent = "PROJECTUSERAGENT"
97
97
  reqb.setHeader("User-Agent", agent)
98
98
  fetchdef.get("headers") match {
99
99
  case hm: JMap[_, _] => hm.asInstanceOf[JMap[String, Object]].put("user-agent", agent)
@@ -79,6 +79,15 @@ private func testResolveMatch(_ ctx2: Context, _ explicitMatch: VMap) -> VMap {
79
79
  return VMap()
80
80
  }
81
81
 
82
+ // The entity's declared identity field (`entity.id.field`), where it names
83
+ // one other than the `id` every record already carries.
84
+ private func testDeclaredIdField(_ config: VMap?, _ entityName: String) -> String? {
85
+ guard let config = config,
86
+ let field = getpath(.map(config), jtp("entity", entityName, "id", "field")).asString,
87
+ field != "", field != "id" else { return nil }
88
+ return field
89
+ }
90
+
82
91
  // The record the mock keeps: the request data without `$body`, which only the
83
92
  // wire carries.
84
93
  private func testRecord(_ reqdata: VMap) -> VMap {
@@ -197,6 +206,9 @@ public final class TestFeature: BaseFeature {
197
206
  _ = walk(.map(entity), { key, val, _, path in
198
207
  if path.count == 2, val.isMap, key.asString != nil {
199
208
  val.asMap!.entries["id"] = .string(strkey(key))
209
+ if let idField = testDeclaredIdField(ctx.config, path[0]) {
210
+ val.asMap!.entries[idField] = .string(strkey(key))
211
+ }
200
212
  }
201
213
  return val
202
214
  }, nil)
@@ -275,6 +287,9 @@ public final class TestFeature: BaseFeature {
275
287
  let ent = clone(.map(testRecord(ctx2.reqdata)))
276
288
  if let entm = ent.asMap {
277
289
  entm.entries["id"] = id
290
+ if let idField = testDeclaredIdField(ctx2.config, op.entity), isNil(entm.entries[idField] ?? .noval) {
291
+ entm.entries[idField] = id
292
+ }
278
293
  if let idStr = id.asString { entmap.entries[idStr] = .map(entm) }
279
294
  delprop(.map(entm), .string("$KEY"))
280
295
  return testRespond(ctx2, 200, clone(.map(entm)), nil)
@@ -144,7 +144,7 @@ private func sendOnce(_ fullurl: String, _ fetchdef: VMap) throws -> Value {
144
144
  }
145
145
  // The default User-Agent is recorded with the headers the request sent.
146
146
  if !hasUA {
147
- let agent = "Mozilla/5.0 (compatible; ProjectNameSDK/1.0)"
147
+ let agent = "PROJECTUSERAGENT"
148
148
  req.setValue(agent, forHTTPHeaderField: "User-Agent")
149
149
  if let sent = gp(fetchdef, "headers").asMap {
150
150
  sent.entries["user-agent"] = .string(agent)
@@ -19,7 +19,27 @@ function mintId(): string {
19
19
  }
20
20
 
21
21
 
22
+ // The entity's declared identity field (`entity.id.field`), where it names
23
+ // one other than the `id` every record already carries.
24
+ function declaredIdField(config: any, getpath: any, entityName: string): string | null {
25
+ const field = getpath(config, ['entity', entityName, 'id', 'field'])
26
+ return 'string' === typeof field && '' !== field && 'id' !== field ? field : null
27
+ }
28
+
29
+
22
30
  function ownIdField(config: any, getpath: any, entityName: string): string {
31
+ return declaredIdField(config, getpath, entityName) ?? routeIdField(config, getpath, entityName)
32
+ }
33
+
34
+
35
+ // The keys besides `id` a record is found by: declared field, then route param.
36
+ function ownIdFields(config: any, getpath: any, entityName: string): string[] {
37
+ const fields = [ownIdField(config, getpath, entityName), routeIdField(config, getpath, entityName)]
38
+ return fields.filter((f, i) => 'id' !== f && i === fields.indexOf(f))
39
+ }
40
+
41
+
42
+ function routeIdField(config: any, getpath: any, entityName: string): string {
23
43
  let fallback = ''
24
44
 
25
45
  for (const opname of ['load', 'remove', 'update', 'patch']) {
@@ -117,8 +137,7 @@ class TestFeature extends BaseFeature {
117
137
  walk(entity, (k: any, v: any, _parent: any, path: any) => {
118
138
  if (2 === size(path)) {
119
139
  setprop(v, 'id', k)
120
- const idField = ownIdField(ctx.config, getpath, String(path[0]))
121
- if ('id' !== idField) {
140
+ for (const idField of ownIdFields(ctx.config, getpath, String(path[0]))) {
122
141
  setprop(v, idField, k)
123
142
  }
124
143
  }
@@ -255,9 +274,10 @@ class TestFeature extends BaseFeature {
255
274
  // the initial walk gives seed data (see ownIdField above) — without
256
275
  // it, only `id` is set, and a load by the entity's own key right
257
276
  // after create (recordKey !== 'id') finds nothing.
258
- const idField = ownIdField(ctx.config, struct.getpath, getprop(op, 'entity'))
259
- if ('id' !== idField && null == getprop(ent, idField)) {
260
- setprop(ent, idField, id)
277
+ for (const idField of ownIdFields(ctx.config, struct.getpath, getprop(op, 'entity'))) {
278
+ if (null == getprop(ent, idField)) {
279
+ setprop(ent, idField, id)
280
+ }
261
281
  }
262
282
 
263
283
  setprop(entmap, id, ent)
@@ -444,6 +464,7 @@ class TestFeature extends BaseFeature {
444
464
  export {
445
465
  TestFeature,
446
466
  ownIdField,
467
+ ownIdFields,
447
468
  mintId,
448
469
  }
449
470
 
@@ -2,9 +2,10 @@
2
2
  import { Context, Response } from '../types'
3
3
 
4
4
 
5
- // Several CDNs refuse a library's own agent, so outside a browser a request
6
- // without one carries the browser-shaped agent most other targets send.
7
- const DEFAULT_USER_AGENT = 'Mozilla/5.0 (compatible; ProjectNameSDK/1.0)'
5
+ // Outside a browser a request without an agent carries the project's
6
+ // (main.kit.http.useragent): browser-shaped unless the model says otherwise,
7
+ // as several CDNs refuse a library's own.
8
+ const DEFAULT_USER_AGENT = 'PROJECTUSERAGENT'
8
9
 
9
10
 
10
11
  function defaultUserAgent(fetchdef: Record<string, any>) {
@@ -26,7 +26,11 @@ async function resultBody(ctx: Context) {
26
26
  }
27
27
  catch (err: any) {
28
28
  if ('SyntaxError' !== err?.name) {
29
- throw err
29
+ if (true === ctx.ctrl?.signal?.aborted) {
30
+ throw err
31
+ }
32
+ result.err = result.err ?? bodyReadError(ctx, result, err)
33
+ return result
30
34
  }
31
35
  result.err = unreadableBody(ctx, {
32
36
  status: result.status, headers: result.headers, text: err.text,
@@ -40,6 +44,20 @@ async function resultBody(ctx: Context) {
40
44
  }
41
45
 
42
46
 
47
+ // A body that stopped before its end (a dropped connection, a deadline),
48
+ // named by the failure's kind alone: its message can carry a value.
49
+ function bodyReadError(ctx: Context, result: any, err: any): any {
50
+ const kind = [err?.name, err?.code ?? err?.cause?.code]
51
+ .filter((part: any) => 'string' === typeof part && '' !== part).join(' ')
52
+ const type = headerValue(result.headers, 'content-type')
53
+ const failed = ctx.error('response_body_read', 'response: body could not be read' +
54
+ ('' === kind ? '' : ' (' + kind + ')') + ' (HTTP ' + result.status + ', content-type ' +
55
+ (type || 'none') + ')')
56
+ Object.defineProperty(failed, 'cause', { value: err, writable: true, configurable: true })
57
+ return failed
58
+ }
59
+
60
+
43
61
  // A body that is not JSON. An HTTP failure keeps its own error, with the
44
62
  // response described; otherwise the code tells a wrong content type from
45
63
  // malformed JSON.
@@ -30,7 +30,8 @@ async function runLiveEntity(
30
30
  ...Object.values(entity.fields || {}).map((field: any) => field?.n).filter(Boolean)])
31
31
  const marks = new Map<string, { name: string, value: any }>()
32
32
  const idField = entity.id?.field || 'id'
33
- const declared = new Set(Object.values(entity.fields || {}).map((field: any) => field?.n))
33
+ const declared = new Set(Object.values(entity.fields || {})
34
+ .filter((field: any) => true !== field?.syn).map((field: any) => field?.n))
34
35
  const repeatsId = null != entity.id && (declared.has(idField) || declared.has('id'))
35
36
  const copy = (value: any) => JSON.parse(JSON.stringify(value ?? {}))
36
37
  const hasCreate = steps.some(step => step.o === 'create')
@@ -54,6 +54,7 @@ pub const TestFeature = struct {
54
54
 
55
55
  // Ensure entity ids are correct.
56
56
  _ = vs.walk(h.A(), entity, fixIds, null, vs.MAXDEPTH) catch {};
57
+ seedDeclaredIds(entity, ctx.config);
57
58
 
58
59
  const tw = h.A().create(TestWrap) catch unreachable;
59
60
  tw.* = .{ .entity = entity };
@@ -93,6 +94,25 @@ fn fixIds(_: Allocator, key: ?[]const u8, val: Value, _: Value, path: []const []
93
94
  return val;
94
95
  }
95
96
 
97
+ // The entity's declared identity field (`entity.id.field`), where it names
98
+ // one other than the `id` every record already carries.
99
+ fn declaredIdField(config: Value, entname: []const u8) ?[]const u8 {
100
+ const field = h.getpath(&.{ "entity", entname, "id", "field" }, config);
101
+ if (field != .string or field.string.len == 0 or std.mem.eql(u8, field.string, "id")) return null;
102
+ return field.string;
103
+ }
104
+
105
+ // fixIds has no closure to read the config from, so the declared field is a
106
+ // second pass over the same seed records.
107
+ fn seedDeclaredIds(entity: Value, config: Value) void {
108
+ for (h.items_vec(entity)) |ent| {
109
+ const id_field = declaredIdField(config, ent.k) orelse continue;
110
+ for (h.items_vec(ent.v)) |rec| {
111
+ if (rec.v == .object) h.setp(rec.v, id_field, h.vstr(rec.k));
112
+ }
113
+ }
114
+ }
115
+
96
116
  // The key a list's response transform
97
117
  // ["`$EACH`", "body", {"`$MERGE`": "`.<key>`"}] reads each item's record
98
118
  // under; null for any other transform.
@@ -351,6 +371,9 @@ fn test_fetch(entity: Value, ctx: *Context, _: []const u8, _: Value) err.E!Value
351
371
  if (ent == .object) {
352
372
  h.del_prop(ent, h.vstr("$body"));
353
373
  h.setp(ent, "id", id);
374
+ if (declaredIdField(ctx.config, op.entity)) |id_field| {
375
+ if (h.is_noval(h.getp(ent, id_field))) h.setp(ent, id_field, id);
376
+ }
354
377
  if (id == .string) h.setp(entmap, id.string, ent);
355
378
  h.del_prop(ent, h.vstr("$KEY"));
356
379
  return respond(ctx, 200, h.clone(ent), &.{});
@@ -3,7 +3,7 @@
3
3
  "package": 1
4
4
  },
5
5
  "name": "@voxgig/sdkgen",
6
- "version": "4.40.0",
6
+ "version": "4.41.0",
7
7
  "provides": {
8
8
  "target": [
9
9
  "c",
package/src/cmp/Main.ts CHANGED
@@ -5,6 +5,7 @@ import { cmp } from '../helpers/component'
5
5
  import { requirePath } from '../utility'
6
6
 
7
7
  import { ensureStdrep } from '../helpers/stdrep'
8
+ import { userAgent } from '../helpers/useragent'
8
9
 
9
10
  import {
10
11
  KIT
@@ -19,6 +20,10 @@ const Main = cmp(function Main(props: any) {
19
20
  // Root.ts cannot know about.
20
21
  const stdrep = ensureStdrep(ctx$)
21
22
 
23
+ // Per target, as `plain` names the target's own release; each Main_<lang>
24
+ // copies its templates with this map before the next target sets it.
25
+ stdrep.PROJECTUSERAGENT = userAgent(model, target.name)
26
+
22
27
  const Main_sdk = requirePath(ctx$, `cmp/${target.name}/Main_${target.name}`)
23
28
 
24
29
  Main_sdk['Main']({ model, target, stdrep })
@@ -38,6 +38,8 @@ type LiveFlowNeeds = {
38
38
  record?: string
39
39
  // match of the list that finds the existing record a create-less load reads
40
40
  discover?: Record<string, string>
41
+ // of that match, the bindings its route does not take, sent only as *_ENTID gives them
42
+ unrouted?: [string, string][]
41
43
  // why the flow cannot run against a live API at all
42
44
  blocked?: string
43
45
  }
@@ -65,9 +67,9 @@ function liveFlowNeeds(entity: any, flow: any): LiveFlowNeeds {
65
67
  if ('create' === step.o) bindings(step.m).forEach(([, k]) => keys.add(k))
66
68
  if ('update' === step.o) bindings(step.d).forEach(([, k]) => keys.add(k))
67
69
  if ('list' === step.o) {
68
- const args = liveStepArgs(entity, step, flow) || []
69
- fill(args)
70
- bindings(step.m).filter(([, k]) => !args.some((arg) => k === arg.key)).forEach(([, k]) => keys.add(k))
70
+ const args = liveStepArgs(entity, step, flow)
71
+ fill(args || [])
72
+ if (null == args) bindings(step.m).forEach(([, k]) => keys.add(k))
71
73
  }
72
74
  if ('load' === step.o && !liveRecordFound(entity, flow)) {
73
75
  const args = liveStepArgs(entity, step, flow)
@@ -91,6 +93,8 @@ function liveFlowNeeds(entity: any, flow: any): LiveFlowNeeds {
91
93
  else if (steps.some((step) => 'load' === step.o)) {
92
94
  if (null != list) {
93
95
  needs.discover = Object.fromEntries(bindings(list.m))
96
+ const unrouted = liveUnrouted(entity, list, flow)
97
+ if (0 < unrouted.length) needs.unrouted = unrouted
94
98
  }
95
99
  else if (unrouted) {
96
100
  needs.blocked = 'the flow loads a ' + entity.name + ' record it has no list to find'
@@ -236,6 +240,30 @@ function liveIdentity(entity: any, flow: any): LiveIdentity | undefined {
236
240
  }
237
241
 
238
242
 
243
+ function liveRepeatsId(entity: any): boolean {
244
+ if (null == entity?.id) return false
245
+ const declared = new Set(Object.values(entity.fields || {})
246
+ .filter((field: any) => true !== field?.syn).map((field: any) => field?.n))
247
+ return declared.has(entity.id.field || 'id') || declared.has('id')
248
+ }
249
+
250
+
251
+ // Only an identity the load sends, as the live entity harness: a listed or created record's, or a route's `id`.
252
+ function liveLoadComparesId(entity: any, flow: any): boolean {
253
+ if (!liveRepeatsId(entity)) return false
254
+ if (liveRecordFound(entity, flow)) return true
255
+ const load = flowSteps(flow).find((step: any) => 'load' === step.o)
256
+ return (liveStepArgs(entity, load, flow) || []).some((arg) => 'id' === arg.name)
257
+ }
258
+
259
+
260
+ function liveUnrouted(entity: any, step: any, flow?: any): [string, string][] {
261
+ if ('list' !== step?.o) return []
262
+ const args = liveStepArgs(entity, step, flow)
263
+ return null == args ? [] : bindings(step.m).filter(([, key]) => !args.some((arg) => key === arg.key))
264
+ }
265
+
266
+
239
267
  // The idmap key a live load of no known record reads its id from.
240
268
  function loadIdKey(entity: any, step: any): string {
241
269
  const binding = getMatchEntries(step).find(([name]) => 'id' === name)?.[1]
@@ -542,6 +570,7 @@ export type { LiveFlowNeeds, LiveIdentity, LiveExcluded, LiveGetOnly, LiveGate,
542
570
 
543
571
  export {
544
572
  liveStrict, liveStrictNote, liveFlowNeeds, liveIdentity,
573
+ liveRepeatsId, liveLoadComparesId, liveUnrouted,
545
574
  liveIdKey, liveExampleIds, liveQueryExamples, liveRouteExamples, liveStepExamples,
546
575
  flowLoadRoute, flowLoadReadsRecord, liveFlowRoutes,
547
576
  liveEntity, LIVE_GETONLY_ENV, liveFlowSteps, liveStepId, liveGetOnly, liveGetOnlyReason,
@@ -0,0 +1,41 @@
1
+ import { KIT } from '@voxgig/apidef'
2
+
3
+ import { SdkGenError } from '../utility'
4
+
5
+ import { packageVersion } from './packageMeta'
6
+
7
+
8
+ // The agent lands verbatim in a string literal in every target, so it may hold
9
+ // only what no target's literal escapes or interpolates: no quote, backslash,
10
+ // `$`, brace or C trigraph.
11
+ const AGENT_CHARS = /^[A-Za-z0-9!#%&()*+,\-./:;<=>?@[\]^_|~](?:[A-Za-z0-9 !#%&()*+,\-./:;<=>?@[\]^_|~]*[A-Za-z0-9!#%&()*+,\-./:;<=>?@[\]^_|~])?$/
12
+
13
+
14
+ // The User-Agent every target's transport sends when the caller sets none,
15
+ // from `main.kit.http.useragent`: unset keeps the browser shape several CDNs
16
+ // need, `plain` sends `<Name>SDK/<version>`, and any other value is sent as is.
17
+ function userAgent(model: any, target: string): string {
18
+ const Name = model?.const?.Name
19
+ const declared = model?.main?.[KIT]?.http?.useragent
20
+
21
+ if (null == declared || '' === declared) {
22
+ return 'Mozilla/5.0 (compatible; ' + Name + 'SDK/1.0)'
23
+ }
24
+
25
+ if ('plain' === declared) {
26
+ return Name + 'SDK/' + packageVersion(model, target)
27
+ }
28
+
29
+ const agent = String(declared)
30
+ if (!AGENT_CHARS.test(agent) || agent.includes('??')) {
31
+ throw new SdkGenError('main.kit.http.useragent: ' + JSON.stringify(agent) +
32
+ ' cannot be sent as written. Use printable ASCII without quotes, a backslash, `$`, ' +
33
+ 'braces or `??`, and no leading or trailing space.')
34
+ }
35
+ return agent
36
+ }
37
+
38
+
39
+ export {
40
+ userAgent,
41
+ }
package/src/sdkgen.ts CHANGED
@@ -83,6 +83,7 @@ import {
83
83
  import type { ExampleLang, LiteralLang } from './helpers/opExample'
84
84
  import {
85
85
  liveStrict, liveStrictNote, liveFlowNeeds, liveIdentity,
86
+ liveRepeatsId, liveLoadComparesId, liveUnrouted,
86
87
  liveIdKey, liveExampleIds, liveQueryExamples, liveRouteExamples, liveStepExamples,
87
88
  flowLoadRoute, flowLoadReadsRecord, liveFlowRoutes,
88
89
  liveEntity, LIVE_GETONLY_ENV, liveFlowSteps, liveStepId, liveGetOnly, liveGetOnlyReason,
@@ -1223,6 +1224,9 @@ export {
1223
1224
  liveStrictNote,
1224
1225
  liveFlowNeeds,
1225
1226
  liveIdentity,
1227
+ liveRepeatsId,
1228
+ liveLoadComparesId,
1229
+ liveUnrouted,
1226
1230
  liveIdKey,
1227
1231
  liveExampleIds,
1228
1232
  liveQueryExamples,
package/src/testkit.ts CHANGED
@@ -24,7 +24,7 @@ const SDKGEN_ROOT = Path.resolve(__dirname, '..')
24
24
 
25
25
  const PLACEHOLDERS = [
26
26
  'ProjectName', 'PROJECTNAME', 'PROJECTENV', 'PROJECTVERSION', 'PROJECTPUBLISHER',
27
- 'GOMODULE',
27
+ 'PROJECTUSERAGENT', 'GOMODULE',
28
28
  ]
29
29
 
30
30