@seriphxyz/astro 0.1.2
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 +21 -0
- package/README.md +207 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +111 -0
- package/dist/loader.d.ts +105 -0
- package/dist/loader.js +141 -0
- package/package.json +50 -0
- package/src/Comments.astro +391 -0
- package/src/Form.astro +225 -0
- package/src/Reactions.astro +290 -0
- package/src/index.ts +212 -0
- package/src/loader.ts +264 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Tim Shedor
|
|
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,207 @@
|
|
|
1
|
+
# @seriphxyz/astro
|
|
2
|
+
|
|
3
|
+
> **Note:** This repo is a read-only mirror. Source lives in a private monorepo.
|
|
4
|
+
> For issues/PRs, please open them here and we'll sync changes back.
|
|
5
|
+
|
|
6
|
+
Astro components and content loader for [Seriph](https://seriph.xyz) - widgets for static sites.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @seriphxyz/astro
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
Add your Seriph site key to your `.env`:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
SERIPH_SITE_KEY=your_site_key_here
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Content Loader (Posts)
|
|
23
|
+
|
|
24
|
+
Fetch posts from Seriph at build time using Astro's content collections:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// src/content.config.ts
|
|
28
|
+
import { defineCollection } from "astro:content";
|
|
29
|
+
import { seriphPostsLoader } from "@seriphxyz/astro/loader";
|
|
30
|
+
|
|
31
|
+
const posts = defineCollection({
|
|
32
|
+
loader: seriphPostsLoader({
|
|
33
|
+
siteKey: import.meta.env.SERIPH_SITE_KEY,
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export const collections = { posts };
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then use in your pages:
|
|
41
|
+
|
|
42
|
+
```astro
|
|
43
|
+
---
|
|
44
|
+
import { getCollection } from "astro:content";
|
|
45
|
+
const posts = await getCollection("posts");
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
{posts.map((post) => (
|
|
49
|
+
<article>
|
|
50
|
+
<h2>{post.data.title}</h2>
|
|
51
|
+
<p>{post.data.excerpt}</p>
|
|
52
|
+
</article>
|
|
53
|
+
))}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Loader Options
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
seriphPostsLoader({
|
|
60
|
+
siteKey: string; // Required - your Seriph site key
|
|
61
|
+
endpoint?: string; // Default: 'https://seriph.xyz'
|
|
62
|
+
tag?: string; // Filter posts by tag
|
|
63
|
+
limit?: number; // Max posts to fetch (default: 500)
|
|
64
|
+
onError?: 'throw' | 'warn' | 'ignore'; // Error handling
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Components
|
|
69
|
+
|
|
70
|
+
### Form
|
|
71
|
+
|
|
72
|
+
A wrapper component for contact forms with built-in spam protection:
|
|
73
|
+
|
|
74
|
+
```astro
|
|
75
|
+
---
|
|
76
|
+
import Form from "@seriphxyz/astro/Form";
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
<Form siteKey={import.meta.env.SERIPH_SITE_KEY} formSlug="contact">
|
|
80
|
+
<input name="name" placeholder="Name" required />
|
|
81
|
+
<input name="email" type="email" placeholder="Email" required />
|
|
82
|
+
<textarea name="message" placeholder="Message" required></textarea>
|
|
83
|
+
<button type="submit">Send</button>
|
|
84
|
+
</Form>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**Props:**
|
|
88
|
+
- `siteKey` (required) - Your Seriph site key
|
|
89
|
+
- `formSlug` (required) - The form slug as configured in Seriph
|
|
90
|
+
- `endpoint` - Base URL (default: `https://seriph.xyz`)
|
|
91
|
+
- `theme` - `'light'` | `'dark'` | `'auto'` (default: `'light'`)
|
|
92
|
+
- `class` - Additional CSS class
|
|
93
|
+
|
|
94
|
+
**Events:**
|
|
95
|
+
- `seriph:loading` - Form submission started
|
|
96
|
+
- `seriph:success` - Submission successful (detail contains response)
|
|
97
|
+
- `seriph:error` - Submission failed (detail contains error)
|
|
98
|
+
|
|
99
|
+
### Comments
|
|
100
|
+
|
|
101
|
+
Threaded comments with a submission form:
|
|
102
|
+
|
|
103
|
+
```astro
|
|
104
|
+
---
|
|
105
|
+
import Comments from "@seriphxyz/astro/Comments";
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
<Comments
|
|
109
|
+
siteKey={import.meta.env.SERIPH_SITE_KEY}
|
|
110
|
+
pageId={Astro.url.pathname}
|
|
111
|
+
/>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Props:**
|
|
115
|
+
- `siteKey` (required) - Your Seriph site key
|
|
116
|
+
- `pageId` (required) - Unique page identifier (e.g., URL path)
|
|
117
|
+
- `endpoint` - Base URL (default: `https://seriph.xyz`)
|
|
118
|
+
- `theme` - `'light'` | `'dark'` | `'auto'` (default: `'light'`)
|
|
119
|
+
- `class` - Additional CSS class
|
|
120
|
+
|
|
121
|
+
**Events:**
|
|
122
|
+
- `seriph:comment-posted` - Comment submitted (detail contains comment)
|
|
123
|
+
|
|
124
|
+
### Reactions
|
|
125
|
+
|
|
126
|
+
Reaction buttons (like, love, clap, etc.):
|
|
127
|
+
|
|
128
|
+
```astro
|
|
129
|
+
---
|
|
130
|
+
import Reactions from "@seriphxyz/astro/Reactions";
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
<Reactions
|
|
134
|
+
siteKey={import.meta.env.SERIPH_SITE_KEY}
|
|
135
|
+
pageId={Astro.url.pathname}
|
|
136
|
+
reactions={["like", "love", "clap"]}
|
|
137
|
+
/>
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Props:**
|
|
141
|
+
- `siteKey` (required) - Your Seriph site key
|
|
142
|
+
- `pageId` (required) - Unique page identifier
|
|
143
|
+
- `reactions` - Array of reaction types (default: `['like']`)
|
|
144
|
+
- `icons` - Custom icons: `{ like: '👍', love: '❤️' }`
|
|
145
|
+
- `endpoint` - Base URL (default: `https://seriph.xyz`)
|
|
146
|
+
- `theme` - `'light'` | `'dark'` | `'auto'` (default: `'light'`)
|
|
147
|
+
- `class` - Additional CSS class
|
|
148
|
+
|
|
149
|
+
**Built-in icons:** `like`, `love`, `clap`, `fire`, `think`, `sad`, `laugh`
|
|
150
|
+
|
|
151
|
+
**Events:**
|
|
152
|
+
- `seriph:reaction-added` - Reaction added
|
|
153
|
+
- `seriph:reaction-removed` - Reaction removed
|
|
154
|
+
|
|
155
|
+
## JavaScript API
|
|
156
|
+
|
|
157
|
+
For advanced use cases, use the JavaScript API directly:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
import {
|
|
161
|
+
submitForm,
|
|
162
|
+
fetchComments,
|
|
163
|
+
postComment,
|
|
164
|
+
fetchReactions,
|
|
165
|
+
addReaction,
|
|
166
|
+
fetchPosts,
|
|
167
|
+
fetchPost,
|
|
168
|
+
} from "@seriphxyz/astro";
|
|
169
|
+
|
|
170
|
+
// Submit a form
|
|
171
|
+
await submitForm({
|
|
172
|
+
siteKey: "your_key",
|
|
173
|
+
formSlug: "contact",
|
|
174
|
+
data: { name: "John", email: "john@example.com", message: "Hello!" },
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Fetch comments
|
|
178
|
+
const comments = await fetchComments({
|
|
179
|
+
siteKey: "your_key",
|
|
180
|
+
pageId: "/blog/my-post",
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Add a reaction
|
|
184
|
+
await addReaction({
|
|
185
|
+
siteKey: "your_key",
|
|
186
|
+
pageId: "/blog/my-post",
|
|
187
|
+
reactionType: "like",
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Styling
|
|
192
|
+
|
|
193
|
+
Components use CSS custom properties for theming. Override them to match your site:
|
|
194
|
+
|
|
195
|
+
```css
|
|
196
|
+
.seriph-comments {
|
|
197
|
+
--seriph-border-color: #e5e7eb;
|
|
198
|
+
--seriph-bg-color: #f9fafb;
|
|
199
|
+
--seriph-text-color: inherit;
|
|
200
|
+
--seriph-button-bg: #3b82f6;
|
|
201
|
+
/* ... see component source for all variables */
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface SeriphConfig {
|
|
2
|
+
/** Your site key (required) */
|
|
3
|
+
siteKey: string;
|
|
4
|
+
/** @deprecated Use siteKey instead */
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
/** Base URL of your Seriph instance (default: 'https://seriph.xyz') */
|
|
7
|
+
endpoint?: string;
|
|
8
|
+
}
|
|
9
|
+
/** @deprecated Use SeriphConfig instead */
|
|
10
|
+
export type SeraphConfig = SeriphConfig;
|
|
11
|
+
export { seriphPostsLoader, seraphPostsLoader, fetchPosts, fetchPost, type SeriphPost, type SeraphPost, type SeriphPostsLoaderOptions, type SeraphPostsLoaderOptions, type FetchPostsOptions, type FetchPostOptions, } from "./loader.js";
|
|
12
|
+
export interface FormSubmitOptions {
|
|
13
|
+
onSuccess?: (data: unknown) => void;
|
|
14
|
+
onError?: (error: Error) => void;
|
|
15
|
+
}
|
|
16
|
+
export interface Comment {
|
|
17
|
+
id: string;
|
|
18
|
+
pageId: string;
|
|
19
|
+
parentId?: string;
|
|
20
|
+
authorName: string;
|
|
21
|
+
content: string;
|
|
22
|
+
createdAt: string;
|
|
23
|
+
replies: Comment[];
|
|
24
|
+
}
|
|
25
|
+
export interface ReactionCounts {
|
|
26
|
+
pageId: string;
|
|
27
|
+
counts: Record<string, number>;
|
|
28
|
+
}
|
|
29
|
+
export interface SubmitFormOptions extends SeriphConfig {
|
|
30
|
+
formSlug: string;
|
|
31
|
+
data: Record<string, unknown>;
|
|
32
|
+
/** Form load timestamp for spam detection (auto-set if not provided) */
|
|
33
|
+
formLoadTime?: number;
|
|
34
|
+
}
|
|
35
|
+
export interface FormSubmitResponse {
|
|
36
|
+
success: boolean;
|
|
37
|
+
message: string;
|
|
38
|
+
}
|
|
39
|
+
export declare function submitForm(options: SubmitFormOptions): Promise<FormSubmitResponse>;
|
|
40
|
+
export interface FetchCommentsOptions extends SeriphConfig {
|
|
41
|
+
pageId: string;
|
|
42
|
+
}
|
|
43
|
+
export declare function fetchComments(options: FetchCommentsOptions): Promise<Comment[]>;
|
|
44
|
+
export interface PostCommentOptions extends SeriphConfig {
|
|
45
|
+
pageId: string;
|
|
46
|
+
authorName: string;
|
|
47
|
+
authorEmail?: string;
|
|
48
|
+
content: string;
|
|
49
|
+
parentId?: string;
|
|
50
|
+
}
|
|
51
|
+
export declare function postComment(options: PostCommentOptions): Promise<Comment>;
|
|
52
|
+
export interface FetchReactionsOptions extends SeriphConfig {
|
|
53
|
+
pageId: string;
|
|
54
|
+
}
|
|
55
|
+
export declare function fetchReactions(options: FetchReactionsOptions): Promise<ReactionCounts>;
|
|
56
|
+
export interface AddReactionOptions extends SeriphConfig {
|
|
57
|
+
pageId: string;
|
|
58
|
+
reactionType?: string;
|
|
59
|
+
}
|
|
60
|
+
export declare function addReaction(options: AddReactionOptions): Promise<{
|
|
61
|
+
reactionType: string;
|
|
62
|
+
count: number;
|
|
63
|
+
}>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const DEFAULT_ENDPOINT = "https://seriph.xyz";
|
|
2
|
+
const API_PATH = "/api/v1";
|
|
3
|
+
// Re-export loader
|
|
4
|
+
export { seriphPostsLoader, seraphPostsLoader, fetchPosts, fetchPost, } from "./loader.js";
|
|
5
|
+
// Helper to build API URL
|
|
6
|
+
function buildUrl(endpoint, path) {
|
|
7
|
+
const base = (endpoint || DEFAULT_ENDPOINT).replace(/\/+$/, "");
|
|
8
|
+
return `${base}${API_PATH}${path}`;
|
|
9
|
+
}
|
|
10
|
+
// Helper to get site key (supports both siteKey and deprecated apiKey)
|
|
11
|
+
function getSiteKey(config) {
|
|
12
|
+
const key = config.siteKey || config.apiKey;
|
|
13
|
+
if (!key) {
|
|
14
|
+
throw new Error("siteKey is required");
|
|
15
|
+
}
|
|
16
|
+
return key;
|
|
17
|
+
}
|
|
18
|
+
// Client utilities
|
|
19
|
+
export async function submitForm(options) {
|
|
20
|
+
const { endpoint, formSlug, data, formLoadTime } = options;
|
|
21
|
+
const siteKey = getSiteKey(options);
|
|
22
|
+
const url = buildUrl(endpoint, `/forms/${formSlug}/submit`);
|
|
23
|
+
// Include timestamp for time-based spam detection
|
|
24
|
+
const payload = {
|
|
25
|
+
...data,
|
|
26
|
+
_seriph_ts: formLoadTime || Math.floor(Date.now() / 1000),
|
|
27
|
+
};
|
|
28
|
+
const response = await fetch(url, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
"X-Seriph-Key": siteKey,
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify(payload),
|
|
35
|
+
});
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
throw new Error(`Form submission failed: ${response.statusText}`);
|
|
38
|
+
}
|
|
39
|
+
return response.json();
|
|
40
|
+
}
|
|
41
|
+
export async function fetchComments(options) {
|
|
42
|
+
const { endpoint, pageId } = options;
|
|
43
|
+
const siteKey = getSiteKey(options);
|
|
44
|
+
const url = buildUrl(endpoint, `/comments/${encodeURIComponent(pageId)}`);
|
|
45
|
+
const response = await fetch(url, {
|
|
46
|
+
headers: {
|
|
47
|
+
"X-Seriph-Key": siteKey,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
throw new Error(`Failed to fetch comments: ${response.statusText}`);
|
|
52
|
+
}
|
|
53
|
+
const data = await response.json();
|
|
54
|
+
return data.data || [];
|
|
55
|
+
}
|
|
56
|
+
export async function postComment(options) {
|
|
57
|
+
const { endpoint, pageId, ...rest } = options;
|
|
58
|
+
const siteKey = getSiteKey(options);
|
|
59
|
+
const url = buildUrl(endpoint, `/comments/${encodeURIComponent(pageId)}`);
|
|
60
|
+
const response = await fetch(url, {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: {
|
|
63
|
+
"Content-Type": "application/json",
|
|
64
|
+
"X-Seriph-Key": siteKey,
|
|
65
|
+
},
|
|
66
|
+
body: JSON.stringify({
|
|
67
|
+
authorName: rest.authorName,
|
|
68
|
+
authorEmail: rest.authorEmail,
|
|
69
|
+
content: rest.content,
|
|
70
|
+
parentId: rest.parentId,
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
throw new Error(`Failed to post comment: ${response.statusText}`);
|
|
75
|
+
}
|
|
76
|
+
const data = await response.json();
|
|
77
|
+
return data.data;
|
|
78
|
+
}
|
|
79
|
+
export async function fetchReactions(options) {
|
|
80
|
+
const { endpoint, pageId } = options;
|
|
81
|
+
const siteKey = getSiteKey(options);
|
|
82
|
+
const url = buildUrl(endpoint, `/reactions/${encodeURIComponent(pageId)}`);
|
|
83
|
+
const response = await fetch(url, {
|
|
84
|
+
headers: {
|
|
85
|
+
"X-Seriph-Key": siteKey,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
throw new Error(`Failed to fetch reactions: ${response.statusText}`);
|
|
90
|
+
}
|
|
91
|
+
const data = await response.json();
|
|
92
|
+
return data.data;
|
|
93
|
+
}
|
|
94
|
+
export async function addReaction(options) {
|
|
95
|
+
const { endpoint, pageId, reactionType = "like" } = options;
|
|
96
|
+
const siteKey = getSiteKey(options);
|
|
97
|
+
const url = buildUrl(endpoint, `/reactions/${encodeURIComponent(pageId)}`);
|
|
98
|
+
const response = await fetch(url, {
|
|
99
|
+
method: "POST",
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json",
|
|
102
|
+
"X-Seriph-Key": siteKey,
|
|
103
|
+
},
|
|
104
|
+
body: JSON.stringify({ reactionType }),
|
|
105
|
+
});
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
throw new Error(`Failed to add reaction: ${response.statusText}`);
|
|
108
|
+
}
|
|
109
|
+
const data = await response.json();
|
|
110
|
+
return data.data;
|
|
111
|
+
}
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Astro Content Loader for Seriph Posts
|
|
3
|
+
*
|
|
4
|
+
* Use this loader to fetch posts from your Seriph instance at build time.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // In src/content.config.ts
|
|
8
|
+
* import { defineCollection } from 'astro:content';
|
|
9
|
+
* import { seriphPostsLoader } from 'seriph-astro/loader';
|
|
10
|
+
*
|
|
11
|
+
* const posts = defineCollection({
|
|
12
|
+
* loader: seriphPostsLoader({
|
|
13
|
+
* siteKey: import.meta.env.SERIPH_SITE_KEY,
|
|
14
|
+
* }),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* export const collections = { posts };
|
|
18
|
+
*/
|
|
19
|
+
export interface SeriphPost {
|
|
20
|
+
id: string;
|
|
21
|
+
title: string;
|
|
22
|
+
slug: string;
|
|
23
|
+
content: string;
|
|
24
|
+
excerpt?: string;
|
|
25
|
+
coverImage?: string;
|
|
26
|
+
metaTitle?: string;
|
|
27
|
+
metaDescription?: string;
|
|
28
|
+
tags: string[];
|
|
29
|
+
publishedAt: string;
|
|
30
|
+
}
|
|
31
|
+
/** @deprecated Use SeriphPost instead */
|
|
32
|
+
export type SeraphPost = SeriphPost;
|
|
33
|
+
export interface SeriphPostsLoaderOptions {
|
|
34
|
+
/** Your site key (required) */
|
|
35
|
+
siteKey?: string;
|
|
36
|
+
/** @deprecated Use siteKey instead */
|
|
37
|
+
apiKey?: string;
|
|
38
|
+
/** Base URL of your Seriph instance (default: 'https://seriph.xyz') */
|
|
39
|
+
endpoint?: string;
|
|
40
|
+
/** Filter posts by tag */
|
|
41
|
+
tag?: string;
|
|
42
|
+
/** Maximum number of posts to fetch (default: 500) */
|
|
43
|
+
limit?: number;
|
|
44
|
+
/** How to handle errors: 'throw' (default), 'warn', or 'ignore' */
|
|
45
|
+
onError?: "throw" | "warn" | "ignore";
|
|
46
|
+
}
|
|
47
|
+
/** @deprecated Use SeriphPostsLoaderOptions instead */
|
|
48
|
+
export type SeraphPostsLoaderOptions = SeriphPostsLoaderOptions;
|
|
49
|
+
interface LoaderContext {
|
|
50
|
+
store: {
|
|
51
|
+
set: (entry: {
|
|
52
|
+
id: string;
|
|
53
|
+
data: SeriphPost;
|
|
54
|
+
}) => void;
|
|
55
|
+
clear: () => void;
|
|
56
|
+
};
|
|
57
|
+
logger: {
|
|
58
|
+
info: (message: string) => void;
|
|
59
|
+
warn: (message: string) => void;
|
|
60
|
+
error: (message: string) => void;
|
|
61
|
+
};
|
|
62
|
+
generateDigest: (data: unknown) => string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Creates an Astro content loader that fetches posts from Seriph.
|
|
66
|
+
*
|
|
67
|
+
* Posts are fetched at build time and cached by Astro.
|
|
68
|
+
*/
|
|
69
|
+
export declare function seriphPostsLoader(options: SeriphPostsLoaderOptions): {
|
|
70
|
+
name: string;
|
|
71
|
+
load(context: LoaderContext): Promise<void>;
|
|
72
|
+
};
|
|
73
|
+
/** @deprecated Use seriphPostsLoader instead (note the 'i' in seriph) */
|
|
74
|
+
export declare const seraphPostsLoader: typeof seriphPostsLoader;
|
|
75
|
+
export interface FetchPostsOptions {
|
|
76
|
+
/** Your site key (required) */
|
|
77
|
+
siteKey?: string;
|
|
78
|
+
/** @deprecated Use siteKey instead */
|
|
79
|
+
apiKey?: string;
|
|
80
|
+
/** Base URL of your Seriph instance (default: 'https://seriph.xyz') */
|
|
81
|
+
endpoint?: string;
|
|
82
|
+
/** Filter posts by tag */
|
|
83
|
+
tag?: string;
|
|
84
|
+
/** Maximum number of posts to fetch (default: 500) */
|
|
85
|
+
limit?: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Utility function to fetch posts directly (for server-side use cases)
|
|
89
|
+
*/
|
|
90
|
+
export declare function fetchPosts(options: FetchPostsOptions): Promise<SeriphPost[]>;
|
|
91
|
+
export interface FetchPostOptions {
|
|
92
|
+
/** Your site key (required) */
|
|
93
|
+
siteKey?: string;
|
|
94
|
+
/** @deprecated Use siteKey instead */
|
|
95
|
+
apiKey?: string;
|
|
96
|
+
/** Base URL of your Seriph instance (default: 'https://seriph.xyz') */
|
|
97
|
+
endpoint?: string;
|
|
98
|
+
/** The post slug to fetch */
|
|
99
|
+
slug: string;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Utility function to fetch a single post by slug
|
|
103
|
+
*/
|
|
104
|
+
export declare function fetchPost(options: FetchPostOptions): Promise<SeriphPost | null>;
|
|
105
|
+
export {};
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Astro Content Loader for Seriph Posts
|
|
3
|
+
*
|
|
4
|
+
* Use this loader to fetch posts from your Seriph instance at build time.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // In src/content.config.ts
|
|
8
|
+
* import { defineCollection } from 'astro:content';
|
|
9
|
+
* import { seriphPostsLoader } from 'seriph-astro/loader';
|
|
10
|
+
*
|
|
11
|
+
* const posts = defineCollection({
|
|
12
|
+
* loader: seriphPostsLoader({
|
|
13
|
+
* siteKey: import.meta.env.SERIPH_SITE_KEY,
|
|
14
|
+
* }),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* export const collections = { posts };
|
|
18
|
+
*/
|
|
19
|
+
const DEFAULT_ENDPOINT = "https://seriph.xyz";
|
|
20
|
+
const API_PATH = "/api/v1";
|
|
21
|
+
// Helper to get site key (supports both siteKey and deprecated apiKey)
|
|
22
|
+
function getSiteKey(options) {
|
|
23
|
+
const key = options.siteKey || options.apiKey;
|
|
24
|
+
if (!key) {
|
|
25
|
+
throw new Error("siteKey is required");
|
|
26
|
+
}
|
|
27
|
+
return key;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates an Astro content loader that fetches posts from Seriph.
|
|
31
|
+
*
|
|
32
|
+
* Posts are fetched at build time and cached by Astro.
|
|
33
|
+
*/
|
|
34
|
+
export function seriphPostsLoader(options) {
|
|
35
|
+
const { endpoint = DEFAULT_ENDPOINT, tag, limit = 500, onError = "throw", } = options;
|
|
36
|
+
const siteKey = getSiteKey(options);
|
|
37
|
+
// Build the base API URL (strip trailing slash, add API path)
|
|
38
|
+
const baseUrl = endpoint.replace(/\/+$/, "") + API_PATH;
|
|
39
|
+
return {
|
|
40
|
+
name: "seriph-posts-loader",
|
|
41
|
+
async load(context) {
|
|
42
|
+
const { store, logger } = context;
|
|
43
|
+
try {
|
|
44
|
+
// Build the URL with query parameters
|
|
45
|
+
const url = new URL(`${baseUrl}/posts`);
|
|
46
|
+
url.searchParams.set("limit", String(limit));
|
|
47
|
+
if (tag) {
|
|
48
|
+
url.searchParams.set("tag", tag);
|
|
49
|
+
}
|
|
50
|
+
logger.info(`Fetching posts from ${url.toString()}`);
|
|
51
|
+
const response = await fetch(url.toString(), {
|
|
52
|
+
headers: {
|
|
53
|
+
"X-Seriph-Key": siteKey,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new Error(`Failed to fetch posts: ${response.status} ${response.statusText}`);
|
|
58
|
+
}
|
|
59
|
+
const data = await response.json();
|
|
60
|
+
// Clear previous entries
|
|
61
|
+
store.clear();
|
|
62
|
+
// Add each post as an entry
|
|
63
|
+
for (const post of data.posts) {
|
|
64
|
+
store.set({
|
|
65
|
+
id: post.slug,
|
|
66
|
+
data: {
|
|
67
|
+
id: post.id,
|
|
68
|
+
title: post.title,
|
|
69
|
+
slug: post.slug,
|
|
70
|
+
content: post.content,
|
|
71
|
+
excerpt: post.excerpt,
|
|
72
|
+
coverImage: post.coverImage,
|
|
73
|
+
metaTitle: post.metaTitle,
|
|
74
|
+
metaDescription: post.metaDescription,
|
|
75
|
+
tags: post.tags,
|
|
76
|
+
publishedAt: post.publishedAt,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
logger.info(`Loaded ${data.posts.length} posts from Seriph`);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
84
|
+
if (onError === "throw") {
|
|
85
|
+
logger.error(`Error loading posts: ${message}`);
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
else if (onError === "warn") {
|
|
89
|
+
logger.warn(`Error loading posts (continuing anyway): ${message}`);
|
|
90
|
+
}
|
|
91
|
+
// onError === "ignore" - silently continue
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** @deprecated Use seriphPostsLoader instead (note the 'i' in seriph) */
|
|
97
|
+
export const seraphPostsLoader = seriphPostsLoader;
|
|
98
|
+
/**
|
|
99
|
+
* Utility function to fetch posts directly (for server-side use cases)
|
|
100
|
+
*/
|
|
101
|
+
export async function fetchPosts(options) {
|
|
102
|
+
const { endpoint = DEFAULT_ENDPOINT, tag, limit = 500 } = options;
|
|
103
|
+
const siteKey = getSiteKey(options);
|
|
104
|
+
const baseUrl = endpoint.replace(/\/+$/, "") + API_PATH;
|
|
105
|
+
const url = new URL(`${baseUrl}/posts`);
|
|
106
|
+
url.searchParams.set("limit", String(limit));
|
|
107
|
+
if (tag) {
|
|
108
|
+
url.searchParams.set("tag", tag);
|
|
109
|
+
}
|
|
110
|
+
const response = await fetch(url.toString(), {
|
|
111
|
+
headers: {
|
|
112
|
+
"X-Seriph-Key": siteKey,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
if (!response.ok) {
|
|
116
|
+
throw new Error(`Failed to fetch posts: ${response.status} ${response.statusText}`);
|
|
117
|
+
}
|
|
118
|
+
const data = await response.json();
|
|
119
|
+
return data.posts;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Utility function to fetch a single post by slug
|
|
123
|
+
*/
|
|
124
|
+
export async function fetchPost(options) {
|
|
125
|
+
const { endpoint = DEFAULT_ENDPOINT, slug } = options;
|
|
126
|
+
const siteKey = getSiteKey(options);
|
|
127
|
+
const baseUrl = endpoint.replace(/\/+$/, "") + API_PATH;
|
|
128
|
+
const response = await fetch(`${baseUrl}/posts/${encodeURIComponent(slug)}`, {
|
|
129
|
+
headers: {
|
|
130
|
+
"X-Seriph-Key": siteKey,
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
if (response.status === 404) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
if (!response.ok) {
|
|
137
|
+
throw new Error(`Failed to fetch post: ${response.status} ${response.statusText}`);
|
|
138
|
+
}
|
|
139
|
+
const data = await response.json();
|
|
140
|
+
return data.public_post || data;
|
|
141
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@seriphxyz/astro",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Astro components and content loader for Seriph widgets (forms, comments, reactions, posts)",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/seriphxyz/astro"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://seriph.xyz",
|
|
10
|
+
"author": "Tim Shedor",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./loader": {
|
|
18
|
+
"types": "./dist/loader.d.ts",
|
|
19
|
+
"import": "./dist/loader.js"
|
|
20
|
+
},
|
|
21
|
+
"./Form": "./src/Form.astro",
|
|
22
|
+
"./Comments": "./src/Comments.astro",
|
|
23
|
+
"./Reactions": "./src/Reactions.astro"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"dev": "tsc --watch"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"astro",
|
|
35
|
+
"seriph",
|
|
36
|
+
"forms",
|
|
37
|
+
"comments",
|
|
38
|
+
"reactions",
|
|
39
|
+
"posts",
|
|
40
|
+
"widgets",
|
|
41
|
+
"content-loader"
|
|
42
|
+
],
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"astro": "^5.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"typescript": "^5.7.3"
|
|
49
|
+
}
|
|
50
|
+
}
|