langsys-js-typescript 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -91,7 +91,18 @@ var LangsysAppAPIClass = class {
91
91
  return await this.get("authorize-project/[projectid]");
92
92
  }
93
93
  async getTranslations(locale) {
94
- return await this.get("projects/[projectid]/translations", { locale });
94
+ return await this.get("translations", { project_id: this.config.projectid, locale });
95
+ }
96
+ /**
97
+ * Register phrases and/or content blocks via the current unified endpoint.
98
+ * Each item carries `type: 'phrase' | 'content_block'`. Replaces the
99
+ * deprecated `projects/{id}/tokens` and `projects/{id}/content-blocks` routes.
100
+ */
101
+ async createTranslatableItems(items) {
102
+ return await this.post("translatable-items", {
103
+ project_id: this.config.projectid,
104
+ translatable_items: items
105
+ });
95
106
  }
96
107
  async post(path, data = {}) {
97
108
  return await this.send("POST", path, data);
@@ -429,6 +440,18 @@ var Translations = class {
429
440
  this.debug.log(`SSR environment detected - using '${strategy}' token strategy`);
430
441
  }
431
442
  }
443
+ /**
444
+ * Record that a locale's catalog is already present without fetching —
445
+ * e.g. after an SSR handoff seeds `sTranslations` + `currentlyLoadedLocale`.
446
+ * This primes the same `lastLoaded` cache `change()` consults, so the first
447
+ * settle for that locale is a cache hit (no redundant fetch) while every
448
+ * later switch *to* it still flows through `change()` normally.
449
+ */
450
+ markLoaded(locale) {
451
+ if (!locale) return;
452
+ this.locale = locale;
453
+ this.lastLoaded[locale] = (/* @__PURE__ */ new Date()).getTime() / 1e3;
454
+ }
432
455
  /**
433
456
  * React to a user-locale change. Fetches translations if we haven't
434
457
  * loaded this locale within the last 60s (or if forced).
@@ -478,20 +501,23 @@ var Translations = class {
478
501
  if (!this.missingTokens.length) return false;
479
502
  this.debug.log("CREATE MISSING TOKENS", this.missingTokens);
480
503
  try {
481
- const tokensForApi = this.missingTokens.map((tokenObj) => ({
482
- projectid: tokenObj.projectid,
483
- token: tokenObj.token,
504
+ const phraseItems = this.missingTokens.map((tokenObj) => ({
505
+ type: "phrase",
506
+ phrase: tokenObj.token,
484
507
  category: tokenObj.category || null
485
508
  }));
486
- const response = await LangsysAppAPI.post("projects/[projectid]/tokens", {
487
- tokens: tokensForApi
488
- });
489
- if (!response.status) {
490
- if (response.errors) {
491
- this.debug.log("TOKEN UPDATE FAIL", this.missingTokens);
492
- this.debug.error("Error updating project tokens", response.errors);
509
+ const BATCH_SIZE = 200;
510
+ for (let offset = 0; offset < phraseItems.length; offset += BATCH_SIZE) {
511
+ const response = await LangsysAppAPI.createTranslatableItems(
512
+ phraseItems.slice(offset, offset + BATCH_SIZE)
513
+ );
514
+ if (!response.status) {
515
+ if (response.errors) {
516
+ this.debug.log("TOKEN UPDATE FAIL", this.missingTokens);
517
+ this.debug.error("Error updating project tokens", response.errors);
518
+ }
519
+ return false;
493
520
  }
494
- return false;
495
521
  }
496
522
  this.missingTokens.forEach((tokenObj) => {
497
523
  const lookupCat = tokenObj.category || "__uncategorized__";
@@ -640,12 +666,14 @@ var LangsysAppClass = class {
640
666
  this.debug.log("Populated sTranslations with initial data for locale:", initialTranslationsLocale);
641
667
  }
642
668
  this.Translations.setup(this.config);
669
+ if (initialTranslations2 && initialTranslationsLocale) {
670
+ this.Translations.markLoaded(initialTranslationsLocale);
671
+ }
643
672
  this.config.sUserLocale.subscribe((locale) => {
644
673
  if (!validateResponse.status) return;
645
674
  this.getLocalesData(locale);
646
675
  this.debug.log("SUBSCRIBING TO USER LOCALE STORE");
647
- const skipFetch = initialTranslationsLocale === locale && !!initialTranslations2;
648
- this.translationsLoadingPromise = this.Translations.change(locale, false, skipFetch);
676
+ this.translationsLoadingPromise = this.Translations.change(locale, false);
649
677
  });
650
678
  return validateResponse;
651
679
  }
@@ -1046,10 +1074,16 @@ async function registerContentBlock(contentBlock) {
1046
1074
  return { status: true };
1047
1075
  }
1048
1076
  try {
1049
- const response = await LangsysAppAPI.post(
1050
- "projects/[projectid]/content-blocks",
1051
- contentBlock
1052
- );
1077
+ const response = await LangsysAppAPI.createTranslatableItems([
1078
+ {
1079
+ type: "content_block",
1080
+ custom_id: contentBlock.custom_id,
1081
+ category: contentBlock.category,
1082
+ content: contentBlock.content,
1083
+ label: contentBlock.label,
1084
+ phrases: contentBlock.tokens.map((phrase) => ({ phrase }))
1085
+ }
1086
+ ]);
1053
1087
  if (!response.status) {
1054
1088
  logger.error("Could not save content block", response.errors);
1055
1089
  return { status: false, errors: response.errors };