signing petition through fetch request instead of direct db connection
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
"dotenv": "^17.2.3",
|
||||
"drizzle-orm": "^0.44.7",
|
||||
"pg": "^8.16.3",
|
||||
"zod": "^4.1.12"
|
||||
"zod": "^4.1.12",
|
||||
"types": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import { z } from "zod"
|
||||
import type z from "zod";
|
||||
import { insertSignature } from "../models";
|
||||
|
||||
const signPetitionSchema = z.object({
|
||||
email: z.string(),
|
||||
name: z.string().trim().min(1).max(30).optional(),
|
||||
comment: z.string().trim().min(10).max(10_000).optional(),
|
||||
})
|
||||
import { signedPetitionSchema, signPetitionSchema } from 'types'
|
||||
|
||||
export const signPetition = async (req: Request): Promise<Response> => {
|
||||
const body = await req.json()
|
||||
@@ -15,15 +10,17 @@ export const signPetition = async (req: Request): Promise<Response> => {
|
||||
return Response.json({ error: validatedBody.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const insertedSignature = await insertSignature({
|
||||
const _insertedSignature = await insertSignature({
|
||||
email: validatedBody.data.email,
|
||||
name: validatedBody.data.name,
|
||||
comment: validatedBody.data.comment,
|
||||
})
|
||||
|
||||
if (!insertedSignature) {
|
||||
if (!_insertedSignature) {
|
||||
return Response.json({ error: "inserting signature in database" }, { status: 500 });
|
||||
}
|
||||
|
||||
const insertedSignature = _insertedSignature satisfies z.infer<typeof signedPetitionSchema>
|
||||
|
||||
return Response.json(insertedSignature, { status: 200 });
|
||||
}
|
||||
|
||||
@@ -60,7 +60,8 @@
|
||||
"tailwind-merge": "^2.6.0",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
"vaul": "^0.9.9",
|
||||
"zod": "^3.25.76"
|
||||
"zod": "^4.1.12",
|
||||
"types": "workspace:types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.32.0",
|
||||
|
||||
15
packages/frontend/src/network/index.ts
Normal file
15
packages/frontend/src/network/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { z } from "zod";
|
||||
import { signedPetitionSchema, signPetitionSchema } from 'types';
|
||||
|
||||
const baseURL = import.meta.env.BASE_URL;
|
||||
|
||||
export const signSignature = async (signature: z.infer<typeof signPetitionSchema>): Promise<z.infer<typeof signedPetitionSchema>> => {
|
||||
const res = await fetch(baseURL, {
|
||||
method: 'POST', body: JSON.stringify(signature),
|
||||
})
|
||||
|
||||
const body = await res.json();
|
||||
const validatedBody = signedPetitionSchema.parse(body);
|
||||
|
||||
return validatedBody
|
||||
}
|
||||
0
packages/frontend/src/state/index.ts
Normal file
0
packages/frontend/src/state/index.ts
Normal file
34
packages/types/.gitignore
vendored
Normal file
34
packages/types/.gitignore
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
# dependencies (bun install)
|
||||
node_modules
|
||||
|
||||
# output
|
||||
out
|
||||
dist
|
||||
*.tgz
|
||||
|
||||
# code coverage
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# logs
|
||||
logs
|
||||
_.log
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# caches
|
||||
.eslintcache
|
||||
.cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
11
packages/types/index.ts
Normal file
11
packages/types/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import z from "zod";
|
||||
|
||||
export const signPetitionSchema = z.object({
|
||||
email: z.string(),
|
||||
name: z.string().trim().min(1).max(30).nullable(),
|
||||
comment: z.string().trim().min(10).max(10_000).nullable(),
|
||||
})
|
||||
|
||||
export const signedPetitionSchema = signPetitionSchema.extend({
|
||||
id: z.uuid(),
|
||||
})
|
||||
15
packages/types/package.json
Normal file
15
packages/types/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "types",
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5"
|
||||
},
|
||||
"dependencies": {
|
||||
"zod": "^4.1.12"
|
||||
}
|
||||
}
|
||||
29
packages/types/tsconfig.json
Normal file
29
packages/types/tsconfig.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
// Environment setup & latest features
|
||||
"lib": ["ESNext"],
|
||||
"target": "ESNext",
|
||||
"module": "Preserve",
|
||||
"moduleDetection": "force",
|
||||
"jsx": "react-jsx",
|
||||
"allowJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noImplicitOverride": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user