@itwin/presentation-core-interop 0.1.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 (129) hide show
  1. package/.mocharc.json +8 -0
  2. package/.nycrc +14 -0
  3. package/CHANGELOG.md +12 -0
  4. package/LICENSE.md +9 -0
  5. package/README.md +99 -0
  6. package/api/presentation-core-interop.api.md +38 -0
  7. package/api/presentation-core-interop.exports.csv +7 -0
  8. package/eslint.config.js +14 -0
  9. package/lib/cjs/core-interop/Formatting.d.ts +44 -0
  10. package/lib/cjs/core-interop/Formatting.d.ts.map +1 -0
  11. package/lib/cjs/core-interop/Formatting.js +147 -0
  12. package/lib/cjs/core-interop/Formatting.js.map +1 -0
  13. package/lib/cjs/core-interop/Logging.d.ts +32 -0
  14. package/lib/cjs/core-interop/Logging.d.ts.map +1 -0
  15. package/lib/cjs/core-interop/Logging.js +47 -0
  16. package/lib/cjs/core-interop/Logging.js.map +1 -0
  17. package/lib/cjs/core-interop/Metadata.d.ts +28 -0
  18. package/lib/cjs/core-interop/Metadata.d.ts.map +1 -0
  19. package/lib/cjs/core-interop/Metadata.js +51 -0
  20. package/lib/cjs/core-interop/Metadata.js.map +1 -0
  21. package/lib/cjs/core-interop/MetadataInternal.d.ts +9 -0
  22. package/lib/cjs/core-interop/MetadataInternal.d.ts.map +1 -0
  23. package/lib/cjs/core-interop/MetadataInternal.js +370 -0
  24. package/lib/cjs/core-interop/MetadataInternal.js.map +1 -0
  25. package/lib/cjs/core-interop/QueryExecutor.d.ts +31 -0
  26. package/lib/cjs/core-interop/QueryExecutor.d.ts.map +1 -0
  27. package/lib/cjs/core-interop/QueryExecutor.js +112 -0
  28. package/lib/cjs/core-interop/QueryExecutor.js.map +1 -0
  29. package/lib/cjs/core-interop/Transactions.d.ts +70 -0
  30. package/lib/cjs/core-interop/Transactions.d.ts.map +1 -0
  31. package/lib/cjs/core-interop/Transactions.js +61 -0
  32. package/lib/cjs/core-interop/Transactions.js.map +1 -0
  33. package/lib/cjs/presentation-core-interop.d.ts +6 -0
  34. package/lib/cjs/presentation-core-interop.d.ts.map +1 -0
  35. package/lib/cjs/presentation-core-interop.js +18 -0
  36. package/lib/cjs/presentation-core-interop.js.map +1 -0
  37. package/lib/cjs/test/Formatting.test.d.ts +2 -0
  38. package/lib/cjs/test/Formatting.test.d.ts.map +1 -0
  39. package/lib/cjs/test/Formatting.test.js +282 -0
  40. package/lib/cjs/test/Formatting.test.js.map +1 -0
  41. package/lib/cjs/test/Logging.test.d.ts +2 -0
  42. package/lib/cjs/test/Logging.test.d.ts.map +1 -0
  43. package/lib/cjs/test/Logging.test.js +57 -0
  44. package/lib/cjs/test/Logging.test.js.map +1 -0
  45. package/lib/cjs/test/Metadata.test.d.ts +2 -0
  46. package/lib/cjs/test/Metadata.test.d.ts.map +1 -0
  47. package/lib/cjs/test/Metadata.test.js +676 -0
  48. package/lib/cjs/test/Metadata.test.js.map +1 -0
  49. package/lib/cjs/test/QueryExecutor.test.d.ts +2 -0
  50. package/lib/cjs/test/QueryExecutor.test.d.ts.map +1 -0
  51. package/lib/cjs/test/QueryExecutor.test.js +178 -0
  52. package/lib/cjs/test/QueryExecutor.test.js.map +1 -0
  53. package/lib/cjs/test/Transactions.test.d.ts +2 -0
  54. package/lib/cjs/test/Transactions.test.d.ts.map +1 -0
  55. package/lib/cjs/test/Transactions.test.js +73 -0
  56. package/lib/cjs/test/Transactions.test.js.map +1 -0
  57. package/lib/cjs/test/index.d.ts +2 -0
  58. package/lib/cjs/test/index.d.ts.map +1 -0
  59. package/lib/cjs/test/index.js +43 -0
  60. package/lib/cjs/test/index.js.map +1 -0
  61. package/lib/esm/core-interop/Formatting.d.ts +44 -0
  62. package/lib/esm/core-interop/Formatting.d.ts.map +1 -0
  63. package/lib/esm/core-interop/Formatting.js +143 -0
  64. package/lib/esm/core-interop/Formatting.js.map +1 -0
  65. package/lib/esm/core-interop/Logging.d.ts +32 -0
  66. package/lib/esm/core-interop/Logging.d.ts.map +1 -0
  67. package/lib/esm/core-interop/Logging.js +43 -0
  68. package/lib/esm/core-interop/Logging.js.map +1 -0
  69. package/lib/esm/core-interop/Metadata.d.ts +28 -0
  70. package/lib/esm/core-interop/Metadata.d.ts.map +1 -0
  71. package/lib/esm/core-interop/Metadata.js +47 -0
  72. package/lib/esm/core-interop/Metadata.js.map +1 -0
  73. package/lib/esm/core-interop/MetadataInternal.d.ts +9 -0
  74. package/lib/esm/core-interop/MetadataInternal.d.ts.map +1 -0
  75. package/lib/esm/core-interop/MetadataInternal.js +364 -0
  76. package/lib/esm/core-interop/MetadataInternal.js.map +1 -0
  77. package/lib/esm/core-interop/QueryExecutor.d.ts +31 -0
  78. package/lib/esm/core-interop/QueryExecutor.d.ts.map +1 -0
  79. package/lib/esm/core-interop/QueryExecutor.js +108 -0
  80. package/lib/esm/core-interop/QueryExecutor.js.map +1 -0
  81. package/lib/esm/core-interop/Transactions.d.ts +70 -0
  82. package/lib/esm/core-interop/Transactions.d.ts.map +1 -0
  83. package/lib/esm/core-interop/Transactions.js +57 -0
  84. package/lib/esm/core-interop/Transactions.js.map +1 -0
  85. package/lib/esm/presentation-core-interop.d.ts +6 -0
  86. package/lib/esm/presentation-core-interop.d.ts.map +1 -0
  87. package/lib/esm/presentation-core-interop.js +10 -0
  88. package/lib/esm/presentation-core-interop.js.map +1 -0
  89. package/lib/esm/test/Formatting.test.d.ts +2 -0
  90. package/lib/esm/test/Formatting.test.d.ts.map +1 -0
  91. package/lib/esm/test/Formatting.test.js +277 -0
  92. package/lib/esm/test/Formatting.test.js.map +1 -0
  93. package/lib/esm/test/Logging.test.d.ts +2 -0
  94. package/lib/esm/test/Logging.test.d.ts.map +1 -0
  95. package/lib/esm/test/Logging.test.js +52 -0
  96. package/lib/esm/test/Logging.test.js.map +1 -0
  97. package/lib/esm/test/Metadata.test.d.ts +2 -0
  98. package/lib/esm/test/Metadata.test.d.ts.map +1 -0
  99. package/lib/esm/test/Metadata.test.js +671 -0
  100. package/lib/esm/test/Metadata.test.js.map +1 -0
  101. package/lib/esm/test/QueryExecutor.test.d.ts +2 -0
  102. package/lib/esm/test/QueryExecutor.test.d.ts.map +1 -0
  103. package/lib/esm/test/QueryExecutor.test.js +173 -0
  104. package/lib/esm/test/QueryExecutor.test.js.map +1 -0
  105. package/lib/esm/test/Transactions.test.d.ts +2 -0
  106. package/lib/esm/test/Transactions.test.d.ts.map +1 -0
  107. package/lib/esm/test/Transactions.test.js +68 -0
  108. package/lib/esm/test/Transactions.test.js.map +1 -0
  109. package/lib/esm/test/index.d.ts +2 -0
  110. package/lib/esm/test/index.d.ts.map +1 -0
  111. package/lib/esm/test/index.js +15 -0
  112. package/lib/esm/test/index.js.map +1 -0
  113. package/package.json +72 -0
  114. package/src/core-interop/Formatting.ts +194 -0
  115. package/src/core-interop/Logging.ts +58 -0
  116. package/src/core-interop/Metadata.ts +56 -0
  117. package/src/core-interop/MetadataInternal.ts +413 -0
  118. package/src/core-interop/QueryExecutor.ts +122 -0
  119. package/src/core-interop/Transactions.ts +92 -0
  120. package/src/presentation-core-interop.ts +10 -0
  121. package/src/test/Formatting.test.ts +358 -0
  122. package/src/test/Logging.test.ts +59 -0
  123. package/src/test/Metadata.test.ts +776 -0
  124. package/src/test/QueryExecutor.test.ts +217 -0
  125. package/src/test/Transactions.test.ts +74 -0
  126. package/src/test/index.ts +17 -0
  127. package/tsconfig.cjs.json +7 -0
  128. package/tsconfig.esm.json +7 -0
  129. package/tsconfig.json +14 -0
package/.mocharc.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "checkLeaks": true,
3
+ "timeout": 60000,
4
+ "file": ["./lib/cjs/test/index.js"],
5
+ "exclude": ["./lib/test/coverage/**/*"],
6
+ "reporter": ["node_modules/@itwin/build-tools/mocha-reporter"],
7
+ "reporterOptions": ["mochaFile=lib/test/junit_results.xml"]
8
+ }
package/.nycrc ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "check-coverage": true,
3
+ "statements": 100,
4
+ "functions": 100,
5
+ "branches": 100,
6
+ "lines": 100,
7
+ "all": true,
8
+ "include": ["src/**/*", "lib/cjs/**/*"],
9
+ "exclude": ["src/presentation-core-interop.ts", "src/test/**", "lib/cjs/test/**"],
10
+ "extension": [".ts"],
11
+ "temp-dir": "./lib/test/coverage/.nyc_output",
12
+ "report-dir": "./lib/test/coverage",
13
+ "reporter": ["text-summary", "lcov", "cobertura"]
14
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # @itwin/presentation-core-interop
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#554](https://github.com/iTwin/presentation/pull/554): Initial package release.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies:
12
+ - @itwin/presentation-shared@0.1.0
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ # MIT License
2
+
3
+ Copyright © 2017-2024 Bentley Systems, Incorporated. All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # @itwin/presentation-core-interop
2
+
3
+ Copyright © Bentley Systems, Incorporated. All rights reserved. See LICENSE.md for license terms and full copyright notice.
4
+
5
+ The `@itwin/presentation-core-interop` package acts as a layer between [`itwinjs-core` packages](https://www.itwinjs.org/reference/) and `presentation` packages:
6
+
7
+ - `@itwin/presentation-hierarchies`
8
+ - `@itwin/unified-selection`
9
+
10
+ Having this interop layer helps us evolve both sides without affecting one another in a major way.
11
+
12
+ ## API
13
+
14
+ ### `createECSqlQueryExecutor`
15
+
16
+ Maps an iModel in the form of `itwinjs-core` [IModelDb](https://www.itwinjs.org/reference/core-backend/imodels/imodeldb/) or [IModelConnection](https://www.itwinjs.org/reference/core-frontend/imodelconnection/imodelconnection/) to an instance of `ECSqlQueryExecutor`, used in `@itwin/presentation-hierarchies` and `@itwin/unified-selection` packages.
17
+
18
+ Example:
19
+
20
+ ```ts
21
+ import { IModelDb } from "@itwin/core-backend";
22
+ import { createECSqlQueryExecutor } from "@itwin/presentation-core-interop";
23
+
24
+ const imodel: IModelDb = getIModelDb();
25
+ const executor = createECSqlQueryExecutor(imodel);
26
+ for await (const row of executor.createQueryReader(MY_QUERY)) {
27
+ // TODO: do something with `row`
28
+ }
29
+ ```
30
+
31
+ ### `createECSchemaProvider`
32
+
33
+ Maps an instance of `itwinjs-core` [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/) class to an instance of `ECSchemaProvider`, used in `@itwin/presentation-hierarchies` and `@itwin/unified-selection` packages.
34
+
35
+ Example:
36
+
37
+ ```ts
38
+ import { SchemaContext } from "@itwin/ecschema-metadata";
39
+ import { createECSchemaProvider } from "@itwin/presentation-core-interop";
40
+
41
+ const schemas = new SchemaContext();
42
+ const schemaProvider = createECSchemaProvider(schemas);
43
+ // the created schema provider may be used in `@itwin/presentation-hierarchies` or `@itwin/unified-selection` packages
44
+ ```
45
+
46
+ ### `createValueFormatter`
47
+
48
+ Creates an instance of `IPrimitiveValueFormatter` that knows how to format primitive property values using their units' information. That information is retrieved from an iModel through `itwinjs-core` [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/).
49
+
50
+ Example:
51
+
52
+ ```ts
53
+ import { SchemaContext } from "@itwin/ecschema-metadata";
54
+ import { createValueFormatter } from "@itwin/presentation-core-interop";
55
+
56
+ const schemaContext = new SchemaContext();
57
+ const formatter = createValueFormatter({ schemaContext, unitSystem: "metric" });
58
+ const formattedValue = await formatter({ type: "Double", value: 1.234, koqName: "MySchema.LengthKindOfQuantity" });
59
+ ```
60
+
61
+ ### `registerTxnListeners`
62
+
63
+ Registers a number of transaction listeners on either the backend [TxnManager](https://www.itwinjs.org/reference/core-backend/imodels/txnmanager/) or the frontend [BriefcaseTxns](https://www.itwinjs.org/reference/core-frontend/imodelconnection/briefcasetxns/) and calls the given `onChange` function whenever there's a change in an iModel holding the transaction manager.
64
+
65
+ Example:
66
+
67
+ ```ts
68
+ import { BriefcaseDb } from "@itwin/core-backend";
69
+ import { registerTxnListeners } from "@itwin/presentation-core-interop";
70
+ import { HierarchyProvider } from "@itwin/presentation-hierarchies";
71
+
72
+ // get iModel and hierarchy provider from arbitrary sources
73
+ const db: BriefcaseDb = getIModel();
74
+ const provider: HierarchyProvider = getHierarchyProvider();
75
+
76
+ // register the listeners
77
+ const unregister = registerTxnListeners(db.txns, () => {
78
+ // notify provided about the changed data
79
+ provider.notifyDataSourceChanged();
80
+ // TODO: force the components using `provider` to reload
81
+ });
82
+
83
+ // clean up on iModel close
84
+ db.onClosed.addOnce(() => unregister());
85
+ ```
86
+
87
+ ### `createLogger`
88
+
89
+ Maps the `itwinjs-core` [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/) class to an `ILogger` interface used by Presentation packages.
90
+
91
+ Example:
92
+
93
+ ```ts
94
+ import { Logger as CoreLogger } from "@itwin/core-bentley";
95
+ import { createLogger as createPresentationLogger } from "@itwin/presentation-core-interop";
96
+ import { setLogger as setPresentationLogger } from "@itwin/presentation-hierarchies";
97
+
98
+ setPresentationLogger(createPresentationLogger(CoreLogger));
99
+ ```
@@ -0,0 +1,38 @@
1
+ ## API Report File for "@itwin/presentation-core-interop"
2
+
3
+ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
4
+
5
+ ```ts
6
+
7
+ import { ECSchemaProvider } from '@itwin/presentation-shared';
8
+ import { ECSqlQueryExecutor } from '@itwin/presentation-shared';
9
+ import { ECSqlReader } from '@itwin/core-common';
10
+ import { Event } from '@itwin/presentation-shared';
11
+ import { ILogger } from '@itwin/presentation-shared';
12
+ import { IPrimitiveValueFormatter } from '@itwin/presentation-shared';
13
+ import { LogLevel } from '@itwin/core-bentley';
14
+ import { QueryBinder } from '@itwin/core-common';
15
+ import { QueryOptions } from '@itwin/core-common';
16
+ import { Schema } from '@itwin/ecschema-metadata';
17
+ import { SchemaContext } from '@itwin/ecschema-metadata';
18
+ import { SchemaKey } from '@itwin/ecschema-metadata';
19
+ import { UnitSystemKey } from '@itwin/core-quantity';
20
+
21
+ // @beta
22
+ export function createECSchemaProvider(schemaContext: CoreSchemaContext): ECSchemaProvider;
23
+
24
+ // @beta
25
+ export function createECSqlQueryExecutor(imodel: CoreECSqlReaderFactory): ECSqlQueryExecutor;
26
+
27
+ // @beta
28
+ export function createLogger(coreLogger: ICoreLogger): ILogger;
29
+
30
+ // @beta
31
+ export function createValueFormatter(props: CreateValueFormatterProps): IPrimitiveValueFormatter;
32
+
33
+ // @beta
34
+ export function registerTxnListeners(txns: ICoreTxnManager, onChanged: () => void): () => void;
35
+
36
+ // (No @packageDocumentation comment for this package)
37
+
38
+ ```
@@ -0,0 +1,7 @@
1
+ sep=;
2
+ Release Tag;API Item
3
+ beta;createECSchemaProvider(schemaContext: CoreSchemaContext): ECSchemaProvider
4
+ beta;createECSqlQueryExecutor(imodel: CoreECSqlReaderFactory): ECSqlQueryExecutor
5
+ beta;createLogger(coreLogger: ICoreLogger): ILogger
6
+ beta;createValueFormatter(props: CreateValueFormatterProps): IPrimitiveValueFormatter
7
+ beta;registerTxnListeners(txns: ICoreTxnManager, onChanged: () => void): () => void
@@ -0,0 +1,14 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ const iTwinPlugin = require("@itwin/eslint-plugin");
6
+ const eslintBaseConfig = require("../../eslint.base.config");
7
+
8
+ module.exports = [
9
+ {
10
+ files: ["**/*.ts"],
11
+ ...iTwinPlugin.configs.iTwinjsRecommendedConfig,
12
+ },
13
+ ...eslintBaseConfig,
14
+ ];
@@ -0,0 +1,44 @@
1
+ import { UnitSystemKey } from "@itwin/core-quantity";
2
+ import { SchemaContext } from "@itwin/ecschema-metadata";
3
+ import { IPrimitiveValueFormatter } from "@itwin/presentation-shared";
4
+ /**
5
+ * Props for `createValueFormatter` function.
6
+ */
7
+ interface CreateValueFormatterProps {
8
+ /**
9
+ * An instance of [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/) for
10
+ * getting units information.
11
+ */
12
+ schemaContext: SchemaContext;
13
+ /**
14
+ * An optional unit system to use for formatting property values. If not provided, default presentation units are used. If a property
15
+ * doesn't have a default presentation unit, then persistence unit is used. Finally, if a property doesn't have a unit assigned at all,
16
+ * `baseFormatter` is used to format the property value.
17
+ */
18
+ unitSystem?: UnitSystemKey;
19
+ /**
20
+ * Base primitive value formatter for cases when a property doesn't have any units' information. Defaults to the result of `createDefaultValueFormatter`
21
+ * from `@itwin/presentation-hierarchies` package.
22
+ */
23
+ baseFormatter?: IPrimitiveValueFormatter;
24
+ }
25
+ /**
26
+ * Creates an instance of `IPrimitiveValueFormatter` that knows how to format values of properties with assigned kind of quantity. In
27
+ * case the property does not have an assigned kind of quantity, the base formatter is used.
28
+ *
29
+ * Usage example:
30
+ *
31
+ * ```ts
32
+ * import { SchemaContext } from "@itwin/ecschema-metadata";
33
+ * import { createValueFormatter } from "@itwin/presentation-core-interop";
34
+ *
35
+ * const schemaContext = new SchemaContext();
36
+ * const formatter = createValueFormatter({ schemaContext, unitSystem: "metric" });
37
+ * const formattedValue = await formatter({ type: "Double", value: 1.234, koqName: "MySchema.LengthKindOfQuantity" });
38
+ * ```
39
+ *
40
+ * @beta
41
+ */
42
+ export declare function createValueFormatter(props: CreateValueFormatterProps): IPrimitiveValueFormatter;
43
+ export {};
44
+ //# sourceMappingURL=Formatting.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Formatting.d.ts","sourceRoot":"","sources":["../../../src/core-interop/Formatting.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1H,OAAO,EAKL,aAAa,EAKd,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAA+B,wBAAwB,EAA2C,MAAM,4BAA4B,CAAC;AAE5I;;GAEG;AACH,UAAU,yBAAyB;IACjC;;;OAGG;IACH,aAAa,EAAE,aAAa,CAAC;IAE7B;;;;OAIG;IACH,UAAU,CAAC,EAAE,aAAa,CAAC;IAE3B;;;OAGG;IACH,aAAa,CAAC,EAAE,wBAAwB,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,GAAG,wBAAwB,CAc/F"}
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.createValueFormatter = void 0;
8
+ const core_quantity_1 = require("@itwin/core-quantity");
9
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
10
+ const presentation_shared_1 = require("@itwin/presentation-shared");
11
+ /**
12
+ * Creates an instance of `IPrimitiveValueFormatter` that knows how to format values of properties with assigned kind of quantity. In
13
+ * case the property does not have an assigned kind of quantity, the base formatter is used.
14
+ *
15
+ * Usage example:
16
+ *
17
+ * ```ts
18
+ * import { SchemaContext } from "@itwin/ecschema-metadata";
19
+ * import { createValueFormatter } from "@itwin/presentation-core-interop";
20
+ *
21
+ * const schemaContext = new SchemaContext();
22
+ * const formatter = createValueFormatter({ schemaContext, unitSystem: "metric" });
23
+ * const formattedValue = await formatter({ type: "Double", value: 1.234, koqName: "MySchema.LengthKindOfQuantity" });
24
+ * ```
25
+ *
26
+ * @beta
27
+ */
28
+ function createValueFormatter(props) {
29
+ const { schemaContext, unitSystem } = props;
30
+ const baseFormatter = props.baseFormatter ?? /* istanbul ignore next */ (0, presentation_shared_1.createDefaultValueFormatter)();
31
+ const unitsProvider = new ecschema_metadata_1.SchemaUnitProvider(schemaContext);
32
+ return async function (value) {
33
+ if (value.type === "Double" && !!value.koqName) {
34
+ const koq = await getKindOfQuantity(schemaContext, value.koqName);
35
+ const spec = await getFormatterSpec(unitsProvider, koq, unitSystem);
36
+ if (spec) {
37
+ return spec.applyFormatting(value.value);
38
+ }
39
+ }
40
+ return baseFormatter(value);
41
+ };
42
+ }
43
+ exports.createValueFormatter = createValueFormatter;
44
+ async function getKindOfQuantity(schemas, fullName) {
45
+ const { schemaName, className: koqName } = (0, presentation_shared_1.parseFullClassName)(fullName);
46
+ const schema = await schemas.getSchema(new ecschema_metadata_1.SchemaKey(schemaName), ecschema_metadata_1.SchemaMatchType.Latest);
47
+ if (!schema) {
48
+ throw new Error(`Invalid schema "${schemaName}" specified in KoQ full name "${fullName}"`);
49
+ }
50
+ const koq = await schema.getItem(koqName);
51
+ if (!koq) {
52
+ throw new Error(`Invalid kind of quantity "${koqName}" specified in KoQ full name "${fullName}" - it does not exist in schema "${schemaName}"`);
53
+ }
54
+ return koq;
55
+ }
56
+ async function getFormatterSpec(unitsProvider, koq, unitSystem) {
57
+ const formattingProps = await getFormattingProps(koq, unitSystem);
58
+ if (!formattingProps) {
59
+ return undefined;
60
+ }
61
+ const { formatProps, persistenceUnitName } = formattingProps;
62
+ const persistenceUnit = await unitsProvider.findUnitByName(persistenceUnitName);
63
+ const format = await core_quantity_1.Format.createFromJSON("", unitsProvider, formatProps);
64
+ return core_quantity_1.FormatterSpec.create("", format, unitsProvider, persistenceUnit);
65
+ }
66
+ async function getFormattingProps(koq, unitSystem) {
67
+ const persistenceUnit = await koq.persistenceUnit;
68
+ // istanbul ignore if
69
+ if (!persistenceUnit) {
70
+ return undefined;
71
+ }
72
+ const formatProps = await getKoqFormatProps(koq, persistenceUnit, unitSystem);
73
+ if (!formatProps) {
74
+ return undefined;
75
+ }
76
+ return { formatProps, persistenceUnitName: persistenceUnit.fullName };
77
+ }
78
+ async function getKoqFormatProps(koq, persistenceUnit, unitSystem) {
79
+ const unitSystems = getUnitSystemGroupNames(unitSystem);
80
+ // use one of KOQ presentation format that matches requested unit system
81
+ const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);
82
+ if (presentationFormat) {
83
+ return getFormatProps(presentationFormat);
84
+ }
85
+ // use persistence unit format if it matches requested unit system and matching presentation format was not found
86
+ const persistenceUnitSystem = await persistenceUnit.unitSystem;
87
+ if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase())) {
88
+ return getPersistenceUnitFormatProps(persistenceUnit);
89
+ }
90
+ // use default presentation format if persistence unit does not match requested unit system
91
+ if (koq.defaultPresentationFormat) {
92
+ return getFormatProps(koq.defaultPresentationFormat);
93
+ }
94
+ return undefined;
95
+ }
96
+ async function getKoqPresentationFormat(koq, unitSystems) {
97
+ const presentationFormats = koq.presentationFormats;
98
+ for (const system of unitSystems) {
99
+ for (const format of presentationFormats) {
100
+ const unit = format.units && format.units[0][0];
101
+ // istanbul ignore if
102
+ if (!unit) {
103
+ continue;
104
+ }
105
+ const currentUnitSystem = await unit.unitSystem;
106
+ if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system) {
107
+ return format;
108
+ }
109
+ }
110
+ }
111
+ return undefined;
112
+ }
113
+ function getFormatProps(format) {
114
+ return format instanceof ecschema_metadata_1.OverrideFormat ? format.getFormatProps() : format.toJSON();
115
+ }
116
+ function getPersistenceUnitFormatProps(persistenceUnit) {
117
+ // Same as Format "DefaultRealU" in Formats ecschema
118
+ return {
119
+ formatTraits: ["keepSingleZero", "keepDecimalPoint", "showUnitLabel"],
120
+ precision: 6,
121
+ type: "Decimal",
122
+ uomSeparator: " ",
123
+ decimalSeparator: ".",
124
+ composite: {
125
+ units: [
126
+ {
127
+ name: persistenceUnit.fullName,
128
+ label: persistenceUnit.label,
129
+ },
130
+ ],
131
+ },
132
+ };
133
+ }
134
+ function getUnitSystemGroupNames(unitSystem) {
135
+ switch (unitSystem) {
136
+ case "imperial":
137
+ return ["IMPERIAL", "USCUSTOM", "INTERNATIONAL", "FINANCE"];
138
+ case "metric":
139
+ return ["SI", "METRIC", "INTERNATIONAL", "FINANCE"];
140
+ case "usCustomary":
141
+ return ["USCUSTOM", "INTERNATIONAL", "FINANCE"];
142
+ case "usSurvey":
143
+ return ["USSURVEY", "USCUSTOM", "INTERNATIONAL", "FINANCE"];
144
+ }
145
+ return [];
146
+ }
147
+ //# sourceMappingURL=Formatting.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Formatting.js","sourceRoot":"","sources":["../../../src/core-interop/Formatting.ts"],"names":[],"mappings":";AAAA;;;gGAGgG;;;AAEhG,wDAA0H;AAC1H,gEAUkC;AAClC,oEAA4I;AA0B5I;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,oBAAoB,CAAC,KAAgC;IACnE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;IAC5C,MAAM,aAAa,GAAG,KAAK,CAAC,aAAa,IAAI,0BAA0B,CAAC,IAAA,iDAA2B,GAAE,CAAC;IACtG,MAAM,aAAa,GAAG,IAAI,sCAAkB,CAAC,aAAa,CAAC,CAAC;IAC5D,OAAO,KAAK,WAAW,KAA0B;QAC/C,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,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,EAAE,CAAC;QACZ,IAAI,EAAE,SAAS;QACf,YAAY,EAAE,GAAG;QACjB,gBAAgB,EAAE,GAAG;QACrB,SAAS,EAAE;YACT,KAAK,EAAE;gBACL;oBACE,IAAI,EAAE,eAAe,CAAC,QAAQ;oBAC9B,KAAK,EAAE,eAAe,CAAC,KAAK;iBAC7B;aACF;SACF;KACF,CAAC;AACJ,CAAC;AAED,SAAS,uBAAuB,CAAC,UAA0B;IACzD,QAAQ,UAAU,EAAE,CAAC;QACnB,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QAC9D,KAAK,QAAQ;YACX,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QACtD,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QAClD,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;IAChE,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n * Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n * See LICENSE.md in the project root for license terms and full copyright notice.\n *--------------------------------------------------------------------------------------------*/\n\nimport { FormatProps, FormatterSpec, Format as QuantityFormat, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\nimport {\n Format,\n InvertedUnit,\n KindOfQuantity,\n OverrideFormat,\n SchemaContext,\n SchemaKey,\n SchemaMatchType,\n SchemaUnitProvider,\n Unit,\n} from \"@itwin/ecschema-metadata\";\nimport { createDefaultValueFormatter, IPrimitiveValueFormatter, parseFullClassName, TypedPrimitiveValue } from \"@itwin/presentation-shared\";\n\n/**\n * Props for `createValueFormatter` function.\n */\ninterface CreateValueFormatterProps {\n /**\n * An instance of [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/) for\n * getting units information.\n */\n schemaContext: SchemaContext;\n\n /**\n * An optional unit system to use for formatting property values. If not provided, default presentation units are used. If a property\n * doesn't have a default presentation unit, then persistence unit is used. Finally, if a property doesn't have a unit assigned at all,\n * `baseFormatter` is used to format the property value.\n */\n unitSystem?: UnitSystemKey;\n\n /**\n * Base primitive value formatter for cases when a property doesn't have any units' information. Defaults to the result of `createDefaultValueFormatter`\n * from `@itwin/presentation-hierarchies` package.\n */\n baseFormatter?: IPrimitiveValueFormatter;\n}\n\n/**\n * Creates an instance of `IPrimitiveValueFormatter` that knows how to format values of properties with assigned kind of quantity. In\n * case the property does not have an assigned kind of quantity, the base formatter is used.\n *\n * Usage example:\n *\n * ```ts\n * import { SchemaContext } from \"@itwin/ecschema-metadata\";\n * import { createValueFormatter } from \"@itwin/presentation-core-interop\";\n *\n * const schemaContext = new SchemaContext();\n * const formatter = createValueFormatter({ schemaContext, unitSystem: \"metric\" });\n * const formattedValue = await formatter({ type: \"Double\", value: 1.234, koqName: \"MySchema.LengthKindOfQuantity\" });\n * ```\n *\n * @beta\n */\nexport function createValueFormatter(props: CreateValueFormatterProps): IPrimitiveValueFormatter {\n const { schemaContext, unitSystem } = props;\n const baseFormatter = props.baseFormatter ?? /* istanbul ignore next */ createDefaultValueFormatter();\n const unitsProvider = new SchemaUnitProvider(schemaContext);\n return async function (value: TypedPrimitiveValue): Promise<string> {\n if (value.type === \"Double\" && !!value.koqName) {\n const koq = await getKindOfQuantity(schemaContext, value.koqName);\n const spec = await getFormatterSpec(unitsProvider, koq, unitSystem);\n if (spec) {\n return spec.applyFormatting(value.value);\n }\n }\n return baseFormatter(value);\n };\n}\n\nasync function getKindOfQuantity(schemas: SchemaContext, fullName: string) {\n const { schemaName, className: koqName } = parseFullClassName(fullName);\n const schema = await schemas.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\n if (!schema) {\n throw new Error(`Invalid schema \"${schemaName}\" specified in KoQ full name \"${fullName}\"`);\n }\n const koq = await schema.getItem<KindOfQuantity>(koqName);\n if (!koq) {\n throw new Error(`Invalid kind of quantity \"${koqName}\" specified in KoQ full name \"${fullName}\" - it does not exist in schema \"${schemaName}\"`);\n }\n return koq;\n}\n\nasync function getFormatterSpec(unitsProvider: UnitsProvider, koq: KindOfQuantity, unitSystem?: UnitSystemKey) {\n const formattingProps = await getFormattingProps(koq, unitSystem);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await unitsProvider.findUnitByName(persistenceUnitName);\n const format = await QuantityFormat.createFromJSON(\"\", unitsProvider, formatProps);\n return FormatterSpec.create(\"\", format, unitsProvider, persistenceUnit);\n}\n\ninterface FormattingProps {\n formatProps: FormatProps;\n persistenceUnitName: string;\n}\n\nasync function getFormattingProps(koq: KindOfQuantity, unitSystem?: UnitSystemKey): Promise<FormattingProps | undefined> {\n const persistenceUnit = await koq.persistenceUnit;\n // istanbul ignore if\n if (!persistenceUnit) {\n return undefined;\n }\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, unitSystem);\n if (!formatProps) {\n return undefined;\n }\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\n}\n\nasync function getKoqFormatProps(koq: KindOfQuantity, persistenceUnit: Unit | InvertedUnit, unitSystem?: UnitSystemKey) {\n const unitSystems = getUnitSystemGroupNames(unitSystem);\n // use one of KOQ presentation format that matches requested unit system\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);\n if (presentationFormat) {\n return getFormatProps(presentationFormat);\n }\n\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\n if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase())) {\n return getPersistenceUnitFormatProps(persistenceUnit);\n }\n\n // use default presentation format if persistence unit does not match requested unit system\n if (koq.defaultPresentationFormat) {\n return getFormatProps(koq.defaultPresentationFormat);\n }\n\n return undefined;\n}\n\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystems: string[]) {\n const presentationFormats = koq.presentationFormats;\n for (const system of unitSystems) {\n for (const format of presentationFormats) {\n const unit = format.units && format.units[0][0];\n // istanbul ignore if\n if (!unit) {\n continue;\n }\n const currentUnitSystem = await unit.unitSystem;\n if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system) {\n return format;\n }\n }\n }\n return undefined;\n}\n\nfunction getFormatProps(format: Format | OverrideFormat): FormatProps {\n return format instanceof OverrideFormat ? format.getFormatProps() : format.toJSON();\n}\n\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\n // Same as Format \"DefaultRealU\" in Formats ecschema\n return {\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\n precision: 6,\n type: \"Decimal\",\n uomSeparator: \" \",\n decimalSeparator: \".\",\n composite: {\n units: [\n {\n name: persistenceUnit.fullName,\n label: persistenceUnit.label,\n },\n ],\n },\n };\n}\n\nfunction getUnitSystemGroupNames(unitSystem?: UnitSystemKey) {\n switch (unitSystem) {\n case \"imperial\":\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\n case \"metric\":\n return [\"SI\", \"METRIC\", \"INTERNATIONAL\", \"FINANCE\"];\n case \"usCustomary\":\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\n case \"usSurvey\":\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\n }\n return [];\n}\n"]}
@@ -0,0 +1,32 @@
1
+ import { LogLevel as CoreLogLevel } from "@itwin/core-bentley";
2
+ import { ILogger } from "@itwin/presentation-shared";
3
+ /**
4
+ * Defines input for `createLogger`. Generally, this is the [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/)
5
+ * class from `@itwin/core-bentley` package.
6
+ */
7
+ interface ICoreLogger {
8
+ isEnabled(category: string, level: CoreLogLevel): boolean;
9
+ logError(category: string, message: string): void;
10
+ logWarning(category: string, message: string): void;
11
+ logInfo(category: string, message: string): void;
12
+ logTrace(category: string, message: string): void;
13
+ }
14
+ /**
15
+ * Creates a presentation `ILogger` instance that forwards all logging calls to the
16
+ * [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/) API.
17
+ *
18
+ * Usage example:
19
+ *
20
+ * ```ts
21
+ * import { Logger } from "@itwin/core-bentley";
22
+ * import { createLogger } from "@itwin/presentation-core-interop";
23
+ * import { setLogger } from "@itwin/presentation-hierarchies";
24
+ *
25
+ * setLogger(createLogger(Logger));
26
+ * ```
27
+ *
28
+ * @beta
29
+ */
30
+ export declare function createLogger(coreLogger: ICoreLogger): ILogger;
31
+ export {};
32
+ //# sourceMappingURL=Logging.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Logging.d.ts","sourceRoot":"","sources":["../../../src/core-interop/Logging.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,IAAI,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAY,MAAM,4BAA4B,CAAC;AAE/D;;;GAGG;AACH,UAAU,WAAW;IACnB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC;IAC1D,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAClD,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACpD,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACjD,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACnD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,WAAW,GAAG,OAAO,CAQ7D"}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.createLogger = void 0;
8
+ const core_bentley_1 = require("@itwin/core-bentley");
9
+ /**
10
+ * Creates a presentation `ILogger` instance that forwards all logging calls to the
11
+ * [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/) API.
12
+ *
13
+ * Usage example:
14
+ *
15
+ * ```ts
16
+ * import { Logger } from "@itwin/core-bentley";
17
+ * import { createLogger } from "@itwin/presentation-core-interop";
18
+ * import { setLogger } from "@itwin/presentation-hierarchies";
19
+ *
20
+ * setLogger(createLogger(Logger));
21
+ * ```
22
+ *
23
+ * @beta
24
+ */
25
+ function createLogger(coreLogger) {
26
+ return {
27
+ isEnabled: (category, level) => coreLogger.isEnabled(category, getCoreLogLevel(level)),
28
+ logError: (category, msg) => coreLogger.logError(category, msg),
29
+ logWarning: (category, msg) => coreLogger.logWarning(category, msg),
30
+ logInfo: (category, msg) => coreLogger.logInfo(category, msg),
31
+ logTrace: (category, msg) => coreLogger.logTrace(category, msg),
32
+ };
33
+ }
34
+ exports.createLogger = createLogger;
35
+ function getCoreLogLevel(level) {
36
+ switch (level) {
37
+ case "error":
38
+ return core_bentley_1.LogLevel.Error;
39
+ case "warning":
40
+ return core_bentley_1.LogLevel.Warning;
41
+ case "info":
42
+ return core_bentley_1.LogLevel.Info;
43
+ case "trace":
44
+ return core_bentley_1.LogLevel.Trace;
45
+ }
46
+ }
47
+ //# sourceMappingURL=Logging.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Logging.js","sourceRoot":"","sources":["../../../src/core-interop/Logging.ts"],"names":[],"mappings":";AAAA;;;gGAGgG;;;AAEhG,sDAA+D;AAe/D;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,YAAY,CAAC,UAAuB;IAClD,OAAO;QACL,SAAS,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,QAAQ,EAAE,eAAe,CAAC,KAAK,CAAC,CAAC;QACtF,QAAQ,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;QAC/D,UAAU,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,QAAQ,EAAE,GAAG,CAAC;QACnE,OAAO,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC;QAC7D,QAAQ,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;KAChE,CAAC;AACJ,CAAC;AARD,oCAQC;AAED,SAAS,eAAe,CAAC,KAAe;IACtC,QAAQ,KAAK,EAAE,CAAC;QACd,KAAK,OAAO;YACV,OAAO,uBAAY,CAAC,KAAK,CAAC;QAC5B,KAAK,SAAS;YACZ,OAAO,uBAAY,CAAC,OAAO,CAAC;QAC9B,KAAK,MAAM;YACT,OAAO,uBAAY,CAAC,IAAI,CAAC;QAC3B,KAAK,OAAO;YACV,OAAO,uBAAY,CAAC,KAAK,CAAC;IAC9B,CAAC;AACH,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n * Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n * See LICENSE.md in the project root for license terms and full copyright notice.\n *--------------------------------------------------------------------------------------------*/\n\nimport { LogLevel as CoreLogLevel } from \"@itwin/core-bentley\";\nimport { ILogger, LogLevel } from \"@itwin/presentation-shared\";\n\n/**\n * Defines input for `createLogger`. Generally, this is the [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/)\n * class from `@itwin/core-bentley` package.\n */\ninterface ICoreLogger {\n isEnabled(category: string, level: CoreLogLevel): boolean;\n logError(category: string, message: string): void;\n logWarning(category: string, message: string): void;\n logInfo(category: string, message: string): void;\n logTrace(category: string, message: string): void;\n}\n\n/**\n * Creates a presentation `ILogger` instance that forwards all logging calls to the\n * [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/) API.\n *\n * Usage example:\n *\n * ```ts\n * import { Logger } from \"@itwin/core-bentley\";\n * import { createLogger } from \"@itwin/presentation-core-interop\";\n * import { setLogger } from \"@itwin/presentation-hierarchies\";\n *\n * setLogger(createLogger(Logger));\n * ```\n *\n * @beta\n */\nexport function createLogger(coreLogger: ICoreLogger): ILogger {\n return {\n isEnabled: (category, level) => coreLogger.isEnabled(category, getCoreLogLevel(level)),\n logError: (category, msg) => coreLogger.logError(category, msg),\n logWarning: (category, msg) => coreLogger.logWarning(category, msg),\n logInfo: (category, msg) => coreLogger.logInfo(category, msg),\n logTrace: (category, msg) => coreLogger.logTrace(category, msg),\n };\n}\n\nfunction getCoreLogLevel(level: LogLevel): CoreLogLevel {\n switch (level) {\n case \"error\":\n return CoreLogLevel.Error;\n case \"warning\":\n return CoreLogLevel.Warning;\n case \"info\":\n return CoreLogLevel.Info;\n case \"trace\":\n return CoreLogLevel.Trace;\n }\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import { Schema as CoreSchema, SchemaKey as CoreSchemaKey } from "@itwin/ecschema-metadata";
2
+ import { ECSchemaProvider } from "@itwin/presentation-shared";
3
+ /**
4
+ * Defines input for `createECSchemaProvider`. Generally, this is an instance of [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/)
5
+ * class from `@itwin/ecschema-metadata` package.
6
+ */
7
+ interface CoreSchemaContext {
8
+ getSchema(key: CoreSchemaKey): Promise<CoreSchema | undefined>;
9
+ }
10
+ /**
11
+ * Creates an `ECSchemaProvider` for given [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/).
12
+ *
13
+ * Usage example:
14
+ *
15
+ * ```ts
16
+ * import { SchemaContext } from "@itwin/ecschema-metadata";
17
+ * import { createECSchemaProvider } from "@itwin/presentation-core-interop";
18
+ *
19
+ * const schemaContext = new SchemaContext();
20
+ * const schemaProvider = createECSchemaProvider(schemas);
21
+ * // the created schema provider may be used in `@itwin/presentation-hierarchies` and other Presentation packages
22
+ * ```
23
+ *
24
+ * @beta
25
+ */
26
+ export declare function createECSchemaProvider(schemaContext: CoreSchemaContext): ECSchemaProvider;
27
+ export {};
28
+ //# sourceMappingURL=Metadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Metadata.d.ts","sourceRoot":"","sources":["../../../src/core-interop/Metadata.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,SAAS,IAAI,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC5F,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAG9D;;;GAGG;AACH,UAAU,iBAAiB;IACzB,SAAS,CAAC,GAAG,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;CAChE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,sBAAsB,CAAC,aAAa,EAAE,iBAAiB,GAAG,gBAAgB,CAsBzF"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.createECSchemaProvider = void 0;
8
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
9
+ const MetadataInternal_1 = require("./MetadataInternal");
10
+ /**
11
+ * Creates an `ECSchemaProvider` for given [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/).
12
+ *
13
+ * Usage example:
14
+ *
15
+ * ```ts
16
+ * import { SchemaContext } from "@itwin/ecschema-metadata";
17
+ * import { createECSchemaProvider } from "@itwin/presentation-core-interop";
18
+ *
19
+ * const schemaContext = new SchemaContext();
20
+ * const schemaProvider = createECSchemaProvider(schemas);
21
+ * // the created schema provider may be used in `@itwin/presentation-hierarchies` and other Presentation packages
22
+ * ```
23
+ *
24
+ * @beta
25
+ */
26
+ function createECSchemaProvider(schemaContext) {
27
+ async function getSchemaUnprotected(schemaName) {
28
+ const coreSchema = await schemaContext.getSchema(new ecschema_metadata_1.SchemaKey(schemaName));
29
+ return coreSchema ? (0, MetadataInternal_1.createECSchema)(coreSchema) : undefined;
30
+ }
31
+ async function getSchemaProtected(schemaName, handledExistingSchemaErrors) {
32
+ // workaround for https://github.com/iTwin/itwinjs-core/issues/6542
33
+ try {
34
+ return await getSchemaUnprotected(schemaName);
35
+ }
36
+ catch (e) {
37
+ if (e instanceof Error && e.message.includes("already exists within this cache") && !handledExistingSchemaErrors.has(schemaName)) {
38
+ handledExistingSchemaErrors.add(schemaName);
39
+ return getSchemaProtected(schemaName, handledExistingSchemaErrors);
40
+ }
41
+ throw e;
42
+ }
43
+ }
44
+ return {
45
+ async getSchema(name) {
46
+ return getSchemaProtected(name, new Set());
47
+ },
48
+ };
49
+ }
50
+ exports.createECSchemaProvider = createECSchemaProvider;
51
+ //# sourceMappingURL=Metadata.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Metadata.js","sourceRoot":"","sources":["../../../src/core-interop/Metadata.ts"],"names":[],"mappings":";AAAA;;;gGAGgG;;;AAEhG,gEAA4F;AAE5F,yDAAoD;AAUpD;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,sBAAsB,CAAC,aAAgC;IACrE,KAAK,UAAU,oBAAoB,CAAC,UAAkB;QACpD,MAAM,UAAU,GAAG,MAAM,aAAa,CAAC,SAAS,CAAC,IAAI,6BAAa,CAAC,UAAU,CAAC,CAAC,CAAC;QAChF,OAAO,UAAU,CAAC,CAAC,CAAC,IAAA,iCAAc,EAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7D,CAAC;IACD,KAAK,UAAU,kBAAkB,CAAC,UAAkB,EAAE,2BAAwC;QAC5F,mEAAmE;QACnE,IAAI,CAAC;YACH,OAAO,MAAM,oBAAoB,CAAC,UAAU,CAAC,CAAC;QAChD,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,kCAAkC,CAAC,IAAI,CAAC,2BAA2B,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;gBACjI,2BAA2B,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;gBAC5C,OAAO,kBAAkB,CAAC,UAAU,EAAE,2BAA2B,CAAC,CAAC;YACrE,CAAC;YACD,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC;IACD,OAAO;QACL,KAAK,CAAC,SAAS,CAAC,IAAI;YAClB,OAAO,kBAAkB,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;QAC7C,CAAC;KACF,CAAC;AACJ,CAAC;AAtBD,wDAsBC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n * Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n * See LICENSE.md in the project root for license terms and full copyright notice.\n *--------------------------------------------------------------------------------------------*/\n\nimport { Schema as CoreSchema, SchemaKey as CoreSchemaKey } from \"@itwin/ecschema-metadata\";\nimport { ECSchemaProvider } from \"@itwin/presentation-shared\";\nimport { createECSchema } from \"./MetadataInternal\";\n\n/**\n * Defines input for `createECSchemaProvider`. Generally, this is an instance of [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/)\n * class from `@itwin/ecschema-metadata` package.\n */\ninterface CoreSchemaContext {\n getSchema(key: CoreSchemaKey): Promise<CoreSchema | undefined>;\n}\n\n/**\n * Creates an `ECSchemaProvider` for given [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/).\n *\n * Usage example:\n *\n * ```ts\n * import { SchemaContext } from \"@itwin/ecschema-metadata\";\n * import { createECSchemaProvider } from \"@itwin/presentation-core-interop\";\n *\n * const schemaContext = new SchemaContext();\n * const schemaProvider = createECSchemaProvider(schemas);\n * // the created schema provider may be used in `@itwin/presentation-hierarchies` and other Presentation packages\n * ```\n *\n * @beta\n */\nexport function createECSchemaProvider(schemaContext: CoreSchemaContext): ECSchemaProvider {\n async function getSchemaUnprotected(schemaName: string) {\n const coreSchema = await schemaContext.getSchema(new CoreSchemaKey(schemaName));\n return coreSchema ? createECSchema(coreSchema) : undefined;\n }\n async function getSchemaProtected(schemaName: string, handledExistingSchemaErrors: Set<string>) {\n // workaround for https://github.com/iTwin/itwinjs-core/issues/6542\n try {\n return await getSchemaUnprotected(schemaName);\n } catch (e) {\n if (e instanceof Error && e.message.includes(\"already exists within this cache\") && !handledExistingSchemaErrors.has(schemaName)) {\n handledExistingSchemaErrors.add(schemaName);\n return getSchemaProtected(schemaName, handledExistingSchemaErrors);\n }\n throw e;\n }\n }\n return {\n async getSchema(name) {\n return getSchemaProtected(name, new Set());\n },\n };\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import { ECClass as CoreClass, Property as CoreProperty, Schema as CoreSchema } from "@itwin/ecschema-metadata";
2
+ import { EC } from "@itwin/presentation-shared";
3
+ /** @internal */
4
+ export declare function createECSchema(schema: CoreSchema): EC.Schema;
5
+ /** @internal */
6
+ export declare function createECClass(coreClass: CoreClass, schema?: EC.Schema): EC.Class;
7
+ /** @internal */
8
+ export declare function createECProperty(coreProperty: CoreProperty, ecClass: EC.Class): EC.Property;
9
+ //# sourceMappingURL=MetadataInternal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MetadataInternal.d.ts","sourceRoot":"","sources":["../../../src/core-interop/MetadataInternal.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,OAAO,IAAI,SAAS,EAUpB,QAAQ,IAAI,YAAY,EAGxB,MAAM,IAAI,UAAU,EASrB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,EAAE,EAAE,MAAM,4BAA4B,CAAC;AAEhD,gBAAgB;AAChB,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,EAAE,CAAC,MAAM,CAQ5D;AAwBD,gBAAgB;AAChB,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,MAAM,GAAG,EAAE,CAAC,KAAK,CAYhF;AAoFD,gBAAgB;AAChB,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,EAAE,CAAC,KAAK,GAAG,EAAE,CAAC,QAAQ,CAqB3F"}