@respectify/astro 0.1.8 → 0.1.9
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/README.md +20 -0
- package/components/CommentSection.astro +7 -4
- package/package.json +2 -1
- package/src/actions.ts +19 -1
- package/src/credentials.ts +1 -0
- package/src/lib/rate-limit.ts +24 -0
- package/src/lib/service.ts +13 -9
- package/src/runtime-config.ts +3 -1
- package/src/types.ts +8 -0
package/README.md
CHANGED
|
@@ -180,6 +180,14 @@ respectify({
|
|
|
180
180
|
commentsApiPath: '/api/respectify/comments',
|
|
181
181
|
showBranding: true,
|
|
182
182
|
getPostUrl: (slug, site) => `${site}/posts/${slug}/`,
|
|
183
|
+
|
|
184
|
+
// Alternative to getPostUrl: hand Respectify the post body directly instead of
|
|
185
|
+
// having it fetch getPostUrl. Useful for staged/preview/auth-gated content, or
|
|
186
|
+
// when the URL isn't live yet right after deploy. Takes priority over getPostUrl.
|
|
187
|
+
// getPostContent: async (slug) => loadPostBody(slug),
|
|
188
|
+
|
|
189
|
+
// Per-IP submission limit, enforced in the `comments.submit` action.
|
|
190
|
+
// In-memory per server process — not shared across multiple instances/regions.
|
|
183
191
|
rateLimit: { windowMs: 300_000, maxRequests: 10 },
|
|
184
192
|
|
|
185
193
|
// Forwarded to RespectifyClient (email/apiKey still default from env)
|
|
@@ -203,6 +211,18 @@ respectify({
|
|
|
203
211
|
});
|
|
204
212
|
```
|
|
205
213
|
|
|
214
|
+
`showBranding` and `commentsApiPath` set here become the defaults for `<CommentSection>` — you can still override either per-instance via props.
|
|
215
|
+
|
|
216
|
+
### Checking Respectify credentials
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { verifyRespectifyCredentials } from '@respectify/astro/credentials';
|
|
220
|
+
|
|
221
|
+
const isConnected = await verifyRespectifyCredentials();
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Useful for an admin health-check page — returns `true` when `RESPECTIFY_EMAIL`/`RESPECTIFY_API_KEY` (or `client.email`/`client.apiKey`) are valid and the subscription is active.
|
|
225
|
+
|
|
206
226
|
### `respectify.config.json`
|
|
207
227
|
|
|
208
228
|
Moderation policy lives in the JSON file. You can also put `client` / `megacall` there (same shapes as above). Integration options win on conflicts.
|
|
@@ -3,13 +3,14 @@ import '@respectify/astro/styles.css';
|
|
|
3
3
|
import PoweredByRespectify from './PoweredByRespectify.astro';
|
|
4
4
|
import CommentList from './CommentList.astro';
|
|
5
5
|
import CommentForm from './CommentForm.astro';
|
|
6
|
+
import { getIntegrationOptions } from '../src/runtime-config';
|
|
6
7
|
|
|
7
8
|
interface Props {
|
|
8
9
|
/** Unique identifier for the page/post (e.g. blog slug) */
|
|
9
10
|
postSlug: string;
|
|
10
|
-
/** API path
|
|
11
|
+
/** API path (default: the integration's `commentsApiPath` option) */
|
|
11
12
|
apiPath?: string;
|
|
12
|
-
/** Show Powered by Respectify branding (default:
|
|
13
|
+
/** Show Powered by Respectify branding (default: the integration's `showBranding` option) */
|
|
13
14
|
showBranding?: boolean;
|
|
14
15
|
/** Enable admin delete controls when authenticated (default: false) */
|
|
15
16
|
enableDelete?: boolean;
|
|
@@ -19,10 +20,12 @@ interface Props {
|
|
|
19
20
|
style?: string;
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
const integrationOptions = getIntegrationOptions();
|
|
24
|
+
|
|
22
25
|
const {
|
|
23
26
|
postSlug,
|
|
24
|
-
apiPath =
|
|
25
|
-
showBranding =
|
|
27
|
+
apiPath = integrationOptions.commentsApiPath,
|
|
28
|
+
showBranding = integrationOptions.showBranding,
|
|
26
29
|
enableDelete = false,
|
|
27
30
|
class: className,
|
|
28
31
|
style,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@respectify/astro",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Respectify-powered commenting for Astro — AI moderation, spam filtering, and beautiful UX out of the box.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"exports": {
|
|
31
31
|
".": "./src/index.ts",
|
|
32
32
|
"./actions": "./src/actions.ts",
|
|
33
|
+
"./credentials": "./src/credentials.ts",
|
|
33
34
|
"./schema": "./src/schema.ts",
|
|
34
35
|
"./routes/comments": "./src/routes/comments.ts",
|
|
35
36
|
"./components/CommentSection.astro": "./components/CommentSection.astro",
|
package/src/actions.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { defineAction, ActionError } from 'astro:actions';
|
|
|
2
2
|
import { z } from 'astro/zod';
|
|
3
3
|
import { db, Comment, eq } from 'astro:db';
|
|
4
4
|
import { analyzeComment } from './lib/service';
|
|
5
|
+
import { checkRateLimit } from './lib/rate-limit';
|
|
6
|
+
import { getIntegrationOptions } from './runtime-config';
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Merge into your src/actions/index.ts:
|
|
@@ -25,7 +27,23 @@ export const respectifyCommentActions = {
|
|
|
25
27
|
content: z.string().min(1).max(5000),
|
|
26
28
|
parentId: z.number().optional(),
|
|
27
29
|
}),
|
|
28
|
-
handler: async ({ postSlug, author, email, content, parentId }) => {
|
|
30
|
+
handler: async ({ postSlug, author, email, content, parentId }, context) => {
|
|
31
|
+
const { rateLimit } = getIntegrationOptions();
|
|
32
|
+
if (rateLimit) {
|
|
33
|
+
let key = 'unknown';
|
|
34
|
+
try {
|
|
35
|
+
key = context.clientAddress;
|
|
36
|
+
} catch {
|
|
37
|
+
// Some adapters/rendering modes don't expose a client address; fall back to a shared bucket.
|
|
38
|
+
}
|
|
39
|
+
if (!checkRateLimit(key, rateLimit.windowMs, rateLimit.maxRequests)) {
|
|
40
|
+
throw new ActionError({
|
|
41
|
+
code: 'TOO_MANY_REQUESTS',
|
|
42
|
+
message: 'Too many comments submitted. Please wait a bit before trying again.',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
29
47
|
let replyToComment: string | undefined;
|
|
30
48
|
if (parentId !== undefined) {
|
|
31
49
|
const [parent] = await db.select().from(Comment).where(eq(Comment.id, parentId)).limit(1);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { verifyRespectifyCredentials } from './lib/service';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface Bucket {
|
|
2
|
+
count: number;
|
|
3
|
+
resetAt: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const buckets = new Map<string, Bucket>();
|
|
7
|
+
|
|
8
|
+
/** Simple in-memory fixed-window rate limiter, keyed per process (not shared across instances/regions). */
|
|
9
|
+
export function checkRateLimit(key: string, windowMs: number, maxRequests: number): boolean {
|
|
10
|
+
const now = Date.now();
|
|
11
|
+
const bucket = buckets.get(key);
|
|
12
|
+
|
|
13
|
+
if (!bucket || now >= bucket.resetAt) {
|
|
14
|
+
buckets.set(key, { count: 1, resetAt: now + windowMs });
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (bucket.count >= maxRequests) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
bucket.count += 1;
|
|
23
|
+
return true;
|
|
24
|
+
}
|
package/src/lib/service.ts
CHANGED
|
@@ -54,18 +54,22 @@ async function getOrInitializeArticleId(postSlug: string, config: RespectifyConf
|
|
|
54
54
|
const cached = articleIdCache.get(postSlug);
|
|
55
55
|
if (cached) return cached;
|
|
56
56
|
|
|
57
|
-
const { getPostUrl } = getIntegrationOptions();
|
|
58
|
-
const site = import.meta.env.SITE;
|
|
59
|
-
if (!site) {
|
|
60
|
-
throw new Error('import.meta.env.SITE must be set in astro.config.ts for Respectify topic initialization.');
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const postUrl = getPostUrl!(postSlug, site);
|
|
57
|
+
const { getPostUrl, getPostContent } = getIntegrationOptions();
|
|
64
58
|
const respectifyClient = getRespectifyClient(config);
|
|
59
|
+
const topicDescription = `Post: ${postSlug}`;
|
|
60
|
+
|
|
61
|
+
const result = await (getPostContent
|
|
62
|
+
? respectifyClient.initTopicFromText(await getPostContent(postSlug), topicDescription)
|
|
63
|
+
: (() => {
|
|
64
|
+
const site = import.meta.env.SITE;
|
|
65
|
+
if (!site) {
|
|
66
|
+
throw new Error('import.meta.env.SITE must be set in astro.config.ts for Respectify topic initialization.');
|
|
67
|
+
}
|
|
68
|
+
return respectifyClient.initTopicFromUrl(getPostUrl!(postSlug, site), topicDescription);
|
|
69
|
+
})());
|
|
65
70
|
|
|
66
|
-
const result = await respectifyClient.initTopicFromUrl(postUrl, `Post: ${postSlug}`);
|
|
67
71
|
articleIdCache.set(postSlug, result.article_id);
|
|
68
|
-
logger.info('Initialized Respectify topic', { postSlug });
|
|
72
|
+
logger.info('Initialized Respectify topic', { postSlug, source: getPostContent ? 'text' : 'url' });
|
|
69
73
|
|
|
70
74
|
return result.article_id;
|
|
71
75
|
}
|
package/src/runtime-config.ts
CHANGED
|
@@ -3,13 +3,14 @@ import type { RespectifyIntegrationOptions, RespectifyClientConfig, RespectifyMe
|
|
|
3
3
|
export type ResolvedIntegrationOptions = Required<
|
|
4
4
|
Pick<RespectifyIntegrationOptions, 'configPath' | 'commentsApiPath' | 'showBranding'>
|
|
5
5
|
> &
|
|
6
|
-
Pick<RespectifyIntegrationOptions, 'getPostUrl' | 'rateLimit' | 'client' | 'megacall'>;
|
|
6
|
+
Pick<RespectifyIntegrationOptions, 'getPostUrl' | 'getPostContent' | 'rateLimit' | 'client' | 'megacall'>;
|
|
7
7
|
|
|
8
8
|
const defaults: ResolvedIntegrationOptions = {
|
|
9
9
|
configPath: './respectify.config.json',
|
|
10
10
|
commentsApiPath: '/api/respectify/comments',
|
|
11
11
|
showBranding: true,
|
|
12
12
|
getPostUrl: (slug, site) => `${site.replace(/\/$/, '')}/posts/${slug}/`,
|
|
13
|
+
getPostContent: undefined,
|
|
13
14
|
rateLimit: { windowMs: 5 * 60 * 1000, maxRequests: 10 },
|
|
14
15
|
client: undefined,
|
|
15
16
|
megacall: undefined,
|
|
@@ -22,6 +23,7 @@ export function setIntegrationOptions(userOptions: RespectifyIntegrationOptions
|
|
|
22
23
|
...defaults,
|
|
23
24
|
...userOptions,
|
|
24
25
|
getPostUrl: userOptions.getPostUrl ?? defaults.getPostUrl,
|
|
26
|
+
getPostContent: userOptions.getPostContent,
|
|
25
27
|
rateLimit: userOptions.rateLimit ?? defaults.rateLimit,
|
|
26
28
|
client: userOptions.client,
|
|
27
29
|
megacall: userOptions.megacall,
|
package/src/types.ts
CHANGED
|
@@ -19,6 +19,14 @@ export interface RespectifyIntegrationOptions {
|
|
|
19
19
|
/** Build the canonical URL for a post — used to initialize Respectify topics */
|
|
20
20
|
getPostUrl?: (slug: string, site: string) => string;
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Provide a post's raw text content directly instead of having Respectify fetch
|
|
24
|
+
* `getPostUrl`. Useful when the URL isn't publicly fetchable (staged/preview content,
|
|
25
|
+
* auth-gated pages, or timing issues right after deploy). Takes priority over
|
|
26
|
+
* `getPostUrl` for topic initialization when provided.
|
|
27
|
+
*/
|
|
28
|
+
getPostContent?: (slug: string) => string | Promise<string>;
|
|
29
|
+
|
|
22
30
|
/** API route path for fetching comments (default: /api/respectify/comments) */
|
|
23
31
|
commentsApiPath?: string;
|
|
24
32
|
|