@vasrefil/api-toolkit 1.0.3 → 1.0.6

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.
Files changed (53) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +6 -0
  3. package/dist/out-tsc/api-response/user.response.d.ts +48 -0
  4. package/dist/out-tsc/api-response/user.response.js +33 -0
  5. package/dist/out-tsc/app-middlewares/airbrake.d.ts +10 -0
  6. package/dist/out-tsc/app-middlewares/airbrake.js +58 -0
  7. package/dist/out-tsc/controllers/_root.control.d.ts +53 -0
  8. package/dist/out-tsc/controllers/_root.control.js +127 -0
  9. package/dist/out-tsc/controllers/index.d.ts +1 -0
  10. package/dist/out-tsc/controllers/index.js +17 -0
  11. package/dist/out-tsc/controllers/sample.control.d.ts +6 -0
  12. package/dist/out-tsc/controllers/sample.control.js +13 -0
  13. package/dist/out-tsc/env.d.ts +12 -0
  14. package/dist/out-tsc/env.js +16 -0
  15. package/dist/out-tsc/helpers/currency-formatter.helper.d.ts +1 -0
  16. package/dist/out-tsc/helpers/currency-formatter.helper.js +11 -0
  17. package/dist/out-tsc/helpers/query.helper.d.ts +22 -0
  18. package/dist/out-tsc/helpers/query.helper.js +133 -0
  19. package/dist/out-tsc/index.d.ts +1 -0
  20. package/dist/out-tsc/index.js +41 -0
  21. package/dist/out-tsc/interfaces/interface.d.ts +27 -0
  22. package/dist/out-tsc/interfaces/interface.js +2 -0
  23. package/dist/out-tsc/interfaces/root-controller.interface.d.ts +21 -0
  24. package/dist/out-tsc/interfaces/root-controller.interface.js +2 -0
  25. package/dist/out-tsc/interfaces/status.interface.d.ts +13 -0
  26. package/dist/out-tsc/interfaces/status.interface.js +17 -0
  27. package/dist/out-tsc/middlewares/auth.midware.d.ts +8 -0
  28. package/dist/out-tsc/middlewares/auth.midware.js +52 -0
  29. package/dist/out-tsc/middlewares/validator.midware.d.ts +12 -0
  30. package/dist/out-tsc/middlewares/validator.midware.js +32 -0
  31. package/dist/out-tsc/models/_config.d.ts +1 -0
  32. package/dist/out-tsc/models/_config.js +27 -0
  33. package/dist/out-tsc/models/sample.model.d.ts +33 -0
  34. package/dist/out-tsc/models/sample.model.js +14 -0
  35. package/dist/out-tsc/public-api.d.ts +1 -0
  36. package/dist/out-tsc/public-api.js +17 -0
  37. package/dist/out-tsc/routes/index.route.d.ts +9 -0
  38. package/dist/out-tsc/routes/index.route.js +54 -0
  39. package/dist/out-tsc/routes/sample.route.d.ts +16 -0
  40. package/dist/out-tsc/routes/sample.route.js +53 -0
  41. package/dist/out-tsc/services/_root.service.d.ts +12 -0
  42. package/dist/out-tsc/services/_root.service.js +75 -0
  43. package/dist/out-tsc/services/sample.service.d.ts +16 -0
  44. package/dist/out-tsc/services/sample.service.js +127 -0
  45. package/dist/out-tsc/utilities/date.util.d.ts +42 -0
  46. package/dist/out-tsc/utilities/date.util.js +93 -0
  47. package/dist/out-tsc/utilities/logger.util.d.ts +4 -0
  48. package/dist/out-tsc/utilities/logger.util.js +48 -0
  49. package/dist/out-tsc/utilities/token.util.d.ts +6 -0
  50. package/dist/out-tsc/utilities/token.util.js +48 -0
  51. package/dist/out-tsc/validations/sample.validator.d.ts +6 -0
  52. package/dist/out-tsc/validations/sample.validator.js +35 -0
  53. package/package.json +2 -2
@@ -0,0 +1,127 @@
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 sample_control_1 = __importDefault(require("../controllers/sample.control"));
7
+ const _root_service_1 = require("./_root.service");
8
+ const status_interface_1 = require("../interfaces/status.interface");
9
+ const { SUCCESS, ERROR, UNPROCESSABLE_ENTRY, PRECONDITION_FAILED, SUCCESS_NO_CONTENT } = status_interface_1.Status;
10
+ class SampleService extends _root_service_1.RootService {
11
+ constructor() {
12
+ super(...arguments);
13
+ this.create = async (req, res) => {
14
+ const actionType = 'CREATE_SAMPLE';
15
+ try {
16
+ const sample = await sample_control_1.default.create(req.body);
17
+ this.sendResponse({ req, res, status: SUCCESS, data: sample, actionType });
18
+ }
19
+ catch (error) {
20
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
21
+ }
22
+ };
23
+ this.getAll = async (req, res) => {
24
+ const actionType = 'GET_ALL_SAMPLES';
25
+ try {
26
+ const samples = await sample_control_1.default.fetchAll({}, req.query);
27
+ this.sendResponse({ req, res, status: SUCCESS, actionType, data: samples });
28
+ }
29
+ catch (error) {
30
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
31
+ }
32
+ };
33
+ this.getOne = async (req, res) => {
34
+ const actionType = 'GET_ONE_SAMPLE';
35
+ try {
36
+ const sample = await sample_control_1.default.getOne(req.query);
37
+ this.sendResponse({ req, res, status: SUCCESS, actionType, data: sample });
38
+ }
39
+ catch (error) {
40
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
41
+ }
42
+ };
43
+ this.getById = async (req, res) => {
44
+ const actionType = 'GET_SAMPLE_BY_ID';
45
+ try {
46
+ const sample = await sample_control_1.default.getById(req.params.id);
47
+ this.sendResponse({ req, res, status: SUCCESS, actionType, data: sample });
48
+ }
49
+ catch (error) {
50
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
51
+ }
52
+ };
53
+ this.updateOne = async (req, res) => {
54
+ const actionType = 'UPDATE_ONE_SAMPLE';
55
+ try {
56
+ const sample = await sample_control_1.default.updateOne(req.query, req.body);
57
+ if (sample.acknowledged)
58
+ return this.sendResponse({ req, res, status: SUCCESS, actionType, data: sample });
59
+ this.sendResponse({ req, res, status: UNPROCESSABLE_ENTRY, actionType, data: 'data not updated' });
60
+ }
61
+ catch (error) {
62
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
63
+ }
64
+ };
65
+ this.updateMany = async (req, res) => {
66
+ const actionType = 'UPDATE_MANY_SAMPLE';
67
+ try {
68
+ const sample = await sample_control_1.default.updateMany(req.query, req.body);
69
+ if (sample.acknowledged)
70
+ return this.sendResponse({ req, res, status: SUCCESS, actionType, data: sample });
71
+ this.sendResponse({ req, res, status: UNPROCESSABLE_ENTRY, actionType, data: 'data not updated' });
72
+ }
73
+ catch (error) {
74
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
75
+ }
76
+ };
77
+ this.updateById = async (req, res) => {
78
+ const actionType = 'UPDATE_SAMPLE_BY_ID';
79
+ try {
80
+ const sample = await sample_control_1.default.updateById(req.params.id, req.body);
81
+ if (sample)
82
+ return this.sendResponse({ req, res, status: SUCCESS, actionType, data: sample });
83
+ this.sendResponse({ req, res, status: PRECONDITION_FAILED, actionType, data: 'data not updated' });
84
+ }
85
+ catch (error) {
86
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
87
+ }
88
+ };
89
+ this.deleteOne = async (req, res) => {
90
+ const actionType = 'DELETE_ONE_SAMPLE';
91
+ try {
92
+ const sample = await sample_control_1.default.deleteOne(req.query);
93
+ if (sample.acknowledged)
94
+ return this.sendResponse({ req, res, status: SUCCESS_NO_CONTENT, actionType, data: sample });
95
+ this.sendResponse({ req, res, status: UNPROCESSABLE_ENTRY, actionType, data: 'data not deleted' });
96
+ }
97
+ catch (error) {
98
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
99
+ }
100
+ };
101
+ this.deleteMany = async (req, res) => {
102
+ const actionType = 'DELETE_MANY_SAMPLES';
103
+ try {
104
+ const sample = await sample_control_1.default.deleteMany(req.query);
105
+ if (sample.acknowledged)
106
+ return this.sendResponse({ req, res, status: SUCCESS_NO_CONTENT, actionType, data: sample });
107
+ this.sendResponse({ req, res, status: UNPROCESSABLE_ENTRY, actionType, data: 'data not deleted' });
108
+ }
109
+ catch (error) {
110
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
111
+ }
112
+ };
113
+ this.deleteById = async (req, res) => {
114
+ const actionType = 'DELETE_SAMPLE_BY_ID';
115
+ try {
116
+ const sample = await sample_control_1.default.deleteById(req.params.id);
117
+ if (sample)
118
+ return this.sendResponse({ req, res, status: SUCCESS_NO_CONTENT, actionType, data: sample });
119
+ this.sendResponse({ req, res, status: PRECONDITION_FAILED, actionType, data: 'data not deleted' });
120
+ }
121
+ catch (error) {
122
+ this.sendResponse({ req, res, status: ERROR, actionType, data: error });
123
+ }
124
+ };
125
+ }
126
+ }
127
+ exports.default = new SampleService;
@@ -0,0 +1,42 @@
1
+ declare class DateUtil_ {
2
+ get_local_date: (date?: Date) => Date;
3
+ get_start_date(date: any): number;
4
+ get_end_date(date: any): number;
5
+ get_date_format(date: Date, format: string): Date;
6
+ get_today_date_range(payload?: {
7
+ format?: string;
8
+ date: Date;
9
+ }): {
10
+ start_date: any;
11
+ end_date: any;
12
+ };
13
+ get_date_range(payload?: {
14
+ format?: string;
15
+ date: Date;
16
+ number_of_days?: number;
17
+ }): {
18
+ start_date: any;
19
+ end_date: any;
20
+ };
21
+ get_custom_date_range({ start_date, end_date }: {
22
+ start_date: Date;
23
+ end_date: Date;
24
+ }): {
25
+ start_date: Date;
26
+ end_date: Date;
27
+ };
28
+ this_week_date_range(): {
29
+ start_date: Date;
30
+ end_date: Date;
31
+ };
32
+ this_month_date_range(): {
33
+ start_date: Date;
34
+ end_date: Date;
35
+ };
36
+ this_year_date_range(): {
37
+ start_date: Date;
38
+ end_date: Date;
39
+ };
40
+ }
41
+ declare const DateUtil: DateUtil_;
42
+ export { DateUtil };
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
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
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.DateUtil = void 0;
27
+ const dateFns = __importStar(require("date-fns"));
28
+ class DateUtil_ {
29
+ constructor() {
30
+ this.get_local_date = (date = new Date()) => {
31
+ const currentDate = new Date(date);
32
+ // const localOffset = currentDate.getTimezoneOffset() * 60000;
33
+ // const localDate = new Date(currentDate.getTime() - localOffset);
34
+ return currentDate;
35
+ };
36
+ }
37
+ get_start_date(date) {
38
+ const d = this.get_local_date(date);
39
+ d.setHours(0, 0, 0, 0);
40
+ return d.getTime();
41
+ }
42
+ get_end_date(date) {
43
+ const d = this.get_local_date(date);
44
+ d.setHours(23, 0, 0, 0);
45
+ return d.getTime();
46
+ }
47
+ get_date_format(date, format) {
48
+ return this.get_local_date(date);
49
+ }
50
+ get_today_date_range(payload) {
51
+ const main_date = payload && payload.date ? this.get_local_date(payload.date) : this.get_local_date();
52
+ return {
53
+ start_date: dateFns.startOfDay(main_date),
54
+ end_date: dateFns.endOfDay(main_date)
55
+ };
56
+ }
57
+ get_date_range(payload) {
58
+ const main_date = payload && payload.date ? this.get_local_date(payload.date) : this.get_local_date();
59
+ const number_of_days = payload?.number_of_days ? payload.number_of_days : 0;
60
+ const start_date = dateFns.subDays(main_date, number_of_days);
61
+ const end_date = dateFns.addDays(main_date, number_of_days);
62
+ return {
63
+ start_date: dateFns.startOfDay(start_date),
64
+ end_date: dateFns.endOfDay(end_date)
65
+ };
66
+ }
67
+ get_custom_date_range({ start_date, end_date }) {
68
+ return {
69
+ start_date: dateFns.startOfDay(this.get_local_date(start_date)),
70
+ end_date: dateFns.endOfDay(this.get_local_date(end_date))
71
+ };
72
+ }
73
+ this_week_date_range() {
74
+ return {
75
+ start_date: dateFns.startOfWeek(this.get_local_date()),
76
+ end_date: dateFns.endOfWeek(this.get_local_date())
77
+ };
78
+ }
79
+ this_month_date_range() {
80
+ return {
81
+ start_date: dateFns.startOfMonth(this.get_local_date()),
82
+ end_date: dateFns.endOfMonth(this.get_local_date())
83
+ };
84
+ }
85
+ this_year_date_range() {
86
+ return {
87
+ start_date: dateFns.startOfYear(this.get_local_date()),
88
+ end_date: dateFns.endOfYear(this.get_local_date())
89
+ };
90
+ }
91
+ }
92
+ const DateUtil = new DateUtil_;
93
+ exports.DateUtil = DateUtil;
@@ -0,0 +1,4 @@
1
+ /// <reference types="node" />
2
+ declare const morgan: (req: import("http").IncomingMessage, res: import("http").ServerResponse, callback: (err?: Error | undefined) => void) => void;
3
+ declare const winston: import("winston").Logger;
4
+ export { morgan, winston };
@@ -0,0 +1,48 @@
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
+ exports.winston = exports.morgan = void 0;
7
+ const env_1 = __importDefault(require("../env"));
8
+ const fs_1 = require("fs");
9
+ const path_1 = require("path");
10
+ const morgan_1 = __importDefault(require("morgan"));
11
+ const winston_1 = require("winston");
12
+ class LoggerUtil {
13
+ morgan() {
14
+ let dev_format = '[:date[web] :remote-addr :remote-user ] :method :url HTTP/:http-version | :status :response-time ms';
15
+ let prod_format = '[:date[web] :remote-addr :remote-user ] :method :url HTTP/:http-version :referrer - :user-agent | :status :response-time ms';
16
+ let morgan_format = env_1.default.NODE_ENV === 'prod' ? prod_format : dev_format;
17
+ let request_log_stream = (0, fs_1.createWriteStream)((0, path_1.resolve)(__dirname, `../../logs/request.log`), { flags: 'a' });
18
+ return (0, morgan_1.default)(morgan_format, { stream: request_log_stream });
19
+ }
20
+ winston() {
21
+ let { colorize, combine, printf, timestamp } = winston_1.format;
22
+ let log_transports = {
23
+ console: new winston_1.transports.Console({ level: 'warn' }),
24
+ combined_log: new winston_1.transports.File({ level: 'info', filename: `logs/combined.log` }),
25
+ error_log: new winston_1.transports.File({ level: 'error', filename: `logs/error.log` }),
26
+ exception_log: new winston_1.transports.File({ filename: 'logs/exception.log' }),
27
+ };
28
+ let log_format = printf(({ level, message, timestamp }) => `[${timestamp} : ${level}] - ${message}`);
29
+ let logger = (0, winston_1.createLogger)({
30
+ transports: [
31
+ log_transports.console,
32
+ log_transports.combined_log,
33
+ log_transports.error_log,
34
+ ],
35
+ exceptionHandlers: [
36
+ log_transports.exception_log,
37
+ ],
38
+ exitOnError: false,
39
+ format: combine(colorize(), timestamp(), log_format)
40
+ });
41
+ return logger;
42
+ }
43
+ }
44
+ const loggerUtil = new LoggerUtil();
45
+ const morgan = loggerUtil.morgan();
46
+ exports.morgan = morgan;
47
+ const winston = loggerUtil.winston();
48
+ exports.winston = winston;
@@ -0,0 +1,6 @@
1
+ declare class TokenUtil {
2
+ sign_admin_user(payload: any, expiresIn: string | number): string;
3
+ verify_admin_user(token: string): any;
4
+ }
5
+ declare const _default: TokenUtil;
6
+ export default _default;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
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
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const jwt = __importStar(require("jsonwebtoken"));
30
+ const env_1 = __importDefault(require("../env"));
31
+ const user_response_1 = require("../api-response/user.response");
32
+ class TokenUtil {
33
+ sign_admin_user(payload, expiresIn) {
34
+ return jwt.sign(payload, env_1.default?.ADMIN_JWT_KEY, { expiresIn: expiresIn ? expiresIn : '1d' });
35
+ }
36
+ verify_admin_user(token) {
37
+ return new Promise((resolve, reject) => {
38
+ try {
39
+ const resp = jwt.verify(token, env_1.default.ADMIN_JWT_KEY);
40
+ resolve(resp);
41
+ }
42
+ catch (error) {
43
+ reject({ error, ...user_response_1.UserApiResp.NOT_AUTHORIZED });
44
+ }
45
+ });
46
+ }
47
+ }
48
+ exports.default = new TokenUtil;
@@ -0,0 +1,6 @@
1
+ import * as joi from 'joi';
2
+ declare class SampleValidator {
3
+ create: joi.ObjectSchema<any>;
4
+ }
5
+ declare const _default: SampleValidator;
6
+ export default _default;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
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
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const joi = __importStar(require("joi"));
27
+ class SampleValidator {
28
+ constructor() {
29
+ this.create = joi.object({
30
+ name: joi.string().required(),
31
+ description: joi.string().required()
32
+ });
33
+ }
34
+ }
35
+ exports.default = new SampleValidator;
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@vasrefil/api-toolkit",
3
3
  "description": "This is Vasrefil API toolkit",
4
- "version": "1.0.3",
4
+ "version": "1.0.6",
5
5
  "author": "Sodiq Alabi",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "scripts": {
9
9
  "start": "ts-node-dev src/index.ts",
10
10
  "build": "tsc",
11
- "prepare": "npm run build",
11
+ "prepublishOnly": "npm run build",
12
12
  "serve": "node dist/index.js",
13
13
  "dev": "nodemon --exec ts-node src/index.ts",
14
14
  "run-ts-node": "ts-node-dev"