@conduit-client/generator-ts 3.16.0 → 3.17.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.
package/dist/v1/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  import * as path from "path";
7
7
  import { resolve } from "path";
8
8
  import * as prettier from "prettier";
9
- import { toTypeScriptSafeIdentifier, JSONStringify, ok, err, stringIsVersion, stableJSONStringify } from "@conduit-client/utils";
9
+ import { toTypeScriptSafeIdentifier, JSONStringify, ok, err, capitalizeFirst, stringIsVersion, stableJSONStringify } from "@conduit-client/utils";
10
10
  import * as fs from "fs";
11
11
  import { readFileSync } from "fs";
12
12
  import { isAnyType, isRefType, isAllOfType, isOneOfType, isNotType, isObjectType as isObjectType$2, isDiscriminatedObjectType, isArrayType, isScalar, isEnumType as isEnumType$1, isNilType, isCanonicalStringType, isCanonicalNumberType, isBooleanType, typesEqual, isIdentifiableType as isIdentifiableType$1, canonicalizeType } from "@conduit-client/model/v1";
@@ -116,6 +116,10 @@ class Files {
116
116
  function fileService() {
117
117
  return new Files();
118
118
  }
119
+ function resolveRelativeImportPath(fromFile, toFile) {
120
+ const rel = path.relative(path.dirname(fromFile), toFile).replace(/\.(js|ts)$/, "");
121
+ return rel.startsWith(".") ? rel : "./" + rel;
122
+ }
119
123
  class Imports {
120
124
  /**
121
125
  * Constructor
@@ -156,10 +160,7 @@ class Imports {
156
160
  }
157
161
  let from;
158
162
  if ("filename" in ref) {
159
- from = path.relative(path.dirname(this.filename), ref.filename).replace(/\.(js|ts)$/, "");
160
- if (!from.startsWith(".")) {
161
- from = "./" + from;
162
- }
163
+ from = resolveRelativeImportPath(this.filename, ref.filename);
163
164
  } else {
164
165
  from = ref.module;
165
166
  }
@@ -224,11 +225,59 @@ class Imports {
224
225
  ].join("");
225
226
  }
226
227
  }
228
+ class ReExports {
229
+ constructor(filename) {
230
+ this.filename = filename;
231
+ this.entries = {};
232
+ this.typeAllPaths = /* @__PURE__ */ new Set();
233
+ }
234
+ resolveFrom(targetFilename) {
235
+ return resolveRelativeImportPath(this.filename, targetFilename);
236
+ }
237
+ /**
238
+ * Registers a value or type re-export: `export { symbol [as alias] } from "…";`
239
+ * @param targetFilename the filename of the target file to re-export
240
+ * @param exportedSymbol the symbol to export
241
+ * @param alias an optional alias for the symbol
242
+ * @param isType true if the re-export is for a type; false if it is for a value
243
+ */
244
+ add(targetFilename, exportedSymbol, alias, isType2) {
245
+ const from = this.resolveFrom(targetFilename);
246
+ if (!this.entries[from]) {
247
+ this.entries[from] = [];
248
+ }
249
+ this.entries[from].push({ exportedSymbol, alias, isType: isType2 });
250
+ }
251
+ /**
252
+ * Registers a wildcard type re-export: `export type * from "…";`
253
+ * @param targetFilename the filename of the target file to re-export
254
+ */
255
+ addTypeAll(targetFilename) {
256
+ this.typeAllPaths.add(this.resolveFrom(targetFilename));
257
+ }
258
+ toString() {
259
+ const statements = [];
260
+ for (const from of Object.keys(this.entries)) {
261
+ const entries = this.entries[from];
262
+ for (const isType2 of [false, true]) {
263
+ const clauses = entries.filter((e) => e.isType === isType2).map((e) => e.alias ? `${e.exportedSymbol} as ${e.alias}` : e.exportedSymbol).join(",");
264
+ if (clauses) {
265
+ statements.push(`export ${isType2 ? "type " : ""}{${clauses}} from '${from}';`);
266
+ }
267
+ }
268
+ }
269
+ for (const from of this.typeAllPaths) {
270
+ statements.push(`export type * from '${from}';`);
271
+ }
272
+ return statements.length > 0 ? statements.join("") : "";
273
+ }
274
+ }
227
275
  const _File = class _File {
228
276
  constructor(filename) {
229
277
  this.filename = filename;
230
278
  this.blocks = /* @__PURE__ */ new Map();
231
279
  this.template = [_File.DEFAULT_BLOCK];
280
+ this.reExports = new ReExports(this.filename);
232
281
  }
233
282
  /**
234
283
  * Appends new chunks of code to the default code block. Chunks are handled
@@ -256,6 +305,18 @@ const _File = class _File {
256
305
  code2.push(...chunks);
257
306
  return this;
258
307
  }
308
+ /** Registers a value re-export: `export { symbol [as alias] } from "…";` */
309
+ reExport(ref, alias) {
310
+ this.reExports.add(ref.filename, ref.exportedSymbol, alias, false);
311
+ }
312
+ /** Registers a type re-export: `export type { symbol [as alias] } from "…";` */
313
+ reExportType(ref, alias) {
314
+ this.reExports.add(ref.filename, ref.exportedSymbol, alias, true);
315
+ }
316
+ /** Registers a wildcard type re-export: `export type * from "…";` */
317
+ reExportTypeAll(targetFilename) {
318
+ this.reExports.addTypeAll(targetFilename);
319
+ }
259
320
  /**
260
321
  * Returns the code for this File, with ImportableReferences resolved.
261
322
  *
@@ -272,7 +333,7 @@ const _File = class _File {
272
333
  const executableCode = finalCode.toString({
273
334
  importStringifier: (ir) => imports.localSymbolFor(ir)
274
335
  });
275
- const code2 = imports.toString() + executableCode;
336
+ const code2 = imports.toString() + executableCode + this.reExports.toString();
276
337
  if (options.prettified) {
277
338
  try {
278
339
  return prettier.format(code2, { parser: "typescript" });
@@ -395,6 +456,9 @@ class TypeDefinitionService {
395
456
  this.fileFor = fileFor;
396
457
  this.cache = /* @__PURE__ */ new Map();
397
458
  }
459
+ get hasTypes() {
460
+ return this.cache.size > 0;
461
+ }
398
462
  build(type, useReferences = true) {
399
463
  return type ? this.buildTypeCode(type, {
400
464
  referenceFor: (t) => {
@@ -581,6 +645,7 @@ class TypeDefinitionService {
581
645
  this.cache.set(typename, result);
582
646
  const declaration = code`export type ${exportedSymbol}=${this.build(type, false)};`;
583
647
  this.services.file.build(filename).push(declaration);
648
+ this.services.file.build(TYPES_INDEX_PATH).reExportTypeAll(filename);
584
649
  return result;
585
650
  }
586
651
  }
@@ -588,7 +653,9 @@ function typeDefinitions(services, options = {}) {
588
653
  return new TypeDefinitionService(services, options.fileFor || SINGLE_FILE);
589
654
  }
590
655
  const FILE_PER_TYPE = (typename) => `types/${typename}.ts`;
591
- const SINGLE_FILE = (_) => "types/types.ts";
656
+ const TYPES_SINGLE_FILE_PATH = "types/types.ts";
657
+ const SINGLE_FILE = (_) => TYPES_SINGLE_FILE_PATH;
658
+ const TYPES_INDEX_PATH = "types/index.ts";
592
659
  const SCHEMA_FILE_PATH = "json-schema/index.ts";
593
660
  const COMBINED_SCHEMA = {
594
661
  filename: SCHEMA_FILE_PATH,
@@ -1453,6 +1520,9 @@ class TypeRegistryGenerator {
1453
1520
  this.cache = /* @__PURE__ */ new Map();
1454
1521
  this.invalidatableTypes = [];
1455
1522
  }
1523
+ get registeredTypes() {
1524
+ return this.cache;
1525
+ }
1456
1526
  get file() {
1457
1527
  return this.buildFile();
1458
1528
  }
@@ -2048,6 +2118,9 @@ class GraphQLTypeRegistryGenerator {
2048
2118
  setGraphQLTypeGenerator(generator) {
2049
2119
  this.graphqlTypeGenerator = generator;
2050
2120
  }
2121
+ get registeredTypes() {
2122
+ return this.cache;
2123
+ }
2051
2124
  get file() {
2052
2125
  if (this._file) {
2053
2126
  return this._file;
@@ -2176,9 +2249,9 @@ class CommandGenerator {
2176
2249
  }
2177
2250
  get commandConstructorArgs() {
2178
2251
  if (this.commandModel.invalidationConfigs.length > 0) {
2179
- return code`private config: Config, private typeRegistry: ${this.services.typeRegistryGenerator.build()}`;
2252
+ return code`public config: Config, public typeRegistry: ${this.services.typeRegistryGenerator.build()}`;
2180
2253
  }
2181
- return code`private config: Config`;
2254
+ return code`public config: Config`;
2182
2255
  }
2183
2256
  get exposeSubscribeAndRefresh() {
2184
2257
  if (this.commandModel.exposeRefresh && this.commandModel.cacheStrategy.type === "none") {
@@ -2259,7 +2332,8 @@ class CommandGenerator {
2259
2332
  file.pushTo(this.commandClassBodySymbol, ...this.classBody);
2260
2333
  const generatedClass = this.commandGeneratedClass;
2261
2334
  if (generatedClass) {
2262
- const constructorSignature = code`new (${this.getBuildCommandClassConstructorParams()}) => ${generatedClass}<${this.getBuildCommandClassReturnTypeGenerics()}>`;
2335
+ const fieldGenerics = this.getBuildCommandClassConstructorFieldGenerics();
2336
+ const constructorSignature = code`new (${this.getBuildCommandClassConstructorParams()}) => ${generatedClass}<${this.getBuildCommandClassReturnTypeGenerics()}, ${fieldGenerics}>`;
2263
2337
  file.pushTo(this.commandBuildCommandClassReturnTypeSymbol, constructorSignature);
2264
2338
  file.pushTo(
2265
2339
  this.commandBuildCommandClassReturnAssertionSymbol,
@@ -2296,6 +2370,16 @@ class CommandGenerator {
2296
2370
  getBuildCommandClassConstructorParams() {
2297
2371
  return code`${this.commandTypeConstructorArgs}, ...args: CommandConstructorParams`;
2298
2372
  }
2373
+ /**
2374
+ * Additional generic type args for the Generated declare class that surface
2375
+ * public constructor fields (config, typeRegistry, etc.) on InstanceType<>.
2376
+ */
2377
+ getBuildCommandClassConstructorFieldGenerics() {
2378
+ if (this.commandModel.invalidationConfigs.length > 0) {
2379
+ return code`Config, ${this.services.typeRegistryGenerator.build()}`;
2380
+ }
2381
+ return code`Config`;
2382
+ }
2299
2383
  buildCommandTypeGenerics() {
2300
2384
  var _a;
2301
2385
  const extraServicesCode = this.buildExtraServices();
@@ -2892,7 +2976,7 @@ class ImperativeAdapterBindingGenerator {
2892
2976
  };
2893
2977
  }
2894
2978
  buildDeclaration() {
2895
- return code`export let ${this.binding.identifier}:${this.bindingTypeCode};`;
2979
+ return code`let ${this.binding.identifier}:${this.bindingTypeCode};`;
2896
2980
  }
2897
2981
  get bindingTypeCode() {
2898
2982
  return code`(config:${this.commandConfigTypeImport}, options?: ${this.commandOptionsTypeImport})=>${this.returnShape}<${this.responseTypeCode}>`;
@@ -2943,7 +3027,7 @@ class ImperativeAdapterBindingGenerator {
2943
3027
  const requiresTypeRegistry = this.commandModel.cacheStrategy.type === "normalized" || this.commandModel.invalidationConfigs.length > 0;
2944
3028
  const typeRegistry = requiresTypeRegistry ? "typeRegistry," : "";
2945
3029
  return code`const ${this.localCommandConstructorName} = services.instrumentCommand(${this.classBuilderImport}(${servicesName}.${this.commandModel.serviceDependencies.baseClass.localName}), '${this.commandModel.className}');
2946
- ${this.binding.identifier}=${this.invoker}<Parameters<typeof ${this.binding.identifier}>,${this.responseTypeCode}>(
3030
+ const ${this.binding.identifier}=${this.invoker}<Parameters<typeof ${this.binding.identifier}>,${this.responseTypeCode}>(
2947
3031
  ({params,assertIsValid})=>{
2948
3032
  const config=params[0];
2949
3033
  const options=params[1];
@@ -2994,7 +3078,7 @@ class ImperativeAdapterLegacyBindingGenerator {
2994
3078
  );
2995
3079
  }
2996
3080
  buildDeclaration() {
2997
- return code`export let ${this.binding.identifier}:${LEGACY_IMPERATIVE_RETURN_SHAPE}<${this.commandConfigTypeImport},${this.responseTypeCode}>;`;
3081
+ return code`let ${this.binding.identifier}:${LEGACY_IMPERATIVE_RETURN_SHAPE}<${this.commandConfigTypeImport},${this.responseTypeCode}>;`;
2998
3082
  }
2999
3083
  get localCommandConstructorName() {
3000
3084
  return `${this.binding.identifier}_ctor`;
@@ -3005,7 +3089,7 @@ class ImperativeAdapterLegacyBindingGenerator {
3005
3089
  buildInvocation(servicesName = "services") {
3006
3090
  const requiresTypeRegistry = this.commandModel.cacheStrategy.type === "normalized" || this.commandModel.invalidationConfigs.length > 0;
3007
3091
  return code`const ${this.localCommandConstructorName} = services.instrumentCommand(${this.classBuilderImport}(${servicesName}.${this.commandModel.serviceDependencies.baseClass.localName}), '${this.commandModel.className}');
3008
- ${this.binding.identifier}=services.${LEGACY_IMPERATIVE_BINDINGS}.bind<${this.commandConfigTypeImport},${this.responseTypeCode}>(
3092
+ const ${this.binding.identifier}=services.${LEGACY_IMPERATIVE_BINDINGS}.bind<${this.commandConfigTypeImport},${this.responseTypeCode}>(
3009
3093
  ({config,assertIsValid})=>{
3010
3094
  const _:<T>(data:unknown,_:${JSON_SCHEMA_TYPE$3})=>asserts data is T=assertIsValid;
3011
3095
  _<${this.commandConfigTypeImport}>(config,${this.commandConfigSchemaImport});
@@ -3054,7 +3138,7 @@ class GraphQLImperativeAdapterLegacyBindingGenerator {
3054
3138
  );
3055
3139
  }
3056
3140
  buildDeclaration() {
3057
- return code`export let ${this.binding.identifier}:${LEGACY_GRAPHQL_IMPERATIVE_RETURN_SHAPE}<${this.commandConfigTypeImport}>;`;
3141
+ return code`let ${this.binding.identifier}:${LEGACY_GRAPHQL_IMPERATIVE_RETURN_SHAPE}<${this.commandConfigTypeImport}>;`;
3058
3142
  }
3059
3143
  get localCommandConstructorName() {
3060
3144
  return `${this.binding.identifier}_ctor`;
@@ -3064,7 +3148,7 @@ class GraphQLImperativeAdapterLegacyBindingGenerator {
3064
3148
  }
3065
3149
  buildInvocation(servicesName = "services") {
3066
3150
  return code`const ${this.localCommandConstructorName} = services.instrumentCommand(${this.classBuilderImport}(${servicesName}.${this.commandModel.serviceDependencies.baseClass.localName}), '${this.commandModel.className}');
3067
- ${this.binding.identifier}=services.${GRAPHQL_LEGACY_IMPERATIVE_BINDINGS}.bind<${this.commandConfigTypeImport}>(
3151
+ const ${this.binding.identifier}=services.${GRAPHQL_LEGACY_IMPERATIVE_BINDINGS}.bind<${this.commandConfigTypeImport}>(
3068
3152
  ({config,assertIsValid})=>{
3069
3153
  const _:<T>(data:unknown,_:${JSON_SCHEMA_TYPE$2})=>asserts data is T=assertIsValid;
3070
3154
  _<${this.commandConfigTypeImport}>(config,${this.commandConfigSchemaImport});
@@ -3111,7 +3195,7 @@ class WireAdapterBindingGenerator {
3111
3195
  );
3112
3196
  }
3113
3197
  buildDeclaration() {
3114
- return code`export let ${this.binding.identifier}:${WIRE_ADAPTER_CONSTRUCTOR$1}<${this.commandConfigTypeImport}, ${WIRE_ADAPTER_RESULT}<${this.responseTypeCode}>, {}>;`;
3198
+ return code`let ${this.binding.identifier}:${WIRE_ADAPTER_CONSTRUCTOR$1}<${this.commandConfigTypeImport}, ${WIRE_ADAPTER_RESULT}<${this.responseTypeCode}>, {}>;`;
3115
3199
  }
3116
3200
  get localCommandConstructorName() {
3117
3201
  return `${this.binding.identifier}_ctor`;
@@ -3121,7 +3205,7 @@ class WireAdapterBindingGenerator {
3121
3205
  }
3122
3206
  buildInvocation(servicesName = "services") {
3123
3207
  return code`const ${this.localCommandConstructorName} = services.instrumentCommand(${this.classBuilderImport}(${servicesName}.${this.commandModel.serviceDependencies.baseClass.localName}), '${this.commandModel.className}');
3124
- ${this.binding.identifier}=services.${LWC_WIRE_BINDINGS}.bind<${this.responseTypeCode}, ${this.commandConfigTypeImport}>(
3208
+ const ${this.binding.identifier}=services.${LWC_WIRE_BINDINGS}.bind<${this.responseTypeCode}, ${this.commandConfigTypeImport}>(
3125
3209
  (config: ${this.commandConfigTypeImport}) => new ${this.localCommandConstructorName}(config!,${this.getTypeRegistryArgument()}${servicesName}),${this.commandConfigSchemaImport},${this.commandModel.exposeRefresh});`;
3126
3210
  }
3127
3211
  getTypeRegistryArgument() {
@@ -3174,7 +3258,7 @@ class GraphQLImperativeAdapterBindingGenerator {
3174
3258
  };
3175
3259
  }
3176
3260
  buildDeclaration() {
3177
- return code`export let ${this.binding.identifier}:${this.bindingTypeCode};`;
3261
+ return code`let ${this.binding.identifier}:${this.bindingTypeCode};`;
3178
3262
  }
3179
3263
  get bindingTypeCode() {
3180
3264
  return code`(config:${this.commandConfigTypeImport}, options?: ${this.commandOptionsTypeImport})=>${this.returnShape}`;
@@ -3202,7 +3286,7 @@ class GraphQLImperativeAdapterBindingGenerator {
3202
3286
  }
3203
3287
  buildInvocation(servicesName = "services") {
3204
3288
  return code`const ${this.localCommandConstructorName} = services.instrumentCommand(${this.classBuilderImport}(${servicesName}.${this.commandModel.serviceDependencies.baseClass.localName}), '${this.commandModel.className}');
3205
- ${this.binding.identifier}=${this.invoker}<Parameters<typeof ${this.binding.identifier}>>(
3289
+ const ${this.binding.identifier}=${this.invoker}<Parameters<typeof ${this.binding.identifier}>>(
3206
3290
  ({params,assertIsValid})=>{
3207
3291
  const config=params[0];
3208
3292
  const options=params[1];
@@ -3253,7 +3337,7 @@ class GraphQLWireAdapterBindingGenerator {
3253
3337
  );
3254
3338
  }
3255
3339
  buildDeclaration() {
3256
- return code`export let ${this.binding.identifier}:${WIRE_ADAPTER_CONSTRUCTOR}<${this.commandConfigTypeImport}, ${GRAPHQL_WIRE_ADAPTER_RESULT}, {}>;`;
3340
+ return code`let ${this.binding.identifier}:${WIRE_ADAPTER_CONSTRUCTOR}<${this.commandConfigTypeImport}, ${GRAPHQL_WIRE_ADAPTER_RESULT}, {}>;`;
3257
3341
  }
3258
3342
  get localCommandConstructorName() {
3259
3343
  return `${this.binding.identifier}_ctor`;
@@ -3263,7 +3347,7 @@ class GraphQLWireAdapterBindingGenerator {
3263
3347
  }
3264
3348
  buildInvocation(servicesName = "services") {
3265
3349
  return code`const ${this.localCommandConstructorName} = services.instrumentCommand(${this.classBuilderImport}(${servicesName}.${this.commandModel.serviceDependencies.baseClass.localName}), '${this.commandModel.className}');
3266
- ${this.binding.identifier}=services.${GRAPHQL_WIRE_BINDINGS}.bind<${this.commandConfigTypeImport}>((config: ${this.commandConfigTypeImport}) => new ${this.localCommandConstructorName}(config,${QUERY_TYPE_VARIABLE_NAME},${servicesName}), ${this.commandConfigSchemaImport}, ${this.commandModel.exposeRefresh});`;
3350
+ const ${this.binding.identifier}=services.${GRAPHQL_WIRE_BINDINGS}.bind<${this.commandConfigTypeImport}>((config: ${this.commandConfigTypeImport}) => new ${this.localCommandConstructorName}(config,${QUERY_TYPE_VARIABLE_NAME},${servicesName}), ${this.commandConfigSchemaImport}, ${this.commandModel.exposeRefresh});`;
3267
3351
  }
3268
3352
  }
3269
3353
  const GRAPHQL_MUTATION_RESULT = {
@@ -3309,7 +3393,7 @@ class GraphQLMutationAdapterBindingGenerator {
3309
3393
  };
3310
3394
  }
3311
3395
  buildDeclaration() {
3312
- return code`export let ${this.binding.identifier}:${this.bindingTypeCode};`;
3396
+ return code`let ${this.binding.identifier}:${this.bindingTypeCode};`;
3313
3397
  }
3314
3398
  get bindingTypeCode() {
3315
3399
  return code`(config:${this.commandConfigTypeImport}, options?: ${this.commandOptionsTypeImport})=>${this.returnShape}`;
@@ -3328,7 +3412,7 @@ class GraphQLMutationAdapterBindingGenerator {
3328
3412
  }
3329
3413
  buildInvocation(servicesName = "services") {
3330
3414
  return code`const ${this.localCommandConstructorName} = services.instrumentCommand(${this.classBuilderImport}(${servicesName}.${this.commandModel.serviceDependencies.baseClass.localName}), '${this.commandModel.className}');
3331
- ${this.binding.identifier}=${this.invoker}<Parameters<typeof ${this.binding.identifier}>>(
3415
+ const ${this.binding.identifier}=${this.invoker}<Parameters<typeof ${this.binding.identifier}>>(
3332
3416
  ({params,assertIsValid})=>{
3333
3417
  const config=params[0];
3334
3418
  const options=params[1];
@@ -3529,7 +3613,12 @@ class BindingsGenerator {
3529
3613
  this.typeInvalidationDefinitionSymbol = Symbol.for("typeInvalidationDefinition");
3530
3614
  this.rootTypesInstantiationSymbol = Symbol.for("rootTypesInstantiation");
3531
3615
  this.exportsTypeMembersSymbol = Symbol.for("exportsTypeMembers");
3532
- this.exportsAssignmentsSymbol = Symbol.for("exportsAssignments");
3616
+ this.initGeneratedBindingsExtraParamsSymbol = Symbol.for(
3617
+ "initGeneratedBindingsExtraParams"
3618
+ );
3619
+ this.returnStatementSymbol = Symbol.for("returnStatement");
3620
+ this.wrapperDeclarationsSymbol = Symbol.for("wrapperDeclarations");
3621
+ this.wrapperAssignmentsSymbol = Symbol.for("wrapperAssignments");
3533
3622
  }
3534
3623
  /**
3535
3624
  * Template for generating the binding structure in the output file.
@@ -3551,25 +3640,31 @@ class BindingsGenerator {
3551
3640
  code`export type BindingsExports = {`,
3552
3641
  this.exportsTypeMembersSymbol,
3553
3642
  code`};`,
3554
- code`export const exports = {} as BindingsExports;`,
3555
3643
  code`export const serviceRequirements=`,
3556
3644
  this.serviceRequirementsSymbol,
3557
3645
  code` as const;`,
3558
- code`export function initGeneratedBindings(services: ${REQUESTED_SERVICES_TYPE}<typeof serviceRequirements>, exports: BindingsExports) {`,
3646
+ code`export function initGeneratedBindings<S extends ${REQUESTED_SERVICES_TYPE}<typeof serviceRequirements>>(services: S`,
3647
+ this.initGeneratedBindingsExtraParamsSymbol,
3648
+ code`): BindingsExports {`,
3559
3649
  this.typeRegistryInstantiationSymbol,
3560
3650
  this.rootTypesInstantiationSymbol,
3561
3651
  this.typeInvalidationDefinitionSymbol,
3562
3652
  this.adapterInvocationSymbol,
3563
- this.exportsAssignmentsSymbol,
3653
+ this.returnStatementSymbol,
3564
3654
  code`}`
3565
3655
  ];
3566
3656
  }
3567
3657
  get wrapperTemplate() {
3568
3658
  return [
3569
- code`export * from './bindings-core';`,
3570
- code`import { exports, serviceRequirements, initGeneratedBindings } from './bindings-core';`,
3571
- code`export const ready = ${INIT_BINDINGS_EXPORTS}({ serviceRequirements, exports, init: initGeneratedBindings }).ready;`,
3572
- code`export default exports;`
3659
+ code`import { serviceRequirements, initGeneratedBindings, type BindingsExports } from './bindings-core';`,
3660
+ this.wrapperDeclarationsSymbol,
3661
+ code`${INIT_BINDINGS_EXPORTS}({`,
3662
+ code`serviceRequirements,`,
3663
+ code`init: (services) => {`,
3664
+ code`const result = initGeneratedBindings(services);`,
3665
+ this.wrapperAssignmentsSymbol,
3666
+ code`},`,
3667
+ code`});`
3573
3668
  ];
3574
3669
  }
3575
3670
  get serviceDependencies() {
@@ -3703,11 +3798,18 @@ class BindingsGenerator {
3703
3798
  const wrapperFilename = "artifacts/bindings.ts";
3704
3799
  const wrapperFile = this.services.file.build(wrapperFilename);
3705
3800
  wrapperFile.template = this.wrapperTemplate;
3801
+ let typeRegistryRef;
3802
+ let graphqlTypeRegistryRef;
3706
3803
  if (this.hasRESTNormalization) {
3707
3804
  const { typeRegistryGenerator } = this.services;
3805
+ typeRegistryRef = typeRegistryGenerator.build();
3806
+ coreFile.pushTo(
3807
+ this.initGeneratedBindingsExtraParamsSymbol,
3808
+ code`, typeRegistry?: ${typeRegistryRef}`
3809
+ );
3708
3810
  coreFile.pushTo(
3709
3811
  this.typeRegistryInstantiationSymbol,
3710
- code`const typeRegistry = new ${typeRegistryGenerator.build()}(services);`
3812
+ code`typeRegistry ??= new ${typeRegistryRef}(services);`
3711
3813
  );
3712
3814
  const { declarations, definitions } = this.buildTypeInvalidation(
3713
3815
  typeRegistryGenerator.invalidatableTypes
@@ -3717,7 +3819,11 @@ class BindingsGenerator {
3717
3819
  }
3718
3820
  if (this.hasGraphQL) {
3719
3821
  const { graphqlTypeRegistryGenerator } = this.services;
3720
- const graphqlTypeRegistry = graphqlTypeRegistryGenerator.build();
3822
+ graphqlTypeRegistryRef = graphqlTypeRegistryGenerator.build();
3823
+ coreFile.pushTo(
3824
+ this.initGeneratedBindingsExtraParamsSymbol,
3825
+ code`, graphqlTypeRegistry?: ${graphqlTypeRegistryRef}`
3826
+ );
3721
3827
  const firstGraphQLCommand = this.commandModels.find(
3722
3828
  (model) => model.operationType === "graphql"
3723
3829
  );
@@ -3737,7 +3843,7 @@ class BindingsGenerator {
3737
3843
  }) : void 0;
3738
3844
  coreFile.pushTo(
3739
3845
  this.rootTypesInstantiationSymbol,
3740
- code`const graphqlTypeRegistry = new ${graphqlTypeRegistry}(services);
3846
+ code`graphqlTypeRegistry ??= new ${graphqlTypeRegistryRef}(services);
3741
3847
  const ${QUERY_TYPE_VARIABLE_NAME} = graphqlTypeRegistry.${queryType.propertyName};`
3742
3848
  );
3743
3849
  if (mutationType) {
@@ -3795,22 +3901,132 @@ class BindingsGenerator {
3795
3901
  )
3796
3902
  );
3797
3903
  coreFile.pushTo(
3798
- this.exportsAssignmentsSymbol,
3904
+ this.returnStatementSymbol,
3905
+ code`return {${Code.join(
3906
+ uniqueExportNames.map((name) => code`${name},`),
3907
+ " "
3908
+ )}};`
3909
+ );
3910
+ wrapperFile.pushTo(
3911
+ this.wrapperDeclarationsSymbol,
3799
3912
  Code.join(
3800
- uniqueExportNames.map((name) => code`exports.${code`${name}`} = ${code`${name}`};`),
3913
+ uniqueExportNames.map(
3914
+ (name) => code`export let ${code`${name}`}: BindingsExports['${name}'];`
3915
+ ),
3801
3916
  "\n"
3802
3917
  )
3803
3918
  );
3919
+ wrapperFile.pushTo(
3920
+ this.wrapperAssignmentsSymbol,
3921
+ Code.join(
3922
+ uniqueExportNames.map((name) => code`${code`${name}`} = result.${code`${name}`};`),
3923
+ "\n"
3924
+ )
3925
+ );
3926
+ this.buildReExports(coreFile, { typeRegistryRef, graphqlTypeRegistryRef });
3927
+ }
3928
+ /**
3929
+ * Emits re-export statements from bindings-core so that custom bindings modules
3930
+ * can import generated internals from a single stable entry point.
3931
+ */
3932
+ buildReExports(coreFile, refs) {
3933
+ this.buildCommandReExports(coreFile);
3934
+ this.buildTypeRegistryReExports(coreFile, refs);
3935
+ this.buildTypeRepositoryReExports(coreFile);
3936
+ this.buildEntityTypeReExports(coreFile);
3937
+ }
3938
+ /**
3939
+ * Re-exports per-command symbols: `buildCommandClass` (aliased to `build<Capitalized>CommandClass`),
3940
+ * `CONFIG_SCHEMA` (aliased to `<UPPER>_CONFIG_SCHEMA`), and the `Config` type
3941
+ * (aliased to `<Capitalized>Config`).
3942
+ *
3943
+ * The CONFIG_SCHEMA alias uses a simple uppercase of the commandName (e.g. `getItemsByIds`
3944
+ * becomes `GETITEMSBYIDS_CONFIG_SCHEMA`) to avoid ambiguity from word-boundary heuristics.
3945
+ */
3946
+ buildCommandReExports(coreFile) {
3947
+ for (const model of this.commandModels) {
3948
+ const capitalized = capitalizeFirst(model.commandName);
3949
+ const upper = model.commandName.toUpperCase();
3950
+ const buildRef = {
3951
+ filename: model.filePath,
3952
+ exportedSymbol: "buildCommandClass",
3953
+ isType: false
3954
+ };
3955
+ const schemaRef = {
3956
+ filename: model.filePath,
3957
+ exportedSymbol: "CONFIG_SCHEMA",
3958
+ isType: false
3959
+ };
3960
+ const configRef = {
3961
+ filename: model.filePath,
3962
+ exportedSymbol: "Config",
3963
+ isType: true
3964
+ };
3965
+ coreFile.reExport(buildRef, `build${capitalized}CommandClass`);
3966
+ coreFile.reExport(schemaRef, `${upper}_CONFIG_SCHEMA`);
3967
+ coreFile.reExportType(configRef, `${capitalized}Config`);
3968
+ }
3969
+ }
3970
+ /**
3971
+ * Re-exports the `TypeRegistry` (REST) and/or `GraphQLTypeRegistry` (GraphQL) classes
3972
+ * so custom bindings can construct or extend them.
3973
+ */
3974
+ buildTypeRegistryReExports(coreFile, refs) {
3975
+ if (refs.typeRegistryRef) {
3976
+ coreFile.reExport(refs.typeRegistryRef);
3977
+ }
3978
+ if (refs.graphqlTypeRegistryRef) {
3979
+ coreFile.reExport(refs.graphqlTypeRegistryRef);
3980
+ }
3981
+ }
3982
+ /**
3983
+ * Re-exports individual generated type repository classes (e.g. `RecordRepresentationRepository`)
3984
+ * so custom bindings can extend or reference them without importing from internal paths.
3985
+ *
3986
+ * If both REST and GraphQL registries produce a repository with the same exported symbol,
3987
+ * the GraphQL version is aliased with a `GraphQL_` prefix to avoid collisions.
3988
+ */
3989
+ buildTypeRepositoryReExports(coreFile) {
3990
+ const restRepos = this.hasRESTNormalization ? [...this.services.typeRegistryGenerator.registeredTypes.values()] : [];
3991
+ const graphqlRepos = this.hasGraphQL ? [...this.services.graphqlTypeRegistryGenerator.registeredTypes.values()] : [];
3992
+ const restNames = new Set(restRepos.map((r) => r.typeClassRef.exportedSymbol));
3993
+ const graphqlNames = new Set(graphqlRepos.map((r) => r.typeClassRef.exportedSymbol));
3994
+ const conflicts = new Set([...restNames].filter((n) => graphqlNames.has(n)));
3995
+ for (const { typeClassRef } of restRepos) {
3996
+ if (!("filename" in typeClassRef) || !typeClassRef.exportedSymbol) continue;
3997
+ coreFile.reExport(typeClassRef);
3998
+ }
3999
+ for (const { typeClassRef } of graphqlRepos) {
4000
+ if (!("filename" in typeClassRef) || !typeClassRef.exportedSymbol) continue;
4001
+ const alias = conflicts.has(typeClassRef.exportedSymbol) ? `GraphQL_${typeClassRef.exportedSymbol}` : void 0;
4002
+ coreFile.reExport(
4003
+ typeClassRef,
4004
+ alias
4005
+ );
4006
+ }
4007
+ }
4008
+ /**
4009
+ * Re-exports all generated entity types so custom bindings can reference
4010
+ * domain models (e.g. `api_Item`) without importing from internal paths.
4011
+ *
4012
+ * The barrel `types/index.ts` is built incrementally by TypeDefinitionService
4013
+ * as types are generated; we just re-export it from bindings-core.
4014
+ * GraphQL-only specs produce no REST types so the barrel is never created.
4015
+ */
4016
+ buildEntityTypeReExports(coreFile) {
4017
+ if (this.services.typeDefinition.hasTypes) {
4018
+ coreFile.reExportTypeAll(TYPES_INDEX_PATH);
4019
+ }
3804
4020
  }
3805
4021
  buildTypeInvalidation(typesWithNormalization) {
3806
4022
  return typesWithNormalization.reduce(
3807
4023
  (acc, { propertyName, typeClassRef }) => {
3808
4024
  const invalidateFunctionName = code`invalidate${toTypeScriptSafeIdentifier(propertyName)}`;
3809
4025
  acc.declarations.push(
3810
- code`export let ${invalidateFunctionName}: ${typeClassRef}["invalidate"];`
4026
+ code`let ${invalidateFunctionName}: ${typeClassRef}["invalidate"];`
3811
4027
  );
3812
4028
  acc.definitions.push(
3813
- code`${invalidateFunctionName} = (configs) => typeRegistry.${toTypeScriptSafeIdentifier(propertyName)}.invalidate(configs);`
4029
+ code`const ${invalidateFunctionName}: ${typeClassRef}["invalidate"] = (configs) => typeRegistry.${toTypeScriptSafeIdentifier(propertyName)}.invalidate(configs);`
3814
4030
  );
3815
4031
  return acc;
3816
4032
  },
@@ -4110,7 +4326,10 @@ function NormalizedCacheControlGeneratorMixin(Base, commandBaseClass, commandGen
4110
4326
  return code`config: Config, typeRegistry: ${this.services.typeRegistryGenerator.build()}`;
4111
4327
  }
4112
4328
  get commandConstructorArgs() {
4113
- return code`private config: Config, private typeRegistry: ${this.services.typeRegistryGenerator.build()}`;
4329
+ return code`public config: Config, public typeRegistry: ${this.services.typeRegistryGenerator.build()}`;
4330
+ }
4331
+ getBuildCommandClassConstructorFieldGenerics() {
4332
+ return code`Config, ${this.services.typeRegistryGenerator.build()}`;
4114
4333
  }
4115
4334
  buildCommandTypeGenerics() {
4116
4335
  const extraServicesCode = this.buildExtraServices();
@@ -4310,8 +4529,11 @@ function GraphQLNormalizedCacheControlGeneratorMixin(Base, commandBaseClass, com
4310
4529
  getBuildCommandClassReturnTypeGenerics() {
4311
4530
  return this.buildCommandTypeGenerics();
4312
4531
  }
4532
+ getBuildCommandClassConstructorFieldGenerics() {
4533
+ return code`CommandConfig, DocumentRootType, ${NAMED_CACHE_CONTROLLER_SERVICE} & ${NAMED_PUBSUB_SERVICE$3} & ${this.buildNetworkServicesCode()} & object`;
4534
+ }
4313
4535
  get commandConstructorArgs() {
4314
- return code`protected config: CommandConfig, protected documentRootType: DocumentRootType, protected services: ${NAMED_CACHE_CONTROLLER_SERVICE} & ${NAMED_PUBSUB_SERVICE$3} & ${this.buildNetworkServicesCode()} & object`;
4536
+ return code`public config: CommandConfig, public documentRootType: DocumentRootType, public services: ${NAMED_CACHE_CONTROLLER_SERVICE} & ${NAMED_PUBSUB_SERVICE$3} & ${this.buildNetworkServicesCode()} & object`;
4315
4537
  }
4316
4538
  get commandConstructorBody() {
4317
4539
  return code`super(config, documentRootType, services);`;
@@ -13653,6 +13875,8 @@ export {
13653
13875
  NormalizedTypeGenerator,
13654
13876
  SINGLE_FILE,
13655
13877
  SINGLE_FILE_NORMALIZED,
13878
+ TYPES_INDEX_PATH,
13879
+ TYPES_SINGLE_FILE_PATH,
13656
13880
  TYPE_REGISTRY_FILE,
13657
13881
  TypeDefinitionService,
13658
13882
  TypeRegistryGenerator,