this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix type errors in queryLabels handler

futur 1c69e085 556938ee

+38 -14
+38 -14
src/LabelerServer.ts
··· 1 1 import type { 2 2 ComAtprotoLabelDefs, 3 + ComAtprotoLabelQueryLabels, 3 4 ToolsOzoneModerationDefs, 4 5 ToolsOzoneModerationEmitEvent, 5 6 } from "@atproto/api"; ··· 235 236 * Handler for com.atproto.label.queryLabels. 236 237 */ 237 238 queryLabelsHandler: QueryHandler< 238 - { uriPatterns?: Array<string>; sources?: Array<string>; limit?: string; cursor?: string } 239 + { 240 + uriPatterns?: string | Array<string>; 241 + sources?: string | Array<string>; 242 + limit?: string; 243 + cursor?: string; 244 + } 239 245 > = async (req, res) => { 240 246 try { 241 - const { 242 - uriPatterns = [], 243 - sources = [], 244 - limit: limitStr = "50", 245 - cursor: cursorStr = "0", 246 - } = req.query; 247 + let uriPatterns: Array<string>; 248 + if (!req.query.uriPatterns) { 249 + uriPatterns = []; 250 + } else if (typeof req.query.uriPatterns === "string") { 251 + uriPatterns = [req.query.uriPatterns]; 252 + } else { 253 + uriPatterns = req.query.uriPatterns || []; 254 + } 255 + 256 + let sources: Array<string>; 257 + if (!req.query.sources) { 258 + sources = []; 259 + } else if (typeof req.query.sources === "string") { 260 + sources = [req.query.sources]; 261 + } else { 262 + sources = req.query.sources || []; 263 + } 247 264 248 - const cursor = parseInt(cursorStr, 10); 265 + const cursor = parseInt(req.query.cursor || "0", 10); 249 266 if (cursor !== undefined && Number.isNaN(cursor)) { 250 267 throw new InvalidRequestError("Cursor must be an integer"); 251 268 } 252 269 253 - const limit = parseInt(limitStr, 10); 270 + const limit = parseInt(req.query.limit || "50", 10); 254 271 if (Number.isNaN(limit) || limit < 1 || limit > 250) { 255 272 throw new InvalidRequestError("Limit must be an integer between 1 and 250"); 256 273 } 257 274 258 275 const patterns = uriPatterns.includes("*") ? [] : uriPatterns.map((pattern) => { 259 - if (pattern.indexOf("*") !== pattern.length - 1) { 276 + pattern = pattern.replaceAll(/%/g, "").replaceAll(/_/g, "\\_"); 277 + 278 + const starIndex = pattern.indexOf("*"); 279 + if (starIndex === -1) return pattern; 280 + 281 + if (starIndex !== pattern.length - 1) { 260 282 throw new InvalidRequestError( 261 283 "Only trailing wildcards are supported in uriPatterns", 262 284 ); 263 285 } 264 - return pattern.replaceAll(/%/g, "").replaceAll(/_/g, "\\_").slice(0, -1) + "%"; 286 + return pattern.slice(0, -1) + "%"; 265 287 }); 266 288 267 - const stmt = this.db.prepare<unknown[], ComAtprotoLabelDefs.Label>(` 289 + const stmt = this.db.prepare<unknown[], SavedLabel>(` 268 290 SELECT * FROM labels 269 291 WHERE 1 = 1 270 292 ${patterns.length ? "AND " + patterns.map(() => "uri LIKE ?").join(" OR ") : ""} ··· 283 305 const rows = stmt.all(params); 284 306 const labels = rows.map(formatLabel); 285 307 286 - const nextCursor = rows[rows.length - 1]?.id ?? 0; 308 + const nextCursor = rows[rows.length - 1]?.id?.toString(10) || "0"; 287 309 288 - await res.send({ cursor: nextCursor, labels }); 310 + await res.send( 311 + { cursor: nextCursor, labels } satisfies ComAtprotoLabelQueryLabels.OutputSchema, 312 + ); 289 313 } catch (e) { 290 314 if (e instanceof XRPCError) { 291 315 await res.status(e.type).send(e.payload);