@amohamud23/notihub 1.0.151 → 1.0.153

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/index.cjs CHANGED
@@ -51,8 +51,11 @@ __export(src_exports, {
51
51
  UserModel: () => UserModel_default,
52
52
  ViewsModel: () => ViewsModel_default,
53
53
  addMonths: () => addMonths,
54
+ formatResponse: () => formatResponse,
54
55
  getDate: () => getDate,
55
- getDateFormat: () => getDateFormat
56
+ getDateFormat: () => getDateFormat,
57
+ handleError: () => handleError,
58
+ handleSuccess: () => handleSuccess
56
59
  });
57
60
  module.exports = __toCommonJS(src_exports);
58
61
 
@@ -109,6 +112,53 @@ function addMonths({ date, months }) {
109
112
  date.setMonth(date.getMonth() + months);
110
113
  return date;
111
114
  }
115
+ var handleError = (error, res, req) => {
116
+ console.error(`[${(/* @__PURE__ */ new Date()).toISOString()}] Error:`, {
117
+ message: error.message,
118
+ errorCode: error.errorCode,
119
+ stack: process.env.NODE_ENV !== "production" ? error.stack : void 0,
120
+ path: req?.path,
121
+ method: req?.method
122
+ });
123
+ let statusCode = 500;
124
+ let message = error.message || "Something went wrong";
125
+ if (error.errorCode === "UserNotFound" || error.message?.toLowerCase().includes("not found")) {
126
+ statusCode = 404;
127
+ } else if (error.message?.toLowerCase().includes("required") || error.message?.toLowerCase().includes("invalid")) {
128
+ statusCode = 400;
129
+ } else if (error.statusCode) {
130
+ statusCode = error.statusCode;
131
+ }
132
+ const response = {
133
+ success: false,
134
+ message,
135
+ error: error.errorCode || error.name,
136
+ statusCode,
137
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
138
+ };
139
+ res.status(statusCode).json(response);
140
+ };
141
+ var handleSuccess = (data, res, statusCode = 200) => {
142
+ const response = {
143
+ success: true,
144
+ data,
145
+ statusCode,
146
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
147
+ };
148
+ res.status(statusCode).json(response);
149
+ };
150
+
151
+ // src/util/responseUtils.ts
152
+ var formatResponse = (data, message = "Success", error, statusCode = 200) => {
153
+ const response = {
154
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
155
+ data: data || null,
156
+ message,
157
+ statusCode,
158
+ error
159
+ };
160
+ return response;
161
+ };
112
162
 
113
163
  // src/models/UserModel.ts
114
164
  var import_mongoose = require("mongoose");
@@ -800,6 +850,9 @@ var InvalidPushToken = "4009";
800
850
  UserModel,
801
851
  ViewsModel,
802
852
  addMonths,
853
+ formatResponse,
803
854
  getDate,
804
- getDateFormat
855
+ getDateFormat,
856
+ handleError,
857
+ handleSuccess
805
858
  });
package/dist/index.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Response as Response$1 } from 'express';
1
2
  import * as mongoose from 'mongoose';
2
3
  import mongoose__default from 'mongoose';
3
4
 
@@ -28,6 +29,13 @@ type ICreateCustomerRequestBody = {
28
29
  name: string;
29
30
  };
30
31
  type ICreateNotiRequestBody = {};
32
+ type Response = {
33
+ data?: any;
34
+ message?: string;
35
+ error?: any;
36
+ statusCode?: number;
37
+ timestamp?: string;
38
+ };
31
39
  type INotiHubCustomerMinified = {
32
40
  _id: string;
33
41
  name: string;
@@ -204,6 +212,24 @@ type AddMonths = {
204
212
  declare function getDate(dateStr: string): string;
205
213
  declare function getDateFormat(dateStr: string): string;
206
214
  declare function addMonths({ date, months }: AddMonths): Date;
215
+ /**
216
+ * Handles errors by logging them and sending a standardized error response.
217
+ * @param error - The error object to handle.
218
+ * @param res - The Express response object to send the error response.
219
+ * @param req - Optional Express request object for additional context.
220
+ * @return void
221
+ */
222
+ declare const handleError: (error: any, res: Response$1, req?: any) => void;
223
+ /**
224
+ * Handles successful responses by sending a standardized success response.
225
+ * @param data - The data to include in the success response.
226
+ * @param res - The Express response object to send the success response.
227
+ * @param statusCode - Optional HTTP status code (default is 200).
228
+ * @return void
229
+ */
230
+ declare const handleSuccess: <T>(data: T, res: Response$1, statusCode?: number) => void;
231
+
232
+ declare const formatResponse: (data: any, message: string | undefined, error: any, statusCode?: number) => Response;
207
233
 
208
234
  declare const UserModel: mongoose.Model<INotiHubUser, {}, {}, {}, mongoose.Document<unknown, {}, INotiHubUser> & INotiHubUser & {
209
235
  _id: mongoose.Types.ObjectId;
@@ -400,4 +426,4 @@ declare namespace errorcodes {
400
426
  export { errorcodes_CustomerNotFound as CustomerNotFound, errorcodes_DocumentNotFound as DocumentNotFound, errorcodes_InvalidEmail as InvalidEmail, errorcodes_InvalidName as InvalidName, errorcodes_InvalidPassword as InvalidPassword, errorcodes_InvalidPushToken as InvalidPushToken, errorcodes_UserNotFound as UserNotFound };
401
427
  }
402
428
 
403
- export { type CreateNotiRequestBody, type CreateSubscriptionRequestBody, type CreateUserRequestBody, Customer$1 as Customer, CustomerMetadata, CustomerMinified, CustomerNotiHubStats, DocumentNotFoundException, errorcodes as ErrorCode, type ICreateCustomerRequestBody, type ICreateNotiRequestBody, type INotiHubCustomer, type INotiHubCustomerMetadata, type INotiHubCustomerMinified, type INotiHubImage, type INotiHubNotification, type INotiHubNotificationStats, type INotiHubPaymentSession, type INotiHubStats, type INotiHubStatsHistory, type INotiHubStripeSubscription, type INotiHubSubscription, type INotiHubSubscriptionActivity, type INotiHubUser, type INotiHubUserView, type INotiType, type INotiTypeStats, type IUserSubscribeNotifier, type IUserSubscription, client as MongooseClient, NotiHubStatsHistory, type NotiHubTypes, NotiTypeModel, NotiTypeStatsModel, NotificationModel, NotificationStatsModel as NotificationStats, PaymentSessionModel, StripScriptionModel, Subscription, SubscriptionActivity, SubscriptionType, TABLES, UserModel, ViewsModel, addMonths, getDate, getDateFormat };
429
+ export { type CreateNotiRequestBody, type CreateSubscriptionRequestBody, type CreateUserRequestBody, Customer$1 as Customer, CustomerMetadata, CustomerMinified, CustomerNotiHubStats, DocumentNotFoundException, errorcodes as ErrorCode, type ICreateCustomerRequestBody, type ICreateNotiRequestBody, type INotiHubCustomer, type INotiHubCustomerMetadata, type INotiHubCustomerMinified, type INotiHubImage, type INotiHubNotification, type INotiHubNotificationStats, type INotiHubPaymentSession, type INotiHubStats, type INotiHubStatsHistory, type INotiHubStripeSubscription, type INotiHubSubscription, type INotiHubSubscriptionActivity, type INotiHubUser, type INotiHubUserView, type INotiType, type INotiTypeStats, type IUserSubscribeNotifier, type IUserSubscription, client as MongooseClient, NotiHubStatsHistory, type NotiHubTypes, NotiTypeModel, NotiTypeStatsModel, NotificationModel, NotificationStatsModel as NotificationStats, PaymentSessionModel, type Response, StripScriptionModel, Subscription, SubscriptionActivity, SubscriptionType, TABLES, UserModel, ViewsModel, addMonths, formatResponse, getDate, getDateFormat, handleError, handleSuccess };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Response as Response$1 } from 'express';
1
2
  import * as mongoose from 'mongoose';
2
3
  import mongoose__default from 'mongoose';
3
4
 
@@ -28,6 +29,13 @@ type ICreateCustomerRequestBody = {
28
29
  name: string;
29
30
  };
30
31
  type ICreateNotiRequestBody = {};
32
+ type Response = {
33
+ data?: any;
34
+ message?: string;
35
+ error?: any;
36
+ statusCode?: number;
37
+ timestamp?: string;
38
+ };
31
39
  type INotiHubCustomerMinified = {
32
40
  _id: string;
33
41
  name: string;
@@ -204,6 +212,24 @@ type AddMonths = {
204
212
  declare function getDate(dateStr: string): string;
205
213
  declare function getDateFormat(dateStr: string): string;
206
214
  declare function addMonths({ date, months }: AddMonths): Date;
215
+ /**
216
+ * Handles errors by logging them and sending a standardized error response.
217
+ * @param error - The error object to handle.
218
+ * @param res - The Express response object to send the error response.
219
+ * @param req - Optional Express request object for additional context.
220
+ * @return void
221
+ */
222
+ declare const handleError: (error: any, res: Response$1, req?: any) => void;
223
+ /**
224
+ * Handles successful responses by sending a standardized success response.
225
+ * @param data - The data to include in the success response.
226
+ * @param res - The Express response object to send the success response.
227
+ * @param statusCode - Optional HTTP status code (default is 200).
228
+ * @return void
229
+ */
230
+ declare const handleSuccess: <T>(data: T, res: Response$1, statusCode?: number) => void;
231
+
232
+ declare const formatResponse: (data: any, message: string | undefined, error: any, statusCode?: number) => Response;
207
233
 
208
234
  declare const UserModel: mongoose.Model<INotiHubUser, {}, {}, {}, mongoose.Document<unknown, {}, INotiHubUser> & INotiHubUser & {
209
235
  _id: mongoose.Types.ObjectId;
@@ -400,4 +426,4 @@ declare namespace errorcodes {
400
426
  export { errorcodes_CustomerNotFound as CustomerNotFound, errorcodes_DocumentNotFound as DocumentNotFound, errorcodes_InvalidEmail as InvalidEmail, errorcodes_InvalidName as InvalidName, errorcodes_InvalidPassword as InvalidPassword, errorcodes_InvalidPushToken as InvalidPushToken, errorcodes_UserNotFound as UserNotFound };
401
427
  }
402
428
 
403
- export { type CreateNotiRequestBody, type CreateSubscriptionRequestBody, type CreateUserRequestBody, Customer$1 as Customer, CustomerMetadata, CustomerMinified, CustomerNotiHubStats, DocumentNotFoundException, errorcodes as ErrorCode, type ICreateCustomerRequestBody, type ICreateNotiRequestBody, type INotiHubCustomer, type INotiHubCustomerMetadata, type INotiHubCustomerMinified, type INotiHubImage, type INotiHubNotification, type INotiHubNotificationStats, type INotiHubPaymentSession, type INotiHubStats, type INotiHubStatsHistory, type INotiHubStripeSubscription, type INotiHubSubscription, type INotiHubSubscriptionActivity, type INotiHubUser, type INotiHubUserView, type INotiType, type INotiTypeStats, type IUserSubscribeNotifier, type IUserSubscription, client as MongooseClient, NotiHubStatsHistory, type NotiHubTypes, NotiTypeModel, NotiTypeStatsModel, NotificationModel, NotificationStatsModel as NotificationStats, PaymentSessionModel, StripScriptionModel, Subscription, SubscriptionActivity, SubscriptionType, TABLES, UserModel, ViewsModel, addMonths, getDate, getDateFormat };
429
+ export { type CreateNotiRequestBody, type CreateSubscriptionRequestBody, type CreateUserRequestBody, Customer$1 as Customer, CustomerMetadata, CustomerMinified, CustomerNotiHubStats, DocumentNotFoundException, errorcodes as ErrorCode, type ICreateCustomerRequestBody, type ICreateNotiRequestBody, type INotiHubCustomer, type INotiHubCustomerMetadata, type INotiHubCustomerMinified, type INotiHubImage, type INotiHubNotification, type INotiHubNotificationStats, type INotiHubPaymentSession, type INotiHubStats, type INotiHubStatsHistory, type INotiHubStripeSubscription, type INotiHubSubscription, type INotiHubSubscriptionActivity, type INotiHubUser, type INotiHubUserView, type INotiType, type INotiTypeStats, type IUserSubscribeNotifier, type IUserSubscription, client as MongooseClient, NotiHubStatsHistory, type NotiHubTypes, NotiTypeModel, NotiTypeStatsModel, NotificationModel, NotificationStatsModel as NotificationStats, PaymentSessionModel, type Response, StripScriptionModel, Subscription, SubscriptionActivity, SubscriptionType, TABLES, UserModel, ViewsModel, addMonths, formatResponse, getDate, getDateFormat, handleError, handleSuccess };
package/dist/index.js CHANGED
@@ -57,6 +57,53 @@ function addMonths({ date, months }) {
57
57
  date.setMonth(date.getMonth() + months);
58
58
  return date;
59
59
  }
60
+ var handleError = (error, res, req) => {
61
+ console.error(`[${(/* @__PURE__ */ new Date()).toISOString()}] Error:`, {
62
+ message: error.message,
63
+ errorCode: error.errorCode,
64
+ stack: process.env.NODE_ENV !== "production" ? error.stack : void 0,
65
+ path: req?.path,
66
+ method: req?.method
67
+ });
68
+ let statusCode = 500;
69
+ let message = error.message || "Something went wrong";
70
+ if (error.errorCode === "UserNotFound" || error.message?.toLowerCase().includes("not found")) {
71
+ statusCode = 404;
72
+ } else if (error.message?.toLowerCase().includes("required") || error.message?.toLowerCase().includes("invalid")) {
73
+ statusCode = 400;
74
+ } else if (error.statusCode) {
75
+ statusCode = error.statusCode;
76
+ }
77
+ const response = {
78
+ success: false,
79
+ message,
80
+ error: error.errorCode || error.name,
81
+ statusCode,
82
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
83
+ };
84
+ res.status(statusCode).json(response);
85
+ };
86
+ var handleSuccess = (data, res, statusCode = 200) => {
87
+ const response = {
88
+ success: true,
89
+ data,
90
+ statusCode,
91
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
92
+ };
93
+ res.status(statusCode).json(response);
94
+ };
95
+
96
+ // src/util/responseUtils.ts
97
+ var formatResponse = (data, message = "Success", error, statusCode = 200) => {
98
+ const response = {
99
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
100
+ data: data || null,
101
+ message,
102
+ statusCode,
103
+ error
104
+ };
105
+ return response;
106
+ };
60
107
 
61
108
  // src/models/UserModel.ts
62
109
  import { Schema, model } from "mongoose";
@@ -758,6 +805,9 @@ export {
758
805
  UserModel_default as UserModel,
759
806
  ViewsModel_default as ViewsModel,
760
807
  addMonths,
808
+ formatResponse,
761
809
  getDate,
762
- getDateFormat
810
+ getDateFormat,
811
+ handleError,
812
+ handleSuccess
763
813
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amohamud23/notihub",
3
- "version": "1.0.151",
3
+ "version": "1.0.153",
4
4
  "description": "Notihub Package",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.cts",
@@ -21,8 +21,10 @@
21
21
  "author": "Abdi Mohamud",
22
22
  "license": "ISC",
23
23
  "devDependencies": {
24
+ "@types/express": "^5.0.3",
24
25
  "@types/jest": "^29.5.14",
25
26
  "@types/node": "^22.8.4",
27
+ "express": "^5.1.0",
26
28
  "jest": "^29.7.0",
27
29
  "ts-jest": "^29.3.4",
28
30
  "typescript": "^5.8.3"