@structcms/api 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 skrischer-mw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,188 @@
1
+ # @structcms/api
2
+
3
+ Storage, domain API, delivery API, and content export for StructCMS. Provides Supabase-agnostic adapter interfaces, handler functions for content CRUD, media management, and JSON export.
4
+
5
+ For architectural context, see [ARCHITECTURE.md](../../ARCHITECTURE.md) (Layer 3: Storage, Layer 4: Domain API, Layer 5: Delivery API).
6
+
7
+ **[← Back to main README](../../README.md)**
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @structcms/api
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { createStorageAdapter, createMediaAdapter } from '@structcms/api';
19
+ import { createClient } from '@supabase/supabase-js';
20
+ import { createNextPagesRoute } from '@structcms/api/next';
21
+
22
+ // 1. Create Supabase client
23
+ const supabase = createClient(
24
+ process.env.SUPABASE_URL!,
25
+ process.env.SUPABASE_SECRET_KEY!
26
+ );
27
+
28
+ // 2. Create adapters
29
+ const storageAdapter = createStorageAdapter({ client: supabase });
30
+ const mediaAdapter = createMediaAdapter({ client: supabase });
31
+
32
+ // 3. Create Next.js API route handlers
33
+ export const { GET, POST } = createNextPagesRoute({
34
+ storageAdapter,
35
+ mediaAdapter
36
+ });
37
+ ```
38
+
39
+ See [examples/test-app/app/api/cms/](../../examples/test-app/app/api/cms/) for complete route handler implementations.
40
+
41
+ ## File Structure
42
+
43
+ ```
44
+ packages/api/
45
+ ├── src/
46
+ │ ├── index.ts # Public exports
47
+ │ ├── storage/
48
+ │ │ ├── types.ts # StorageAdapter interface, Page, Navigation types
49
+ │ │ ├── supabase-adapter.ts # Supabase implementation of StorageAdapter
50
+ │ │ ├── handlers.ts # Page/Navigation CRUD handlers
51
+ │ │ └── *.test.ts
52
+ │ ├── delivery/
53
+ │ │ ├── types.ts # PageResponse, NavigationResponse types
54
+ │ │ ├── handlers.ts # Delivery handlers (list, get by slug)
55
+ │ │ └── *.test.ts
56
+ │ ├── media/
57
+ │ │ ├── types.ts # MediaAdapter interface, MediaFile types
58
+ │ │ ├── supabase-adapter.ts # Supabase implementation of MediaAdapter
59
+ │ │ ├── handlers.ts # Upload, list, delete handlers
60
+ │ │ ├── resolve.ts # Media reference resolution
61
+ │ │ └── *.test.ts
62
+ │ ├── export/
63
+ │ │ ├── types.ts # Export response types
64
+ │ │ ├── handlers.ts # Export handlers (page, site, navigation)
65
+ │ │ └── *.test.ts
66
+ │ ├── utils/
67
+ │ │ ├── slug.ts # Slug generation and uniqueness
68
+ │ │ └── sanitize.ts # HTML sanitization (XSS protection)
69
+ │ └── types/
70
+ │ └── database.types.ts # Generated Supabase types
71
+ ├── package.json
72
+ ├── tsconfig.json
73
+ └── tsup.config.ts
74
+ ```
75
+
76
+ ## Key Concepts
77
+
78
+ ### Handler Functions
79
+
80
+ This package exports **handler functions**, not complete route handlers. This keeps the package framework-agnostic and allows adapter injection. Host projects create thin route handlers that inject their adapters.
81
+
82
+ ### Adapter Interfaces
83
+
84
+ - **`StorageAdapter`** — Interface for page and navigation persistence
85
+ - **`MediaAdapter`** — Interface for media file operations
86
+
87
+ Both have Supabase implementations but the interfaces are Supabase-agnostic for future portability.
88
+
89
+ ### HTML Sanitization
90
+
91
+ Rich text content is sanitized on write using `sanitize-html` to prevent XSS attacks.
92
+
93
+ ## API Reference
94
+
95
+ ### Adapter Factories
96
+
97
+ - **`createStorageAdapter({ client })`** — Create a Supabase storage adapter
98
+ - **`createMediaAdapter({ client, bucketName? })`** — Create a Supabase media adapter
99
+
100
+ ### Storage Handlers
101
+
102
+ - **`handleCreatePage(adapter, input)`** — Create a page with auto-generated slug
103
+ - **`handleUpdatePage(adapter, input)`** — Update an existing page
104
+ - **`handleDeletePage(adapter, id)`** — Delete a page by ID
105
+ - **`handleCreateNavigation(adapter, input)`** — Create a navigation
106
+ - **`handleUpdateNavigation(adapter, input)`** — Update a navigation
107
+ - **`handleDeleteNavigation(adapter, id)`** — Delete a navigation
108
+
109
+ ### Delivery Handlers
110
+
111
+ - **`handleListPages(adapter, options?)`** — List pages with optional filtering
112
+ - **`handleGetPageBySlug(adapter, slug)`** — Get a single page by slug
113
+ - **`handleGetNavigation(adapter, name)`** — Get a navigation by name
114
+
115
+ ### Media Handlers
116
+
117
+ - **`handleUploadMedia(adapter, input)`** — Upload a media file (validates MIME type)
118
+ - **`handleGetMedia(adapter, id)`** — Get media by ID
119
+ - **`handleListMedia(adapter, filter?)`** — List media files
120
+ - **`handleDeleteMedia(adapter, id)`** — Delete a media file
121
+ - **`resolveMediaReferences(adapter, sections)`** — Resolve media IDs to URLs in section data
122
+
123
+ **Allowed MIME types:** `image/jpeg`, `image/png`, `image/gif`, `image/webp`, `image/svg+xml`
124
+
125
+ ### Export Handlers
126
+
127
+ - **`handleExportPage(adapter, mediaAdapter, slug)`** — Export a single page with resolved media
128
+ - **`handleExportAllPages(adapter, mediaAdapter)`** — Export all pages
129
+ - **`handleExportNavigations(adapter)`** — Export all navigations
130
+ - **`handleExportSite(adapter, mediaAdapter)`** — Full site export (pages + navigations + media)
131
+
132
+ ### Next.js Preset Factories
133
+
134
+ - **`createNextPagesRoute({ storageAdapter, mediaAdapter })`** — List and create pages
135
+ - **`createNextPageBySlugRoute({ storageAdapter, mediaAdapter })`** — Get page by slug
136
+ - **`createNextPageByIdRoute({ storageAdapter })`** — Update/delete page by ID
137
+ - **`createNextMediaRoute({ mediaAdapter })`** — List and upload media
138
+ - **`createNextMediaByIdRoute({ mediaAdapter })`** — Delete media by ID
139
+ - **`createNextNavigationRoute({ storageAdapter })`** — List and create navigation
140
+ - **`createNextNavigationByIdRoute({ storageAdapter })`** — Update/delete navigation by ID
141
+
142
+ ### Error Classes
143
+
144
+ - **`StorageError`** — Thrown by storage adapter on database errors
145
+ - **`StorageValidationError`** — Thrown on validation failures (empty title, duplicate slug). Has `code` property.
146
+ - **`MediaError`** — Thrown by media adapter on storage/database errors
147
+ - **`MediaValidationError`** — Thrown on validation failures (invalid MIME type). Has `code` property.
148
+
149
+ ### Constants
150
+
151
+ - **`ALLOWED_MIME_TYPES`** — Readonly array of allowed MIME types
152
+
153
+ ### Utilities
154
+
155
+ - **`generateSlug(title)`** — Generate a URL-safe slug from a title
156
+ - **`ensureUniqueSlug(slug, existingSlugs)`** — Ensure slug uniqueness by appending a suffix if needed
157
+
158
+ ## Dependencies
159
+
160
+ | Package | Purpose |
161
+ |---------|---------|
162
+ | `@supabase/supabase-js` | Database and storage client |
163
+ | `sanitize-html` | Server-side HTML sanitization for XSS protection |
164
+
165
+ ## Database
166
+
167
+ Migrations live in `supabase/migrations/` at the monorepo root. See [ARCHITECTURE.md](../../ARCHITECTURE.md) for the database schema.
168
+
169
+ ## Development
170
+
171
+ ```bash
172
+ # Run tests (watch mode)
173
+ pnpm test
174
+
175
+ # Run tests once
176
+ pnpm test run
177
+
178
+ # Build
179
+ pnpm build
180
+
181
+ # Type check
182
+ pnpm typecheck
183
+
184
+ # Regenerate Supabase types
185
+ pnpm gen:types
186
+ ```
187
+
188
+ **[← Back to main README](../../README.md)**