@brightsideoy/kama 1.0.0-alpha.31 → 1.0.0-alpha.32
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/assets/{index-_Tc6Vb8T.js → index-DTHhZNC5.js} +1 -1
- package/dist/{dist-DKRU6nhX.js → dist-CpYaG9eL.js} +4772 -4783
- package/dist/index.html +1 -1
- package/dist/index.js +4342 -4333
- package/dist/schema-compiler.js +1 -1
- package/documentation/FORMS.md +13 -9
- package/package.json +1 -1
- package/templates/astro-starter/src/components/blocks/Form.astro +2 -6
- package/templates/astro-starter/src/components/global/Header.astro +5 -7
- package/templates/astro-starter/src/components/global/MetaHead.astro +3 -2
- package/templates/astro-starter/src/lib/cms-fetch.test.ts +111 -0
- package/templates/astro-starter/src/lib/cms-fetch.ts +12 -2
- package/templates/astro-starter/src/lib/cms.ts +13 -12
- package/templates/astro-starter/src/lib/media/media-proxy.ts +2 -1
- package/templates/astro-starter/src/middleware.ts +6 -13
- package/templates/astro-starter/src/pages/[...slug].astro +4 -6
- package/templates/astro-starter/src/pages/api/forms/[...path].ts +2 -6
- package/templates/astro-starter/src/pages/api/load-nodes.ts +2 -1
- package/templates/astro-starter/src/pages/api/partials/node-cards.astro +2 -4
- package/templates/astro-starter/src/pages/llms-full.txt.ts +7 -7
- package/templates/astro-starter/src/pages/llms.txt.ts +5 -6
- package/templates/astro-starter/src/pages/partials/footer.astro +2 -4
- package/templates/astro-starter/src/pages/partials/nav.astro +3 -5
- package/dist/nodemailer-Ccic7tRm-Dw2jwzbm.js +0 -7877
- package/dist/nodemailer-Ccic7tRm.js +0 -7908
- package/dist/rolldown-runtime-B0aSnxlc.js +0 -20
package/dist/schema-compiler.js
CHANGED
|
@@ -30,7 +30,7 @@ async function f() {
|
|
|
30
30
|
if (n(f)) l = (await import(t(f).href)).compileDefinitions;
|
|
31
31
|
else if (n(p)) l = (await import(t(p).href)).compileDefinitions;
|
|
32
32
|
else try {
|
|
33
|
-
l = (await import("./dist-
|
|
33
|
+
l = (await import("./dist-CpYaG9eL.js")).compileDefinitions;
|
|
34
34
|
} catch (e) {
|
|
35
35
|
console.error("\x1B[31m ✖\x1B[0m \x1B[31mCould not resolve Kama Schema Engine:\x1B[0m", e), process.exit(1);
|
|
36
36
|
}
|
package/documentation/FORMS.md
CHANGED
|
@@ -231,15 +231,19 @@ production form; configure real keys for deployed environments.
|
|
|
231
231
|
|
|
232
232
|
When available, the Astro starter uses the `CMS_WORKER` service binding for CMS
|
|
233
233
|
API and `/media/*` requests. Form submissions go through the same-origin
|
|
234
|
-
`/api/forms/:formId/submissions` proxy.
|
|
235
|
-
`
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
234
|
+
`/api/forms/:formId/submissions` proxy. Configure `PUBLIC_API_URL` (or
|
|
235
|
+
`PUBLIC_CMS_URL`) on the Astro site, even when using a service binding.
|
|
236
|
+
There is no implicit CMS URL.
|
|
237
|
+
|
|
238
|
+
Set the URL to the intended CMS instance in both development and deployment
|
|
239
|
+
(for example, `http://127.0.0.1:8686` for a Node server on that port). If a
|
|
240
|
+
local service binding reports its target Worker is not running, form, page and
|
|
241
|
+
middleware media requests retry only at the configured loopback URL. Keep the
|
|
242
|
+
site's and CMS Worker's `FRONTEND_API_SECRET` values matched in their local
|
|
243
|
+
secret files. Never expose a server-only API secret in browser code. Astro can
|
|
244
|
+
embed public environment variables at build time, so rebuild after changing
|
|
245
|
+
them. Use a production build on a VPS: Astro development mode enables
|
|
246
|
+
Turnstile test keys.
|
|
243
247
|
|
|
244
248
|
### Draft preview iframe
|
|
245
249
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
// src/components/blocks/Form.astro
|
|
3
|
-
import { fetchCmsRequest } from '../../lib/cms-fetch';
|
|
3
|
+
import { fetchCmsRequest, getCmsApiUrl } from '../../lib/cms-fetch';
|
|
4
4
|
|
|
5
5
|
interface Props {
|
|
6
6
|
data: {
|
|
@@ -16,11 +16,6 @@ const formId = data?.formId;
|
|
|
16
16
|
|
|
17
17
|
const safeEnv = (Astro.locals as any).safeEnv || {};
|
|
18
18
|
const cmsWorker = safeEnv.CMS_WORKER;
|
|
19
|
-
const kamaApiUrl = String(
|
|
20
|
-
import.meta.env.DEV && !cmsWorker
|
|
21
|
-
? 'http://localhost:8787'
|
|
22
|
-
: import.meta.env.PUBLIC_API_URL || safeEnv.PUBLIC_API_URL || 'http://localhost:8787'
|
|
23
|
-
).replace(/\/$/, '');
|
|
24
19
|
const configuredTurnstileSiteKey = String(
|
|
25
20
|
import.meta.env.PUBLIC_TURNSTILE_SITE_KEY || safeEnv.PUBLIC_TURNSTILE_SITE_KEY || ''
|
|
26
21
|
).trim();
|
|
@@ -32,6 +27,7 @@ const turnstileSiteKey = configuredTurnstileSiteKey || (
|
|
|
32
27
|
let schema = null;
|
|
33
28
|
if (formId) {
|
|
34
29
|
try {
|
|
30
|
+
const kamaApiUrl = getCmsApiUrl(safeEnv);
|
|
35
31
|
const schemaUrl = `${kamaApiUrl}/api/v1/forms/${encodeURIComponent(formId)}`;
|
|
36
32
|
const schemaRequest = new Request(schemaUrl, { headers: { Accept: 'application/json' } });
|
|
37
33
|
const res = await fetchCmsRequest(schemaRequest, cmsWorker);
|
|
@@ -5,6 +5,7 @@ import NavigationMenu from "@/components/global/NavigationMenu.astro";
|
|
|
5
5
|
import KamaImage from "@/components/ui/KamaImage.astro";
|
|
6
6
|
import { getTree, getTreeId, getChannel, getChannelId } from "@/lib/cms";
|
|
7
7
|
import type { TreeNode } from "@/lib/cms";
|
|
8
|
+
import { getCmsApiUrl } from "@/lib/cms-fetch";
|
|
8
9
|
|
|
9
10
|
interface Props {
|
|
10
11
|
node?: Record<string, any>;
|
|
@@ -25,10 +26,7 @@ const customFetch = (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
25
26
|
let urlStr = input instanceof URL ? input.toString() : String(input);
|
|
26
27
|
|
|
27
28
|
if (urlStr.startsWith("/")) {
|
|
28
|
-
const baseUrl =
|
|
29
|
-
import.meta.env.PUBLIC_API_URL ||
|
|
30
|
-
safeEnv.PUBLIC_API_URL ||
|
|
31
|
-
"http://127.0.0.1:8787";
|
|
29
|
+
const baseUrl = getCmsApiUrl(safeEnv);
|
|
32
30
|
urlStr = `${baseUrl}${urlStr}`;
|
|
33
31
|
}
|
|
34
32
|
|
|
@@ -49,13 +47,13 @@ let channelData: Record<string, any> | null = null;
|
|
|
49
47
|
try {
|
|
50
48
|
if (!rawTree || rawTree.length === 0) {
|
|
51
49
|
const [fetchedChannel, fetchedTree] = await Promise.all([
|
|
52
|
-
getChannel(channelId, customFetch),
|
|
53
|
-
getTree(treeId, channelId, customFetch),
|
|
50
|
+
getChannel(channelId, customFetch, safeEnv),
|
|
51
|
+
getTree(treeId, channelId, customFetch, safeEnv),
|
|
54
52
|
]);
|
|
55
53
|
channelData = fetchedChannel;
|
|
56
54
|
rawTree = fetchedTree || [];
|
|
57
55
|
} else {
|
|
58
|
-
channelData = await getChannel(channelId, customFetch);
|
|
56
|
+
channelData = await getChannel(channelId, customFetch, safeEnv);
|
|
59
57
|
}
|
|
60
58
|
} catch (e) {
|
|
61
59
|
console.error("[Header] Failed to load layout data:", e);
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import type { ContentBlock } from "@/lib/cms";
|
|
5
5
|
import { getChannel, getChannelId } from "@/lib/cms";
|
|
6
6
|
import { getImageUri } from "@/lib/media/media-url";
|
|
7
|
+
import { getCmsApiUrl } from "@/lib/cms-fetch";
|
|
7
8
|
|
|
8
9
|
interface Props {
|
|
9
10
|
node: Record<string, any>;
|
|
@@ -34,7 +35,7 @@ const customFetch = (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
34
35
|
let urlStr = input instanceof URL ? input.toString() : String(input);
|
|
35
36
|
|
|
36
37
|
if (urlStr.startsWith("/")) {
|
|
37
|
-
const baseUrl =
|
|
38
|
+
const baseUrl = getCmsApiUrl(safeEnv);
|
|
38
39
|
urlStr = `${baseUrl}${urlStr}`;
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -47,7 +48,7 @@ const customFetch = (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
47
48
|
};
|
|
48
49
|
|
|
49
50
|
// 🎯 Fetch Global Channel Metadata
|
|
50
|
-
const channel = await getChannel(getChannelId(), customFetch);
|
|
51
|
+
const channel = await getChannel(getChannelId(), customFetch, safeEnv);
|
|
51
52
|
|
|
52
53
|
// 🎯 Language Cascade Final Resolution:
|
|
53
54
|
// Prop / Node Language -> Channel Default -> Undefined
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import { createServer } from 'vite';
|
|
4
|
+
|
|
5
|
+
async function createTestServer(apiUrl: string, cmsUrl = '') {
|
|
6
|
+
return createServer({
|
|
7
|
+
configFile: false,
|
|
8
|
+
define: {
|
|
9
|
+
'import.meta.env.PUBLIC_API_URL': JSON.stringify(apiUrl),
|
|
10
|
+
'import.meta.env.PUBLIC_CMS_URL': JSON.stringify(cmsUrl),
|
|
11
|
+
},
|
|
12
|
+
server: { middlewareMode: true, hmr: false, watch: { ignored: ['**'] } },
|
|
13
|
+
appType: 'custom',
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
test('form requests use the configured CMS in development', async () => {
|
|
18
|
+
const server = await createTestServer('http://127.0.0.1:8686/');
|
|
19
|
+
const originalFetch = globalThis.fetch;
|
|
20
|
+
try {
|
|
21
|
+
const { getCmsApiUrl } = await server.ssrLoadModule('/templates/astro-starter/src/lib/cms-fetch.ts');
|
|
22
|
+
const { POST } = await server.ssrLoadModule('/templates/astro-starter/src/pages/api/forms/[...path].ts');
|
|
23
|
+
assert.equal(getCmsApiUrl(), 'http://127.0.0.1:8686');
|
|
24
|
+
|
|
25
|
+
let forwarded: Request | undefined;
|
|
26
|
+
globalThis.fetch = async (input) => {
|
|
27
|
+
assert.ok(input instanceof Request);
|
|
28
|
+
forwarded = input;
|
|
29
|
+
return Response.json({ success: true });
|
|
30
|
+
};
|
|
31
|
+
const response = await POST({
|
|
32
|
+
params: { path: 'contact/submissions' },
|
|
33
|
+
request: new Request('http://localhost:4322/api/forms/contact/submissions', {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: { 'content-type': 'application/json' },
|
|
36
|
+
body: JSON.stringify({ email: 'test@example.com' }),
|
|
37
|
+
}),
|
|
38
|
+
locals: {},
|
|
39
|
+
});
|
|
40
|
+
assert.equal(response.status, 200);
|
|
41
|
+
assert.equal(forwarded?.url, 'http://127.0.0.1:8686/api/v1/forms/contact/submissions');
|
|
42
|
+
assert.deepEqual(await forwarded?.json(), { email: 'test@example.com' });
|
|
43
|
+
} finally {
|
|
44
|
+
globalThis.fetch = originalFetch;
|
|
45
|
+
await server.close();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('form requests require a configured CMS URL', async () => {
|
|
50
|
+
const server = await createTestServer('');
|
|
51
|
+
const originalApiUrl = process.env.PUBLIC_API_URL;
|
|
52
|
+
const originalCmsUrl = process.env.PUBLIC_CMS_URL;
|
|
53
|
+
const originalConsoleError = console.error;
|
|
54
|
+
delete process.env.PUBLIC_API_URL;
|
|
55
|
+
delete process.env.PUBLIC_CMS_URL;
|
|
56
|
+
try {
|
|
57
|
+
const { getCmsApiUrl } = await server.ssrLoadModule('/templates/astro-starter/src/lib/cms-fetch.ts');
|
|
58
|
+
const { getTree } = await server.ssrLoadModule('/templates/astro-starter/src/lib/cms.ts');
|
|
59
|
+
const { POST } = await server.ssrLoadModule('/templates/astro-starter/src/pages/api/forms/[...path].ts');
|
|
60
|
+
assert.equal(getCmsApiUrl({ PUBLIC_API_URL: 'http://127.0.0.1:8686/' }), 'http://127.0.0.1:8686');
|
|
61
|
+
assert.equal(getCmsApiUrl({ PUBLIC_CMS_URL: 'http://127.0.0.1:8686/' }), 'http://127.0.0.1:8686');
|
|
62
|
+
assert.throws(() => getCmsApiUrl(), /Set PUBLIC_API_URL or PUBLIC_CMS_URL/);
|
|
63
|
+
let treeUrl = '';
|
|
64
|
+
await getTree('main-tree', 'site', async (input: RequestInfo | URL) => {
|
|
65
|
+
treeUrl = String(input);
|
|
66
|
+
return Response.json({ items: [] });
|
|
67
|
+
}, { PUBLIC_API_URL: 'http://127.0.0.1:8686' });
|
|
68
|
+
assert.equal(treeUrl, 'http://127.0.0.1:8686/api/v1/tree/main-tree?channelId=site');
|
|
69
|
+
|
|
70
|
+
let loggedError: unknown;
|
|
71
|
+
console.error = (_message, error) => { loggedError = error; };
|
|
72
|
+
const response = await POST({
|
|
73
|
+
params: { path: 'contact/submissions' },
|
|
74
|
+
request: new Request('http://localhost:4322/api/forms/contact/submissions', { method: 'POST' }),
|
|
75
|
+
locals: {},
|
|
76
|
+
});
|
|
77
|
+
assert.equal(response.status, 502);
|
|
78
|
+
assert.match(String(loggedError), /Set PUBLIC_API_URL or PUBLIC_CMS_URL/);
|
|
79
|
+
} finally {
|
|
80
|
+
console.error = originalConsoleError;
|
|
81
|
+
if (originalApiUrl !== undefined) process.env.PUBLIC_API_URL = originalApiUrl;
|
|
82
|
+
if (originalCmsUrl !== undefined) process.env.PUBLIC_CMS_URL = originalCmsUrl;
|
|
83
|
+
await server.close();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('a missing local service binding retries only the configured loopback URL', async () => {
|
|
88
|
+
const server = await createTestServer('');
|
|
89
|
+
const originalFetch = globalThis.fetch;
|
|
90
|
+
try {
|
|
91
|
+
const { fetchCmsRequest } = await server.ssrLoadModule('/templates/astro-starter/src/lib/cms-fetch.ts');
|
|
92
|
+
const binding = {
|
|
93
|
+
fetch: async () => new Response('Worker "cms" not found. Make sure it is running locally.', { status: 503 }),
|
|
94
|
+
};
|
|
95
|
+
const calls: string[] = [];
|
|
96
|
+
globalThis.fetch = async (input) => {
|
|
97
|
+
assert.ok(input instanceof Request);
|
|
98
|
+
calls.push(input.url);
|
|
99
|
+
return new Response('ok');
|
|
100
|
+
};
|
|
101
|
+
const local = await fetchCmsRequest(new Request('http://127.0.0.1:8686/api/v1/forms/contact'), binding);
|
|
102
|
+
assert.equal(local.status, 200);
|
|
103
|
+
assert.deepEqual(calls, ['http://127.0.0.1:8686/api/v1/forms/contact']);
|
|
104
|
+
const remote = await fetchCmsRequest(new Request('https://cms.example.com/api/v1/forms/contact'), binding);
|
|
105
|
+
assert.equal(remote.status, 503);
|
|
106
|
+
assert.equal(calls.length, 1);
|
|
107
|
+
} finally {
|
|
108
|
+
globalThis.fetch = originalFetch;
|
|
109
|
+
await server.close();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
export function getCmsApiUrl(env: { PUBLIC_API_URL?: string; PUBLIC_CMS_URL?: string } = {}): string {
|
|
2
|
+
const url = (
|
|
3
|
+
import.meta.env.PUBLIC_API_URL || env.PUBLIC_API_URL ||
|
|
4
|
+
import.meta.env.PUBLIC_CMS_URL || env.PUBLIC_CMS_URL ||
|
|
5
|
+
(typeof process !== 'undefined' && (process.env.PUBLIC_API_URL || process.env.PUBLIC_CMS_URL))
|
|
6
|
+
);
|
|
7
|
+
if (!url || !url.trim()) throw new Error('Set PUBLIC_API_URL or PUBLIC_CMS_URL to the CMS URL.');
|
|
8
|
+
return url.trim().replace(/\/+$/, '');
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
export async function fetchCmsRequest(
|
|
2
12
|
request: Request,
|
|
3
13
|
cmsWorker: { fetch: (request: Request) => Promise<Response> } | undefined,
|
|
@@ -17,6 +27,6 @@ export async function fetchCmsRequest(
|
|
|
17
27
|
return response;
|
|
18
28
|
}
|
|
19
29
|
|
|
20
|
-
|
|
21
|
-
return fetch(
|
|
30
|
+
if (!['localhost', '127.0.0.1', '[::1]'].includes(requestUrl.hostname)) return response;
|
|
31
|
+
return fetch(fallbackRequest);
|
|
22
32
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// src/lib/cms.ts
|
|
2
2
|
|
|
3
|
+
import { getCmsApiUrl } from './cms-fetch';
|
|
4
|
+
|
|
3
5
|
export interface TreeNode {
|
|
4
6
|
id: number;
|
|
5
7
|
treeNodeId: number;
|
|
@@ -78,12 +80,8 @@ export interface ChannelResponse {
|
|
|
78
80
|
/**
|
|
79
81
|
* Resolves base CMS URL across runtime environments safely via Astro
|
|
80
82
|
*/
|
|
81
|
-
export function getCmsUrl(): string {
|
|
82
|
-
|
|
83
|
-
import.meta.env.PUBLIC_API_URL ||
|
|
84
|
-
import.meta.env.PUBLIC_CMS_URL ||
|
|
85
|
-
'http://localhost:8787';
|
|
86
|
-
return url.replace(/\/+$/, '');
|
|
83
|
+
export function getCmsUrl(env: { PUBLIC_API_URL?: string; PUBLIC_CMS_URL?: string } = {}): string {
|
|
84
|
+
return getCmsApiUrl(env);
|
|
87
85
|
}
|
|
88
86
|
|
|
89
87
|
/**
|
|
@@ -114,9 +112,10 @@ function getAuthHeaders(): Record<string, string> {
|
|
|
114
112
|
*/
|
|
115
113
|
export async function getChannel(
|
|
116
114
|
channelKeyOrId: string = getChannelId(),
|
|
117
|
-
customFetch: typeof fetch = fetch
|
|
115
|
+
customFetch: typeof fetch = fetch,
|
|
116
|
+
env: { PUBLIC_API_URL?: string; PUBLIC_CMS_URL?: string } = {}
|
|
118
117
|
): Promise<ChannelResponse | null> {
|
|
119
|
-
const baseUrl = getCmsUrl();
|
|
118
|
+
const baseUrl = getCmsUrl(env);
|
|
120
119
|
const requestUrl = `${baseUrl}/api/v1/channel/${channelKeyOrId}`;
|
|
121
120
|
|
|
122
121
|
try {
|
|
@@ -146,9 +145,10 @@ export async function getChannel(
|
|
|
146
145
|
export async function getTree(
|
|
147
146
|
treeKeyOrId: string = getTreeId(),
|
|
148
147
|
channel: string | number = getChannelId(),
|
|
149
|
-
customFetch: typeof fetch = fetch
|
|
148
|
+
customFetch: typeof fetch = fetch,
|
|
149
|
+
env: { PUBLIC_API_URL?: string; PUBLIC_CMS_URL?: string } = {}
|
|
150
150
|
): Promise<TreeNode[]> {
|
|
151
|
-
const baseUrl = getCmsUrl();
|
|
151
|
+
const baseUrl = getCmsUrl(env);
|
|
152
152
|
const requestUrl = `${baseUrl}/api/v1/tree/${treeKeyOrId}?channelId=${channel}`;
|
|
153
153
|
|
|
154
154
|
try {
|
|
@@ -180,12 +180,13 @@ export async function getNodeByPath(
|
|
|
180
180
|
path: string,
|
|
181
181
|
channel: string | number = getChannelId(),
|
|
182
182
|
preview = false,
|
|
183
|
-
customFetch: typeof fetch = fetch
|
|
183
|
+
customFetch: typeof fetch = fetch,
|
|
184
|
+
env: { PUBLIC_API_URL?: string; PUBLIC_CMS_URL?: string } = {}
|
|
184
185
|
): Promise<NodeResponse | null> {
|
|
185
186
|
// 🎯 We keep 'index' as the fallback string for Hono routing compatibility,
|
|
186
187
|
// because the backend dynamically intercepts 'index' and returns the channelRoot!
|
|
187
188
|
const cleanPath = path.replace(/^\/+|\/+$/g, '') || 'index';
|
|
188
|
-
const baseUrl = getCmsUrl();
|
|
189
|
+
const baseUrl = getCmsUrl(env);
|
|
189
190
|
const requestUrl = `${baseUrl}/api/v1/node/${cleanPath}?channelId=${channel}&preview=${preview}`;
|
|
190
191
|
|
|
191
192
|
try {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/lib/media/media-proxy.ts
|
|
2
2
|
|
|
3
3
|
import type { APIContext } from "astro";
|
|
4
|
+
import { getCmsApiUrl } from "../cms-fetch";
|
|
4
5
|
|
|
5
6
|
export function getMimeType(filePath: string): string {
|
|
6
7
|
const ext = filePath.split(".").pop()?.toLowerCase();
|
|
@@ -108,7 +109,7 @@ export async function handleMediaProxy({ request, params, url, locals }: APICont
|
|
|
108
109
|
}
|
|
109
110
|
// B. Local Dev Mode (Standard HTTP Fetch to local Hono Server)
|
|
110
111
|
else {
|
|
111
|
-
const cmsUrl =
|
|
112
|
+
const cmsUrl = getCmsApiUrl(workerEnv);
|
|
112
113
|
const targetUrl = `${cmsUrl}${url.pathname}${url.search}`;
|
|
113
114
|
|
|
114
115
|
// Forward request headers (like Accept for WebP/AVIF negotiation)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/middleware.ts
|
|
2
2
|
|
|
3
3
|
import { defineMiddleware } from "astro:middleware";
|
|
4
|
-
import { fetchCmsRequest } from "./lib/cms-fetch";
|
|
4
|
+
import { fetchCmsRequest, getCmsApiUrl } from "./lib/cms-fetch";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Safely extracts environment variables across Astro 7, Node.js, and Cloudflare Workers
|
|
@@ -20,7 +20,7 @@ async function getSafeEnv(locals: any): Promise<Record<string, any>> {
|
|
|
20
20
|
// 2. Safe check for locals.runtime / cfContext (Local Node.js / Hono)
|
|
21
21
|
const runtime = locals?.runtime || locals?.cfContext;
|
|
22
22
|
if (runtime && typeof runtime === "object") {
|
|
23
|
-
if ("CMS_WORKER" in runtime || "PUBLIC_API_URL" in runtime) {
|
|
23
|
+
if ("CMS_WORKER" in runtime || "PUBLIC_API_URL" in runtime || "PUBLIC_CMS_URL" in runtime) {
|
|
24
24
|
return runtime;
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -105,16 +105,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
|
|
|
105
105
|
|
|
106
106
|
// 2. Cold Fetch: Pass to CMS worker for AVIF transformation
|
|
107
107
|
const cmsWorker = workerEnv?.CMS_WORKER;
|
|
108
|
-
const
|
|
109
|
-
const workerApiUrl = workerEnv?.PUBLIC_API_URL || workerEnv?.PUBLIC_CMS_URL;
|
|
110
|
-
const localDevApiUrl = "http://127.0.0.1:8787";
|
|
111
|
-
const fallbackApiUrl = isDev ? localDevApiUrl : "https://kama.kamaengine.com";
|
|
112
|
-
const baseApiUrl = String(
|
|
113
|
-
isDev && !cmsWorker
|
|
114
|
-
? localDevApiUrl
|
|
115
|
-
: workerApiUrl || envApiUrl || fallbackApiUrl
|
|
116
|
-
);
|
|
117
|
-
const cleanApiUrl = baseApiUrl.endsWith("/") ? baseApiUrl.slice(0, -1) : baseApiUrl;
|
|
108
|
+
const cleanApiUrl = getCmsApiUrl(workerEnv);
|
|
118
109
|
|
|
119
110
|
const mediaUrl = `${cleanApiUrl}${pathname}${context.url.search}`;
|
|
120
111
|
const mediaReq = new Request(mediaUrl, { headers: context.request.headers });
|
|
@@ -537,7 +528,9 @@ export const onRequest = defineMiddleware(async (context, next) => {
|
|
|
537
528
|
response.headers.set("X-Content-Type-Options", "nosniff");
|
|
538
529
|
|
|
539
530
|
// 🎯 Extract origin from env dynamically
|
|
540
|
-
const cmsUrl = workerEnv?.PUBLIC_CMS_URL ||
|
|
531
|
+
const cmsUrl = workerEnv?.PUBLIC_CMS_URL || import.meta.env.PUBLIC_CMS_URL
|
|
532
|
+
|| (typeof process !== "undefined" && process.env.PUBLIC_CMS_URL)
|
|
533
|
+
|| getCmsApiUrl(workerEnv);
|
|
541
534
|
let cmsOrigin = "";
|
|
542
535
|
try {
|
|
543
536
|
cmsOrigin = new URL(cmsUrl).origin;
|
|
@@ -6,7 +6,7 @@ import SkeletonLayout from "../layouts/SkeletonLayout.astro";
|
|
|
6
6
|
import BlockRenderer from "../components/blocks/BlockRenderer.astro";
|
|
7
7
|
import Breadcrumb from "../components/global/Breadcrumb.astro";
|
|
8
8
|
import { getNodeByPath, getChannelId, getTree, getTreeId } from "../lib/cms";
|
|
9
|
-
import { fetchCmsRequest } from "../lib/cms-fetch";
|
|
9
|
+
import { fetchCmsRequest, getCmsApiUrl } from "../lib/cms-fetch";
|
|
10
10
|
import type { NodeResponse, TreeNode } from "../lib/cms";
|
|
11
11
|
|
|
12
12
|
export const prerender = false;
|
|
@@ -56,9 +56,7 @@ const envHomeSlug = import.meta.env.PUBLIC_HOME_SLUG;
|
|
|
56
56
|
const workerHomeSlug = safeEnv.PUBLIC_HOME_SLUG;
|
|
57
57
|
const homeSlug = String(envHomeSlug || workerHomeSlug || "index").trim();
|
|
58
58
|
|
|
59
|
-
const
|
|
60
|
-
const workerApiUrl = safeEnv.PUBLIC_API_URL;
|
|
61
|
-
const baseApiUrl = String(envApiUrl || workerApiUrl || "http://localhost:8787");
|
|
59
|
+
const baseApiUrl = getCmsApiUrl(safeEnv);
|
|
62
60
|
|
|
63
61
|
// 🎯 Safely extract the secret across both environments
|
|
64
62
|
const frontendApiSecret =
|
|
@@ -128,11 +126,11 @@ let navTree: TreeNode[] = [];
|
|
|
128
126
|
// Fetch the tree and node in parallel; only preview requests bypass API caches.
|
|
129
127
|
try {
|
|
130
128
|
const [treeResult, nodeResult] = await Promise.all([
|
|
131
|
-
getTree(treeId, channelId, customFetch).catch(err => {
|
|
129
|
+
getTree(treeId, channelId, customFetch, safeEnv).catch(err => {
|
|
132
130
|
console.error(`❌ [SSR Error] Failed to fetch tree "${treeId}":`, err);
|
|
133
131
|
return [];
|
|
134
132
|
}),
|
|
135
|
-
getNodeByPath(targetSlug, channelId, isPreview, customFetch).catch(err => {
|
|
133
|
+
getNodeByPath(targetSlug, channelId, isPreview, customFetch, safeEnv).catch(err => {
|
|
136
134
|
console.error(
|
|
137
135
|
`❌ [SSR Error] Failed to fetch page data for path "${targetSlug}":`,
|
|
138
136
|
err,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { APIRoute } from "astro";
|
|
2
|
-
import { fetchCmsRequest } from "../../../lib/cms-fetch";
|
|
2
|
+
import { fetchCmsRequest, getCmsApiUrl } from "../../../lib/cms-fetch";
|
|
3
3
|
|
|
4
4
|
export const prerender = false;
|
|
5
5
|
|
|
@@ -11,14 +11,10 @@ export const POST: APIRoute = async ({ params, request, locals }) => {
|
|
|
11
11
|
|
|
12
12
|
const safeEnv = (locals as any).safeEnv || {};
|
|
13
13
|
const cmsWorker = safeEnv.CMS_WORKER;
|
|
14
|
-
const apiUrl = String(
|
|
15
|
-
import.meta.env.DEV && !cmsWorker
|
|
16
|
-
? "http://localhost:8787"
|
|
17
|
-
: import.meta.env.PUBLIC_API_URL || safeEnv.PUBLIC_API_URL || "http://localhost:8787"
|
|
18
|
-
).replace(/\/$/, "");
|
|
19
14
|
const formId = encodeURIComponent(path[0]);
|
|
20
15
|
|
|
21
16
|
try {
|
|
17
|
+
const apiUrl = getCmsApiUrl(safeEnv);
|
|
22
18
|
const contentType = request.headers.get("content-type") || "";
|
|
23
19
|
const payload = contentType.includes("application/json")
|
|
24
20
|
? await request.text()
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/pages/api/load-nodes.ts
|
|
2
2
|
import type { APIRoute } from "astro";
|
|
3
|
+
import { getCmsApiUrl } from "../../lib/cms-fetch";
|
|
3
4
|
|
|
4
5
|
export const prerender = false; // Required to ensure this runs server-side on Cloudflare
|
|
5
6
|
|
|
@@ -11,7 +12,7 @@ export const GET: APIRoute = async ({ request, locals }) => {
|
|
|
11
12
|
const cfEnv = (locals as any).safeEnv || {};
|
|
12
13
|
|
|
13
14
|
const cmsWorker = cfEnv.CMS_WORKER;
|
|
14
|
-
const apiUrl = cfEnv
|
|
15
|
+
const apiUrl = getCmsApiUrl(cfEnv);
|
|
15
16
|
const secret = cfEnv.FRONTEND_API_SECRET || import.meta.env.FRONTEND_API_SECRET;
|
|
16
17
|
|
|
17
18
|
if (!secret) {
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
export const partial = true; // Tells Astro not to inject DOCTYPE or <head> tags
|
|
4
4
|
|
|
5
5
|
import KamaImage from "@/components/ui/KamaImage.astro";
|
|
6
|
+
import { getCmsApiUrl } from "@/lib/cms-fetch";
|
|
6
7
|
|
|
7
8
|
const { request, locals } = Astro;
|
|
8
9
|
const url = new URL(request.url);
|
|
@@ -11,10 +12,7 @@ const searchParams = url.searchParams;
|
|
|
11
12
|
// 1. Get Environment Variables securely via middleware
|
|
12
13
|
const cfEnv = (locals as any).safeEnv || {};
|
|
13
14
|
const cmsWorker = cfEnv.CMS_WORKER;
|
|
14
|
-
const apiUrl =
|
|
15
|
-
cfEnv.PUBLIC_API_URL ||
|
|
16
|
-
import.meta.env.PUBLIC_API_URL ||
|
|
17
|
-
"http://localhost:8787";
|
|
15
|
+
const apiUrl = getCmsApiUrl(cfEnv);
|
|
18
16
|
const secret = cfEnv.FRONTEND_API_SECRET || import.meta.env.FRONTEND_API_SECRET;
|
|
19
17
|
|
|
20
18
|
// 2. Construct the actual backend URL
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/pages/llms-full.txt.ts
|
|
2
2
|
import type { APIRoute } from "astro";
|
|
3
3
|
import { getTree, getTreeId, getChannelId, getNodeByPath } from "../lib/cms";
|
|
4
|
+
import { getCmsApiUrl } from "../lib/cms-fetch";
|
|
4
5
|
import { renderTiptapNode } from "../utils/tiptap";
|
|
5
6
|
|
|
6
7
|
export const prerender = false;
|
|
@@ -8,12 +9,10 @@ export const prerender = false;
|
|
|
8
9
|
export const GET: APIRoute = async (context) => {
|
|
9
10
|
// 1. Setup Worker Fetch Binding
|
|
10
11
|
const locals = (context.locals as any) || {};
|
|
11
|
-
const cfEnv = locals.cfContext?.env || {};
|
|
12
|
+
const cfEnv = locals.safeEnv || locals.cfContext?.env || {};
|
|
12
13
|
const cmsWorker = cfEnv.CMS_WORKER;
|
|
13
14
|
|
|
14
|
-
const
|
|
15
|
-
const workerApiUrl = cfEnv.PUBLIC_API_URL || (typeof process !== "undefined" ? process.env?.PUBLIC_API_URL : undefined);
|
|
16
|
-
const baseApiUrl = String(envApiUrl || workerApiUrl || "http://localhost:8787").replace(/\/$/, "");
|
|
15
|
+
const baseApiUrl = getCmsApiUrl(cfEnv);
|
|
17
16
|
|
|
18
17
|
const customFetch = async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
19
18
|
let urlStr = input instanceof URL ? input.toString() : String(input);
|
|
@@ -40,7 +39,7 @@ export const GET: APIRoute = async (context) => {
|
|
|
40
39
|
|
|
41
40
|
try {
|
|
42
41
|
// 2. Fetch Navigation Tree first to determine root nodes
|
|
43
|
-
const rawNavTree = await getTree(treeId, channelId, customFetch).catch(() => []);
|
|
42
|
+
const rawNavTree = await getTree(treeId, channelId, customFetch, cfEnv).catch(() => []);
|
|
44
43
|
const navTree = Array.isArray(rawNavTree)
|
|
45
44
|
? rawNavTree
|
|
46
45
|
: (rawNavTree?.tree || rawNavTree?.items || rawNavTree?.data || []);
|
|
@@ -50,7 +49,7 @@ export const GET: APIRoute = async (context) => {
|
|
|
50
49
|
const resolvedHomeSlug = rootTreeSlug || homeSlug;
|
|
51
50
|
|
|
52
51
|
// Fetch Home Node (for global context)
|
|
53
|
-
const homeData = await getNodeByPath(resolvedHomeSlug, channelId, false, customFetch).catch(() => null);
|
|
52
|
+
const homeData = await getNodeByPath(resolvedHomeSlug, channelId, false, customFetch, cfEnv).catch(() => null);
|
|
54
53
|
|
|
55
54
|
const siteSummary = homeData?.node?.abstract || homeData?.node?.summary || "Full Application Documentation Corpus";
|
|
56
55
|
|
|
@@ -102,7 +101,8 @@ export const GET: APIRoute = async (context) => {
|
|
|
102
101
|
fetchSlug,
|
|
103
102
|
channelId,
|
|
104
103
|
false,
|
|
105
|
-
customFetch
|
|
104
|
+
customFetch,
|
|
105
|
+
cfEnv
|
|
106
106
|
).catch(() => null);
|
|
107
107
|
|
|
108
108
|
// Add Document Title Header
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/pages/llms.txt.ts
|
|
2
2
|
import type { APIRoute } from "astro";
|
|
3
3
|
import { getTree, getTreeId, getChannelId, getNodeByPath } from "../lib/cms";
|
|
4
|
+
import { getCmsApiUrl } from "../lib/cms-fetch";
|
|
4
5
|
|
|
5
6
|
// Ensure this route is dynamically rendered (SSR)
|
|
6
7
|
export const prerender = false;
|
|
@@ -8,12 +9,10 @@ export const prerender = false;
|
|
|
8
9
|
export const GET: APIRoute = async (context) => {
|
|
9
10
|
// 1. Setup Cloudflare Worker Fetch Binding safely
|
|
10
11
|
const locals = (context.locals as any) || {};
|
|
11
|
-
const cfEnv = locals.cfContext?.env || {};
|
|
12
|
+
const cfEnv = locals.safeEnv || locals.cfContext?.env || {};
|
|
12
13
|
const cmsWorker = cfEnv.CMS_WORKER;
|
|
13
14
|
|
|
14
|
-
const
|
|
15
|
-
const workerApiUrl = cfEnv.PUBLIC_API_URL || (typeof process !== "undefined" ? process.env?.PUBLIC_API_URL : undefined);
|
|
16
|
-
const baseApiUrl = String(envApiUrl || workerApiUrl || "http://localhost:8787").replace(/\/$/, "");
|
|
15
|
+
const baseApiUrl = getCmsApiUrl(cfEnv);
|
|
17
16
|
|
|
18
17
|
const customFetch = async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
19
18
|
let urlStr = input instanceof URL ? input.toString() : String(input);
|
|
@@ -41,7 +40,7 @@ export const GET: APIRoute = async (context) => {
|
|
|
41
40
|
|
|
42
41
|
try {
|
|
43
42
|
// 3. Parallel fetch for Navigation Tree first to resolve root node slug
|
|
44
|
-
const rawNavTree = await getTree(treeId, channelId, customFetch).catch(() => []);
|
|
43
|
+
const rawNavTree = await getTree(treeId, channelId, customFetch, cfEnv).catch(() => []);
|
|
45
44
|
const navTree = Array.isArray(rawNavTree)
|
|
46
45
|
? rawNavTree
|
|
47
46
|
: (rawNavTree?.tree || rawNavTree?.items || rawNavTree?.data || []);
|
|
@@ -50,7 +49,7 @@ export const GET: APIRoute = async (context) => {
|
|
|
50
49
|
const resolvedHomeSlug = rootTreeSlug || homeSlug;
|
|
51
50
|
|
|
52
51
|
// Fetch Home Node summary
|
|
53
|
-
const homeData = await getNodeByPath(resolvedHomeSlug, channelId, false, customFetch).catch(() => null);
|
|
52
|
+
const homeData = await getNodeByPath(resolvedHomeSlug, channelId, false, customFetch, cfEnv).catch(() => null);
|
|
54
53
|
|
|
55
54
|
const siteSummary = homeData?.node?.abstract || homeData?.node?.summary || "Global Content Delivery at the Edge";
|
|
56
55
|
|
|
@@ -4,6 +4,7 @@ export const partial = true;
|
|
|
4
4
|
|
|
5
5
|
import BlockRenderer from "@/components/blocks/BlockRenderer.astro";
|
|
6
6
|
import { getChannelId } from "@/lib/cms";
|
|
7
|
+
import { getCmsApiUrl } from "@/lib/cms-fetch";
|
|
7
8
|
|
|
8
9
|
const safeEnv = (Astro.locals as any).safeEnv || {};
|
|
9
10
|
const cmsWorker = safeEnv.CMS_WORKER;
|
|
@@ -15,10 +16,7 @@ const customFetch = (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
15
16
|
|
|
16
17
|
let urlStr = input instanceof URL ? input.toString() : String(input);
|
|
17
18
|
if (urlStr.startsWith("/")) {
|
|
18
|
-
const baseUrl =
|
|
19
|
-
import.meta.env.PUBLIC_API_URL ||
|
|
20
|
-
safeEnv.PUBLIC_API_URL ||
|
|
21
|
-
"http://127.0.0.1:8787";
|
|
19
|
+
const baseUrl = getCmsApiUrl(safeEnv);
|
|
22
20
|
urlStr = `${baseUrl}${urlStr}`;
|
|
23
21
|
}
|
|
24
22
|
|
|
@@ -4,6 +4,7 @@ export const partial = true; // 🎯 Tells Astro not to wrap this in <html><body
|
|
|
4
4
|
|
|
5
5
|
import { getTree, getTreeId, getChannelId } from "@/lib/cms";
|
|
6
6
|
import { getTreeNodeMenuTitle, type TreeNode } from "@/lib/cms";
|
|
7
|
+
import { getCmsApiUrl } from "@/lib/cms-fetch";
|
|
7
8
|
|
|
8
9
|
const safeEnv = (Astro.locals as any).safeEnv || {};
|
|
9
10
|
const cmsWorker = safeEnv.CMS_WORKER;
|
|
@@ -14,10 +15,7 @@ const customFetch = (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
14
15
|
|
|
15
16
|
let urlStr = input instanceof URL ? input.toString() : String(input);
|
|
16
17
|
if (urlStr.startsWith("/")) {
|
|
17
|
-
const baseUrl =
|
|
18
|
-
import.meta.env.PUBLIC_API_URL ||
|
|
19
|
-
safeEnv.PUBLIC_API_URL ||
|
|
20
|
-
"http://127.0.0.1:8787";
|
|
18
|
+
const baseUrl = getCmsApiUrl(safeEnv);
|
|
21
19
|
urlStr = `${baseUrl}${urlStr}`;
|
|
22
20
|
}
|
|
23
21
|
|
|
@@ -32,7 +30,7 @@ const channelId = getChannelId() || "website";
|
|
|
32
30
|
const treeId = getTreeId() || "main-nav";
|
|
33
31
|
let tree: TreeNode[] = [];
|
|
34
32
|
try {
|
|
35
|
-
tree = (await getTree(treeId, channelId, customFetch)) || [];
|
|
33
|
+
tree = (await getTree(treeId, channelId, customFetch, safeEnv)) || [];
|
|
36
34
|
} catch (e) {
|
|
37
35
|
console.error("[Partials/Nav] Failed to fetch tree:", e);
|
|
38
36
|
}
|