@kiriti/comments-server 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +143 -2
- package/dist/chunk-MHK3GDPA.js +75 -0
- package/dist/cli.cjs +223 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +207 -0
- package/dist/index.cjs +765 -0
- package/dist/index.d.cts +246 -0
- package/dist/index.d.ts +246 -0
- package/dist/index.js +657 -0
- package/package.json +53 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { Db, MongoClient, ObjectId, Collection } from 'mongodb';
|
|
3
|
+
import { ThreadingConfig, CommentSort, ThreadsPage, SessionUser, CommentDTO, ModerationStatus, ModerationPage, ModerationAction } from '@kiriti/comments-core';
|
|
4
|
+
export * from '@kiriti/comments-core';
|
|
5
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
6
|
+
|
|
7
|
+
interface RateLimitRule {
|
|
8
|
+
/** Allowed requests per window. */
|
|
9
|
+
max: number;
|
|
10
|
+
/** Window length in milliseconds. */
|
|
11
|
+
windowMs: number;
|
|
12
|
+
}
|
|
13
|
+
/** Counts hits per key in fixed windows. Implement this to use Redis or similar. */
|
|
14
|
+
interface RateLimitStore {
|
|
15
|
+
increment(key: string, windowMs: number): Promise<{
|
|
16
|
+
count: number;
|
|
17
|
+
resetAt: number;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
/** Per-process counters. Fine for one server; use the Mongo store (or your own) when running several instances. */
|
|
21
|
+
declare const createMemoryRateLimitStore: () => RateLimitStore;
|
|
22
|
+
/** Shared counters in MongoDB (atomic $inc + TTL cleanup), so limits hold across instances and serverless. */
|
|
23
|
+
declare const createMongoRateLimitStore: (db: Db, collectionName?: string) => Promise<RateLimitStore>;
|
|
24
|
+
|
|
25
|
+
interface GoogleIdentity {
|
|
26
|
+
/** Google's stable account id ("sub"). */
|
|
27
|
+
sub: string;
|
|
28
|
+
name: string;
|
|
29
|
+
picture: string | null;
|
|
30
|
+
/** Only when Google says it is verified; used to recognise moderators, never stored. */
|
|
31
|
+
email?: string;
|
|
32
|
+
}
|
|
33
|
+
interface RateLimitOptions {
|
|
34
|
+
/** New comments per user. Default 5 per minute. */
|
|
35
|
+
comment?: RateLimitRule | false;
|
|
36
|
+
/** Likes, unlikes, reports, edits and deletes per user. Default 60 per minute. */
|
|
37
|
+
action?: RateLimitRule | false;
|
|
38
|
+
/** Sign-ins per IP address. Default 20 per 15 minutes. */
|
|
39
|
+
signIn?: RateLimitRule | false;
|
|
40
|
+
/** "memory" (default, one server), "mongo" (shared across instances) or your own store. */
|
|
41
|
+
store?: "memory" | "mongo" | RateLimitStore;
|
|
42
|
+
}
|
|
43
|
+
interface CommentsServerOptions {
|
|
44
|
+
/** MongoDB connection string, e.g. "mongodb+srv://user:pass@cluster/". Ignored when `client` is given. */
|
|
45
|
+
mongoUrl?: string;
|
|
46
|
+
/** Reuse an existing MongoClient instead of `mongoUrl`. The package never closes a client it didn't create. */
|
|
47
|
+
client?: MongoClient;
|
|
48
|
+
/** Database name. Default "comments". */
|
|
49
|
+
dbName?: string;
|
|
50
|
+
/** Prefix for collection names, to share a database with other data. Default "" → "comments", "comment_users". */
|
|
51
|
+
collectionPrefix?: string;
|
|
52
|
+
/** Google OAuth Web client id; sign-in tokens must be issued for it. Env: COMMENTS_GOOGLE_CLIENT_ID. */
|
|
53
|
+
googleClientId?: string;
|
|
54
|
+
/** Secret that signs the session tokens this server issues after Google sign-in (32+ random characters). Env: COMMENTS_SESSION_SECRET. */
|
|
55
|
+
sessionSecret?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Sites that show the widget, e.g. ["https://example.com"]. Each must also be an "Authorized JavaScript origin" of
|
|
58
|
+
* the Google OAuth client. Published at /config so the widget can warn when it runs elsewhere. Env: COMMENTS_ALLOWED_ORIGINS.
|
|
59
|
+
*/
|
|
60
|
+
allowedOrigins?: string[];
|
|
61
|
+
/** Skip the setup checklist logged at startup. */
|
|
62
|
+
quiet?: boolean;
|
|
63
|
+
/** How long a session lasts, e.g. "7d", "12h". Default "7d". */
|
|
64
|
+
sessionTtl?: string;
|
|
65
|
+
/** true/false, or { enabled, maxDepth }. Default { enabled: true, maxDepth: 3 }. */
|
|
66
|
+
threading?: Partial<ThreadingConfig> | boolean;
|
|
67
|
+
/** A comment is hidden once this many distinct users flag it. Default 5. */
|
|
68
|
+
flagThreshold?: number;
|
|
69
|
+
/** Max comment length in characters. Default 2000. */
|
|
70
|
+
maxLength?: number;
|
|
71
|
+
/** Top-level comments per page (max 50). Default 20. */
|
|
72
|
+
pageSize?: number;
|
|
73
|
+
/** Google account emails that can moderate (case-insensitive). Applied at sign-in. */
|
|
74
|
+
moderatorEmails?: string[];
|
|
75
|
+
/** Minutes after posting during which authors can edit. Default null = any time. */
|
|
76
|
+
editWindowMinutes?: number | null;
|
|
77
|
+
/** Spam protection. `false` turns all limits off. */
|
|
78
|
+
rateLimit?: RateLimitOptions | false;
|
|
79
|
+
/** Override Google token verification (tests, or a custom identity provider). */
|
|
80
|
+
verifyGoogleCredential?: (credential: string) => Promise<GoogleIdentity>;
|
|
81
|
+
}
|
|
82
|
+
interface ResolvedOptions {
|
|
83
|
+
mongoUrl?: string;
|
|
84
|
+
allowedOrigins: string[];
|
|
85
|
+
quiet: boolean;
|
|
86
|
+
dbName: string;
|
|
87
|
+
collectionPrefix: string;
|
|
88
|
+
googleClientId: string;
|
|
89
|
+
sessionSecret: string;
|
|
90
|
+
sessionTtl: string;
|
|
91
|
+
threading: ThreadingConfig;
|
|
92
|
+
flagThreshold: number;
|
|
93
|
+
maxLength: number;
|
|
94
|
+
pageSize: number;
|
|
95
|
+
moderatorEmails: Set<string>;
|
|
96
|
+
editWindowMinutes: number | null;
|
|
97
|
+
rateLimit: {
|
|
98
|
+
comment?: RateLimitRule;
|
|
99
|
+
action?: RateLimitRule;
|
|
100
|
+
signIn?: RateLimitRule;
|
|
101
|
+
store: "memory" | "mongo" | RateLimitStore;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
type Env = Record<string, string | undefined>;
|
|
105
|
+
/**
|
|
106
|
+
* Merges options with COMMENTS_* environment variables (options win) and validates everything up front,
|
|
107
|
+
* so a misconfigured install fails at startup with a message naming the setting, not on the first request.
|
|
108
|
+
*/
|
|
109
|
+
declare const resolveOptions: (input: CommentsServerOptions, env?: Env) => ResolvedOptions;
|
|
110
|
+
|
|
111
|
+
interface CommentsRouter extends Router {
|
|
112
|
+
/** Closes the MongoDB connection if the package opened it. */
|
|
113
|
+
close: () => Promise<void>;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Express router with the whole comments API. Mount it anywhere:
|
|
117
|
+
* app.use("/api/comments", await createCommentsRouter({ mongoUrl, googleClientId, sessionSecret }))
|
|
118
|
+
*
|
|
119
|
+
* GET /config public settings for the UI
|
|
120
|
+
* POST /auth/google {credential} exchange a Google sign-in for a session token
|
|
121
|
+
* GET /me the signed-in user
|
|
122
|
+
* GET /threads?slug=&sort=&offset=&limit=
|
|
123
|
+
* POST /comments {slug, content, parentId?}
|
|
124
|
+
* PATCH /comments/:id {content} author edits
|
|
125
|
+
* DELETE /comments/:id author or moderator
|
|
126
|
+
* POST /comments/:id/like | /unlike | /flag
|
|
127
|
+
* GET /moderation/queue?status=flagged|hidden|recent&offset=
|
|
128
|
+
* POST /moderation/comments/:id/approve | /hide | /unhide | /delete
|
|
129
|
+
* POST /moderation/users/:id/ban | /unban
|
|
130
|
+
*/
|
|
131
|
+
declare const createCommentsRouter: (options: CommentsServerOptions) => Promise<CommentsRouter>;
|
|
132
|
+
|
|
133
|
+
interface CommentsHandlerOptions extends CommentsServerOptions {
|
|
134
|
+
/** Public URL prefix of the API, e.g. "/api/comments" (the widget's `apiBaseUrl`). Default "/api/comments". */
|
|
135
|
+
basePath?: string;
|
|
136
|
+
/**
|
|
137
|
+
* Query parameter holding the API sub-path when a rewrite sends every request to one function, e.g.
|
|
138
|
+
* `/api/comments/:path*` → `/api/some-function?path=:path*`. Default "path".
|
|
139
|
+
*/
|
|
140
|
+
pathParam?: string;
|
|
141
|
+
/** Other query parameters added by your rewrite that the API should not see (e.g. ["action"]). */
|
|
142
|
+
stripParams?: string[];
|
|
143
|
+
}
|
|
144
|
+
type NodeHandler = (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* A ready-to-mount Node request handler for the whole comments API — Vercel functions, Next.js API routes
|
|
147
|
+
* (`export const config = { api: { bodyParser: false } }` optional), or plain `http.createServer`.
|
|
148
|
+
*
|
|
149
|
+
* // api/comments/[...path].ts on Vercel, or behind a rewrite to one shared function:
|
|
150
|
+
* const handler = createCommentsHandler({}); // settings from COMMENTS_* env vars
|
|
151
|
+
* export default handler;
|
|
152
|
+
*
|
|
153
|
+
* The router (and MongoDB connection) starts on the first request and is reused while the instance stays warm;
|
|
154
|
+
* if startup fails (e.g. missing settings) requests get a 503 and the next one retries.
|
|
155
|
+
*/
|
|
156
|
+
declare const createCommentsHandler: (options?: CommentsHandlerOptions) => NodeHandler & {
|
|
157
|
+
close: () => Promise<void>;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
interface CommentDoc {
|
|
161
|
+
_id: ObjectId;
|
|
162
|
+
slug: string;
|
|
163
|
+
parentId: ObjectId | null;
|
|
164
|
+
rootId: ObjectId;
|
|
165
|
+
depth: number;
|
|
166
|
+
content: string;
|
|
167
|
+
authorId: ObjectId;
|
|
168
|
+
authorName: string;
|
|
169
|
+
authorPicture: string | null;
|
|
170
|
+
replyToName: string | null;
|
|
171
|
+
likes: number;
|
|
172
|
+
likedBy: ObjectId[];
|
|
173
|
+
flags: number;
|
|
174
|
+
flaggedBy: ObjectId[];
|
|
175
|
+
hidden: boolean;
|
|
176
|
+
/** A moderator reviewed it; reports no longer auto-hide it. Missing on older documents = false. */
|
|
177
|
+
approved?: boolean;
|
|
178
|
+
/** Soft delete: kept as a placeholder while it has replies. Missing = false. */
|
|
179
|
+
deleted?: boolean;
|
|
180
|
+
deletedBy?: "author" | "moderator";
|
|
181
|
+
editedAt?: Date | null;
|
|
182
|
+
createdAt: Date;
|
|
183
|
+
updatedAt: Date;
|
|
184
|
+
}
|
|
185
|
+
interface UserDoc {
|
|
186
|
+
_id: ObjectId;
|
|
187
|
+
googleSub: string;
|
|
188
|
+
name: string;
|
|
189
|
+
picture: string | null;
|
|
190
|
+
createdAt: Date;
|
|
191
|
+
lastLoginAt: Date;
|
|
192
|
+
/** Set from `moderatorEmails` at each sign-in. */
|
|
193
|
+
isModerator?: boolean;
|
|
194
|
+
/** Banned users can read but not post, like, report or edit. */
|
|
195
|
+
banned?: boolean;
|
|
196
|
+
bannedAt?: Date | null;
|
|
197
|
+
}
|
|
198
|
+
interface Store {
|
|
199
|
+
comments: Collection<CommentDoc>;
|
|
200
|
+
users: Collection<UserDoc>;
|
|
201
|
+
db: Db;
|
|
202
|
+
/** Closes the connection only if this package opened it. */
|
|
203
|
+
close: () => Promise<void>;
|
|
204
|
+
}
|
|
205
|
+
/** Connects (or reuses `client`), returns typed collections and creates the indexes once. */
|
|
206
|
+
declare const openStore: (opts: ResolvedOptions, mongoUrl?: string, client?: MongoClient) => Promise<Store>;
|
|
207
|
+
|
|
208
|
+
interface ListOptions {
|
|
209
|
+
slug: unknown;
|
|
210
|
+
sort?: CommentSort;
|
|
211
|
+
offset?: number;
|
|
212
|
+
limit?: number;
|
|
213
|
+
viewerId?: string | null;
|
|
214
|
+
}
|
|
215
|
+
interface CreateInput {
|
|
216
|
+
slug: unknown;
|
|
217
|
+
content: unknown;
|
|
218
|
+
parentId?: unknown;
|
|
219
|
+
}
|
|
220
|
+
declare const createCommentsService: (store: Store, opts: ResolvedOptions) => {
|
|
221
|
+
listThreads: ({ slug: rawSlug, sort, offset, limit, viewerId }: ListOptions) => Promise<ThreadsPage>;
|
|
222
|
+
createComment: (input: CreateInput, author: SessionUser) => Promise<CommentDTO>;
|
|
223
|
+
editComment: (commentId: unknown, content: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
224
|
+
deleteComment: (commentId: unknown, user: SessionUser) => Promise<void>;
|
|
225
|
+
like: (commentId: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
226
|
+
unlike: (commentId: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
227
|
+
flag: (commentId: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
228
|
+
upsertGoogleUser: (identity: GoogleIdentity) => Promise<SessionUser>;
|
|
229
|
+
listModerationQueue: (user: SessionUser, status: ModerationStatus, offset?: number) => Promise<ModerationPage>;
|
|
230
|
+
moderateComment: (commentId: unknown, action: ModerationAction | "delete", user: SessionUser) => Promise<void>;
|
|
231
|
+
setUserBanned: (userId: unknown, banned: boolean, moderator: SessionUser) => Promise<void>;
|
|
232
|
+
};
|
|
233
|
+
type CommentsService = ReturnType<typeof createCommentsService>;
|
|
234
|
+
|
|
235
|
+
/** An error with an HTTP status and a stable machine-readable code; its message is safe to show users. */
|
|
236
|
+
declare class CommentsError extends Error {
|
|
237
|
+
readonly status: number;
|
|
238
|
+
readonly code: string;
|
|
239
|
+
/** Seconds until the client may retry (sent as Retry-After on 429s). */
|
|
240
|
+
readonly retryAfter?: number | undefined;
|
|
241
|
+
constructor(status: number, code: string, message: string,
|
|
242
|
+
/** Seconds until the client may retry (sent as Retry-After on 429s). */
|
|
243
|
+
retryAfter?: number | undefined);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export { type CommentDoc, CommentsError, type CommentsHandlerOptions, type CommentsRouter, type CommentsServerOptions, type CommentsService, type GoogleIdentity, type RateLimitOptions, type RateLimitRule, type RateLimitStore, type Store, type UserDoc, createCommentsHandler, createCommentsRouter, createCommentsService, createMemoryRateLimitStore, createMongoRateLimitStore, openStore, resolveOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { Db, MongoClient, ObjectId, Collection } from 'mongodb';
|
|
3
|
+
import { ThreadingConfig, CommentSort, ThreadsPage, SessionUser, CommentDTO, ModerationStatus, ModerationPage, ModerationAction } from '@kiriti/comments-core';
|
|
4
|
+
export * from '@kiriti/comments-core';
|
|
5
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
6
|
+
|
|
7
|
+
interface RateLimitRule {
|
|
8
|
+
/** Allowed requests per window. */
|
|
9
|
+
max: number;
|
|
10
|
+
/** Window length in milliseconds. */
|
|
11
|
+
windowMs: number;
|
|
12
|
+
}
|
|
13
|
+
/** Counts hits per key in fixed windows. Implement this to use Redis or similar. */
|
|
14
|
+
interface RateLimitStore {
|
|
15
|
+
increment(key: string, windowMs: number): Promise<{
|
|
16
|
+
count: number;
|
|
17
|
+
resetAt: number;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
/** Per-process counters. Fine for one server; use the Mongo store (or your own) when running several instances. */
|
|
21
|
+
declare const createMemoryRateLimitStore: () => RateLimitStore;
|
|
22
|
+
/** Shared counters in MongoDB (atomic $inc + TTL cleanup), so limits hold across instances and serverless. */
|
|
23
|
+
declare const createMongoRateLimitStore: (db: Db, collectionName?: string) => Promise<RateLimitStore>;
|
|
24
|
+
|
|
25
|
+
interface GoogleIdentity {
|
|
26
|
+
/** Google's stable account id ("sub"). */
|
|
27
|
+
sub: string;
|
|
28
|
+
name: string;
|
|
29
|
+
picture: string | null;
|
|
30
|
+
/** Only when Google says it is verified; used to recognise moderators, never stored. */
|
|
31
|
+
email?: string;
|
|
32
|
+
}
|
|
33
|
+
interface RateLimitOptions {
|
|
34
|
+
/** New comments per user. Default 5 per minute. */
|
|
35
|
+
comment?: RateLimitRule | false;
|
|
36
|
+
/** Likes, unlikes, reports, edits and deletes per user. Default 60 per minute. */
|
|
37
|
+
action?: RateLimitRule | false;
|
|
38
|
+
/** Sign-ins per IP address. Default 20 per 15 minutes. */
|
|
39
|
+
signIn?: RateLimitRule | false;
|
|
40
|
+
/** "memory" (default, one server), "mongo" (shared across instances) or your own store. */
|
|
41
|
+
store?: "memory" | "mongo" | RateLimitStore;
|
|
42
|
+
}
|
|
43
|
+
interface CommentsServerOptions {
|
|
44
|
+
/** MongoDB connection string, e.g. "mongodb+srv://user:pass@cluster/". Ignored when `client` is given. */
|
|
45
|
+
mongoUrl?: string;
|
|
46
|
+
/** Reuse an existing MongoClient instead of `mongoUrl`. The package never closes a client it didn't create. */
|
|
47
|
+
client?: MongoClient;
|
|
48
|
+
/** Database name. Default "comments". */
|
|
49
|
+
dbName?: string;
|
|
50
|
+
/** Prefix for collection names, to share a database with other data. Default "" → "comments", "comment_users". */
|
|
51
|
+
collectionPrefix?: string;
|
|
52
|
+
/** Google OAuth Web client id; sign-in tokens must be issued for it. Env: COMMENTS_GOOGLE_CLIENT_ID. */
|
|
53
|
+
googleClientId?: string;
|
|
54
|
+
/** Secret that signs the session tokens this server issues after Google sign-in (32+ random characters). Env: COMMENTS_SESSION_SECRET. */
|
|
55
|
+
sessionSecret?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Sites that show the widget, e.g. ["https://example.com"]. Each must also be an "Authorized JavaScript origin" of
|
|
58
|
+
* the Google OAuth client. Published at /config so the widget can warn when it runs elsewhere. Env: COMMENTS_ALLOWED_ORIGINS.
|
|
59
|
+
*/
|
|
60
|
+
allowedOrigins?: string[];
|
|
61
|
+
/** Skip the setup checklist logged at startup. */
|
|
62
|
+
quiet?: boolean;
|
|
63
|
+
/** How long a session lasts, e.g. "7d", "12h". Default "7d". */
|
|
64
|
+
sessionTtl?: string;
|
|
65
|
+
/** true/false, or { enabled, maxDepth }. Default { enabled: true, maxDepth: 3 }. */
|
|
66
|
+
threading?: Partial<ThreadingConfig> | boolean;
|
|
67
|
+
/** A comment is hidden once this many distinct users flag it. Default 5. */
|
|
68
|
+
flagThreshold?: number;
|
|
69
|
+
/** Max comment length in characters. Default 2000. */
|
|
70
|
+
maxLength?: number;
|
|
71
|
+
/** Top-level comments per page (max 50). Default 20. */
|
|
72
|
+
pageSize?: number;
|
|
73
|
+
/** Google account emails that can moderate (case-insensitive). Applied at sign-in. */
|
|
74
|
+
moderatorEmails?: string[];
|
|
75
|
+
/** Minutes after posting during which authors can edit. Default null = any time. */
|
|
76
|
+
editWindowMinutes?: number | null;
|
|
77
|
+
/** Spam protection. `false` turns all limits off. */
|
|
78
|
+
rateLimit?: RateLimitOptions | false;
|
|
79
|
+
/** Override Google token verification (tests, or a custom identity provider). */
|
|
80
|
+
verifyGoogleCredential?: (credential: string) => Promise<GoogleIdentity>;
|
|
81
|
+
}
|
|
82
|
+
interface ResolvedOptions {
|
|
83
|
+
mongoUrl?: string;
|
|
84
|
+
allowedOrigins: string[];
|
|
85
|
+
quiet: boolean;
|
|
86
|
+
dbName: string;
|
|
87
|
+
collectionPrefix: string;
|
|
88
|
+
googleClientId: string;
|
|
89
|
+
sessionSecret: string;
|
|
90
|
+
sessionTtl: string;
|
|
91
|
+
threading: ThreadingConfig;
|
|
92
|
+
flagThreshold: number;
|
|
93
|
+
maxLength: number;
|
|
94
|
+
pageSize: number;
|
|
95
|
+
moderatorEmails: Set<string>;
|
|
96
|
+
editWindowMinutes: number | null;
|
|
97
|
+
rateLimit: {
|
|
98
|
+
comment?: RateLimitRule;
|
|
99
|
+
action?: RateLimitRule;
|
|
100
|
+
signIn?: RateLimitRule;
|
|
101
|
+
store: "memory" | "mongo" | RateLimitStore;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
type Env = Record<string, string | undefined>;
|
|
105
|
+
/**
|
|
106
|
+
* Merges options with COMMENTS_* environment variables (options win) and validates everything up front,
|
|
107
|
+
* so a misconfigured install fails at startup with a message naming the setting, not on the first request.
|
|
108
|
+
*/
|
|
109
|
+
declare const resolveOptions: (input: CommentsServerOptions, env?: Env) => ResolvedOptions;
|
|
110
|
+
|
|
111
|
+
interface CommentsRouter extends Router {
|
|
112
|
+
/** Closes the MongoDB connection if the package opened it. */
|
|
113
|
+
close: () => Promise<void>;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Express router with the whole comments API. Mount it anywhere:
|
|
117
|
+
* app.use("/api/comments", await createCommentsRouter({ mongoUrl, googleClientId, sessionSecret }))
|
|
118
|
+
*
|
|
119
|
+
* GET /config public settings for the UI
|
|
120
|
+
* POST /auth/google {credential} exchange a Google sign-in for a session token
|
|
121
|
+
* GET /me the signed-in user
|
|
122
|
+
* GET /threads?slug=&sort=&offset=&limit=
|
|
123
|
+
* POST /comments {slug, content, parentId?}
|
|
124
|
+
* PATCH /comments/:id {content} author edits
|
|
125
|
+
* DELETE /comments/:id author or moderator
|
|
126
|
+
* POST /comments/:id/like | /unlike | /flag
|
|
127
|
+
* GET /moderation/queue?status=flagged|hidden|recent&offset=
|
|
128
|
+
* POST /moderation/comments/:id/approve | /hide | /unhide | /delete
|
|
129
|
+
* POST /moderation/users/:id/ban | /unban
|
|
130
|
+
*/
|
|
131
|
+
declare const createCommentsRouter: (options: CommentsServerOptions) => Promise<CommentsRouter>;
|
|
132
|
+
|
|
133
|
+
interface CommentsHandlerOptions extends CommentsServerOptions {
|
|
134
|
+
/** Public URL prefix of the API, e.g. "/api/comments" (the widget's `apiBaseUrl`). Default "/api/comments". */
|
|
135
|
+
basePath?: string;
|
|
136
|
+
/**
|
|
137
|
+
* Query parameter holding the API sub-path when a rewrite sends every request to one function, e.g.
|
|
138
|
+
* `/api/comments/:path*` → `/api/some-function?path=:path*`. Default "path".
|
|
139
|
+
*/
|
|
140
|
+
pathParam?: string;
|
|
141
|
+
/** Other query parameters added by your rewrite that the API should not see (e.g. ["action"]). */
|
|
142
|
+
stripParams?: string[];
|
|
143
|
+
}
|
|
144
|
+
type NodeHandler = (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* A ready-to-mount Node request handler for the whole comments API — Vercel functions, Next.js API routes
|
|
147
|
+
* (`export const config = { api: { bodyParser: false } }` optional), or plain `http.createServer`.
|
|
148
|
+
*
|
|
149
|
+
* // api/comments/[...path].ts on Vercel, or behind a rewrite to one shared function:
|
|
150
|
+
* const handler = createCommentsHandler({}); // settings from COMMENTS_* env vars
|
|
151
|
+
* export default handler;
|
|
152
|
+
*
|
|
153
|
+
* The router (and MongoDB connection) starts on the first request and is reused while the instance stays warm;
|
|
154
|
+
* if startup fails (e.g. missing settings) requests get a 503 and the next one retries.
|
|
155
|
+
*/
|
|
156
|
+
declare const createCommentsHandler: (options?: CommentsHandlerOptions) => NodeHandler & {
|
|
157
|
+
close: () => Promise<void>;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
interface CommentDoc {
|
|
161
|
+
_id: ObjectId;
|
|
162
|
+
slug: string;
|
|
163
|
+
parentId: ObjectId | null;
|
|
164
|
+
rootId: ObjectId;
|
|
165
|
+
depth: number;
|
|
166
|
+
content: string;
|
|
167
|
+
authorId: ObjectId;
|
|
168
|
+
authorName: string;
|
|
169
|
+
authorPicture: string | null;
|
|
170
|
+
replyToName: string | null;
|
|
171
|
+
likes: number;
|
|
172
|
+
likedBy: ObjectId[];
|
|
173
|
+
flags: number;
|
|
174
|
+
flaggedBy: ObjectId[];
|
|
175
|
+
hidden: boolean;
|
|
176
|
+
/** A moderator reviewed it; reports no longer auto-hide it. Missing on older documents = false. */
|
|
177
|
+
approved?: boolean;
|
|
178
|
+
/** Soft delete: kept as a placeholder while it has replies. Missing = false. */
|
|
179
|
+
deleted?: boolean;
|
|
180
|
+
deletedBy?: "author" | "moderator";
|
|
181
|
+
editedAt?: Date | null;
|
|
182
|
+
createdAt: Date;
|
|
183
|
+
updatedAt: Date;
|
|
184
|
+
}
|
|
185
|
+
interface UserDoc {
|
|
186
|
+
_id: ObjectId;
|
|
187
|
+
googleSub: string;
|
|
188
|
+
name: string;
|
|
189
|
+
picture: string | null;
|
|
190
|
+
createdAt: Date;
|
|
191
|
+
lastLoginAt: Date;
|
|
192
|
+
/** Set from `moderatorEmails` at each sign-in. */
|
|
193
|
+
isModerator?: boolean;
|
|
194
|
+
/** Banned users can read but not post, like, report or edit. */
|
|
195
|
+
banned?: boolean;
|
|
196
|
+
bannedAt?: Date | null;
|
|
197
|
+
}
|
|
198
|
+
interface Store {
|
|
199
|
+
comments: Collection<CommentDoc>;
|
|
200
|
+
users: Collection<UserDoc>;
|
|
201
|
+
db: Db;
|
|
202
|
+
/** Closes the connection only if this package opened it. */
|
|
203
|
+
close: () => Promise<void>;
|
|
204
|
+
}
|
|
205
|
+
/** Connects (or reuses `client`), returns typed collections and creates the indexes once. */
|
|
206
|
+
declare const openStore: (opts: ResolvedOptions, mongoUrl?: string, client?: MongoClient) => Promise<Store>;
|
|
207
|
+
|
|
208
|
+
interface ListOptions {
|
|
209
|
+
slug: unknown;
|
|
210
|
+
sort?: CommentSort;
|
|
211
|
+
offset?: number;
|
|
212
|
+
limit?: number;
|
|
213
|
+
viewerId?: string | null;
|
|
214
|
+
}
|
|
215
|
+
interface CreateInput {
|
|
216
|
+
slug: unknown;
|
|
217
|
+
content: unknown;
|
|
218
|
+
parentId?: unknown;
|
|
219
|
+
}
|
|
220
|
+
declare const createCommentsService: (store: Store, opts: ResolvedOptions) => {
|
|
221
|
+
listThreads: ({ slug: rawSlug, sort, offset, limit, viewerId }: ListOptions) => Promise<ThreadsPage>;
|
|
222
|
+
createComment: (input: CreateInput, author: SessionUser) => Promise<CommentDTO>;
|
|
223
|
+
editComment: (commentId: unknown, content: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
224
|
+
deleteComment: (commentId: unknown, user: SessionUser) => Promise<void>;
|
|
225
|
+
like: (commentId: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
226
|
+
unlike: (commentId: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
227
|
+
flag: (commentId: unknown, user: SessionUser) => Promise<CommentDTO>;
|
|
228
|
+
upsertGoogleUser: (identity: GoogleIdentity) => Promise<SessionUser>;
|
|
229
|
+
listModerationQueue: (user: SessionUser, status: ModerationStatus, offset?: number) => Promise<ModerationPage>;
|
|
230
|
+
moderateComment: (commentId: unknown, action: ModerationAction | "delete", user: SessionUser) => Promise<void>;
|
|
231
|
+
setUserBanned: (userId: unknown, banned: boolean, moderator: SessionUser) => Promise<void>;
|
|
232
|
+
};
|
|
233
|
+
type CommentsService = ReturnType<typeof createCommentsService>;
|
|
234
|
+
|
|
235
|
+
/** An error with an HTTP status and a stable machine-readable code; its message is safe to show users. */
|
|
236
|
+
declare class CommentsError extends Error {
|
|
237
|
+
readonly status: number;
|
|
238
|
+
readonly code: string;
|
|
239
|
+
/** Seconds until the client may retry (sent as Retry-After on 429s). */
|
|
240
|
+
readonly retryAfter?: number | undefined;
|
|
241
|
+
constructor(status: number, code: string, message: string,
|
|
242
|
+
/** Seconds until the client may retry (sent as Retry-After on 429s). */
|
|
243
|
+
retryAfter?: number | undefined);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export { type CommentDoc, CommentsError, type CommentsHandlerOptions, type CommentsRouter, type CommentsServerOptions, type CommentsService, type GoogleIdentity, type RateLimitOptions, type RateLimitRule, type RateLimitStore, type Store, type UserDoc, createCommentsHandler, createCommentsRouter, createCommentsService, createMemoryRateLimitStore, createMongoRateLimitStore, openStore, resolveOptions };
|