speedly 1.2.46 → 1.2.48

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.
@@ -15,23 +15,13 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
35
25
  Object.defineProperty(exports, "__esModule", { value: true });
36
26
  exports.default = default_1;
37
27
  const yup = __importStar(require("yup"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "speedly",
3
- "version": "1.2.46",
3
+ "version": "1.2.48",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -1,18 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
3
- type UseAuth = (req: Request, res: Response, next: NextFunction) => UseAuth | {
4
- user?: (...handlers: Handler[]) => unknown;
5
- admin?: (...handlers: [{
6
- permission: string;
7
- }, ...Handler[]] | Handler[]) => unknown;
8
- any?: (...handlers: Handler[]) => unknown;
9
- };
10
- declare const auth: (config?: {
11
- admin: {
12
- role: string;
13
- model: string;
14
- };
15
- jwtSecretEnv: string;
16
- customValidator: (req: Request, key: string) => boolean;
17
- }) => UseAuth;
18
- export default auth;
@@ -1,93 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const getConfig_1 = __importDefault(require("../util/getConfig"));
7
- const gConfig = {
8
- admin: { role: "ADMIN", model: "../models/admin" },
9
- jwtSecretEnv: "JWT_KEY",
10
- customValidator: (req, key) => {
11
- return true;
12
- },
13
- ...(0, getConfig_1.default)("auth"),
14
- };
15
- const holders = {};
16
- const auth = (config = gConfig) => {
17
- // const adminModel = require('../models/admin')
18
- let handlerState = {};
19
- let useAuth = async (req, res, next) => {
20
- try {
21
- const nextFunc = (handlers, index = 0) => (errorMessage = "") => {
22
- if (errorMessage)
23
- return next(errorMessage);
24
- if (!handlers.length || !handlers[index + 1])
25
- return next();
26
- handlers[index + 1](req, res, nextFunc(handlers, index + 1));
27
- };
28
- const keys = Object.keys(handlerState);
29
- for (let i = 0; i < keys.length; i++) {
30
- const key = keys[i];
31
- if (!handlerState[key]?.handlers?.length)
32
- continue;
33
- if (await gConfig?.customValidator?.(req, key)) {
34
- return await handlerState[key].handlers[0](req, res, nextFunc(handlerState[key].handlers));
35
- }
36
- else if ((await gConfig?.customValidator?.(req, key)) == null) {
37
- return next({ status: 401, json: { message: "unauthorized" } });
38
- }
39
- else if (i === keys.length - 1) {
40
- next({
41
- status: 405,
42
- json: { message: "you don't have access to this section" },
43
- });
44
- }
45
- else
46
- continue;
47
- }
48
- }
49
- catch (error) {
50
- console.log("auth", 42, error);
51
- next({
52
- status: 403,
53
- json: {
54
- message: error instanceof Error
55
- ? error.message
56
- : "error on authentication please login again",
57
- },
58
- });
59
- }
60
- };
61
- holders.admin = (...handlers) => {
62
- if (!Array.isArray(handlers))
63
- throw new Error("handlers must be an array");
64
- const hasConfig = typeof handlers[0] === "object" && "permission" in handlers[0];
65
- const configObj = hasConfig
66
- ? handlers[0]
67
- : undefined;
68
- const handlerFns = hasConfig
69
- ? handlers.slice(1)
70
- : handlers;
71
- handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ""}`] = {
72
- ...(configObj ? { config: configObj } : {}),
73
- handlers: handlerFns,
74
- };
75
- return useAuth;
76
- };
77
- holders.user = (...handlers) => {
78
- if (!Array.isArray(handlers))
79
- throw new Error("handlers must be an array");
80
- handlerState.user = { handlers };
81
- return useAuth;
82
- };
83
- holders.any = (...handlers) => {
84
- if (!Array.isArray(handlers))
85
- throw new Error("handlers must be an array");
86
- handlerState.any = { handlers };
87
- return useAuth;
88
- };
89
- useAuth = Object.assign(useAuth, holders);
90
- return useAuth;
91
- };
92
- console.log("auth", 81, typeof auth);
93
- exports.default = auth;
@@ -1,2 +0,0 @@
1
- import yup from "yup";
2
- export default yup;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const mongoose_1 = require("mongoose");
7
- const yup_1 = __importDefault(require("yup"));
8
- yup_1.default.addMethod(yup_1.default.string, "oid", function (errorMessage = "آیدی درست نیست") {
9
- return this.test({
10
- name: "oid",
11
- message: errorMessage,
12
- test(value) {
13
- if (!value)
14
- return true;
15
- if (value == "$$REMOVE")
16
- return true;
17
- if (!value || !(0, mongoose_1.isValidObjectId)(value)) {
18
- return false; // Invalid ObjectId
19
- }
20
- return true; // Valid ObjectId
21
- },
22
- });
23
- });
24
- exports.default = yup_1.default;