@localess/client 3.0.1-dev.20260410071322 → 3.0.1-dev.20260412201733

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.js CHANGED
@@ -1,292 +1 @@
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
- isBrowser: () => isBrowser,
24
- isIframe: () => isIframe,
25
- isServer: () => isServer,
26
- loadLocalessSync: () => loadLocalessSync,
27
- localessClient: () => localessClient,
28
- localessEditable: () => localessEditable,
29
- localessEditableField: () => localessEditableField
30
- });
31
- module.exports = __toCommonJS(index_exports);
32
-
33
- // src/utils.ts
34
- var RESET = "\x1B[0m";
35
- var FG_BLUE = "\x1B[34m";
36
- var isBrowser = () => typeof window !== "undefined";
37
- var isServer = () => typeof window === "undefined";
38
- var isIframe = () => isBrowser() && window.self !== window.top;
39
-
40
- // src/cache.ts
41
- var NoCache = class {
42
- set(key, value) {
43
- }
44
- get(key) {
45
- return void 0;
46
- }
47
- has(key) {
48
- return false;
49
- }
50
- };
51
- var TTLCache = class {
52
- /**
53
- * Creates a TTLCache with a specified time-to-live (TTL) for each entry.
54
- * default is 5 minutes (300000 ms).
55
- * @param ttlMs
56
- */
57
- constructor(ttlMs = 3e5) {
58
- this.ttlMs = ttlMs;
59
- }
60
- ttlMs;
61
- cache = /* @__PURE__ */ new Map();
62
- set(key, value) {
63
- this.cache.set(key, { value, expiresAt: Date.now() + this.ttlMs });
64
- }
65
- get(key) {
66
- const entry = this.cache.get(key);
67
- if (!entry) return void 0;
68
- if (Date.now() > entry.expiresAt) {
69
- this.cache.delete(key);
70
- return void 0;
71
- }
72
- return entry.value;
73
- }
74
- has(key) {
75
- return this.get(key) !== void 0;
76
- }
77
- };
78
-
79
- // src/client.ts
80
- var LOG_GROUP = `${FG_BLUE}[Localess:Client]${RESET}`;
81
- function localessClient(options) {
82
- if (options.debug) {
83
- console.log(LOG_GROUP, "Client Options : ", options);
84
- }
85
- const normalizedOrigin = options.origin.replace(/\/+$/, "");
86
- const fetchOptions = {
87
- redirect: "follow",
88
- headers: {
89
- "Content-Type": "application/json",
90
- "Accept": "application/json",
91
- "X-Localess-Agent": "Localess-JS-Client",
92
- "X-Localess-Agent-Version": "0.9.0"
93
- }
94
- };
95
- const cache = options.cacheTTL === false ? new NoCache() : new TTLCache(options.cacheTTL);
96
- return {
97
- async getLinks(params) {
98
- if (options.debug) {
99
- console.log(LOG_GROUP, "getLinks() params : ", JSON.stringify(params));
100
- }
101
- let kind = "";
102
- if (params?.kind) {
103
- kind = `&kind=${params.kind}`;
104
- }
105
- let parentSlug = "";
106
- if (params?.parentSlug) {
107
- parentSlug = `&parentSlug=${params.parentSlug}`;
108
- }
109
- let excludeChildren = "";
110
- if (params?.excludeChildren) {
111
- excludeChildren = `&excludeChildren=${params.excludeChildren}`;
112
- }
113
- let url = `${normalizedOrigin}/api/v1/spaces/${options.spaceId}/links?token=${options.token}${kind}${parentSlug}${excludeChildren}`;
114
- if (options.debug) {
115
- console.log(LOG_GROUP, "getLinks fetch url : ", url);
116
- }
117
- if (cache.has(url)) {
118
- if (options.debug) {
119
- console.log(LOG_GROUP, "getLinks cache hit");
120
- }
121
- return cache.get(url);
122
- }
123
- try {
124
- const response = await fetch(url, fetchOptions);
125
- if (options.debug) {
126
- console.log(LOG_GROUP, "getLinks status : ", response.status);
127
- }
128
- const data = await response.json();
129
- cache.set(url, data);
130
- return data;
131
- } catch (error) {
132
- console.error(LOG_GROUP, "getLinks error : ", error);
133
- return {};
134
- }
135
- },
136
- async getContentBySlug(slug, params) {
137
- if (options.debug) {
138
- console.log(LOG_GROUP, "getContentBySlug() slug : ", slug);
139
- console.log(LOG_GROUP, "getContentBySlug() params : ", JSON.stringify(params));
140
- }
141
- let version = "";
142
- if (options?.version && options.version == "draft") {
143
- version = `&version=${options.version}`;
144
- }
145
- if (params?.version && params.version == "draft") {
146
- version = `&version=${params.version}`;
147
- }
148
- const locale = params?.locale ? `&locale=${params.locale}` : "";
149
- const resolveReference = params?.resolveReference ? `&resolveReference=${params.resolveReference}` : "";
150
- const resolveLink = params?.resolveLink ? `&resolveLink=${params.resolveLink}` : "";
151
- let url = `${normalizedOrigin}/api/v1/spaces/${options.spaceId}/contents/slugs/${slug}?token=${options.token}${version}${locale}${resolveReference}${resolveLink}`;
152
- if (options.debug) {
153
- console.log(LOG_GROUP, "getContentBySlug fetch url : ", url);
154
- }
155
- if (cache.has(url)) {
156
- if (options.debug) {
157
- console.log(LOG_GROUP, "getContentBySlug cache hit");
158
- }
159
- return cache.get(url);
160
- }
161
- try {
162
- const response = await fetch(url, fetchOptions);
163
- if (options.debug) {
164
- console.log(LOG_GROUP, "getContentBySlug status : ", response.status);
165
- }
166
- const data = await response.json();
167
- cache.set(url, data);
168
- return data;
169
- } catch (error) {
170
- console.error(LOG_GROUP, "getContentBySlug error : ", error);
171
- return {};
172
- }
173
- },
174
- async getContentById(id, params) {
175
- if (options.debug) {
176
- console.log(LOG_GROUP, "getContentById() id : ", id);
177
- console.log(LOG_GROUP, "getContentById() params : ", JSON.stringify(params));
178
- }
179
- let version = "";
180
- if (options?.version && options.version == "draft") {
181
- version = `&version=${options.version}`;
182
- }
183
- if (params?.version && params.version == "draft") {
184
- version = `&version=${params.version}`;
185
- }
186
- const locale = params?.locale ? `&locale=${params.locale}` : "";
187
- const resolveReference = params?.resolveReference ? `&resolveReference=${params.resolveReference}` : "";
188
- const resolveLink = params?.resolveLink ? `&resolveLink=${params.resolveLink}` : "";
189
- let url = `${normalizedOrigin}/api/v1/spaces/${options.spaceId}/contents/${id}?token=${options.token}${version}${locale}${resolveReference}${resolveLink}`;
190
- if (options.debug) {
191
- console.log(LOG_GROUP, "getContentById fetch url : ", url);
192
- }
193
- if (cache.has(url)) {
194
- if (options.debug) {
195
- console.log(LOG_GROUP, "getContentById cache hit");
196
- }
197
- return cache.get(url);
198
- }
199
- try {
200
- const response = await fetch(url, fetchOptions);
201
- if (options.debug) {
202
- console.log(LOG_GROUP, "getContentById status : ", response.status);
203
- }
204
- const data = await response.json();
205
- cache.set(url, data);
206
- return data;
207
- } catch (error) {
208
- console.error(LOG_GROUP, "getContentById error : ", error);
209
- return {};
210
- }
211
- },
212
- async getTranslations(locale) {
213
- if (options.debug) {
214
- console.log(LOG_GROUP, "getTranslations() locale : ", locale);
215
- }
216
- let url = `${normalizedOrigin}/api/v1/spaces/${options.spaceId}/translations/${locale}?token=${options.token}`;
217
- if (options.debug) {
218
- console.log(LOG_GROUP, "getTranslations fetch url : ", url);
219
- }
220
- if (cache.has(url)) {
221
- if (options.debug) {
222
- console.log(LOG_GROUP, "getTranslations cache hit");
223
- }
224
- return cache.get(url);
225
- }
226
- try {
227
- const response = await fetch(url, fetchOptions);
228
- if (options.debug) {
229
- console.log(LOG_GROUP, "getTranslations status : ", response.status);
230
- }
231
- const data = await response.json();
232
- cache.set(url, data);
233
- return data;
234
- } catch (error) {
235
- console.error(LOG_GROUP, "getTranslations error : ", error);
236
- return {};
237
- }
238
- },
239
- syncScriptUrl() {
240
- return `${normalizedOrigin}/scripts/sync-v1.js`;
241
- },
242
- assetLink(asset) {
243
- if (typeof asset === "string") {
244
- return `${normalizedOrigin}/api/v1/spaces/${options.spaceId}/assets/${asset}`;
245
- } else {
246
- return `${normalizedOrigin}/api/v1/spaces/${options.spaceId}/assets/${asset.uri}`;
247
- }
248
- }
249
- };
250
- }
251
-
252
- // src/editable.ts
253
- function localessEditable(content) {
254
- return {
255
- "data-ll-id": content._id,
256
- "data-ll-schema": content._schema
257
- };
258
- }
259
- function localessEditableField(fieldName) {
260
- return {
261
- "data-ll-field": fieldName
262
- };
263
- }
264
-
265
- // src/sync.ts
266
- var JS_SYNC_ID = "localess-js-sync";
267
- function loadLocalessSync(origin, force = false) {
268
- if (isServer()) return;
269
- if (!isIframe()) return;
270
- const isSyncLoaded = typeof window.localess !== "undefined";
271
- if (isSyncLoaded) return;
272
- const scriptEl = document.getElementById(JS_SYNC_ID);
273
- if (scriptEl) return;
274
- const script = document.createElement("script");
275
- script.id = JS_SYNC_ID;
276
- script.type = "text/javascript";
277
- script.src = `${origin}/scripts/sync-v1.js`;
278
- script.async = true;
279
- script.onerror = (error) => console.error(error);
280
- script.onload = (event) => console.info("Localess Sync Script loaded");
281
- document.head.appendChild(script);
282
- }
283
- // Annotate the CommonJS export names for ESM import in node:
284
- 0 && (module.exports = {
285
- isBrowser,
286
- isIframe,
287
- isServer,
288
- loadLocalessSync,
289
- localessClient,
290
- localessEditable,
291
- localessEditableField
292
- });
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=`\x1B[0m`,t=`\x1B[34m`,n=()=>typeof window<`u`,r=()=>typeof window>`u`,i=()=>n()&&window.self!==window.top,a=class{set(e,t){}get(e){}has(e){return!1}},o=class{cache=new Map;constructor(e=3e5){this.ttlMs=e}set(e,t){this.cache.set(e,{value:t,expiresAt:Date.now()+this.ttlMs})}get(e){let t=this.cache.get(e);if(t){if(Date.now()>t.expiresAt){this.cache.delete(e);return}return t.value}}has(e){return this.get(e)!==void 0}},s=`${t}[Localess:Client]${e}`;function c(e){e.debug&&console.log(s,`Client Options : `,e);let t=e.origin.replace(/\/+$/,``),n={redirect:`follow`,headers:{"Content-Type":`application/json`,Accept:`application/json`,"X-Localess-Agent":`Localess-JS-Client`,"X-Localess-Agent-Version":`0.9.0`}},r=e.cacheTTL===!1?new a:new o(e.cacheTTL);return{async getLinks(i){e.debug&&console.log(s,`getLinks() params : `,JSON.stringify(i));let a=``;i?.kind&&(a=`&kind=${i.kind}`);let o=``;i?.parentSlug&&(o=`&parentSlug=${i.parentSlug}`);let c=``;i?.excludeChildren&&(c=`&excludeChildren=${i.excludeChildren}`);let l=`${t}/api/v1/spaces/${e.spaceId}/links?token=${e.token}${a}${o}${c}`;if(e.debug&&console.log(s,`getLinks fetch url : `,l),r.has(l))return e.debug&&console.log(s,`getLinks cache hit`),r.get(l);try{let t=await fetch(l,n);e.debug&&console.log(s,`getLinks status : `,t.status);let i=await t.json();return r.set(l,i),i}catch(e){return console.error(s,`getLinks error : `,e),{}}},async getContentBySlug(i,a){e.debug&&(console.log(s,`getContentBySlug() slug : `,i),console.log(s,`getContentBySlug() params : `,JSON.stringify(a)));let o=``;e?.version&&e.version==`draft`&&(o=`&version=${e.version}`),a?.version&&a.version==`draft`&&(o=`&version=${a.version}`);let c=a?.locale?`&locale=${a.locale}`:``,l=a?.resolveReference?`&resolveReference=${a.resolveReference}`:``,u=a?.resolveLink?`&resolveLink=${a.resolveLink}`:``,d=`${t}/api/v1/spaces/${e.spaceId}/contents/slugs/${i}?token=${e.token}${o}${c}${l}${u}`;if(e.debug&&console.log(s,`getContentBySlug fetch url : `,d),r.has(d))return e.debug&&console.log(s,`getContentBySlug cache hit`),r.get(d);try{let t=await fetch(d,n);e.debug&&console.log(s,`getContentBySlug status : `,t.status);let i=await t.json();return r.set(d,i),i}catch(e){return console.error(s,`getContentBySlug error : `,e),{}}},async getContentById(i,a){e.debug&&(console.log(s,`getContentById() id : `,i),console.log(s,`getContentById() params : `,JSON.stringify(a)));let o=``;e?.version&&e.version==`draft`&&(o=`&version=${e.version}`),a?.version&&a.version==`draft`&&(o=`&version=${a.version}`);let c=a?.locale?`&locale=${a.locale}`:``,l=a?.resolveReference?`&resolveReference=${a.resolveReference}`:``,u=a?.resolveLink?`&resolveLink=${a.resolveLink}`:``,d=`${t}/api/v1/spaces/${e.spaceId}/contents/${i}?token=${e.token}${o}${c}${l}${u}`;if(e.debug&&console.log(s,`getContentById fetch url : `,d),r.has(d))return e.debug&&console.log(s,`getContentById cache hit`),r.get(d);try{let t=await fetch(d,n);e.debug&&console.log(s,`getContentById status : `,t.status);let i=await t.json();return r.set(d,i),i}catch(e){return console.error(s,`getContentById error : `,e),{}}},async getTranslations(i){e.debug&&console.log(s,`getTranslations() locale : `,i);let a=`${t}/api/v1/spaces/${e.spaceId}/translations/${i}?token=${e.token}`;if(e.debug&&console.log(s,`getTranslations fetch url : `,a),r.has(a))return e.debug&&console.log(s,`getTranslations cache hit`),r.get(a);try{let t=await fetch(a,n);e.debug&&console.log(s,`getTranslations status : `,t.status);let i=await t.json();return r.set(a,i),i}catch(e){return console.error(s,`getTranslations error : `,e),{}}},syncScriptUrl(){return`${t}/scripts/sync-v1.js`},assetLink(n){return typeof n==`string`?`${t}/api/v1/spaces/${e.spaceId}/assets/${n}`:`${t}/api/v1/spaces/${e.spaceId}/assets/${n.uri}`}}}function l(e){return{"data-ll-id":e._id,"data-ll-schema":e._schema}}function u(e){return{"data-ll-field":e}}var d=`localess-js-sync`;function f(e,t=!1){if(r()||!i()||window.localess!==void 0||document.getElementById(d))return;let n=document.createElement(`script`);n.id=d,n.type=`text/javascript`,n.src=`${e}/scripts/sync-v1.js`,n.async=!0,n.onerror=e=>console.error(e),n.onload=e=>console.info(`Localess Sync Script loaded`),document.head.appendChild(n)}exports.isBrowser=n,exports.isIframe=i,exports.isServer=r,exports.loadLocalessSync=f,exports.localessClient=c,exports.localessEditable=l,exports.localessEditableField=u;