@chaibuilder/pages 0.3.0 → 0.3.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/dist/server.cjs +1 -1
- package/dist/server.js +37 -32
- package/package.json +1 -1
package/dist/server.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var P=Object.defineProperty;var A=(
|
|
1
|
+
"use strict";var P=Object.defineProperty;var A=(i,a,t)=>a in i?P(i,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[a]=t;var l=(i,a,t)=>A(i,typeof a!="symbol"?a+"":a,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("lodash-es"),u={},k=(i,a)=>{u[i]={key:i,...a,hasSlug:!0}},f=(i,a)=>{u[i]={key:i,...a,hasSlug:!1}},g=()=>Object.values(u),s=i=>u[i];let b=async i=>({...i});const C=i=>b=i,d=async i=>{try{return await b(i)}catch(a){return console.error(a),{}}};class w{async verifyTokenAndGetUser(a){return{id:"mock-chai-user",name:"Mock Chai User",email:"mock-chai-user@chaibuilder.com"}}async isUserActive(a){return!0}async getUserRoleAndPermissions(a){return{role:"admin",permissions:null}}async getUserInfo(a){return{id:"mock-chai-users",email:"mock-chai-user@chaibuilder.com",name:"Mock Chai User",avatar:"https://i.pravatar.cc/300"}}}const S={ab:"Abkhazian",aa:"Afar",af:"Afrikaans",ak:"Akan",sq:"Albanian",am:"Amharic",ar:"Arabic",an:"Aragonese",hy:"Armenian",as:"Assamese",av:"Avaric",ae:"Avestan",ay:"Aymara",az:"Azerbaijani",bm:"Bambara",ba:"Bashkir",eu:"Basque",be:"Belarusian",bn:"Bengali",bh:"Bihari",bi:"Bislama",bs:"Bosnian",br:"Breton",bg:"Bulgarian",my:"Burmese",ca:"Catalan",ch:"Chamorro",ce:"Chechen",ny:"Chichewa",zh:"Chinese","zh-Hans":"Chinese (Simplified)","zh-Hant":"Chinese (Traditional)",cv:"Chuvash",kw:"Cornish",co:"Corsican",cr:"Cree",hr:"Croatian",cs:"Czech",da:"Danish",dv:"Maldivian",nl:"Dutch",dz:"Dzongkha",en:"English",eo:"Esperanto",et:"Estonian",ee:"Ewe",fo:"Faroese",fj:"Fijian",fi:"Finnish",fr:"French",ff:"Fula, Pular",gl:"Galician",gd:"Gaelic (Scottish)",gv:"Manx",ka:"Georgian",de:"German",el:"Greek",kl:"Kalaallisut",gn:"Guarani",gu:"Gujarati",ht:"Haitian Creole",ha:"Hausa",he:"Hebrew",hz:"Herero",hi:"Hindi",ho:"Hiri Motu",hu:"Hungarian",is:"Icelandic",io:"Ido",ig:"Igbo",id:"Indonesian",ia:"Interlingua",ie:"Interlingue",iu:"Inuktitut",ik:"Inupiak",ga:"Irish",it:"Italian",ja:"Japanese",jv:"Javanese",kn:"Kannada",kr:"Kanuri",ks:"Kashmiri",kk:"Kazakh",km:"Khmer",ki:"Kikuyu",rw:"Kinyarwanda",rn:"Kirundi",ky:"Kyrgyz",kv:"Komi",kg:"Kongo",ko:"Korean",ku:"Kurdish",kj:"Kwanyama",lo:"Lao",la:"Latin",lv:"Latvian",li:"Limburgish",ln:"Lingala",lt:"Lithuanian",lu:"Luga-Katanga",lg:"Luganda, Ganda",lb:"Luxembourgish",mk:"Macedonian",mg:"Malagasy",ms:"Malay",ml:"Malayalam",mt:"Maltese",mi:"Maori",mr:"Marathi",mh:"Marshallese",mo:"Moldavian",mn:"Mongolian",na:"Nauru",nv:"Navajo",ng:"Ndonga",nd:"Northern Ndebele",ne:"Nepali",no:"Norwegian",nb:"Norwegian bokmål",nn:"Norwegian nynorsk",ii:"Sichuan Yi",oc:"Occitan",oj:"Ojibwe",cu:"Old Church Slavonic",or:"Oriya",om:"Oromo",os:"Ossetian",pi:"Pāli",ps:"Pashto, Pushto",fa:"Persian (Farsi)",pl:"Polish",pt:"Portuguese",pa:"Punjabi (Eastern)",qu:"Quechua",rm:"Romansh",ro:"Romanian",ru:"Russian",se:"Sami",sm:"Samoan",sg:"Sango",sa:"Sanskrit",sr:"Serbian",sh:"Serbo-Croatian",st:"Sesotho",tn:"Setswana",sn:"Shona",sd:"Sindhi",si:"Sinhalese",ss:"Swati",sk:"Slovak",sl:"Slovenian",so:"Somali",nr:"Southern Ndebele",es:"Spanish",su:"Sundanese",sw:"Swahili (Kiswahili)",sv:"Swedish",tl:"Tagalog",ty:"Tahitian",tg:"Tajik",ta:"Tamil",tt:"Tatar",te:"Telugu",th:"Thai",bo:"Tibetan",ti:"Tigrinya",to:"Tonga",ts:"Tsonga",tr:"Turkish",tk:"Turkmen",tw:"Twi",ug:"Uyghur",uk:"Ukrainian",ur:"Urdu",uz:"Uzbek",ve:"Venda",vi:"Vietnamese",vo:"Volapük",wa:"Wallon",cy:"Welsh",wo:"Wolof",fy:"Western Frisian",xh:"Xhosa",yi:"Yiddish",yo:"Yoruba",za:"Zhuang, Chuang",zu:"Zulu"};class y{constructor(a){l(this,"fallbackLang","");l(this,"currentLang","");l(this,"draftMode",!1);l(this,"userMgmt",new w);this.backend=a,this._registerDefaultPageTypes()}_registerDefaultPageTypes(){k("page",{name:"Static Page",icon:'<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M5 8V20H19V8H5ZM5 6H19V4H5V6ZM20 22H4C3.44772 22 3 21.5523 3 21V3C3 2.44772 3.44772 2 4 2H20C20.5523 2 21 2.44772 21 3V21C21 21.5523 20.5523 22 20 22ZM7 10H11V14H7V10ZM7 16H17V18H7V16ZM13 11H17V13H13V11Z"></path></svg>'}),f("global",{name:"Global Block",helpText:"A global block can be reused in multiple pages.",icon:'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-globe w-4 h-4 text-slate-500 stroke-[1]"><circle cx="12" cy="12" r="10"></circle><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"></path><path d="M2 12h20"></path></svg>'})}setUserManagement(a){this.userMgmt=a}setFallbackLang(a){if(c.isEmpty(a))throw new Error("Fallback language cannot be empty");this.fallbackLang=a}getFallbackLang(){return this.fallbackLang}setCurrentLang(a){if(c.isEmpty(a))throw new Error("Current language cannot be empty");this.currentLang=a}getCurrentLang(){return this.currentLang}setDraftMode(a){this.draftMode=a}isDraftMode(){return this.draftMode}async handleUpload(a,t=""){return await this.backend.handleUpload(a,t)}async handle(a,t){switch(a.action){case"CHECK_USER_STATUS":return this.userMgmt.isUserActive(t);case"GET_ROLE_AND_PERMISSIONS":return this.userMgmt.getUserRoleAndPermissions(t);case"GET_CHAI_USER":return this.userMgmt.getUserInfo(t)}if(a.action==="GET_BUILDER_PAGE_DATA"){const e=await d({lang:a.data.lang,draft:!0,inBuilder:!0}),r=s(a.data.pageType);return r?{...r.dataProvider?await r.dataProvider({lang:a.data.lang,draft:!0,inBuilder:!0}):{},global:e}:{global:e}}if(a.action==="GET_PAGE_TYPES")return Promise.all(g().map(async e=>({key:e.key,helpText:e.helpText??"",icon:e.icon??"",dynamicSegments:e.dynamicSegments??"",dynamicSlug:e.dynamicSlug??"",hasSlug:e.hasSlug??!0,name:typeof e.name=="function"?await e.name():e.name})));if(a.action==="SEARCH_PAGE_TYPE_ITEMS"){const e=s(a.data.pageType);return e?e.search?await e.search(a.data.query):this.backend.handleAction(a):{error:"Page type not found"}}const n=await this.backend.handleAction(a,t);return this.emit(a.action,n),n}emit(a,t){var r,o,h;if(!["CREATE_PAGE","UPDATE_PAGE","DELETE_PAGE"].includes(a))return;const n=c.get(t,"page");if(!n)return;const e=s(n.pageType);if(e)switch(a){case"CREATE_PAGE":return(r=e.onCreate)==null?void 0:r.call(e,n);case"UPDATE_PAGE":return(o=e.onUpdate)==null?void 0:o.call(e,n);case"DELETE_PAGE":return(h=e.onDelete)==null?void 0:h.call(e,n)}}setLanguageFromSlug(a){if(!this.getFallbackLang())throw new Error("Please set a fallback language using setFallbackLang(lang: string)");const t=c.first(a||[""]);Object.keys(S).includes(t)?this.setCurrentLang(t):this.setCurrentLang(this.getFallbackLang())}async resolveLink(a,t){const n=s(a);if(!n)return"/not-found";const e=this.isDraftMode(),r=this.getCurrentLang();return n.resolveLink?await n.resolveLink(t,e,r):await this.backend.handleAction({action:"GET_LINK",data:{pageType:a,id:t,draft:e,lang:r}})}async getPageBySlug(a){const t=this.isDraftMode(),n={};return g().forEach(e=>{c.has(e,"dynamicSegments")&&e.dynamicSegments&&(n[e.key]=e.dynamicSegments)}),await this.backend.handleAction({action:"GET_PAGE_META",data:{slug:a,draft:t,dynamicSegments:n}})}async getFullPage(a){const t=this.isDraftMode(),n=this.getCurrentLang(),e=this.getFallbackLang();return await this.backend.handleAction({action:"GET_PAGE",data:{id:a,lang:e===n?void 0:n,draft:t,mergePartials:!0}})}async getSiteSettings(){const a=this.isDraftMode();return await this.backend.handleAction({action:"GET_WEBSITE_SETTINGS",data:{draft:a}})}async getPageData(a,t={}){var m;const n=this.isDraftMode(),e=this.getCurrentLang(),r=s(a),[o,h]=await Promise.all([this.getGlobalData(),((m=r==null?void 0:r.dataProvider)==null?void 0:m.call(r,{lang:e,draft:n,inBuilder:!1,...t}))||Promise.resolve({})]);return r?{...h,global:o}:{global:o}}async getGlobalData(){const a=this.getCurrentLang();return await d({lang:a,draft:this.isDraftMode(),inBuilder:!1})}async request(a){return await this.backend.handleAction(a)}}class p{constructor(a="",t=E){this.apiKey=a,this.apiUrl=t}async handleAction(a,t=""){return await(await fetch(`${this.apiUrl}/v1/api/chai`,{cache:"no-store",method:"POST",body:JSON.stringify(a),headers:{"x-chai-api-key":this.apiKey,...t?{"x-chai-user":t}:{}}})).json()}async handleUpload(a,t=""){return console.log("handleUpload",typeof a.get("file")),await(await fetch(`${this.apiUrl}/v1/api/chai`,{method:"POST",body:a,headers:{"x-chai-api-key":this.apiKey,"x-chai-upload":"true",...t?{"x-chai-user":t}:{}}})).json()}}const E="https://api.chaibuilder.com";exports.API_URL=E;exports.ChaiBuilderPages=y;exports.ChaiBuilderPagesBackend=p;exports.ChaiBuilderPagesUserManagement=w;exports.LANGUAGES=S;exports.getChaiGlobalData=d;exports.getChaiPageType=s;exports.getChaiPageTypes=g;exports.registerChaiGlobalDataProvider=C;exports.registerChaiPageType=k;exports.registerChaiPartialType=f;
|
package/dist/server.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
var f = Object.defineProperty;
|
|
2
|
-
var b = (
|
|
3
|
-
var o = (
|
|
2
|
+
var b = (i, a, n) => a in i ? f(i, a, { enumerable: !0, configurable: !0, writable: !0, value: n }) : i[a] = n;
|
|
3
|
+
var o = (i, a, n) => b(i, typeof a != "symbol" ? a + "" : a, n);
|
|
4
4
|
import { isEmpty as g, get as w, first as S, has as E } from "lodash-es";
|
|
5
|
-
const h = {}, A = (
|
|
6
|
-
h[
|
|
7
|
-
}, C = (
|
|
8
|
-
h[
|
|
9
|
-
}, d = () => Object.values(h), l = (
|
|
10
|
-
let k = async (
|
|
11
|
-
...
|
|
5
|
+
const h = {}, A = (i, a) => {
|
|
6
|
+
h[i] = { key: i, ...a, hasSlug: !0 };
|
|
7
|
+
}, C = (i, a) => {
|
|
8
|
+
h[i] = { key: i, ...a, hasSlug: !1 };
|
|
9
|
+
}, d = () => Object.values(h), l = (i) => h[i];
|
|
10
|
+
let k = async (i) => ({
|
|
11
|
+
...i
|
|
12
12
|
});
|
|
13
|
-
const L = (
|
|
13
|
+
const L = (i) => k = i, m = async (i) => {
|
|
14
14
|
try {
|
|
15
|
-
return await k(
|
|
15
|
+
return await k(i);
|
|
16
16
|
} catch (a) {
|
|
17
17
|
return console.error(a), {};
|
|
18
18
|
}
|
|
@@ -326,23 +326,23 @@ class T {
|
|
|
326
326
|
);
|
|
327
327
|
return e ? e.search ? await e.search(a.data.query) : this.backend.handleAction(a) : { error: "Page type not found" };
|
|
328
328
|
}
|
|
329
|
-
const
|
|
330
|
-
return this.emit(a.action,
|
|
329
|
+
const t = await this.backend.handleAction(a, n);
|
|
330
|
+
return this.emit(a.action, t), t;
|
|
331
331
|
}
|
|
332
332
|
emit(a, n) {
|
|
333
333
|
var r, s, c;
|
|
334
334
|
if (!["CREATE_PAGE", "UPDATE_PAGE", "DELETE_PAGE"].includes(a)) return;
|
|
335
|
-
const
|
|
336
|
-
if (!
|
|
337
|
-
const e = l(
|
|
335
|
+
const t = w(n, "page");
|
|
336
|
+
if (!t) return;
|
|
337
|
+
const e = l(t.pageType);
|
|
338
338
|
if (e)
|
|
339
339
|
switch (a) {
|
|
340
340
|
case "CREATE_PAGE":
|
|
341
|
-
return (r = e.onCreate) == null ? void 0 : r.call(e,
|
|
341
|
+
return (r = e.onCreate) == null ? void 0 : r.call(e, t);
|
|
342
342
|
case "UPDATE_PAGE":
|
|
343
|
-
return (s = e.onUpdate) == null ? void 0 : s.call(e,
|
|
343
|
+
return (s = e.onUpdate) == null ? void 0 : s.call(e, t);
|
|
344
344
|
case "DELETE_PAGE":
|
|
345
|
-
return (c = e.onDelete) == null ? void 0 : c.call(e,
|
|
345
|
+
return (c = e.onDelete) == null ? void 0 : c.call(e, t);
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
348
|
// APIS
|
|
@@ -355,11 +355,11 @@ class T {
|
|
|
355
355
|
Object.keys(y).includes(n) ? this.setCurrentLang(n) : this.setCurrentLang(this.getFallbackLang());
|
|
356
356
|
}
|
|
357
357
|
async resolveLink(a, n) {
|
|
358
|
-
const
|
|
359
|
-
if (!
|
|
358
|
+
const t = l(a);
|
|
359
|
+
if (!t)
|
|
360
360
|
return "/not-found";
|
|
361
361
|
const e = this.isDraftMode(), r = this.getCurrentLang();
|
|
362
|
-
return
|
|
362
|
+
return t.resolveLink ? await t.resolveLink(n, e, r) : await this.backend.handleAction({
|
|
363
363
|
action: "GET_LINK",
|
|
364
364
|
data: {
|
|
365
365
|
pageType: a,
|
|
@@ -370,19 +370,24 @@ class T {
|
|
|
370
370
|
});
|
|
371
371
|
}
|
|
372
372
|
async getPageBySlug(a) {
|
|
373
|
-
const n = this.isDraftMode(),
|
|
373
|
+
const n = this.isDraftMode(), t = {};
|
|
374
374
|
return d().forEach((e) => {
|
|
375
|
-
E(e, "dynamicSegments") && e.dynamicSegments && (
|
|
375
|
+
E(e, "dynamicSegments") && e.dynamicSegments && (t[e.key] = e.dynamicSegments);
|
|
376
376
|
}), await this.backend.handleAction({
|
|
377
377
|
action: "GET_PAGE_META",
|
|
378
|
-
data: { slug: a, draft: n, dynamicSegments:
|
|
378
|
+
data: { slug: a, draft: n, dynamicSegments: t }
|
|
379
379
|
});
|
|
380
380
|
}
|
|
381
381
|
async getFullPage(a) {
|
|
382
|
-
const n = this.isDraftMode(),
|
|
382
|
+
const n = this.isDraftMode(), t = this.getCurrentLang(), e = this.getFallbackLang();
|
|
383
383
|
return await this.backend.handleAction({
|
|
384
384
|
action: "GET_PAGE",
|
|
385
|
-
data: {
|
|
385
|
+
data: {
|
|
386
|
+
id: a,
|
|
387
|
+
lang: e === t ? void 0 : t,
|
|
388
|
+
draft: n,
|
|
389
|
+
mergePartials: !0
|
|
390
|
+
}
|
|
386
391
|
});
|
|
387
392
|
}
|
|
388
393
|
async getSiteSettings() {
|
|
@@ -394,11 +399,11 @@ class T {
|
|
|
394
399
|
}
|
|
395
400
|
async getPageData(a, n = {}) {
|
|
396
401
|
var u;
|
|
397
|
-
const
|
|
402
|
+
const t = this.isDraftMode(), e = this.getCurrentLang(), r = l(a), [s, c] = await Promise.all([
|
|
398
403
|
this.getGlobalData(),
|
|
399
404
|
((u = r == null ? void 0 : r.dataProvider) == null ? void 0 : u.call(r, {
|
|
400
405
|
lang: e,
|
|
401
|
-
draft:
|
|
406
|
+
draft: t,
|
|
402
407
|
inBuilder: !1,
|
|
403
408
|
...n
|
|
404
409
|
})) || Promise.resolve({})
|
|
@@ -421,7 +426,7 @@ class T {
|
|
|
421
426
|
}
|
|
422
427
|
}
|
|
423
428
|
class G {
|
|
424
|
-
constructor(a = "", n =
|
|
429
|
+
constructor(a = "", n = v) {
|
|
425
430
|
this.apiKey = a, this.apiUrl = n;
|
|
426
431
|
}
|
|
427
432
|
async handleAction(a, n = "") {
|
|
@@ -448,9 +453,9 @@ class G {
|
|
|
448
453
|
})).json();
|
|
449
454
|
}
|
|
450
455
|
}
|
|
451
|
-
const
|
|
456
|
+
const v = "https://api.chaibuilder.com";
|
|
452
457
|
export {
|
|
453
|
-
|
|
458
|
+
v as API_URL,
|
|
454
459
|
T as ChaiBuilderPages,
|
|
455
460
|
G as ChaiBuilderPagesBackend,
|
|
456
461
|
P as ChaiBuilderPagesUserManagement,
|