@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.
Files changed (99) hide show
  1. package/README.md +171 -18
  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 +39 -33
  51. package/server/app.js +78 -0
  52. package/src/auth/admin.js +181 -0
  53. package/src/auth/portal.js +177 -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 +58 -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 +117 -0
  74. package/test/index.js +17 -0
  75. package/ts-reference/integrations/cloudinary.ts +26 -0
  76. package/ts-reference/integrations/prisma.ts +13 -0
  77. package/ts-reference/integrations/sendgrid.ts +27 -0
  78. package/ts-reference/integrations/stripe.ts +15 -0
  79. package/ts-reference/integrations/twilio.ts +20 -0
  80. package/ts-reference/middleware/bugsnag.ts +10 -0
  81. package/ts-reference/middleware/cors.ts +13 -0
  82. package/ts-reference/middleware/errorHandler.ts +24 -0
  83. package/ts-reference/middleware/multipart.ts +8 -0
  84. package/ts-reference/middleware/rateLimit.ts +8 -0
  85. package/ts-reference/middleware/underPressure.ts +11 -0
  86. package/ts-reference/utils/colorize.ts +45 -0
  87. package/ts-reference/utils/formatBytes.ts +8 -0
  88. package/ts-reference/utils/randomUUID.ts +3 -0
  89. package/ts-reference/utils/statAsync.ts +4 -0
  90. package/tsconfig.json +13 -8
  91. package/xConfigReference.js +1526 -0
  92. package/xConfigWorkingList.js +720 -0
  93. package/.github/workflows/ci.yml +0 -19
  94. package/.taprc +0 -3
  95. package/index.d.ts +0 -13
  96. package/index.js +0 -9
  97. package/test/index.test-d.ts +0 -13
  98. package/test/index.test.js +0 -14
  99. package/test/xConfig.js +0 -115
@@ -1,19 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
1
- ts: false
2
- jsx: false
3
- coverage: false
package/index.d.ts DELETED
@@ -1,13 +0,0 @@
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 DELETED
@@ -1,9 +0,0 @@
1
- 'use strict'
2
-
3
- const fp = require('fastify-plugin')
4
-
5
- module.exports = fp(async function (fastify, opts) {
6
- fastify.decorate('exampleDecorator', () => {
7
- return 'decorated'
8
- })
9
- }, { fastify: '^4.x' })
@@ -1,13 +0,0 @@
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
- }
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,115 +0,0 @@
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
- });