@respectify/astro 1.0.0 → 1.0.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/README.md +4 -2
- package/package.json +5 -5
- package/src/define-runtime.ts +23 -0
- package/src/index.ts +5 -14
- package/src/lib/config.ts +11 -13
- package/src/options.ts +37 -44
- package/src/store/schemas.ts +10 -12
package/README.md
CHANGED
|
@@ -70,7 +70,7 @@ The store, and any functions, live in a module that runs inside your server bund
|
|
|
70
70
|
|
|
71
71
|
```ts
|
|
72
72
|
// src/respectify.runtime.ts
|
|
73
|
-
import { defineRespectifyRuntime } from '@respectify/astro';
|
|
73
|
+
import { defineRespectifyRuntime } from '@respectify/astro/runtime';
|
|
74
74
|
import { createPostgresCommentStore } from '@respectify/astro/postgres';
|
|
75
75
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
76
76
|
import { Pool } from 'pg';
|
|
@@ -85,6 +85,8 @@ export default defineRespectifyRuntime({
|
|
|
85
85
|
});
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
+
Import `defineRespectifyRuntime` from `@respectify/astro/runtime`, not `@respectify/astro`: the main entry is the integration and imports Astro's config, which bundling adapters such as Vercel can't put in a server function.
|
|
89
|
+
|
|
88
90
|
For SQLite, use `createSqliteCommentStore(db)` from `@respectify/astro/sqlite` with your Drizzle SQLite database (for libSQL/Turso, `drizzle({ client: createClient({ url, authToken }) })` from `drizzle-orm/libsql`).
|
|
89
91
|
|
|
90
92
|
### 4. Add the integration
|
|
@@ -366,7 +368,7 @@ The SQLite store's default table is exactly the `Comment` table Astro DB created
|
|
|
366
368
|
|
|
367
369
|
```ts
|
|
368
370
|
// src/respectify.runtime.ts
|
|
369
|
-
import { defineRespectifyRuntime } from '@respectify/astro';
|
|
371
|
+
import { defineRespectifyRuntime } from '@respectify/astro/runtime';
|
|
370
372
|
import { createSqliteCommentStore } from '@respectify/astro/sqlite';
|
|
371
373
|
import { createClient } from '@libsql/client';
|
|
372
374
|
import { drizzle } from 'drizzle-orm/libsql';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@respectify/astro",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Respectify-powered commenting for Astro \u2014 AI moderation, spam filtering, and beautiful UX out of the box.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
],
|
|
30
30
|
"exports": {
|
|
31
31
|
".": "./src/index.ts",
|
|
32
|
+
"./runtime": "./src/define-runtime.ts",
|
|
32
33
|
"./actions": "./src/actions.ts",
|
|
33
34
|
"./credentials": "./src/credentials.ts",
|
|
34
35
|
"./routes/comments": "./src/routes/comments.ts",
|
|
@@ -56,8 +57,7 @@
|
|
|
56
57
|
"astro": "^5.0.0 || ^6.0.0 || ^7.0.0",
|
|
57
58
|
"drizzle-orm": "^0.45.0",
|
|
58
59
|
"pg": "^8.0.0",
|
|
59
|
-
"vitest": "^3.0.0"
|
|
60
|
-
"zod": "^3.0.0"
|
|
60
|
+
"vitest": "^3.0.0 || ^4.0.0 || ^5.0.0"
|
|
61
61
|
},
|
|
62
62
|
"peerDependenciesMeta": {
|
|
63
63
|
"@libsql/client": {
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
|
-
"zod": "^
|
|
77
|
+
"zod": "^4.6.6"
|
|
78
78
|
},
|
|
79
79
|
"scripts": {
|
|
80
80
|
"typecheck": "tsc --noEmit",
|
|
@@ -89,6 +89,6 @@
|
|
|
89
89
|
"drizzle-kit": "^0.31.11",
|
|
90
90
|
"drizzle-orm": "^0.45.4",
|
|
91
91
|
"typescript": "^5.4.3",
|
|
92
|
-
"vitest": "^
|
|
92
|
+
"vitest": "^5.0.5"
|
|
93
93
|
}
|
|
94
94
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RespectifyRuntime } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Define the runtime module named by `respectify({ runtime })`. Its default export must be
|
|
5
|
+
* the value this returns:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* // src/respectify.runtime.ts
|
|
9
|
+
* import { defineRespectifyRuntime } from '@respectify/astro/runtime';
|
|
10
|
+
* import { createSqliteCommentStore } from '@respectify/astro/sqlite';
|
|
11
|
+
*
|
|
12
|
+
* export default defineRespectifyRuntime({ store: createSqliteCommentStore(db) });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* This entry is bundled into the server, so it must never value-import `astro`, `astro/*`
|
|
16
|
+
* or `astro:*`: bundling adapters such as Vercel cannot put Astro's compiler in a function.
|
|
17
|
+
*/
|
|
18
|
+
export function defineRespectifyRuntime(runtime: RespectifyRuntime): RespectifyRuntime {
|
|
19
|
+
return runtime;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type { RespectifyRuntime } from './types';
|
|
23
|
+
export type { ICommentStore, CommentRecord, NewComment, PublicComment } from './store/types';
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import type { AstroIntegration } from 'astro';
|
|
4
4
|
import { envField } from 'astro/config';
|
|
5
5
|
import { parseIntegrationOptions, toResolvedIntegrationOptions } from './options';
|
|
6
|
-
import type { RespectifyIntegrationOptions
|
|
6
|
+
import type { RespectifyIntegrationOptions } from './types';
|
|
7
7
|
|
|
8
8
|
const OPTIONS_MODULE_ID = 'virtual:@respectify/astro/options';
|
|
9
9
|
const RUNTIME_MODULE_ID = 'virtual:@respectify/astro/runtime';
|
|
@@ -94,20 +94,11 @@ export default function respectify(userOptions: RespectifyIntegrationOptions): A
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
/**
|
|
97
|
-
*
|
|
98
|
-
* the
|
|
99
|
-
*
|
|
100
|
-
* ```ts
|
|
101
|
-
* // src/respectify.runtime.ts
|
|
102
|
-
* import { defineRespectifyRuntime } from '@respectify/astro';
|
|
103
|
-
* import { createSqliteCommentStore } from '@respectify/astro/sqlite';
|
|
104
|
-
*
|
|
105
|
-
* export default defineRespectifyRuntime({ store: createSqliteCommentStore(db) });
|
|
106
|
-
* ```
|
|
97
|
+
* Re-exported for compatibility. Runtime modules should import `defineRespectifyRuntime` from
|
|
98
|
+
* `@respectify/astro/runtime`: this entry is the integration and imports `astro/config`, which
|
|
99
|
+
* bundling adapters such as Vercel cannot put in a server function.
|
|
107
100
|
*/
|
|
108
|
-
export
|
|
109
|
-
return runtime;
|
|
110
|
-
}
|
|
101
|
+
export { defineRespectifyRuntime } from './define-runtime';
|
|
111
102
|
|
|
112
103
|
export type {
|
|
113
104
|
RespectifyIntegrationOptions,
|
package/src/lib/config.ts
CHANGED
|
@@ -22,14 +22,13 @@ const BELOW_FLOOR_TOLERANCE_LEVELS: readonly ToleranceLevel[] = ['off', 'permiss
|
|
|
22
22
|
/** A floored category's level, rejecting "permissive", "relaxed" and "off" with the same wording the API uses. */
|
|
23
23
|
function flooredToleranceLevelSchema(
|
|
24
24
|
category: FlooredModerationCategory,
|
|
25
|
-
): z.ZodEnum<
|
|
25
|
+
): z.ZodEnum<{ [Level in FlooredToleranceLevel]: Level }> {
|
|
26
26
|
return z.enum(FLOORED_TOLERANCE_LEVELS, {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}),
|
|
27
|
+
// Returning undefined falls through to Zod's default message for any other invalid value
|
|
28
|
+
error: (issue) =>
|
|
29
|
+
BELOW_FLOOR_TOLERANCE_LEVELS.includes(issue.input as ToleranceLevel)
|
|
30
|
+
? `${category} can be made stricter but not relaxed or turned off`
|
|
31
|
+
: undefined,
|
|
33
32
|
});
|
|
34
33
|
}
|
|
35
34
|
|
|
@@ -38,7 +37,7 @@ function flooredToleranceLevelSchema(
|
|
|
38
37
|
* quality). Items not named are "standard". Unknown items are rejected.
|
|
39
38
|
*/
|
|
40
39
|
const ModerationTolerancesSchema = z
|
|
41
|
-
.
|
|
40
|
+
.strictObject({
|
|
42
41
|
threat: flooredToleranceLevelSchema('threat').optional(),
|
|
43
42
|
identity_attack: flooredToleranceLevelSchema('identity_attack').optional(),
|
|
44
43
|
sexual_content: flooredToleranceLevelSchema('sexual_content').optional(),
|
|
@@ -49,8 +48,7 @@ const ModerationTolerancesSchema = z
|
|
|
49
48
|
logical_fallacy: ToleranceLevelSchema.optional(),
|
|
50
49
|
low_effort: ToleranceLevelSchema.optional(),
|
|
51
50
|
quality: ToleranceLevelSchema.optional(),
|
|
52
|
-
})
|
|
53
|
-
.strict() satisfies z.ZodType<ModerationTolerances>;
|
|
51
|
+
}) satisfies z.ZodType<ModerationTolerances>;
|
|
54
52
|
|
|
55
53
|
const MegacallConfigSchema = z
|
|
56
54
|
.object({
|
|
@@ -67,11 +65,11 @@ const MegacallConfigSchema = z
|
|
|
67
65
|
|
|
68
66
|
const ClientConfigSchema = z
|
|
69
67
|
.object({
|
|
70
|
-
baseUrl: z.
|
|
68
|
+
baseUrl: z.url().optional(),
|
|
71
69
|
version: z.string().optional(),
|
|
72
70
|
timeout: z.number().positive().optional(),
|
|
73
71
|
website: z.string().optional(),
|
|
74
|
-
email: z.
|
|
72
|
+
email: z.email().optional(),
|
|
75
73
|
apiKey: z.string().optional(),
|
|
76
74
|
})
|
|
77
75
|
.optional();
|
|
@@ -79,7 +77,7 @@ const ClientConfigSchema = z
|
|
|
79
77
|
const RespectifyConfigSchema = z.object({
|
|
80
78
|
enabled: z.boolean(),
|
|
81
79
|
/** @deprecated Prefer `client.baseUrl` — still accepted and mapped to the client base URL */
|
|
82
|
-
apiEndpoint: z.
|
|
80
|
+
apiEndpoint: z.url().optional(),
|
|
83
81
|
requiresAuth: z.boolean().optional(),
|
|
84
82
|
autoPublish: z
|
|
85
83
|
.object({
|
package/src/options.ts
CHANGED
|
@@ -22,29 +22,25 @@ const RUNTIME_ONLY_OPTIONS = ['getPostUrl', 'getPostContent'] as const;
|
|
|
22
22
|
/** Credentials are never serialized into the server bundle. */
|
|
23
23
|
const CREDENTIAL_CLIENT_OPTIONS = ['email', 'apiKey'] as const;
|
|
24
24
|
|
|
25
|
-
const clientConfigSchema = z
|
|
26
|
-
.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
32
|
-
.strict() satisfies z.ZodType<RespectifyIntegrationClientConfig>;
|
|
25
|
+
const clientConfigSchema = z.strictObject({
|
|
26
|
+
baseUrl: z.url().optional(),
|
|
27
|
+
version: z.string().optional(),
|
|
28
|
+
timeout: z.number().positive().optional(),
|
|
29
|
+
website: z.string().optional(),
|
|
30
|
+
}) satisfies z.ZodType<RespectifyIntegrationClientConfig>;
|
|
33
31
|
|
|
34
|
-
const megacallConfigSchema = z
|
|
35
|
-
.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
})
|
|
47
|
-
.strict() satisfies z.ZodType<RespectifyMegacallConfig>;
|
|
32
|
+
const megacallConfigSchema = z.strictObject({
|
|
33
|
+
includeSpam: z.boolean().optional(),
|
|
34
|
+
includeRelevance: z.boolean().optional(),
|
|
35
|
+
includeCommentScore: z.boolean().optional(),
|
|
36
|
+
includeDogwhistle: z.boolean().optional(),
|
|
37
|
+
includePerspectiveAnalyzeComment: z.boolean().optional(),
|
|
38
|
+
bannedTopics: z.array(z.string()).optional(),
|
|
39
|
+
sensitiveTopics: z.array(z.string()).optional(),
|
|
40
|
+
dogwhistleExamples: z.array(z.string()).optional(),
|
|
41
|
+
replyToComment: z.string().optional(),
|
|
42
|
+
contextComments: z.array(z.string()).optional(),
|
|
43
|
+
}) satisfies z.ZodType<RespectifyMegacallConfig>;
|
|
48
44
|
|
|
49
45
|
/** Tolerances are checked by the Respectify client's own validator, so the wording matches the API. */
|
|
50
46
|
const tolerancesSchema = z.custom<ModerationTolerances>().superRefine((tolerances, ctx) => {
|
|
@@ -52,31 +48,28 @@ const tolerancesSchema = z.custom<ModerationTolerances>().superRefine((tolerance
|
|
|
52
48
|
validateModerationTolerances(tolerances);
|
|
53
49
|
} catch (error) {
|
|
54
50
|
if (!(error instanceof RespectifyError)) throw error;
|
|
55
|
-
ctx.addIssue({ code:
|
|
51
|
+
ctx.addIssue({ code: 'custom', message: `Invalid Respectify tolerances: ${error.message}` });
|
|
56
52
|
}
|
|
57
53
|
});
|
|
58
54
|
|
|
59
|
-
const integrationOptionsSchema = z
|
|
60
|
-
.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
tolerances: tolerancesSchema.optional(),
|
|
78
|
-
})
|
|
79
|
-
.strict();
|
|
55
|
+
const integrationOptionsSchema = z.strictObject({
|
|
56
|
+
runtime: z.string().min(1, 'runtime must name the module that calls defineRespectifyRuntime()'),
|
|
57
|
+
configPath: z.string().min(1).default(DEFAULT_INTEGRATION_OPTIONS.configPath),
|
|
58
|
+
commentsApiPath: z
|
|
59
|
+
.string()
|
|
60
|
+
.startsWith('/', 'commentsApiPath must be a path starting with "/"')
|
|
61
|
+
.default(DEFAULT_INTEGRATION_OPTIONS.commentsApiPath),
|
|
62
|
+
showBranding: z.boolean().default(DEFAULT_INTEGRATION_OPTIONS.showBranding),
|
|
63
|
+
rateLimit: z
|
|
64
|
+
.strictObject({
|
|
65
|
+
windowMs: z.number().int().positive(),
|
|
66
|
+
maxRequests: z.number().int().positive(),
|
|
67
|
+
})
|
|
68
|
+
.default(DEFAULT_INTEGRATION_OPTIONS.rateLimit),
|
|
69
|
+
client: clientConfigSchema.optional(),
|
|
70
|
+
megacall: megacallConfigSchema.optional(),
|
|
71
|
+
tolerances: tolerancesSchema.optional(),
|
|
72
|
+
});
|
|
80
73
|
|
|
81
74
|
/** The validated options: what the server bundle sees, plus the runtime module's specifier. */
|
|
82
75
|
export type ParsedIntegrationOptions = ResolvedIntegrationOptions & { runtime: string };
|
package/src/store/schemas.ts
CHANGED
|
@@ -6,22 +6,20 @@ import type { NewComment } from './types';
|
|
|
6
6
|
export const commentIdSchema = z.number().int().positive();
|
|
7
7
|
|
|
8
8
|
/** What a store accepts in `create`. Every store validates its input against this. */
|
|
9
|
-
export const newCommentSchema = z
|
|
10
|
-
.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
})
|
|
18
|
-
.strict() satisfies z.ZodType<NewComment>;
|
|
9
|
+
export const newCommentSchema = z.strictObject({
|
|
10
|
+
postSlug: z.string().min(1).max(COMMENT_LIMITS.postSlugMax),
|
|
11
|
+
author: z.string().min(1).max(COMMENT_LIMITS.authorMax),
|
|
12
|
+
email: z.email().max(COMMENT_LIMITS.emailMax).optional(),
|
|
13
|
+
content: z.string().min(1).max(COMMENT_LIMITS.contentMax),
|
|
14
|
+
approved: z.boolean(),
|
|
15
|
+
parentId: commentIdSchema.optional(),
|
|
16
|
+
}) satisfies z.ZodType<NewComment>;
|
|
19
17
|
|
|
20
18
|
/** Thrown by a store when `create` is given input that does not match `newCommentSchema`. */
|
|
21
19
|
export class CommentValidationError extends Error {
|
|
22
|
-
readonly issues: z.ZodIssue[];
|
|
20
|
+
readonly issues: z.core.$ZodIssue[];
|
|
23
21
|
|
|
24
|
-
constructor(issues: z.ZodIssue[]) {
|
|
22
|
+
constructor(issues: z.core.$ZodIssue[]) {
|
|
25
23
|
super(`Invalid comment: ${issues.map((issue) => `${issue.path.join('.') || 'input'}: ${issue.message}`).join('; ')}`);
|
|
26
24
|
this.name = 'CommentValidationError';
|
|
27
25
|
this.issues = issues;
|