@chaibuilder/pages 0.1.3 → 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;
@@ -57,6 +112,13 @@ export declare type ChaiBuilderTaxonomyType = {
57
112
  slug?: string;
58
113
  };
59
114
 
115
+ declare type ChaiBuilderUserInfo = {
116
+ id: string;
117
+ email: string;
118
+ name: string;
119
+ avatar?: string;
120
+ };
121
+
60
122
  export declare const getChaiGlobalData: (lang: string, isDraft?: boolean, inBuilder?: boolean) => Promise<any>;
61
123
 
62
124
  export declare const getChaiPageType: (key: keyof typeof PAGE_TYPES) => ChaiBuilderPageType;
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
- }, C = {
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,33 +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 = {}, b = (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), G = (i, a) => {
203
+ const T = (i) => m = i, d = async (i, a, t) => await m(i, a, t), G = (i, a) => {
226
204
  ({ ...a });
227
205
  }, v = "https://api.chaibuilder.com";
228
- class M {
229
- constructor(a, t = v) {
230
- c(this, "fallbackLang", "");
231
- c(this, "currentLang", "");
232
- c(this, "draftMode", !1);
233
- this.apiKey = a, this.apiUrl = t, b("page", {
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", {
234
283
  name: "Static Page",
235
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>'
236
- }), P("global", {
285
+ }), E("global", {
237
286
  name: "Global Block",
238
287
  helpText: "A global block can be reused in multiple pages.",
239
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>'
240
289
  });
241
290
  }
291
+ setAuth(a) {
292
+ this.auth = a;
293
+ }
242
294
  setFallbackLang(a) {
243
295
  this.fallbackLang = a;
244
296
  }
@@ -257,9 +309,28 @@ class M {
257
309
  isDraftMode() {
258
310
  return this.draftMode;
259
311
  }
260
- 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
+ }
261
332
  if (a.action === "GET_BUILDER_PAGE_DATA") {
262
- const e = await m(a.data.lang, !0, !0), n = h(
333
+ const e = await d(a.data.lang, !0, !0), n = l(
263
334
  a.data.pageType
264
335
  );
265
336
  return n ? {
@@ -267,9 +338,9 @@ class M {
267
338
  global: e
268
339
  } : { global: e };
269
340
  }
270
- if (a.action === "GET_PAGE_TYPES" || a.action === "GET_COLLECTIONS")
341
+ if (a.action === "GET_PAGE_TYPES")
271
342
  return Promise.all(
272
- d().map(async (e) => ({
343
+ g().map(async (e) => ({
273
344
  key: e.key,
274
345
  helpText: e.helpText ?? "",
275
346
  icon: e.icon ?? "",
@@ -279,136 +350,102 @@ class M {
279
350
  name: typeof e.name == "function" ? await e.name() : e.name
280
351
  }))
281
352
  );
282
- if (a.action === "SEARCH_COLLECTION_ITEMS" || a.action === "SEARCH_PAGE_TYPE_ITEMS") {
283
- const e = h(
353
+ if (a.action === "SEARCH_PAGE_TYPE_ITEMS") {
354
+ const e = l(
284
355
  a.data.pageType
285
356
  );
286
- return e ? e.search ? await e.search(a.data.query) : await (await fetch(`${this.apiUrl}/v1/api/chai`, {
287
- cache: "no-store",
288
- method: "POST",
289
- body: JSON.stringify({
290
- action: "SEARCH_PAGES",
291
- data: { query: a.data.query, pageType: a.data.pageType }
292
- }),
293
- headers: { "x-chai-api-key": this.apiKey }
294
- })).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" };
295
358
  }
296
- const s = await E(a, this.apiKey, t, this.apiUrl);
297
- 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;
298
361
  }
299
362
  emit(a, t) {
300
- var n, r, o;
301
- if (!["CREATE_PAGE", "UPDATE_PAGE", "DELETE_PAGE"].includes(a))
302
- return;
303
- const s = g(t, "page");
304
- if (!s)
305
- return;
306
- const e = h(s.pageType);
307
- 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)
308
369
  switch (a) {
309
370
  case "CREATE_PAGE":
310
- return (n = e.onCreate) == null ? void 0 : n.call(e, s);
371
+ return (r = n.onCreate) == null ? void 0 : r.call(n, e);
311
372
  case "UPDATE_PAGE":
312
- return (r = e.onUpdate) == null ? void 0 : r.call(e, s);
373
+ return (o = n.onUpdate) == null ? void 0 : o.call(n, e);
313
374
  case "DELETE_PAGE":
314
- return (o = e.onDelete) == null ? void 0 : o.call(e, s);
375
+ return (h = n.onDelete) == null ? void 0 : h.call(n, e);
315
376
  }
316
377
  }
317
378
  // APIS
318
379
  setLanguageFromSlug(a) {
319
- const t = k(a || [""]);
320
- Object.keys(C).includes(t) ? this.setCurrentLang(t) : this.setCurrentLang("");
321
- }
322
- async getPageTypeFromSlug(a) {
323
- return a === "/" || a === `/${this.getCurrentLang()}`, "page";
380
+ const t = f(a || [""]);
381
+ Object.keys(w).includes(t) ? this.setCurrentLang(t) : this.setCurrentLang("");
324
382
  }
325
383
  async resolveLink(a, t) {
326
- const s = h(a);
327
- if (!s)
384
+ const e = l(a);
385
+ if (!e)
328
386
  return "/not-found";
329
- const e = this.isDraftMode(), n = this.getCurrentLang();
330
- if (s.resolveLink)
331
- return await s.resolveLink(t, e, n);
332
- const o = await (await fetch(`${this.apiUrl}/v1/api/chai`, {
333
- method: "POST",
334
- body: JSON.stringify({
335
- action: "GET_LINK",
336
- data: {
337
- pageType: a,
338
- id: t,
339
- draft: e,
340
- ...n ? { lang: n } : {}
341
- }
342
- }),
343
- headers: { "x-chai-api-key": this.apiKey }
344
- })).json();
345
- 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
+ });
346
397
  }
347
398
  async getPageBySlug(a) {
348
- const t = this.isDraftMode(), s = {};
349
- return d().forEach((n) => {
350
- y(n, "dynamicSegments") && n.dynamicSegments && (s[n.key] = n.dynamicSegments);
351
- }), (await fetch(`${this.apiUrl}/v1/api/chai`, {
352
- method: "POST",
353
- body: JSON.stringify({
354
- action: "GET_PAGE_META",
355
- data: { slug: a, draft: t, dynamicSegments: s }
356
- }),
357
- headers: { "x-chai-api-key": this.apiKey }
358
- })).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
+ });
359
406
  }
360
407
  async getFullPage(a) {
361
- const t = this.isDraftMode(), s = this.getCurrentLang();
362
- return (await fetch(`${this.apiUrl}/v1/api/chai`, {
363
- method: "POST",
364
- body: JSON.stringify({
365
- action: "GET_PAGE",
366
- data: { id: a, lang: s, draft: t, mergePartials: !0 }
367
- }),
368
- headers: { "x-chai-api-key": this.apiKey }
369
- })).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
+ });
370
413
  }
371
414
  async getSiteSettings() {
372
415
  const a = this.isDraftMode();
373
- return (await fetch(`${this.apiUrl}/v1/api/chai`, {
374
- method: "POST",
375
- body: JSON.stringify({
376
- action: "GET_WEBSITE_SETTINGS",
377
- data: { draft: a }
378
- }),
379
- headers: { "x-chai-api-key": this.apiKey }
380
- })).json();
416
+ return await this.backend.handleAction({
417
+ action: "GET_WEBSITE_SETTINGS",
418
+ data: { draft: a }
419
+ });
381
420
  }
382
421
  async getPageData(a, t) {
383
422
  var u;
384
- 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([
385
424
  this.getGlobalData(),
386
- ((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({})
387
426
  ]);
388
- return n ? {
389
- ...o,
390
- global: r
391
- } : { global: r };
427
+ return r ? {
428
+ ...h,
429
+ global: o
430
+ } : { global: o };
392
431
  }
393
432
  async getGlobalData() {
394
433
  const a = this.getCurrentLang();
395
- return await m(a, this.isDraftMode());
434
+ return await d(a, this.isDraftMode());
396
435
  }
397
436
  async request(a) {
398
- return (await fetch(`${this.apiUrl}/v1/api/chai`, {
399
- method: "POST",
400
- body: JSON.stringify(a),
401
- headers: { "x-chai-api-key": this.apiKey }
402
- })).json();
437
+ return await this.backend.handleAction(a);
403
438
  }
404
439
  }
405
440
  export {
406
- M as ChaiBuilderPages,
407
- m as getChaiGlobalData,
408
- h as getChaiPageType,
409
- d as getChaiPageTypes,
410
- L as registerChaiGlobalDataProvider,
411
- b as registerChaiPageType,
412
- 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,
413
450
  G as registerChaiTaxonomy
414
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.3",
6
+ "version": "0.1.4",
7
7
  "type": "module",
8
8
  "files": [
9
9
  "dist"