@twin.org/context 0.0.3-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +21 -0
- package/dist/es/factories/contextIdHandlerFactory.js +9 -0
- package/dist/es/factories/contextIdHandlerFactory.js.map +1 -0
- package/dist/es/helpers/contextIdHelper.js +144 -0
- package/dist/es/helpers/contextIdHelper.js.map +1 -0
- package/dist/es/index.js +9 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IContextIdHandler.js +4 -0
- package/dist/es/models/IContextIdHandler.js.map +1 -0
- package/dist/es/models/IContextIds.js +4 -0
- package/dist/es/models/IContextIds.js.map +1 -0
- package/dist/es/models/contextIdKeys.js +25 -0
- package/dist/es/models/contextIdKeys.js.map +1 -0
- package/dist/es/utils/contextIdStore.js +49 -0
- package/dist/es/utils/contextIdStore.js.map +1 -0
- package/dist/types/factories/contextIdHandlerFactory.d.ts +6 -0
- package/dist/types/helpers/contextIdHelper.d.ts +79 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/models/IContextIdHandler.d.ts +17 -0
- package/dist/types/models/IContextIds.d.ts +9 -0
- package/dist/types/models/contextIdKeys.d.ts +25 -0
- package/dist/types/utils/contextIdStore.d.ts +31 -0
- package/docs/changelog.md +22 -0
- package/docs/examples.md +1 -0
- package/docs/reference/classes/ContextIdHelper.md +295 -0
- package/docs/reference/classes/ContextIdStore.md +69 -0
- package/docs/reference/index.md +20 -0
- package/docs/reference/interfaces/IContextIdHandler.md +49 -0
- package/docs/reference/interfaces/IContextIds.md +9 -0
- package/docs/reference/type-aliases/ContextIdKeys.md +5 -0
- package/docs/reference/variables/ContextIdHandlerFactory.md +5 -0
- package/docs/reference/variables/ContextIdKeys.md +31 -0
- package/locales/en.json +11 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 IOTA Stiftung.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# TWIN Framework Context
|
|
2
|
+
|
|
3
|
+
This package contains helper methods and classes for using contexts.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install @twin.org/context
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
Usage of the APIs is shown in the examples [docs/examples.md](docs/examples.md)
|
|
14
|
+
|
|
15
|
+
## Reference
|
|
16
|
+
|
|
17
|
+
Detailed reference documentation for the API can be found in [docs/reference/index.md](docs/reference/index.md)
|
|
18
|
+
|
|
19
|
+
## Changelog
|
|
20
|
+
|
|
21
|
+
The changes between each version can be found in [docs/changelog.md](docs/changelog.md)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { Factory } from "@twin.org/core";
|
|
4
|
+
/**
|
|
5
|
+
* Factory for creating implementation of context ID handlers.
|
|
6
|
+
*/
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
8
|
+
export const ContextIdHandlerFactory = Factory.createFactory("contextIdHandler");
|
|
9
|
+
//# sourceMappingURL=contextIdHandlerFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextIdHandlerFactory.js","sourceRoot":"","sources":["../../../src/factories/contextIdHandlerFactory.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAGzC;;GAEG;AACH,gEAAgE;AAChE,MAAM,CAAC,MAAM,uBAAuB,GAAG,OAAO,CAAC,aAAa,CAAoB,kBAAkB,CAAC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Factory } from \"@twin.org/core\";\nimport type { IContextIdHandler } from \"../models/IContextIdHandler.js\";\n\n/**\n * Factory for creating implementation of context ID handlers.\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const ContextIdHandlerFactory = Factory.createFactory<IContextIdHandler>(\"contextIdHandler\");\n"]}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { GeneralError, Guards, Is } from "@twin.org/core";
|
|
4
|
+
import { ContextIdHandlerFactory } from "../factories/contextIdHandlerFactory.js";
|
|
5
|
+
/**
|
|
6
|
+
* Class to help with context IDs.
|
|
7
|
+
*/
|
|
8
|
+
export class ContextIdHelper {
|
|
9
|
+
/**
|
|
10
|
+
* Runtime name for the class.
|
|
11
|
+
*/
|
|
12
|
+
static CLASS_NAME = "ContextIdHelper";
|
|
13
|
+
/**
|
|
14
|
+
* Perform a runtime guard on the provided context ID value.
|
|
15
|
+
* @param contextIds The context IDs to guard.
|
|
16
|
+
* @param key The context ID key to guard.
|
|
17
|
+
* @throws Guard error if the value is invalid.
|
|
18
|
+
*/
|
|
19
|
+
static guard(contextIds, key) {
|
|
20
|
+
Guards.stringValue(ContextIdHelper.CLASS_NAME, "key", key);
|
|
21
|
+
if (Is.undefined(contextIds?.[key])) {
|
|
22
|
+
throw new GeneralError(ContextIdHelper.CLASS_NAME, "contextIdMissing", { key });
|
|
23
|
+
}
|
|
24
|
+
const handler = ContextIdHandlerFactory.getIfExists(key);
|
|
25
|
+
if (handler?.guard) {
|
|
26
|
+
handler.guard(contextIds[key]);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
Guards.stringValue(ContextIdHelper.CLASS_NAME, `contextIds.${key}`, contextIds[key]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Gets the short version of a context ID.
|
|
34
|
+
* @param contextIds The context IDs to get the short version from.
|
|
35
|
+
* @param key The context ID key to get the short version for.
|
|
36
|
+
* @returns The short version of the context ID.
|
|
37
|
+
* @throws Guard error if the value is invalid.
|
|
38
|
+
*/
|
|
39
|
+
static short(contextIds, key) {
|
|
40
|
+
Guards.stringValue(ContextIdHelper.CLASS_NAME, "key", key);
|
|
41
|
+
if (Is.undefined(contextIds?.[key])) {
|
|
42
|
+
throw new GeneralError(ContextIdHelper.CLASS_NAME, "contextIdMissing", { key });
|
|
43
|
+
}
|
|
44
|
+
const handler = ContextIdHandlerFactory.getIfExists(key);
|
|
45
|
+
if (handler?.short) {
|
|
46
|
+
return handler.short(contextIds[key]);
|
|
47
|
+
}
|
|
48
|
+
return contextIds[key];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Perform a runtime guard on the provided context ID values.
|
|
52
|
+
* @param contextIds The context IDs to guard.
|
|
53
|
+
* @param keys The context ID keys to guard.
|
|
54
|
+
* @throws Guard error if the value is invalid.
|
|
55
|
+
*/
|
|
56
|
+
static guardAll(contextIds, keys) {
|
|
57
|
+
if (Is.arrayValue(keys)) {
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
ContextIdHelper.guard(contextIds, key);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Gets the short versions of multiple context IDs.
|
|
65
|
+
* @param contextIds The context IDs to get the short versions from.
|
|
66
|
+
* @param keys The context ID keys to get the short versions for.
|
|
67
|
+
* @returns The short versions of the context IDs.
|
|
68
|
+
*/
|
|
69
|
+
static shortAll(contextIds, keys) {
|
|
70
|
+
const short = {};
|
|
71
|
+
if (Is.arrayValue(keys)) {
|
|
72
|
+
for (const key of keys) {
|
|
73
|
+
short[key] = ContextIdHelper.short(contextIds, key);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return short;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Gets the combined short version.
|
|
80
|
+
* @param contextIds The context IDs to get the short versions from.
|
|
81
|
+
* @param keys The context ID keys to get the short versions for.
|
|
82
|
+
* @param separator The separator to use between parts.
|
|
83
|
+
* @returns The short version combined.
|
|
84
|
+
*/
|
|
85
|
+
static shortCombined(contextIds, keys, separator = "/") {
|
|
86
|
+
const short = ContextIdHelper.shortAll(contextIds, keys);
|
|
87
|
+
if (Object.keys(short).length === 0) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
return Object.values(short).join(separator);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Split a combined short version in to the separate context IDs.
|
|
94
|
+
* @param keys The context ID keys to get the short versions for.
|
|
95
|
+
* @param combined The combined short version to separate.
|
|
96
|
+
* @param separator The separator used between parts.
|
|
97
|
+
* @returns The short version combined.
|
|
98
|
+
* @throws GeneralError if the number of parts does not match the number of keys.
|
|
99
|
+
*/
|
|
100
|
+
static shortSplit(keys, combined, separator = "/") {
|
|
101
|
+
Guards.arrayValue(ContextIdHelper.CLASS_NAME, "keys", keys);
|
|
102
|
+
Guards.stringValue(ContextIdHelper.CLASS_NAME, "combined", combined);
|
|
103
|
+
const parts = combined.split(separator);
|
|
104
|
+
const result = {};
|
|
105
|
+
if (parts.length !== keys.length) {
|
|
106
|
+
throw new GeneralError(ContextIdHelper.CLASS_NAME, "contextIdSplitMismatch", {
|
|
107
|
+
expected: keys.length,
|
|
108
|
+
actual: parts.length
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
for (let i = 0; i < keys.length; i++) {
|
|
112
|
+
result[keys[i]] = parts[i];
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Create a combined key.
|
|
118
|
+
* @param contextIds The context IDs to create the combined key for.
|
|
119
|
+
* @param keys The context ID keys to get the short versions for.
|
|
120
|
+
* @param separator The separator to use between parts.
|
|
121
|
+
* @returns The short version combined.
|
|
122
|
+
*/
|
|
123
|
+
static combinedContextKey(contextIds, keys, separator = "/") {
|
|
124
|
+
ContextIdHelper.guardAll(contextIds, keys);
|
|
125
|
+
const short = ContextIdHelper.shortAll(contextIds, keys);
|
|
126
|
+
if (Object.keys(short).length === 0) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
return Object.values(short).join(separator);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Pick only the desired keys from the available keys.
|
|
133
|
+
* @param availableKeys The available keys to pick from.
|
|
134
|
+
* @param desiredKeys The desired keys to pick.
|
|
135
|
+
* @returns The picked keys.
|
|
136
|
+
*/
|
|
137
|
+
static pickKeysFromAvailable(availableKeys, desiredKeys) {
|
|
138
|
+
if (!Is.arrayValue(availableKeys) || !Is.arrayValue(desiredKeys)) {
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
return desiredKeys.filter(key => availableKeys.includes(key));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=contextIdHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextIdHelper.js","sourceRoot":"","sources":["../../../src/helpers/contextIdHelper.ts"],"names":[],"mappings":"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","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { ContextIdHandlerFactory } from \"../factories/contextIdHandlerFactory.js\";\nimport type { IContextIds } from \"../models/IContextIds.js\";\n\n/**\n * Class to help with context IDs.\n */\nexport class ContextIdHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ContextIdHelper>();\n\n\t/**\n\t * Perform a runtime guard on the provided context ID value.\n\t * @param contextIds The context IDs to guard.\n\t * @param key The context ID key to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static guard<T extends { [k: string]: string | undefined }, K extends string>(\n\t\tcontextIds: T | undefined,\n\t\tkey: K\n\t): asserts contextIds is T & { [P in K]: string } {\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(key), key);\n\n\t\tif (Is.undefined(contextIds?.[key])) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdMissing\", { key });\n\t\t}\n\n\t\tconst handler = ContextIdHandlerFactory.getIfExists(key);\n\t\tif (handler?.guard) {\n\t\t\thandler.guard(contextIds[key]);\n\t\t} else {\n\t\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, `contextIds.${key}`, contextIds[key]);\n\t\t}\n\t}\n\n\t/**\n\t * Gets the short version of a context ID.\n\t * @param contextIds The context IDs to get the short version from.\n\t * @param key The context ID key to get the short version for.\n\t * @returns The short version of the context ID.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static short(contextIds: IContextIds | undefined, key: string): string {\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(key), key);\n\t\tif (Is.undefined(contextIds?.[key])) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdMissing\", { key });\n\t\t}\n\n\t\tconst handler = ContextIdHandlerFactory.getIfExists(key);\n\t\tif (handler?.short) {\n\t\t\treturn handler.short(contextIds[key]);\n\t\t}\n\t\treturn contextIds[key];\n\t}\n\n\t/**\n\t * Perform a runtime guard on the provided context ID values.\n\t * @param contextIds The context IDs to guard.\n\t * @param keys The context ID keys to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static guardAll<T extends { [k: string]: string | undefined }, K extends string>(\n\t\tcontextIds: T | undefined,\n\t\tkeys: readonly K[] | undefined\n\t): asserts contextIds is T & { [P in K]: string } {\n\t\tif (Is.arrayValue(keys)) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tContextIdHelper.guard(contextIds, key);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Gets the short versions of multiple context IDs.\n\t * @param contextIds The context IDs to get the short versions from.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @returns The short versions of the context IDs.\n\t */\n\tpublic static shortAll(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined\n\t): IContextIds {\n\t\tconst short: IContextIds = {};\n\t\tif (Is.arrayValue(keys)) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tshort[key] = ContextIdHelper.short(contextIds, key);\n\t\t\t}\n\t\t}\n\t\treturn short;\n\t}\n\n\t/**\n\t * Gets the combined short version.\n\t * @param contextIds The context IDs to get the short versions from.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param separator The separator to use between parts.\n\t * @returns The short version combined.\n\t */\n\tpublic static shortCombined(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined,\n\t\tseparator: string = \"/\"\n\t): string | undefined {\n\t\tconst short = ContextIdHelper.shortAll(contextIds, keys);\n\t\tif (Object.keys(short).length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn Object.values(short).join(separator);\n\t}\n\n\t/**\n\t * Split a combined short version in to the separate context IDs.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param combined The combined short version to separate.\n\t * @param separator The separator used between parts.\n\t * @returns The short version combined.\n\t * @throws GeneralError if the number of parts does not match the number of keys.\n\t */\n\tpublic static shortSplit(keys: string[], combined: string, separator: string = \"/\"): IContextIds {\n\t\tGuards.arrayValue<string>(ContextIdHelper.CLASS_NAME, nameof(keys), keys);\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(combined), combined);\n\n\t\tconst parts = combined.split(separator);\n\t\tconst result: IContextIds = {};\n\n\t\tif (parts.length !== keys.length) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdSplitMismatch\", {\n\t\t\t\texpected: keys.length,\n\t\t\t\tactual: parts.length\n\t\t\t});\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tresult[keys[i]] = parts[i];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Create a combined key.\n\t * @param contextIds The context IDs to create the combined key for.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param separator The separator to use between parts.\n\t * @returns The short version combined.\n\t */\n\tpublic static combinedContextKey(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined,\n\t\tseparator: string = \"/\"\n\t): string | undefined {\n\t\tContextIdHelper.guardAll(contextIds, keys);\n\n\t\tconst short = ContextIdHelper.shortAll(contextIds, keys);\n\n\t\tif (Object.keys(short).length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn Object.values(short).join(separator);\n\t}\n\n\t/**\n\t * Pick only the desired keys from the available keys.\n\t * @param availableKeys The available keys to pick from.\n\t * @param desiredKeys The desired keys to pick.\n\t * @returns The picked keys.\n\t */\n\tpublic static pickKeysFromAvailable(availableKeys?: string[], desiredKeys?: string[]): string[] {\n\t\tif (!Is.arrayValue(availableKeys) || !Is.arrayValue(desiredKeys)) {\n\t\t\treturn [];\n\t\t}\n\n\t\treturn desiredKeys.filter(key => availableKeys.includes(key));\n\t}\n}\n"]}
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
export * from "./factories/contextIdHandlerFactory.js";
|
|
4
|
+
export * from "./helpers/contextIdHelper.js";
|
|
5
|
+
export * from "./models/contextIdKeys.js";
|
|
6
|
+
export * from "./models/IContextIdHandler.js";
|
|
7
|
+
export * from "./models/IContextIds.js";
|
|
8
|
+
export * from "./utils/contextIdStore.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,wCAAwC,CAAC;AACvD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./factories/contextIdHandlerFactory.js\";\nexport * from \"./helpers/contextIdHelper.js\";\nexport * from \"./models/contextIdKeys.js\";\nexport * from \"./models/IContextIdHandler.js\";\nexport * from \"./models/IContextIds.js\";\nexport * from \"./utils/contextIdStore.js\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IContextIdHandler.js","sourceRoot":"","sources":["../../../src/models/IContextIdHandler.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Interface describing a context ID handler.\n */\nexport interface IContextIdHandler {\n\t/**\n\t * The short form version of the context ID, should be unique enough to partition data.\n\t * @param value The full context ID value.\n\t * @returns The short form version of the context ID.\n\t */\n\tshort?(value: string): string;\n\n\t/**\n\t * Performs a runtime guard on the provided context ID value.\n\t * @param value The context ID value to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tguard?(value: string): void;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IContextIds.js","sourceRoot":"","sources":["../../../src/models/IContextIds.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Interface describing context ids which can be accessed async from a request.\n */\nexport interface IContextIds {\n\t/**\n\t * The context id keys and values.\n\t */\n\t[id: string]: string | undefined;\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
/**
|
|
4
|
+
* Default definition of some context keys.
|
|
5
|
+
*/
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
7
|
+
export const ContextIdKeys = {
|
|
8
|
+
/**
|
|
9
|
+
* Standard property type definition for node.
|
|
10
|
+
*/
|
|
11
|
+
Node: "node",
|
|
12
|
+
/**
|
|
13
|
+
* Standard property type definition for tenant.
|
|
14
|
+
*/
|
|
15
|
+
Tenant: "tenant",
|
|
16
|
+
/**
|
|
17
|
+
* Standard property type definition for organization.
|
|
18
|
+
*/
|
|
19
|
+
Organization: "organization",
|
|
20
|
+
/**
|
|
21
|
+
* Standard property type definition for user.
|
|
22
|
+
*/
|
|
23
|
+
User: "user"
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=contextIdKeys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextIdKeys.js","sourceRoot":"","sources":["../../../src/models/contextIdKeys.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AAEvC;;GAEG;AACH,gEAAgE;AAChE,MAAM,CAAC,MAAM,aAAa,GAAG;IAC5B;;OAEG;IACH,IAAI,EAAE,MAAM;IAEZ;;OAEG;IACH,MAAM,EAAE,QAAQ;IAEhB;;OAEG;IACH,YAAY,EAAE,cAAc;IAE5B;;OAEG;IACH,IAAI,EAAE,MAAM;CACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Default definition of some context keys.\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const ContextIdKeys = {\n\t/**\n\t * Standard property type definition for node.\n\t */\n\tNode: \"node\",\n\n\t/**\n\t * Standard property type definition for tenant.\n\t */\n\tTenant: \"tenant\",\n\n\t/**\n\t * Standard property type definition for organization.\n\t */\n\tOrganization: \"organization\",\n\n\t/**\n\t * Standard property type definition for user.\n\t */\n\tUser: \"user\"\n} as const;\n\n/**\n * Default definition of some context keys.\n */\nexport type ContextIdKeys = (typeof ContextIdKeys)[keyof typeof ContextIdKeys];\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { BaseError, GeneralError } from "@twin.org/core";
|
|
2
|
+
/**
|
|
3
|
+
* Class to maintain context ids and execute an async method.
|
|
4
|
+
*/
|
|
5
|
+
export class ContextIdStore {
|
|
6
|
+
/**
|
|
7
|
+
* Runtime name for the class.
|
|
8
|
+
*/
|
|
9
|
+
static CLASS_NAME = "ContextIdStore";
|
|
10
|
+
/**
|
|
11
|
+
* The async local storage for the context ids.
|
|
12
|
+
*/
|
|
13
|
+
static _storage;
|
|
14
|
+
/**
|
|
15
|
+
* Execute the method wrapped in the context.
|
|
16
|
+
* @param contextIds The context IDs.
|
|
17
|
+
* @param asyncMethod The async method to run.
|
|
18
|
+
* @returns Nothing.
|
|
19
|
+
*/
|
|
20
|
+
static async run(contextIds, asyncMethod) {
|
|
21
|
+
return (await ContextIdStore.createStorage()).run(contextIds, asyncMethod);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get the context IDs.
|
|
25
|
+
* @returns The context IDs.
|
|
26
|
+
*/
|
|
27
|
+
static async getContextIds() {
|
|
28
|
+
return (await ContextIdStore.createStorage()).getStore();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create the storage if it doesn't exist.
|
|
32
|
+
* @returns The storage.
|
|
33
|
+
*/
|
|
34
|
+
static async createStorage() {
|
|
35
|
+
if (!ContextIdStore._storage) {
|
|
36
|
+
try {
|
|
37
|
+
const hooks = await import("node:async_hooks");
|
|
38
|
+
ContextIdStore._storage = new hooks.AsyncLocalStorage({
|
|
39
|
+
name: "AsyncContextIdsStorage"
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
throw new GeneralError(ContextIdStore.CLASS_NAME, "asyncHooksNotAvailable", undefined, BaseError.fromError(err));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return ContextIdStore._storage;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=contextIdStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextIdStore.js","sourceRoot":"","sources":["../../../src/utils/contextIdStore.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAIzD;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;OAEG;IACI,MAAM,CAAU,UAAU,oBAAoC;IAErE;;OAEG;IACK,MAAM,CAAC,QAAQ,CAAiC;IAExD;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAc,UAAuB,EAAE,WAAoB;QACjF,OAAO,CAAC,MAAM,cAAc,CAAC,aAAa,EAAE,CAAC,CAAC,GAAG,CAAI,UAAU,EAAE,WAAW,CAAC,CAAC;IAC/E,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK,CAAC,aAAa;QAChC,OAAO,CAAC,MAAM,cAAc,CAAC,aAAa,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;IAC1D,CAAC;IAED;;;OAGG;IACK,MAAM,CAAC,KAAK,CAAC,aAAa;QACjC,IAAI,CAAC,cAAc,CAAC,QAAQ,EAAE,CAAC;YAC9B,IAAI,CAAC;gBACJ,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;gBAC/C,cAAc,CAAC,QAAQ,GAAG,IAAI,KAAK,CAAC,iBAAiB,CAAc;oBAClE,IAAI,EAAE,wBAAwB;iBAC9B,CAAC,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,MAAM,IAAI,YAAY,CACrB,cAAc,CAAC,UAAU,EACzB,wBAAwB,EACxB,SAAS,EACT,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CACxB,CAAC;YACH,CAAC;QACF,CAAC;QACD,OAAO,cAAc,CAAC,QAAQ,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { AsyncLocalStorage } from \"node:async_hooks\";\nimport { BaseError, GeneralError } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IContextIds } from \"../models/IContextIds.js\";\n\n/**\n * Class to maintain context ids and execute an async method.\n */\nexport class ContextIdStore {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ContextIdStore>();\n\n\t/**\n\t * The async local storage for the context ids.\n\t */\n\tprivate static _storage: AsyncLocalStorage<IContextIds>;\n\n\t/**\n\t * Execute the method wrapped in the context.\n\t * @param contextIds The context IDs.\n\t * @param asyncMethod The async method to run.\n\t * @returns Nothing.\n\t */\n\tpublic static async run<T = unknown>(contextIds: IContextIds, asyncMethod: () => T): Promise<T> {\n\t\treturn (await ContextIdStore.createStorage()).run<T>(contextIds, asyncMethod);\n\t}\n\n\t/**\n\t * Get the context IDs.\n\t * @returns The context IDs.\n\t */\n\tpublic static async getContextIds(): Promise<IContextIds | undefined> {\n\t\treturn (await ContextIdStore.createStorage()).getStore();\n\t}\n\n\t/**\n\t * Create the storage if it doesn't exist.\n\t * @returns The storage.\n\t */\n\tprivate static async createStorage(): Promise<AsyncLocalStorage<IContextIds>> {\n\t\tif (!ContextIdStore._storage) {\n\t\t\ttry {\n\t\t\t\tconst hooks = await import(\"node:async_hooks\");\n\t\t\t\tContextIdStore._storage = new hooks.AsyncLocalStorage<IContextIds>({\n\t\t\t\t\tname: \"AsyncContextIdsStorage\"\n\t\t\t\t});\n\t\t\t} catch (err) {\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tContextIdStore.CLASS_NAME,\n\t\t\t\t\t\"asyncHooksNotAvailable\",\n\t\t\t\t\tundefined,\n\t\t\t\t\tBaseError.fromError(err)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn ContextIdStore._storage;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { IContextIds } from "../models/IContextIds.js";
|
|
2
|
+
/**
|
|
3
|
+
* Class to help with context IDs.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ContextIdHelper {
|
|
6
|
+
/**
|
|
7
|
+
* Runtime name for the class.
|
|
8
|
+
*/
|
|
9
|
+
static readonly CLASS_NAME: string;
|
|
10
|
+
/**
|
|
11
|
+
* Perform a runtime guard on the provided context ID value.
|
|
12
|
+
* @param contextIds The context IDs to guard.
|
|
13
|
+
* @param key The context ID key to guard.
|
|
14
|
+
* @throws Guard error if the value is invalid.
|
|
15
|
+
*/
|
|
16
|
+
static guard<T extends {
|
|
17
|
+
[k: string]: string | undefined;
|
|
18
|
+
}, K extends string>(contextIds: T | undefined, key: K): asserts contextIds is T & {
|
|
19
|
+
[P in K]: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Gets the short version of a context ID.
|
|
23
|
+
* @param contextIds The context IDs to get the short version from.
|
|
24
|
+
* @param key The context ID key to get the short version for.
|
|
25
|
+
* @returns The short version of the context ID.
|
|
26
|
+
* @throws Guard error if the value is invalid.
|
|
27
|
+
*/
|
|
28
|
+
static short(contextIds: IContextIds | undefined, key: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* Perform a runtime guard on the provided context ID values.
|
|
31
|
+
* @param contextIds The context IDs to guard.
|
|
32
|
+
* @param keys The context ID keys to guard.
|
|
33
|
+
* @throws Guard error if the value is invalid.
|
|
34
|
+
*/
|
|
35
|
+
static guardAll<T extends {
|
|
36
|
+
[k: string]: string | undefined;
|
|
37
|
+
}, K extends string>(contextIds: T | undefined, keys: readonly K[] | undefined): asserts contextIds is T & {
|
|
38
|
+
[P in K]: string;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Gets the short versions of multiple context IDs.
|
|
42
|
+
* @param contextIds The context IDs to get the short versions from.
|
|
43
|
+
* @param keys The context ID keys to get the short versions for.
|
|
44
|
+
* @returns The short versions of the context IDs.
|
|
45
|
+
*/
|
|
46
|
+
static shortAll(contextIds: IContextIds | undefined, keys: string[] | undefined): IContextIds;
|
|
47
|
+
/**
|
|
48
|
+
* Gets the combined short version.
|
|
49
|
+
* @param contextIds The context IDs to get the short versions from.
|
|
50
|
+
* @param keys The context ID keys to get the short versions for.
|
|
51
|
+
* @param separator The separator to use between parts.
|
|
52
|
+
* @returns The short version combined.
|
|
53
|
+
*/
|
|
54
|
+
static shortCombined(contextIds: IContextIds | undefined, keys: string[] | undefined, separator?: string): string | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Split a combined short version in to the separate context IDs.
|
|
57
|
+
* @param keys The context ID keys to get the short versions for.
|
|
58
|
+
* @param combined The combined short version to separate.
|
|
59
|
+
* @param separator The separator used between parts.
|
|
60
|
+
* @returns The short version combined.
|
|
61
|
+
* @throws GeneralError if the number of parts does not match the number of keys.
|
|
62
|
+
*/
|
|
63
|
+
static shortSplit(keys: string[], combined: string, separator?: string): IContextIds;
|
|
64
|
+
/**
|
|
65
|
+
* Create a combined key.
|
|
66
|
+
* @param contextIds The context IDs to create the combined key for.
|
|
67
|
+
* @param keys The context ID keys to get the short versions for.
|
|
68
|
+
* @param separator The separator to use between parts.
|
|
69
|
+
* @returns The short version combined.
|
|
70
|
+
*/
|
|
71
|
+
static combinedContextKey(contextIds: IContextIds | undefined, keys: string[] | undefined, separator?: string): string | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Pick only the desired keys from the available keys.
|
|
74
|
+
* @param availableKeys The available keys to pick from.
|
|
75
|
+
* @param desiredKeys The desired keys to pick.
|
|
76
|
+
* @returns The picked keys.
|
|
77
|
+
*/
|
|
78
|
+
static pickKeysFromAvailable(availableKeys?: string[], desiredKeys?: string[]): string[];
|
|
79
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from "./factories/contextIdHandlerFactory.js";
|
|
2
|
+
export * from "./helpers/contextIdHelper.js";
|
|
3
|
+
export * from "./models/contextIdKeys.js";
|
|
4
|
+
export * from "./models/IContextIdHandler.js";
|
|
5
|
+
export * from "./models/IContextIds.js";
|
|
6
|
+
export * from "./utils/contextIdStore.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface describing a context ID handler.
|
|
3
|
+
*/
|
|
4
|
+
export interface IContextIdHandler {
|
|
5
|
+
/**
|
|
6
|
+
* The short form version of the context ID, should be unique enough to partition data.
|
|
7
|
+
* @param value The full context ID value.
|
|
8
|
+
* @returns The short form version of the context ID.
|
|
9
|
+
*/
|
|
10
|
+
short?(value: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Performs a runtime guard on the provided context ID value.
|
|
13
|
+
* @param value The context ID value to guard.
|
|
14
|
+
* @throws Guard error if the value is invalid.
|
|
15
|
+
*/
|
|
16
|
+
guard?(value: string): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default definition of some context keys.
|
|
3
|
+
*/
|
|
4
|
+
export declare const ContextIdKeys: {
|
|
5
|
+
/**
|
|
6
|
+
* Standard property type definition for node.
|
|
7
|
+
*/
|
|
8
|
+
readonly Node: "node";
|
|
9
|
+
/**
|
|
10
|
+
* Standard property type definition for tenant.
|
|
11
|
+
*/
|
|
12
|
+
readonly Tenant: "tenant";
|
|
13
|
+
/**
|
|
14
|
+
* Standard property type definition for organization.
|
|
15
|
+
*/
|
|
16
|
+
readonly Organization: "organization";
|
|
17
|
+
/**
|
|
18
|
+
* Standard property type definition for user.
|
|
19
|
+
*/
|
|
20
|
+
readonly User: "user";
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Default definition of some context keys.
|
|
24
|
+
*/
|
|
25
|
+
export type ContextIdKeys = (typeof ContextIdKeys)[keyof typeof ContextIdKeys];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { IContextIds } from "../models/IContextIds.js";
|
|
2
|
+
/**
|
|
3
|
+
* Class to maintain context ids and execute an async method.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ContextIdStore {
|
|
6
|
+
/**
|
|
7
|
+
* Runtime name for the class.
|
|
8
|
+
*/
|
|
9
|
+
static readonly CLASS_NAME: string;
|
|
10
|
+
/**
|
|
11
|
+
* The async local storage for the context ids.
|
|
12
|
+
*/
|
|
13
|
+
private static _storage;
|
|
14
|
+
/**
|
|
15
|
+
* Execute the method wrapped in the context.
|
|
16
|
+
* @param contextIds The context IDs.
|
|
17
|
+
* @param asyncMethod The async method to run.
|
|
18
|
+
* @returns Nothing.
|
|
19
|
+
*/
|
|
20
|
+
static run<T = unknown>(contextIds: IContextIds, asyncMethod: () => T): Promise<T>;
|
|
21
|
+
/**
|
|
22
|
+
* Get the context IDs.
|
|
23
|
+
* @returns The context IDs.
|
|
24
|
+
*/
|
|
25
|
+
static getContextIds(): Promise<IContextIds | undefined>;
|
|
26
|
+
/**
|
|
27
|
+
* Create the storage if it doesn't exist.
|
|
28
|
+
* @returns The storage.
|
|
29
|
+
*/
|
|
30
|
+
private static createStorage;
|
|
31
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.0.3-next.1](https://github.com/twinfoundation/framework/compare/context-v0.0.3-next.0...context-v0.0.3-next.1) (2025-11-10)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add context id features ([#206](https://github.com/twinfoundation/framework/issues/206)) ([ef0d4ee](https://github.com/twinfoundation/framework/commit/ef0d4ee11a4f5fc6cc6f52a4958ce905c04ee13b))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/core bumped from 0.0.3-next.0 to 0.0.3-next.1
|
|
16
|
+
* @twin.org/nameof bumped from 0.0.3-next.0 to 0.0.3-next.1
|
|
17
|
+
* devDependencies
|
|
18
|
+
* @twin.org/nameof-transformer bumped from 0.0.3-next.0 to 0.0.3-next.1
|
|
19
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.3-next.0 to 0.0.3-next.1
|
|
20
|
+
* @twin.org/validate-locales bumped from 0.0.3-next.0 to 0.0.3-next.1
|
|
21
|
+
|
|
22
|
+
## @twin.org/context - Changelog
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @twin.org/context - Examples
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# Class: ContextIdHelper
|
|
2
|
+
|
|
3
|
+
Class to help with context IDs.
|
|
4
|
+
|
|
5
|
+
## Constructors
|
|
6
|
+
|
|
7
|
+
### Constructor
|
|
8
|
+
|
|
9
|
+
> **new ContextIdHelper**(): `ContextIdHelper`
|
|
10
|
+
|
|
11
|
+
#### Returns
|
|
12
|
+
|
|
13
|
+
`ContextIdHelper`
|
|
14
|
+
|
|
15
|
+
## Properties
|
|
16
|
+
|
|
17
|
+
### CLASS\_NAME
|
|
18
|
+
|
|
19
|
+
> `readonly` `static` **CLASS\_NAME**: `string`
|
|
20
|
+
|
|
21
|
+
Runtime name for the class.
|
|
22
|
+
|
|
23
|
+
## Methods
|
|
24
|
+
|
|
25
|
+
### guard()
|
|
26
|
+
|
|
27
|
+
> `static` **guard**\<`T`, `K`\>(`contextIds`, `key`): `asserts contextIds is T & { [P in string]: string }`
|
|
28
|
+
|
|
29
|
+
Perform a runtime guard on the provided context ID value.
|
|
30
|
+
|
|
31
|
+
#### Type Parameters
|
|
32
|
+
|
|
33
|
+
##### T
|
|
34
|
+
|
|
35
|
+
`T` *extends* `object`
|
|
36
|
+
|
|
37
|
+
##### K
|
|
38
|
+
|
|
39
|
+
`K` *extends* `string`
|
|
40
|
+
|
|
41
|
+
#### Parameters
|
|
42
|
+
|
|
43
|
+
##### contextIds
|
|
44
|
+
|
|
45
|
+
The context IDs to guard.
|
|
46
|
+
|
|
47
|
+
`T` | `undefined`
|
|
48
|
+
|
|
49
|
+
##### key
|
|
50
|
+
|
|
51
|
+
`K`
|
|
52
|
+
|
|
53
|
+
The context ID key to guard.
|
|
54
|
+
|
|
55
|
+
#### Returns
|
|
56
|
+
|
|
57
|
+
`asserts contextIds is T & { [P in string]: string }`
|
|
58
|
+
|
|
59
|
+
#### Throws
|
|
60
|
+
|
|
61
|
+
Guard error if the value is invalid.
|
|
62
|
+
|
|
63
|
+
***
|
|
64
|
+
|
|
65
|
+
### short()
|
|
66
|
+
|
|
67
|
+
> `static` **short**(`contextIds`, `key`): `string`
|
|
68
|
+
|
|
69
|
+
Gets the short version of a context ID.
|
|
70
|
+
|
|
71
|
+
#### Parameters
|
|
72
|
+
|
|
73
|
+
##### contextIds
|
|
74
|
+
|
|
75
|
+
The context IDs to get the short version from.
|
|
76
|
+
|
|
77
|
+
[`IContextIds`](../interfaces/IContextIds.md) | `undefined`
|
|
78
|
+
|
|
79
|
+
##### key
|
|
80
|
+
|
|
81
|
+
`string`
|
|
82
|
+
|
|
83
|
+
The context ID key to get the short version for.
|
|
84
|
+
|
|
85
|
+
#### Returns
|
|
86
|
+
|
|
87
|
+
`string`
|
|
88
|
+
|
|
89
|
+
The short version of the context ID.
|
|
90
|
+
|
|
91
|
+
#### Throws
|
|
92
|
+
|
|
93
|
+
Guard error if the value is invalid.
|
|
94
|
+
|
|
95
|
+
***
|
|
96
|
+
|
|
97
|
+
### guardAll()
|
|
98
|
+
|
|
99
|
+
> `static` **guardAll**\<`T`, `K`\>(`contextIds`, `keys`): `asserts contextIds is T & { [P in string]: string }`
|
|
100
|
+
|
|
101
|
+
Perform a runtime guard on the provided context ID values.
|
|
102
|
+
|
|
103
|
+
#### Type Parameters
|
|
104
|
+
|
|
105
|
+
##### T
|
|
106
|
+
|
|
107
|
+
`T` *extends* `object`
|
|
108
|
+
|
|
109
|
+
##### K
|
|
110
|
+
|
|
111
|
+
`K` *extends* `string`
|
|
112
|
+
|
|
113
|
+
#### Parameters
|
|
114
|
+
|
|
115
|
+
##### contextIds
|
|
116
|
+
|
|
117
|
+
The context IDs to guard.
|
|
118
|
+
|
|
119
|
+
`T` | `undefined`
|
|
120
|
+
|
|
121
|
+
##### keys
|
|
122
|
+
|
|
123
|
+
The context ID keys to guard.
|
|
124
|
+
|
|
125
|
+
readonly `K`[] | `undefined`
|
|
126
|
+
|
|
127
|
+
#### Returns
|
|
128
|
+
|
|
129
|
+
`asserts contextIds is T & { [P in string]: string }`
|
|
130
|
+
|
|
131
|
+
#### Throws
|
|
132
|
+
|
|
133
|
+
Guard error if the value is invalid.
|
|
134
|
+
|
|
135
|
+
***
|
|
136
|
+
|
|
137
|
+
### shortAll()
|
|
138
|
+
|
|
139
|
+
> `static` **shortAll**(`contextIds`, `keys`): [`IContextIds`](../interfaces/IContextIds.md)
|
|
140
|
+
|
|
141
|
+
Gets the short versions of multiple context IDs.
|
|
142
|
+
|
|
143
|
+
#### Parameters
|
|
144
|
+
|
|
145
|
+
##### contextIds
|
|
146
|
+
|
|
147
|
+
The context IDs to get the short versions from.
|
|
148
|
+
|
|
149
|
+
[`IContextIds`](../interfaces/IContextIds.md) | `undefined`
|
|
150
|
+
|
|
151
|
+
##### keys
|
|
152
|
+
|
|
153
|
+
The context ID keys to get the short versions for.
|
|
154
|
+
|
|
155
|
+
`string`[] | `undefined`
|
|
156
|
+
|
|
157
|
+
#### Returns
|
|
158
|
+
|
|
159
|
+
[`IContextIds`](../interfaces/IContextIds.md)
|
|
160
|
+
|
|
161
|
+
The short versions of the context IDs.
|
|
162
|
+
|
|
163
|
+
***
|
|
164
|
+
|
|
165
|
+
### shortCombined()
|
|
166
|
+
|
|
167
|
+
> `static` **shortCombined**(`contextIds`, `keys`, `separator`): `string` \| `undefined`
|
|
168
|
+
|
|
169
|
+
Gets the combined short version.
|
|
170
|
+
|
|
171
|
+
#### Parameters
|
|
172
|
+
|
|
173
|
+
##### contextIds
|
|
174
|
+
|
|
175
|
+
The context IDs to get the short versions from.
|
|
176
|
+
|
|
177
|
+
[`IContextIds`](../interfaces/IContextIds.md) | `undefined`
|
|
178
|
+
|
|
179
|
+
##### keys
|
|
180
|
+
|
|
181
|
+
The context ID keys to get the short versions for.
|
|
182
|
+
|
|
183
|
+
`string`[] | `undefined`
|
|
184
|
+
|
|
185
|
+
##### separator
|
|
186
|
+
|
|
187
|
+
`string` = `"/"`
|
|
188
|
+
|
|
189
|
+
The separator to use between parts.
|
|
190
|
+
|
|
191
|
+
#### Returns
|
|
192
|
+
|
|
193
|
+
`string` \| `undefined`
|
|
194
|
+
|
|
195
|
+
The short version combined.
|
|
196
|
+
|
|
197
|
+
***
|
|
198
|
+
|
|
199
|
+
### shortSplit()
|
|
200
|
+
|
|
201
|
+
> `static` **shortSplit**(`keys`, `combined`, `separator`): [`IContextIds`](../interfaces/IContextIds.md)
|
|
202
|
+
|
|
203
|
+
Split a combined short version in to the separate context IDs.
|
|
204
|
+
|
|
205
|
+
#### Parameters
|
|
206
|
+
|
|
207
|
+
##### keys
|
|
208
|
+
|
|
209
|
+
`string`[]
|
|
210
|
+
|
|
211
|
+
The context ID keys to get the short versions for.
|
|
212
|
+
|
|
213
|
+
##### combined
|
|
214
|
+
|
|
215
|
+
`string`
|
|
216
|
+
|
|
217
|
+
The combined short version to separate.
|
|
218
|
+
|
|
219
|
+
##### separator
|
|
220
|
+
|
|
221
|
+
`string` = `"/"`
|
|
222
|
+
|
|
223
|
+
The separator used between parts.
|
|
224
|
+
|
|
225
|
+
#### Returns
|
|
226
|
+
|
|
227
|
+
[`IContextIds`](../interfaces/IContextIds.md)
|
|
228
|
+
|
|
229
|
+
The short version combined.
|
|
230
|
+
|
|
231
|
+
#### Throws
|
|
232
|
+
|
|
233
|
+
GeneralError if the number of parts does not match the number of keys.
|
|
234
|
+
|
|
235
|
+
***
|
|
236
|
+
|
|
237
|
+
### combinedContextKey()
|
|
238
|
+
|
|
239
|
+
> `static` **combinedContextKey**(`contextIds`, `keys`, `separator`): `string` \| `undefined`
|
|
240
|
+
|
|
241
|
+
Create a combined key.
|
|
242
|
+
|
|
243
|
+
#### Parameters
|
|
244
|
+
|
|
245
|
+
##### contextIds
|
|
246
|
+
|
|
247
|
+
The context IDs to create the combined key for.
|
|
248
|
+
|
|
249
|
+
[`IContextIds`](../interfaces/IContextIds.md) | `undefined`
|
|
250
|
+
|
|
251
|
+
##### keys
|
|
252
|
+
|
|
253
|
+
The context ID keys to get the short versions for.
|
|
254
|
+
|
|
255
|
+
`string`[] | `undefined`
|
|
256
|
+
|
|
257
|
+
##### separator
|
|
258
|
+
|
|
259
|
+
`string` = `"/"`
|
|
260
|
+
|
|
261
|
+
The separator to use between parts.
|
|
262
|
+
|
|
263
|
+
#### Returns
|
|
264
|
+
|
|
265
|
+
`string` \| `undefined`
|
|
266
|
+
|
|
267
|
+
The short version combined.
|
|
268
|
+
|
|
269
|
+
***
|
|
270
|
+
|
|
271
|
+
### pickKeysFromAvailable()
|
|
272
|
+
|
|
273
|
+
> `static` **pickKeysFromAvailable**(`availableKeys?`, `desiredKeys?`): `string`[]
|
|
274
|
+
|
|
275
|
+
Pick only the desired keys from the available keys.
|
|
276
|
+
|
|
277
|
+
#### Parameters
|
|
278
|
+
|
|
279
|
+
##### availableKeys?
|
|
280
|
+
|
|
281
|
+
`string`[]
|
|
282
|
+
|
|
283
|
+
The available keys to pick from.
|
|
284
|
+
|
|
285
|
+
##### desiredKeys?
|
|
286
|
+
|
|
287
|
+
`string`[]
|
|
288
|
+
|
|
289
|
+
The desired keys to pick.
|
|
290
|
+
|
|
291
|
+
#### Returns
|
|
292
|
+
|
|
293
|
+
`string`[]
|
|
294
|
+
|
|
295
|
+
The picked keys.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Class: ContextIdStore
|
|
2
|
+
|
|
3
|
+
Class to maintain context ids and execute an async method.
|
|
4
|
+
|
|
5
|
+
## Constructors
|
|
6
|
+
|
|
7
|
+
### Constructor
|
|
8
|
+
|
|
9
|
+
> **new ContextIdStore**(): `ContextIdStore`
|
|
10
|
+
|
|
11
|
+
#### Returns
|
|
12
|
+
|
|
13
|
+
`ContextIdStore`
|
|
14
|
+
|
|
15
|
+
## Properties
|
|
16
|
+
|
|
17
|
+
### CLASS\_NAME
|
|
18
|
+
|
|
19
|
+
> `readonly` `static` **CLASS\_NAME**: `string`
|
|
20
|
+
|
|
21
|
+
Runtime name for the class.
|
|
22
|
+
|
|
23
|
+
## Methods
|
|
24
|
+
|
|
25
|
+
### run()
|
|
26
|
+
|
|
27
|
+
> `static` **run**\<`T`\>(`contextIds`, `asyncMethod`): `Promise`\<`T`\>
|
|
28
|
+
|
|
29
|
+
Execute the method wrapped in the context.
|
|
30
|
+
|
|
31
|
+
#### Type Parameters
|
|
32
|
+
|
|
33
|
+
##### T
|
|
34
|
+
|
|
35
|
+
`T` = `unknown`
|
|
36
|
+
|
|
37
|
+
#### Parameters
|
|
38
|
+
|
|
39
|
+
##### contextIds
|
|
40
|
+
|
|
41
|
+
[`IContextIds`](../interfaces/IContextIds.md)
|
|
42
|
+
|
|
43
|
+
The context IDs.
|
|
44
|
+
|
|
45
|
+
##### asyncMethod
|
|
46
|
+
|
|
47
|
+
() => `T`
|
|
48
|
+
|
|
49
|
+
The async method to run.
|
|
50
|
+
|
|
51
|
+
#### Returns
|
|
52
|
+
|
|
53
|
+
`Promise`\<`T`\>
|
|
54
|
+
|
|
55
|
+
Nothing.
|
|
56
|
+
|
|
57
|
+
***
|
|
58
|
+
|
|
59
|
+
### getContextIds()
|
|
60
|
+
|
|
61
|
+
> `static` **getContextIds**(): `Promise`\<[`IContextIds`](../interfaces/IContextIds.md) \| `undefined`\>
|
|
62
|
+
|
|
63
|
+
Get the context IDs.
|
|
64
|
+
|
|
65
|
+
#### Returns
|
|
66
|
+
|
|
67
|
+
`Promise`\<[`IContextIds`](../interfaces/IContextIds.md) \| `undefined`\>
|
|
68
|
+
|
|
69
|
+
The context IDs.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @twin.org/context
|
|
2
|
+
|
|
3
|
+
## Classes
|
|
4
|
+
|
|
5
|
+
- [ContextIdHelper](classes/ContextIdHelper.md)
|
|
6
|
+
- [ContextIdStore](classes/ContextIdStore.md)
|
|
7
|
+
|
|
8
|
+
## Interfaces
|
|
9
|
+
|
|
10
|
+
- [IContextIdHandler](interfaces/IContextIdHandler.md)
|
|
11
|
+
- [IContextIds](interfaces/IContextIds.md)
|
|
12
|
+
|
|
13
|
+
## Type Aliases
|
|
14
|
+
|
|
15
|
+
- [ContextIdKeys](type-aliases/ContextIdKeys.md)
|
|
16
|
+
|
|
17
|
+
## Variables
|
|
18
|
+
|
|
19
|
+
- [ContextIdHandlerFactory](variables/ContextIdHandlerFactory.md)
|
|
20
|
+
- [ContextIdKeys](variables/ContextIdKeys.md)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Interface: IContextIdHandler
|
|
2
|
+
|
|
3
|
+
Interface describing a context ID handler.
|
|
4
|
+
|
|
5
|
+
## Methods
|
|
6
|
+
|
|
7
|
+
### short()?
|
|
8
|
+
|
|
9
|
+
> `optional` **short**(`value`): `string`
|
|
10
|
+
|
|
11
|
+
The short form version of the context ID, should be unique enough to partition data.
|
|
12
|
+
|
|
13
|
+
#### Parameters
|
|
14
|
+
|
|
15
|
+
##### value
|
|
16
|
+
|
|
17
|
+
`string`
|
|
18
|
+
|
|
19
|
+
The full context ID value.
|
|
20
|
+
|
|
21
|
+
#### Returns
|
|
22
|
+
|
|
23
|
+
`string`
|
|
24
|
+
|
|
25
|
+
The short form version of the context ID.
|
|
26
|
+
|
|
27
|
+
***
|
|
28
|
+
|
|
29
|
+
### guard()?
|
|
30
|
+
|
|
31
|
+
> `optional` **guard**(`value`): `void`
|
|
32
|
+
|
|
33
|
+
Performs a runtime guard on the provided context ID value.
|
|
34
|
+
|
|
35
|
+
#### Parameters
|
|
36
|
+
|
|
37
|
+
##### value
|
|
38
|
+
|
|
39
|
+
`string`
|
|
40
|
+
|
|
41
|
+
The context ID value to guard.
|
|
42
|
+
|
|
43
|
+
#### Returns
|
|
44
|
+
|
|
45
|
+
`void`
|
|
46
|
+
|
|
47
|
+
#### Throws
|
|
48
|
+
|
|
49
|
+
Guard error if the value is invalid.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Variable: ContextIdKeys
|
|
2
|
+
|
|
3
|
+
> `const` **ContextIdKeys**: `object`
|
|
4
|
+
|
|
5
|
+
Default definition of some context keys.
|
|
6
|
+
|
|
7
|
+
## Type Declaration
|
|
8
|
+
|
|
9
|
+
### Node
|
|
10
|
+
|
|
11
|
+
> `readonly` **Node**: `"node"` = `"node"`
|
|
12
|
+
|
|
13
|
+
Standard property type definition for node.
|
|
14
|
+
|
|
15
|
+
### Tenant
|
|
16
|
+
|
|
17
|
+
> `readonly` **Tenant**: `"tenant"` = `"tenant"`
|
|
18
|
+
|
|
19
|
+
Standard property type definition for tenant.
|
|
20
|
+
|
|
21
|
+
### Organization
|
|
22
|
+
|
|
23
|
+
> `readonly` **Organization**: `"organization"` = `"organization"`
|
|
24
|
+
|
|
25
|
+
Standard property type definition for organization.
|
|
26
|
+
|
|
27
|
+
### User
|
|
28
|
+
|
|
29
|
+
> `readonly` **User**: `"user"` = `"user"`
|
|
30
|
+
|
|
31
|
+
Standard property type definition for user.
|
package/locales/en.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"error": {
|
|
3
|
+
"contextIdHelper": {
|
|
4
|
+
"contextIdMissing": "The context ID \"{key}\" is missing from the provided context IDs",
|
|
5
|
+
"contextIdSplitMismatch": "The context ID split parts length \"{actual}\" does not match the provided keys length \"{expected}\""
|
|
6
|
+
},
|
|
7
|
+
"contextIdStore": {
|
|
8
|
+
"asyncHooksNotAvailable": "Async Hooks are not available in this environment, please ensure you are running in Node.js"
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@twin.org/context",
|
|
3
|
+
"version": "0.0.3-next.1",
|
|
4
|
+
"description": "Helper methods/classes for context handling",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/twinfoundation/framework.git",
|
|
8
|
+
"directory": "packages/context"
|
|
9
|
+
},
|
|
10
|
+
"author": "martyn.janes@iota.org",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20.0.0"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@twin.org/core": "0.0.3-next.1",
|
|
18
|
+
"@twin.org/nameof": "0.0.3-next.1"
|
|
19
|
+
},
|
|
20
|
+
"main": "./dist/es/index.js",
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/types/index.d.ts",
|
|
25
|
+
"import": "./dist/es/index.js",
|
|
26
|
+
"default": "./dist/es/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./locales/*.json": "./locales/*.json"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist/es",
|
|
32
|
+
"dist/types",
|
|
33
|
+
"locales",
|
|
34
|
+
"docs"
|
|
35
|
+
],
|
|
36
|
+
"keywords": [
|
|
37
|
+
"twin",
|
|
38
|
+
"trade",
|
|
39
|
+
"iota",
|
|
40
|
+
"framework",
|
|
41
|
+
"blockchain",
|
|
42
|
+
"context",
|
|
43
|
+
"foundation",
|
|
44
|
+
"utilities"
|
|
45
|
+
],
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "git+https://github.com/twinfoundation/framework/issues"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://twindev.org"
|
|
50
|
+
}
|