@chaibuilder/pages 0.1.3 → 0.1.5

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
+ protected backend: ChaiBuilderPagesBackendInterface;
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
+ export declare class ChaiBuilderPagesBaseAuth implements ChaiBuilderPagesAuthInterface {
63
+ protected token: string;
64
+ protected userId: string;
65
+ constructor(token: string);
66
+ verifyToken(): Promise<boolean>;
67
+ isUserActive(): Promise<boolean>;
68
+ getUserPermissions(): Promise<string[]>;
69
+ getUserId(): string;
70
+ }
71
+
72
+ declare interface ChaiBuilderPagesInterface {
73
+ setFallbackLang(lang: string): void;
74
+ getFallbackLang(): string;
75
+ setCurrentLang(lang: string): void;
76
+ getCurrentLang(): string;
77
+ setDraftMode(draft: boolean): void;
78
+ isDraftMode(): boolean;
79
+ setAuth(auth: ChaiBuilderPagesAuthInterface): void;
80
+ verifyToken(): Promise<boolean>;
81
+ getUserInfo(userId: string): Promise<ChaiBuilderUserInfo>;
82
+ handle(body: any): Promise<any>;
83
+ emit(action: string, data: any): void;
84
+ setLanguageFromSlug(slug: string[]): void;
85
+ resolveLink(pageTypeKey: string, id: string): Promise<string>;
86
+ getPageBySlug(slug: string): Promise<any>;
87
+ getFullPage(id: string): Promise<any>;
88
+ getSiteSettings(): Promise<any>;
89
+ getPageData(pageType: string, props: any): Promise<any>;
90
+ getGlobalData(): Promise<any>;
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 _ = (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) => _(i, typeof a != "symbol" ? a + "" : a, t);
4
+ import { get as b, 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,109 @@ 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 y = (i) => m = i, d = async (i, a, t) => await m(i, a, t), T = (i, a) => {
226
204
  ({ ...a });
227
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
+ }
228
257
  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", {
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 G {
274
+ constructor(a) {
275
+ s(this, "fallbackLang", "");
276
+ s(this, "currentLang", "");
277
+ s(this, "draftMode", !1);
278
+ s(this, "auth", new p(
279
+ "DUMMY_TOKEN"
280
+ ));
281
+ this.backend = a, this._registerPageTypes();
282
+ }
283
+ _registerPageTypes() {
284
+ A("page", {
234
285
  name: "Static Page",
235
286
  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", {
287
+ }), E("global", {
237
288
  name: "Global Block",
238
289
  helpText: "A global block can be reused in multiple pages.",
239
290
  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
291
  });
241
292
  }
293
+ setAuth(a) {
294
+ this.auth = a;
295
+ }
242
296
  setFallbackLang(a) {
243
297
  this.fallbackLang = a;
244
298
  }
@@ -257,9 +311,28 @@ class M {
257
311
  isDraftMode() {
258
312
  return this.draftMode;
259
313
  }
260
- async handle(a, t) {
314
+ async verifyToken() {
315
+ return await this.auth.verifyToken();
316
+ }
317
+ async getUserInfo(a) {
318
+ return {
319
+ id: a,
320
+ email: `${a}@chaibuilder.com`,
321
+ name: "Mock Chai User",
322
+ avatar: "https://i.pravatar.cc/300"
323
+ };
324
+ }
325
+ async handle(a) {
326
+ switch (a.action) {
327
+ case "CHECK_USER_STATUS":
328
+ return this.auth.isUserActive(a.data.authToken);
329
+ case "GET_PERMISSIONS":
330
+ return this.auth.getUserPermissions(a.data.authToken);
331
+ case "GET_CHAI_USER":
332
+ return this.getUserInfo(a.data.userId);
333
+ }
261
334
  if (a.action === "GET_BUILDER_PAGE_DATA") {
262
- const e = await m(a.data.lang, !0, !0), n = h(
335
+ const e = await d(a.data.lang, !0, !0), n = l(
263
336
  a.data.pageType
264
337
  );
265
338
  return n ? {
@@ -267,9 +340,9 @@ class M {
267
340
  global: e
268
341
  } : { global: e };
269
342
  }
270
- if (a.action === "GET_PAGE_TYPES" || a.action === "GET_COLLECTIONS")
343
+ if (a.action === "GET_PAGE_TYPES")
271
344
  return Promise.all(
272
- d().map(async (e) => ({
345
+ g().map(async (e) => ({
273
346
  key: e.key,
274
347
  helpText: e.helpText ?? "",
275
348
  icon: e.icon ?? "",
@@ -279,136 +352,102 @@ class M {
279
352
  name: typeof e.name == "function" ? await e.name() : e.name
280
353
  }))
281
354
  );
282
- if (a.action === "SEARCH_COLLECTION_ITEMS" || a.action === "SEARCH_PAGE_TYPE_ITEMS") {
283
- const e = h(
355
+ if (a.action === "SEARCH_PAGE_TYPE_ITEMS") {
356
+ const e = l(
284
357
  a.data.pageType
285
358
  );
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" };
359
+ return e ? e.search ? await e.search(a.data.query) : this.backend.handleAction(a) : { error: "Page type not found" };
295
360
  }
296
- const s = await E(a, this.apiKey, t, this.apiUrl);
297
- return this.emit(a.action, s), s;
361
+ const t = await this.backend.handleAction(a, this.auth.getUserId());
362
+ return this.emit(a.action, t), t;
298
363
  }
299
364
  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)
365
+ var r, o, h;
366
+ if (!["CREATE_PAGE", "UPDATE_PAGE", "DELETE_PAGE"].includes(a)) return;
367
+ const e = b(t, "page");
368
+ if (!e) return;
369
+ const n = l(e.pageType);
370
+ if (n)
308
371
  switch (a) {
309
372
  case "CREATE_PAGE":
310
- return (n = e.onCreate) == null ? void 0 : n.call(e, s);
373
+ return (r = n.onCreate) == null ? void 0 : r.call(n, e);
311
374
  case "UPDATE_PAGE":
312
- return (r = e.onUpdate) == null ? void 0 : r.call(e, s);
375
+ return (o = n.onUpdate) == null ? void 0 : o.call(n, e);
313
376
  case "DELETE_PAGE":
314
- return (o = e.onDelete) == null ? void 0 : o.call(e, s);
377
+ return (h = n.onDelete) == null ? void 0 : h.call(n, e);
315
378
  }
316
379
  }
317
380
  // APIS
318
381
  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";
382
+ const t = f(a || [""]);
383
+ Object.keys(w).includes(t) ? this.setCurrentLang(t) : this.setCurrentLang("");
324
384
  }
325
385
  async resolveLink(a, t) {
326
- const s = h(a);
327
- if (!s)
386
+ const e = l(a);
387
+ if (!e)
328
388
  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");
389
+ const n = this.isDraftMode(), r = this.getCurrentLang();
390
+ return e.resolveLink ? await e.resolveLink(t, n, r) : await this.backend.handleAction({
391
+ action: "GET_LINK",
392
+ data: {
393
+ pageType: a,
394
+ id: t,
395
+ draft: n,
396
+ lang: r
397
+ }
398
+ });
346
399
  }
347
400
  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();
401
+ const t = this.isDraftMode(), e = {};
402
+ return g().forEach((n) => {
403
+ S(n, "dynamicSegments") && n.dynamicSegments && (e[n.key] = n.dynamicSegments);
404
+ }), await this.backend.handleAction({
405
+ action: "GET_PAGE_META",
406
+ data: { slug: a, draft: t, dynamicSegments: e }
407
+ });
359
408
  }
360
409
  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();
410
+ const t = this.isDraftMode(), e = this.getCurrentLang();
411
+ return await this.backend.handleAction({
412
+ action: "GET_PAGE",
413
+ data: { id: a, lang: e, draft: t, mergePartials: !0 }
414
+ });
370
415
  }
371
416
  async getSiteSettings() {
372
417
  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();
418
+ return await this.backend.handleAction({
419
+ action: "GET_WEBSITE_SETTINGS",
420
+ data: { draft: a }
421
+ });
381
422
  }
382
423
  async getPageData(a, t) {
383
424
  var u;
384
- const s = this.isDraftMode(), e = this.getCurrentLang(), n = h(a), [r, o] = await Promise.all([
425
+ const e = this.isDraftMode(), n = this.getCurrentLang(), r = l(a), [o, h] = await Promise.all([
385
426
  this.getGlobalData(),
386
- ((u = n == null ? void 0 : n.dataProvider) == null ? void 0 : u.call(n, t, e, s)) || Promise.resolve({})
427
+ ((u = r == null ? void 0 : r.dataProvider) == null ? void 0 : u.call(r, t, n, e)) || Promise.resolve({})
387
428
  ]);
388
- return n ? {
389
- ...o,
390
- global: r
391
- } : { global: r };
429
+ return r ? {
430
+ ...h,
431
+ global: o
432
+ } : { global: o };
392
433
  }
393
434
  async getGlobalData() {
394
435
  const a = this.getCurrentLang();
395
- return await m(a, this.isDraftMode());
436
+ return await d(a, this.isDraftMode());
396
437
  }
397
438
  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();
439
+ return await this.backend.handleAction(a);
403
440
  }
404
441
  }
405
442
  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,
413
- G as registerChaiTaxonomy
443
+ G as ChaiBuilderPages,
444
+ M as ChaiBuilderPagesBackend,
445
+ p as ChaiBuilderPagesBaseAuth,
446
+ d as getChaiGlobalData,
447
+ l as getChaiPageType,
448
+ g as getChaiPageTypes,
449
+ y as registerChaiGlobalDataProvider,
450
+ A as registerChaiPageType,
451
+ E as registerChaiPartialType,
452
+ T as registerChaiTaxonomy
414
453
  };
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.5",
7
7
  "type": "module",
8
8
  "files": [
9
9
  "dist"