speedly 1.2.12 → 1.2.13

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.
@@ -1,4 +1,14 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
+ type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
3
+ type AdminArgs = [{
4
+ permission: string;
5
+ }, ...Handler[]] | Handler[];
6
+ interface UseAuth {
7
+ (req: Request, res: Response, next: NextFunction): any;
8
+ user?: (...handlers: Handler[]) => UseAuth;
9
+ admin?: (...handlers: AdminArgs) => UseAuth;
10
+ any?: (...handlers: Handler[]) => UseAuth;
11
+ }
2
12
  declare const auth: (config?: {
3
13
  admin: {
4
14
  role: string;
@@ -6,5 +16,5 @@ declare const auth: (config?: {
6
16
  };
7
17
  jwtSecretEnv: string;
8
18
  customValidator: (req: Request, key: string) => boolean;
9
- }) => (req: Request, res: Response, next: NextFunction) => Promise<unknown>;
19
+ }) => UseAuth;
10
20
  export default auth;
@@ -9,7 +9,7 @@ const holders = {};
9
9
  const auth = (config = gConfig) => {
10
10
  // const adminModel = require('../models/admin')
11
11
  let handlerState = {};
12
- const useAuth = async (req, res, next) => {
12
+ let useAuth = async (req, res, next) => {
13
13
  try {
14
14
  const nextFunc = (handlers, index = 0) => (errorMessage = '') => {
15
15
  if (errorMessage)
@@ -35,23 +35,6 @@ const auth = (config = gConfig) => {
35
35
  else
36
36
  continue;
37
37
  }
38
- // اینجا اگر i === keys.length - 1 باشد یعنی آخرین آیتم هستی
39
- // }else if (!req.cookies.AT_SECRET) {
40
- // if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(403).json({ message: 'you dont have access for this section' })
41
- // handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
42
- // }else {
43
- // if(!handlerState.admin || !handlerState.admin?.handlers?.length) {
44
- // if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(404).json({ message: 'route not found :(' })
45
- // handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
46
- // } else {
47
- // const tokenPayload = jwt.verify(req.cookies.AT_SECRET , process.env[gConfig.jwtSecretEnv])
48
- // const adminDoc = await adminModel.findById(tokenPayload.id)
49
- // if(!adminDoc)return res.status(403).json({ message: 'you don\'t have access for this section' })
50
- // if(adminDoc.role !='OWNER' && adminDoc.role != config?.admin?.role ) return res.status(403).json({ message: 'you dont have access for this section' })
51
- // req.admin = adminDoc
52
- // handlerState.admin.handlers[0](req , res , nextFunc(handlerState.admin.handlers))
53
- // }
54
- // }
55
38
  }
56
39
  catch (error) {
57
40
  console.log('auth', 42, error);
@@ -82,7 +65,8 @@ const auth = (config = gConfig) => {
82
65
  handlerState.any = { handlers };
83
66
  return useAuth;
84
67
  };
85
- Object.assign(useAuth, holders);
68
+ useAuth = Object.assign(useAuth, holders);
86
69
  return useAuth;
87
70
  };
71
+ console.log('auth', 81, typeof auth);
88
72
  exports.default = auth;
@@ -1,4 +1,14 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
+ type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
3
+ type AdminArgs = [{
4
+ permission: string;
5
+ }, ...Handler[]] | Handler[];
6
+ interface UseAuth {
7
+ (req: Request, res: Response, next: NextFunction): any;
8
+ user?: (...handlers: Handler[]) => UseAuth;
9
+ admin?: (...handlers: AdminArgs) => UseAuth;
10
+ any?: (...handlers: Handler[]) => UseAuth;
11
+ }
2
12
  declare const auth: (config?: {
3
13
  admin: {
4
14
  role: string;
@@ -6,5 +16,5 @@ declare const auth: (config?: {
6
16
  };
7
17
  jwtSecretEnv: string;
8
18
  customValidator: (req: Request, key: string) => boolean;
9
- }) => (req: Request, res: Response, next: NextFunction) => Promise<unknown>;
19
+ }) => UseAuth;
10
20
  export default auth;
@@ -9,7 +9,7 @@ const holders = {};
9
9
  const auth = (config = gConfig) => {
10
10
  // const adminModel = require('../models/admin')
11
11
  let handlerState = {};
12
- const useAuth = async (req, res, next) => {
12
+ let useAuth = async (req, res, next) => {
13
13
  try {
14
14
  const nextFunc = (handlers, index = 0) => (errorMessage = '') => {
15
15
  if (errorMessage)
@@ -35,23 +35,6 @@ const auth = (config = gConfig) => {
35
35
  else
36
36
  continue;
37
37
  }
38
- // اینجا اگر i === keys.length - 1 باشد یعنی آخرین آیتم هستی
39
- // }else if (!req.cookies.AT_SECRET) {
40
- // if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(403).json({ message: 'you dont have access for this section' })
41
- // handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
42
- // }else {
43
- // if(!handlerState.admin || !handlerState.admin?.handlers?.length) {
44
- // if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(404).json({ message: 'route not found :(' })
45
- // handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
46
- // } else {
47
- // const tokenPayload = jwt.verify(req.cookies.AT_SECRET , process.env[gConfig.jwtSecretEnv])
48
- // const adminDoc = await adminModel.findById(tokenPayload.id)
49
- // if(!adminDoc)return res.status(403).json({ message: 'you don\'t have access for this section' })
50
- // if(adminDoc.role !='OWNER' && adminDoc.role != config?.admin?.role ) return res.status(403).json({ message: 'you dont have access for this section' })
51
- // req.admin = adminDoc
52
- // handlerState.admin.handlers[0](req , res , nextFunc(handlerState.admin.handlers))
53
- // }
54
- // }
55
38
  }
56
39
  catch (error) {
57
40
  console.log('auth', 42, error);
@@ -82,7 +65,8 @@ const auth = (config = gConfig) => {
82
65
  handlerState.any = { handlers };
83
66
  return useAuth;
84
67
  };
85
- Object.assign(useAuth, holders);
68
+ useAuth = Object.assign(useAuth, holders);
86
69
  return useAuth;
87
70
  };
71
+ console.log('auth', 81, typeof auth);
88
72
  exports.default = auth;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "speedly",
3
- "version": "1.2.12",
3
+ "version": "1.2.13",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/esm/index.d.ts",