@xenterprises/fastify-xconfig 1.0.1 → 1.1.0
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/README.md +171 -18
- package/dist/integrations/cloudinary.d.ts +1 -0
- package/dist/integrations/cloudinary.js +25 -0
- package/dist/integrations/cloudinary.js.map +1 -0
- package/dist/integrations/prisma.d.ts +1 -0
- package/dist/integrations/prisma.js +13 -0
- package/dist/integrations/prisma.js.map +1 -0
- package/dist/integrations/sendgrid.d.ts +1 -0
- package/dist/integrations/sendgrid.js +22 -0
- package/dist/integrations/sendgrid.js.map +1 -0
- package/dist/integrations/stripe.d.ts +1 -0
- package/dist/integrations/stripe.js +15 -0
- package/dist/integrations/stripe.js.map +1 -0
- package/dist/integrations/twilio.d.ts +1 -0
- package/dist/integrations/twilio.js +17 -0
- package/dist/integrations/twilio.js.map +1 -0
- package/dist/middleware/bugsnag.d.ts +2 -0
- package/dist/middleware/bugsnag.js +9 -0
- package/dist/middleware/bugsnag.js.map +1 -0
- package/dist/middleware/cors.d.ts +2 -0
- package/dist/middleware/cors.js +11 -0
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/errorHandler.d.ts +2 -0
- package/dist/middleware/errorHandler.js +19 -0
- package/dist/middleware/errorHandler.js.map +1 -0
- package/dist/middleware/multipart.d.ts +2 -0
- package/dist/middleware/multipart.js +7 -0
- package/dist/middleware/multipart.js.map +1 -0
- package/dist/middleware/rateLimit.d.ts +2 -0
- package/dist/middleware/rateLimit.js +7 -0
- package/dist/middleware/rateLimit.js.map +1 -0
- package/dist/middleware/underPressure.d.ts +2 -0
- package/dist/middleware/underPressure.js +7 -0
- package/dist/middleware/underPressure.js.map +1 -0
- package/dist/utils/colorize.d.ts +4 -0
- package/dist/utils/colorize.js +33 -0
- package/dist/utils/colorize.js.map +1 -0
- package/dist/utils/formatBytes.d.ts +1 -0
- package/dist/utils/formatBytes.js +10 -0
- package/dist/utils/formatBytes.js.map +1 -0
- package/dist/utils/randomUUID.d.ts +1 -0
- package/dist/utils/randomUUID.js +3 -0
- package/dist/utils/randomUUID.js.map +1 -0
- package/dist/utils/statAsync.d.ts +2 -0
- package/dist/utils/statAsync.js +4 -0
- package/dist/utils/statAsync.js.map +1 -0
- package/dist/xConfig.d.ts +3 -0
- package/dist/xConfig.js +9 -0
- package/dist/xConfig.js.map +1 -0
- package/package.json +39 -33
- package/server/app.js +78 -0
- package/src/auth/admin.js +181 -0
- package/src/auth/portal.js +177 -0
- package/src/integrations/cloudinary.js +98 -0
- package/src/integrations/geocode.js +43 -0
- package/src/integrations/prisma.js +30 -0
- package/src/integrations/sendgrid.js +58 -0
- package/src/integrations/twilio.js +146 -0
- package/src/lifecycle/xFastifyAfter.js +27 -0
- package/src/middleware/bugsnag.js +10 -0
- package/src/middleware/cors.js +10 -0
- package/src/middleware/fancyErrors.js +26 -0
- package/src/middleware/multipart.js +6 -0
- package/src/middleware/rateLimit.js +6 -0
- package/src/middleware/underPressure.js +6 -0
- package/src/utils/colorize.js +37 -0
- package/src/utils/cookie.js +5 -0
- package/src/utils/formatBytes.js +16 -0
- package/src/utils/health.js +126 -0
- package/src/utils/xEcho.js +12 -0
- package/src/utils/xSlugify.js +20 -0
- package/src/utils/xUUID.js +14 -0
- package/src/xConfig.js +117 -0
- package/test/index.js +17 -0
- package/ts-reference/integrations/cloudinary.ts +26 -0
- package/ts-reference/integrations/prisma.ts +13 -0
- package/ts-reference/integrations/sendgrid.ts +27 -0
- package/ts-reference/integrations/stripe.ts +15 -0
- package/ts-reference/integrations/twilio.ts +20 -0
- package/ts-reference/middleware/bugsnag.ts +10 -0
- package/ts-reference/middleware/cors.ts +13 -0
- package/ts-reference/middleware/errorHandler.ts +24 -0
- package/ts-reference/middleware/multipart.ts +8 -0
- package/ts-reference/middleware/rateLimit.ts +8 -0
- package/ts-reference/middleware/underPressure.ts +11 -0
- package/ts-reference/utils/colorize.ts +45 -0
- package/ts-reference/utils/formatBytes.ts +8 -0
- package/ts-reference/utils/randomUUID.ts +3 -0
- package/ts-reference/utils/statAsync.ts +4 -0
- package/tsconfig.json +13 -8
- package/xConfigReference.js +1526 -0
- package/xConfigWorkingList.js +720 -0
- package/.github/workflows/ci.yml +0 -19
- package/.taprc +0 -3
- package/index.d.ts +0 -13
- package/index.js +0 -9
- package/test/index.test-d.ts +0 -13
- package/test/index.test.js +0 -14
- package/test/xConfig.js +0 -115
|
@@ -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
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
"extends": "fastify-tsconfig",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist",
|
|
5
|
+
"sourceMap": true,
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"module": "NodeNext",
|
|
8
|
+
"target": "ES2022",
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/**/*.ts", "ts-reference/**/*.ts"],
|
|
13
|
+
"exclude": ["node_modules", "dist"]
|
|
14
|
+
}
|