dolphin-server-modules 1.0.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/auth/auth.ts +685 -0
- package/controller/controller.ts +248 -0
- package/curd/crud.ts +267 -0
- package/dist/auth/auth.d.ts +81 -0
- package/dist/auth/auth.js +565 -0
- package/dist/auth/auth.js.map +1 -0
- package/dist/controller/controller.d.ts +36 -0
- package/dist/controller/controller.js +185 -0
- package/dist/controller/controller.js.map +1 -0
- package/dist/curd/crud.d.ts +71 -0
- package/dist/curd/crud.js +217 -0
- package/dist/curd/crud.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/zod.d.ts +20 -0
- package/dist/middleware/zod.js +76 -0
- package/dist/middleware/zod.js.map +1 -0
- package/index.ts +12 -0
- package/middleware/zod.ts +73 -0
- package/package.json +39 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Framework-agnostic validation utility.
|
|
5
|
+
* Validates any given data against a provided Zod schema.
|
|
6
|
+
*/
|
|
7
|
+
export const validateStructure = <T>(schema: z.ZodSchema<T>, data: unknown): T => {
|
|
8
|
+
try {
|
|
9
|
+
return schema.parse(data);
|
|
10
|
+
} catch (err: any) {
|
|
11
|
+
if (err instanceof ZodError) {
|
|
12
|
+
const zodErr = err as ZodError<any>;
|
|
13
|
+
throw {
|
|
14
|
+
status: 400,
|
|
15
|
+
message: 'Validation Error',
|
|
16
|
+
errors: zodErr.issues.map((e: z.ZodIssue) => ({ field: e.path.join('.'), message: e.message }))
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
throw err;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Express / Next.js Pages API Middleware
|
|
25
|
+
* Validates request body, query, and params.
|
|
26
|
+
*/
|
|
27
|
+
export const validatePagesRequest = (schemas: {
|
|
28
|
+
body?: z.ZodSchema<any>;
|
|
29
|
+
query?: z.ZodSchema<any>;
|
|
30
|
+
params?: z.ZodSchema<any>;
|
|
31
|
+
}) => {
|
|
32
|
+
return (req: any, res: any, next: Function) => {
|
|
33
|
+
try {
|
|
34
|
+
if (schemas.body) req.body = validateStructure(schemas.body, req.body);
|
|
35
|
+
if (schemas.query) req.query = validateStructure(schemas.query, req.query);
|
|
36
|
+
if (schemas.params) req.params = validateStructure(schemas.params, req.params);
|
|
37
|
+
next();
|
|
38
|
+
} catch (err: any) {
|
|
39
|
+
return res.status(err.status || 400).json(err);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* App Router Route Handler Wrapper
|
|
46
|
+
* Used to wrap a standard Route Handler function to ensure request validation.
|
|
47
|
+
*/
|
|
48
|
+
export const validateAppRoute = (
|
|
49
|
+
schema: z.ZodSchema<any>,
|
|
50
|
+
handler: (req: Request, validatedData: any, ...args: any[]) => Promise<Response> | Response
|
|
51
|
+
) => {
|
|
52
|
+
return async (req: Request, ...args: any[]) => {
|
|
53
|
+
try {
|
|
54
|
+
// Typically we validate the body for App Router POST/PUT requests
|
|
55
|
+
let body;
|
|
56
|
+
if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH') {
|
|
57
|
+
body = await req.json().catch(() => ({}));
|
|
58
|
+
} else {
|
|
59
|
+
const url = new URL(req.url);
|
|
60
|
+
body = Object.fromEntries(url.searchParams.entries());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const validatedData = validateStructure(schema, body);
|
|
64
|
+
return handler(req, validatedData, ...args);
|
|
65
|
+
} catch (err: any) {
|
|
66
|
+
const status = err.status || 400;
|
|
67
|
+
return new Response(JSON.stringify(err), {
|
|
68
|
+
status,
|
|
69
|
+
headers: { 'Content-Type': 'application/json' }
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dolphin-server-modules",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Core utility modules for Auth, CRUD, and Controllers",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/index.js",
|
|
9
|
+
"./auth": "./dist/auth/auth.js",
|
|
10
|
+
"./controller": "./dist/controller/controller.js",
|
|
11
|
+
"./crud": "./dist/curd/crud.js",
|
|
12
|
+
"./middleware/zod": "./dist/middleware/zod.js"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"test": "jest",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"dolphin",
|
|
21
|
+
"server",
|
|
22
|
+
"auth",
|
|
23
|
+
"crud",
|
|
24
|
+
"controller"
|
|
25
|
+
],
|
|
26
|
+
"author": "",
|
|
27
|
+
"license": "ISC",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"argon2": "^0.40.1",
|
|
30
|
+
"zod": "^4.3.6"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/jest": "^29.5.12",
|
|
34
|
+
"@types/node": "^20.11.30",
|
|
35
|
+
"jest": "^29.7.0",
|
|
36
|
+
"ts-jest": "^29.1.2",
|
|
37
|
+
"typescript": "^5.4.3"
|
|
38
|
+
}
|
|
39
|
+
}
|