@chaibuilder/pages 0.1.2 → 0.1.4

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.d.ts CHANGED
@@ -7,23 +7,29 @@ export declare type ChaiBuilderPage = {
7
7
  seo: Record<string, any>;
8
8
  };
9
9
 
10
- export declare class ChaiBuilderPages {
11
- private apiKey;
12
- private apiUrl;
10
+ export declare class ChaiBuilderPages implements ChaiBuilderPagesInterface {
11
+ private backend;
13
12
  private fallbackLang;
14
13
  private currentLang;
15
14
  private draftMode;
16
- constructor(apiKey: string, apiUrl?: string);
15
+ private auth;
16
+ constructor(backend?: ChaiBuilderPagesBackendInterface);
17
+ private _registerPageTypes;
18
+ setAuth(auth: ChaiBuilderPagesAuthInterface): void;
17
19
  setFallbackLang(lang: string): void;
18
20
  getFallbackLang(): string;
19
21
  setCurrentLang(lang: string): void;
20
22
  getCurrentLang(): string;
21
23
  setDraftMode(draft: boolean): void;
22
24
  isDraftMode(): boolean;
23
- handle(body: any, chaiUser: string): Promise<any>;
25
+ verifyToken(): Promise<boolean>;
26
+ getUserInfo(userId: string): Promise<ChaiBuilderUserInfo>;
27
+ handle(body: {
28
+ action: string;
29
+ data: Record<string, any>;
30
+ }): Promise<any>;
24
31
  emit(action: string, data: any): Promise<void> | undefined;
25
32
  setLanguageFromSlug(slug: string[]): void;
26
- getPageTypeFromSlug(slug: string): Promise<string>;
27
33
  resolveLink(pageTypeKey: string, id: string): Promise<any>;
28
34
  getPageBySlug(slug: string): Promise<any>;
29
35
  getFullPage(id: string): Promise<any>;
@@ -35,6 +41,55 @@ export declare class ChaiBuilderPages {
35
41
  request(body: any): Promise<any>;
36
42
  }
37
43
 
44
+ declare interface ChaiBuilderPagesAuthInterface {
45
+ isUserActive(chaiUser: string): Promise<boolean>;
46
+ getUserPermissions(chaiUser: string): Promise<string[]>;
47
+ verifyToken(): Promise<boolean>;
48
+ getUserId(): string;
49
+ }
50
+
51
+ export declare class ChaiBuilderPagesBackend implements ChaiBuilderPagesBackendInterface {
52
+ private apiKey;
53
+ private apiUrl;
54
+ constructor(apiKey?: string, apiUrl?: string);
55
+ handleAction(body: any, chaiUser?: string): Promise<any>;
56
+ }
57
+
58
+ export declare interface ChaiBuilderPagesBackendInterface {
59
+ handleAction(body: any, chaiUser?: string): Promise<any>;
60
+ }
61
+
62
+ declare interface ChaiBuilderPagesInterface {
63
+ setFallbackLang(lang: string): void;
64
+ getFallbackLang(): string;
65
+ setCurrentLang(lang: string): void;
66
+ getCurrentLang(): string;
67
+ setDraftMode(draft: boolean): void;
68
+ isDraftMode(): boolean;
69
+ setAuth(auth: ChaiBuilderPagesAuthInterface): void;
70
+ verifyToken(): Promise<boolean>;
71
+ getUserInfo(userId: string): Promise<ChaiBuilderUserInfo>;
72
+ handle(body: any): Promise<any>;
73
+ emit(action: string, data: any): void;
74
+ setLanguageFromSlug(slug: string[]): void;
75
+ resolveLink(pageTypeKey: string, id: string): Promise<string>;
76
+ getPageBySlug(slug: string): Promise<any>;
77
+ getFullPage(id: string): Promise<any>;
78
+ getSiteSettings(): Promise<any>;
79
+ getPageData(pageType: string, props: any): Promise<any>;
80
+ getGlobalData(): Promise<any>;
81
+ }
82
+
83
+ export declare class ChaiBuilderPagesMockAuth implements ChaiBuilderPagesAuthInterface {
84
+ private token;
85
+ private userId;
86
+ constructor(token?: string);
87
+ verifyToken(): Promise<boolean>;
88
+ isUserActive(): Promise<boolean>;
89
+ getUserPermissions(): Promise<string[]>;
90
+ getUserId(): string;
91
+ }
92
+
38
93
  export declare type ChaiBuilderPageType = {
39
94
  key: string;
40
95
  helpText?: string;
@@ -51,6 +106,19 @@ export declare type ChaiBuilderPageType = {
51
106
  dataProvider?: (props: any, lang: string, isDraft?: boolean, inBuilder?: boolean) => Promise<Record<string, any>>;
52
107
  };
53
108
 
109
+ export declare type ChaiBuilderTaxonomyType = {
110
+ key: string;
111
+ name: string;
112
+ slug?: string;
113
+ };
114
+
115
+ declare type ChaiBuilderUserInfo = {
116
+ id: string;
117
+ email: string;
118
+ name: string;
119
+ avatar?: string;
120
+ };
121
+
54
122
  export declare const getChaiGlobalData: (lang: string, isDraft?: boolean, inBuilder?: boolean) => Promise<any>;
55
123
 
56
124
  export declare const getChaiPageType: (key: keyof typeof PAGE_TYPES) => ChaiBuilderPageType;
@@ -71,4 +139,6 @@ export declare const registerChaiPageType: (key: string, pageTypeOptions: Omit<C
71
139
 
72
140
  export declare const registerChaiPartialType: (key: string, pageTypeOptions: Omit<ChaiBuilderPageType, "key">) => void;
73
141
 
142
+ export declare const registerChaiTaxonomy: (key: string, taxonomyOptions: Omit<ChaiBuilderTaxonomyType, "key">) => void;
143
+
74
144
  export { }
package/dist/server.js CHANGED
@@ -1,30 +1,8 @@
1
- var p = Object.defineProperty;
2
- var S = (i, a, t) => a in i ? p(i, a, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[a] = t;
3
- var c = (i, a, t) => S(i, typeof a != "symbol" ? a + "" : a, t);
4
- import { get as g, first as k, has as y } from "lodash-es";
5
- const w = "https://api.chaibuilder.com", E = async (i, a, t, s = w) => {
6
- try {
7
- const e = new Headers();
8
- e.append("x-chai-api-key", a), e.append("x-chai-user", t);
9
- const n = {
10
- headers: e,
11
- method: "POST",
12
- body: JSON.stringify(i),
13
- cache: "no-store"
14
- }, r = await fetch(
15
- s + "/v1/api/chai",
16
- n
17
- );
18
- if (r.ok)
19
- return await r.json();
20
- {
21
- const o = await r.json();
22
- throw new Error((o == null ? void 0 : o.error) || r.statusText);
23
- }
24
- } catch (e) {
25
- return { error: e.message };
26
- }
27
- }, b = {
1
+ var k = Object.defineProperty;
2
+ var b = (i, a, t) => a in i ? k(i, a, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[a] = t;
3
+ var s = (i, a, t) => b(i, typeof a != "symbol" ? a + "" : a, t);
4
+ import { get as _, first as f, has as S } from "lodash-es";
5
+ const w = {
28
6
  ab: "Abkhazian",
29
7
  aa: "Afar",
30
8
  af: "Afrikaans",
@@ -212,31 +190,107 @@ const w = "https://api.chaibuilder.com", E = async (i, a, t, s = w) => {
212
190
  yo: "Yoruba",
213
191
  za: "Zhuang, Chuang",
214
192
  zu: "Zulu"
215
- }, l = {}, C = (i, a) => {
216
- l[i] = { key: i, ...a, hasSlug: !0 };
217
- }, P = (i, a) => {
218
- l[i] = { key: i, ...a, hasSlug: !1 };
219
- }, d = () => Object.values(l), h = (i) => l[i];
220
- let f = async (i, a = !1, t = !1) => ({
193
+ }, c = {}, A = (i, a) => {
194
+ c[i] = { key: i, ...a, hasSlug: !0 };
195
+ }, E = (i, a) => {
196
+ c[i] = { key: i, ...a, hasSlug: !1 };
197
+ }, g = () => Object.values(c), l = (i) => c[i];
198
+ let m = async (i, a = !1, t = !1) => ({
221
199
  lang: i,
222
200
  isDraft: a,
223
201
  inBuilder: t
224
202
  });
225
- const L = (i) => f = i, m = async (i, a, t) => await f(i, a, t), v = "https://api.chaibuilder.com";
226
- class G {
227
- constructor(a, t = v) {
228
- c(this, "fallbackLang", "");
229
- c(this, "currentLang", "");
230
- c(this, "draftMode", !1);
231
- this.apiKey = a, this.apiUrl = t, C("page", {
203
+ const T = (i) => m = i, d = async (i, a, t) => await m(i, a, t), G = (i, a) => {
204
+ ({ ...a });
205
+ }, v = "https://api.chaibuilder.com";
206
+ class p {
207
+ constructor(a = "") {
208
+ s(this, "userId", "");
209
+ this.token = a, console.log("Token", this.token);
210
+ }
211
+ async verifyToken() {
212
+ return this.userId = "mock-chai-user", !0;
213
+ }
214
+ async isUserActive() {
215
+ return !0;
216
+ }
217
+ async getUserPermissions() {
218
+ return [
219
+ "add_block",
220
+ "delete_block",
221
+ "edit_block",
222
+ "move_block",
223
+ "edit_theme",
224
+ "save_page",
225
+ "edit_styles",
226
+ "import_html",
227
+ "add_page",
228
+ "edit_page",
229
+ "edit_slug",
230
+ "delete_page",
231
+ "publish_page",
232
+ "unpublish_page",
233
+ "add_partial",
234
+ "edit_partial",
235
+ "delete_partial",
236
+ "publish_partial",
237
+ "unpublish_partial",
238
+ "edit_seo",
239
+ "restore_revision",
240
+ "delete_revision",
241
+ "publish_theme",
242
+ "add_library_block",
243
+ "add_library_template",
244
+ "edit_library_block",
245
+ "edit_library_template",
246
+ "delete_library_block",
247
+ "delete_library_template",
248
+ "add_media",
249
+ "edit_media",
250
+ "delete_media"
251
+ ];
252
+ }
253
+ getUserId() {
254
+ return this.userId;
255
+ }
256
+ }
257
+ class C {
258
+ constructor(a = "", t = v) {
259
+ this.apiKey = a, this.apiUrl = t;
260
+ }
261
+ async handleAction(a, t = "") {
262
+ return await (await fetch(`${this.apiUrl}/v1/api/chai`, {
263
+ cache: "no-store",
264
+ method: "POST",
265
+ body: JSON.stringify(a),
266
+ headers: {
267
+ "x-chai-api-key": this.apiKey,
268
+ ...t ? { "x-chai-user": t } : {}
269
+ }
270
+ })).json();
271
+ }
272
+ }
273
+ class L {
274
+ constructor(a = new C()) {
275
+ s(this, "fallbackLang", "");
276
+ s(this, "currentLang", "");
277
+ s(this, "draftMode", !1);
278
+ s(this, "auth", new p());
279
+ this.backend = a, this._registerPageTypes();
280
+ }
281
+ _registerPageTypes() {
282
+ A("page", {
232
283
  name: "Static Page",
233
284
  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>'
234
- }), P("global", {
285
+ }), E("global", {
235
286
  name: "Global Block",
236
287
  helpText: "A global block can be reused in multiple pages.",
237
288
  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>'
238
289
  });
239
290
  }
291
+ setAuth(a) {
292
+ this.auth = a;
293
+ }
240
294
  setFallbackLang(a) {
241
295
  this.fallbackLang = a;
242
296
  }
@@ -255,9 +309,28 @@ class G {
255
309
  isDraftMode() {
256
310
  return this.draftMode;
257
311
  }
258
- async handle(a, t) {
312
+ async verifyToken() {
313
+ return await this.auth.verifyToken();
314
+ }
315
+ async getUserInfo(a) {
316
+ return {
317
+ id: a,
318
+ email: `${a}@chaibuilder.com`,
319
+ name: "Mock Chai User",
320
+ avatar: "https://i.pravatar.cc/300"
321
+ };
322
+ }
323
+ async handle(a) {
324
+ switch (a.action) {
325
+ case "CHECK_USER_STATUS":
326
+ return this.auth.isUserActive(a.data.authToken);
327
+ case "GET_PERMISSIONS":
328
+ return this.auth.getUserPermissions(a.data.authToken);
329
+ case "GET_CHAI_USER":
330
+ return this.getUserInfo(a.data.userId);
331
+ }
259
332
  if (a.action === "GET_BUILDER_PAGE_DATA") {
260
- const e = await m(a.data.lang, !0, !0), n = h(
333
+ const e = await d(a.data.lang, !0, !0), n = l(
261
334
  a.data.pageType
262
335
  );
263
336
  return n ? {
@@ -265,9 +338,9 @@ class G {
265
338
  global: e
266
339
  } : { global: e };
267
340
  }
268
- if (a.action === "GET_PAGE_TYPES" || a.action === "GET_COLLECTIONS")
341
+ if (a.action === "GET_PAGE_TYPES")
269
342
  return Promise.all(
270
- d().map(async (e) => ({
343
+ g().map(async (e) => ({
271
344
  key: e.key,
272
345
  helpText: e.helpText ?? "",
273
346
  icon: e.icon ?? "",
@@ -277,135 +350,102 @@ class G {
277
350
  name: typeof e.name == "function" ? await e.name() : e.name
278
351
  }))
279
352
  );
280
- if (a.action === "SEARCH_COLLECTION_ITEMS" || a.action === "SEARCH_PAGE_TYPE_ITEMS") {
281
- const e = h(
353
+ if (a.action === "SEARCH_PAGE_TYPE_ITEMS") {
354
+ const e = l(
282
355
  a.data.pageType
283
356
  );
284
- return e ? e.search ? await e.search(a.data.query) : await (await fetch(`${this.apiUrl}/v1/api/chai`, {
285
- cache: "no-store",
286
- method: "POST",
287
- body: JSON.stringify({
288
- action: "SEARCH_PAGES",
289
- data: { query: a.data.query, pageType: a.data.pageType }
290
- }),
291
- headers: { "x-chai-api-key": this.apiKey }
292
- })).json() : { error: "Page type not found" };
357
+ return e ? e.search ? await e.search(a.data.query) : this.backend.handleAction(a) : { error: "Page type not found" };
293
358
  }
294
- const s = await E(a, this.apiKey, t, this.apiUrl);
295
- return this.emit(a.action, s), s;
359
+ const t = await this.backend.handleAction(a, this.auth.getUserId());
360
+ return this.emit(a.action, t), t;
296
361
  }
297
362
  emit(a, t) {
298
- var n, r, o;
299
- if (!["CREATE_PAGE", "UPDATE_PAGE", "DELETE_PAGE"].includes(a))
300
- return;
301
- const s = g(t, "page");
302
- if (!s)
303
- return;
304
- const e = h(s.pageType);
305
- if (e)
363
+ var r, o, h;
364
+ if (!["CREATE_PAGE", "UPDATE_PAGE", "DELETE_PAGE"].includes(a)) return;
365
+ const e = _(t, "page");
366
+ if (!e) return;
367
+ const n = l(e.pageType);
368
+ if (n)
306
369
  switch (a) {
307
370
  case "CREATE_PAGE":
308
- return (n = e.onCreate) == null ? void 0 : n.call(e, s);
371
+ return (r = n.onCreate) == null ? void 0 : r.call(n, e);
309
372
  case "UPDATE_PAGE":
310
- return (r = e.onUpdate) == null ? void 0 : r.call(e, s);
373
+ return (o = n.onUpdate) == null ? void 0 : o.call(n, e);
311
374
  case "DELETE_PAGE":
312
- return (o = e.onDelete) == null ? void 0 : o.call(e, s);
375
+ return (h = n.onDelete) == null ? void 0 : h.call(n, e);
313
376
  }
314
377
  }
315
378
  // APIS
316
379
  setLanguageFromSlug(a) {
317
- const t = k(a || [""]);
318
- Object.keys(b).includes(t) ? this.setCurrentLang(t) : this.setCurrentLang("");
319
- }
320
- async getPageTypeFromSlug(a) {
321
- return a === "/" || a === `/${this.getCurrentLang()}`, "page";
380
+ const t = f(a || [""]);
381
+ Object.keys(w).includes(t) ? this.setCurrentLang(t) : this.setCurrentLang("");
322
382
  }
323
383
  async resolveLink(a, t) {
324
- const s = h(a);
325
- if (!s)
384
+ const e = l(a);
385
+ if (!e)
326
386
  return "/not-found";
327
- const e = this.isDraftMode(), n = this.getCurrentLang();
328
- if (s.resolveLink)
329
- return await s.resolveLink(t, e, n);
330
- const o = await (await fetch(`${this.apiUrl}/v1/api/chai`, {
331
- method: "POST",
332
- body: JSON.stringify({
333
- action: "GET_LINK",
334
- data: {
335
- pageType: a,
336
- id: t,
337
- draft: e,
338
- ...n ? { lang: n } : {}
339
- }
340
- }),
341
- headers: { "x-chai-api-key": this.apiKey }
342
- })).json();
343
- return g(o, "link", "/not-found");
387
+ const n = this.isDraftMode(), r = this.getCurrentLang();
388
+ return e.resolveLink ? await e.resolveLink(t, n, r) : await this.backend.handleAction({
389
+ action: "GET_LINK",
390
+ data: {
391
+ pageType: a,
392
+ id: t,
393
+ draft: n,
394
+ lang: r
395
+ }
396
+ });
344
397
  }
345
398
  async getPageBySlug(a) {
346
- const t = this.isDraftMode(), s = {};
347
- return d().forEach((n) => {
348
- y(n, "dynamicSegments") && n.dynamicSegments && (s[n.key] = n.dynamicSegments);
349
- }), (await fetch(`${this.apiUrl}/v1/api/chai`, {
350
- method: "POST",
351
- body: JSON.stringify({
352
- action: "GET_PAGE_META",
353
- data: { slug: a, draft: t, dynamicSegments: s }
354
- }),
355
- headers: { "x-chai-api-key": this.apiKey }
356
- })).json();
399
+ const t = this.isDraftMode(), e = {};
400
+ return g().forEach((n) => {
401
+ S(n, "dynamicSegments") && n.dynamicSegments && (e[n.key] = n.dynamicSegments);
402
+ }), await this.backend.handleAction({
403
+ action: "GET_PAGE_META",
404
+ data: { slug: a, draft: t, dynamicSegments: e }
405
+ });
357
406
  }
358
407
  async getFullPage(a) {
359
- const t = this.isDraftMode(), s = this.getCurrentLang();
360
- return (await fetch(`${this.apiUrl}/v1/api/chai`, {
361
- method: "POST",
362
- body: JSON.stringify({
363
- action: "GET_PAGE",
364
- data: { id: a, lang: s, draft: t, mergePartials: !0 }
365
- }),
366
- headers: { "x-chai-api-key": this.apiKey }
367
- })).json();
408
+ const t = this.isDraftMode(), e = this.getCurrentLang();
409
+ return await this.backend.handleAction({
410
+ action: "GET_PAGE",
411
+ data: { id: a, lang: e, draft: t, mergePartials: !0 }
412
+ });
368
413
  }
369
414
  async getSiteSettings() {
370
415
  const a = this.isDraftMode();
371
- return (await fetch(`${this.apiUrl}/v1/api/chai`, {
372
- method: "POST",
373
- body: JSON.stringify({
374
- action: "GET_WEBSITE_SETTINGS",
375
- data: { draft: a }
376
- }),
377
- headers: { "x-chai-api-key": this.apiKey }
378
- })).json();
416
+ return await this.backend.handleAction({
417
+ action: "GET_WEBSITE_SETTINGS",
418
+ data: { draft: a }
419
+ });
379
420
  }
380
421
  async getPageData(a, t) {
381
422
  var u;
382
- const s = this.isDraftMode(), e = this.getCurrentLang(), n = h(a), [r, o] = await Promise.all([
423
+ const e = this.isDraftMode(), n = this.getCurrentLang(), r = l(a), [o, h] = await Promise.all([
383
424
  this.getGlobalData(),
384
- ((u = n == null ? void 0 : n.dataProvider) == null ? void 0 : u.call(n, t, e, s)) || Promise.resolve({})
425
+ ((u = r == null ? void 0 : r.dataProvider) == null ? void 0 : u.call(r, t, n, e)) || Promise.resolve({})
385
426
  ]);
386
- return n ? {
387
- ...o,
388
- global: r
389
- } : { global: r };
427
+ return r ? {
428
+ ...h,
429
+ global: o
430
+ } : { global: o };
390
431
  }
391
432
  async getGlobalData() {
392
433
  const a = this.getCurrentLang();
393
- return await m(a, this.isDraftMode());
434
+ return await d(a, this.isDraftMode());
394
435
  }
395
436
  async request(a) {
396
- return (await fetch(`${this.apiUrl}/v1/api/chai`, {
397
- method: "POST",
398
- body: JSON.stringify(a),
399
- headers: { "x-chai-api-key": this.apiKey }
400
- })).json();
437
+ return await this.backend.handleAction(a);
401
438
  }
402
439
  }
403
440
  export {
404
- G as ChaiBuilderPages,
405
- m as getChaiGlobalData,
406
- h as getChaiPageType,
407
- d as getChaiPageTypes,
408
- L as registerChaiGlobalDataProvider,
409
- C as registerChaiPageType,
410
- P as registerChaiPartialType
441
+ L as ChaiBuilderPages,
442
+ C as ChaiBuilderPagesBackend,
443
+ p as ChaiBuilderPagesMockAuth,
444
+ d as getChaiGlobalData,
445
+ l as getChaiPageType,
446
+ g as getChaiPageTypes,
447
+ T as registerChaiGlobalDataProvider,
448
+ A as registerChaiPageType,
449
+ E as registerChaiPartialType,
450
+ G as registerChaiTaxonomy
411
451
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "Suraj Air",
4
4
  "license": "Private",
5
5
  "homepage": "https://chaibuilder.com",
6
- "version": "0.1.2",
6
+ "version": "0.1.4",
7
7
  "type": "module",
8
8
  "files": [
9
9
  "dist"
@@ -59,7 +59,7 @@
59
59
  "test": "vitest"
60
60
  },
61
61
  "peerDependencies": {
62
- "@chaibuilder/sdk": "2.0.2",
62
+ "@chaibuilder/sdk": "2.0.4",
63
63
  "@types/react": "*",
64
64
  "@types/react-dom": "*",
65
65
  "react": "^18.0 || ^19.0",
@@ -78,7 +78,7 @@
78
78
  },
79
79
  "dependencies": {
80
80
  "@chaibuilder/runtime": "2.0.0",
81
- "@chaibuilder/sdk": "2.0.2",
81
+ "@chaibuilder/sdk": "2.0.4",
82
82
  "@mhsdesign/jit-browser-tailwindcss": "^0.4.2",
83
83
  "@tailwindcss/aspect-ratio": "^0.4.2",
84
84
  "@tailwindcss/forms": "^0.5.10",