Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .env.example

This file was deleted.

9 changes: 9 additions & 0 deletions functions/_shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// <reference types="@cloudflare/workers-types" />

import type { AuthenticatedUser } from "./utils"

export interface Env {
DB: D1Database
CACHE: KVNamespace
Expand All @@ -22,3 +24,10 @@ export interface EventContext<E = unknown> {
next(input?: Request | string, init?: RequestInit): Promise<Response>
data: Record<string, unknown>
}
export type AuthResult = {
success: true
user: AuthenticatedUser
} | {
success: false
response: Response
}
69 changes: 69 additions & 0 deletions functions/_shared/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Centralized validation schemas
import { z } from 'zod'

// Base schemas
export const IdParamSchema = z.string().regex(/^\d+$/, 'Invalid ID format').transform(Number)
export const SlugSchema = z.string().min(1, 'Slug is required').max(100, 'Slug too long')
export const PaginationSchema = z.object({
limit: z.string().regex(/^\d+$/).transform(Number).pipe(z.number().min(1).max(100)),
offset: z.string().regex(/^\d+$/).transform(Number).pipe(z.number().min(0))
})

// Petition schemas
export const CreatePetitionSchema = z.object({
title: z.string().min(1, 'Title is required').max(200, 'Title too long'),
description: z.string().min(10, 'Description must be at least 10 characters').max(5000, 'Description too long'),
type: z.enum(['local', 'national'], { errorMap: () => ({ message: 'Type must be "local" or "national"' }) }),
image_url: z.string().url().optional(),
target_count: z.number().min(1, 'Target count must be at least 1').max(1000000, 'Target count too high').optional(),
location: z.string().max(100, 'Location too long').optional(),
due_date: z.string().datetime().optional(),
category_ids: z.array(z.number()).max(5, 'Maximum 5 categories allowed').optional()
})

export const UpdatePetitionSchema = CreatePetitionSchema.partial()

// Signature schemas
export const CreateSignatureSchema = z.object({
petition_id: z.number().positive('Invalid petition ID'),
comment: z.string().max(1000, 'Comment too long').optional(),
anonymous: z.boolean().optional()
})

// Report schemas
export const CreateReportSchema = z.object({
reported_item_type: z.enum(['petition', 'signature'], {
errorMap: () => ({ message: 'Reported item type must be "petition" or "signature"' })
}),
reported_item_id: z.number().positive('Invalid reported item ID'),
report_reason: z.enum([
'spam', 'inappropriate_content', 'harassment', 'misinformation',
'hate_speech', 'violence', 'copyright_violation', 'other'
], { errorMap: () => ({ message: 'Invalid report reason' }) }),
report_description: z.string().max(1000, 'Description too long').optional()
})

export const UpdateReportSchema = z.object({
status: z.enum(['pending', 'reviewed', 'resolved', 'dismissed']).optional(),
admin_notes: z.string().max(1000, 'Admin notes too long').optional()
})

// Category schemas
export const CreateCategorySchema = z.object({
name: z.string().min(1, 'Category name is required').max(50, 'Category name too long'),
description: z.string().max(200, 'Description too long').optional()
})

// Query parameter schemas
export const PetitionQuerySchema = z.object({
type: z.enum(['local', 'national']).optional(),
limit: z.string().regex(/^\d+$/).transform(Number).pipe(z.number().min(1).max(100)).optional(),
offset: z.string().regex(/^\d+$/).transform(Number).pipe(z.number().min(0)).optional(),
userId: z.string().optional()
})

export const ReportQuerySchema = z.object({
status: z.enum(['pending', 'reviewed', 'resolved', 'dismissed']).optional(),
limit: z.string().regex(/^\d+$/).transform(Number).pipe(z.number().min(1).max(100)).optional(),
offset: z.string().regex(/^\d+$/).transform(Number).pipe(z.number().min(0)).optional()
})
18 changes: 18 additions & 0 deletions functions/helpers/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Env, EventContext } from '../_shared/types'

export function createContext(
request: Request,
env: Env,
params: Record<string, string> = {}
): EventContext<Env> {
return {
request,
env,
params,
waitUntil: () => {},
next: async () => new Response('Not implemented', { status: 501 }),
data: {},
}
}


57 changes: 57 additions & 0 deletions functions/helpers/wrappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { Env, EventContext } from '../_shared/types'
import { createContext } from './context'
import { requireAuth, requirePetitionOwnership } from '../middleware/auth-middleware'

export function wrapHandler(handler: (context: EventContext<Env>) => Promise<Response>) {
return async (request: Request, env: Env) => {
const context = createContext(request, env, request.params || {})
return await handler(context)
}
}

export function wrapHandlerWithAuth(handler: (context: EventContext<Env>) => Promise<Response>) {
return async (request: Request, env: Env) => {
const context = createContext(request, env, request.params || {})
const authResult = await requireAuth(context)
if (!authResult.success) {
return authResult.response
}
context.data.user = authResult.user
return await handler(context)
}
}

export function wrapHandlerWithConditionalAuth(handler: (context: EventContext<Env>) => Promise<Response>) {
return async (request: Request, env: Env) => {
const url = new URL(request.url)
const userId = url.searchParams.get('userId')

const context = createContext(request, env, request.params || {})

if (userId) {
const authResult = await requireAuth(context)
if (!authResult.success) {
return authResult.response
}
context.data.user = authResult.user
return await handler(context)
}

return await handler(context)
}
}

export function wrapHandlerWithOwnership(handler: (context: EventContext<Env>) => Promise<Response>) {
return async (request: Request, env: Env) => {
const petitionId = parseInt(request.params?.id || '0')
const context = createContext(request, env, request.params || {})
const authResult = await requirePetitionOwnership(context, petitionId)
if (!authResult.success) {
return authResult.response
}
context.data.user = authResult.user
return await handler(context)
}
}


Loading