|
1 | 1 | import { applyChaiDataBinding } from "@chaibuilder/sdk/render"; |
2 | | -import { ChaiBlock, ChaiPageProps } from "@chaibuilder/sdk/runtime"; |
| 2 | +import type { ChaiBlock, ChaiPage, ChaiPageProps, ChaiWebsiteSetting } from "@chaibuilder/sdk/types"; |
3 | 3 | import { unstable_cache } from "next/cache"; |
| 4 | +import { notFound } from "next/navigation"; |
4 | 5 | import { cache } from "react"; |
5 | | -import type { ChaiBuilderPage } from "./lib"; |
6 | 6 | import * as utils from "./lib"; |
| 7 | +import { ChaiFullPage, ChaiPartialPage } from "./types"; |
7 | 8 |
|
8 | 9 | class ChaiBuilder { |
9 | 10 | private static appId?: string; |
@@ -51,53 +52,71 @@ class ChaiBuilder { |
51 | 52 | return ChaiBuilder.fallbackLang; |
52 | 53 | } |
53 | 54 |
|
54 | | - static async getSiteSettings(): Promise<{ fallbackLang?: string; [key: string]: unknown }> { |
| 55 | + static async getSiteSettings(): Promise<ChaiWebsiteSetting> { |
55 | 56 | ChaiBuilder.verifyInit(); |
56 | 57 | return await unstable_cache( |
57 | 58 | async () => await utils.getSiteSettings(ChaiBuilder.appId!, ChaiBuilder.draftMode), |
58 | 59 | [`website-settings-${ChaiBuilder.getAppId()}`], |
59 | | - { |
60 | | - tags: [`website-settings`, `website-settings-${ChaiBuilder.getAppId()}`], |
61 | | - }, |
| 60 | + { tags: [`website-settings`, `website-settings-${ChaiBuilder.getAppId()}`] }, |
62 | 61 | )(); |
63 | 62 | } |
64 | 63 |
|
65 | | - static async getPageBySlug(slug: string): Promise<ChaiBuilderPage | { error: string }> { |
| 64 | + static async getPageBySlug(slug: string): Promise<ChaiPartialPage> { |
66 | 65 | ChaiBuilder.verifyInit(); |
67 | | - return await utils.getPageBySlug(slug, this.appId!, this.draftMode); |
| 66 | + try { |
| 67 | + return await utils.getPageBySlug(slug, this.appId!, this.draftMode); |
| 68 | + } catch (error) { |
| 69 | + if (error instanceof Error && error.message === "PAGE_NOT_FOUND") { |
| 70 | + throw notFound(); |
| 71 | + } |
| 72 | + throw error; |
| 73 | + } |
68 | 74 | } |
69 | 75 |
|
70 | | - static async getFullPage(pageId: string): Promise<Record<string, unknown>> { |
| 76 | + static async getFullPage(pageId: string): Promise<ChaiFullPage> { |
71 | 77 | ChaiBuilder.verifyInit(); |
72 | 78 | return await utils.getFullPage(pageId, this.appId!, this.draftMode); |
73 | 79 | } |
74 | 80 |
|
75 | | - static async getPartialPageBySlug(slug: string): Promise<Record<string, unknown>> { |
| 81 | + static async getPartialPageBySlug( |
| 82 | + slug: string, |
| 83 | + ): Promise< |
| 84 | + Pick< |
| 85 | + ChaiPage, |
| 86 | + | "id" |
| 87 | + | "name" |
| 88 | + | "slug" |
| 89 | + | "lang" |
| 90 | + | "primaryPage" |
| 91 | + | "seo" |
| 92 | + | "currentEditor" |
| 93 | + | "pageType" |
| 94 | + | "lastSaved" |
| 95 | + | "dynamic" |
| 96 | + | "parent" |
| 97 | + | "blocks" |
| 98 | + > |
| 99 | + > { |
76 | 100 | ChaiBuilder.verifyInit(); |
77 | 101 | // if the slug is of format /_partial/{langcode}/{uuid}, get the uuid. langcode is optional |
78 | 102 | const uuid = slug.split("/").pop(); |
79 | 103 | if (!uuid) { |
80 | | - throw new Error("Invalid slug format for partial page"); |
| 104 | + throw new Error("PAGE_NOT_FOUND"); |
81 | 105 | } |
82 | 106 |
|
83 | 107 | // Fetch the partial page data |
84 | 108 | const siteSettings = await ChaiBuilder.getSiteSettings(); |
85 | 109 | const data = await ChaiBuilder.getFullPage(uuid); |
86 | | - const fallbackLang = siteSettings?.fallbackLang; |
87 | | - const lang = slug.split("/").length > 3 ? slug.split("/")[2] : fallbackLang; |
88 | | - return { ...data, fallbackLang, lang }; |
| 110 | + const lang = slug.split("/").length > 3 ? slug.split("/")[2] : siteSettings?.fallbackLang; |
| 111 | + return { ...data, lang }; |
89 | 112 | } |
90 | 113 |
|
91 | | - static getPage = cache(async (slug: string): Promise<ChaiBuilderPage | Record<string, unknown>> => { |
| 114 | + static getPage = cache(async (slug: string): Promise<ChaiFullPage> => { |
92 | 115 | ChaiBuilder.verifyInit(); |
93 | 116 | if (slug.startsWith("/_partial/")) { |
94 | 117 | return await ChaiBuilder.getPartialPageBySlug(slug); |
95 | 118 | } |
96 | | - const page: ChaiBuilderPage = await ChaiBuilder.getPageBySlug(slug); |
97 | | - if ("error" in page) { |
98 | | - return page; |
99 | | - } |
100 | | - |
| 119 | + const page = await ChaiBuilder.getPageBySlug(slug); |
101 | 120 | const siteSettings = await ChaiBuilder.getSiteSettings(); |
102 | 121 | const tagPageId = page.id; |
103 | 122 | const languagePageId = page.languagePageId || page.id; |
@@ -129,16 +148,12 @@ class ChaiBuilder { |
129 | 148 | }; |
130 | 149 | } |
131 | 150 | const page = await ChaiBuilder.getPage(slug); |
132 | | - if ("error" in page) { |
133 | | - return { |
134 | | - title: "Page Not Found", |
135 | | - description: "The requested page could not be found.", |
136 | | - robots: { index: false, follow: false }, |
137 | | - }; |
| 151 | + if ("error" in page && page.error === "PAGE_NOT_FOUND") { |
| 152 | + throw notFound(); |
138 | 153 | } |
139 | 154 |
|
140 | 155 | // Type assertion after error check |
141 | | - const pageData = page as Exclude<ChaiBuilderPage, { error: string }>; |
| 156 | + const pageData = page as ChaiPage; |
142 | 157 |
|
143 | 158 | const externalData = await ChaiBuilder.getPageExternalData({ |
144 | 159 | blocks: pageData.blocks, |
@@ -194,7 +209,7 @@ class ChaiBuilder { |
194 | 209 |
|
195 | 210 | static async getPageData(args: { |
196 | 211 | blocks: ChaiBlock[]; |
197 | | - pageProps: Record<string, unknown>; |
| 212 | + pageProps: ChaiPageProps; |
198 | 213 | pageType: string; |
199 | 214 | lang: string; |
200 | 215 | }): Promise<Record<string, unknown>> { |
|
0 commit comments