@codenokami/node-api-master 1.4.2 → 1.5.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/dist/AppError-ByGDb4IL.d.mts +20 -0
- package/dist/AppError-ByGDb4IL.d.ts +20 -0
- package/dist/chunk-B3SAPWGT.mjs +1 -0
- package/dist/chunk-ZJQTZFDU.js +1 -0
- package/dist/index.d.mts +5 -179
- package/dist/index.d.ts +5 -179
- package/dist/index.js +1 -411
- package/dist/index.mjs +1 -368
- package/dist/socket.d.mts +55 -0
- package/dist/socket.d.ts +55 -0
- package/dist/socket.js +1 -0
- package/dist/socket.mjs +1 -0
- package/dist/validation.d.mts +101 -0
- package/dist/validation.d.ts +101 -0
- package/dist/validation.js +1 -0
- package/dist/validation.mjs +1 -0
- package/package.json +15 -15
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom Error Class for API Errors
|
|
3
|
+
*/
|
|
4
|
+
class AppError extends Error {
|
|
5
|
+
constructor(message, statusCode) {
|
|
6
|
+
super(message);
|
|
7
|
+
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
// status က 4xx ဆိုရင် 'fail', 5xx ဆိုရင် 'error' လို့ သတ်မှတ်မယ်
|
|
10
|
+
this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
|
|
11
|
+
|
|
12
|
+
// ဒါက ကျွန်တော်တို့ကိုယ်တိုင် သတ်မှတ်လိုက်တဲ့ Error ဖြစ်ကြောင်း အမှတ်အသားပြုတာပါ
|
|
13
|
+
this.isOperational = true;
|
|
14
|
+
|
|
15
|
+
// Error ဘယ်နေရာမှာ ဖြစ်တယ်ဆိုတဲ့ stack trace ကို သိမ်းထားမယ်
|
|
16
|
+
Error.captureStackTrace(this, this.constructor);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { AppError as A };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom Error Class for API Errors
|
|
3
|
+
*/
|
|
4
|
+
class AppError extends Error {
|
|
5
|
+
constructor(message, statusCode) {
|
|
6
|
+
super(message);
|
|
7
|
+
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
// status က 4xx ဆိုရင် 'fail', 5xx ဆိုရင် 'error' လို့ သတ်မှတ်မယ်
|
|
10
|
+
this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
|
|
11
|
+
|
|
12
|
+
// ဒါက ကျွန်တော်တို့ကိုယ်တိုင် သတ်မှတ်လိုက်တဲ့ Error ဖြစ်ကြောင်း အမှတ်အသားပြုတာပါ
|
|
13
|
+
this.isOperational = true;
|
|
14
|
+
|
|
15
|
+
// Error ဘယ်နေရာမှာ ဖြစ်တယ်ဆိုတဲ့ stack trace ကို သိမ်းထားမယ်
|
|
16
|
+
Error.captureStackTrace(this, this.constructor);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { AppError as A };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t=class extends Error{constructor(s,r){super(s),this.statusCode=r,this.status=`${r}`.startsWith("4")?"fail":"error",this.isOperational=!0,Error.captureStackTrace(this,this.constructor)}},e=t;export{e as a};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var t=class extends Error{constructor(s,r){super(s),this.statusCode=r,this.status=`${r}`.startsWith("4")?"fail":"error",this.isOperational=!0,Error.captureStackTrace(this,this.constructor)}},e= exports.a =t;exports.a = e;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import Joi from 'joi';
|
|
2
|
-
export { default as Joi } from 'joi';
|
|
3
1
|
import mongoose from 'mongoose';
|
|
4
2
|
import jwt from 'jsonwebtoken';
|
|
3
|
+
import { A as AppError } from './AppError-ByGDb4IL.mjs';
|
|
5
4
|
import bcrypt from 'bcryptjs';
|
|
6
5
|
import crypto from 'crypto';
|
|
7
|
-
import { Server } from 'socket.io';
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* Database သို့ ချိတ်ဆက်ပေးသော function
|
|
@@ -27,25 +25,6 @@ const connectDB = async (url) => {
|
|
|
27
25
|
}
|
|
28
26
|
};
|
|
29
27
|
|
|
30
|
-
/**
|
|
31
|
-
* Custom Error Class for API Errors
|
|
32
|
-
*/
|
|
33
|
-
class AppError extends Error {
|
|
34
|
-
constructor(message, statusCode) {
|
|
35
|
-
super(message);
|
|
36
|
-
|
|
37
|
-
this.statusCode = statusCode;
|
|
38
|
-
// status က 4xx ဆိုရင် 'fail', 5xx ဆိုရင် 'error' လို့ သတ်မှတ်မယ်
|
|
39
|
-
this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
|
|
40
|
-
|
|
41
|
-
// ဒါက ကျွန်တော်တို့ကိုယ်တိုင် သတ်မှတ်လိုက်တဲ့ Error ဖြစ်ကြောင်း အမှတ်အသားပြုတာပါ
|
|
42
|
-
this.isOperational = true;
|
|
43
|
-
|
|
44
|
-
// Error ဘယ်နေရာမှာ ဖြစ်တယ်ဆိုတဲ့ stack trace ကို သိမ်းထားမယ်
|
|
45
|
-
Error.captureStackTrace(this, this.constructor);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
28
|
/**
|
|
50
29
|
* catchAsync - try-catch block တွေကို အစားထိုးရန်
|
|
51
30
|
* @param {Function} fn - Async controller function
|
|
@@ -61,7 +40,7 @@ const catchAsync = (fn) => {
|
|
|
61
40
|
* Auth Middleware - Supports Bearer Token & Cookies
|
|
62
41
|
* @param {string} secret - JWT Secret (Default is process.env.JWT_SECRET)
|
|
63
42
|
*/
|
|
64
|
-
const auth = (secret = process.env.JWT_SECRET) =>
|
|
43
|
+
const auth = (secret = process.env.JWT_SECRET, options = {}) =>
|
|
65
44
|
catchAsync(async (req, res, next) => {
|
|
66
45
|
let token;
|
|
67
46
|
|
|
@@ -73,8 +52,8 @@ const auth = (secret = process.env.JWT_SECRET) =>
|
|
|
73
52
|
token = req.headers.authorization.split(" ")[1];
|
|
74
53
|
}
|
|
75
54
|
// 2) Cookie ကနေ Token ကို စစ်ဆေးခြင်း
|
|
76
|
-
else if (req.cookies && req.cookies.jwt) {
|
|
77
|
-
token = req.cookies.jwt;
|
|
55
|
+
else if (req.cookies && req.cookies[options.cookieName || "jwt"]) {
|
|
56
|
+
token = req.cookies[options.cookieName || "jwt"];
|
|
78
57
|
}
|
|
79
58
|
|
|
80
59
|
// Secret မရှိရင် Developer ကို သတိပေးဖို့ Error ပြမယ်
|
|
@@ -118,32 +97,6 @@ const admin = (req, res, next) => {
|
|
|
118
97
|
}
|
|
119
98
|
};
|
|
120
99
|
|
|
121
|
-
/**
|
|
122
|
-
* Joi Schema Validation Middleware
|
|
123
|
-
* @param {Object} schema - Joi validation schema
|
|
124
|
-
*/
|
|
125
|
-
const validate = (schema) => (req, res, next) => {
|
|
126
|
-
// body, query, params အားလုံးကို စစ်လို့ရအောင် req object တစ်ခုလုံးကို schema နဲ့ တိုက်စစ်မယ်
|
|
127
|
-
const { error, value } = schema.validate(req.body, {
|
|
128
|
-
abortEarly: false, // Error အားလုံးကို တစ်ခါတည်း ပြရန်
|
|
129
|
-
allowUnknown: true, // Schema ထဲမပါတဲ့ field တွေပါလာရင် လက်ခံရန်
|
|
130
|
-
stripUnknown: true, // Schema ထဲမပါတဲ့ field တွေကို ဖယ်ထုတ်ပစ်ရန်
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (error) {
|
|
134
|
-
// Error message တွေကို စုစည်းပြီး format လုပ်မယ်
|
|
135
|
-
const errorMessage = error.details
|
|
136
|
-
.map((detail) => detail.message.replace(/"/g, ""))
|
|
137
|
-
.join(", ");
|
|
138
|
-
|
|
139
|
-
return next(new AppError(errorMessage, 400));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// စစ်ဆေးပြီးသား data (sanitized data) ကို req.body ထဲ ပြန်ထည့်မယ်
|
|
143
|
-
req.body = value;
|
|
144
|
-
next();
|
|
145
|
-
};
|
|
146
|
-
|
|
147
100
|
/**
|
|
148
101
|
* Standard HTTP Status Codes
|
|
149
102
|
*/
|
|
@@ -302,131 +255,4 @@ const generateUUID = () => {
|
|
|
302
255
|
);
|
|
303
256
|
};
|
|
304
257
|
|
|
305
|
-
|
|
306
|
-
* User Validation Schemas
|
|
307
|
-
*/
|
|
308
|
-
const userSchema = {
|
|
309
|
-
// Standard Register Schema
|
|
310
|
-
register: Joi.object({
|
|
311
|
-
username: Joi.string().alphanum().min(3).max(30).required().messages({
|
|
312
|
-
"string.min": "Username must be at least 3 characters long",
|
|
313
|
-
"any.required": "Username is a required field",
|
|
314
|
-
}),
|
|
315
|
-
email: Joi.string()
|
|
316
|
-
.email({ minDomainSegments: 2, tlds: { allow: ["com", "net", "org"] } })
|
|
317
|
-
.required()
|
|
318
|
-
.messages({
|
|
319
|
-
"string.email": "Please provide a valid email address",
|
|
320
|
-
}),
|
|
321
|
-
password: Joi.string().min(8).required().messages({
|
|
322
|
-
"string.min": "Password must be at least 8 characters long",
|
|
323
|
-
}),
|
|
324
|
-
confirmPassword: Joi.any()
|
|
325
|
-
.equal(Joi.ref("password"))
|
|
326
|
-
.required()
|
|
327
|
-
.messages({ "any.only": "Passwords do not match" }),
|
|
328
|
-
role: Joi.string().valid("user", "admin").default("user"),
|
|
329
|
-
}),
|
|
330
|
-
|
|
331
|
-
// Login Schema
|
|
332
|
-
login: Joi.object({
|
|
333
|
-
email: Joi.string().email().required(),
|
|
334
|
-
password: Joi.string().required(),
|
|
335
|
-
}),
|
|
336
|
-
|
|
337
|
-
// Password Update Schema
|
|
338
|
-
updatePassword: Joi.object({
|
|
339
|
-
currentPassword: Joi.string().required(),
|
|
340
|
-
newPassword: Joi.string().min(8).required(),
|
|
341
|
-
}),
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* စိတ်ကြိုက် schema အသစ်ဆောက်ချင်ရင် သုံးရန် (Dynamic Flexibility)
|
|
345
|
-
* @param {Object} schemaDefinition - Joi object definition
|
|
346
|
-
*/
|
|
347
|
-
custom: (schemaDefinition) => Joi.object(schemaDefinition),
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Common Joi Schemas
|
|
352
|
-
*/
|
|
353
|
-
const commonSchema = {
|
|
354
|
-
// MongoDB ObjectId စစ်ဆေးရန် (24 hex characters)
|
|
355
|
-
objectId: Joi.string()
|
|
356
|
-
.regex(/^[0-9a-fA-F]{24}$/)
|
|
357
|
-
.messages({
|
|
358
|
-
"string.pattern.base": "Invalid ID format. Must be a valid ObjectId.",
|
|
359
|
-
}),
|
|
360
|
-
|
|
361
|
-
// Pagination အတွက် (Query strings)
|
|
362
|
-
pagination: Joi.object({
|
|
363
|
-
page: Joi.number().integer().min(1).default(1),
|
|
364
|
-
limit: Joi.number().integer().min(1).max(100).default(10),
|
|
365
|
-
sort: Joi.string().optional(),
|
|
366
|
-
fields: Joi.string().optional(),
|
|
367
|
-
}),
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
const schemas = {
|
|
371
|
-
user: userSchema,
|
|
372
|
-
common: commonSchema,
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
let io;
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Socket Authentication Middleware
|
|
379
|
-
*/
|
|
380
|
-
const socketAuth = (secret) => {
|
|
381
|
-
return (socket, next) => {
|
|
382
|
-
const token =
|
|
383
|
-
socket.handshake.auth?.token || socket.handshake.headers?.token;
|
|
384
|
-
|
|
385
|
-
if (!token) {
|
|
386
|
-
return next(new Error("Authentication error: Token missing"));
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
try {
|
|
390
|
-
const decoded = jwt.verify(token, secret || process.env.JWT_SECRET);
|
|
391
|
-
socket.user = decoded; // socket ထဲမှာ user data သိမ်းထားမယ်
|
|
392
|
-
next();
|
|
393
|
-
} catch (err) {
|
|
394
|
-
next(new Error("Authentication error: Invalid token"));
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Socket Initialize လုပ်ခြင်း
|
|
401
|
-
*/
|
|
402
|
-
const initSocket = (server, options = {}) => {
|
|
403
|
-
io = new Server(server, {
|
|
404
|
-
cors: {
|
|
405
|
-
origin: options.origin || "*",
|
|
406
|
-
},
|
|
407
|
-
...options,
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
// JWT Middleware ကို အသုံးပြုခြင်း
|
|
411
|
-
if (options.authRequired) {
|
|
412
|
-
io.use(socketAuth(options.jwtSecret));
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
return io;
|
|
416
|
-
};
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* Initialize ပြီးသား IO object ကို ပြန်ယူခြင်း
|
|
420
|
-
*/
|
|
421
|
-
const getIO = () => {
|
|
422
|
-
if (!io) throw new Error("Socket.io not initialized!");
|
|
423
|
-
return io;
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
declare const index_getIO: typeof getIO;
|
|
427
|
-
declare const index_initSocket: typeof initSocket;
|
|
428
|
-
declare namespace index {
|
|
429
|
-
export { index_getIO as getIO, index_initSocket as initSocket };
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
export { AppError, STATUS_CODES, admin, apiResponse, auth, catchAsync, comparePassword, connectDB, errorMiddleware, generateToken, generateUUID, hashPassword, schemas, index as socket, validate };
|
|
258
|
+
export { AppError, STATUS_CODES, admin, apiResponse, auth, catchAsync, comparePassword, connectDB, errorMiddleware, generateToken, generateUUID, hashPassword };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import Joi from 'joi';
|
|
2
|
-
export { default as Joi } from 'joi';
|
|
3
1
|
import mongoose from 'mongoose';
|
|
4
2
|
import jwt from 'jsonwebtoken';
|
|
3
|
+
import { A as AppError } from './AppError-ByGDb4IL.js';
|
|
5
4
|
import bcrypt from 'bcryptjs';
|
|
6
5
|
import crypto from 'crypto';
|
|
7
|
-
import { Server } from 'socket.io';
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* Database သို့ ချိတ်ဆက်ပေးသော function
|
|
@@ -27,25 +25,6 @@ const connectDB = async (url) => {
|
|
|
27
25
|
}
|
|
28
26
|
};
|
|
29
27
|
|
|
30
|
-
/**
|
|
31
|
-
* Custom Error Class for API Errors
|
|
32
|
-
*/
|
|
33
|
-
class AppError extends Error {
|
|
34
|
-
constructor(message, statusCode) {
|
|
35
|
-
super(message);
|
|
36
|
-
|
|
37
|
-
this.statusCode = statusCode;
|
|
38
|
-
// status က 4xx ဆိုရင် 'fail', 5xx ဆိုရင် 'error' လို့ သတ်မှတ်မယ်
|
|
39
|
-
this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
|
|
40
|
-
|
|
41
|
-
// ဒါက ကျွန်တော်တို့ကိုယ်တိုင် သတ်မှတ်လိုက်တဲ့ Error ဖြစ်ကြောင်း အမှတ်အသားပြုတာပါ
|
|
42
|
-
this.isOperational = true;
|
|
43
|
-
|
|
44
|
-
// Error ဘယ်နေရာမှာ ဖြစ်တယ်ဆိုတဲ့ stack trace ကို သိမ်းထားမယ်
|
|
45
|
-
Error.captureStackTrace(this, this.constructor);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
28
|
/**
|
|
50
29
|
* catchAsync - try-catch block တွေကို အစားထိုးရန်
|
|
51
30
|
* @param {Function} fn - Async controller function
|
|
@@ -61,7 +40,7 @@ const catchAsync = (fn) => {
|
|
|
61
40
|
* Auth Middleware - Supports Bearer Token & Cookies
|
|
62
41
|
* @param {string} secret - JWT Secret (Default is process.env.JWT_SECRET)
|
|
63
42
|
*/
|
|
64
|
-
const auth = (secret = process.env.JWT_SECRET) =>
|
|
43
|
+
const auth = (secret = process.env.JWT_SECRET, options = {}) =>
|
|
65
44
|
catchAsync(async (req, res, next) => {
|
|
66
45
|
let token;
|
|
67
46
|
|
|
@@ -73,8 +52,8 @@ const auth = (secret = process.env.JWT_SECRET) =>
|
|
|
73
52
|
token = req.headers.authorization.split(" ")[1];
|
|
74
53
|
}
|
|
75
54
|
// 2) Cookie ကနေ Token ကို စစ်ဆေးခြင်း
|
|
76
|
-
else if (req.cookies && req.cookies.jwt) {
|
|
77
|
-
token = req.cookies.jwt;
|
|
55
|
+
else if (req.cookies && req.cookies[options.cookieName || "jwt"]) {
|
|
56
|
+
token = req.cookies[options.cookieName || "jwt"];
|
|
78
57
|
}
|
|
79
58
|
|
|
80
59
|
// Secret မရှိရင် Developer ကို သတိပေးဖို့ Error ပြမယ်
|
|
@@ -118,32 +97,6 @@ const admin = (req, res, next) => {
|
|
|
118
97
|
}
|
|
119
98
|
};
|
|
120
99
|
|
|
121
|
-
/**
|
|
122
|
-
* Joi Schema Validation Middleware
|
|
123
|
-
* @param {Object} schema - Joi validation schema
|
|
124
|
-
*/
|
|
125
|
-
const validate = (schema) => (req, res, next) => {
|
|
126
|
-
// body, query, params အားလုံးကို စစ်လို့ရအောင် req object တစ်ခုလုံးကို schema နဲ့ တိုက်စစ်မယ်
|
|
127
|
-
const { error, value } = schema.validate(req.body, {
|
|
128
|
-
abortEarly: false, // Error အားလုံးကို တစ်ခါတည်း ပြရန်
|
|
129
|
-
allowUnknown: true, // Schema ထဲမပါတဲ့ field တွေပါလာရင် လက်ခံရန်
|
|
130
|
-
stripUnknown: true, // Schema ထဲမပါတဲ့ field တွေကို ဖယ်ထုတ်ပစ်ရန်
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (error) {
|
|
134
|
-
// Error message တွေကို စုစည်းပြီး format လုပ်မယ်
|
|
135
|
-
const errorMessage = error.details
|
|
136
|
-
.map((detail) => detail.message.replace(/"/g, ""))
|
|
137
|
-
.join(", ");
|
|
138
|
-
|
|
139
|
-
return next(new AppError(errorMessage, 400));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// စစ်ဆေးပြီးသား data (sanitized data) ကို req.body ထဲ ပြန်ထည့်မယ်
|
|
143
|
-
req.body = value;
|
|
144
|
-
next();
|
|
145
|
-
};
|
|
146
|
-
|
|
147
100
|
/**
|
|
148
101
|
* Standard HTTP Status Codes
|
|
149
102
|
*/
|
|
@@ -302,131 +255,4 @@ const generateUUID = () => {
|
|
|
302
255
|
);
|
|
303
256
|
};
|
|
304
257
|
|
|
305
|
-
|
|
306
|
-
* User Validation Schemas
|
|
307
|
-
*/
|
|
308
|
-
const userSchema = {
|
|
309
|
-
// Standard Register Schema
|
|
310
|
-
register: Joi.object({
|
|
311
|
-
username: Joi.string().alphanum().min(3).max(30).required().messages({
|
|
312
|
-
"string.min": "Username must be at least 3 characters long",
|
|
313
|
-
"any.required": "Username is a required field",
|
|
314
|
-
}),
|
|
315
|
-
email: Joi.string()
|
|
316
|
-
.email({ minDomainSegments: 2, tlds: { allow: ["com", "net", "org"] } })
|
|
317
|
-
.required()
|
|
318
|
-
.messages({
|
|
319
|
-
"string.email": "Please provide a valid email address",
|
|
320
|
-
}),
|
|
321
|
-
password: Joi.string().min(8).required().messages({
|
|
322
|
-
"string.min": "Password must be at least 8 characters long",
|
|
323
|
-
}),
|
|
324
|
-
confirmPassword: Joi.any()
|
|
325
|
-
.equal(Joi.ref("password"))
|
|
326
|
-
.required()
|
|
327
|
-
.messages({ "any.only": "Passwords do not match" }),
|
|
328
|
-
role: Joi.string().valid("user", "admin").default("user"),
|
|
329
|
-
}),
|
|
330
|
-
|
|
331
|
-
// Login Schema
|
|
332
|
-
login: Joi.object({
|
|
333
|
-
email: Joi.string().email().required(),
|
|
334
|
-
password: Joi.string().required(),
|
|
335
|
-
}),
|
|
336
|
-
|
|
337
|
-
// Password Update Schema
|
|
338
|
-
updatePassword: Joi.object({
|
|
339
|
-
currentPassword: Joi.string().required(),
|
|
340
|
-
newPassword: Joi.string().min(8).required(),
|
|
341
|
-
}),
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* စိတ်ကြိုက် schema အသစ်ဆောက်ချင်ရင် သုံးရန် (Dynamic Flexibility)
|
|
345
|
-
* @param {Object} schemaDefinition - Joi object definition
|
|
346
|
-
*/
|
|
347
|
-
custom: (schemaDefinition) => Joi.object(schemaDefinition),
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Common Joi Schemas
|
|
352
|
-
*/
|
|
353
|
-
const commonSchema = {
|
|
354
|
-
// MongoDB ObjectId စစ်ဆေးရန် (24 hex characters)
|
|
355
|
-
objectId: Joi.string()
|
|
356
|
-
.regex(/^[0-9a-fA-F]{24}$/)
|
|
357
|
-
.messages({
|
|
358
|
-
"string.pattern.base": "Invalid ID format. Must be a valid ObjectId.",
|
|
359
|
-
}),
|
|
360
|
-
|
|
361
|
-
// Pagination အတွက် (Query strings)
|
|
362
|
-
pagination: Joi.object({
|
|
363
|
-
page: Joi.number().integer().min(1).default(1),
|
|
364
|
-
limit: Joi.number().integer().min(1).max(100).default(10),
|
|
365
|
-
sort: Joi.string().optional(),
|
|
366
|
-
fields: Joi.string().optional(),
|
|
367
|
-
}),
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
const schemas = {
|
|
371
|
-
user: userSchema,
|
|
372
|
-
common: commonSchema,
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
let io;
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Socket Authentication Middleware
|
|
379
|
-
*/
|
|
380
|
-
const socketAuth = (secret) => {
|
|
381
|
-
return (socket, next) => {
|
|
382
|
-
const token =
|
|
383
|
-
socket.handshake.auth?.token || socket.handshake.headers?.token;
|
|
384
|
-
|
|
385
|
-
if (!token) {
|
|
386
|
-
return next(new Error("Authentication error: Token missing"));
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
try {
|
|
390
|
-
const decoded = jwt.verify(token, secret || process.env.JWT_SECRET);
|
|
391
|
-
socket.user = decoded; // socket ထဲမှာ user data သိမ်းထားမယ်
|
|
392
|
-
next();
|
|
393
|
-
} catch (err) {
|
|
394
|
-
next(new Error("Authentication error: Invalid token"));
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Socket Initialize လုပ်ခြင်း
|
|
401
|
-
*/
|
|
402
|
-
const initSocket = (server, options = {}) => {
|
|
403
|
-
io = new Server(server, {
|
|
404
|
-
cors: {
|
|
405
|
-
origin: options.origin || "*",
|
|
406
|
-
},
|
|
407
|
-
...options,
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
// JWT Middleware ကို အသုံးပြုခြင်း
|
|
411
|
-
if (options.authRequired) {
|
|
412
|
-
io.use(socketAuth(options.jwtSecret));
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
return io;
|
|
416
|
-
};
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* Initialize ပြီးသား IO object ကို ပြန်ယူခြင်း
|
|
420
|
-
*/
|
|
421
|
-
const getIO = () => {
|
|
422
|
-
if (!io) throw new Error("Socket.io not initialized!");
|
|
423
|
-
return io;
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
declare const index_getIO: typeof getIO;
|
|
427
|
-
declare const index_initSocket: typeof initSocket;
|
|
428
|
-
declare namespace index {
|
|
429
|
-
export { index_getIO as getIO, index_initSocket as initSocket };
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
export { AppError, STATUS_CODES, admin, apiResponse, auth, catchAsync, comparePassword, connectDB, errorMiddleware, generateToken, generateUUID, hashPassword, schemas, index as socket, validate };
|
|
258
|
+
export { AppError, STATUS_CODES, admin, apiResponse, auth, catchAsync, comparePassword, connectDB, errorMiddleware, generateToken, generateUUID, hashPassword };
|