@aidc-toolkit/core 1.0.32-beta → 1.0.34-beta
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/README.md +99 -92
- package/dist/app-data-storage.d.ts +118 -0
- package/dist/app-data-storage.d.ts.map +1 -0
- package/dist/app-data-storage.js +117 -0
- package/dist/app-data-storage.js.map +1 -0
- package/dist/app-data.d.ts +26 -0
- package/dist/app-data.d.ts.map +1 -0
- package/dist/app-data.js +79 -0
- package/dist/app-data.js.map +1 -0
- package/dist/browser-app-data-storage.d.ts +26 -0
- package/dist/browser-app-data-storage.d.ts.map +1 -0
- package/dist/browser-app-data-storage.js +34 -0
- package/dist/browser-app-data-storage.js.map +1 -0
- package/dist/file-app-data-storage.d.ts +26 -0
- package/dist/file-app-data-storage.d.ts.map +1 -0
- package/dist/file-app-data-storage.js +40 -0
- package/dist/file-app-data-storage.js.map +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -1
- package/dist/local-app-data-storage.d.ts +8 -0
- package/dist/local-app-data-storage.d.ts.map +1 -0
- package/dist/local-app-data-storage.js +11 -0
- package/dist/local-app-data-storage.js.map +1 -0
- package/dist/locale/en/locale-resources.d.ts +3 -0
- package/dist/locale/en/locale-resources.d.ts.map +1 -1
- package/dist/locale/en/locale-resources.js +3 -0
- package/dist/locale/en/locale-resources.js.map +1 -1
- package/dist/locale/fr/locale-resources.d.ts +3 -0
- package/dist/locale/fr/locale-resources.d.ts.map +1 -1
- package/dist/locale/fr/locale-resources.js +3 -0
- package/dist/locale/fr/locale-resources.js.map +1 -1
- package/dist/locale/i18n.d.ts +22 -13
- package/dist/locale/i18n.d.ts.map +1 -1
- package/dist/locale/i18n.js +62 -43
- package/dist/locale/i18n.js.map +1 -1
- package/dist/logger.d.ts +4 -2
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +9 -3
- package/dist/logger.js.map +1 -1
- package/dist/parse-version.d.ts +36 -0
- package/dist/parse-version.d.ts.map +1 -0
- package/dist/parse-version.js +23 -0
- package/dist/parse-version.js.map +1 -0
- package/dist/phase-url.d.ts +17 -0
- package/dist/phase-url.d.ts.map +1 -0
- package/dist/phase-url.js +39 -0
- package/dist/phase-url.js.map +1 -0
- package/dist/remote-app-data-storage.d.ts +18 -0
- package/dist/remote-app-data-storage.d.ts.map +1 -0
- package/dist/remote-app-data-storage.js +37 -0
- package/dist/remote-app-data-storage.js.map +1 -0
- package/dist/type-helper.js.map +1 -1
- package/package.json +11 -8
- package/src/app-data-storage.ts +166 -0
- package/src/app-data.ts +94 -0
- package/src/browser-app-data-storage.ts +37 -0
- package/src/file-app-data-storage.ts +49 -0
- package/src/index.ts +10 -1
- package/src/local-app-data-storage.ts +12 -0
- package/src/locale/en/locale-resources.ts +3 -0
- package/src/locale/fr/locale-resources.ts +3 -0
- package/src/locale/i18n.ts +81 -49
- package/src/logger.ts +10 -3
- package/src/parse-version.ts +54 -0
- package/src/phase-url.ts +42 -0
- package/src/remote-app-data-storage.ts +38 -0
- package/src/type-helper.ts +2 -2
package/src/locale/i18n.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import i18next, {
|
|
1
|
+
import i18next, {
|
|
2
|
+
type i18n,
|
|
3
|
+
type LanguageDetectorModule,
|
|
4
|
+
type Module,
|
|
5
|
+
type Newable,
|
|
6
|
+
type NewableModule,
|
|
7
|
+
type Resource
|
|
8
|
+
} from "i18next";
|
|
2
9
|
import I18nextBrowserLanguageDetector from "i18next-browser-languagedetector";
|
|
3
10
|
import I18nextCLILanguageDetector from "i18next-cli-language-detector";
|
|
4
11
|
import enLocaleResources from "./en/locale-resources.js";
|
|
@@ -63,9 +70,9 @@ export const coreNS = "aidct_core";
|
|
|
63
70
|
export type CoreLocaleResources = typeof enLocaleResources;
|
|
64
71
|
|
|
65
72
|
/**
|
|
66
|
-
* Core
|
|
73
|
+
* Core resource bundle.
|
|
67
74
|
*/
|
|
68
|
-
export const
|
|
75
|
+
export const coreResourceBundle: Resource = {
|
|
69
76
|
en: {
|
|
70
77
|
aidct_core: enLocaleResources
|
|
71
78
|
},
|
|
@@ -77,24 +84,11 @@ export const coreResources: Resource = {
|
|
|
77
84
|
// Explicit type is necessary because type can't be inferred without additional references.
|
|
78
85
|
export const i18nextCore: i18n = i18next.createInstance();
|
|
79
86
|
|
|
80
|
-
/**
|
|
81
|
-
* Initialize internationalization.
|
|
82
|
-
*
|
|
83
|
-
* @param environment
|
|
84
|
-
* Environment in which the application is running.
|
|
85
|
-
*
|
|
86
|
-
* @param debug
|
|
87
|
-
* Debug setting.
|
|
88
|
-
*/
|
|
89
|
-
export async function i18nCoreInit(environment: I18nEnvironment, debug = false): Promise<void> {
|
|
90
|
-
await i18nFinalizeInit(i18nextCore, environment, debug, coreNS, coreResources);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
87
|
/**
|
|
94
88
|
* Initialize internationalization.
|
|
95
89
|
*
|
|
96
90
|
* @param i18next
|
|
97
|
-
* Internationalization object. As multiple objects
|
|
91
|
+
* Internationalization object. As multiple objects exist, this parameter represents the one for the module for which
|
|
98
92
|
* internationalization is being initialized.
|
|
99
93
|
*
|
|
100
94
|
* @param environment
|
|
@@ -106,56 +100,94 @@ export async function i18nCoreInit(environment: I18nEnvironment, debug = false):
|
|
|
106
100
|
* @param defaultNS
|
|
107
101
|
* Default namespace.
|
|
108
102
|
*
|
|
109
|
-
* @param
|
|
110
|
-
*
|
|
103
|
+
* @param defaultResourceBundle
|
|
104
|
+
* Default resource bundle.
|
|
105
|
+
*
|
|
106
|
+
* @param i18nDependencyInits
|
|
107
|
+
* Dependency internationalization initialization functions.
|
|
108
|
+
*
|
|
109
|
+
* @returns
|
|
110
|
+
* Default resource bundle.
|
|
111
111
|
*/
|
|
112
|
-
export async function
|
|
112
|
+
export async function i18nInit(i18next: i18n, environment: I18nEnvironment, debug: boolean, defaultNS: string, defaultResourceBundle: Resource, ...i18nDependencyInits: Array<(environment: I18nEnvironment, debug: boolean) => Promise<Resource>>): Promise<Resource> {
|
|
113
113
|
// Initialization may be called more than once.
|
|
114
114
|
if (!i18next.isInitialized) {
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
115
|
+
const mergedResourceBundle: Resource = {};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Merge a package resource bundle into the merged resource bundle.
|
|
119
|
+
*
|
|
120
|
+
* @param resourceBundle
|
|
121
|
+
* Package resource bundle.
|
|
122
|
+
*/
|
|
123
|
+
function mergeResourceBundle(resourceBundle: Resource): void {
|
|
119
124
|
// Merge languages.
|
|
120
|
-
for (const [language,
|
|
121
|
-
if (!(language in
|
|
122
|
-
|
|
125
|
+
for (const [language, languageResourceBundle] of Object.entries(resourceBundle)) {
|
|
126
|
+
if (!(language in mergedResourceBundle)) {
|
|
127
|
+
mergedResourceBundle[language] = {};
|
|
123
128
|
}
|
|
124
129
|
|
|
125
|
-
const
|
|
130
|
+
const mergedLanguageResourceBundle = mergedResourceBundle[language];
|
|
126
131
|
|
|
127
132
|
// Merge namespaces.
|
|
128
|
-
for (const [namespace, resourceKey] of Object.entries(
|
|
129
|
-
|
|
133
|
+
for (const [namespace, resourceKey] of Object.entries(languageResourceBundle)) {
|
|
134
|
+
mergedLanguageResourceBundle[namespace] = resourceKey;
|
|
130
135
|
}
|
|
131
136
|
}
|
|
132
137
|
}
|
|
133
138
|
|
|
134
|
-
|
|
139
|
+
mergeResourceBundle(defaultResourceBundle);
|
|
135
140
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
break;
|
|
141
|
+
// Initialize dependencies and merge their resource bundles.
|
|
142
|
+
await Promise.all(i18nDependencyInits.map(async i18nDependencyInit =>
|
|
143
|
+
i18nDependencyInit(environment, debug).then(mergeResourceBundle))
|
|
144
|
+
).then(() => {
|
|
145
|
+
let module: Module | Newable<Module> | NewableModule<Module>;
|
|
142
146
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
147
|
+
switch (environment) {
|
|
148
|
+
case I18nEnvironments.CLI:
|
|
149
|
+
// TODO Refactor when https://github.com/neet/i18next-cli-language-detector/issues/281 resolved.
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Per above.
|
|
151
|
+
module = I18nextCLILanguageDetector as unknown as LanguageDetectorModule;
|
|
152
|
+
break;
|
|
146
153
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
case I18nEnvironments.Browser:
|
|
155
|
+
module = I18nextBrowserLanguageDetector;
|
|
156
|
+
break;
|
|
150
157
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}).then(
|
|
158
|
+
default:
|
|
159
|
+
throw new Error("Not supported");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return module;
|
|
163
|
+
}).then(async module =>
|
|
164
|
+
i18next.use(module).init({
|
|
165
|
+
debug,
|
|
166
|
+
resources: mergedResourceBundle,
|
|
167
|
+
fallbackLng: "en",
|
|
168
|
+
defaultNS
|
|
169
|
+
})
|
|
170
|
+
).then(() => {
|
|
157
171
|
// Add toLowerCase function.
|
|
158
172
|
i18next.services.formatter?.add("toLowerCase", value => typeof value === "string" ? toLowerCase(value) : String(value));
|
|
159
173
|
});
|
|
160
174
|
}
|
|
175
|
+
|
|
176
|
+
return defaultResourceBundle;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Initialize internationalization.
|
|
181
|
+
*
|
|
182
|
+
* @param environment
|
|
183
|
+
* Environment in which the application is running.
|
|
184
|
+
*
|
|
185
|
+
* @param debug
|
|
186
|
+
* Debug setting.
|
|
187
|
+
*
|
|
188
|
+
* @returns
|
|
189
|
+
* Core resource bundle.
|
|
190
|
+
*/
|
|
191
|
+
export async function i18nCoreInit(environment: I18nEnvironment, debug = false): Promise<Resource> {
|
|
192
|
+
return i18nInit(i18nextCore, environment, debug, coreNS, coreResourceBundle);
|
|
161
193
|
}
|
package/src/logger.ts
CHANGED
|
@@ -67,10 +67,12 @@ export function logLevelOf(untypedLogLevel?: string | number): LogLevel {
|
|
|
67
67
|
* [`tslog`](https://tslog.js.org/).
|
|
68
68
|
*
|
|
69
69
|
* @param logLevel
|
|
70
|
-
* Log level as enumeration value or string. Mapped to `minLevel` in
|
|
70
|
+
* Log level as enumeration value or string. Mapped to `minLevel` and sets `hideLogPositionForProduction` to true in
|
|
71
|
+
* settings if at {@linkcode LogLevels.Info} or higher. Default is {@linkcode LogLevels.Info}.
|
|
71
72
|
*
|
|
72
73
|
* @param settings
|
|
73
|
-
* Detailed settings. See [`tslog`](https://tslog.js.org/#/?id=settings) documentation for details.
|
|
74
|
+
* Detailed settings. See [`tslog`](https://tslog.js.org/#/?id=settings) documentation for details. The `minLevel` is
|
|
75
|
+
* ignored in favour of `logLevel` but `hideLogPositionForProduction` will override the default logic.
|
|
74
76
|
*
|
|
75
77
|
* @param logObj
|
|
76
78
|
* Default log object. See [`tslog`](https://tslog.js.org/#/?id=defining-and-accessing-logobj) documentation for
|
|
@@ -83,9 +85,14 @@ export function logLevelOf(untypedLogLevel?: string | number): LogLevel {
|
|
|
83
85
|
* Log object type.
|
|
84
86
|
*/
|
|
85
87
|
export function getLogger<T extends object = object>(logLevel?: string | number, settings?: ISettingsParam<T>, logObj?: T): Logger<T> {
|
|
88
|
+
const minLevel = logLevelOf(logLevel);
|
|
89
|
+
|
|
86
90
|
return new Logger({
|
|
91
|
+
// Hiding log position for production can be overridden in settings parameter.
|
|
92
|
+
hideLogPositionForProduction: minLevel >= LogLevels.Info,
|
|
87
93
|
...settings ?? {},
|
|
88
|
-
|
|
94
|
+
// Minimum log level overrides settings parameter.
|
|
95
|
+
minLevel
|
|
89
96
|
}, logObj);
|
|
90
97
|
}
|
|
91
98
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parsed version.
|
|
3
|
+
*/
|
|
4
|
+
export interface ParsedVersion {
|
|
5
|
+
/**
|
|
6
|
+
* Major version.
|
|
7
|
+
*/
|
|
8
|
+
readonly majorVersion: number;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Minor version.
|
|
12
|
+
*/
|
|
13
|
+
readonly minorVersion: number;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Patch version.
|
|
17
|
+
*/
|
|
18
|
+
readonly patchVersion: number;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Pre-release identifier.
|
|
22
|
+
*/
|
|
23
|
+
readonly preReleaseIdentifier: string | undefined;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Date/time included with pre-release identifier.
|
|
27
|
+
*/
|
|
28
|
+
readonly dateTime: string | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parse version.
|
|
33
|
+
*
|
|
34
|
+
* @param version
|
|
35
|
+
* Version, typically from package.json.
|
|
36
|
+
*
|
|
37
|
+
* @returns
|
|
38
|
+
* Parsed version.
|
|
39
|
+
*/
|
|
40
|
+
export function parseVersion(version: string): ParsedVersion {
|
|
41
|
+
const parsedVersionGroups = /^(?<majorVersion>\d+)\.(?<minorVersion>\d+)\.(?<patchVersion>\d+)(?:-(?<preReleaseIdentifier>alpha|beta)(?:\.(?<dateTime>\d{12}))?)?$/u.exec(version)?.groups;
|
|
42
|
+
|
|
43
|
+
if (parsedVersionGroups === undefined) {
|
|
44
|
+
throw new Error(`Invalid package version ${version}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
majorVersion: Number(parsedVersionGroups["majorVersion"]),
|
|
49
|
+
minorVersion: Number(parsedVersionGroups["minorVersion"]),
|
|
50
|
+
patchVersion: Number(parsedVersionGroups["patchVersion"]),
|
|
51
|
+
preReleaseIdentifier: parsedVersionGroups["preReleaseIdentifier"],
|
|
52
|
+
dateTime: parsedVersionGroups["dateTime"]
|
|
53
|
+
};
|
|
54
|
+
}
|
package/src/phase-url.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { parseVersion } from "./parse-version.js";
|
|
2
|
+
|
|
3
|
+
const NON_ALPHA_BASE_URL = "https://aidc-toolkit.com";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get the URL for the phase as determined by the package version.
|
|
7
|
+
*
|
|
8
|
+
* @param version
|
|
9
|
+
* Package version.
|
|
10
|
+
*
|
|
11
|
+
* @param alphaURL
|
|
12
|
+
* Alpha URL.
|
|
13
|
+
*
|
|
14
|
+
* @param nonAlphaRelativeURL
|
|
15
|
+
* Non-alpha URL, relative to non-alpha base URL plus optionally the pre-release identifier and version.
|
|
16
|
+
*
|
|
17
|
+
* @returns
|
|
18
|
+
* Fully-formed URL for the phase.
|
|
19
|
+
*/
|
|
20
|
+
export function phaseURL(version: string, alphaURL: string, nonAlphaRelativeURL?: string): string {
|
|
21
|
+
const parsedVersion = parseVersion(version);
|
|
22
|
+
const preReleaseIdentifier = parsedVersion.preReleaseIdentifier;
|
|
23
|
+
|
|
24
|
+
let url: string;
|
|
25
|
+
|
|
26
|
+
if (preReleaseIdentifier === "alpha") {
|
|
27
|
+
// Alpha URL is absolute.
|
|
28
|
+
url = alphaURL;
|
|
29
|
+
} else {
|
|
30
|
+
const relativeURL = nonAlphaRelativeURL !== undefined && nonAlphaRelativeURL !== "" ? `/${nonAlphaRelativeURL}` : "";
|
|
31
|
+
|
|
32
|
+
if (preReleaseIdentifier !== undefined) {
|
|
33
|
+
// Non-alpha URL is relative to non-alpha base URL plus pre-release identifier and version.
|
|
34
|
+
url = `${NON_ALPHA_BASE_URL}/${preReleaseIdentifier}/v${parsedVersion.majorVersion}.${parsedVersion.minorVersion}${relativeURL}`;
|
|
35
|
+
} else {
|
|
36
|
+
// Production URL is relative to non-alpha base URL.
|
|
37
|
+
url = `${NON_ALPHA_BASE_URL}${relativeURL}`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return url;
|
|
42
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ReadOnlyAppDataStorage } from "./app-data-storage.js";
|
|
2
|
+
import { i18nextCore } from "./locale/i18n.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Remote application data storage using HTTP. The `store()` and `delete()` methods are not supported.
|
|
6
|
+
*/
|
|
7
|
+
export class RemoteAppDataStorage extends ReadOnlyAppDataStorage<true> {
|
|
8
|
+
/**
|
|
9
|
+
* Constructor.
|
|
10
|
+
*
|
|
11
|
+
* @param baseURL
|
|
12
|
+
* Base URL. The URL must not end with a slash.
|
|
13
|
+
*/
|
|
14
|
+
constructor(baseURL: string) {
|
|
15
|
+
super(true, baseURL);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @inheritDoc
|
|
20
|
+
*/
|
|
21
|
+
protected override async doRead(key: string, asBinary: boolean | undefined): Promise<string | Uint8Array | undefined> {
|
|
22
|
+
return fetch(key).then(async (response) => {
|
|
23
|
+
let result: string | Uint8Array | undefined;
|
|
24
|
+
|
|
25
|
+
if (response.ok) {
|
|
26
|
+
result = asBinary === true ? new Uint8Array(await response.arrayBuffer()) : await response.text();
|
|
27
|
+
} else if (response.status === 404) {
|
|
28
|
+
result = undefined;
|
|
29
|
+
} else {
|
|
30
|
+
throw new RangeError(i18nextCore.t("RemoteAppDataStorage.httpError", {
|
|
31
|
+
status: response.status
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return result;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/type-helper.ts
CHANGED
|
@@ -94,7 +94,7 @@ export function pick<T extends object, K extends keyof T>(o: T, ...keys: K[]): P
|
|
|
94
94
|
*/
|
|
95
95
|
export function exclude<TWide extends TNarrow, TNarrow extends object, K extends keyof TNarrow>(wide: TWide, narrow: TNarrow): Omit<TWide, K> {
|
|
96
96
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Keys are valid.
|
|
97
|
-
return omit(wide, ...
|
|
97
|
+
return omit(wide, ...Object.keys(narrow) as K[]);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
/**
|
|
@@ -120,7 +120,7 @@ export function exclude<TWide extends TNarrow, TNarrow extends object, K extends
|
|
|
120
120
|
*/
|
|
121
121
|
export function include<TWide extends TNarrow, TNarrow extends object, K extends keyof TNarrow>(wide: TWide, narrow: TNarrow): Pick<TWide, K> {
|
|
122
122
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Keys are valid.
|
|
123
|
-
return pick(wide, ...
|
|
123
|
+
return pick(wide, ...Object.keys(narrow) as K[]);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
/**
|