@gravito/cosmos 2.0.0 → 3.0.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.
@@ -0,0 +1,20 @@
1
+
2
+ $ bun run build.ts
3
+ Building @gravito/cosmos...
4
+ ⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⠋⠙⠹CLI Building entry: src/index.ts
5
+ CLI Using tsconfig: tsconfig.json
6
+ CLI tsup v8.5.1
7
+ CLI Target: esnext
8
+ ESM Build start
9
+ CJS Build start
10
+ ESM You have emitDecoratorMetadata enabled but @swc/core was not installed, skipping swc plugin
11
+ CJS You have emitDecoratorMetadata enabled but @swc/core was not installed, skipping swc plugin
12
+ ESM dist/index.js 6.93 KB
13
+ ESM ⚡️ Build success in 72ms
14
+ CJS dist/index.cjs 8.20 KB
15
+ CJS ⚡️ Build success in 72ms
16
+ DTS Build start
17
+ DTS ⚡️ Build success in 7986ms
18
+ DTS dist/index.d.ts 7.87 KB
19
+ DTS dist/index.d.cts 7.87 KB
20
+ ⠙✅ Build complete!
@@ -0,0 +1,35 @@
1
+ $ bun test --coverage --coverage-threshold=80
2
+ bun test v1.3.4 (5eb2145b)
3
+
4
+ tests/manager.test.ts:
5
+ (pass) Orbit I18n Manager > it selects default locale initially [0.11ms]
6
+ (pass) Orbit I18n Manager > it can change locale [0.10ms]
7
+ (pass) Orbit I18n Manager > it translates simple keys [0.10ms]
8
+ (pass) Orbit I18n Manager > it handles fallback to default locale [0.09ms]
9
+ (pass) Orbit I18n Manager > it returns key if not found [0.02ms]
10
+ (pass) Orbit I18n Manager > it supports replacements [0.14ms]
11
+
12
+ tests/service.test.ts:
13
+ (pass) I18nManager and I18nInstance > adds resources and translates with replacements [0.06ms]
14
+ (pass) I18nManager and I18nInstance > clones instances with explicit locale [0.06ms]
15
+ (pass) I18nManager and I18nInstance > returns key when value is not a string
16
+ (pass) localeMiddleware > prefers route param locale and injects i18n [0.31ms]
17
+ (pass) localeMiddleware > uses default locale when none provided [0.08ms]
18
+ (pass) OrbitCosmos > installs locale middleware and logs initialization [0.09ms]
19
+
20
+ tests/loader.test.ts:
21
+ (pass) loadTranslations > loads json translation files from a directory [3.04ms]
22
+ (pass) loadTranslations > skips invalid json files and returns empty for missing dir [3.82ms]
23
+ --------------------|---------|---------|-------------------
24
+ File | % Funcs | % Lines | Uncovered Line #s
25
+ --------------------|---------|---------|-------------------
26
+ All files | 93.94 | 98.60 |
27
+ src/I18nService.ts | 81.82 | 95.79 | 96,137,146
28
+ src/index.ts | 100.00 | 100.00 |
29
+ src/loader.ts | 100.00 | 100.00 |
30
+ --------------------|---------|---------|-------------------
31
+
32
+ 14 pass
33
+ 0 fail
34
+ 22 expect() calls
35
+ Ran 14 tests across 3 files. [32.00ms]
@@ -0,0 +1,35 @@
1
+ $ bun test --coverage --coverage-threshold=80
2
+ bun test v1.3.4 (5eb2145b)
3
+
4
+ tests/manager.test.ts:
5
+ (pass) Orbit I18n Manager > it selects default locale initially [1.76ms]
6
+ (pass) Orbit I18n Manager > it can change locale [0.57ms]
7
+ (pass) Orbit I18n Manager > it translates simple keys [0.55ms]
8
+ (pass) Orbit I18n Manager > it handles fallback to default locale [0.06ms]
9
+ (pass) Orbit I18n Manager > it returns key if not found [0.03ms]
10
+ (pass) Orbit I18n Manager > it supports replacements [1.15ms]
11
+
12
+ tests/service.test.ts:
13
+ (pass) I18nManager and I18nInstance > adds resources and translates with replacements [0.29ms]
14
+ (pass) I18nManager and I18nInstance > clones instances with explicit locale [0.12ms]
15
+ (pass) I18nManager and I18nInstance > returns key when value is not a string
16
+ (pass) localeMiddleware > prefers route param locale and injects i18n [0.76ms]
17
+ (pass) localeMiddleware > uses default locale when none provided [11.82ms]
18
+ (pass) OrbitCosmos > installs locale middleware and logs initialization [2.78ms]
19
+
20
+ tests/loader.test.ts:
21
+ (pass) loadTranslations > loads json translation files from a directory [34.71ms]
22
+ (pass) loadTranslations > skips invalid json files and returns empty for missing dir [15.72ms]
23
+ --------------------|---------|---------|-------------------
24
+ File | % Funcs | % Lines | Uncovered Line #s
25
+ --------------------|---------|---------|-------------------
26
+ All files | 92.00 | 96.30 |
27
+ src/I18nService.ts | 76.00 | 88.89 | 160,167-174,215,224
28
+ src/index.ts | 100.00 | 100.00 |
29
+ src/loader.ts | 100.00 | 100.00 |
30
+ --------------------|---------|---------|-------------------
31
+
32
+ 14 pass
33
+ 0 fail
34
+ 23 expect() calls
35
+ Ran 14 tests across 3 files. [175.00ms]
@@ -0,0 +1,27 @@
1
+ $ bun test
2
+ bun test v1.3.4 (5eb2145b)
3
+
4
+ tests/manager.test.ts:
5
+ (pass) Orbit I18n Manager > it selects default locale initially [0.40ms]
6
+ (pass) Orbit I18n Manager > it can change locale [0.19ms]
7
+ (pass) Orbit I18n Manager > it translates simple keys [0.49ms]
8
+ (pass) Orbit I18n Manager > it handles fallback to default locale
9
+ (pass) Orbit I18n Manager > it returns key if not found [0.09ms]
10
+ (pass) Orbit I18n Manager > it supports replacements [32.97ms]
11
+
12
+ tests/service.test.ts:
13
+ (pass) I18nManager and I18nInstance > adds resources and translates with replacements [0.67ms]
14
+ (pass) I18nManager and I18nInstance > clones instances with explicit locale [0.85ms]
15
+ (pass) I18nManager and I18nInstance > returns key when value is not a string
16
+ (pass) localeMiddleware > prefers route param locale and injects i18n [1.68ms]
17
+ (pass) localeMiddleware > uses default locale when none provided [0.22ms]
18
+ (pass) OrbitCosmos > installs locale middleware and logs initialization [89.64ms]
19
+
20
+ tests/loader.test.ts:
21
+ (pass) loadTranslations > loads json translation files from a directory [127.44ms]
22
+ (pass) loadTranslations > skips invalid json files and returns empty for missing dir [126.24ms]
23
+
24
+ 14 pass
25
+ 0 fail
26
+ 23 expect() calls
27
+ Ran 14 tests across 3 files. [1257.00ms]
@@ -0,0 +1,2 @@
1
+
2
+ $ bun tsc -p tsconfig.json --noEmit --skipLibCheck
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @gravito/cosmos
2
2
 
3
+ ## 3.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @gravito/core@1.2.1
9
+
10
+ ## 3.0.0
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies
15
+ - @gravito/core@1.2.0
16
+
3
17
  ## 2.0.0
4
18
 
5
19
  ### Patch Changes
package/build.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { spawn } from 'bun'
2
+
3
+ console.log('Building @gravito/cosmos...')
4
+
5
+ // Clean dist
6
+ await Bun.$`rm -rf dist`
7
+
8
+ // Use tsup for multi-format build
9
+ const tsup = spawn(
10
+ [
11
+ 'npx',
12
+ 'tsup',
13
+ 'src/index.ts',
14
+ '--format',
15
+ 'esm,cjs',
16
+ '--dts',
17
+ '--external',
18
+ '@gravito/core,@gravito/photon',
19
+ '--outDir',
20
+ 'dist',
21
+ ],
22
+ {
23
+ stdout: 'inherit',
24
+ stderr: 'inherit',
25
+ }
26
+ )
27
+
28
+ const tsupCode = await tsup.exited
29
+ if (tsupCode !== 0) {
30
+ console.error('❌ tsup build failed')
31
+ process.exit(1)
32
+ }
33
+
34
+ console.log('✅ Build complete!')
35
+ process.exit(0)
package/dist/index.cjs ADDED
@@ -0,0 +1,309 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ I18nInstance: () => I18nInstance,
24
+ I18nManager: () => I18nManager,
25
+ I18nOrbit: () => I18nOrbit,
26
+ OrbitCosmos: () => OrbitCosmos,
27
+ loadTranslations: () => loadTranslations,
28
+ localeMiddleware: () => localeMiddleware
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/I18nService.ts
33
+ var I18nInstance = class _I18nInstance {
34
+ /**
35
+ * Create a new I18nInstance.
36
+ *
37
+ * @param manager - The I18nManager instance.
38
+ * @param initialLocale - The initial locale for this instance.
39
+ */
40
+ constructor(manager, initialLocale) {
41
+ this.manager = manager;
42
+ this._locale = initialLocale;
43
+ }
44
+ _locale;
45
+ get locale() {
46
+ return this._locale;
47
+ }
48
+ set locale(value) {
49
+ this.setLocale(value);
50
+ }
51
+ /**
52
+ * Set the current locale.
53
+ *
54
+ * @param locale - The locale to set.
55
+ */
56
+ setLocale(locale) {
57
+ if (this.manager.getConfig().supportedLocales.includes(locale)) {
58
+ this._locale = locale;
59
+ }
60
+ }
61
+ /**
62
+ * Get the current locale.
63
+ *
64
+ * @returns The current locale string.
65
+ */
66
+ getLocale() {
67
+ return this._locale;
68
+ }
69
+ /**
70
+ * Translate a key.
71
+ *
72
+ * @param key - The translation key (e.g., 'messages.welcome').
73
+ * @param replacements - Optional replacements for parameters in the translation string.
74
+ * @returns The translated string, or the key if not found.
75
+ */
76
+ t(key, replacements) {
77
+ return this.manager.translate(this._locale, key, replacements);
78
+ }
79
+ /**
80
+ * Check if a translation key exists.
81
+ *
82
+ * @param key - The translation key to check.
83
+ * @returns True if the key exists, false otherwise.
84
+ */
85
+ has(key) {
86
+ return this.t(key) !== key;
87
+ }
88
+ /**
89
+ * Clone the current instance with a potentially new locale.
90
+ *
91
+ * @param locale - Optional new locale for the cloned instance.
92
+ * @returns A new I18nInstance.
93
+ */
94
+ clone(locale) {
95
+ return new _I18nInstance(this.manager, locale || this._locale);
96
+ }
97
+ /**
98
+ * Get the I18n configuration.
99
+ */
100
+ getConfig() {
101
+ return this.manager.getConfig();
102
+ }
103
+ /**
104
+ * Get the translations.
105
+ */
106
+ get translations() {
107
+ return this.manager.translations;
108
+ }
109
+ };
110
+ var I18nManager = class {
111
+ /**
112
+ * Create a new I18nManager.
113
+ *
114
+ * @param config - The I18n configuration.
115
+ */
116
+ constructor(config) {
117
+ this.config = config;
118
+ if (config.translations) {
119
+ this.translations = config.translations;
120
+ }
121
+ this.globalInstance = new I18nInstance(this, config.defaultLocale);
122
+ }
123
+ translations = {};
124
+ // Default instance for global usage (e.g. CLI or background jobs)
125
+ globalInstance;
126
+ // --- I18nService Implementation (Delegates to global instance) ---
127
+ get locale() {
128
+ return this.globalInstance.locale;
129
+ }
130
+ set locale(value) {
131
+ this.globalInstance.locale = value;
132
+ }
133
+ /**
134
+ * Set the global locale.
135
+ *
136
+ * @param locale - The locale to set.
137
+ */
138
+ setLocale(locale) {
139
+ this.globalInstance.setLocale(locale);
140
+ }
141
+ /**
142
+ * Get the global locale.
143
+ *
144
+ * @returns The global locale string.
145
+ */
146
+ getLocale() {
147
+ return this.globalInstance.getLocale();
148
+ }
149
+ /**
150
+ * Translate a key using the global locale.
151
+ *
152
+ * @param key - The translation key.
153
+ * @param replacements - Optional replacements.
154
+ * @returns The translated string.
155
+ */
156
+ t(key, replacements) {
157
+ return this.globalInstance.t(key, replacements);
158
+ }
159
+ /**
160
+ * Check if a translation key exists in the global locale.
161
+ *
162
+ * @param key - The translation key.
163
+ * @returns True if found.
164
+ */
165
+ has(key) {
166
+ return this.globalInstance.has(key);
167
+ }
168
+ /**
169
+ * Clone the global instance.
170
+ *
171
+ * @param locale - Optional locale for the clone.
172
+ * @returns A new I18nInstance.
173
+ */
174
+ clone(locale) {
175
+ return new I18nInstance(this, locale || this.config.defaultLocale);
176
+ }
177
+ // --- Manager Internal API ---
178
+ /**
179
+ * Get the I18n configuration.
180
+ *
181
+ * @returns The configuration object.
182
+ */
183
+ getConfig() {
184
+ return this.config;
185
+ }
186
+ /**
187
+ * Add a resource bundle for a specific locale.
188
+ *
189
+ * @param locale - The locale string.
190
+ * @param translations - The translations object.
191
+ */
192
+ addResource(locale, translations) {
193
+ this.translations[locale] = {
194
+ ...this.translations[locale] || {},
195
+ ...translations
196
+ };
197
+ }
198
+ /**
199
+ * Internal translation logic used by instances
200
+ */
201
+ translate(locale, key, replacements) {
202
+ const keys = key.split(".");
203
+ let value = this.translations[locale];
204
+ for (const k of keys) {
205
+ if (value && typeof value === "object" && k in value) {
206
+ value = value[k];
207
+ } else {
208
+ value = void 0;
209
+ break;
210
+ }
211
+ }
212
+ if (value === void 0 && locale !== this.config.defaultLocale) {
213
+ let fallbackValue = this.translations[this.config.defaultLocale];
214
+ for (const k of keys) {
215
+ if (fallbackValue && typeof fallbackValue === "object" && k in fallbackValue) {
216
+ fallbackValue = fallbackValue[k];
217
+ } else {
218
+ fallbackValue = void 0;
219
+ break;
220
+ }
221
+ }
222
+ value = fallbackValue;
223
+ }
224
+ if (value === void 0 || typeof value !== "string") {
225
+ return key;
226
+ }
227
+ if (replacements && Object.keys(replacements).length > 0) {
228
+ value = value.replace(REPLACEMENT_REGEX, (match, key2) => {
229
+ return replacements[key2] !== void 0 ? String(replacements[key2]) : match;
230
+ });
231
+ }
232
+ return value;
233
+ }
234
+ };
235
+ var REPLACEMENT_REGEX = /:([a-zA-Z0-9_]+)/g;
236
+ var localeMiddleware = (i18nManager) => {
237
+ return async (c, next) => {
238
+ let locale = c.req.param("locale") || c.req.query("lang");
239
+ if (!locale) {
240
+ const acceptLang = c.req.header("Accept-Language");
241
+ if (acceptLang) {
242
+ locale = acceptLang.split(",")[0]?.trim();
243
+ }
244
+ }
245
+ const i18n = i18nManager.clone(locale);
246
+ c.set("i18n", i18n);
247
+ return await next();
248
+ };
249
+ };
250
+
251
+ // src/loader.ts
252
+ var import_promises = require("fs/promises");
253
+ var import_node_path = require("path");
254
+ async function loadTranslations(directory) {
255
+ const translations = {};
256
+ try {
257
+ const files = await (0, import_promises.readdir)(directory);
258
+ for (const file of files) {
259
+ if (!file.endsWith(".json")) {
260
+ continue;
261
+ }
262
+ const locale = (0, import_node_path.parse)(file).name;
263
+ const content = await (0, import_promises.readFile)((0, import_node_path.join)(directory, file), "utf-8");
264
+ try {
265
+ translations[locale] = JSON.parse(content);
266
+ } catch (e) {
267
+ console.error(`[Orbit-I18n] Failed to parse translation file: ${file}`, e);
268
+ }
269
+ }
270
+ } catch (_e) {
271
+ console.warn(
272
+ `[Orbit-I18n] Could not load translations from ${directory}. Directory might not exist.`
273
+ );
274
+ }
275
+ return translations;
276
+ }
277
+
278
+ // src/index.ts
279
+ var OrbitCosmos = class {
280
+ /**
281
+ * Create a new OrbitCosmos instance.
282
+ * @param config - The i18n configuration options.
283
+ */
284
+ constructor(config) {
285
+ this.config = config;
286
+ }
287
+ /**
288
+ * Install the i18n service into PlanetCore.
289
+ * Registers the I18nManager and sets up the locale middleware.
290
+ *
291
+ * @param core - The PlanetCore instance.
292
+ */
293
+ install(core) {
294
+ const i18nManager = new I18nManager(this.config);
295
+ core.container.instance("i18n", i18nManager);
296
+ core.adapter.use("*", localeMiddleware(i18nManager));
297
+ core.logger.info(`[OrbitCosmos] I18n initialized with locale: ${this.config.defaultLocale}`);
298
+ }
299
+ };
300
+ var I18nOrbit = OrbitCosmos;
301
+ // Annotate the CommonJS export names for ESM import in node:
302
+ 0 && (module.exports = {
303
+ I18nInstance,
304
+ I18nManager,
305
+ I18nOrbit,
306
+ OrbitCosmos,
307
+ loadTranslations,
308
+ localeMiddleware
309
+ });