@xenterprises/fastify-xconfig 0.0.10 → 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 -134
- 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 -92
- package/src/auth/admin.js +0 -241
- package/src/auth/portal.js +0 -286
- 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 -1495
- package/xConfigWorkingList.js +0 -720
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
name: CI workflow
|
|
2
|
+
on: [push, pull_request]
|
|
3
|
+
jobs:
|
|
4
|
+
test:
|
|
5
|
+
runs-on: ${{ matrix.os }}
|
|
6
|
+
strategy:
|
|
7
|
+
matrix:
|
|
8
|
+
node-version: [10.x, 12.x, 14.x]
|
|
9
|
+
os: [ubuntu-latest, windows-latest]
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v2
|
|
12
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
13
|
+
uses: actions/setup-node@v1
|
|
14
|
+
with:
|
|
15
|
+
node-version: ${{ matrix.node-version }}
|
|
16
|
+
- name: Install Dependencies
|
|
17
|
+
run: npm install --ignore-scripts
|
|
18
|
+
- name: Test
|
|
19
|
+
run: npm run test
|
package/.taprc
ADDED
package/README.md
CHANGED
|
@@ -1,145 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# __MY_PLUGIN__
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](http://standardjs.com/) 
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
Supports Fastify versions `4.x`
|
|
6
7
|
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
- Provides authentication setup for both Admin and User, with JWT-based token handling.
|
|
12
|
-
- Includes Prisma database connection and gracefully handles disconnecting on server shutdown.
|
|
13
|
-
- Adds health check routes and resource usage monitoring, with environment validation.
|
|
14
|
-
- Customizes route listing and applies various performance and security options.
|
|
8
|
+
## Install
|
|
9
|
+
```
|
|
10
|
+
npm i __MY_PLUGIN__
|
|
11
|
+
```
|
|
15
12
|
|
|
16
13
|
## Usage
|
|
14
|
+
Require `__MY_PLUGIN__` and register.
|
|
15
|
+
```js
|
|
16
|
+
const fastify = require('fastify')()
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
fastify.register(require('__MY_PLUGIN__'), {
|
|
19
|
+
// put your options here
|
|
20
|
+
})
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
import Fastify from "fastify";
|
|
24
|
-
import xConfig from "./path/to/xConfig";
|
|
25
|
-
|
|
26
|
-
const fastify = Fastify();
|
|
27
|
-
fastify.register(xConfig, {
|
|
28
|
-
prisma: { active: true },
|
|
29
|
-
sendGrid: { apiKey: "your-sendgrid-api-key" },
|
|
30
|
-
twilio: {
|
|
31
|
-
accountSid: "your-account-sid",
|
|
32
|
-
authToken: "your-auth-token",
|
|
33
|
-
phoneNumber: "your-twilio-number",
|
|
34
|
-
},
|
|
35
|
-
cloudinary: {
|
|
36
|
-
cloudName: "your-cloud-name",
|
|
37
|
-
apiKey: "your-api-key",
|
|
38
|
-
apiSecret: "your-api-secret",
|
|
39
|
-
},
|
|
40
|
-
auth: {
|
|
41
|
-
admin: { secret: "admin-jwt-secret", expiresIn: "1h" },
|
|
42
|
-
user: { secret: "user-jwt-secret", expiresIn: "1h" },
|
|
43
|
-
},
|
|
44
|
-
cors: { origin: ["https://your-frontend.com"], credentials: true },
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
fastify.listen({ port: 3000 });
|
|
22
|
+
fastify.listen({ port: 3000 })
|
|
48
23
|
```
|
|
49
24
|
|
|
50
|
-
##
|
|
51
|
-
|
|
52
|
-
- **prisma**: Prisma Client configuration (optional).
|
|
53
|
-
- **sendGrid**: SendGrid configuration for email services (optional).
|
|
54
|
-
- **twilio**: Twilio configuration for SMS services (optional).
|
|
55
|
-
- **cloudinary**: Cloudinary configuration for media upload services (optional).
|
|
56
|
-
- **auth**: Authentication configuration for Admin and User JWT tokens (optional).
|
|
57
|
-
- **cors**: CORS configuration (optional).
|
|
58
|
-
- **rateLimit**: Rate-limiting options (optional).
|
|
59
|
-
- **multipart**: Multipart handling options for file uploads (optional).
|
|
60
|
-
- **bugsnag**: Bugsnag error reporting configuration (optional).
|
|
61
|
-
- **underPressure**: Under Pressure plugin options for monitoring and throttling under load (optional).
|
|
62
|
-
|
|
63
|
-
## Authentication
|
|
64
|
-
|
|
65
|
-
The plugin provides both Admin and User authentication with JWT support. It sets secure cookies, manages token refresh, and validates tokens on protected routes.
|
|
66
|
-
|
|
67
|
-
- `/admin/auth/login` for admin login.
|
|
68
|
-
- `/portal/auth/login` for user login.
|
|
69
|
-
- `/admin/auth/me` to check authentication status for admins.
|
|
70
|
-
- `/portal/auth/me` to check authentication status for users.
|
|
71
|
-
|
|
72
|
-
## Health Check
|
|
73
|
-
|
|
74
|
-
The `/health` route provides a health check with details about uptime, memory usage, CPU load, database and Redis status, and environment variable validation.
|
|
75
|
-
|
|
76
|
-
## Services
|
|
25
|
+
## Acknowledgements
|
|
77
26
|
|
|
78
|
-
|
|
79
|
-
- **SendGrid**: Provides an email sending service through SendGrid, integrated with the Fastify instance.
|
|
80
|
-
- **Twilio**: Provides SMS sending and phone number validation services.
|
|
81
|
-
- **Cloudinary**: Handles file uploads to Cloudinary and deletion of media files.
|
|
82
|
-
- **Authentication**: JWT-based authentication for Admin and User with token-based sessions.
|
|
27
|
+
## License
|
|
83
28
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
- **onRequest**: Validates JWT tokens for protected routes.
|
|
87
|
-
- **onClose**: Gracefully disconnects Prisma and other services on server shutdown.
|
|
88
|
-
|
|
89
|
-
## Error Handling
|
|
90
|
-
|
|
91
|
-
Enhanced error handling is enabled by default, showing detailed error messages during development. Bugsnag integration is optional for real-time error reporting.
|
|
92
|
-
|
|
93
|
-
## Route Listing
|
|
94
|
-
|
|
95
|
-
The plugin prints all routes after registration, color-coded by HTTP method, unless disabled.
|
|
96
|
-
|
|
97
|
-
## Dependencies
|
|
98
|
-
|
|
99
|
-
The following dependencies are used for various services and integrations:
|
|
100
|
-
|
|
101
|
-
- Prisma Client, SendGrid, Twilio, Cloudinary, Fastify CORS, Fastify Rate Limit, Fastify Multipart, Fastify Under Pressure, and Fastify Bugsnag.
|
|
102
|
-
|
|
103
|
-
## Notes
|
|
104
|
-
|
|
105
|
-
- Environment variables such as `DATABASE_URL`, `ADMIN_JWT_SECRET`, and `USER_JWT_SECRET` are expected to be set.
|
|
106
|
-
- Services like SendGrid, Twilio, and Cloudinary require API keys to be passed via the options.
|
|
107
|
-
- This plugin is highly customizable, with options to enable/disable each feature.
|
|
108
|
-
|
|
109
|
-
## Example Configuration
|
|
110
|
-
|
|
111
|
-
```javascript
|
|
112
|
-
fastify.register(xConfig, {
|
|
113
|
-
prisma: { active: true },
|
|
114
|
-
sendGrid: { apiKey: "SG.your_api_key" },
|
|
115
|
-
twilio: {
|
|
116
|
-
accountSid: "ACxxxxxxxxxxxxxxxx",
|
|
117
|
-
authToken: "your_auth_token",
|
|
118
|
-
phoneNumber: "+1234567890",
|
|
119
|
-
},
|
|
120
|
-
cloudinary: {
|
|
121
|
-
cloudName: "your-cloud-name",
|
|
122
|
-
apiKey: "your-api-key",
|
|
123
|
-
apiSecret: "your-api-secret",
|
|
124
|
-
},
|
|
125
|
-
auth: {
|
|
126
|
-
admin: {
|
|
127
|
-
secret: "admin-secret",
|
|
128
|
-
expiresIn: "1h",
|
|
129
|
-
cookieOptions: {
|
|
130
|
-
name: "adminCookie",
|
|
131
|
-
refreshTokenName: "adminRefreshToken",
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
user: {
|
|
135
|
-
secret: "user-secret",
|
|
136
|
-
expiresIn: "1h",
|
|
137
|
-
cookieOptions: {
|
|
138
|
-
name: "userCookie",
|
|
139
|
-
refreshTokenName: "userRefreshToken",
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
},
|
|
143
|
-
cors: { origin: ["https://your-frontend.com"], credentials: true },
|
|
144
|
-
});
|
|
145
|
-
```
|
|
29
|
+
Licensed under [MIT](./LICENSE).<br/>
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FastifyPluginCallback } from 'fastify'
|
|
2
|
+
|
|
3
|
+
declare module 'fastify' {
|
|
4
|
+
export interface FastifyInstance {
|
|
5
|
+
// This is an example decorator type added to fastify
|
|
6
|
+
exampleDecorator: () => string
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare const example: FastifyPluginCallback<() => string>
|
|
11
|
+
|
|
12
|
+
export { example }
|
|
13
|
+
export default example
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,48 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xenterprises/fastify-xconfig",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "[](http://standardjs.com/) ",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"directories": {
|
|
7
|
+
"test": "test"
|
|
8
|
+
},
|
|
7
9
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
10
|
+
"test": "npm run lint && npm run unit && npm run test:typescript",
|
|
11
|
+
"lint": "standard && npm run lint:typescript",
|
|
12
|
+
"lint:typescript": "ts-standard",
|
|
13
|
+
"test:typescript": "tsd",
|
|
14
|
+
"unit": "node --test"
|
|
11
15
|
},
|
|
16
|
+
"keywords": [],
|
|
12
17
|
"author": "Tim Mushen",
|
|
13
|
-
"license": "
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"types": "index.d.ts",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"fastify-plugin": "^4.0.0"
|
|
22
|
+
},
|
|
14
23
|
"devDependencies": {
|
|
15
|
-
"@types/node": "^
|
|
16
|
-
"c8": "^9.0.0",
|
|
17
|
-
"fastify": "^4.28.1",
|
|
18
|
-
"fastify-plugin": "^4.0.0",
|
|
24
|
+
"@types/node": "^20.4.4",
|
|
19
25
|
"fastify-tsconfig": "^2.0.0",
|
|
20
|
-
"
|
|
26
|
+
"standard": "^17.0.0",
|
|
27
|
+
"ts-standard": "^12.0.1",
|
|
28
|
+
"tsd": "^0.31.0",
|
|
29
|
+
"typescript": "^5.2.2"
|
|
21
30
|
},
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"@sendgrid/mail": "^8.1.3",
|
|
34
|
-
"bcrypt": "^5.1.1",
|
|
35
|
-
"check-disk-space": "^3.4.0",
|
|
36
|
-
"fastify": "^4.28.1",
|
|
37
|
-
"fastify-bugsnag": "^4.1.4",
|
|
38
|
-
"fastify-cli": "^6.2.1",
|
|
39
|
-
"fastify-cloudinary": "^2.0.0",
|
|
40
|
-
"fastify-list-routes": "^1.0.0",
|
|
41
|
-
"fastify-multipart": "^5.4.0",
|
|
42
|
-
"fastify-plugin": "^4.0.0",
|
|
43
|
-
"fastify-rate-limit": "^5.9.0",
|
|
44
|
-
"stripe": "^16.12.0",
|
|
45
|
-
"twilio": "^5.3.2",
|
|
46
|
-
"uncrypto": "^0.1.3"
|
|
47
|
-
}
|
|
31
|
+
"tsd": {
|
|
32
|
+
"directory": "test"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/xenterprises/module-fastify-xconfig.git"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/xenterprises/module-fastify-xconfig/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/xenterprises/module-fastify-xconfig#readme"
|
|
48
42
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fastify from 'fastify'
|
|
2
|
+
import example from '..'
|
|
3
|
+
import { expectType } from 'tsd'
|
|
4
|
+
|
|
5
|
+
let app
|
|
6
|
+
try {
|
|
7
|
+
app = fastify()
|
|
8
|
+
void app.ready()
|
|
9
|
+
void app.register(example)
|
|
10
|
+
expectType<() => string>(app.exampleDecorator)
|
|
11
|
+
} catch (err) {
|
|
12
|
+
console.error(err)
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
const assert = require('node:assert')
|
|
5
|
+
|
|
6
|
+
test('should register the correct decorator', async t => {
|
|
7
|
+
const app = require('fastify')()
|
|
8
|
+
|
|
9
|
+
app.register(require('..'))
|
|
10
|
+
|
|
11
|
+
await app.ready()
|
|
12
|
+
|
|
13
|
+
assert.equal(app.exampleDecorator(), 'decorated')
|
|
14
|
+
})
|
package/test/xConfig.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import * as assert from 'node:assert';
|
|
3
|
+
import Fastify from 'fastify';
|
|
4
|
+
import xConfig from '../path/to/xConfig.js'; // Adjust the path accordingly
|
|
5
|
+
|
|
6
|
+
test('xConfig plugin works standalone', async (t) => {
|
|
7
|
+
const fastify = Fastify();
|
|
8
|
+
|
|
9
|
+
// Register the xConfig plugin with necessary options
|
|
10
|
+
await fastify.register(xConfig, {
|
|
11
|
+
prisma: { active: false }, // Mocked or disabled for testing
|
|
12
|
+
sendGrid: { active: false },
|
|
13
|
+
twilio: { active: false },
|
|
14
|
+
stripe: { active: false },
|
|
15
|
+
auth: {
|
|
16
|
+
admin: { active: false },
|
|
17
|
+
user: { active: false }
|
|
18
|
+
},
|
|
19
|
+
cors: { active: true },
|
|
20
|
+
underPressure: { active: false },
|
|
21
|
+
rateLimit: { active: false }
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
await fastify.ready();
|
|
25
|
+
|
|
26
|
+
// Test if the plugin registered correctly
|
|
27
|
+
assert.ok(fastify.prisma === undefined, 'Prisma should not be registered');
|
|
28
|
+
assert.ok(fastify.sendGrid === undefined, 'SendGrid should not be registered');
|
|
29
|
+
assert.ok(fastify.stripe === undefined, 'Stripe should not be registered');
|
|
30
|
+
|
|
31
|
+
// Test if health route is registered and works
|
|
32
|
+
const response = await fastify.inject({
|
|
33
|
+
method: 'GET',
|
|
34
|
+
url: '/health'
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const body = JSON.parse(response.body);
|
|
38
|
+
assert.strictEqual(response.statusCode, 200, 'Health route should return status 200');
|
|
39
|
+
assert.strictEqual(body.status, 'healthy', 'Health status should be healthy');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('xConfig CORS is enabled', async (t) => {
|
|
43
|
+
const fastify = Fastify();
|
|
44
|
+
|
|
45
|
+
// Register the xConfig plugin with CORS enabled
|
|
46
|
+
await fastify.register(xConfig, {
|
|
47
|
+
cors: {
|
|
48
|
+
active: true,
|
|
49
|
+
origin: ['http://localhost:3000'],
|
|
50
|
+
credentials: true
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await fastify.ready();
|
|
55
|
+
|
|
56
|
+
// Check if the CORS headers are returned correctly
|
|
57
|
+
const response = await fastify.inject({
|
|
58
|
+
method: 'OPTIONS',
|
|
59
|
+
url: '/',
|
|
60
|
+
headers: {
|
|
61
|
+
origin: 'http://localhost:3000'
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
assert.strictEqual(response.headers['access-control-allow-origin'], 'http://localhost:3000');
|
|
66
|
+
assert.strictEqual(response.headers['access-control-allow-credentials'], 'true');
|
|
67
|
+
assert.strictEqual(response.statusCode, 204, 'CORS preflight request should return 204');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('xConfig routes are listed correctly', async (t) => {
|
|
71
|
+
const fastify = Fastify();
|
|
72
|
+
|
|
73
|
+
// Register xConfig plugin with a dummy route to check if routes are listed
|
|
74
|
+
await fastify.register(xConfig, {
|
|
75
|
+
prisma: { active: false },
|
|
76
|
+
auth: { admin: { active: false }, user: { active: false } }
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
fastify.get('/test-route', async (request, reply) => {
|
|
80
|
+
reply.send({ message: 'Test Route' });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await fastify.ready();
|
|
84
|
+
|
|
85
|
+
// Test if the '/test-route' is available
|
|
86
|
+
const response = await fastify.inject({
|
|
87
|
+
method: 'GET',
|
|
88
|
+
url: '/test-route'
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
assert.strictEqual(response.statusCode, 200, 'Test route should return 200');
|
|
92
|
+
assert.strictEqual(JSON.parse(response.body).message, 'Test Route');
|
|
93
|
+
|
|
94
|
+
// Check if routes are printed correctly (this is more of a manual log observation)
|
|
95
|
+
fastify.ready(() => {
|
|
96
|
+
fastify.printRoutes(); // This will print routes to console
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('xConfig fails if required options are missing', async (t) => {
|
|
101
|
+
const fastify = Fastify();
|
|
102
|
+
|
|
103
|
+
// Test missing required options like SendGrid API Key
|
|
104
|
+
try {
|
|
105
|
+
await fastify.register(xConfig, {
|
|
106
|
+
sendGrid: { active: true }
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
await fastify.ready();
|
|
110
|
+
assert.fail('Should have thrown an error due to missing SendGrid API Key');
|
|
111
|
+
} catch (err) {
|
|
112
|
+
assert.ok(err instanceof Error, 'Should throw an error');
|
|
113
|
+
assert.strictEqual(err.message, 'SendGrid API key must be provided.');
|
|
114
|
+
}
|
|
115
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setupCloudinary(fastify: any, cloudinaryOptions: any): Promise<void>;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { v2 as Cloudinary } from "cloudinary";
|
|
2
|
-
export async function setupCloudinary(fastify, cloudinaryOptions) {
|
|
3
|
-
if (cloudinaryOptions.active !== false) {
|
|
4
|
-
Cloudinary.config({
|
|
5
|
-
cloud_name: cloudinaryOptions.cloudName,
|
|
6
|
-
api_key: cloudinaryOptions.apiKey,
|
|
7
|
-
api_secret: cloudinaryOptions.apiSecret,
|
|
8
|
-
});
|
|
9
|
-
fastify.decorate("cloudinary", {
|
|
10
|
-
async upload(fileStream, options = {}) {
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
const uploadStream = Cloudinary.uploader.upload_stream(options, (error, result) => {
|
|
13
|
-
if (error)
|
|
14
|
-
reject(error);
|
|
15
|
-
else
|
|
16
|
-
resolve(result);
|
|
17
|
-
});
|
|
18
|
-
fileStream.pipe(uploadStream);
|
|
19
|
-
});
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
console.info(" ✅ Cloudinary Enabled");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
//# sourceMappingURL=cloudinary.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cloudinary.js","sourceRoot":"","sources":["../../src/integrations/cloudinary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,UAAU,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,OAAY,EAAE,iBAAsB;IACxE,IAAI,iBAAiB,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QACvC,UAAU,CAAC,MAAM,CAAC;YAChB,UAAU,EAAE,iBAAiB,CAAC,SAAS;YACvC,OAAO,EAAE,iBAAiB,CAAC,MAAM;YACjC,UAAU,EAAE,iBAAiB,CAAC,SAAS;SACxC,CAAC,CAAC;QACH,OAAO,CAAC,QAAQ,CAAC,YAAY,EAAE;YAC7B,KAAK,CAAC,MAAM,CAAC,UAAe,EAAE,OAAO,GAAG,EAAE;gBACxC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;oBACrC,MAAM,YAAY,GAAG,UAAU,CAAC,QAAQ,CAAC,aAAa,CACpD,OAAO,EACP,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;wBAChB,IAAI,KAAK;4BAAE,MAAM,CAAC,KAAK,CAAC,CAAC;;4BACpB,OAAO,CAAC,MAAM,CAAC,CAAC;oBACvB,CAAC,CACF,CAAC;oBACF,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;gBAChC,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;IAC1C,CAAC;AACH,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setupPrisma(fastify: any, prismaOptions: any): Promise<void>;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { PrismaClient } from "@prisma/client";
|
|
2
|
-
export async function setupPrisma(fastify, prismaOptions) {
|
|
3
|
-
if (prismaOptions.active !== false) {
|
|
4
|
-
const prisma = new PrismaClient(prismaOptions);
|
|
5
|
-
await prisma.$connect();
|
|
6
|
-
fastify.decorate("prisma", prisma);
|
|
7
|
-
fastify.addHook("onClose", async () => {
|
|
8
|
-
await fastify.prisma.$disconnect();
|
|
9
|
-
});
|
|
10
|
-
console.info(" ✅ Prisma Enabled");
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
//# sourceMappingURL=prisma.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"prisma.js","sourceRoot":"","sources":["../../src/integrations/prisma.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAAY,EAAE,aAAkB;IAChE,IAAI,aAAa,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QACnC,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,aAAa,CAAC,CAAC;QAC/C,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAC;QACxB,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QACnC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,KAAK,IAAI,EAAE;YACpC,MAAM,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;QACrC,CAAC,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;IACtC,CAAC;AACH,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setupSendGrid(fastify: any, sendGridOptions: any): Promise<void>;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import Sendgrid from "@sendgrid/mail";
|
|
2
|
-
export async function setupSendGrid(fastify, sendGridOptions) {
|
|
3
|
-
if (sendGridOptions.active !== false) {
|
|
4
|
-
if (!sendGridOptions.apiKey)
|
|
5
|
-
throw new Error("SendGrid API key must be provided.");
|
|
6
|
-
Sendgrid.setApiKey(sendGridOptions.apiKey);
|
|
7
|
-
fastify.decorate("sendgrid", {
|
|
8
|
-
async sendEmail(to, subject, templateId, dynamicTemplateData) {
|
|
9
|
-
const msg = {
|
|
10
|
-
to,
|
|
11
|
-
from: sendGridOptions.fromEmail,
|
|
12
|
-
subject,
|
|
13
|
-
templateId,
|
|
14
|
-
dynamicTemplateData,
|
|
15
|
-
};
|
|
16
|
-
await Sendgrid.send(msg);
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
console.info(" ✅ SendGrid Enabled");
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
//# sourceMappingURL=sendgrid.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sendgrid.js","sourceRoot":"","sources":["../../src/integrations/sendgrid.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAEtC,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,OAAY,EAAE,eAAoB;IACpE,IAAI,eAAe,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QACrC,IAAI,CAAC,eAAe,CAAC,MAAM;YACzB,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;QACxD,QAAQ,CAAC,SAAS,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;QAC3C,OAAO,CAAC,QAAQ,CAAC,UAAU,EAAE;YAC3B,KAAK,CAAC,SAAS,CACb,EAAU,EACV,OAAe,EACf,UAAkB,EAClB,mBAAwB;gBAExB,MAAM,GAAG,GAAG;oBACV,EAAE;oBACF,IAAI,EAAE,eAAe,CAAC,SAAS;oBAC/B,OAAO;oBACP,UAAU;oBACV,mBAAmB;iBACpB,CAAC;gBACF,MAAM,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC3B,CAAC;SACF,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;IACxC,CAAC;AACH,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setupStripe(fastify: any, stripeOptions: any): Promise<void>;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import Stripe from "stripe";
|
|
2
|
-
export async function setupStripe(fastify, stripeOptions) {
|
|
3
|
-
if (stripeOptions.active !== false) {
|
|
4
|
-
const stripeClient = new Stripe(stripeOptions.apiKey, {
|
|
5
|
-
apiVersion: "2024-06-20",
|
|
6
|
-
});
|
|
7
|
-
fastify.decorate("stripe", {
|
|
8
|
-
async createPaymentIntent(amount, currency = "usd") {
|
|
9
|
-
return await stripeClient.paymentIntents.create({ amount, currency });
|
|
10
|
-
},
|
|
11
|
-
});
|
|
12
|
-
console.info(" ✅ Stripe Enabled");
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
//# sourceMappingURL=stripe.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stripe.js","sourceRoot":"","sources":["../../src/integrations/stripe.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAAY,EAAE,aAAkB;IAChE,IAAI,aAAa,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QACnC,MAAM,YAAY,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE;YACpD,UAAU,EAAE,YAAY;SACzB,CAAC,CAAC;QACH,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE;YACzB,KAAK,CAAC,mBAAmB,CAAC,MAAc,EAAE,QAAQ,GAAG,KAAK;gBACxD,OAAO,MAAM,YAAY,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;YACxE,CAAC;SACF,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;IACtC,CAAC;AACH,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setupTwilio(fastify: any, twilioOptions: any): Promise<void>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import Twilio from "twilio";
|
|
2
|
-
export async function setupTwilio(fastify, twilioOptions) {
|
|
3
|
-
if (twilioOptions.active !== false) {
|
|
4
|
-
const twilioClient = Twilio(twilioOptions.accountSid, twilioOptions.authToken);
|
|
5
|
-
fastify.decorate("twilio", {
|
|
6
|
-
async sendSMS(to, body) {
|
|
7
|
-
return await twilioClient.messages.create({
|
|
8
|
-
body,
|
|
9
|
-
to,
|
|
10
|
-
from: twilioOptions.phoneNumber,
|
|
11
|
-
});
|
|
12
|
-
},
|
|
13
|
-
});
|
|
14
|
-
console.info(" ✅ Twilio Enabled");
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=twilio.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"twilio.js","sourceRoot":"","sources":["../../src/integrations/twilio.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAAY,EAAE,aAAkB;IAChE,IAAI,aAAa,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QACnC,MAAM,YAAY,GAAG,MAAM,CACzB,aAAa,CAAC,UAAU,EACxB,aAAa,CAAC,SAAS,CACxB,CAAC;QACF,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE;YACzB,KAAK,CAAC,OAAO,CAAC,EAAU,EAAE,IAAY;gBACpC,OAAO,MAAM,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC;oBACxC,IAAI;oBACJ,EAAE;oBACF,IAAI,EAAE,aAAa,CAAC,WAAW;iBAChC,CAAC,CAAC;YACL,CAAC;SACF,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;IACtC,CAAC;AACH,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export async function setupBugsnag(fastify, options) {
|
|
2
|
-
if (options.active !== false && options.apiKey) {
|
|
3
|
-
await fastify.register(import("fastify-bugsnag"), {
|
|
4
|
-
apiKey: options.apiKey,
|
|
5
|
-
});
|
|
6
|
-
console.info(" ✅ Bugsnag Enabled");
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
//# sourceMappingURL=bugsnag.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bugsnag.js","sourceRoot":"","sources":["../../src/middleware/bugsnag.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,OAAwB,EAAE,OAAY;IACvE,IAAI,OAAO,CAAC,MAAM,KAAK,KAAK,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QAC/C,MAAM,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,iBAAiB,CAAC,EAAE;YAChD,MAAM,EAAE,OAAO,CAAC,MAAM;SACvB,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;IACvC,CAAC;AACH,CAAC"}
|
package/dist/middleware/cors.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export async function setupCors(fastify, options) {
|
|
2
|
-
if (options.active !== false) {
|
|
3
|
-
await fastify.register(import("@fastify/cors"), {
|
|
4
|
-
origin: options.origin || ["https://getx.io", "http://localhost:3000"],
|
|
5
|
-
credentials: options.credentials !== undefined ? options.credentials : true,
|
|
6
|
-
methods: options.methods || ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
7
|
-
});
|
|
8
|
-
console.info(" ✅ CORS Enabled");
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=cors.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cors.js","sourceRoot":"","sources":["../../src/middleware/cors.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,OAAwB,EAAE,OAAY;IACpE,IAAI,OAAO,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QAC7B,MAAM,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE;YAC9C,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,CAAC,iBAAiB,EAAE,uBAAuB,CAAC;YACtE,WAAW,EACT,OAAO,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI;YAChE,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,CAAC;SACxE,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;IACpC,CAAC;AACH,CAAC"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export async function setupErrorHandler(fastify, fancyErrors) {
|
|
2
|
-
if (fancyErrors !== false) {
|
|
3
|
-
fastify.setErrorHandler((error, request, reply) => {
|
|
4
|
-
const statusCode = error.statusCode || 500;
|
|
5
|
-
const response = {
|
|
6
|
-
status: statusCode,
|
|
7
|
-
message: error.message || "Internal Server Error",
|
|
8
|
-
stack: process.env.NODE_ENV === "development" ? error.stack : undefined,
|
|
9
|
-
};
|
|
10
|
-
// Optional Bugsnag error reporting
|
|
11
|
-
if (fastify.bugsnag)
|
|
12
|
-
fastify.bugsnag.notify(error);
|
|
13
|
-
fastify.log.error(response);
|
|
14
|
-
reply.status(statusCode).send(response);
|
|
15
|
-
});
|
|
16
|
-
console.info(" ✅ Fancy Errors Enabled");
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=errorHandler.js.map
|