@fgv/ts-res 5.1.0-1 → 5.1.0-3
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/packlets/config/common.js +78 -0
- package/dist/packlets/config/configInitFactory.js +258 -0
- package/dist/packlets/config/convert.js +56 -0
- package/dist/packlets/config/index.browser.js +34 -0
- package/dist/packlets/config/index.js +28 -0
- package/dist/packlets/config/json.js +23 -0
- package/dist/packlets/config/predefined/default.js +138 -0
- package/dist/packlets/config/predefined/extended.js +190 -0
- package/dist/packlets/config/predefined/index.js +25 -0
- package/dist/packlets/config/systemConfiguration.js +147 -0
- package/dist/packlets/qualifier-types/config/convert.js +124 -0
- package/dist/packlets/qualifier-types/config/index.js +25 -0
- package/dist/packlets/qualifier-types/config/json.js +32 -0
- package/dist/packlets/resource-types/config/convert.js +35 -0
- package/dist/packlets/resource-types/config/index.js +25 -0
- package/dist/packlets/resource-types/config/json.js +23 -0
- package/lib/packlets/config/common.d.ts +35 -0
- package/lib/packlets/config/common.js +97 -0
- package/lib/packlets/config/configInitFactory.d.ts +217 -0
- package/lib/packlets/config/configInitFactory.js +303 -0
- package/lib/packlets/config/convert.d.ts +23 -0
- package/lib/packlets/config/convert.js +93 -0
- package/lib/packlets/config/index.browser.d.ts +7 -0
- package/lib/packlets/config/index.browser.js +74 -0
- package/lib/packlets/config/index.d.ts +7 -0
- package/lib/packlets/config/index.js +68 -0
- package/lib/packlets/config/json.d.ts +20 -0
- package/lib/packlets/config/json.js +24 -0
- package/lib/packlets/config/predefined/default.d.ts +57 -0
- package/lib/packlets/config/predefined/default.js +141 -0
- package/lib/packlets/config/predefined/extended.d.ts +25 -0
- package/lib/packlets/config/predefined/extended.js +193 -0
- package/lib/packlets/config/predefined/index.d.ts +4 -0
- package/lib/packlets/config/predefined/index.js +62 -0
- package/lib/packlets/config/systemConfiguration.d.ts +94 -0
- package/lib/packlets/config/systemConfiguration.js +152 -0
- package/lib/packlets/qualifier-types/config/convert.d.ts +65 -0
- package/lib/packlets/qualifier-types/config/convert.js +161 -0
- package/lib/packlets/qualifier-types/config/index.d.ts +4 -0
- package/lib/packlets/qualifier-types/config/index.js +64 -0
- package/lib/packlets/qualifier-types/config/json.d.ts +91 -0
- package/lib/packlets/qualifier-types/config/json.js +35 -0
- package/lib/packlets/resource-types/config/convert.d.ts +8 -0
- package/lib/packlets/resource-types/config/convert.js +38 -0
- package/lib/packlets/resource-types/config/index.d.ts +4 -0
- package/lib/packlets/resource-types/config/index.js +64 -0
- package/lib/packlets/resource-types/config/json.d.ts +11 -0
- package/lib/packlets/resource-types/config/json.js +24 -0
- package/package.json +24 -24
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
+
}
|
|
29
|
+
Object.defineProperty(o, k2, desc);
|
|
30
|
+
}) : (function(o, m, k, k2) {
|
|
31
|
+
if (k2 === undefined) k2 = k;
|
|
32
|
+
o[k2] = m[k];
|
|
33
|
+
}));
|
|
34
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
35
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.allPredefinedSystemConfigurations = void 0;
|
|
39
|
+
exports.getPredefinedDeclaration = getPredefinedDeclaration;
|
|
40
|
+
exports.getPredefinedSystemConfiguration = getPredefinedSystemConfiguration;
|
|
41
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
42
|
+
const systemConfiguration_1 = require("./systemConfiguration");
|
|
43
|
+
const ts_json_base_1 = require("@fgv/ts-json-base");
|
|
44
|
+
const predefined_1 = require("./predefined");
|
|
45
|
+
/**
|
|
46
|
+
* An array of all well-known predefined system configurations.
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
exports.allPredefinedSystemConfigurations = [
|
|
50
|
+
'default',
|
|
51
|
+
'language-priority',
|
|
52
|
+
'territory-priority',
|
|
53
|
+
'extended-example'
|
|
54
|
+
];
|
|
55
|
+
__exportStar(require("./predefined"), exports);
|
|
56
|
+
const predefinedDecls = {
|
|
57
|
+
default: predefined_1.Default.DefaultSystemConfiguration,
|
|
58
|
+
'language-priority': predefined_1.Default.LanguagePrioritySystemConfiguration,
|
|
59
|
+
'territory-priority': predefined_1.Default.TerritoryPrioritySystemConfiguration,
|
|
60
|
+
'extended-example': predefined_1.Example.ExtendedSystemConfiguration
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Returns the {@link Config.Model.ISystemConfiguration | system configuration} declaration for the
|
|
64
|
+
* specified predefined system configuration.
|
|
65
|
+
* @param name - The name of the predefined system configuration.
|
|
66
|
+
* @param initParams - Optional {@link Config.ISystemConfigurationInitParams | initialization parameters}.
|
|
67
|
+
* @returns `Success` with the {@link Config.Model.ISystemConfiguration | system configuration}
|
|
68
|
+
* declaration if successful, `Failure` with an error message otherwise.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
function getPredefinedDeclaration(name, initParams) {
|
|
72
|
+
if (name in predefinedDecls) {
|
|
73
|
+
const baseConfig = (0, ts_json_base_1.sanitizeJsonObject)(predefinedDecls[name]);
|
|
74
|
+
if (initParams === null || initParams === void 0 ? void 0 : initParams.qualifierDefaultValues) {
|
|
75
|
+
return baseConfig.onSuccess((config) => (0, systemConfiguration_1.updateSystemConfigurationQualifierDefaultValues)(config, initParams.qualifierDefaultValues));
|
|
76
|
+
}
|
|
77
|
+
return baseConfig;
|
|
78
|
+
}
|
|
79
|
+
/* c8 ignore next 3 - defense in depth */
|
|
80
|
+
return (0, ts_utils_1.fail)(`Unknown predefined system configuration: ${name}`);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Returns the {@link Config.SystemConfiguration | SystemConfiguration} for the specified
|
|
84
|
+
* predefined system configuration.
|
|
85
|
+
* @param name - The name of the predefined system configuration.
|
|
86
|
+
* @param initParams - Optional {@link Config.ISystemConfigurationInitParams | initialization parameters}.
|
|
87
|
+
* @returns `Success` with the {@link Config.SystemConfiguration | SystemConfiguration}
|
|
88
|
+
* if successful, `Failure` with an error message otherwise.
|
|
89
|
+
* @public
|
|
90
|
+
*/
|
|
91
|
+
function getPredefinedSystemConfiguration(name, initParams) {
|
|
92
|
+
if (name in predefinedDecls) {
|
|
93
|
+
return systemConfiguration_1.SystemConfiguration.create(predefinedDecls[name], initParams);
|
|
94
|
+
}
|
|
95
|
+
return (0, ts_utils_1.fail)(`Unknown predefined system configuration: ${name}`);
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
import { QualifierType, SystemQualifierType } from '../qualifier-types';
|
|
3
|
+
import * as QualifierTypes from '../qualifier-types';
|
|
4
|
+
import * as ResourceTypes from '../resource-types';
|
|
5
|
+
import { ResourceType } from '../resource-types';
|
|
6
|
+
/**
|
|
7
|
+
* Function signature for creating a qualifier type from configuration.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export type QualifierTypeFactoryFunction<T extends QualifierType = QualifierType> = (config: QualifierTypes.Config.IAnyQualifierTypeConfig) => Result<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Function signature for creating a resource type from configuration.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export type ResourceTypeFactoryFunction = (config: ResourceTypes.Config.IResourceTypeConfig) => Result<ResourceType>;
|
|
16
|
+
/**
|
|
17
|
+
* Interface for a factory that creates a new instance of a configuration object.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export interface IConfigInitFactory<TConfig, T> {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new instance of a configuration object.
|
|
23
|
+
* @param config - The configuration object to create.
|
|
24
|
+
* @returns A result containing the new instance of the configuration object.
|
|
25
|
+
*/
|
|
26
|
+
create(config: TConfig): Result<T>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a {@link Config.IConfigInitFactory | IConfigInitFactory} from a factory function.
|
|
30
|
+
* @param fn - The factory function to wrap.
|
|
31
|
+
* @returns An `IConfigInitFactory` instance that delegates to the function.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export declare function createQualifierTypeFactory<T extends QualifierType = QualifierType>(fn: QualifierTypeFactoryFunction<T>): IConfigInitFactory<QualifierTypes.Config.IAnyQualifierTypeConfig, T>;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a {@link Config.IConfigInitFactory | IConfigInitFactory} from a resource type factory function.
|
|
37
|
+
* @param fn - The factory function to wrap.
|
|
38
|
+
* @returns An `IConfigInitFactory` instance that delegates to the function.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export declare function createResourceTypeFactory(fn: ResourceTypeFactoryFunction): IConfigInitFactory<ResourceTypes.Config.IResourceTypeConfig, ResourceType>;
|
|
42
|
+
/**
|
|
43
|
+
* A factory that chains multiple factories together.
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export declare class ChainedConfigInitFactory<TConfig, T> implements IConfigInitFactory<TConfig, T> {
|
|
47
|
+
readonly factories: IConfigInitFactory<TConfig, T>[];
|
|
48
|
+
/**
|
|
49
|
+
* Constructor for a chained config init factory.
|
|
50
|
+
* @param factories - The factories to chain.
|
|
51
|
+
*/
|
|
52
|
+
constructor(factories: IConfigInitFactory<TConfig, T>[]);
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new instance of a configuration object.
|
|
55
|
+
* @param config - The configuration object to create.
|
|
56
|
+
* @returns A result containing the new instance of the configuration object.
|
|
57
|
+
*/
|
|
58
|
+
create(config: TConfig): Result<T>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A factory that creates a {@link QualifierTypes.SystemQualifierType | SystemQualifierType} from
|
|
62
|
+
* {@link QualifierTypes.Config.IAnyQualifierTypeConfig | any qualifier type configuration}.
|
|
63
|
+
* @returns `Success` with the new {@link QualifierTypes.SystemQualifierType | SystemQualifierType}
|
|
64
|
+
* if successful, `Failure` with an error message otherwise.
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
export declare class BuiltInQualifierTypeFactory implements IConfigInitFactory<QualifierTypes.Config.ISystemQualifierTypeConfig, SystemQualifierType> {
|
|
68
|
+
/** {@inheritDoc Config.IConfigInitFactory.create} */
|
|
69
|
+
create(config: QualifierTypes.Config.IAnyQualifierTypeConfig): Result<SystemQualifierType>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A factory that creates {@link QualifierTypes.QualifierType | QualifierType} instances from configuration,
|
|
73
|
+
* supporting both built-in system types and custom external types.
|
|
74
|
+
*
|
|
75
|
+
* This factory allows external consumers to extend the qualifier type system with their own custom types
|
|
76
|
+
* while maintaining support for all built-in types (Language, Territory, Literal).
|
|
77
|
+
*
|
|
78
|
+
* @typeParam T - The custom qualifier type(s) to support. Defaults to {@link QualifierTypes.SystemQualifierType | SystemQualifierType}.
|
|
79
|
+
*
|
|
80
|
+
* @example Creating a factory with custom qualifier types
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // Define a custom qualifier type
|
|
83
|
+
* class CustomQualifierType extends QualifierType {
|
|
84
|
+
* // ... implementation
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* // Define a discriminated union of all types
|
|
88
|
+
* type AppQualifierType = SystemQualifierType | CustomQualifierType;
|
|
89
|
+
*
|
|
90
|
+
* // Create a factory that handles custom types
|
|
91
|
+
* const customFactory: IConfigInitFactory<IAnyQualifierTypeConfig, CustomQualifierType> = {
|
|
92
|
+
* create(config) {
|
|
93
|
+
* // ... handle custom type creation
|
|
94
|
+
* }
|
|
95
|
+
* };
|
|
96
|
+
*
|
|
97
|
+
* // Create the combined factory
|
|
98
|
+
* const qualifierTypeFactory = new QualifierTypeFactory<AppQualifierType>([customFactory]);
|
|
99
|
+
*
|
|
100
|
+
* // The factory returns T | SystemQualifierType, supporting all types
|
|
101
|
+
* const result = qualifierTypeFactory.create(config); // Result<AppQualifierType | SystemQualifierType>
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* @remarks
|
|
105
|
+
* - The factory chains custom factories with the built-in factory
|
|
106
|
+
* - Custom factories are tried first, falling back to built-in types
|
|
107
|
+
* - The return type is a union of custom types (T) and system types
|
|
108
|
+
*
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
export declare class QualifierTypeFactory<T extends QualifierType = SystemQualifierType> extends ChainedConfigInitFactory<QualifierTypes.Config.IAnyQualifierTypeConfig, T | SystemQualifierType> {
|
|
112
|
+
/**
|
|
113
|
+
* Constructor for a {@link Config.QualifierTypeFactory | qualifier type factory}.
|
|
114
|
+
* @param factories - Array of factories for custom qualifier types. Can be:
|
|
115
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
116
|
+
* - {@link Config.QualifierTypeFactoryFunction | Factory functions}
|
|
117
|
+
* - A mix of both
|
|
118
|
+
* These are tried in order before falling back to built-in types.
|
|
119
|
+
* @remarks The {@link Config.BuiltInQualifierTypeFactory | built-in factory} is always appended to handle
|
|
120
|
+
* system qualifier types (Language, Territory, Literal).
|
|
121
|
+
*/
|
|
122
|
+
constructor(factories: Array<IConfigInitFactory<QualifierTypes.Config.IAnyQualifierTypeConfig, T> | QualifierTypeFactoryFunction<T>>);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* A factory that validates and creates {@link QualifierTypes.QualifierType | QualifierType} instances
|
|
126
|
+
* from weakly-typed configuration objects. This factory accepts configurations with unvalidated
|
|
127
|
+
* string properties and validates them before delegating to the underlying factory chain.
|
|
128
|
+
*
|
|
129
|
+
* This pattern is useful at package boundaries where type identity issues may occur with
|
|
130
|
+
* branded types across different package instances.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* // Accept weakly-typed config from external source
|
|
135
|
+
* const validatingFactory = new ValidatingQualifierTypeFactory([customFactory]);
|
|
136
|
+
*
|
|
137
|
+
* // Config can have plain string types instead of branded types
|
|
138
|
+
* const config = {
|
|
139
|
+
* name: 'my-qualifier', // plain string, not QualifierTypeName
|
|
140
|
+
* systemType: 'custom', // plain string
|
|
141
|
+
* configuration: { ... }
|
|
142
|
+
* };
|
|
143
|
+
*
|
|
144
|
+
* const result = validatingFactory.create(config); // Validates and converts internally
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @public
|
|
148
|
+
*/
|
|
149
|
+
export declare class ValidatingQualifierTypeFactory<T extends QualifierType = SystemQualifierType> implements IConfigInitFactory<unknown, T | SystemQualifierType> {
|
|
150
|
+
private readonly _innerFactory;
|
|
151
|
+
/**
|
|
152
|
+
* Constructor for a validating qualifier type factory.
|
|
153
|
+
* @param factories - Array of factories for custom qualifier types. Can be:
|
|
154
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
155
|
+
* - {@link Config.QualifierTypeFactoryFunction | Factory functions}
|
|
156
|
+
* - A mix of both
|
|
157
|
+
*/
|
|
158
|
+
constructor(factories: Array<IConfigInitFactory<QualifierTypes.Config.IAnyQualifierTypeConfig, T> | QualifierTypeFactoryFunction<T>>);
|
|
159
|
+
/**
|
|
160
|
+
* Creates a qualifier type from a weakly-typed configuration object.
|
|
161
|
+
* @param config - The configuration object to validate and use for creation.
|
|
162
|
+
* @returns A result containing the new qualifier type if successful.
|
|
163
|
+
*/
|
|
164
|
+
create(config: unknown): Result<T | SystemQualifierType>;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* A factory that creates a {@link ResourceTypes.ResourceType | ResourceType} from a {@link ResourceTypes.Config.IResourceTypeConfig | resource type configuration}.
|
|
168
|
+
* @public
|
|
169
|
+
*/
|
|
170
|
+
export declare class BuiltInResourceTypeFactory implements IConfigInitFactory<ResourceTypes.Config.IResourceTypeConfig, ResourceType> {
|
|
171
|
+
/** {@inheritDoc Config.IConfigInitFactory.create} */
|
|
172
|
+
create(config: ResourceTypes.Config.IResourceTypeConfig): Result<ResourceType>;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* A factory that creates a {@link ResourceTypes.ResourceType | ResourceType} from a {@link ResourceTypes.Config.IResourceTypeConfig | resource type configuration}
|
|
176
|
+
* by chaining a supplied factory with a {@link Config.BuiltInResourceTypeFactory | built-in factory} that handles built-in resource types.
|
|
177
|
+
* @public
|
|
178
|
+
*/
|
|
179
|
+
export declare class ResourceTypeFactory extends ChainedConfigInitFactory<ResourceTypes.Config.IResourceTypeConfig, ResourceType> {
|
|
180
|
+
/**
|
|
181
|
+
* Constructor for a resource type factory.
|
|
182
|
+
* @param factories - Array of factories for resource types. Can be:
|
|
183
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
184
|
+
* - {@link Config.ResourceTypeFactoryFunction | Factory functions}
|
|
185
|
+
* - A mix of both
|
|
186
|
+
* @remarks The {@link Config.BuiltInResourceTypeFactory | built-in factory} is always added to the end of the chain.
|
|
187
|
+
*/
|
|
188
|
+
constructor(factories: Array<IConfigInitFactory<ResourceTypes.Config.IResourceTypeConfig, ResourceType> | ResourceTypeFactoryFunction>);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* A factory that validates and creates {@link ResourceTypes.ResourceType | ResourceType} instances
|
|
192
|
+
* from weakly-typed configuration objects. This factory accepts configurations with unvalidated
|
|
193
|
+
* string properties and validates them before delegating to the underlying factory chain.
|
|
194
|
+
*
|
|
195
|
+
* This pattern is useful at package boundaries where type identity issues may occur with
|
|
196
|
+
* branded types across different package instances.
|
|
197
|
+
*
|
|
198
|
+
* @public
|
|
199
|
+
*/
|
|
200
|
+
export declare class ValidatingResourceTypeFactory implements IConfigInitFactory<unknown, ResourceType> {
|
|
201
|
+
private readonly _innerFactory;
|
|
202
|
+
/**
|
|
203
|
+
* Constructor for a validating resource type factory.
|
|
204
|
+
* @param factories - Array of factories for resource types. Can be:
|
|
205
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
206
|
+
* - {@link Config.ResourceTypeFactoryFunction | Factory functions}
|
|
207
|
+
* - A mix of both
|
|
208
|
+
*/
|
|
209
|
+
constructor(factories: Array<IConfigInitFactory<ResourceTypes.Config.IResourceTypeConfig, ResourceType> | ResourceTypeFactoryFunction>);
|
|
210
|
+
/**
|
|
211
|
+
* Creates a resource type from a weakly-typed configuration object.
|
|
212
|
+
* @param config - The configuration object to validate and use for creation.
|
|
213
|
+
* @returns A result containing the new resource type if successful.
|
|
214
|
+
*/
|
|
215
|
+
create(config: unknown): Result<ResourceType>;
|
|
216
|
+
}
|
|
217
|
+
//# sourceMappingURL=configInitFactory.d.ts.map
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
+
}
|
|
29
|
+
Object.defineProperty(o, k2, desc);
|
|
30
|
+
}) : (function(o, m, k, k2) {
|
|
31
|
+
if (k2 === undefined) k2 = k;
|
|
32
|
+
o[k2] = m[k];
|
|
33
|
+
}));
|
|
34
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
35
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
36
|
+
}) : function(o, v) {
|
|
37
|
+
o["default"] = v;
|
|
38
|
+
});
|
|
39
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
40
|
+
var ownKeys = function(o) {
|
|
41
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
42
|
+
var ar = [];
|
|
43
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
44
|
+
return ar;
|
|
45
|
+
};
|
|
46
|
+
return ownKeys(o);
|
|
47
|
+
};
|
|
48
|
+
return function (mod) {
|
|
49
|
+
if (mod && mod.__esModule) return mod;
|
|
50
|
+
var result = {};
|
|
51
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
52
|
+
__setModuleDefault(result, mod);
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
})();
|
|
56
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
+
exports.ValidatingResourceTypeFactory = exports.ResourceTypeFactory = exports.BuiltInResourceTypeFactory = exports.ValidatingQualifierTypeFactory = exports.QualifierTypeFactory = exports.BuiltInQualifierTypeFactory = exports.ChainedConfigInitFactory = void 0;
|
|
58
|
+
exports.createQualifierTypeFactory = createQualifierTypeFactory;
|
|
59
|
+
exports.createResourceTypeFactory = createResourceTypeFactory;
|
|
60
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
61
|
+
const QualifierTypes = __importStar(require("../qualifier-types"));
|
|
62
|
+
const ResourceTypes = __importStar(require("../resource-types"));
|
|
63
|
+
/**
|
|
64
|
+
* Creates a {@link Config.IConfigInitFactory | IConfigInitFactory} from a factory function.
|
|
65
|
+
* @param fn - The factory function to wrap.
|
|
66
|
+
* @returns An `IConfigInitFactory` instance that delegates to the function.
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
function createQualifierTypeFactory(fn) {
|
|
70
|
+
return {
|
|
71
|
+
create: fn
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a {@link Config.IConfigInitFactory | IConfigInitFactory} from a resource type factory function.
|
|
76
|
+
* @param fn - The factory function to wrap.
|
|
77
|
+
* @returns An `IConfigInitFactory` instance that delegates to the function.
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
function createResourceTypeFactory(fn) {
|
|
81
|
+
return {
|
|
82
|
+
create: fn
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A factory that chains multiple factories together.
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
class ChainedConfigInitFactory {
|
|
90
|
+
/**
|
|
91
|
+
* Constructor for a chained config init factory.
|
|
92
|
+
* @param factories - The factories to chain.
|
|
93
|
+
*/
|
|
94
|
+
constructor(factories) {
|
|
95
|
+
this.factories = factories;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Creates a new instance of a configuration object.
|
|
99
|
+
* @param config - The configuration object to create.
|
|
100
|
+
* @returns A result containing the new instance of the configuration object.
|
|
101
|
+
*/
|
|
102
|
+
create(config) {
|
|
103
|
+
for (const factory of this.factories) {
|
|
104
|
+
const result = factory.create(config);
|
|
105
|
+
if (result.isSuccess()) {
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return (0, ts_utils_1.fail)('No factory was able to create the configuration object');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.ChainedConfigInitFactory = ChainedConfigInitFactory;
|
|
113
|
+
/**
|
|
114
|
+
* A factory that creates a {@link QualifierTypes.SystemQualifierType | SystemQualifierType} from
|
|
115
|
+
* {@link QualifierTypes.Config.IAnyQualifierTypeConfig | any qualifier type configuration}.
|
|
116
|
+
* @returns `Success` with the new {@link QualifierTypes.SystemQualifierType | SystemQualifierType}
|
|
117
|
+
* if successful, `Failure` with an error message otherwise.
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
class BuiltInQualifierTypeFactory {
|
|
121
|
+
/** {@inheritDoc Config.IConfigInitFactory.create} */
|
|
122
|
+
create(config) {
|
|
123
|
+
if (QualifierTypes.Config.isSystemQualifierTypeConfig(config)) {
|
|
124
|
+
return QualifierTypes.createQualifierTypeFromSystemConfig(config);
|
|
125
|
+
}
|
|
126
|
+
/* c8 ignore next 2 - functional code tested but coverage intermittently missed */
|
|
127
|
+
return (0, ts_utils_1.fail)(`${config.name}: unknown built-in qualifier type (${config.systemType})`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.BuiltInQualifierTypeFactory = BuiltInQualifierTypeFactory;
|
|
131
|
+
/**
|
|
132
|
+
* A factory that creates {@link QualifierTypes.QualifierType | QualifierType} instances from configuration,
|
|
133
|
+
* supporting both built-in system types and custom external types.
|
|
134
|
+
*
|
|
135
|
+
* This factory allows external consumers to extend the qualifier type system with their own custom types
|
|
136
|
+
* while maintaining support for all built-in types (Language, Territory, Literal).
|
|
137
|
+
*
|
|
138
|
+
* @typeParam T - The custom qualifier type(s) to support. Defaults to {@link QualifierTypes.SystemQualifierType | SystemQualifierType}.
|
|
139
|
+
*
|
|
140
|
+
* @example Creating a factory with custom qualifier types
|
|
141
|
+
* ```typescript
|
|
142
|
+
* // Define a custom qualifier type
|
|
143
|
+
* class CustomQualifierType extends QualifierType {
|
|
144
|
+
* // ... implementation
|
|
145
|
+
* }
|
|
146
|
+
*
|
|
147
|
+
* // Define a discriminated union of all types
|
|
148
|
+
* type AppQualifierType = SystemQualifierType | CustomQualifierType;
|
|
149
|
+
*
|
|
150
|
+
* // Create a factory that handles custom types
|
|
151
|
+
* const customFactory: IConfigInitFactory<IAnyQualifierTypeConfig, CustomQualifierType> = {
|
|
152
|
+
* create(config) {
|
|
153
|
+
* // ... handle custom type creation
|
|
154
|
+
* }
|
|
155
|
+
* };
|
|
156
|
+
*
|
|
157
|
+
* // Create the combined factory
|
|
158
|
+
* const qualifierTypeFactory = new QualifierTypeFactory<AppQualifierType>([customFactory]);
|
|
159
|
+
*
|
|
160
|
+
* // The factory returns T | SystemQualifierType, supporting all types
|
|
161
|
+
* const result = qualifierTypeFactory.create(config); // Result<AppQualifierType | SystemQualifierType>
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* @remarks
|
|
165
|
+
* - The factory chains custom factories with the built-in factory
|
|
166
|
+
* - Custom factories are tried first, falling back to built-in types
|
|
167
|
+
* - The return type is a union of custom types (T) and system types
|
|
168
|
+
*
|
|
169
|
+
* @public
|
|
170
|
+
*/
|
|
171
|
+
class QualifierTypeFactory extends ChainedConfigInitFactory {
|
|
172
|
+
/**
|
|
173
|
+
* Constructor for a {@link Config.QualifierTypeFactory | qualifier type factory}.
|
|
174
|
+
* @param factories - Array of factories for custom qualifier types. Can be:
|
|
175
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
176
|
+
* - {@link Config.QualifierTypeFactoryFunction | Factory functions}
|
|
177
|
+
* - A mix of both
|
|
178
|
+
* These are tried in order before falling back to built-in types.
|
|
179
|
+
* @remarks The {@link Config.BuiltInQualifierTypeFactory | built-in factory} is always appended to handle
|
|
180
|
+
* system qualifier types (Language, Territory, Literal).
|
|
181
|
+
*/
|
|
182
|
+
constructor(factories) {
|
|
183
|
+
const normalizedFactories = factories.map((f) => typeof f === 'function' ? createQualifierTypeFactory(f) : f);
|
|
184
|
+
super([...normalizedFactories, new BuiltInQualifierTypeFactory()]);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.QualifierTypeFactory = QualifierTypeFactory;
|
|
188
|
+
/**
|
|
189
|
+
* A factory that validates and creates {@link QualifierTypes.QualifierType | QualifierType} instances
|
|
190
|
+
* from weakly-typed configuration objects. This factory accepts configurations with unvalidated
|
|
191
|
+
* string properties and validates them before delegating to the underlying factory chain.
|
|
192
|
+
*
|
|
193
|
+
* This pattern is useful at package boundaries where type identity issues may occur with
|
|
194
|
+
* branded types across different package instances.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```typescript
|
|
198
|
+
* // Accept weakly-typed config from external source
|
|
199
|
+
* const validatingFactory = new ValidatingQualifierTypeFactory([customFactory]);
|
|
200
|
+
*
|
|
201
|
+
* // Config can have plain string types instead of branded types
|
|
202
|
+
* const config = {
|
|
203
|
+
* name: 'my-qualifier', // plain string, not QualifierTypeName
|
|
204
|
+
* systemType: 'custom', // plain string
|
|
205
|
+
* configuration: { ... }
|
|
206
|
+
* };
|
|
207
|
+
*
|
|
208
|
+
* const result = validatingFactory.create(config); // Validates and converts internally
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* @public
|
|
212
|
+
*/
|
|
213
|
+
class ValidatingQualifierTypeFactory {
|
|
214
|
+
/**
|
|
215
|
+
* Constructor for a validating qualifier type factory.
|
|
216
|
+
* @param factories - Array of factories for custom qualifier types. Can be:
|
|
217
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
218
|
+
* - {@link Config.QualifierTypeFactoryFunction | Factory functions}
|
|
219
|
+
* - A mix of both
|
|
220
|
+
*/
|
|
221
|
+
constructor(factories) {
|
|
222
|
+
this._innerFactory = new QualifierTypeFactory(factories);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Creates a qualifier type from a weakly-typed configuration object.
|
|
226
|
+
* @param config - The configuration object to validate and use for creation.
|
|
227
|
+
* @returns A result containing the new qualifier type if successful.
|
|
228
|
+
*/
|
|
229
|
+
create(config) {
|
|
230
|
+
return QualifierTypes.Config.Convert.anyQualifierTypeConfig
|
|
231
|
+
.convert(config)
|
|
232
|
+
.onSuccess((validatedConfig) => {
|
|
233
|
+
return this._innerFactory.create(validatedConfig);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
exports.ValidatingQualifierTypeFactory = ValidatingQualifierTypeFactory;
|
|
238
|
+
/**
|
|
239
|
+
* A factory that creates a {@link ResourceTypes.ResourceType | ResourceType} from a {@link ResourceTypes.Config.IResourceTypeConfig | resource type configuration}.
|
|
240
|
+
* @public
|
|
241
|
+
*/
|
|
242
|
+
class BuiltInResourceTypeFactory {
|
|
243
|
+
/** {@inheritDoc Config.IConfigInitFactory.create} */
|
|
244
|
+
create(config) {
|
|
245
|
+
return ResourceTypes.createResourceTypeFromConfig(config);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
exports.BuiltInResourceTypeFactory = BuiltInResourceTypeFactory;
|
|
249
|
+
/**
|
|
250
|
+
* A factory that creates a {@link ResourceTypes.ResourceType | ResourceType} from a {@link ResourceTypes.Config.IResourceTypeConfig | resource type configuration}
|
|
251
|
+
* by chaining a supplied factory with a {@link Config.BuiltInResourceTypeFactory | built-in factory} that handles built-in resource types.
|
|
252
|
+
* @public
|
|
253
|
+
*/
|
|
254
|
+
class ResourceTypeFactory extends ChainedConfigInitFactory {
|
|
255
|
+
/**
|
|
256
|
+
* Constructor for a resource type factory.
|
|
257
|
+
* @param factories - Array of factories for resource types. Can be:
|
|
258
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
259
|
+
* - {@link Config.ResourceTypeFactoryFunction | Factory functions}
|
|
260
|
+
* - A mix of both
|
|
261
|
+
* @remarks The {@link Config.BuiltInResourceTypeFactory | built-in factory} is always added to the end of the chain.
|
|
262
|
+
*/
|
|
263
|
+
constructor(factories) {
|
|
264
|
+
factories = factories !== null && factories !== void 0 ? factories : [];
|
|
265
|
+
const normalizedFactories = factories.map((f) => typeof f === 'function' ? createResourceTypeFactory(f) : f);
|
|
266
|
+
super([...normalizedFactories, new BuiltInResourceTypeFactory()]);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
exports.ResourceTypeFactory = ResourceTypeFactory;
|
|
270
|
+
/**
|
|
271
|
+
* A factory that validates and creates {@link ResourceTypes.ResourceType | ResourceType} instances
|
|
272
|
+
* from weakly-typed configuration objects. This factory accepts configurations with unvalidated
|
|
273
|
+
* string properties and validates them before delegating to the underlying factory chain.
|
|
274
|
+
*
|
|
275
|
+
* This pattern is useful at package boundaries where type identity issues may occur with
|
|
276
|
+
* branded types across different package instances.
|
|
277
|
+
*
|
|
278
|
+
* @public
|
|
279
|
+
*/
|
|
280
|
+
class ValidatingResourceTypeFactory {
|
|
281
|
+
/**
|
|
282
|
+
* Constructor for a validating resource type factory.
|
|
283
|
+
* @param factories - Array of factories for resource types. Can be:
|
|
284
|
+
* - {@link Config.IConfigInitFactory | IConfigInitFactory} instances
|
|
285
|
+
* - {@link Config.ResourceTypeFactoryFunction | Factory functions}
|
|
286
|
+
* - A mix of both
|
|
287
|
+
*/
|
|
288
|
+
constructor(factories) {
|
|
289
|
+
this._innerFactory = new ResourceTypeFactory(factories);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Creates a resource type from a weakly-typed configuration object.
|
|
293
|
+
* @param config - The configuration object to validate and use for creation.
|
|
294
|
+
* @returns A result containing the new resource type if successful.
|
|
295
|
+
*/
|
|
296
|
+
create(config) {
|
|
297
|
+
return ResourceTypes.Config.Convert.resourceTypeConfig.convert(config).onSuccess((validatedConfig) => {
|
|
298
|
+
return this._innerFactory.create(validatedConfig);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
exports.ValidatingResourceTypeFactory = ValidatingResourceTypeFactory;
|
|
303
|
+
//# sourceMappingURL=configInitFactory.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
import { ISystemConfiguration } from './json';
|
|
3
|
+
/**
|
|
4
|
+
* A `Converter` for {@link Config.Model.ISystemConfiguration | ISystemConfiguration} objects.
|
|
5
|
+
* @returns A `Converter` for {@link Config.Model.ISystemConfiguration | ISystemConfiguration} objects.
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export declare const systemConfiguration: import("@fgv/ts-utils").ObjectConverter<ISystemConfiguration, unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* A `Converter` for {@link Config.PredefinedSystemConfiguration | PredefinedSystemConfiguration} values.
|
|
11
|
+
* @returns A `Converter` for {@link Config.PredefinedSystemConfiguration | PredefinedSystemConfiguration} values.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export declare const predefinedSystemConfiguration: import("@fgv/ts-utils").Converter<import("./common").PredefinedSystemConfiguration, readonly import("./common").PredefinedSystemConfiguration[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Validate a {@link Config.Model.ISystemConfiguration | ISystemConfiguration} object.
|
|
17
|
+
* @param config - The system configuration to validate
|
|
18
|
+
* @returns `Success` with the validated system configuration if successful,
|
|
19
|
+
* or `Failure` with an error message if validation fails.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export declare function validateSystemConfiguration(config: unknown): Result<ISystemConfiguration>;
|
|
23
|
+
//# sourceMappingURL=convert.d.ts.map
|