@xenterprises/fastify-xconfig 0.0.1 → 0.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 (91) hide show
  1. package/README.md +145 -0
  2. package/dist/integrations/cloudinary.d.ts +1 -0
  3. package/dist/integrations/cloudinary.js +25 -0
  4. package/dist/integrations/cloudinary.js.map +1 -0
  5. package/dist/integrations/prisma.d.ts +1 -0
  6. package/dist/integrations/prisma.js +13 -0
  7. package/dist/integrations/prisma.js.map +1 -0
  8. package/dist/integrations/sendgrid.d.ts +1 -0
  9. package/dist/integrations/sendgrid.js +22 -0
  10. package/dist/integrations/sendgrid.js.map +1 -0
  11. package/dist/integrations/stripe.d.ts +1 -0
  12. package/dist/integrations/stripe.js +15 -0
  13. package/dist/integrations/stripe.js.map +1 -0
  14. package/dist/integrations/twilio.d.ts +1 -0
  15. package/dist/integrations/twilio.js +17 -0
  16. package/dist/integrations/twilio.js.map +1 -0
  17. package/dist/middleware/bugsnag.d.ts +2 -0
  18. package/dist/middleware/bugsnag.js +9 -0
  19. package/dist/middleware/bugsnag.js.map +1 -0
  20. package/dist/middleware/cors.d.ts +2 -0
  21. package/dist/middleware/cors.js +11 -0
  22. package/dist/middleware/cors.js.map +1 -0
  23. package/dist/middleware/errorHandler.d.ts +2 -0
  24. package/dist/middleware/errorHandler.js +19 -0
  25. package/dist/middleware/errorHandler.js.map +1 -0
  26. package/dist/middleware/multipart.d.ts +2 -0
  27. package/dist/middleware/multipart.js +7 -0
  28. package/dist/middleware/multipart.js.map +1 -0
  29. package/dist/middleware/rateLimit.d.ts +2 -0
  30. package/dist/middleware/rateLimit.js +7 -0
  31. package/dist/middleware/rateLimit.js.map +1 -0
  32. package/dist/middleware/underPressure.d.ts +2 -0
  33. package/dist/middleware/underPressure.js +7 -0
  34. package/dist/middleware/underPressure.js.map +1 -0
  35. package/dist/utils/colorize.d.ts +4 -0
  36. package/dist/utils/colorize.js +33 -0
  37. package/dist/utils/colorize.js.map +1 -0
  38. package/dist/utils/formatBytes.d.ts +1 -0
  39. package/dist/utils/formatBytes.js +10 -0
  40. package/dist/utils/formatBytes.js.map +1 -0
  41. package/dist/utils/randomUUID.d.ts +1 -0
  42. package/dist/utils/randomUUID.js +3 -0
  43. package/dist/utils/randomUUID.js.map +1 -0
  44. package/dist/utils/statAsync.d.ts +2 -0
  45. package/dist/utils/statAsync.js +4 -0
  46. package/dist/utils/statAsync.js.map +1 -0
  47. package/dist/xConfig.d.ts +3 -0
  48. package/dist/xConfig.js +9 -0
  49. package/dist/xConfig.js.map +1 -0
  50. package/package.json +25 -2
  51. package/server/app.js +78 -1
  52. package/src/auth/admin.js +241 -0
  53. package/src/auth/portal.js +284 -0
  54. package/src/integrations/cloudinary.js +98 -0
  55. package/src/integrations/geocode.js +43 -0
  56. package/src/integrations/prisma.js +30 -0
  57. package/src/integrations/sendgrid.js +57 -0
  58. package/src/integrations/twilio.js +146 -0
  59. package/src/lifecycle/xFastifyAfter.js +27 -0
  60. package/src/middleware/bugsnag.js +10 -0
  61. package/src/middleware/cors.js +10 -0
  62. package/src/middleware/fancyErrors.js +26 -0
  63. package/src/middleware/multipart.js +6 -0
  64. package/src/middleware/rateLimit.js +6 -0
  65. package/src/middleware/underPressure.js +6 -0
  66. package/src/utils/colorize.js +37 -0
  67. package/src/utils/cookie.js +5 -0
  68. package/src/utils/formatBytes.js +16 -0
  69. package/src/utils/health.js +126 -0
  70. package/src/utils/xEcho.js +12 -0
  71. package/src/utils/xSlugify.js +20 -0
  72. package/src/utils/xUUID.js +14 -0
  73. package/src/xConfig.js +110 -3
  74. package/ts-reference/integrations/cloudinary.ts +26 -0
  75. package/ts-reference/integrations/prisma.ts +13 -0
  76. package/ts-reference/integrations/sendgrid.ts +27 -0
  77. package/ts-reference/integrations/stripe.ts +15 -0
  78. package/ts-reference/integrations/twilio.ts +20 -0
  79. package/ts-reference/middleware/bugsnag.ts +10 -0
  80. package/ts-reference/middleware/cors.ts +13 -0
  81. package/ts-reference/middleware/errorHandler.ts +24 -0
  82. package/ts-reference/middleware/multipart.ts +8 -0
  83. package/ts-reference/middleware/rateLimit.ts +8 -0
  84. package/ts-reference/middleware/underPressure.ts +11 -0
  85. package/ts-reference/utils/colorize.ts +45 -0
  86. package/ts-reference/utils/formatBytes.ts +8 -0
  87. package/ts-reference/utils/randomUUID.ts +3 -0
  88. package/ts-reference/utils/statAsync.ts +4 -0
  89. package/xConfigReference.js +1495 -0
  90. package/xConfigWorkingList.js +720 -0
  91. package/tsconfig.json +0 -16
@@ -0,0 +1,26 @@
1
+ import { v2 as Cloudinary } from "cloudinary";
2
+
3
+ export async function setupCloudinary(fastify: any, cloudinaryOptions: any) {
4
+ if (cloudinaryOptions.active !== false) {
5
+ Cloudinary.config({
6
+ cloud_name: cloudinaryOptions.cloudName,
7
+ api_key: cloudinaryOptions.apiKey,
8
+ api_secret: cloudinaryOptions.apiSecret,
9
+ });
10
+ fastify.decorate("cloudinary", {
11
+ async upload(fileStream: any, options = {}) {
12
+ return new Promise((resolve, reject) => {
13
+ const uploadStream = Cloudinary.uploader.upload_stream(
14
+ options,
15
+ (error, result) => {
16
+ if (error) reject(error);
17
+ else resolve(result);
18
+ }
19
+ );
20
+ fileStream.pipe(uploadStream);
21
+ });
22
+ },
23
+ });
24
+ console.info(" ✅ Cloudinary Enabled");
25
+ }
26
+ }
@@ -0,0 +1,13 @@
1
+ import { PrismaClient } from "@prisma/client";
2
+
3
+ export async function setupPrisma(fastify: any, prismaOptions: any) {
4
+ if (prismaOptions.active !== false) {
5
+ const prisma = new PrismaClient(prismaOptions);
6
+ await prisma.$connect();
7
+ fastify.decorate("prisma", prisma);
8
+ fastify.addHook("onClose", async () => {
9
+ await fastify.prisma.$disconnect();
10
+ });
11
+ console.info(" ✅ Prisma Enabled");
12
+ }
13
+ }
@@ -0,0 +1,27 @@
1
+ import Sendgrid from "@sendgrid/mail";
2
+
3
+ export async function setupSendGrid(fastify: any, sendGridOptions: any) {
4
+ if (sendGridOptions.active !== false) {
5
+ if (!sendGridOptions.apiKey)
6
+ throw new Error("SendGrid API key must be provided.");
7
+ Sendgrid.setApiKey(sendGridOptions.apiKey);
8
+ fastify.decorate("sendgrid", {
9
+ async sendEmail(
10
+ to: string,
11
+ subject: string,
12
+ templateId: string,
13
+ dynamicTemplateData: any
14
+ ) {
15
+ const msg = {
16
+ to,
17
+ from: sendGridOptions.fromEmail,
18
+ subject,
19
+ templateId,
20
+ dynamicTemplateData,
21
+ };
22
+ await Sendgrid.send(msg);
23
+ },
24
+ });
25
+ console.info(" ✅ SendGrid Enabled");
26
+ }
27
+ }
@@ -0,0 +1,15 @@
1
+ import Stripe from "stripe";
2
+
3
+ export async function setupStripe(fastify: any, stripeOptions: any) {
4
+ if (stripeOptions.active !== false) {
5
+ const stripeClient = new Stripe(stripeOptions.apiKey, {
6
+ apiVersion: "2024-06-20",
7
+ });
8
+ fastify.decorate("stripe", {
9
+ async createPaymentIntent(amount: number, currency = "usd") {
10
+ return await stripeClient.paymentIntents.create({ amount, currency });
11
+ },
12
+ });
13
+ console.info(" ✅ Stripe Enabled");
14
+ }
15
+ }
@@ -0,0 +1,20 @@
1
+ import Twilio from "twilio";
2
+
3
+ export async function setupTwilio(fastify: any, twilioOptions: any) {
4
+ if (twilioOptions.active !== false) {
5
+ const twilioClient = Twilio(
6
+ twilioOptions.accountSid,
7
+ twilioOptions.authToken
8
+ );
9
+ fastify.decorate("twilio", {
10
+ async sendSMS(to: string, body: string) {
11
+ return await twilioClient.messages.create({
12
+ body,
13
+ to,
14
+ from: twilioOptions.phoneNumber,
15
+ });
16
+ },
17
+ });
18
+ console.info(" ✅ Twilio Enabled");
19
+ }
20
+ }
@@ -0,0 +1,10 @@
1
+ import { FastifyInstance } from "fastify";
2
+
3
+ export async function setupBugsnag(fastify: FastifyInstance, options: any) {
4
+ if (options.active !== false && options.apiKey) {
5
+ await fastify.register(import("fastify-bugsnag"), {
6
+ apiKey: options.apiKey,
7
+ });
8
+ console.info(" ✅ Bugsnag Enabled");
9
+ }
10
+ }
@@ -0,0 +1,13 @@
1
+ import { FastifyInstance } from "fastify";
2
+
3
+ export async function setupCors(fastify: FastifyInstance, options: any) {
4
+ if (options.active !== false) {
5
+ await fastify.register(import("@fastify/cors"), {
6
+ origin: options.origin || ["https://getx.io", "http://localhost:3000"],
7
+ credentials:
8
+ options.credentials !== undefined ? options.credentials : true,
9
+ methods: options.methods || ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
10
+ });
11
+ console.info(" ✅ CORS Enabled");
12
+ }
13
+ }
@@ -0,0 +1,24 @@
1
+ import { FastifyInstance } from "fastify";
2
+
3
+ export async function setupErrorHandler(
4
+ fastify: FastifyInstance,
5
+ fancyErrors: boolean
6
+ ) {
7
+ if (fancyErrors !== false) {
8
+ fastify.setErrorHandler((error, request, reply) => {
9
+ const statusCode = error.statusCode || 500;
10
+ const response = {
11
+ status: statusCode,
12
+ message: error.message || "Internal Server Error",
13
+ stack: process.env.NODE_ENV === "development" ? error.stack : undefined,
14
+ };
15
+
16
+ // Optional Bugsnag error reporting
17
+ if (fastify.bugsnag) fastify.bugsnag.notify(error);
18
+
19
+ fastify.log.error(response);
20
+ reply.status(statusCode).send(response);
21
+ });
22
+ console.info(" ✅ Fancy Errors Enabled");
23
+ }
24
+ }
@@ -0,0 +1,8 @@
1
+ import { FastifyInstance } from "fastify";
2
+
3
+ export async function setupMultipart(fastify: FastifyInstance, options: any) {
4
+ if (options.active !== false) {
5
+ await fastify.register(import("@fastify/multipart"), options);
6
+ console.info(" ✅ Multipart Enabled");
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import { FastifyInstance } from "fastify";
2
+
3
+ export async function setupRateLimit(fastify: FastifyInstance, options: any) {
4
+ if (options.active !== false) {
5
+ await fastify.register(import("@fastify/rate-limit"), options);
6
+ console.info(" ✅ Rate Limiting Enabled");
7
+ }
8
+ }
@@ -0,0 +1,11 @@
1
+ import { FastifyInstance } from "fastify";
2
+
3
+ export async function setupUnderPressure(
4
+ fastify: FastifyInstance,
5
+ options: any
6
+ ) {
7
+ if (options.active !== false) {
8
+ await fastify.register(import("@fastify/under-pressure"), options);
9
+ console.info(" ✅ Under Pressure Enabled");
10
+ }
11
+ }
@@ -0,0 +1,45 @@
1
+ import type { FastifyInstance, RouteOptions } from "fastify";
2
+
3
+ type HttpMethod = "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "clear";
4
+
5
+ const COLORS: Record<HttpMethod, number> = {
6
+ POST: 33,
7
+ GET: 32,
8
+ PUT: 34,
9
+ DELETE: 31,
10
+ PATCH: 90,
11
+ clear: 39,
12
+ };
13
+
14
+ // Function to colorize method and path names
15
+ export function colorize(method: string, text: string) {
16
+ const colorCode = COLORS[method as HttpMethod] || COLORS.clear;
17
+ return `\u001b[${colorCode}m${text}\u001b[${COLORS.clear}m`;
18
+ }
19
+
20
+ // Function to print the collected routes
21
+ export function printRoutes(routes: RouteOptions[], colors = true): void {
22
+ routes
23
+ .sort((a, b) => a.url.localeCompare(b.url))
24
+ .forEach(({ method, url }) => {
25
+ const methodsArray = Array.isArray(method) ? method : [method];
26
+ methodsArray
27
+ .filter((m) => m !== "HEAD")
28
+ .forEach((m) =>
29
+ console.info(
30
+ `${colors ? colorize(m, m) : m}\t${colors ? colorize(m, url) : url}`
31
+ )
32
+ );
33
+ });
34
+ }
35
+
36
+ // Helper function to capture all registered routes
37
+ export function captureRoutes(fastify: FastifyInstance): RouteOptions[] {
38
+ const routes: RouteOptions[] = [];
39
+
40
+ fastify.addHook("onRoute", (routeOptions) => {
41
+ routes.push(routeOptions);
42
+ });
43
+
44
+ return routes;
45
+ }
@@ -0,0 +1,8 @@
1
+ export function formatBytes(bytes: number, decimals = 2): string {
2
+ if (bytes === 0) return "0 Bytes";
3
+ const k = 1024;
4
+ const dm = decimals < 0 ? 0 : decimals;
5
+ const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB"];
6
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
7
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
8
+ }
@@ -0,0 +1,3 @@
1
+ import { randomUUID } from "uncrypto";
2
+
3
+ export const generateUUID = randomUUID;
@@ -0,0 +1,4 @@
1
+ import fs from "fs";
2
+ import util from "util";
3
+
4
+ export const statAsync = util.promisify(fs.stat);