@cancia/astro 0.0.1

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.
@@ -0,0 +1,41 @@
1
+ import {
2
+ detectImageType,
3
+ isValidSite
4
+ } from "../chunk-5IPHDIC6.js";
5
+
6
+ // src/endpoints/upload.ts
7
+ import { getCanciaRuntime } from "virtual:cancia/runtime";
8
+ async function POST({ request }) {
9
+ const { uploadHandler, secret, maxUploadMB } = getCanciaRuntime();
10
+ if (secret) {
11
+ const token = request.headers.get("Authorization")?.replace("Bearer ", "").trim();
12
+ if (token !== secret)
13
+ return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
14
+ }
15
+ const form = await request.formData().catch(() => null);
16
+ const file = form?.get("file");
17
+ const site = form?.get("site");
18
+ if (!(file instanceof File))
19
+ return new Response(JSON.stringify({ error: "Missing file field" }), { status: 400 });
20
+ if (typeof site !== "string" || !isValidSite(site))
21
+ return new Response(
22
+ JSON.stringify({ error: "Invalid site (allowed: a-z, 0-9, and . _ -; must start alphanumeric)" }),
23
+ { status: 400 }
24
+ );
25
+ if (file.size > maxUploadMB * 1024 * 1024)
26
+ return new Response(
27
+ JSON.stringify({ error: `File too large (max ${maxUploadMB}MB)` }),
28
+ { status: 413 }
29
+ );
30
+ const buf = new Uint8Array(await file.arrayBuffer());
31
+ const detected = detectImageType(buf);
32
+ if (!detected)
33
+ return new Response(JSON.stringify({ error: "File type not allowed" }), { status: 415 });
34
+ const url = await uploadHandler(file, site, detected);
35
+ return new Response(JSON.stringify({ url }), {
36
+ headers: { "Content-Type": "application/json" }
37
+ });
38
+ }
39
+ export {
40
+ POST
41
+ };
@@ -0,0 +1,145 @@
1
+ import { AstroIntegration } from 'astro';
2
+ import { C as CanciaStorage, a as CanciaStorageV2 } from './types-BMlLS-OS.js';
3
+ export { b as CanciaKVStore, c as CanciaListStore, d as CanciaPageStore, L as ListEntry, P as PageMeta, e as PageRecord, f as PageSEO, R as Rev, g as RevConflictError } from './types-BMlLS-OS.js';
4
+ import { U as UploadHandler } from './upload-DwCGjXbz.js';
5
+ export { m as makeLocalUploadHandler } from './upload-DwCGjXbz.js';
6
+ export { CanciaLoaderOptions, canciaLoader } from './loader/index.js';
7
+ export { FieldDescription, FieldMeta, FieldWidget, ListDescription, ListSchema, SchemasModule, defineList, describeList } from './schema/index.js';
8
+ export { z } from 'zod';
9
+ import 'astro/loaders';
10
+
11
+ interface R2UploadHandlerOptions {
12
+ /** Cloudflare account ID (from R2 dashboard) */
13
+ accountId: string;
14
+ /** R2 bucket name */
15
+ bucket: string;
16
+ /** R2 access key ID */
17
+ accessKeyId: string;
18
+ /** R2 secret access key */
19
+ secretAccessKey: string;
20
+ /**
21
+ * Public base URL for the bucket (e.g. "https://cdn.yoursite.com").
22
+ * The returned image URL will be: `${publicUrl}/<key>`
23
+ */
24
+ publicUrl: string;
25
+ /**
26
+ * Optional path prefix inside the bucket (e.g. "cancia/uploads").
27
+ * Default: "uploads"
28
+ */
29
+ prefix?: string;
30
+ }
31
+ declare function makeR2UploadHandler(opts: R2UploadHandlerOptions): UploadHandler;
32
+
33
+ interface CanciaIntegrationOptions {
34
+ /** Site identifier — used as namespace in storage. Default: "default" */
35
+ site?: string;
36
+ /** Secret token for API auth. Reads CANCIA_TOKEN env var if not set. */
37
+ token?: string;
38
+ /**
39
+ * Available languages. First one is default.
40
+ * If omitted, automatically derived from Astro's built-in `i18n.locales` config.
41
+ * Falls back to ["en"] if neither is set.
42
+ */
43
+ languages?: string[];
44
+ /** Accent colour for the toolbar highlight + buttons. Default: "#6366f1" */
45
+ accentColor?: string;
46
+ /** Deploy hook URL. Reads CANCIA_DEPLOY_HOOK env var if not set. */
47
+ deployHook?: string;
48
+ /**
49
+ * Custom storage adapter.
50
+ * Default: SQLite (cancia.db in project root) — works anywhere with Node.
51
+ * For Cloudflare: pass a D1 adapter.
52
+ * For Vercel: pass a KV adapter.
53
+ * For Netlify: pass a Blobs adapter.
54
+ */
55
+ storage?: CanciaStorage;
56
+ /** Path to the storage file. For JSON adapter: cancia-content.json. For SQLite: cancia.db. */
57
+ dbPath?: string;
58
+ /**
59
+ * Custom upload handler. Receives a File and site string, returns a URL.
60
+ * Default: saves to public/uploads/<site>/ and returns a relative URL.
61
+ * Override with an R2, S3, or Cloudinary handler for production.
62
+ */
63
+ uploadHandler?: UploadHandler;
64
+ /**
65
+ * R2/S3 upload config. When set, Cancia reads these at server start time
66
+ * (after .env is loaded) and creates the R2 handler automatically.
67
+ * Takes precedence over uploadHandler if both are set.
68
+ * Values can reference env vars — they are resolved at server:setup time.
69
+ */
70
+ r2?: R2UploadHandlerOptions | (() => R2UploadHandlerOptions);
71
+ /** Max upload size in MB. Default: 10 */
72
+ maxUploadMB?: number;
73
+ /**
74
+ * Skip session auth and always show the toolbar.
75
+ * Use for public demos only — anyone can edit if this is enabled.
76
+ */
77
+ public?: boolean;
78
+ /**
79
+ * v2 storage adapter (KV + pages + lists). When omitted, Cancia builds a
80
+ * JSON-file v2 adapter automatically at <projectRoot>/.cancia/. List/page
81
+ * routes return 503 if v2 is explicitly set to null.
82
+ */
83
+ storageV2?: CanciaStorageV2 | null;
84
+ /**
85
+ * Override the path to the schemas module. Defaults to
86
+ * `<projectRoot>/src/cms/schemas.ts`. The file must export `schemas`
87
+ * (named or default) — a record of list name → defineList() output.
88
+ */
89
+ schemasPath?: string;
90
+ }
91
+ declare function canciaIntegration(opts?: CanciaIntegrationOptions): AstroIntegration;
92
+
93
+ type CMSData = Record<string, string>;
94
+ /**
95
+ * Fetches all CMS overrides for a site from the Cancia API.
96
+ * Call this in your Astro layout or a shared data-fetching utility.
97
+ *
98
+ * @example
99
+ * // src/i18n/cms.ts
100
+ * import { fetchCMSData } from "@cancia/astro";
101
+ * export const cmsData = await fetchCMSData({
102
+ * apiUrl: import.meta.env.CANCIA_API_URL,
103
+ * site: import.meta.env.CANCIA_SITE,
104
+ * token: import.meta.env.CANCIA_TOKEN,
105
+ * });
106
+ */
107
+ declare function fetchCMSData(opts: {
108
+ apiUrl: string;
109
+ site: string;
110
+ token?: string;
111
+ }): Promise<CMSData>;
112
+
113
+ /**
114
+ * Enhanced useTranslations — identical API to the standard Astro i18n pattern,
115
+ * but accepts optional CMS data to override individual keys.
116
+ *
117
+ * @example
118
+ * // src/i18n/utils.ts
119
+ * import { makeUseTranslations } from "@cancia/astro";
120
+ * import { ui, defaultLang } from "./ui";
121
+ * export const useTranslations = makeUseTranslations(ui, defaultLang);
122
+ *
123
+ * // In your .astro component:
124
+ * const t = useTranslations(lang, cmsData);
125
+ * t("hero.title") // returns CMS override if present, else ui.ts value
126
+ */
127
+ declare function makeUseTranslations<TUI extends Record<string, Record<string, string>>, TLang extends keyof TUI>(ui: TUI, defaultLang: TLang): (lang: TLang, cmsData?: CMSData) => (key: keyof TUI[TLang]) => string;
128
+
129
+ declare function createJsonFileAdapter(filePath?: string): CanciaStorage;
130
+
131
+ interface JsonFileV2Options {
132
+ /** Project root. Defaults to process.cwd(). */
133
+ projectRoot?: string;
134
+ /** Override the KV file path. Defaults to <root>/cancia-content.json. */
135
+ kvPath?: string;
136
+ /** Override the pages file path. Defaults to <root>/.cancia/pages.json. */
137
+ pagesPath?: string;
138
+ /** Override the lists directory. Defaults to <root>/.cancia/lists. */
139
+ listsDir?: string;
140
+ }
141
+ declare function createJsonFileAdapterV2(opts?: JsonFileV2Options): CanciaStorageV2;
142
+
143
+ declare function createSQLiteAdapter(dbPath?: string): CanciaStorage;
144
+
145
+ export { type CMSData, type CanciaIntegrationOptions, CanciaStorage, CanciaStorageV2, type R2UploadHandlerOptions, UploadHandler, canciaIntegration, createJsonFileAdapter, createJsonFileAdapterV2, createSQLiteAdapter, canciaIntegration as default, fetchCMSData, makeR2UploadHandler, makeUseTranslations };