@itwin/presentation-core-interop 0.1.2 → 0.2.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.
Files changed (83) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/lib/cjs/core-interop/Metadata.d.ts.map +1 -1
  3. package/lib/cjs/core-interop/Metadata.js +7 -1
  4. package/lib/cjs/core-interop/Metadata.js.map +1 -1
  5. package/lib/cjs/core-interop/QueryExecutor.d.ts.map +1 -1
  6. package/lib/cjs/core-interop/QueryExecutor.js +5 -1
  7. package/lib/cjs/core-interop/QueryExecutor.js.map +1 -1
  8. package/lib/esm/core-interop/Metadata.d.ts.map +1 -1
  9. package/lib/esm/core-interop/Metadata.js +7 -1
  10. package/lib/esm/core-interop/Metadata.js.map +1 -1
  11. package/lib/esm/core-interop/QueryExecutor.d.ts.map +1 -1
  12. package/lib/esm/core-interop/QueryExecutor.js +5 -1
  13. package/lib/esm/core-interop/QueryExecutor.js.map +1 -1
  14. package/package.json +12 -12
  15. package/.mocharc.json +0 -8
  16. package/.nycrc +0 -14
  17. package/api/presentation-core-interop.api.md +0 -38
  18. package/api/presentation-core-interop.exports.csv +0 -7
  19. package/eslint.config.js +0 -14
  20. package/lib/cjs/test/Formatting.test.d.ts +0 -2
  21. package/lib/cjs/test/Formatting.test.d.ts.map +0 -1
  22. package/lib/cjs/test/Formatting.test.js +0 -282
  23. package/lib/cjs/test/Formatting.test.js.map +0 -1
  24. package/lib/cjs/test/Logging.test.d.ts +0 -2
  25. package/lib/cjs/test/Logging.test.d.ts.map +0 -1
  26. package/lib/cjs/test/Logging.test.js +0 -57
  27. package/lib/cjs/test/Logging.test.js.map +0 -1
  28. package/lib/cjs/test/Metadata.test.d.ts +0 -2
  29. package/lib/cjs/test/Metadata.test.d.ts.map +0 -1
  30. package/lib/cjs/test/Metadata.test.js +0 -684
  31. package/lib/cjs/test/Metadata.test.js.map +0 -1
  32. package/lib/cjs/test/QueryExecutor.test.d.ts +0 -2
  33. package/lib/cjs/test/QueryExecutor.test.d.ts.map +0 -1
  34. package/lib/cjs/test/QueryExecutor.test.js +0 -178
  35. package/lib/cjs/test/QueryExecutor.test.js.map +0 -1
  36. package/lib/cjs/test/Transactions.test.d.ts +0 -2
  37. package/lib/cjs/test/Transactions.test.d.ts.map +0 -1
  38. package/lib/cjs/test/Transactions.test.js +0 -73
  39. package/lib/cjs/test/Transactions.test.js.map +0 -1
  40. package/lib/cjs/test/index.d.ts +0 -2
  41. package/lib/cjs/test/index.d.ts.map +0 -1
  42. package/lib/cjs/test/index.js +0 -43
  43. package/lib/cjs/test/index.js.map +0 -1
  44. package/lib/esm/test/Formatting.test.d.ts +0 -2
  45. package/lib/esm/test/Formatting.test.d.ts.map +0 -1
  46. package/lib/esm/test/Formatting.test.js +0 -277
  47. package/lib/esm/test/Formatting.test.js.map +0 -1
  48. package/lib/esm/test/Logging.test.d.ts +0 -2
  49. package/lib/esm/test/Logging.test.d.ts.map +0 -1
  50. package/lib/esm/test/Logging.test.js +0 -52
  51. package/lib/esm/test/Logging.test.js.map +0 -1
  52. package/lib/esm/test/Metadata.test.d.ts +0 -2
  53. package/lib/esm/test/Metadata.test.d.ts.map +0 -1
  54. package/lib/esm/test/Metadata.test.js +0 -679
  55. package/lib/esm/test/Metadata.test.js.map +0 -1
  56. package/lib/esm/test/QueryExecutor.test.d.ts +0 -2
  57. package/lib/esm/test/QueryExecutor.test.d.ts.map +0 -1
  58. package/lib/esm/test/QueryExecutor.test.js +0 -173
  59. package/lib/esm/test/QueryExecutor.test.js.map +0 -1
  60. package/lib/esm/test/Transactions.test.d.ts +0 -2
  61. package/lib/esm/test/Transactions.test.d.ts.map +0 -1
  62. package/lib/esm/test/Transactions.test.js +0 -68
  63. package/lib/esm/test/Transactions.test.js.map +0 -1
  64. package/lib/esm/test/index.d.ts +0 -2
  65. package/lib/esm/test/index.d.ts.map +0 -1
  66. package/lib/esm/test/index.js +0 -15
  67. package/lib/esm/test/index.js.map +0 -1
  68. package/src/core-interop/Formatting.ts +0 -194
  69. package/src/core-interop/Logging.ts +0 -58
  70. package/src/core-interop/Metadata.ts +0 -62
  71. package/src/core-interop/MetadataInternal.ts +0 -413
  72. package/src/core-interop/QueryExecutor.ts +0 -122
  73. package/src/core-interop/Transactions.ts +0 -92
  74. package/src/presentation-core-interop.ts +0 -10
  75. package/src/test/Formatting.test.ts +0 -358
  76. package/src/test/Logging.test.ts +0 -59
  77. package/src/test/Metadata.test.ts +0 -786
  78. package/src/test/QueryExecutor.test.ts +0 -217
  79. package/src/test/Transactions.test.ts +0 -74
  80. package/src/test/index.ts +0 -17
  81. package/tsconfig.cjs.json +0 -7
  82. package/tsconfig.esm.json +0 -7
  83. package/tsconfig.json +0 -14
@@ -1,194 +0,0 @@
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
-
6
- import { FormatProps, FormatterSpec, Format as QuantityFormat, UnitsProvider, UnitSystemKey } from "@itwin/core-quantity";
7
- import {
8
- Format,
9
- InvertedUnit,
10
- KindOfQuantity,
11
- OverrideFormat,
12
- SchemaContext,
13
- SchemaKey,
14
- SchemaMatchType,
15
- SchemaUnitProvider,
16
- Unit,
17
- } from "@itwin/ecschema-metadata";
18
- import { createDefaultValueFormatter, IPrimitiveValueFormatter, parseFullClassName, TypedPrimitiveValue } from "@itwin/presentation-shared";
19
-
20
- /**
21
- * Props for `createValueFormatter` function.
22
- */
23
- interface CreateValueFormatterProps {
24
- /**
25
- * An instance of [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/) for
26
- * getting units information.
27
- */
28
- schemaContext: SchemaContext;
29
-
30
- /**
31
- * An optional unit system to use for formatting property values. If not provided, default presentation units are used. If a property
32
- * doesn't have a default presentation unit, then persistence unit is used. Finally, if a property doesn't have a unit assigned at all,
33
- * `baseFormatter` is used to format the property value.
34
- */
35
- unitSystem?: UnitSystemKey;
36
-
37
- /**
38
- * Base primitive value formatter for cases when a property doesn't have any units' information. Defaults to the result of `createDefaultValueFormatter`
39
- * from `@itwin/presentation-hierarchies` package.
40
- */
41
- baseFormatter?: IPrimitiveValueFormatter;
42
- }
43
-
44
- /**
45
- * Creates an instance of `IPrimitiveValueFormatter` that knows how to format values of properties with assigned kind of quantity. In
46
- * case the property does not have an assigned kind of quantity, the base formatter is used.
47
- *
48
- * Usage example:
49
- *
50
- * ```ts
51
- * import { SchemaContext } from "@itwin/ecschema-metadata";
52
- * import { createValueFormatter } from "@itwin/presentation-core-interop";
53
- *
54
- * const schemaContext = new SchemaContext();
55
- * const formatter = createValueFormatter({ schemaContext, unitSystem: "metric" });
56
- * const formattedValue = await formatter({ type: "Double", value: 1.234, koqName: "MySchema.LengthKindOfQuantity" });
57
- * ```
58
- *
59
- * @beta
60
- */
61
- export function createValueFormatter(props: CreateValueFormatterProps): IPrimitiveValueFormatter {
62
- const { schemaContext, unitSystem } = props;
63
- const baseFormatter = props.baseFormatter ?? /* istanbul ignore next */ createDefaultValueFormatter();
64
- const unitsProvider = new SchemaUnitProvider(schemaContext);
65
- return async function (value: TypedPrimitiveValue): Promise<string> {
66
- if (value.type === "Double" && !!value.koqName) {
67
- const koq = await getKindOfQuantity(schemaContext, value.koqName);
68
- const spec = await getFormatterSpec(unitsProvider, koq, unitSystem);
69
- if (spec) {
70
- return spec.applyFormatting(value.value);
71
- }
72
- }
73
- return baseFormatter(value);
74
- };
75
- }
76
-
77
- async function getKindOfQuantity(schemas: SchemaContext, fullName: string) {
78
- const { schemaName, className: koqName } = parseFullClassName(fullName);
79
- const schema = await schemas.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);
80
- if (!schema) {
81
- throw new Error(`Invalid schema "${schemaName}" specified in KoQ full name "${fullName}"`);
82
- }
83
- const koq = await schema.getItem<KindOfQuantity>(koqName);
84
- if (!koq) {
85
- throw new Error(`Invalid kind of quantity "${koqName}" specified in KoQ full name "${fullName}" - it does not exist in schema "${schemaName}"`);
86
- }
87
- return koq;
88
- }
89
-
90
- async function getFormatterSpec(unitsProvider: UnitsProvider, koq: KindOfQuantity, unitSystem?: UnitSystemKey) {
91
- const formattingProps = await getFormattingProps(koq, unitSystem);
92
- if (!formattingProps) {
93
- return undefined;
94
- }
95
- const { formatProps, persistenceUnitName } = formattingProps;
96
- const persistenceUnit = await unitsProvider.findUnitByName(persistenceUnitName);
97
- const format = await QuantityFormat.createFromJSON("", unitsProvider, formatProps);
98
- return FormatterSpec.create("", format, unitsProvider, persistenceUnit);
99
- }
100
-
101
- interface FormattingProps {
102
- formatProps: FormatProps;
103
- persistenceUnitName: string;
104
- }
105
-
106
- async function getFormattingProps(koq: KindOfQuantity, unitSystem?: UnitSystemKey): Promise<FormattingProps | undefined> {
107
- const persistenceUnit = await koq.persistenceUnit;
108
- // istanbul ignore if
109
- if (!persistenceUnit) {
110
- return undefined;
111
- }
112
- const formatProps = await getKoqFormatProps(koq, persistenceUnit, unitSystem);
113
- if (!formatProps) {
114
- return undefined;
115
- }
116
- return { formatProps, persistenceUnitName: persistenceUnit.fullName };
117
- }
118
-
119
- async function getKoqFormatProps(koq: KindOfQuantity, persistenceUnit: Unit | InvertedUnit, unitSystem?: UnitSystemKey) {
120
- const unitSystems = getUnitSystemGroupNames(unitSystem);
121
- // use one of KOQ presentation format that matches requested unit system
122
- const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);
123
- if (presentationFormat) {
124
- return getFormatProps(presentationFormat);
125
- }
126
-
127
- // use persistence unit format if it matches requested unit system and matching presentation format was not found
128
- const persistenceUnitSystem = await persistenceUnit.unitSystem;
129
- if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase())) {
130
- return getPersistenceUnitFormatProps(persistenceUnit);
131
- }
132
-
133
- // use default presentation format if persistence unit does not match requested unit system
134
- if (koq.defaultPresentationFormat) {
135
- return getFormatProps(koq.defaultPresentationFormat);
136
- }
137
-
138
- return undefined;
139
- }
140
-
141
- async function getKoqPresentationFormat(koq: KindOfQuantity, unitSystems: string[]) {
142
- const presentationFormats = koq.presentationFormats;
143
- for (const system of unitSystems) {
144
- for (const format of presentationFormats) {
145
- const unit = format.units && format.units[0][0];
146
- // istanbul ignore if
147
- if (!unit) {
148
- continue;
149
- }
150
- const currentUnitSystem = await unit.unitSystem;
151
- if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system) {
152
- return format;
153
- }
154
- }
155
- }
156
- return undefined;
157
- }
158
-
159
- function getFormatProps(format: Format | OverrideFormat): FormatProps {
160
- return format instanceof OverrideFormat ? format.getFormatProps() : format.toJSON();
161
- }
162
-
163
- function getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {
164
- // Same as Format "DefaultRealU" in Formats ecschema
165
- return {
166
- formatTraits: ["keepSingleZero", "keepDecimalPoint", "showUnitLabel"],
167
- precision: 6,
168
- type: "Decimal",
169
- uomSeparator: " ",
170
- decimalSeparator: ".",
171
- composite: {
172
- units: [
173
- {
174
- name: persistenceUnit.fullName,
175
- label: persistenceUnit.label,
176
- },
177
- ],
178
- },
179
- };
180
- }
181
-
182
- function getUnitSystemGroupNames(unitSystem?: UnitSystemKey) {
183
- switch (unitSystem) {
184
- case "imperial":
185
- return ["IMPERIAL", "USCUSTOM", "INTERNATIONAL", "FINANCE"];
186
- case "metric":
187
- return ["SI", "METRIC", "INTERNATIONAL", "FINANCE"];
188
- case "usCustomary":
189
- return ["USCUSTOM", "INTERNATIONAL", "FINANCE"];
190
- case "usSurvey":
191
- return ["USSURVEY", "USCUSTOM", "INTERNATIONAL", "FINANCE"];
192
- }
193
- return [];
194
- }
@@ -1,58 +0,0 @@
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
-
6
- import { LogLevel as CoreLogLevel } from "@itwin/core-bentley";
7
- import { ILogger, LogLevel } from "@itwin/presentation-shared";
8
-
9
- /**
10
- * Defines input for `createLogger`. Generally, this is the [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/)
11
- * class from `@itwin/core-bentley` package.
12
- */
13
- interface ICoreLogger {
14
- isEnabled(category: string, level: CoreLogLevel): boolean;
15
- logError(category: string, message: string): void;
16
- logWarning(category: string, message: string): void;
17
- logInfo(category: string, message: string): void;
18
- logTrace(category: string, message: string): void;
19
- }
20
-
21
- /**
22
- * Creates a presentation `ILogger` instance that forwards all logging calls to the
23
- * [Logger](https://www.itwinjs.org/reference/core-bentley/logging/logger/) API.
24
- *
25
- * Usage example:
26
- *
27
- * ```ts
28
- * import { Logger } from "@itwin/core-bentley";
29
- * import { createLogger } from "@itwin/presentation-core-interop";
30
- * import { setLogger } from "@itwin/presentation-hierarchies";
31
- *
32
- * setLogger(createLogger(Logger));
33
- * ```
34
- *
35
- * @beta
36
- */
37
- export function createLogger(coreLogger: ICoreLogger): ILogger {
38
- return {
39
- isEnabled: (category, level) => coreLogger.isEnabled(category, getCoreLogLevel(level)),
40
- logError: (category, msg) => coreLogger.logError(category, msg),
41
- logWarning: (category, msg) => coreLogger.logWarning(category, msg),
42
- logInfo: (category, msg) => coreLogger.logInfo(category, msg),
43
- logTrace: (category, msg) => coreLogger.logTrace(category, msg),
44
- };
45
- }
46
-
47
- function getCoreLogLevel(level: LogLevel): CoreLogLevel {
48
- switch (level) {
49
- case "error":
50
- return CoreLogLevel.Error;
51
- case "warning":
52
- return CoreLogLevel.Warning;
53
- case "info":
54
- return CoreLogLevel.Info;
55
- case "trace":
56
- return CoreLogLevel.Trace;
57
- }
58
- }
@@ -1,62 +0,0 @@
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
-
6
- import { Schema as CoreSchema, SchemaKey as CoreSchemaKey } from "@itwin/ecschema-metadata";
7
- import { ECSchemaProvider } from "@itwin/presentation-shared";
8
- import { createECSchema } from "./MetadataInternal";
9
-
10
- /**
11
- * Defines input for `createECSchemaProvider`. Generally, this is an instance of [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/)
12
- * class from `@itwin/ecschema-metadata` package.
13
- */
14
- interface CoreSchemaContext {
15
- getSchema(key: CoreSchemaKey): Promise<CoreSchema | undefined>;
16
- }
17
-
18
- /**
19
- * Creates an `ECSchemaProvider` for given [SchemaContext](https://www.itwinjs.org/reference/ecschema-metadata/context/schemacontext/).
20
- *
21
- * Usage example:
22
- *
23
- * ```ts
24
- * import { SchemaContext } from "@itwin/ecschema-metadata";
25
- * import { createECSchemaProvider } from "@itwin/presentation-core-interop";
26
- *
27
- * const schemaContext = new SchemaContext();
28
- * const schemaProvider = createECSchemaProvider(schemas);
29
- * // the created schema provider may be used in `@itwin/presentation-hierarchies` and other Presentation packages
30
- * ```
31
- *
32
- * @beta
33
- */
34
- export function createECSchemaProvider(schemaContext: CoreSchemaContext): ECSchemaProvider {
35
- async function getSchemaUnprotected(schemaName: string) {
36
- const coreSchema = await schemaContext.getSchema(new CoreSchemaKey(schemaName));
37
- return coreSchema ? createECSchema(coreSchema) : undefined;
38
- }
39
- async function getSchemaProtected(schemaName: string, handledExistingSchemaErrors: Set<string>) {
40
- // workaround for https://github.com/iTwin/itwinjs-core/issues/6542
41
- try {
42
- return await getSchemaUnprotected(schemaName);
43
- } catch (e) {
44
- // istanbul ignore else
45
- if (e instanceof Error) {
46
- if (e.message.includes("already exists within this cache") && !handledExistingSchemaErrors.has(schemaName)) {
47
- handledExistingSchemaErrors.add(schemaName);
48
- return getSchemaProtected(schemaName, handledExistingSchemaErrors);
49
- }
50
- if (e.message.includes("schema not found")) {
51
- return undefined;
52
- }
53
- }
54
- throw e;
55
- }
56
- }
57
- return {
58
- async getSchema(name) {
59
- return getSchemaProtected(name, new Set());
60
- },
61
- };
62
- }