@xenterprises/fastify-xconfig 1.0.0 → 1.0.1
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/.github/workflows/ci.yml +19 -0
- package/.taprc +3 -0
- package/README.md +18 -171
- package/index.d.ts +13 -0
- package/index.js +9 -0
- package/package.json +33 -39
- package/test/index.test-d.ts +13 -0
- package/test/index.test.js +14 -0
- package/test/xConfig.js +115 -0
- package/tsconfig.json +9 -0
- package/dist/integrations/cloudinary.d.ts +0 -1
- package/dist/integrations/cloudinary.js +0 -25
- package/dist/integrations/cloudinary.js.map +0 -1
- package/dist/integrations/prisma.d.ts +0 -1
- package/dist/integrations/prisma.js +0 -13
- package/dist/integrations/prisma.js.map +0 -1
- package/dist/integrations/sendgrid.d.ts +0 -1
- package/dist/integrations/sendgrid.js +0 -22
- package/dist/integrations/sendgrid.js.map +0 -1
- package/dist/integrations/stripe.d.ts +0 -1
- package/dist/integrations/stripe.js +0 -15
- package/dist/integrations/stripe.js.map +0 -1
- package/dist/integrations/twilio.d.ts +0 -1
- package/dist/integrations/twilio.js +0 -17
- package/dist/integrations/twilio.js.map +0 -1
- package/dist/middleware/bugsnag.d.ts +0 -2
- package/dist/middleware/bugsnag.js +0 -9
- package/dist/middleware/bugsnag.js.map +0 -1
- package/dist/middleware/cors.d.ts +0 -2
- package/dist/middleware/cors.js +0 -11
- package/dist/middleware/cors.js.map +0 -1
- package/dist/middleware/errorHandler.d.ts +0 -2
- package/dist/middleware/errorHandler.js +0 -19
- package/dist/middleware/errorHandler.js.map +0 -1
- package/dist/middleware/multipart.d.ts +0 -2
- package/dist/middleware/multipart.js +0 -7
- package/dist/middleware/multipart.js.map +0 -1
- package/dist/middleware/rateLimit.d.ts +0 -2
- package/dist/middleware/rateLimit.js +0 -7
- package/dist/middleware/rateLimit.js.map +0 -1
- package/dist/middleware/underPressure.d.ts +0 -2
- package/dist/middleware/underPressure.js +0 -7
- package/dist/middleware/underPressure.js.map +0 -1
- package/dist/utils/colorize.d.ts +0 -4
- package/dist/utils/colorize.js +0 -33
- package/dist/utils/colorize.js.map +0 -1
- package/dist/utils/formatBytes.d.ts +0 -1
- package/dist/utils/formatBytes.js +0 -10
- package/dist/utils/formatBytes.js.map +0 -1
- package/dist/utils/randomUUID.d.ts +0 -1
- package/dist/utils/randomUUID.js +0 -3
- package/dist/utils/randomUUID.js.map +0 -1
- package/dist/utils/statAsync.d.ts +0 -2
- package/dist/utils/statAsync.js +0 -4
- package/dist/utils/statAsync.js.map +0 -1
- package/dist/xConfig.d.ts +0 -3
- package/dist/xConfig.js +0 -9
- package/dist/xConfig.js.map +0 -1
- package/server/app.js +0 -85
- package/src/auth/admin.js +0 -181
- package/src/auth/portal.js +0 -177
- package/src/integrations/cloudinary.js +0 -98
- package/src/integrations/geocode.js +0 -43
- package/src/integrations/prisma.js +0 -30
- package/src/integrations/sendgrid.js +0 -58
- package/src/integrations/twilio.js +0 -146
- package/src/lifecycle/xFastifyAfter.js +0 -27
- package/src/middleware/bugsnag.js +0 -10
- package/src/middleware/cors.js +0 -10
- package/src/middleware/fancyErrors.js +0 -26
- package/src/middleware/multipart.js +0 -6
- package/src/middleware/rateLimit.js +0 -6
- package/src/middleware/underPressure.js +0 -6
- package/src/utils/colorize.js +0 -37
- package/src/utils/cookie.js +0 -5
- package/src/utils/formatBytes.js +0 -16
- package/src/utils/health.js +0 -126
- package/src/utils/xEcho.js +0 -12
- package/src/utils/xSlugify.js +0 -20
- package/src/utils/xUUID.js +0 -14
- package/src/xConfig.js +0 -117
- package/test/index.js +0 -17
- package/ts-reference/integrations/cloudinary.ts +0 -26
- package/ts-reference/integrations/prisma.ts +0 -13
- package/ts-reference/integrations/sendgrid.ts +0 -27
- package/ts-reference/integrations/stripe.ts +0 -15
- package/ts-reference/integrations/twilio.ts +0 -20
- package/ts-reference/middleware/bugsnag.ts +0 -10
- package/ts-reference/middleware/cors.ts +0 -13
- package/ts-reference/middleware/errorHandler.ts +0 -24
- package/ts-reference/middleware/multipart.ts +0 -8
- package/ts-reference/middleware/rateLimit.ts +0 -8
- package/ts-reference/middleware/underPressure.ts +0 -11
- package/ts-reference/utils/colorize.ts +0 -45
- package/ts-reference/utils/formatBytes.ts +0 -8
- package/ts-reference/utils/randomUUID.ts +0 -3
- package/ts-reference/utils/statAsync.ts +0 -4
- package/xConfigReference.js +0 -1526
- package/xConfigWorkingList.js +0 -720
package/src/utils/xSlugify.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// src/utils/xSlugify.js
|
|
2
|
-
import fp from "fastify-plugin";
|
|
3
|
-
|
|
4
|
-
async function xSlugify(fastify, options) {
|
|
5
|
-
fastify.decorate('slugify', (string) => {
|
|
6
|
-
return string
|
|
7
|
-
.toString()
|
|
8
|
-
.toLowerCase() // Convert to lowercase
|
|
9
|
-
.trim() // Trim leading and trailing spaces
|
|
10
|
-
.replace(/\s+/g, '-') // Replace spaces with -
|
|
11
|
-
.replace(/[^\w\-]+/g, '') // Remove all non-word characters (allow only letters, numbers, and dashes)
|
|
12
|
-
.replace(/\-\-+/g, '-') // Replace multiple dashes with a single dash
|
|
13
|
-
.replace(/^-+/, '') // Trim leading dashes
|
|
14
|
-
.replace(/-+$/, ''); // Trim trailing dashes
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export default fp(xSlugify, {
|
|
19
|
-
name: "xSlugify",
|
|
20
|
-
});
|
package/src/utils/xUUID.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import fp from "fastify-plugin";
|
|
2
|
-
import { randomUUID } from "uncrypto";
|
|
3
|
-
|
|
4
|
-
async function xUUID(fastify, options) {
|
|
5
|
-
fastify.decorate("generateUUID", randomUUID);
|
|
6
|
-
|
|
7
|
-
fastify.decorate('randomUUID', () => {
|
|
8
|
-
return randomUUID(); // Generate a UUID using uncrypto's randomUUID
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default fp(xUUID, {
|
|
13
|
-
name: "xUUID",
|
|
14
|
-
});
|
package/src/xConfig.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
// src/xConfig.js
|
|
2
|
-
import fp from "fastify-plugin";
|
|
3
|
-
const isProduction = process.env.NODE_ENV === 'production';
|
|
4
|
-
/*
|
|
5
|
-
* Auth
|
|
6
|
-
*/
|
|
7
|
-
import { setupAdminAuth } from "./auth/admin.js";
|
|
8
|
-
import { setupAuth } from "./auth/portal.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/*
|
|
12
|
-
* Integrations
|
|
13
|
-
*/
|
|
14
|
-
import { setupPrisma } from "./integrations/prisma.js";
|
|
15
|
-
import { setupSendgrid } from "./integrations/sendgrid.js";
|
|
16
|
-
import { setupTwilio } from "./integrations/twilio.js";
|
|
17
|
-
import { setupCloudinary } from "./integrations/cloudinary.js";
|
|
18
|
-
import { setupGeocode } from "./integrations/geocode.js";
|
|
19
|
-
|
|
20
|
-
/*
|
|
21
|
-
* Lifecycle
|
|
22
|
-
*/
|
|
23
|
-
import { xFastifyAfter } from "./lifecycle/xFastifyAfter.js";
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
/*
|
|
27
|
-
* Middleware
|
|
28
|
-
*/
|
|
29
|
-
import { setupCors } from './middleware/cors.js';
|
|
30
|
-
import { setupUnderPressure } from './middleware/underPressure.js';
|
|
31
|
-
import { setupRateLimit } from './middleware/rateLimit.js';
|
|
32
|
-
import { setupMultipart } from './middleware/multipart.js';
|
|
33
|
-
import { setupBugsnag } from './middleware/bugsnag.js';
|
|
34
|
-
import { setupFancyErrors } from './middleware/fancyErrors.js'
|
|
35
|
-
|
|
36
|
-
/*
|
|
37
|
-
* Utils
|
|
38
|
-
*/
|
|
39
|
-
import xEcho from "./utils/xEcho.js";
|
|
40
|
-
import { setupHealth } from "./utils/health.js";
|
|
41
|
-
import xSlugify from "./utils/xSlugify.js";
|
|
42
|
-
import xUUID from "./utils/xUUID.js";
|
|
43
|
-
import { setupCookie } from "./utils/cookie.js";
|
|
44
|
-
|
|
45
|
-
async function xConfig(fastify, options) {
|
|
46
|
-
const {
|
|
47
|
-
professional = false,
|
|
48
|
-
fancyErrors = true,
|
|
49
|
-
prisma: prismaOptions = {},
|
|
50
|
-
bugsnag: bugsnagOptions = {},
|
|
51
|
-
stripe: stripeOptions = {},
|
|
52
|
-
sendGrid: sendGridOptions = {},
|
|
53
|
-
twilio: twilioOptions = {},
|
|
54
|
-
cloudinary: cloudinaryOptions = {},
|
|
55
|
-
auth: authOptions = {},
|
|
56
|
-
cors: corsOptions = {},
|
|
57
|
-
underPressure: underPressureOptions = {},
|
|
58
|
-
multipart: multipartOptions = {},
|
|
59
|
-
rateLimit: rateLimitOptions = {},
|
|
60
|
-
geocode: geocodeOptions = {},
|
|
61
|
-
} = options;
|
|
62
|
-
|
|
63
|
-
// add starting console with emoji
|
|
64
|
-
console.info("\n 🌮 Starting xConfig...\n");
|
|
65
|
-
|
|
66
|
-
/*
|
|
67
|
-
===== LIST ROUTES =====
|
|
68
|
-
Moved the onRoute hook to the top to capture all routes.
|
|
69
|
-
*/
|
|
70
|
-
const routes = [];
|
|
71
|
-
fastify.addHook("onRoute", (r) => routes.push(r));
|
|
72
|
-
|
|
73
|
-
/*
|
|
74
|
-
* Integrations
|
|
75
|
-
*/
|
|
76
|
-
await setupPrisma(fastify, prismaOptions);
|
|
77
|
-
await setupSendgrid(fastify, sendGridOptions);
|
|
78
|
-
await setupTwilio(fastify, twilioOptions);
|
|
79
|
-
await setupCloudinary(fastify, cloudinaryOptions);
|
|
80
|
-
await setupGeocode(fastify, geocodeOptions);
|
|
81
|
-
|
|
82
|
-
/*
|
|
83
|
-
* Middleware
|
|
84
|
-
*/
|
|
85
|
-
await setupCors(fastify, corsOptions);
|
|
86
|
-
await setupUnderPressure(fastify, underPressureOptions);
|
|
87
|
-
await setupRateLimit(fastify, rateLimitOptions);
|
|
88
|
-
await setupMultipart(fastify, multipartOptions);
|
|
89
|
-
await setupBugsnag(fastify, bugsnagOptions);
|
|
90
|
-
await setupFancyErrors(fastify, fancyErrors);
|
|
91
|
-
fastify.register(import('@fastify/sensible'))
|
|
92
|
-
|
|
93
|
-
/*
|
|
94
|
-
* Utils
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
fastify.register(xEcho);
|
|
98
|
-
fastify.register(xSlugify);
|
|
99
|
-
fastify.register(xUUID);
|
|
100
|
-
await setupHealth(fastify);
|
|
101
|
-
await setupCookie(fastify, authOptions);
|
|
102
|
-
|
|
103
|
-
/*
|
|
104
|
-
* Auth
|
|
105
|
-
*/
|
|
106
|
-
await setupAuth(fastify, authOptions);
|
|
107
|
-
await setupAdminAuth(fastify, authOptions);
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
//.after() method to ensure this runs after all plugins are registered.
|
|
111
|
-
await xFastifyAfter(fastify, { professional, routes });
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export default fp(xConfig, {
|
|
116
|
-
name: "xConfig",
|
|
117
|
-
});
|
package/test/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// test.js
|
|
2
|
-
const fastify = require('fastify')();
|
|
3
|
-
const myPlugin = require('../src/xConfig');
|
|
4
|
-
|
|
5
|
-
fastify.register(myPlugin);
|
|
6
|
-
|
|
7
|
-
fastify.get('/', async (request, reply) => {
|
|
8
|
-
return { message: fastify.myPluginMethod() };
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
fastify.listen(3000, (err, address) => {
|
|
12
|
-
if (err) {
|
|
13
|
-
console.error(err);
|
|
14
|
-
process.exit(1);
|
|
15
|
-
}
|
|
16
|
-
console.log(`Server running at ${address}`);
|
|
17
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
}
|