saas-backend-kit 1.0.1 → 1.0.3

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 (55) hide show
  1. package/README.md +205 -6
  2. package/dist/auth/index.js +2 -1
  3. package/dist/auth/index.js.map +1 -1
  4. package/dist/auth/index.mjs +2 -1
  5. package/dist/auth/index.mjs.map +1 -1
  6. package/dist/config/index.js +1 -0
  7. package/dist/config/index.js.map +1 -1
  8. package/dist/config/index.mjs +1 -0
  9. package/dist/config/index.mjs.map +1 -1
  10. package/dist/index.js +124 -41
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +122 -42
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/logger/index.js +1 -0
  15. package/dist/logger/index.js.map +1 -1
  16. package/dist/logger/index.mjs +1 -0
  17. package/dist/logger/index.mjs.map +1 -1
  18. package/dist/notifications/index.js +1 -0
  19. package/dist/notifications/index.js.map +1 -1
  20. package/dist/notifications/index.mjs +1 -0
  21. package/dist/notifications/index.mjs.map +1 -1
  22. package/dist/queue/index.js +1 -0
  23. package/dist/queue/index.js.map +1 -1
  24. package/dist/queue/index.mjs +1 -0
  25. package/dist/queue/index.mjs.map +1 -1
  26. package/dist/rate-limit/index.js +2 -0
  27. package/dist/rate-limit/index.js.map +1 -1
  28. package/dist/rate-limit/index.mjs +2 -0
  29. package/dist/rate-limit/index.mjs.map +1 -1
  30. package/dist/response/index.js +51 -40
  31. package/dist/response/index.js.map +1 -1
  32. package/dist/response/index.mjs +51 -40
  33. package/dist/response/index.mjs.map +1 -1
  34. package/dist/upload/index.js +1 -0
  35. package/dist/upload/index.js.map +1 -1
  36. package/dist/upload/index.mjs +1 -0
  37. package/dist/upload/index.mjs.map +1 -1
  38. package/examples/express/.env.example +4 -1
  39. package/jest-output.json +72 -0
  40. package/jest.config.js +19 -0
  41. package/package.json +10 -7
  42. package/src/auth/jwt.ts +1 -1
  43. package/src/config/index.ts +1 -0
  44. package/src/database/index.ts +102 -0
  45. package/src/index.ts +1 -0
  46. package/src/rate-limit/express.ts +1 -0
  47. package/src/response/index.ts +49 -40
  48. package/tests/auth.test.ts +134 -0
  49. package/tests/config.test.ts +36 -0
  50. package/tests/logger.test.ts +47 -0
  51. package/tests/notifications.test.ts +19 -0
  52. package/tests/rate-limit.test.ts +50 -0
  53. package/tests/upload.test.ts +33 -0
  54. package/tsconfig.test.json +14 -0
  55. package/tsup.config.ts +2 -1
package/dist/index.mjs CHANGED
@@ -4,9 +4,9 @@ import { Queue, Worker } from 'bullmq';
4
4
  import jwt from 'jsonwebtoken';
5
5
  import bcrypt from 'bcryptjs';
6
6
  import nodemailer from 'nodemailer';
7
- import { Response } from 'express';
8
7
  import { S3Client, PutObjectCommand, DeleteObjectCommand, GetObjectCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
9
8
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
9
+ import { MongoClient } from 'mongodb';
10
10
 
11
11
  var __defProp = Object.defineProperty;
12
12
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -42,6 +42,7 @@ var init_config = __esm({
42
42
  NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
43
43
  PORT: z.string().default("3000"),
44
44
  DATABASE_URL: z.string().optional(),
45
+ MONGODB_URL: z.string().optional(),
45
46
  REDIS_URL: z.string().default("redis://localhost:6379"),
46
47
  JWT_SECRET: z.string().min(32).optional(),
47
48
  JWT_EXPIRES_IN: z.string().default("7d"),
@@ -438,6 +439,7 @@ var init_express = __esm({
438
439
  cleanupInterval;
439
440
  constructor() {
440
441
  this.cleanupInterval = setInterval(() => this.cleanup(), 6e4);
442
+ this.cleanupInterval.unref();
441
443
  }
442
444
  cleanup() {
443
445
  const now = Date.now();
@@ -568,7 +570,7 @@ var JWTService = class {
568
570
  return jwt.verify(token, this.refreshSecret);
569
571
  }
570
572
  refreshTokens(refreshToken) {
571
- const payload = this.verifyRefreshToken(refreshToken);
573
+ const { iat, exp, nbf, ...payload } = this.verifyRefreshToken(refreshToken);
572
574
  return this.generateTokenPair(payload);
573
575
  }
574
576
  };
@@ -1045,6 +1047,8 @@ init_express();
1045
1047
 
1046
1048
  // src/index.ts
1047
1049
  init_config();
1050
+
1051
+ // src/response/index.ts
1048
1052
  var ResponseHelper = class {
1049
1053
  static success(res, data, message, statusCode = 200) {
1050
1054
  const response2 = {
@@ -1111,45 +1115,51 @@ var ResponseHelper = class {
1111
1115
  return res.status(204).send();
1112
1116
  }
1113
1117
  };
1114
- Response.prototype.success = function(data, message, statusCode = 200) {
1115
- return ResponseHelper.success(this, data, message, statusCode);
1116
- };
1117
- Response.prototype.created = function(data, message) {
1118
- return ResponseHelper.created(this, data, message);
1119
- };
1120
- Response.prototype.updated = function(data, message) {
1121
- return ResponseHelper.updated(this, data, message);
1122
- };
1123
- Response.prototype.deleted = function(message) {
1124
- return ResponseHelper.deleted(this, message);
1125
- };
1126
- Response.prototype.error = function(error, statusCode = 400, code, details) {
1127
- return ResponseHelper.error(this, error, statusCode, code, details);
1128
- };
1129
- Response.prototype.badRequest = function(error, code) {
1130
- return ResponseHelper.badRequest(this, error, code);
1131
- };
1132
- Response.prototype.unauthorized = function(error, code) {
1133
- return ResponseHelper.unauthorized(this, error, code);
1134
- };
1135
- Response.prototype.forbidden = function(error, code) {
1136
- return ResponseHelper.forbidden(this, error, code);
1137
- };
1138
- Response.prototype.notFound = function(error, code) {
1139
- return ResponseHelper.notFound(this, error, code);
1140
- };
1141
- Response.prototype.conflict = function(error, code) {
1142
- return ResponseHelper.conflict(this, error, code);
1143
- };
1144
- Response.prototype.validationError = function(error, details) {
1145
- return ResponseHelper.validationError(this, error, details);
1146
- };
1147
- Response.prototype.internalError = function(error) {
1148
- return ResponseHelper.internalError(this, error);
1149
- };
1150
- Response.prototype.paginated = function(data, page, limit, total) {
1151
- return ResponseHelper.paginated(this, data, page, limit, total);
1152
- };
1118
+ try {
1119
+ const proto = __require("express").response;
1120
+ if (proto) {
1121
+ proto.success = function(data, message, statusCode = 200) {
1122
+ return ResponseHelper.success(this, data, message, statusCode);
1123
+ };
1124
+ proto.created = function(data, message) {
1125
+ return ResponseHelper.created(this, data, message);
1126
+ };
1127
+ proto.updated = function(data, message) {
1128
+ return ResponseHelper.updated(this, data, message);
1129
+ };
1130
+ proto.deleted = function(message) {
1131
+ return ResponseHelper.deleted(this, message);
1132
+ };
1133
+ proto.error = function(error, statusCode = 400, code, details) {
1134
+ return ResponseHelper.error(this, error, statusCode, code, details);
1135
+ };
1136
+ proto.badRequest = function(error, code) {
1137
+ return ResponseHelper.badRequest(this, error, code);
1138
+ };
1139
+ proto.unauthorized = function(error, code) {
1140
+ return ResponseHelper.unauthorized(this, error, code);
1141
+ };
1142
+ proto.forbidden = function(error, code) {
1143
+ return ResponseHelper.forbidden(this, error, code);
1144
+ };
1145
+ proto.notFound = function(error, code) {
1146
+ return ResponseHelper.notFound(this, error, code);
1147
+ };
1148
+ proto.conflict = function(error, code) {
1149
+ return ResponseHelper.conflict(this, error, code);
1150
+ };
1151
+ proto.validationError = function(error, details) {
1152
+ return ResponseHelper.validationError(this, error, details);
1153
+ };
1154
+ proto.internalError = function(error) {
1155
+ return ResponseHelper.internalError(this, error);
1156
+ };
1157
+ proto.paginated = function(data, page, limit, total) {
1158
+ return ResponseHelper.paginated(this, data, page, limit, total);
1159
+ };
1160
+ }
1161
+ } catch {
1162
+ }
1153
1163
  var response = ResponseHelper;
1154
1164
 
1155
1165
  // src/upload/index.ts
@@ -1450,6 +1460,76 @@ function createExpressApp(options = {}) {
1450
1460
  builder.initialize(app);
1451
1461
  return app;
1452
1462
  }
1463
+ var DatabaseManager = class {
1464
+ client = null;
1465
+ db = null;
1466
+ url = null;
1467
+ async connect(options) {
1468
+ if (this.db) return this.db;
1469
+ this.url = options.url;
1470
+ const clientOptions = {
1471
+ ...options.options,
1472
+ maxPoolSize: 10,
1473
+ minPoolSize: 1,
1474
+ serverSelectionTimeoutMS: 5e3,
1475
+ socketTimeoutMS: 45e3
1476
+ };
1477
+ this.client = new MongoClient(this.url, clientOptions);
1478
+ await this.client.connect();
1479
+ this.db = this.client.db();
1480
+ return this.db;
1481
+ }
1482
+ async connectTo(uri, dbName, options) {
1483
+ if (this.db) return this.db;
1484
+ const clientOptions = {
1485
+ ...options,
1486
+ maxPoolSize: 10,
1487
+ minPoolSize: 1,
1488
+ serverSelectionTimeoutMS: 5e3,
1489
+ socketTimeoutMS: 45e3
1490
+ };
1491
+ this.client = new MongoClient(uri, clientOptions);
1492
+ await this.client.connect();
1493
+ this.db = this.client.db(dbName);
1494
+ return this.db;
1495
+ }
1496
+ getDb() {
1497
+ if (!this.db) {
1498
+ throw new Error("Database not connected. Call connect() first.");
1499
+ }
1500
+ return this.db;
1501
+ }
1502
+ getClient() {
1503
+ if (!this.client) {
1504
+ throw new Error("Database not connected. Call connect() first.");
1505
+ }
1506
+ return this.client;
1507
+ }
1508
+ collection(name) {
1509
+ return this.getDb().collection(name);
1510
+ }
1511
+ async close() {
1512
+ if (this.client) {
1513
+ await this.client.close();
1514
+ this.client = null;
1515
+ this.db = null;
1516
+ }
1517
+ }
1518
+ isConnected() {
1519
+ return this.client !== null && this.db !== null;
1520
+ }
1521
+ };
1522
+ var databaseManager = new DatabaseManager();
1523
+ var database = {
1524
+ connect: (options) => databaseManager.connect(options),
1525
+ connectTo: (uri, dbName, options) => databaseManager.connectTo(uri, dbName, options),
1526
+ getDb: () => databaseManager.getDb(),
1527
+ getClient: () => databaseManager.getClient(),
1528
+ collection: (name) => databaseManager.collection(name),
1529
+ close: () => databaseManager.close(),
1530
+ isConnected: () => databaseManager.isConnected()
1531
+ };
1532
+ var db = database;
1453
1533
 
1454
1534
  // src/index.ts
1455
1535
  init_types();
@@ -1457,6 +1537,6 @@ init_queue();
1457
1537
  init_logger();
1458
1538
  init_config();
1459
1539
 
1460
- export { Auth, AuthService, PluginManager, QueueManager, ResponseHelper, SaaSAppBuilder, auth, config, createApp, createAuth, createExpressApp, createQueue, createRateLimiter, logger, notification, notify, queue, rateLimit, response, s3Service, upload };
1540
+ export { Auth, AuthService, DatabaseManager, PluginManager, QueueManager, ResponseHelper, SaaSAppBuilder, auth, config, createApp, createAuth, createExpressApp, createQueue, createRateLimiter, database, db, logger, notification, notify, queue, rateLimit, response, s3Service, upload };
1461
1541
  //# sourceMappingURL=index.mjs.map
1462
1542
  //# sourceMappingURL=index.mjs.map